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/types.h>
23 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/wait.h>
26 #include <linux/sched.h>
27 #include <linux/semaphore.h>
28 #include <linux/spinlock.h>
29 #include <linux/device.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <linux/platform_device.h>
34 #include <linux/dmaengine.h>
37 #include <mach/sdma.h>
39 #include <mach/hardware.h>
42 #define SDMA_H_C0PTR 0x000
43 #define SDMA_H_INTR 0x004
44 #define SDMA_H_STATSTOP 0x008
45 #define SDMA_H_START 0x00c
46 #define SDMA_H_EVTOVR 0x010
47 #define SDMA_H_DSPOVR 0x014
48 #define SDMA_H_HOSTOVR 0x018
49 #define SDMA_H_EVTPEND 0x01c
50 #define SDMA_H_DSPENBL 0x020
51 #define SDMA_H_RESET 0x024
52 #define SDMA_H_EVTERR 0x028
53 #define SDMA_H_INTRMSK 0x02c
54 #define SDMA_H_PSW 0x030
55 #define SDMA_H_EVTERRDBG 0x034
56 #define SDMA_H_CONFIG 0x038
57 #define SDMA_ONCE_ENB 0x040
58 #define SDMA_ONCE_DATA 0x044
59 #define SDMA_ONCE_INSTR 0x048
60 #define SDMA_ONCE_STAT 0x04c
61 #define SDMA_ONCE_CMD 0x050
62 #define SDMA_EVT_MIRROR 0x054
63 #define SDMA_ILLINSTADDR 0x058
64 #define SDMA_CHN0ADDR 0x05c
65 #define SDMA_ONCE_RTB 0x060
66 #define SDMA_XTRIG_CONF1 0x070
67 #define SDMA_XTRIG_CONF2 0x074
68 #define SDMA_CHNENBL0_V2 0x200
69 #define SDMA_CHNENBL0_V1 0x080
70 #define SDMA_CHNPRI_0 0x100
73 * Buffer descriptor status values.
84 * Data Node descriptor status values.
86 #define DND_END_OF_FRAME 0x80
87 #define DND_END_OF_XFER 0x40
89 #define DND_UNUSED 0x01
92 * IPCV2 descriptor status values.
94 #define BD_IPCV2_END_OF_FRAME 0x40
96 #define IPCV2_MAX_NODES 50
98 * Error bit set in the CCB status field by the SDMA,
99 * in setbd routine, in case of a transfer error
101 #define DATA_ERROR 0x10000000
104 * Buffer descriptor commands.
109 #define C0_SETCTX 0x07
110 #define C0_GETCTX 0x03
111 #define C0_SETDM 0x01
112 #define C0_SETPM 0x04
113 #define C0_GETDM 0x02
114 #define C0_GETPM 0x08
116 * Change endianness indicator in the BD command field
118 #define CHANGE_ENDIANNESS 0x80
121 * Mode/Count of data node descriptors - IPCv2
123 struct sdma_mode_count {
124 u32 count : 16; /* size of the buffer pointed by this BD */
125 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
126 u32 command : 8; /* command mostlky used for channel 0 */
132 struct sdma_buffer_descriptor {
133 struct sdma_mode_count mode;
134 u32 buffer_addr; /* address of the buffer described */
135 u32 ext_buffer_addr; /* extended buffer address */
136 } __attribute__ ((packed));
139 * struct sdma_channel_control - Channel control Block
141 * @current_bd_ptr current buffer descriptor processed
142 * @base_bd_ptr first element of buffer descriptor array
143 * @unused padding. The SDMA engine expects an array of 128 byte
146 struct sdma_channel_control {
150 } __attribute__ ((packed));
153 * struct sdma_state_registers - SDMA context for a channel
155 * @pc: program counter
156 * @t: test bit: status of arithmetic & test instruction
157 * @rpc: return program counter
158 * @sf: source fault while loading data
159 * @spc: loop start program counter
160 * @df: destination fault while storing data
161 * @epc: loop end program counter
164 struct sdma_state_registers {
176 } __attribute__ ((packed));
179 * struct sdma_context_data - sdma context specific to a channel
181 * @channel_state: channel state bits
182 * @gReg: general registers
183 * @mda: burst dma destination address register
184 * @msa: burst dma source address register
185 * @ms: burst dma status register
186 * @md: burst dma data register
187 * @pda: peripheral dma destination address register
188 * @psa: peripheral dma source address register
189 * @ps: peripheral dma status register
190 * @pd: peripheral dma data register
191 * @ca: CRC polynomial register
192 * @cs: CRC accumulator register
193 * @dda: dedicated core destination address register
194 * @dsa: dedicated core source address register
195 * @ds: dedicated core status register
196 * @dd: dedicated core data register
198 struct sdma_context_data {
199 struct sdma_state_registers channel_state;
223 } __attribute__ ((packed));
225 #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
230 * struct sdma_channel - housekeeping for a SDMA channel
232 * @sdma pointer to the SDMA engine for this channel
233 * @channel the channel number, matches dmaengine chan_id
234 * @direction transfer type. Needed for setting SDMA script
235 * @peripheral_type Peripheral type. Needed for setting SDMA script
236 * @event_id0 aka dma request line
237 * @event_id1 for channels that use 2 events
238 * @word_size peripheral access size
239 * @buf_tail ID of the buffer that was processed
240 * @done channel completion
241 * @num_bd max NUM_BD. number of descriptors currently handling
243 struct sdma_channel {
244 struct sdma_engine *sdma;
245 unsigned int channel;
246 enum dma_data_direction direction;
247 enum sdma_peripheral_type peripheral_type;
248 unsigned int event_id0;
249 unsigned int event_id1;
250 enum dma_slave_buswidth word_size;
251 unsigned int buf_tail;
252 struct completion done;
254 struct sdma_buffer_descriptor *bd;
256 unsigned int pc_from_device, pc_to_device;
258 dma_addr_t per_address;
259 u32 event_mask0, event_mask1;
261 u32 shp_addr, per_addr;
262 struct dma_chan chan;
264 struct dma_async_tx_descriptor desc;
265 dma_cookie_t last_completed;
266 enum dma_status status;
269 #define IMX_DMA_SG_LOOP (1 << 0)
271 #define MAX_DMA_CHANNELS 32
272 #define MXC_SDMA_DEFAULT_PRIORITY 1
273 #define MXC_SDMA_MIN_PRIORITY 1
274 #define MXC_SDMA_MAX_PRIORITY 7
276 #define SDMA_FIRMWARE_MAGIC 0x414d4453
279 * struct sdma_firmware_header - Layout of the firmware image
282 * @version_major increased whenever layout of struct sdma_script_start_addrs
284 * @version_minor firmware minor version (for binary compatible changes)
285 * @script_addrs_start offset of struct sdma_script_start_addrs in this image
286 * @num_script_addrs Number of script addresses in this image
287 * @ram_code_start offset of SDMA ram image in this firmware image
288 * @ram_code_size size of SDMA ram image
289 * @script_addrs Stores the start address of the SDMA scripts
290 * (in SDMA memory space)
292 struct sdma_firmware_header {
296 u32 script_addrs_start;
297 u32 num_script_addrs;
304 struct sdma_channel channel[MAX_DMA_CHANNELS];
305 struct sdma_channel_control *channel_control;
307 unsigned int version;
308 unsigned int num_events;
309 struct sdma_context_data *context;
310 dma_addr_t context_phys;
311 struct dma_device dma_device;
313 struct sdma_script_start_addrs *script_addrs;
316 #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
317 #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
318 #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
319 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
321 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
323 u32 chnenbl0 = (sdma->version == 2 ? SDMA_CHNENBL0_V2 : SDMA_CHNENBL0_V1);
325 return chnenbl0 + event * 4;
328 static int sdma_config_ownership(struct sdma_channel *sdmac,
329 bool event_override, bool mcu_override, bool dsp_override)
331 struct sdma_engine *sdma = sdmac->sdma;
332 int channel = sdmac->channel;
335 if (event_override && mcu_override && dsp_override)
338 evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
339 mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
340 dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
343 dsp &= ~(1 << channel);
345 dsp |= (1 << channel);
348 evt &= ~(1 << channel);
350 evt |= (1 << channel);
353 mcu &= ~(1 << channel);
355 mcu |= (1 << channel);
357 __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
358 __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
359 __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
365 * sdma_run_channel - run a channel and wait till it's done
367 static int sdma_run_channel(struct sdma_channel *sdmac)
369 struct sdma_engine *sdma = sdmac->sdma;
370 int channel = sdmac->channel;
373 init_completion(&sdmac->done);
375 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
377 ret = wait_for_completion_timeout(&sdmac->done, HZ);
379 return ret ? 0 : -ETIMEDOUT;
382 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
385 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
390 buf_virt = dma_alloc_coherent(NULL,
392 &buf_phys, GFP_KERNEL);
396 bd0->mode.command = C0_SETPM;
397 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
398 bd0->mode.count = size / 2;
399 bd0->buffer_addr = buf_phys;
400 bd0->ext_buffer_addr = address;
402 memcpy(buf_virt, buf, size);
404 ret = sdma_run_channel(&sdma->channel[0]);
406 dma_free_coherent(NULL, size, buf_virt, buf_phys);
411 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
413 struct sdma_engine *sdma = sdmac->sdma;
414 int channel = sdmac->channel;
416 u32 chnenbl = chnenbl_ofs(sdma, event);
418 val = __raw_readl(sdma->regs + chnenbl);
419 val |= (1 << channel);
420 __raw_writel(val, sdma->regs + chnenbl);
423 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
425 struct sdma_engine *sdma = sdmac->sdma;
426 int channel = sdmac->channel;
427 u32 chnenbl = chnenbl_ofs(sdma, event);
430 val = __raw_readl(sdma->regs + chnenbl);
431 val &= ~(1 << channel);
432 __raw_writel(val, sdma->regs + chnenbl);
435 static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
437 struct sdma_buffer_descriptor *bd;
440 * loop mode. Iterate over descriptors, re-setup them and
441 * call callback function.
444 bd = &sdmac->bd[sdmac->buf_tail];
446 if (bd->mode.status & BD_DONE)
449 if (bd->mode.status & BD_RROR)
450 sdmac->status = DMA_ERROR;
452 sdmac->status = DMA_SUCCESS;
454 bd->mode.status |= BD_DONE;
456 sdmac->buf_tail %= sdmac->num_bd;
458 if (sdmac->desc.callback)
459 sdmac->desc.callback(sdmac->desc.callback_param);
463 static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
465 struct sdma_buffer_descriptor *bd;
469 * non loop mode. Iterate over all descriptors, collect
470 * errors and call callback function
472 for (i = 0; i < sdmac->num_bd; i++) {
475 if (bd->mode.status & (BD_DONE | BD_RROR))
480 sdmac->status = DMA_ERROR;
482 sdmac->status = DMA_SUCCESS;
484 if (sdmac->desc.callback)
485 sdmac->desc.callback(sdmac->desc.callback_param);
486 sdmac->last_completed = sdmac->desc.cookie;
489 static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
491 complete(&sdmac->done);
493 /* not interested in channel 0 interrupts */
494 if (sdmac->channel == 0)
497 if (sdmac->flags & IMX_DMA_SG_LOOP)
498 sdma_handle_channel_loop(sdmac);
500 mxc_sdma_handle_channel_normal(sdmac);
503 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
505 struct sdma_engine *sdma = dev_id;
508 stat = __raw_readl(sdma->regs + SDMA_H_INTR);
509 __raw_writel(stat, sdma->regs + SDMA_H_INTR);
512 int channel = fls(stat) - 1;
513 struct sdma_channel *sdmac = &sdma->channel[channel];
515 mxc_sdma_handle_channel(sdmac);
517 stat &= ~(1 << channel);
524 * sets the pc of SDMA script according to the peripheral type
526 static void sdma_get_pc(struct sdma_channel *sdmac,
527 enum sdma_peripheral_type peripheral_type)
529 struct sdma_engine *sdma = sdmac->sdma;
530 int per_2_emi = 0, emi_2_per = 0;
532 * These are needed once we start to support transfers between
533 * two peripherals or memory-to-memory transfers
535 int per_2_per = 0, emi_2_emi = 0;
537 sdmac->pc_from_device = 0;
538 sdmac->pc_to_device = 0;
540 switch (peripheral_type) {
541 case IMX_DMATYPE_MEMORY:
542 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
544 case IMX_DMATYPE_DSP:
545 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
546 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
548 case IMX_DMATYPE_FIRI:
549 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
550 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
552 case IMX_DMATYPE_UART:
553 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
554 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
556 case IMX_DMATYPE_UART_SP:
557 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
558 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
560 case IMX_DMATYPE_ATA:
561 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
562 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
564 case IMX_DMATYPE_CSPI:
565 case IMX_DMATYPE_EXT:
566 case IMX_DMATYPE_SSI:
567 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
568 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
570 case IMX_DMATYPE_SSI_SP:
571 case IMX_DMATYPE_MMC:
572 case IMX_DMATYPE_SDHC:
573 case IMX_DMATYPE_CSPI_SP:
574 case IMX_DMATYPE_ESAI:
575 case IMX_DMATYPE_MSHC_SP:
576 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
577 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
579 case IMX_DMATYPE_ASRC:
580 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
581 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
582 per_2_per = sdma->script_addrs->per_2_per_addr;
584 case IMX_DMATYPE_MSHC:
585 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
586 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
588 case IMX_DMATYPE_CCM:
589 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
591 case IMX_DMATYPE_SPDIF:
592 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
593 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
595 case IMX_DMATYPE_IPU_MEMORY:
596 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
602 sdmac->pc_from_device = per_2_emi;
603 sdmac->pc_to_device = emi_2_per;
606 static int sdma_load_context(struct sdma_channel *sdmac)
608 struct sdma_engine *sdma = sdmac->sdma;
609 int channel = sdmac->channel;
611 struct sdma_context_data *context = sdma->context;
612 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
615 if (sdmac->direction == DMA_FROM_DEVICE) {
616 load_address = sdmac->pc_from_device;
618 load_address = sdmac->pc_to_device;
621 if (load_address < 0)
624 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
625 dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
626 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
627 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
628 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
629 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
631 memset(context, 0, sizeof(*context));
632 context->channel_state.pc = load_address;
634 /* Send by context the event mask,base address for peripheral
635 * and watermark level
637 context->gReg[0] = sdmac->event_mask1;
638 context->gReg[1] = sdmac->event_mask0;
639 context->gReg[2] = sdmac->per_addr;
640 context->gReg[6] = sdmac->shp_addr;
641 context->gReg[7] = sdmac->watermark_level;
643 bd0->mode.command = C0_SETDM;
644 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
645 bd0->mode.count = sizeof(*context) / 4;
646 bd0->buffer_addr = sdma->context_phys;
647 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
649 ret = sdma_run_channel(&sdma->channel[0]);
654 static void sdma_disable_channel(struct sdma_channel *sdmac)
656 struct sdma_engine *sdma = sdmac->sdma;
657 int channel = sdmac->channel;
659 __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
660 sdmac->status = DMA_ERROR;
663 static int sdma_config_channel(struct sdma_channel *sdmac)
667 sdma_disable_channel(sdmac);
669 sdmac->event_mask0 = 0;
670 sdmac->event_mask1 = 0;
674 if (sdmac->event_id0) {
675 if (sdmac->event_id0 > 32)
677 sdma_event_enable(sdmac, sdmac->event_id0);
680 switch (sdmac->peripheral_type) {
681 case IMX_DMATYPE_DSP:
682 sdma_config_ownership(sdmac, false, true, true);
684 case IMX_DMATYPE_MEMORY:
685 sdma_config_ownership(sdmac, false, true, false);
688 sdma_config_ownership(sdmac, true, true, false);
692 sdma_get_pc(sdmac, sdmac->peripheral_type);
694 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
695 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
696 /* Handle multiple event channels differently */
697 if (sdmac->event_id1) {
698 sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
699 if (sdmac->event_id1 > 31)
700 sdmac->watermark_level |= 1 << 31;
701 sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
702 if (sdmac->event_id0 > 31)
703 sdmac->watermark_level |= 1 << 30;
705 sdmac->event_mask0 = 1 << sdmac->event_id0;
706 sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
708 /* Watermark Level */
709 sdmac->watermark_level |= sdmac->watermark_level;
711 sdmac->shp_addr = sdmac->per_address;
713 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
716 ret = sdma_load_context(sdmac);
721 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
722 unsigned int priority)
724 struct sdma_engine *sdma = sdmac->sdma;
725 int channel = sdmac->channel;
727 if (priority < MXC_SDMA_MIN_PRIORITY
728 || priority > MXC_SDMA_MAX_PRIORITY) {
732 __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
737 static int sdma_request_channel(struct sdma_channel *sdmac)
739 struct sdma_engine *sdma = sdmac->sdma;
740 int channel = sdmac->channel;
743 sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
749 memset(sdmac->bd, 0, PAGE_SIZE);
751 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
752 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
754 clk_enable(sdma->clk);
756 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
758 init_completion(&sdmac->done);
768 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
770 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
773 static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdma)
775 dma_cookie_t cookie = sdma->chan.cookie;
780 sdma->chan.cookie = cookie;
781 sdma->desc.cookie = cookie;
786 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
788 return container_of(chan, struct sdma_channel, chan);
791 static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
793 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
794 struct sdma_engine *sdma = sdmac->sdma;
797 spin_lock_irq(&sdmac->lock);
799 cookie = sdma_assign_cookie(sdmac);
801 sdma_enable_channel(sdma, tx->chan->chan_id);
803 spin_unlock_irq(&sdmac->lock);
808 static int sdma_alloc_chan_resources(struct dma_chan *chan)
810 struct sdma_channel *sdmac = to_sdma_chan(chan);
811 struct imx_dma_data *data = chan->private;
814 /* No need to execute this for internal channel 0 */
815 if (chan->chan_id == 0)
821 switch (data->priority) {
825 case DMA_PRIO_MEDIUM:
834 sdmac->peripheral_type = data->peripheral_type;
835 sdmac->event_id0 = data->dma_request;
836 ret = sdma_set_channel_priority(sdmac, prio);
840 ret = sdma_request_channel(sdmac);
844 dma_async_tx_descriptor_init(&sdmac->desc, chan);
845 sdmac->desc.tx_submit = sdma_tx_submit;
846 /* txd.flags will be overwritten in prep funcs */
847 sdmac->desc.flags = DMA_CTRL_ACK;
852 static void sdma_free_chan_resources(struct dma_chan *chan)
854 struct sdma_channel *sdmac = to_sdma_chan(chan);
855 struct sdma_engine *sdma = sdmac->sdma;
857 sdma_disable_channel(sdmac);
859 if (sdmac->event_id0)
860 sdma_event_disable(sdmac, sdmac->event_id0);
861 if (sdmac->event_id1)
862 sdma_event_disable(sdmac, sdmac->event_id1);
864 sdmac->event_id0 = 0;
865 sdmac->event_id1 = 0;
867 sdma_set_channel_priority(sdmac, 0);
869 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
871 clk_disable(sdma->clk);
874 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
875 struct dma_chan *chan, struct scatterlist *sgl,
876 unsigned int sg_len, enum dma_data_direction direction,
879 struct sdma_channel *sdmac = to_sdma_chan(chan);
880 struct sdma_engine *sdma = sdmac->sdma;
882 int channel = chan->chan_id;
883 struct scatterlist *sg;
885 if (sdmac->status == DMA_IN_PROGRESS)
887 sdmac->status = DMA_IN_PROGRESS;
891 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
894 sdmac->direction = direction;
895 ret = sdma_load_context(sdmac);
899 if (sg_len > NUM_BD) {
900 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
901 channel, sg_len, NUM_BD);
906 for_each_sg(sgl, sg, sg_len, i) {
907 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
910 bd->buffer_addr = sg->dma_address;
914 if (count > 0xffff) {
915 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
916 channel, count, 0xffff);
921 bd->mode.count = count;
923 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
927 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
928 bd->mode.command = 0;
930 bd->mode.command = sdmac->word_size;
932 param = BD_DONE | BD_EXTD | BD_CONT;
937 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
938 i, count, sg->dma_address,
939 param & BD_WRAP ? "wrap" : "",
940 param & BD_INTR ? " intr" : "");
942 bd->mode.status = param;
945 sdmac->num_bd = sg_len;
946 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
950 sdmac->status = DMA_ERROR;
954 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
955 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
956 size_t period_len, enum dma_data_direction direction)
958 struct sdma_channel *sdmac = to_sdma_chan(chan);
959 struct sdma_engine *sdma = sdmac->sdma;
960 int num_periods = buf_len / period_len;
961 int channel = chan->chan_id;
962 int ret, i = 0, buf = 0;
964 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
966 if (sdmac->status == DMA_IN_PROGRESS)
969 sdmac->status = DMA_IN_PROGRESS;
971 sdmac->flags |= IMX_DMA_SG_LOOP;
972 sdmac->direction = direction;
973 ret = sdma_load_context(sdmac);
977 if (num_periods > NUM_BD) {
978 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
979 channel, num_periods, NUM_BD);
983 if (period_len > 0xffff) {
984 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
985 channel, period_len, 0xffff);
989 while (buf < buf_len) {
990 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
993 bd->buffer_addr = dma_addr;
995 bd->mode.count = period_len;
997 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
999 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1000 bd->mode.command = 0;
1002 bd->mode.command = sdmac->word_size;
1004 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1005 if (i + 1 == num_periods)
1008 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
1009 i, period_len, dma_addr,
1010 param & BD_WRAP ? "wrap" : "",
1011 param & BD_INTR ? " intr" : "");
1013 bd->mode.status = param;
1015 dma_addr += period_len;
1021 sdmac->num_bd = num_periods;
1022 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1024 return &sdmac->desc;
1026 sdmac->status = DMA_ERROR;
1030 static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1033 struct sdma_channel *sdmac = to_sdma_chan(chan);
1034 struct dma_slave_config *dmaengine_cfg = (void *)arg;
1037 case DMA_TERMINATE_ALL:
1038 sdma_disable_channel(sdmac);
1040 case DMA_SLAVE_CONFIG:
1041 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
1042 sdmac->per_address = dmaengine_cfg->src_addr;
1043 sdmac->watermark_level = dmaengine_cfg->src_maxburst;
1044 sdmac->word_size = dmaengine_cfg->src_addr_width;
1046 sdmac->per_address = dmaengine_cfg->dst_addr;
1047 sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
1048 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1050 return sdma_config_channel(sdmac);
1058 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1059 dma_cookie_t cookie,
1060 struct dma_tx_state *txstate)
1062 struct sdma_channel *sdmac = to_sdma_chan(chan);
1063 dma_cookie_t last_used;
1064 enum dma_status ret;
1066 last_used = chan->cookie;
1068 ret = dma_async_is_complete(cookie, sdmac->last_completed, last_used);
1069 dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
1074 static void sdma_issue_pending(struct dma_chan *chan)
1077 * Nothing to do. We only have a single descriptor
1081 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1083 static void sdma_add_scripts(struct sdma_engine *sdma,
1084 const struct sdma_script_start_addrs *addr)
1086 s32 *addr_arr = (u32 *)addr;
1087 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1090 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1091 if (addr_arr[i] > 0)
1092 saddr_arr[i] = addr_arr[i];
1095 static int __init sdma_get_firmware(struct sdma_engine *sdma,
1096 const char *cpu_name, int to_version)
1098 const struct firmware *fw;
1100 const struct sdma_firmware_header *header;
1102 const struct sdma_script_start_addrs *addr;
1103 unsigned short *ram_code;
1105 fwname = kasprintf(GFP_KERNEL, "sdma-%s-to%d.bin", cpu_name, to_version);
1109 ret = request_firmware(&fw, fwname, sdma->dev);
1116 if (fw->size < sizeof(*header))
1119 header = (struct sdma_firmware_header *)fw->data;
1121 if (header->magic != SDMA_FIRMWARE_MAGIC)
1123 if (header->ram_code_start + header->ram_code_size > fw->size)
1126 addr = (void *)header + header->script_addrs_start;
1127 ram_code = (void *)header + header->ram_code_start;
1129 clk_enable(sdma->clk);
1130 /* download the RAM image for SDMA */
1131 sdma_load_script(sdma, ram_code,
1132 header->ram_code_size,
1133 sdma->script_addrs->ram_code_start_addr);
1134 clk_disable(sdma->clk);
1136 sdma_add_scripts(sdma, addr);
1138 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1139 header->version_major,
1140 header->version_minor);
1143 release_firmware(fw);
1148 static int __init sdma_init(struct sdma_engine *sdma)
1151 dma_addr_t ccb_phys;
1153 switch (sdma->version) {
1155 sdma->num_events = 32;
1158 sdma->num_events = 48;
1161 dev_err(sdma->dev, "Unknown version %d. aborting\n", sdma->version);
1165 clk_enable(sdma->clk);
1167 /* Be sure SDMA has not started yet */
1168 __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
1170 sdma->channel_control = dma_alloc_coherent(NULL,
1171 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1172 sizeof(struct sdma_context_data),
1173 &ccb_phys, GFP_KERNEL);
1175 if (!sdma->channel_control) {
1180 sdma->context = (void *)sdma->channel_control +
1181 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1182 sdma->context_phys = ccb_phys +
1183 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1185 /* Zero-out the CCB structures array just allocated */
1186 memset(sdma->channel_control, 0,
1187 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1189 /* disable all channels */
1190 for (i = 0; i < sdma->num_events; i++)
1191 __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
1193 /* All channels have priority 0 */
1194 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1195 __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1197 ret = sdma_request_channel(&sdma->channel[0]);
1201 sdma_config_ownership(&sdma->channel[0], false, true, false);
1203 /* Set Command Channel (Channel Zero) */
1204 __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
1206 /* Set bits of CONFIG register but with static context switching */
1207 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1208 __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
1210 __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1212 /* Set bits of CONFIG register with given context switching mode */
1213 __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
1215 /* Initializes channel's priorities */
1216 sdma_set_channel_priority(&sdma->channel[0], 7);
1218 clk_disable(sdma->clk);
1223 clk_disable(sdma->clk);
1224 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1228 static int __init sdma_probe(struct platform_device *pdev)
1232 struct resource *iores;
1233 struct sdma_platform_data *pdata = pdev->dev.platform_data;
1235 dma_cap_mask_t mask;
1236 struct sdma_engine *sdma;
1238 sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
1242 sdma->dev = &pdev->dev;
1244 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1245 irq = platform_get_irq(pdev, 0);
1246 if (!iores || irq < 0 || !pdata) {
1251 if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
1253 goto err_request_region;
1256 sdma->clk = clk_get(&pdev->dev, NULL);
1257 if (IS_ERR(sdma->clk)) {
1258 ret = PTR_ERR(sdma->clk);
1262 sdma->regs = ioremap(iores->start, resource_size(iores));
1268 ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
1270 goto err_request_irq;
1272 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
1273 if (!sdma->script_addrs)
1276 sdma->version = pdata->sdma_version;
1278 INIT_LIST_HEAD(&sdma->dma_device.channels);
1279 /* Initialize channel parameters */
1280 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1281 struct sdma_channel *sdmac = &sdma->channel[i];
1284 spin_lock_init(&sdmac->lock);
1286 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1287 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1289 sdmac->chan.device = &sdma->dma_device;
1290 sdmac->chan.chan_id = i;
1293 /* Add the channel to the DMAC list */
1294 list_add_tail(&sdmac->chan.device_node, &sdma->dma_device.channels);
1297 ret = sdma_init(sdma);
1301 if (pdata->script_addrs)
1302 sdma_add_scripts(sdma, pdata->script_addrs);
1304 sdma_get_firmware(sdma, pdata->cpu_name, pdata->to_version);
1306 sdma->dma_device.dev = &pdev->dev;
1308 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1309 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1310 sdma->dma_device.device_tx_status = sdma_tx_status;
1311 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1312 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1313 sdma->dma_device.device_control = sdma_control;
1314 sdma->dma_device.device_issue_pending = sdma_issue_pending;
1316 ret = dma_async_device_register(&sdma->dma_device);
1318 dev_err(&pdev->dev, "unable to register\n");
1322 /* request channel 0. This is an internal control channel
1323 * to the SDMA engine and not available to clients.
1326 dma_cap_set(DMA_SLAVE, mask);
1327 dma_request_channel(mask, NULL, NULL);
1329 dev_info(sdma->dev, "initialized\n");
1334 kfree(sdma->script_addrs);
1336 free_irq(irq, sdma);
1338 iounmap(sdma->regs);
1342 release_mem_region(iores->start, resource_size(iores));
1349 static int __exit sdma_remove(struct platform_device *pdev)
1354 static struct platform_driver sdma_driver = {
1358 .remove = __exit_p(sdma_remove),
1361 static int __init sdma_module_init(void)
1363 return platform_driver_probe(&sdma_driver, sdma_probe);
1365 module_init(sdma_module_init);
1367 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1368 MODULE_DESCRIPTION("i.MX SDMA driver");
1369 MODULE_LICENSE("GPL");