staging: iio: max517: Fix iio_info changes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / dma / imx-sdma.c
blobb6d1455fa9362ecba3dbfe088df72bc77b2ef21e
1 /*
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>
22 #include <linux/mm.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>
36 #include <asm/irq.h>
37 #include <mach/sdma.h>
38 #include <mach/dma.h>
39 #include <mach/hardware.h>
41 /* SDMA registers */
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.
75 #define BD_DONE 0x01
76 #define BD_WRAP 0x02
77 #define BD_CONT 0x04
78 #define BD_INTR 0x08
79 #define BD_RROR 0x10
80 #define BD_LAST 0x20
81 #define BD_EXTD 0x80
84 * Data Node descriptor status values.
86 #define DND_END_OF_FRAME 0x80
87 #define DND_END_OF_XFER 0x40
88 #define DND_DONE 0x20
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.
106 #define C0_ADDR 0x01
107 #define C0_LOAD 0x02
108 #define C0_DUMP 0x03
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 */
130 * Buffer descriptor
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
144 * control blocks
146 struct sdma_channel_control {
147 u32 current_bd_ptr;
148 u32 base_bd_ptr;
149 u32 unused[2];
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
162 * @lm: loop mode
164 struct sdma_state_registers {
165 u32 pc :14;
166 u32 unused1: 1;
167 u32 t : 1;
168 u32 rpc :14;
169 u32 unused0: 1;
170 u32 sf : 1;
171 u32 spc :14;
172 u32 unused2: 1;
173 u32 df : 1;
174 u32 epc :14;
175 u32 lm : 2;
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;
200 u32 gReg[8];
201 u32 mda;
202 u32 msa;
203 u32 ms;
204 u32 md;
205 u32 pda;
206 u32 psa;
207 u32 ps;
208 u32 pd;
209 u32 ca;
210 u32 cs;
211 u32 dda;
212 u32 dsa;
213 u32 ds;
214 u32 dd;
215 u32 scratch0;
216 u32 scratch1;
217 u32 scratch2;
218 u32 scratch3;
219 u32 scratch4;
220 u32 scratch5;
221 u32 scratch6;
222 u32 scratch7;
223 } __attribute__ ((packed));
225 #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
227 struct sdma_engine;
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 + 1
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;
253 unsigned int num_bd;
254 struct sdma_buffer_descriptor *bd;
255 dma_addr_t bd_phys;
256 unsigned int pc_from_device, pc_to_device;
257 unsigned long flags;
258 dma_addr_t per_address;
259 u32 event_mask0, event_mask1;
260 u32 watermark_level;
261 u32 shp_addr, per_addr;
262 struct dma_chan chan;
263 spinlock_t lock;
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
281 * @magic "SDMA"
282 * @version_major increased whenever layout of struct sdma_script_start_addrs
283 * changes.
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 {
293 u32 magic;
294 u32 version_major;
295 u32 version_minor;
296 u32 script_addrs_start;
297 u32 num_script_addrs;
298 u32 ram_code_start;
299 u32 ram_code_size;
302 struct sdma_engine {
303 struct device *dev;
304 struct device_dma_parameters dma_parms;
305 struct sdma_channel channel[MAX_DMA_CHANNELS];
306 struct sdma_channel_control *channel_control;
307 void __iomem *regs;
308 unsigned int version;
309 unsigned int num_events;
310 struct sdma_context_data *context;
311 dma_addr_t context_phys;
312 struct dma_device dma_device;
313 struct clk *clk;
314 struct sdma_script_start_addrs *script_addrs;
317 #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
318 #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
319 #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
320 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
322 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
324 u32 chnenbl0 = (sdma->version == 2 ? SDMA_CHNENBL0_V2 : SDMA_CHNENBL0_V1);
326 return chnenbl0 + event * 4;
329 static int sdma_config_ownership(struct sdma_channel *sdmac,
330 bool event_override, bool mcu_override, bool dsp_override)
332 struct sdma_engine *sdma = sdmac->sdma;
333 int channel = sdmac->channel;
334 u32 evt, mcu, dsp;
336 if (event_override && mcu_override && dsp_override)
337 return -EINVAL;
339 evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
340 mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
341 dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
343 if (dsp_override)
344 dsp &= ~(1 << channel);
345 else
346 dsp |= (1 << channel);
348 if (event_override)
349 evt &= ~(1 << channel);
350 else
351 evt |= (1 << channel);
353 if (mcu_override)
354 mcu &= ~(1 << channel);
355 else
356 mcu |= (1 << channel);
358 __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
359 __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
360 __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
362 return 0;
366 * sdma_run_channel - run a channel and wait till it's done
368 static int sdma_run_channel(struct sdma_channel *sdmac)
370 struct sdma_engine *sdma = sdmac->sdma;
371 int channel = sdmac->channel;
372 int ret;
374 init_completion(&sdmac->done);
376 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
378 ret = wait_for_completion_timeout(&sdmac->done, HZ);
380 return ret ? 0 : -ETIMEDOUT;
383 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
384 u32 address)
386 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
387 void *buf_virt;
388 dma_addr_t buf_phys;
389 int ret;
391 buf_virt = dma_alloc_coherent(NULL,
392 size,
393 &buf_phys, GFP_KERNEL);
394 if (!buf_virt)
395 return -ENOMEM;
397 bd0->mode.command = C0_SETPM;
398 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
399 bd0->mode.count = size / 2;
400 bd0->buffer_addr = buf_phys;
401 bd0->ext_buffer_addr = address;
403 memcpy(buf_virt, buf, size);
405 ret = sdma_run_channel(&sdma->channel[0]);
407 dma_free_coherent(NULL, size, buf_virt, buf_phys);
409 return ret;
412 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
414 struct sdma_engine *sdma = sdmac->sdma;
415 int channel = sdmac->channel;
416 u32 val;
417 u32 chnenbl = chnenbl_ofs(sdma, event);
419 val = __raw_readl(sdma->regs + chnenbl);
420 val |= (1 << channel);
421 __raw_writel(val, sdma->regs + chnenbl);
424 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
426 struct sdma_engine *sdma = sdmac->sdma;
427 int channel = sdmac->channel;
428 u32 chnenbl = chnenbl_ofs(sdma, event);
429 u32 val;
431 val = __raw_readl(sdma->regs + chnenbl);
432 val &= ~(1 << channel);
433 __raw_writel(val, sdma->regs + chnenbl);
436 static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
438 struct sdma_buffer_descriptor *bd;
441 * loop mode. Iterate over descriptors, re-setup them and
442 * call callback function.
444 while (1) {
445 bd = &sdmac->bd[sdmac->buf_tail];
447 if (bd->mode.status & BD_DONE)
448 break;
450 if (bd->mode.status & BD_RROR)
451 sdmac->status = DMA_ERROR;
452 else
453 sdmac->status = DMA_IN_PROGRESS;
455 bd->mode.status |= BD_DONE;
456 sdmac->buf_tail++;
457 sdmac->buf_tail %= sdmac->num_bd;
459 if (sdmac->desc.callback)
460 sdmac->desc.callback(sdmac->desc.callback_param);
464 static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
466 struct sdma_buffer_descriptor *bd;
467 int i, error = 0;
470 * non loop mode. Iterate over all descriptors, collect
471 * errors and call callback function
473 for (i = 0; i < sdmac->num_bd; i++) {
474 bd = &sdmac->bd[i];
476 if (bd->mode.status & (BD_DONE | BD_RROR))
477 error = -EIO;
480 if (error)
481 sdmac->status = DMA_ERROR;
482 else
483 sdmac->status = DMA_SUCCESS;
485 if (sdmac->desc.callback)
486 sdmac->desc.callback(sdmac->desc.callback_param);
487 sdmac->last_completed = sdmac->desc.cookie;
490 static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
492 complete(&sdmac->done);
494 /* not interested in channel 0 interrupts */
495 if (sdmac->channel == 0)
496 return;
498 if (sdmac->flags & IMX_DMA_SG_LOOP)
499 sdma_handle_channel_loop(sdmac);
500 else
501 mxc_sdma_handle_channel_normal(sdmac);
504 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
506 struct sdma_engine *sdma = dev_id;
507 u32 stat;
509 stat = __raw_readl(sdma->regs + SDMA_H_INTR);
510 __raw_writel(stat, sdma->regs + SDMA_H_INTR);
512 while (stat) {
513 int channel = fls(stat) - 1;
514 struct sdma_channel *sdmac = &sdma->channel[channel];
516 mxc_sdma_handle_channel(sdmac);
518 stat &= ~(1 << channel);
521 return IRQ_HANDLED;
525 * sets the pc of SDMA script according to the peripheral type
527 static void sdma_get_pc(struct sdma_channel *sdmac,
528 enum sdma_peripheral_type peripheral_type)
530 struct sdma_engine *sdma = sdmac->sdma;
531 int per_2_emi = 0, emi_2_per = 0;
533 * These are needed once we start to support transfers between
534 * two peripherals or memory-to-memory transfers
536 int per_2_per = 0, emi_2_emi = 0;
538 sdmac->pc_from_device = 0;
539 sdmac->pc_to_device = 0;
541 switch (peripheral_type) {
542 case IMX_DMATYPE_MEMORY:
543 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
544 break;
545 case IMX_DMATYPE_DSP:
546 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
547 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
548 break;
549 case IMX_DMATYPE_FIRI:
550 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
551 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
552 break;
553 case IMX_DMATYPE_UART:
554 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
555 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
556 break;
557 case IMX_DMATYPE_UART_SP:
558 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
559 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
560 break;
561 case IMX_DMATYPE_ATA:
562 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
563 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
564 break;
565 case IMX_DMATYPE_CSPI:
566 case IMX_DMATYPE_EXT:
567 case IMX_DMATYPE_SSI:
568 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
569 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
570 break;
571 case IMX_DMATYPE_SSI_SP:
572 case IMX_DMATYPE_MMC:
573 case IMX_DMATYPE_SDHC:
574 case IMX_DMATYPE_CSPI_SP:
575 case IMX_DMATYPE_ESAI:
576 case IMX_DMATYPE_MSHC_SP:
577 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
578 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
579 break;
580 case IMX_DMATYPE_ASRC:
581 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
582 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
583 per_2_per = sdma->script_addrs->per_2_per_addr;
584 break;
585 case IMX_DMATYPE_MSHC:
586 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
587 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
588 break;
589 case IMX_DMATYPE_CCM:
590 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
591 break;
592 case IMX_DMATYPE_SPDIF:
593 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
594 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
595 break;
596 case IMX_DMATYPE_IPU_MEMORY:
597 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
598 break;
599 default:
600 break;
603 sdmac->pc_from_device = per_2_emi;
604 sdmac->pc_to_device = emi_2_per;
607 static int sdma_load_context(struct sdma_channel *sdmac)
609 struct sdma_engine *sdma = sdmac->sdma;
610 int channel = sdmac->channel;
611 int load_address;
612 struct sdma_context_data *context = sdma->context;
613 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
614 int ret;
616 if (sdmac->direction == DMA_FROM_DEVICE) {
617 load_address = sdmac->pc_from_device;
618 } else {
619 load_address = sdmac->pc_to_device;
622 if (load_address < 0)
623 return load_address;
625 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
626 dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
627 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
628 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
629 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
630 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
632 memset(context, 0, sizeof(*context));
633 context->channel_state.pc = load_address;
635 /* Send by context the event mask,base address for peripheral
636 * and watermark level
638 context->gReg[0] = sdmac->event_mask1;
639 context->gReg[1] = sdmac->event_mask0;
640 context->gReg[2] = sdmac->per_addr;
641 context->gReg[6] = sdmac->shp_addr;
642 context->gReg[7] = sdmac->watermark_level;
644 bd0->mode.command = C0_SETDM;
645 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
646 bd0->mode.count = sizeof(*context) / 4;
647 bd0->buffer_addr = sdma->context_phys;
648 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
650 ret = sdma_run_channel(&sdma->channel[0]);
652 return ret;
655 static void sdma_disable_channel(struct sdma_channel *sdmac)
657 struct sdma_engine *sdma = sdmac->sdma;
658 int channel = sdmac->channel;
660 __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
661 sdmac->status = DMA_ERROR;
664 static int sdma_config_channel(struct sdma_channel *sdmac)
666 int ret;
668 sdma_disable_channel(sdmac);
670 sdmac->event_mask0 = 0;
671 sdmac->event_mask1 = 0;
672 sdmac->shp_addr = 0;
673 sdmac->per_addr = 0;
675 if (sdmac->event_id0) {
676 if (sdmac->event_id0 > 32)
677 return -EINVAL;
678 sdma_event_enable(sdmac, sdmac->event_id0);
681 switch (sdmac->peripheral_type) {
682 case IMX_DMATYPE_DSP:
683 sdma_config_ownership(sdmac, false, true, true);
684 break;
685 case IMX_DMATYPE_MEMORY:
686 sdma_config_ownership(sdmac, false, true, false);
687 break;
688 default:
689 sdma_config_ownership(sdmac, true, true, false);
690 break;
693 sdma_get_pc(sdmac, sdmac->peripheral_type);
695 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
696 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
697 /* Handle multiple event channels differently */
698 if (sdmac->event_id1) {
699 sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
700 if (sdmac->event_id1 > 31)
701 sdmac->watermark_level |= 1 << 31;
702 sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
703 if (sdmac->event_id0 > 31)
704 sdmac->watermark_level |= 1 << 30;
705 } else {
706 sdmac->event_mask0 = 1 << sdmac->event_id0;
707 sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
709 /* Watermark Level */
710 sdmac->watermark_level |= sdmac->watermark_level;
711 /* Address */
712 sdmac->shp_addr = sdmac->per_address;
713 } else {
714 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
717 ret = sdma_load_context(sdmac);
719 return ret;
722 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
723 unsigned int priority)
725 struct sdma_engine *sdma = sdmac->sdma;
726 int channel = sdmac->channel;
728 if (priority < MXC_SDMA_MIN_PRIORITY
729 || priority > MXC_SDMA_MAX_PRIORITY) {
730 return -EINVAL;
733 __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
735 return 0;
738 static int sdma_request_channel(struct sdma_channel *sdmac)
740 struct sdma_engine *sdma = sdmac->sdma;
741 int channel = sdmac->channel;
742 int ret = -EBUSY;
744 sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
745 if (!sdmac->bd) {
746 ret = -ENOMEM;
747 goto out;
750 memset(sdmac->bd, 0, PAGE_SIZE);
752 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
753 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
755 clk_enable(sdma->clk);
757 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
759 init_completion(&sdmac->done);
761 sdmac->buf_tail = 0;
763 return 0;
764 out:
766 return ret;
769 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
771 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
774 static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac)
776 dma_cookie_t cookie = sdmac->chan.cookie;
778 if (++cookie < 0)
779 cookie = 1;
781 sdmac->chan.cookie = cookie;
782 sdmac->desc.cookie = cookie;
784 return cookie;
787 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
789 return container_of(chan, struct sdma_channel, chan);
792 static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
794 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
795 struct sdma_engine *sdma = sdmac->sdma;
796 dma_cookie_t cookie;
798 spin_lock_irq(&sdmac->lock);
800 cookie = sdma_assign_cookie(sdmac);
802 sdma_enable_channel(sdma, sdmac->channel);
804 spin_unlock_irq(&sdmac->lock);
806 return cookie;
809 static int sdma_alloc_chan_resources(struct dma_chan *chan)
811 struct sdma_channel *sdmac = to_sdma_chan(chan);
812 struct imx_dma_data *data = chan->private;
813 int prio, ret;
815 if (!data)
816 return -EINVAL;
818 switch (data->priority) {
819 case DMA_PRIO_HIGH:
820 prio = 3;
821 break;
822 case DMA_PRIO_MEDIUM:
823 prio = 2;
824 break;
825 case DMA_PRIO_LOW:
826 default:
827 prio = 1;
828 break;
831 sdmac->peripheral_type = data->peripheral_type;
832 sdmac->event_id0 = data->dma_request;
833 ret = sdma_set_channel_priority(sdmac, prio);
834 if (ret)
835 return ret;
837 ret = sdma_request_channel(sdmac);
838 if (ret)
839 return ret;
841 dma_async_tx_descriptor_init(&sdmac->desc, chan);
842 sdmac->desc.tx_submit = sdma_tx_submit;
843 /* txd.flags will be overwritten in prep funcs */
844 sdmac->desc.flags = DMA_CTRL_ACK;
846 return 0;
849 static void sdma_free_chan_resources(struct dma_chan *chan)
851 struct sdma_channel *sdmac = to_sdma_chan(chan);
852 struct sdma_engine *sdma = sdmac->sdma;
854 sdma_disable_channel(sdmac);
856 if (sdmac->event_id0)
857 sdma_event_disable(sdmac, sdmac->event_id0);
858 if (sdmac->event_id1)
859 sdma_event_disable(sdmac, sdmac->event_id1);
861 sdmac->event_id0 = 0;
862 sdmac->event_id1 = 0;
864 sdma_set_channel_priority(sdmac, 0);
866 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
868 clk_disable(sdma->clk);
871 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
872 struct dma_chan *chan, struct scatterlist *sgl,
873 unsigned int sg_len, enum dma_data_direction direction,
874 unsigned long flags)
876 struct sdma_channel *sdmac = to_sdma_chan(chan);
877 struct sdma_engine *sdma = sdmac->sdma;
878 int ret, i, count;
879 int channel = sdmac->channel;
880 struct scatterlist *sg;
882 if (sdmac->status == DMA_IN_PROGRESS)
883 return NULL;
884 sdmac->status = DMA_IN_PROGRESS;
886 sdmac->flags = 0;
888 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
889 sg_len, channel);
891 sdmac->direction = direction;
892 ret = sdma_load_context(sdmac);
893 if (ret)
894 goto err_out;
896 if (sg_len > NUM_BD) {
897 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
898 channel, sg_len, NUM_BD);
899 ret = -EINVAL;
900 goto err_out;
903 for_each_sg(sgl, sg, sg_len, i) {
904 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
905 int param;
907 bd->buffer_addr = sg->dma_address;
909 count = sg->length;
911 if (count > 0xffff) {
912 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
913 channel, count, 0xffff);
914 ret = -EINVAL;
915 goto err_out;
918 bd->mode.count = count;
920 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
921 ret = -EINVAL;
922 goto err_out;
925 switch (sdmac->word_size) {
926 case DMA_SLAVE_BUSWIDTH_4_BYTES:
927 bd->mode.command = 0;
928 if (count & 3 || sg->dma_address & 3)
929 return NULL;
930 break;
931 case DMA_SLAVE_BUSWIDTH_2_BYTES:
932 bd->mode.command = 2;
933 if (count & 1 || sg->dma_address & 1)
934 return NULL;
935 break;
936 case DMA_SLAVE_BUSWIDTH_1_BYTE:
937 bd->mode.command = 1;
938 break;
939 default:
940 return NULL;
943 param = BD_DONE | BD_EXTD | BD_CONT;
945 if (i + 1 == sg_len) {
946 param |= BD_INTR;
947 param |= BD_LAST;
948 param &= ~BD_CONT;
951 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
952 i, count, sg->dma_address,
953 param & BD_WRAP ? "wrap" : "",
954 param & BD_INTR ? " intr" : "");
956 bd->mode.status = param;
959 sdmac->num_bd = sg_len;
960 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
962 return &sdmac->desc;
963 err_out:
964 sdmac->status = DMA_ERROR;
965 return NULL;
968 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
969 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
970 size_t period_len, enum dma_data_direction direction)
972 struct sdma_channel *sdmac = to_sdma_chan(chan);
973 struct sdma_engine *sdma = sdmac->sdma;
974 int num_periods = buf_len / period_len;
975 int channel = sdmac->channel;
976 int ret, i = 0, buf = 0;
978 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
980 if (sdmac->status == DMA_IN_PROGRESS)
981 return NULL;
983 sdmac->status = DMA_IN_PROGRESS;
985 sdmac->flags |= IMX_DMA_SG_LOOP;
986 sdmac->direction = direction;
987 ret = sdma_load_context(sdmac);
988 if (ret)
989 goto err_out;
991 if (num_periods > NUM_BD) {
992 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
993 channel, num_periods, NUM_BD);
994 goto err_out;
997 if (period_len > 0xffff) {
998 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
999 channel, period_len, 0xffff);
1000 goto err_out;
1003 while (buf < buf_len) {
1004 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1005 int param;
1007 bd->buffer_addr = dma_addr;
1009 bd->mode.count = period_len;
1011 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1012 goto err_out;
1013 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1014 bd->mode.command = 0;
1015 else
1016 bd->mode.command = sdmac->word_size;
1018 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1019 if (i + 1 == num_periods)
1020 param |= BD_WRAP;
1022 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
1023 i, period_len, dma_addr,
1024 param & BD_WRAP ? "wrap" : "",
1025 param & BD_INTR ? " intr" : "");
1027 bd->mode.status = param;
1029 dma_addr += period_len;
1030 buf += period_len;
1032 i++;
1035 sdmac->num_bd = num_periods;
1036 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1038 return &sdmac->desc;
1039 err_out:
1040 sdmac->status = DMA_ERROR;
1041 return NULL;
1044 static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1045 unsigned long arg)
1047 struct sdma_channel *sdmac = to_sdma_chan(chan);
1048 struct dma_slave_config *dmaengine_cfg = (void *)arg;
1050 switch (cmd) {
1051 case DMA_TERMINATE_ALL:
1052 sdma_disable_channel(sdmac);
1053 return 0;
1054 case DMA_SLAVE_CONFIG:
1055 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
1056 sdmac->per_address = dmaengine_cfg->src_addr;
1057 sdmac->watermark_level = dmaengine_cfg->src_maxburst;
1058 sdmac->word_size = dmaengine_cfg->src_addr_width;
1059 } else {
1060 sdmac->per_address = dmaengine_cfg->dst_addr;
1061 sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
1062 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1064 return sdma_config_channel(sdmac);
1065 default:
1066 return -ENOSYS;
1069 return -EINVAL;
1072 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1073 dma_cookie_t cookie,
1074 struct dma_tx_state *txstate)
1076 struct sdma_channel *sdmac = to_sdma_chan(chan);
1077 dma_cookie_t last_used;
1079 last_used = chan->cookie;
1081 dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
1083 return sdmac->status;
1086 static void sdma_issue_pending(struct dma_chan *chan)
1089 * Nothing to do. We only have a single descriptor
1093 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1095 static void sdma_add_scripts(struct sdma_engine *sdma,
1096 const struct sdma_script_start_addrs *addr)
1098 s32 *addr_arr = (u32 *)addr;
1099 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1100 int i;
1102 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1103 if (addr_arr[i] > 0)
1104 saddr_arr[i] = addr_arr[i];
1107 static int __init sdma_get_firmware(struct sdma_engine *sdma,
1108 const char *cpu_name, int to_version)
1110 const struct firmware *fw;
1111 char *fwname;
1112 const struct sdma_firmware_header *header;
1113 int ret;
1114 const struct sdma_script_start_addrs *addr;
1115 unsigned short *ram_code;
1117 fwname = kasprintf(GFP_KERNEL, "sdma-%s-to%d.bin", cpu_name, to_version);
1118 if (!fwname)
1119 return -ENOMEM;
1121 ret = request_firmware(&fw, fwname, sdma->dev);
1122 if (ret) {
1123 kfree(fwname);
1124 return ret;
1126 kfree(fwname);
1128 if (fw->size < sizeof(*header))
1129 goto err_firmware;
1131 header = (struct sdma_firmware_header *)fw->data;
1133 if (header->magic != SDMA_FIRMWARE_MAGIC)
1134 goto err_firmware;
1135 if (header->ram_code_start + header->ram_code_size > fw->size)
1136 goto err_firmware;
1138 addr = (void *)header + header->script_addrs_start;
1139 ram_code = (void *)header + header->ram_code_start;
1141 clk_enable(sdma->clk);
1142 /* download the RAM image for SDMA */
1143 sdma_load_script(sdma, ram_code,
1144 header->ram_code_size,
1145 addr->ram_code_start_addr);
1146 clk_disable(sdma->clk);
1148 sdma_add_scripts(sdma, addr);
1150 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1151 header->version_major,
1152 header->version_minor);
1154 err_firmware:
1155 release_firmware(fw);
1157 return ret;
1160 static int __init sdma_init(struct sdma_engine *sdma)
1162 int i, ret;
1163 dma_addr_t ccb_phys;
1165 switch (sdma->version) {
1166 case 1:
1167 sdma->num_events = 32;
1168 break;
1169 case 2:
1170 sdma->num_events = 48;
1171 break;
1172 default:
1173 dev_err(sdma->dev, "Unknown version %d. aborting\n", sdma->version);
1174 return -ENODEV;
1177 clk_enable(sdma->clk);
1179 /* Be sure SDMA has not started yet */
1180 __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
1182 sdma->channel_control = dma_alloc_coherent(NULL,
1183 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1184 sizeof(struct sdma_context_data),
1185 &ccb_phys, GFP_KERNEL);
1187 if (!sdma->channel_control) {
1188 ret = -ENOMEM;
1189 goto err_dma_alloc;
1192 sdma->context = (void *)sdma->channel_control +
1193 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1194 sdma->context_phys = ccb_phys +
1195 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1197 /* Zero-out the CCB structures array just allocated */
1198 memset(sdma->channel_control, 0,
1199 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1201 /* disable all channels */
1202 for (i = 0; i < sdma->num_events; i++)
1203 __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
1205 /* All channels have priority 0 */
1206 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1207 __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1209 ret = sdma_request_channel(&sdma->channel[0]);
1210 if (ret)
1211 goto err_dma_alloc;
1213 sdma_config_ownership(&sdma->channel[0], false, true, false);
1215 /* Set Command Channel (Channel Zero) */
1216 __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
1218 /* Set bits of CONFIG register but with static context switching */
1219 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1220 __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
1222 __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1224 /* Set bits of CONFIG register with given context switching mode */
1225 __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
1227 /* Initializes channel's priorities */
1228 sdma_set_channel_priority(&sdma->channel[0], 7);
1230 clk_disable(sdma->clk);
1232 return 0;
1234 err_dma_alloc:
1235 clk_disable(sdma->clk);
1236 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1237 return ret;
1240 static int __init sdma_probe(struct platform_device *pdev)
1242 int ret;
1243 int irq;
1244 struct resource *iores;
1245 struct sdma_platform_data *pdata = pdev->dev.platform_data;
1246 int i;
1247 struct sdma_engine *sdma;
1249 sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
1250 if (!sdma)
1251 return -ENOMEM;
1253 sdma->dev = &pdev->dev;
1255 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1256 irq = platform_get_irq(pdev, 0);
1257 if (!iores || irq < 0 || !pdata) {
1258 ret = -EINVAL;
1259 goto err_irq;
1262 if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
1263 ret = -EBUSY;
1264 goto err_request_region;
1267 sdma->clk = clk_get(&pdev->dev, NULL);
1268 if (IS_ERR(sdma->clk)) {
1269 ret = PTR_ERR(sdma->clk);
1270 goto err_clk;
1273 sdma->regs = ioremap(iores->start, resource_size(iores));
1274 if (!sdma->regs) {
1275 ret = -ENOMEM;
1276 goto err_ioremap;
1279 ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
1280 if (ret)
1281 goto err_request_irq;
1283 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
1284 if (!sdma->script_addrs)
1285 goto err_alloc;
1287 sdma->version = pdata->sdma_version;
1289 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1290 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1292 INIT_LIST_HEAD(&sdma->dma_device.channels);
1293 /* Initialize channel parameters */
1294 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1295 struct sdma_channel *sdmac = &sdma->channel[i];
1297 sdmac->sdma = sdma;
1298 spin_lock_init(&sdmac->lock);
1300 sdmac->chan.device = &sdma->dma_device;
1301 sdmac->channel = i;
1304 * Add the channel to the DMAC list. Do not add channel 0 though
1305 * because we need it internally in the SDMA driver. This also means
1306 * that channel 0 in dmaengine counting matches sdma channel 1.
1308 if (i)
1309 list_add_tail(&sdmac->chan.device_node,
1310 &sdma->dma_device.channels);
1313 ret = sdma_init(sdma);
1314 if (ret)
1315 goto err_init;
1317 if (pdata->script_addrs)
1318 sdma_add_scripts(sdma, pdata->script_addrs);
1320 sdma_get_firmware(sdma, pdata->cpu_name, pdata->to_version);
1322 sdma->dma_device.dev = &pdev->dev;
1324 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1325 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1326 sdma->dma_device.device_tx_status = sdma_tx_status;
1327 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1328 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1329 sdma->dma_device.device_control = sdma_control;
1330 sdma->dma_device.device_issue_pending = sdma_issue_pending;
1331 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1332 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
1334 ret = dma_async_device_register(&sdma->dma_device);
1335 if (ret) {
1336 dev_err(&pdev->dev, "unable to register\n");
1337 goto err_init;
1340 dev_info(sdma->dev, "initialized\n");
1342 return 0;
1344 err_init:
1345 kfree(sdma->script_addrs);
1346 err_alloc:
1347 free_irq(irq, sdma);
1348 err_request_irq:
1349 iounmap(sdma->regs);
1350 err_ioremap:
1351 clk_put(sdma->clk);
1352 err_clk:
1353 release_mem_region(iores->start, resource_size(iores));
1354 err_request_region:
1355 err_irq:
1356 kfree(sdma);
1357 return ret;
1360 static int __exit sdma_remove(struct platform_device *pdev)
1362 return -EBUSY;
1365 static struct platform_driver sdma_driver = {
1366 .driver = {
1367 .name = "imx-sdma",
1369 .remove = __exit_p(sdma_remove),
1372 static int __init sdma_module_init(void)
1374 return platform_driver_probe(&sdma_driver, sdma_probe);
1376 module_init(sdma_module_init);
1378 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1379 MODULE_DESCRIPTION("i.MX SDMA driver");
1380 MODULE_LICENSE("GPL");