2 * drivers/dma/imx-sdma.c
4 * This file contains a driver for the Freescale Smart DMA engine
6 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8 * Based on code from Freescale:
10 * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
12 * The code contained herein is licensed under the GNU General Public
13 * License. You may obtain a copy of the GNU General Public License
14 * Version 2 or later at the following locations:
16 * http://www.opensource.org/licenses/gpl-license.html
17 * http://www.gnu.org/copyleft/gpl.html
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
24 #include <linux/interrupt.h>
25 #include <linux/clk.h>
26 #include <linux/wait.h>
27 #include <linux/sched.h>
28 #include <linux/semaphore.h>
29 #include <linux/spinlock.h>
30 #include <linux/device.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/firmware.h>
33 #include <linux/slab.h>
34 #include <linux/platform_device.h>
35 #include <linux/dmaengine.h>
37 #include <linux/of_device.h>
40 #include <mach/sdma.h>
42 #include <mach/hardware.h>
45 #define SDMA_H_C0PTR 0x000
46 #define SDMA_H_INTR 0x004
47 #define SDMA_H_STATSTOP 0x008
48 #define SDMA_H_START 0x00c
49 #define SDMA_H_EVTOVR 0x010
50 #define SDMA_H_DSPOVR 0x014
51 #define SDMA_H_HOSTOVR 0x018
52 #define SDMA_H_EVTPEND 0x01c
53 #define SDMA_H_DSPENBL 0x020
54 #define SDMA_H_RESET 0x024
55 #define SDMA_H_EVTERR 0x028
56 #define SDMA_H_INTRMSK 0x02c
57 #define SDMA_H_PSW 0x030
58 #define SDMA_H_EVTERRDBG 0x034
59 #define SDMA_H_CONFIG 0x038
60 #define SDMA_ONCE_ENB 0x040
61 #define SDMA_ONCE_DATA 0x044
62 #define SDMA_ONCE_INSTR 0x048
63 #define SDMA_ONCE_STAT 0x04c
64 #define SDMA_ONCE_CMD 0x050
65 #define SDMA_EVT_MIRROR 0x054
66 #define SDMA_ILLINSTADDR 0x058
67 #define SDMA_CHN0ADDR 0x05c
68 #define SDMA_ONCE_RTB 0x060
69 #define SDMA_XTRIG_CONF1 0x070
70 #define SDMA_XTRIG_CONF2 0x074
71 #define SDMA_CHNENBL0_IMX35 0x200
72 #define SDMA_CHNENBL0_IMX31 0x080
73 #define SDMA_CHNPRI_0 0x100
76 * Buffer descriptor status values.
87 * Data Node descriptor status values.
89 #define DND_END_OF_FRAME 0x80
90 #define DND_END_OF_XFER 0x40
92 #define DND_UNUSED 0x01
95 * IPCV2 descriptor status values.
97 #define BD_IPCV2_END_OF_FRAME 0x40
99 #define IPCV2_MAX_NODES 50
101 * Error bit set in the CCB status field by the SDMA,
102 * in setbd routine, in case of a transfer error
104 #define DATA_ERROR 0x10000000
107 * Buffer descriptor commands.
112 #define C0_SETCTX 0x07
113 #define C0_GETCTX 0x03
114 #define C0_SETDM 0x01
115 #define C0_SETPM 0x04
116 #define C0_GETDM 0x02
117 #define C0_GETPM 0x08
119 * Change endianness indicator in the BD command field
121 #define CHANGE_ENDIANNESS 0x80
124 * Mode/Count of data node descriptors - IPCv2
126 struct sdma_mode_count {
127 u32 count : 16; /* size of the buffer pointed by this BD */
128 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
129 u32 command : 8; /* command mostlky used for channel 0 */
135 struct sdma_buffer_descriptor {
136 struct sdma_mode_count mode;
137 u32 buffer_addr; /* address of the buffer described */
138 u32 ext_buffer_addr; /* extended buffer address */
139 } __attribute__ ((packed));
142 * struct sdma_channel_control - Channel control Block
144 * @current_bd_ptr current buffer descriptor processed
145 * @base_bd_ptr first element of buffer descriptor array
146 * @unused padding. The SDMA engine expects an array of 128 byte
149 struct sdma_channel_control {
153 } __attribute__ ((packed));
156 * struct sdma_state_registers - SDMA context for a channel
158 * @pc: program counter
159 * @t: test bit: status of arithmetic & test instruction
160 * @rpc: return program counter
161 * @sf: source fault while loading data
162 * @spc: loop start program counter
163 * @df: destination fault while storing data
164 * @epc: loop end program counter
167 struct sdma_state_registers {
179 } __attribute__ ((packed));
182 * struct sdma_context_data - sdma context specific to a channel
184 * @channel_state: channel state bits
185 * @gReg: general registers
186 * @mda: burst dma destination address register
187 * @msa: burst dma source address register
188 * @ms: burst dma status register
189 * @md: burst dma data register
190 * @pda: peripheral dma destination address register
191 * @psa: peripheral dma source address register
192 * @ps: peripheral dma status register
193 * @pd: peripheral dma data register
194 * @ca: CRC polynomial register
195 * @cs: CRC accumulator register
196 * @dda: dedicated core destination address register
197 * @dsa: dedicated core source address register
198 * @ds: dedicated core status register
199 * @dd: dedicated core data register
201 struct sdma_context_data {
202 struct sdma_state_registers channel_state;
226 } __attribute__ ((packed));
228 #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
233 * struct sdma_channel - housekeeping for a SDMA channel
235 * @sdma pointer to the SDMA engine for this channel
236 * @channel the channel number, matches dmaengine chan_id + 1
237 * @direction transfer type. Needed for setting SDMA script
238 * @peripheral_type Peripheral type. Needed for setting SDMA script
239 * @event_id0 aka dma request line
240 * @event_id1 for channels that use 2 events
241 * @word_size peripheral access size
242 * @buf_tail ID of the buffer that was processed
243 * @done channel completion
244 * @num_bd max NUM_BD. number of descriptors currently handling
246 struct sdma_channel {
247 struct sdma_engine *sdma;
248 unsigned int channel;
249 enum dma_data_direction direction;
250 enum sdma_peripheral_type peripheral_type;
251 unsigned int event_id0;
252 unsigned int event_id1;
253 enum dma_slave_buswidth word_size;
254 unsigned int buf_tail;
255 struct completion done;
257 struct sdma_buffer_descriptor *bd;
259 unsigned int pc_from_device, pc_to_device;
261 dma_addr_t per_address;
262 u32 event_mask0, event_mask1;
264 u32 shp_addr, per_addr;
265 struct dma_chan chan;
267 struct dma_async_tx_descriptor desc;
268 dma_cookie_t last_completed;
269 enum dma_status status;
272 #define IMX_DMA_SG_LOOP (1 << 0)
274 #define MAX_DMA_CHANNELS 32
275 #define MXC_SDMA_DEFAULT_PRIORITY 1
276 #define MXC_SDMA_MIN_PRIORITY 1
277 #define MXC_SDMA_MAX_PRIORITY 7
279 #define SDMA_FIRMWARE_MAGIC 0x414d4453
282 * struct sdma_firmware_header - Layout of the firmware image
285 * @version_major increased whenever layout of struct sdma_script_start_addrs
287 * @version_minor firmware minor version (for binary compatible changes)
288 * @script_addrs_start offset of struct sdma_script_start_addrs in this image
289 * @num_script_addrs Number of script addresses in this image
290 * @ram_code_start offset of SDMA ram image in this firmware image
291 * @ram_code_size size of SDMA ram image
292 * @script_addrs Stores the start address of the SDMA scripts
293 * (in SDMA memory space)
295 struct sdma_firmware_header {
299 u32 script_addrs_start;
300 u32 num_script_addrs;
306 IMX31_SDMA, /* runs on i.mx31 */
307 IMX35_SDMA, /* runs on i.mx35 and later */
312 struct device_dma_parameters dma_parms;
313 struct sdma_channel channel[MAX_DMA_CHANNELS];
314 struct sdma_channel_control *channel_control;
316 enum sdma_devtype devtype;
317 unsigned int num_events;
318 struct sdma_context_data *context;
319 dma_addr_t context_phys;
320 struct dma_device dma_device;
322 struct mutex channel_0_lock;
323 struct sdma_script_start_addrs *script_addrs;
326 static struct platform_device_id sdma_devtypes[] = {
328 .name = "imx31-sdma",
329 .driver_data = IMX31_SDMA,
331 .name = "imx35-sdma",
332 .driver_data = IMX35_SDMA,
337 MODULE_DEVICE_TABLE(platform, sdma_devtypes);
339 static const struct of_device_id sdma_dt_ids[] = {
340 { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], },
341 { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], },
344 MODULE_DEVICE_TABLE(of, sdma_dt_ids);
346 #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
347 #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
348 #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
349 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
351 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
353 u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
354 SDMA_CHNENBL0_IMX35);
355 return chnenbl0 + event * 4;
358 static int sdma_config_ownership(struct sdma_channel *sdmac,
359 bool event_override, bool mcu_override, bool dsp_override)
361 struct sdma_engine *sdma = sdmac->sdma;
362 int channel = sdmac->channel;
365 if (event_override && mcu_override && dsp_override)
368 evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
369 mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
370 dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
373 dsp &= ~(1 << channel);
375 dsp |= (1 << channel);
378 evt &= ~(1 << channel);
380 evt |= (1 << channel);
383 mcu &= ~(1 << channel);
385 mcu |= (1 << channel);
387 __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
388 __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
389 __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
395 * sdma_run_channel - run a channel and wait till it's done
397 static int sdma_run_channel(struct sdma_channel *sdmac)
399 struct sdma_engine *sdma = sdmac->sdma;
400 int channel = sdmac->channel;
403 init_completion(&sdmac->done);
405 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
407 ret = wait_for_completion_timeout(&sdmac->done, HZ);
409 return ret ? 0 : -ETIMEDOUT;
412 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
415 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
420 mutex_lock(&sdma->channel_0_lock);
422 buf_virt = dma_alloc_coherent(NULL,
424 &buf_phys, GFP_KERNEL);
430 bd0->mode.command = C0_SETPM;
431 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
432 bd0->mode.count = size / 2;
433 bd0->buffer_addr = buf_phys;
434 bd0->ext_buffer_addr = address;
436 memcpy(buf_virt, buf, size);
438 ret = sdma_run_channel(&sdma->channel[0]);
440 dma_free_coherent(NULL, size, buf_virt, buf_phys);
443 mutex_unlock(&sdma->channel_0_lock);
448 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
450 struct sdma_engine *sdma = sdmac->sdma;
451 int channel = sdmac->channel;
453 u32 chnenbl = chnenbl_ofs(sdma, event);
455 val = __raw_readl(sdma->regs + chnenbl);
456 val |= (1 << channel);
457 __raw_writel(val, sdma->regs + chnenbl);
460 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
462 struct sdma_engine *sdma = sdmac->sdma;
463 int channel = sdmac->channel;
464 u32 chnenbl = chnenbl_ofs(sdma, event);
467 val = __raw_readl(sdma->regs + chnenbl);
468 val &= ~(1 << channel);
469 __raw_writel(val, sdma->regs + chnenbl);
472 static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
474 struct sdma_buffer_descriptor *bd;
477 * loop mode. Iterate over descriptors, re-setup them and
478 * call callback function.
481 bd = &sdmac->bd[sdmac->buf_tail];
483 if (bd->mode.status & BD_DONE)
486 if (bd->mode.status & BD_RROR)
487 sdmac->status = DMA_ERROR;
489 sdmac->status = DMA_IN_PROGRESS;
491 bd->mode.status |= BD_DONE;
493 sdmac->buf_tail %= sdmac->num_bd;
495 if (sdmac->desc.callback)
496 sdmac->desc.callback(sdmac->desc.callback_param);
500 static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
502 struct sdma_buffer_descriptor *bd;
506 * non loop mode. Iterate over all descriptors, collect
507 * errors and call callback function
509 for (i = 0; i < sdmac->num_bd; i++) {
512 if (bd->mode.status & (BD_DONE | BD_RROR))
517 sdmac->status = DMA_ERROR;
519 sdmac->status = DMA_SUCCESS;
521 if (sdmac->desc.callback)
522 sdmac->desc.callback(sdmac->desc.callback_param);
523 sdmac->last_completed = sdmac->desc.cookie;
526 static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
528 complete(&sdmac->done);
530 /* not interested in channel 0 interrupts */
531 if (sdmac->channel == 0)
534 if (sdmac->flags & IMX_DMA_SG_LOOP)
535 sdma_handle_channel_loop(sdmac);
537 mxc_sdma_handle_channel_normal(sdmac);
540 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
542 struct sdma_engine *sdma = dev_id;
545 stat = __raw_readl(sdma->regs + SDMA_H_INTR);
546 __raw_writel(stat, sdma->regs + SDMA_H_INTR);
549 int channel = fls(stat) - 1;
550 struct sdma_channel *sdmac = &sdma->channel[channel];
552 mxc_sdma_handle_channel(sdmac);
554 stat &= ~(1 << channel);
561 * sets the pc of SDMA script according to the peripheral type
563 static void sdma_get_pc(struct sdma_channel *sdmac,
564 enum sdma_peripheral_type peripheral_type)
566 struct sdma_engine *sdma = sdmac->sdma;
567 int per_2_emi = 0, emi_2_per = 0;
569 * These are needed once we start to support transfers between
570 * two peripherals or memory-to-memory transfers
572 int per_2_per = 0, emi_2_emi = 0;
574 sdmac->pc_from_device = 0;
575 sdmac->pc_to_device = 0;
577 switch (peripheral_type) {
578 case IMX_DMATYPE_MEMORY:
579 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
581 case IMX_DMATYPE_DSP:
582 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
583 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
585 case IMX_DMATYPE_FIRI:
586 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
587 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
589 case IMX_DMATYPE_UART:
590 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
591 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
593 case IMX_DMATYPE_UART_SP:
594 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
595 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
597 case IMX_DMATYPE_ATA:
598 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
599 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
601 case IMX_DMATYPE_CSPI:
602 case IMX_DMATYPE_EXT:
603 case IMX_DMATYPE_SSI:
604 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
605 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
607 case IMX_DMATYPE_SSI_SP:
608 case IMX_DMATYPE_MMC:
609 case IMX_DMATYPE_SDHC:
610 case IMX_DMATYPE_CSPI_SP:
611 case IMX_DMATYPE_ESAI:
612 case IMX_DMATYPE_MSHC_SP:
613 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
614 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
616 case IMX_DMATYPE_ASRC:
617 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
618 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
619 per_2_per = sdma->script_addrs->per_2_per_addr;
621 case IMX_DMATYPE_MSHC:
622 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
623 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
625 case IMX_DMATYPE_CCM:
626 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
628 case IMX_DMATYPE_SPDIF:
629 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
630 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
632 case IMX_DMATYPE_IPU_MEMORY:
633 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
639 sdmac->pc_from_device = per_2_emi;
640 sdmac->pc_to_device = emi_2_per;
643 static int sdma_load_context(struct sdma_channel *sdmac)
645 struct sdma_engine *sdma = sdmac->sdma;
646 int channel = sdmac->channel;
648 struct sdma_context_data *context = sdma->context;
649 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
652 if (sdmac->direction == DMA_FROM_DEVICE) {
653 load_address = sdmac->pc_from_device;
655 load_address = sdmac->pc_to_device;
658 if (load_address < 0)
661 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
662 dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
663 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
664 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
665 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
666 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
668 mutex_lock(&sdma->channel_0_lock);
670 memset(context, 0, sizeof(*context));
671 context->channel_state.pc = load_address;
673 /* Send by context the event mask,base address for peripheral
674 * and watermark level
676 context->gReg[0] = sdmac->event_mask1;
677 context->gReg[1] = sdmac->event_mask0;
678 context->gReg[2] = sdmac->per_addr;
679 context->gReg[6] = sdmac->shp_addr;
680 context->gReg[7] = sdmac->watermark_level;
682 bd0->mode.command = C0_SETDM;
683 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
684 bd0->mode.count = sizeof(*context) / 4;
685 bd0->buffer_addr = sdma->context_phys;
686 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
688 ret = sdma_run_channel(&sdma->channel[0]);
690 mutex_unlock(&sdma->channel_0_lock);
695 static void sdma_disable_channel(struct sdma_channel *sdmac)
697 struct sdma_engine *sdma = sdmac->sdma;
698 int channel = sdmac->channel;
700 __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
701 sdmac->status = DMA_ERROR;
704 static int sdma_config_channel(struct sdma_channel *sdmac)
708 sdma_disable_channel(sdmac);
710 sdmac->event_mask0 = 0;
711 sdmac->event_mask1 = 0;
715 if (sdmac->event_id0) {
716 if (sdmac->event_id0 > 32)
718 sdma_event_enable(sdmac, sdmac->event_id0);
721 switch (sdmac->peripheral_type) {
722 case IMX_DMATYPE_DSP:
723 sdma_config_ownership(sdmac, false, true, true);
725 case IMX_DMATYPE_MEMORY:
726 sdma_config_ownership(sdmac, false, true, false);
729 sdma_config_ownership(sdmac, true, true, false);
733 sdma_get_pc(sdmac, sdmac->peripheral_type);
735 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
736 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
737 /* Handle multiple event channels differently */
738 if (sdmac->event_id1) {
739 sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
740 if (sdmac->event_id1 > 31)
741 sdmac->watermark_level |= 1 << 31;
742 sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
743 if (sdmac->event_id0 > 31)
744 sdmac->watermark_level |= 1 << 30;
746 sdmac->event_mask0 = 1 << sdmac->event_id0;
747 sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
749 /* Watermark Level */
750 sdmac->watermark_level |= sdmac->watermark_level;
752 sdmac->shp_addr = sdmac->per_address;
754 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
757 ret = sdma_load_context(sdmac);
762 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
763 unsigned int priority)
765 struct sdma_engine *sdma = sdmac->sdma;
766 int channel = sdmac->channel;
768 if (priority < MXC_SDMA_MIN_PRIORITY
769 || priority > MXC_SDMA_MAX_PRIORITY) {
773 __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
778 static int sdma_request_channel(struct sdma_channel *sdmac)
780 struct sdma_engine *sdma = sdmac->sdma;
781 int channel = sdmac->channel;
784 sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
790 memset(sdmac->bd, 0, PAGE_SIZE);
792 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
793 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
795 clk_enable(sdma->clk);
797 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
799 init_completion(&sdmac->done);
809 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
811 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
814 static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac)
816 dma_cookie_t cookie = sdmac->chan.cookie;
821 sdmac->chan.cookie = cookie;
822 sdmac->desc.cookie = cookie;
827 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
829 return container_of(chan, struct sdma_channel, chan);
832 static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
834 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
835 struct sdma_engine *sdma = sdmac->sdma;
838 spin_lock_irq(&sdmac->lock);
840 cookie = sdma_assign_cookie(sdmac);
842 sdma_enable_channel(sdma, sdmac->channel);
844 spin_unlock_irq(&sdmac->lock);
849 static int sdma_alloc_chan_resources(struct dma_chan *chan)
851 struct sdma_channel *sdmac = to_sdma_chan(chan);
852 struct imx_dma_data *data = chan->private;
858 switch (data->priority) {
862 case DMA_PRIO_MEDIUM:
871 sdmac->peripheral_type = data->peripheral_type;
872 sdmac->event_id0 = data->dma_request;
873 ret = sdma_set_channel_priority(sdmac, prio);
877 ret = sdma_request_channel(sdmac);
881 dma_async_tx_descriptor_init(&sdmac->desc, chan);
882 sdmac->desc.tx_submit = sdma_tx_submit;
883 /* txd.flags will be overwritten in prep funcs */
884 sdmac->desc.flags = DMA_CTRL_ACK;
889 static void sdma_free_chan_resources(struct dma_chan *chan)
891 struct sdma_channel *sdmac = to_sdma_chan(chan);
892 struct sdma_engine *sdma = sdmac->sdma;
894 sdma_disable_channel(sdmac);
896 if (sdmac->event_id0)
897 sdma_event_disable(sdmac, sdmac->event_id0);
898 if (sdmac->event_id1)
899 sdma_event_disable(sdmac, sdmac->event_id1);
901 sdmac->event_id0 = 0;
902 sdmac->event_id1 = 0;
904 sdma_set_channel_priority(sdmac, 0);
906 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
908 clk_disable(sdma->clk);
911 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
912 struct dma_chan *chan, struct scatterlist *sgl,
913 unsigned int sg_len, enum dma_data_direction direction,
916 struct sdma_channel *sdmac = to_sdma_chan(chan);
917 struct sdma_engine *sdma = sdmac->sdma;
919 int channel = sdmac->channel;
920 struct scatterlist *sg;
922 if (sdmac->status == DMA_IN_PROGRESS)
924 sdmac->status = DMA_IN_PROGRESS;
928 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
931 sdmac->direction = direction;
932 ret = sdma_load_context(sdmac);
936 if (sg_len > NUM_BD) {
937 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
938 channel, sg_len, NUM_BD);
943 for_each_sg(sgl, sg, sg_len, i) {
944 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
947 bd->buffer_addr = sg->dma_address;
951 if (count > 0xffff) {
952 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
953 channel, count, 0xffff);
958 bd->mode.count = count;
960 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
965 switch (sdmac->word_size) {
966 case DMA_SLAVE_BUSWIDTH_4_BYTES:
967 bd->mode.command = 0;
968 if (count & 3 || sg->dma_address & 3)
971 case DMA_SLAVE_BUSWIDTH_2_BYTES:
972 bd->mode.command = 2;
973 if (count & 1 || sg->dma_address & 1)
976 case DMA_SLAVE_BUSWIDTH_1_BYTE:
977 bd->mode.command = 1;
983 param = BD_DONE | BD_EXTD | BD_CONT;
985 if (i + 1 == sg_len) {
991 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
992 i, count, sg->dma_address,
993 param & BD_WRAP ? "wrap" : "",
994 param & BD_INTR ? " intr" : "");
996 bd->mode.status = param;
999 sdmac->num_bd = sg_len;
1000 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1002 return &sdmac->desc;
1004 sdmac->status = DMA_ERROR;
1008 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1009 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1010 size_t period_len, enum dma_data_direction direction)
1012 struct sdma_channel *sdmac = to_sdma_chan(chan);
1013 struct sdma_engine *sdma = sdmac->sdma;
1014 int num_periods = buf_len / period_len;
1015 int channel = sdmac->channel;
1016 int ret, i = 0, buf = 0;
1018 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1020 if (sdmac->status == DMA_IN_PROGRESS)
1023 sdmac->status = DMA_IN_PROGRESS;
1025 sdmac->flags |= IMX_DMA_SG_LOOP;
1026 sdmac->direction = direction;
1027 ret = sdma_load_context(sdmac);
1031 if (num_periods > NUM_BD) {
1032 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1033 channel, num_periods, NUM_BD);
1037 if (period_len > 0xffff) {
1038 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
1039 channel, period_len, 0xffff);
1043 while (buf < buf_len) {
1044 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1047 bd->buffer_addr = dma_addr;
1049 bd->mode.count = period_len;
1051 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1053 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1054 bd->mode.command = 0;
1056 bd->mode.command = sdmac->word_size;
1058 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1059 if (i + 1 == num_periods)
1062 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
1063 i, period_len, dma_addr,
1064 param & BD_WRAP ? "wrap" : "",
1065 param & BD_INTR ? " intr" : "");
1067 bd->mode.status = param;
1069 dma_addr += period_len;
1075 sdmac->num_bd = num_periods;
1076 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1078 return &sdmac->desc;
1080 sdmac->status = DMA_ERROR;
1084 static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1087 struct sdma_channel *sdmac = to_sdma_chan(chan);
1088 struct dma_slave_config *dmaengine_cfg = (void *)arg;
1091 case DMA_TERMINATE_ALL:
1092 sdma_disable_channel(sdmac);
1094 case DMA_SLAVE_CONFIG:
1095 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
1096 sdmac->per_address = dmaengine_cfg->src_addr;
1097 sdmac->watermark_level = dmaengine_cfg->src_maxburst;
1098 sdmac->word_size = dmaengine_cfg->src_addr_width;
1100 sdmac->per_address = dmaengine_cfg->dst_addr;
1101 sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
1102 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1104 return sdma_config_channel(sdmac);
1112 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1113 dma_cookie_t cookie,
1114 struct dma_tx_state *txstate)
1116 struct sdma_channel *sdmac = to_sdma_chan(chan);
1117 dma_cookie_t last_used;
1119 last_used = chan->cookie;
1121 dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
1123 return sdmac->status;
1126 static void sdma_issue_pending(struct dma_chan *chan)
1129 * Nothing to do. We only have a single descriptor
1133 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1135 static void sdma_add_scripts(struct sdma_engine *sdma,
1136 const struct sdma_script_start_addrs *addr)
1138 s32 *addr_arr = (u32 *)addr;
1139 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1142 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1143 if (addr_arr[i] > 0)
1144 saddr_arr[i] = addr_arr[i];
1147 static void sdma_load_firmware(const struct firmware *fw, void *context)
1149 struct sdma_engine *sdma = context;
1150 const struct sdma_firmware_header *header;
1151 const struct sdma_script_start_addrs *addr;
1152 unsigned short *ram_code;
1155 dev_err(sdma->dev, "firmware not found\n");
1159 if (fw->size < sizeof(*header))
1162 header = (struct sdma_firmware_header *)fw->data;
1164 if (header->magic != SDMA_FIRMWARE_MAGIC)
1166 if (header->ram_code_start + header->ram_code_size > fw->size)
1169 addr = (void *)header + header->script_addrs_start;
1170 ram_code = (void *)header + header->ram_code_start;
1172 clk_enable(sdma->clk);
1173 /* download the RAM image for SDMA */
1174 sdma_load_script(sdma, ram_code,
1175 header->ram_code_size,
1176 addr->ram_code_start_addr);
1177 clk_disable(sdma->clk);
1179 sdma_add_scripts(sdma, addr);
1181 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1182 header->version_major,
1183 header->version_minor);
1186 release_firmware(fw);
1189 static int __init sdma_get_firmware(struct sdma_engine *sdma,
1190 const char *fw_name)
1194 ret = request_firmware_nowait(THIS_MODULE,
1195 FW_ACTION_HOTPLUG, fw_name, sdma->dev,
1196 GFP_KERNEL, sdma, sdma_load_firmware);
1201 static int __init sdma_init(struct sdma_engine *sdma)
1204 dma_addr_t ccb_phys;
1206 switch (sdma->devtype) {
1208 sdma->num_events = 32;
1211 sdma->num_events = 48;
1214 dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
1219 clk_enable(sdma->clk);
1221 /* Be sure SDMA has not started yet */
1222 __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
1224 sdma->channel_control = dma_alloc_coherent(NULL,
1225 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1226 sizeof(struct sdma_context_data),
1227 &ccb_phys, GFP_KERNEL);
1229 if (!sdma->channel_control) {
1234 sdma->context = (void *)sdma->channel_control +
1235 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1236 sdma->context_phys = ccb_phys +
1237 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1239 /* Zero-out the CCB structures array just allocated */
1240 memset(sdma->channel_control, 0,
1241 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1243 /* disable all channels */
1244 for (i = 0; i < sdma->num_events; i++)
1245 __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
1247 /* All channels have priority 0 */
1248 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1249 __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1251 ret = sdma_request_channel(&sdma->channel[0]);
1255 sdma_config_ownership(&sdma->channel[0], false, true, false);
1257 /* Set Command Channel (Channel Zero) */
1258 __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
1260 /* Set bits of CONFIG register but with static context switching */
1261 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1262 __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
1264 __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1266 /* Set bits of CONFIG register with given context switching mode */
1267 __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
1269 /* Initializes channel's priorities */
1270 sdma_set_channel_priority(&sdma->channel[0], 7);
1272 clk_disable(sdma->clk);
1277 clk_disable(sdma->clk);
1278 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1282 static int __init sdma_probe(struct platform_device *pdev)
1284 const struct of_device_id *of_id =
1285 of_match_device(sdma_dt_ids, &pdev->dev);
1286 struct device_node *np = pdev->dev.of_node;
1287 const char *fw_name;
1290 struct resource *iores;
1291 struct sdma_platform_data *pdata = pdev->dev.platform_data;
1293 struct sdma_engine *sdma;
1296 sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
1300 mutex_init(&sdma->channel_0_lock);
1302 sdma->dev = &pdev->dev;
1304 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1305 irq = platform_get_irq(pdev, 0);
1306 if (!iores || irq < 0) {
1311 if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
1313 goto err_request_region;
1316 sdma->clk = clk_get(&pdev->dev, NULL);
1317 if (IS_ERR(sdma->clk)) {
1318 ret = PTR_ERR(sdma->clk);
1322 sdma->regs = ioremap(iores->start, resource_size(iores));
1328 ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
1330 goto err_request_irq;
1332 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
1333 if (!sdma->script_addrs) {
1338 /* initially no scripts available */
1339 saddr_arr = (s32 *)sdma->script_addrs;
1340 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1341 saddr_arr[i] = -EINVAL;
1344 pdev->id_entry = of_id->data;
1345 sdma->devtype = pdev->id_entry->driver_data;
1347 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1348 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1350 INIT_LIST_HEAD(&sdma->dma_device.channels);
1351 /* Initialize channel parameters */
1352 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1353 struct sdma_channel *sdmac = &sdma->channel[i];
1356 spin_lock_init(&sdmac->lock);
1358 sdmac->chan.device = &sdma->dma_device;
1362 * Add the channel to the DMAC list. Do not add channel 0 though
1363 * because we need it internally in the SDMA driver. This also means
1364 * that channel 0 in dmaengine counting matches sdma channel 1.
1367 list_add_tail(&sdmac->chan.device_node,
1368 &sdma->dma_device.channels);
1371 ret = sdma_init(sdma);
1375 if (pdata && pdata->script_addrs)
1376 sdma_add_scripts(sdma, pdata->script_addrs);
1379 sdma_get_firmware(sdma, pdata->fw_name);
1382 * Because that device tree does not encode ROM script address,
1383 * the RAM script in firmware is mandatory for device tree
1384 * probe, otherwise it fails.
1386 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1389 dev_err(&pdev->dev, "failed to get firmware name\n");
1393 ret = sdma_get_firmware(sdma, fw_name);
1395 dev_err(&pdev->dev, "failed to get firmware\n");
1400 sdma->dma_device.dev = &pdev->dev;
1402 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1403 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1404 sdma->dma_device.device_tx_status = sdma_tx_status;
1405 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1406 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1407 sdma->dma_device.device_control = sdma_control;
1408 sdma->dma_device.device_issue_pending = sdma_issue_pending;
1409 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1410 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
1412 ret = dma_async_device_register(&sdma->dma_device);
1414 dev_err(&pdev->dev, "unable to register\n");
1418 dev_info(sdma->dev, "initialized\n");
1423 kfree(sdma->script_addrs);
1425 free_irq(irq, sdma);
1427 iounmap(sdma->regs);
1431 release_mem_region(iores->start, resource_size(iores));
1438 static int __exit sdma_remove(struct platform_device *pdev)
1443 static struct platform_driver sdma_driver = {
1446 .of_match_table = sdma_dt_ids,
1448 .id_table = sdma_devtypes,
1449 .remove = __exit_p(sdma_remove),
1452 static int __init sdma_module_init(void)
1454 return platform_driver_probe(&sdma_driver, sdma_probe);
1456 module_init(sdma_module_init);
1458 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1459 MODULE_DESCRIPTION("i.MX SDMA driver");
1460 MODULE_LICENSE("GPL");