blob: b939e163f3b13e1e078ee22feac6008e0a387830 [file] [log] [blame]
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001/*
2 * drivers/dma/imx-sdma.c
3 *
4 * This file contains a driver for the Freescale Smart DMA engine
5 *
6 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 *
8 * Based on code from Freescale:
9 *
10 * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
11 *
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:
15 *
16 * http://www.opensource.org/licenses/gpl-license.html
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
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>
Shawn Guo580975d2011-07-14 08:35:48 +080035#include <linux/of.h>
36#include <linux/of_device.h>
Paul Gortmaker5c45ad72011-07-31 16:14:17 -040037#include <linux/module.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000038
39#include <asm/irq.h>
40#include <mach/sdma.h>
41#include <mach/dma.h>
42#include <mach/hardware.h>
43
44/* SDMA registers */
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
Shawn Guo62550cd2011-07-13 21:33:17 +080071#define SDMA_CHNENBL0_IMX35 0x200
72#define SDMA_CHNENBL0_IMX31 0x080
Sascha Hauer1ec1e822010-09-30 13:56:34 +000073#define SDMA_CHNPRI_0 0x100
74
75/*
76 * Buffer descriptor status values.
77 */
78#define BD_DONE 0x01
79#define BD_WRAP 0x02
80#define BD_CONT 0x04
81#define BD_INTR 0x08
82#define BD_RROR 0x10
83#define BD_LAST 0x20
84#define BD_EXTD 0x80
85
86/*
87 * Data Node descriptor status values.
88 */
89#define DND_END_OF_FRAME 0x80
90#define DND_END_OF_XFER 0x40
91#define DND_DONE 0x20
92#define DND_UNUSED 0x01
93
94/*
95 * IPCV2 descriptor status values.
96 */
97#define BD_IPCV2_END_OF_FRAME 0x40
98
99#define IPCV2_MAX_NODES 50
100/*
101 * Error bit set in the CCB status field by the SDMA,
102 * in setbd routine, in case of a transfer error
103 */
104#define DATA_ERROR 0x10000000
105
106/*
107 * Buffer descriptor commands.
108 */
109#define C0_ADDR 0x01
110#define C0_LOAD 0x02
111#define C0_DUMP 0x03
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
118/*
119 * Change endianness indicator in the BD command field
120 */
121#define CHANGE_ENDIANNESS 0x80
122
123/*
124 * Mode/Count of data node descriptors - IPCv2
125 */
126struct 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 */
130};
131
132/*
133 * Buffer descriptor
134 */
135struct 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));
140
141/**
142 * struct sdma_channel_control - Channel control Block
143 *
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
147 * control blocks
148 */
149struct sdma_channel_control {
150 u32 current_bd_ptr;
151 u32 base_bd_ptr;
152 u32 unused[2];
153} __attribute__ ((packed));
154
155/**
156 * struct sdma_state_registers - SDMA context for a channel
157 *
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
165 * @lm: loop mode
166 */
167struct sdma_state_registers {
168 u32 pc :14;
169 u32 unused1: 1;
170 u32 t : 1;
171 u32 rpc :14;
172 u32 unused0: 1;
173 u32 sf : 1;
174 u32 spc :14;
175 u32 unused2: 1;
176 u32 df : 1;
177 u32 epc :14;
178 u32 lm : 2;
179} __attribute__ ((packed));
180
181/**
182 * struct sdma_context_data - sdma context specific to a channel
183 *
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
200 */
201struct sdma_context_data {
202 struct sdma_state_registers channel_state;
203 u32 gReg[8];
204 u32 mda;
205 u32 msa;
206 u32 ms;
207 u32 md;
208 u32 pda;
209 u32 psa;
210 u32 ps;
211 u32 pd;
212 u32 ca;
213 u32 cs;
214 u32 dda;
215 u32 dsa;
216 u32 ds;
217 u32 dd;
218 u32 scratch0;
219 u32 scratch1;
220 u32 scratch2;
221 u32 scratch3;
222 u32 scratch4;
223 u32 scratch5;
224 u32 scratch6;
225 u32 scratch7;
226} __attribute__ ((packed));
227
228#define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
229
230struct sdma_engine;
231
232/**
233 * struct sdma_channel - housekeeping for a SDMA channel
234 *
235 * @sdma pointer to the SDMA engine for this channel
Sascha Hauer23889c62011-01-31 10:56:58 +0100236 * @channel the channel number, matches dmaengine chan_id + 1
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000237 * @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
245 */
246struct 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;
256 unsigned int num_bd;
257 struct sdma_buffer_descriptor *bd;
258 dma_addr_t bd_phys;
259 unsigned int pc_from_device, pc_to_device;
260 unsigned long flags;
261 dma_addr_t per_address;
262 u32 event_mask0, event_mask1;
263 u32 watermark_level;
264 u32 shp_addr, per_addr;
265 struct dma_chan chan;
266 spinlock_t lock;
267 struct dma_async_tx_descriptor desc;
268 dma_cookie_t last_completed;
269 enum dma_status status;
270};
271
272#define IMX_DMA_SG_LOOP (1 << 0)
273
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
278
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000279#define SDMA_FIRMWARE_MAGIC 0x414d4453
280
281/**
282 * struct sdma_firmware_header - Layout of the firmware image
283 *
284 * @magic "SDMA"
285 * @version_major increased whenever layout of struct sdma_script_start_addrs
286 * changes.
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)
294 */
295struct sdma_firmware_header {
296 u32 magic;
297 u32 version_major;
298 u32 version_minor;
299 u32 script_addrs_start;
300 u32 num_script_addrs;
301 u32 ram_code_start;
302 u32 ram_code_size;
303};
304
Shawn Guo62550cd2011-07-13 21:33:17 +0800305enum sdma_devtype {
306 IMX31_SDMA, /* runs on i.mx31 */
307 IMX35_SDMA, /* runs on i.mx35 and later */
308};
309
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000310struct sdma_engine {
311 struct device *dev;
Sascha Hauerb9b3f822011-01-12 12:12:31 +0100312 struct device_dma_parameters dma_parms;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000313 struct sdma_channel channel[MAX_DMA_CHANNELS];
314 struct sdma_channel_control *channel_control;
315 void __iomem *regs;
Shawn Guo62550cd2011-07-13 21:33:17 +0800316 enum sdma_devtype devtype;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000317 unsigned int num_events;
318 struct sdma_context_data *context;
319 dma_addr_t context_phys;
320 struct dma_device dma_device;
321 struct clk *clk;
322 struct sdma_script_start_addrs *script_addrs;
323};
324
Shawn Guo62550cd2011-07-13 21:33:17 +0800325static struct platform_device_id sdma_devtypes[] = {
326 {
327 .name = "imx31-sdma",
328 .driver_data = IMX31_SDMA,
329 }, {
330 .name = "imx35-sdma",
331 .driver_data = IMX35_SDMA,
332 }, {
333 /* sentinel */
334 }
335};
336MODULE_DEVICE_TABLE(platform, sdma_devtypes);
337
Shawn Guo580975d2011-07-14 08:35:48 +0800338static const struct of_device_id sdma_dt_ids[] = {
339 { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], },
340 { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], },
341 { /* sentinel */ }
342};
343MODULE_DEVICE_TABLE(of, sdma_dt_ids);
344
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000345#define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
346#define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
347#define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
348#define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
349
350static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
351{
Shawn Guo62550cd2011-07-13 21:33:17 +0800352 u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
353 SDMA_CHNENBL0_IMX35);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000354 return chnenbl0 + event * 4;
355}
356
357static int sdma_config_ownership(struct sdma_channel *sdmac,
358 bool event_override, bool mcu_override, bool dsp_override)
359{
360 struct sdma_engine *sdma = sdmac->sdma;
361 int channel = sdmac->channel;
362 u32 evt, mcu, dsp;
363
364 if (event_override && mcu_override && dsp_override)
365 return -EINVAL;
366
367 evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
368 mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
369 dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
370
371 if (dsp_override)
372 dsp &= ~(1 << channel);
373 else
374 dsp |= (1 << channel);
375
376 if (event_override)
377 evt &= ~(1 << channel);
378 else
379 evt |= (1 << channel);
380
381 if (mcu_override)
382 mcu &= ~(1 << channel);
383 else
384 mcu |= (1 << channel);
385
386 __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
387 __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
388 __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
389
390 return 0;
391}
392
393/*
394 * sdma_run_channel - run a channel and wait till it's done
395 */
396static int sdma_run_channel(struct sdma_channel *sdmac)
397{
398 struct sdma_engine *sdma = sdmac->sdma;
399 int channel = sdmac->channel;
400 int ret;
401
402 init_completion(&sdmac->done);
403
404 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
405
406 ret = wait_for_completion_timeout(&sdmac->done, HZ);
407
408 return ret ? 0 : -ETIMEDOUT;
409}
410
411static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
412 u32 address)
413{
414 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
415 void *buf_virt;
416 dma_addr_t buf_phys;
417 int ret;
418
419 buf_virt = dma_alloc_coherent(NULL,
420 size,
421 &buf_phys, GFP_KERNEL);
422 if (!buf_virt)
423 return -ENOMEM;
424
425 bd0->mode.command = C0_SETPM;
426 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
427 bd0->mode.count = size / 2;
428 bd0->buffer_addr = buf_phys;
429 bd0->ext_buffer_addr = address;
430
431 memcpy(buf_virt, buf, size);
432
433 ret = sdma_run_channel(&sdma->channel[0]);
434
435 dma_free_coherent(NULL, size, buf_virt, buf_phys);
436
437 return ret;
438}
439
440static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
441{
442 struct sdma_engine *sdma = sdmac->sdma;
443 int channel = sdmac->channel;
444 u32 val;
445 u32 chnenbl = chnenbl_ofs(sdma, event);
446
447 val = __raw_readl(sdma->regs + chnenbl);
448 val |= (1 << channel);
449 __raw_writel(val, sdma->regs + chnenbl);
450}
451
452static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
453{
454 struct sdma_engine *sdma = sdmac->sdma;
455 int channel = sdmac->channel;
456 u32 chnenbl = chnenbl_ofs(sdma, event);
457 u32 val;
458
459 val = __raw_readl(sdma->regs + chnenbl);
460 val &= ~(1 << channel);
461 __raw_writel(val, sdma->regs + chnenbl);
462}
463
464static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
465{
466 struct sdma_buffer_descriptor *bd;
467
468 /*
469 * loop mode. Iterate over descriptors, re-setup them and
470 * call callback function.
471 */
472 while (1) {
473 bd = &sdmac->bd[sdmac->buf_tail];
474
475 if (bd->mode.status & BD_DONE)
476 break;
477
478 if (bd->mode.status & BD_RROR)
479 sdmac->status = DMA_ERROR;
480 else
Shawn Guo1e9cebb2011-01-20 05:50:38 +0800481 sdmac->status = DMA_IN_PROGRESS;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000482
483 bd->mode.status |= BD_DONE;
484 sdmac->buf_tail++;
485 sdmac->buf_tail %= sdmac->num_bd;
486
487 if (sdmac->desc.callback)
488 sdmac->desc.callback(sdmac->desc.callback_param);
489 }
490}
491
492static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
493{
494 struct sdma_buffer_descriptor *bd;
495 int i, error = 0;
496
497 /*
498 * non loop mode. Iterate over all descriptors, collect
499 * errors and call callback function
500 */
501 for (i = 0; i < sdmac->num_bd; i++) {
502 bd = &sdmac->bd[i];
503
504 if (bd->mode.status & (BD_DONE | BD_RROR))
505 error = -EIO;
506 }
507
508 if (error)
509 sdmac->status = DMA_ERROR;
510 else
511 sdmac->status = DMA_SUCCESS;
512
513 if (sdmac->desc.callback)
514 sdmac->desc.callback(sdmac->desc.callback_param);
515 sdmac->last_completed = sdmac->desc.cookie;
516}
517
518static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
519{
520 complete(&sdmac->done);
521
522 /* not interested in channel 0 interrupts */
523 if (sdmac->channel == 0)
524 return;
525
526 if (sdmac->flags & IMX_DMA_SG_LOOP)
527 sdma_handle_channel_loop(sdmac);
528 else
529 mxc_sdma_handle_channel_normal(sdmac);
530}
531
532static irqreturn_t sdma_int_handler(int irq, void *dev_id)
533{
534 struct sdma_engine *sdma = dev_id;
535 u32 stat;
536
537 stat = __raw_readl(sdma->regs + SDMA_H_INTR);
538 __raw_writel(stat, sdma->regs + SDMA_H_INTR);
539
540 while (stat) {
541 int channel = fls(stat) - 1;
542 struct sdma_channel *sdmac = &sdma->channel[channel];
543
544 mxc_sdma_handle_channel(sdmac);
545
546 stat &= ~(1 << channel);
547 }
548
549 return IRQ_HANDLED;
550}
551
552/*
553 * sets the pc of SDMA script according to the peripheral type
554 */
555static void sdma_get_pc(struct sdma_channel *sdmac,
556 enum sdma_peripheral_type peripheral_type)
557{
558 struct sdma_engine *sdma = sdmac->sdma;
559 int per_2_emi = 0, emi_2_per = 0;
560 /*
561 * These are needed once we start to support transfers between
562 * two peripherals or memory-to-memory transfers
563 */
564 int per_2_per = 0, emi_2_emi = 0;
565
566 sdmac->pc_from_device = 0;
567 sdmac->pc_to_device = 0;
568
569 switch (peripheral_type) {
570 case IMX_DMATYPE_MEMORY:
571 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
572 break;
573 case IMX_DMATYPE_DSP:
574 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
575 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
576 break;
577 case IMX_DMATYPE_FIRI:
578 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
579 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
580 break;
581 case IMX_DMATYPE_UART:
582 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
583 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
584 break;
585 case IMX_DMATYPE_UART_SP:
586 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
587 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
588 break;
589 case IMX_DMATYPE_ATA:
590 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
591 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
592 break;
593 case IMX_DMATYPE_CSPI:
594 case IMX_DMATYPE_EXT:
595 case IMX_DMATYPE_SSI:
596 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
597 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
598 break;
599 case IMX_DMATYPE_SSI_SP:
600 case IMX_DMATYPE_MMC:
601 case IMX_DMATYPE_SDHC:
602 case IMX_DMATYPE_CSPI_SP:
603 case IMX_DMATYPE_ESAI:
604 case IMX_DMATYPE_MSHC_SP:
605 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
606 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
607 break;
608 case IMX_DMATYPE_ASRC:
609 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
610 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
611 per_2_per = sdma->script_addrs->per_2_per_addr;
612 break;
613 case IMX_DMATYPE_MSHC:
614 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
615 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
616 break;
617 case IMX_DMATYPE_CCM:
618 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
619 break;
620 case IMX_DMATYPE_SPDIF:
621 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
622 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
623 break;
624 case IMX_DMATYPE_IPU_MEMORY:
625 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
626 break;
627 default:
628 break;
629 }
630
631 sdmac->pc_from_device = per_2_emi;
632 sdmac->pc_to_device = emi_2_per;
633}
634
635static int sdma_load_context(struct sdma_channel *sdmac)
636{
637 struct sdma_engine *sdma = sdmac->sdma;
638 int channel = sdmac->channel;
639 int load_address;
640 struct sdma_context_data *context = sdma->context;
641 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
642 int ret;
643
644 if (sdmac->direction == DMA_FROM_DEVICE) {
645 load_address = sdmac->pc_from_device;
646 } else {
647 load_address = sdmac->pc_to_device;
648 }
649
650 if (load_address < 0)
651 return load_address;
652
653 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
654 dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
655 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
656 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
657 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
658 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
659
660 memset(context, 0, sizeof(*context));
661 context->channel_state.pc = load_address;
662
663 /* Send by context the event mask,base address for peripheral
664 * and watermark level
665 */
666 context->gReg[0] = sdmac->event_mask1;
667 context->gReg[1] = sdmac->event_mask0;
668 context->gReg[2] = sdmac->per_addr;
669 context->gReg[6] = sdmac->shp_addr;
670 context->gReg[7] = sdmac->watermark_level;
671
672 bd0->mode.command = C0_SETDM;
673 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
674 bd0->mode.count = sizeof(*context) / 4;
675 bd0->buffer_addr = sdma->context_phys;
676 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
677
678 ret = sdma_run_channel(&sdma->channel[0]);
679
680 return ret;
681}
682
683static void sdma_disable_channel(struct sdma_channel *sdmac)
684{
685 struct sdma_engine *sdma = sdmac->sdma;
686 int channel = sdmac->channel;
687
688 __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
689 sdmac->status = DMA_ERROR;
690}
691
692static int sdma_config_channel(struct sdma_channel *sdmac)
693{
694 int ret;
695
696 sdma_disable_channel(sdmac);
697
698 sdmac->event_mask0 = 0;
699 sdmac->event_mask1 = 0;
700 sdmac->shp_addr = 0;
701 sdmac->per_addr = 0;
702
703 if (sdmac->event_id0) {
704 if (sdmac->event_id0 > 32)
705 return -EINVAL;
706 sdma_event_enable(sdmac, sdmac->event_id0);
707 }
708
709 switch (sdmac->peripheral_type) {
710 case IMX_DMATYPE_DSP:
711 sdma_config_ownership(sdmac, false, true, true);
712 break;
713 case IMX_DMATYPE_MEMORY:
714 sdma_config_ownership(sdmac, false, true, false);
715 break;
716 default:
717 sdma_config_ownership(sdmac, true, true, false);
718 break;
719 }
720
721 sdma_get_pc(sdmac, sdmac->peripheral_type);
722
723 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
724 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
725 /* Handle multiple event channels differently */
726 if (sdmac->event_id1) {
727 sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
728 if (sdmac->event_id1 > 31)
729 sdmac->watermark_level |= 1 << 31;
730 sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
731 if (sdmac->event_id0 > 31)
732 sdmac->watermark_level |= 1 << 30;
733 } else {
734 sdmac->event_mask0 = 1 << sdmac->event_id0;
735 sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
736 }
737 /* Watermark Level */
738 sdmac->watermark_level |= sdmac->watermark_level;
739 /* Address */
740 sdmac->shp_addr = sdmac->per_address;
741 } else {
742 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
743 }
744
745 ret = sdma_load_context(sdmac);
746
747 return ret;
748}
749
750static int sdma_set_channel_priority(struct sdma_channel *sdmac,
751 unsigned int priority)
752{
753 struct sdma_engine *sdma = sdmac->sdma;
754 int channel = sdmac->channel;
755
756 if (priority < MXC_SDMA_MIN_PRIORITY
757 || priority > MXC_SDMA_MAX_PRIORITY) {
758 return -EINVAL;
759 }
760
761 __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
762
763 return 0;
764}
765
766static int sdma_request_channel(struct sdma_channel *sdmac)
767{
768 struct sdma_engine *sdma = sdmac->sdma;
769 int channel = sdmac->channel;
770 int ret = -EBUSY;
771
772 sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
773 if (!sdmac->bd) {
774 ret = -ENOMEM;
775 goto out;
776 }
777
778 memset(sdmac->bd, 0, PAGE_SIZE);
779
780 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
781 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
782
783 clk_enable(sdma->clk);
784
785 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
786
787 init_completion(&sdmac->done);
788
789 sdmac->buf_tail = 0;
790
791 return 0;
792out:
793
794 return ret;
795}
796
797static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
798{
799 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
800}
801
Shawn Guod718f4e2011-01-17 22:39:24 +0800802static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000803{
Shawn Guod718f4e2011-01-17 22:39:24 +0800804 dma_cookie_t cookie = sdmac->chan.cookie;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000805
806 if (++cookie < 0)
807 cookie = 1;
808
Shawn Guod718f4e2011-01-17 22:39:24 +0800809 sdmac->chan.cookie = cookie;
810 sdmac->desc.cookie = cookie;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000811
812 return cookie;
813}
814
815static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
816{
817 return container_of(chan, struct sdma_channel, chan);
818}
819
820static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
821{
822 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
823 struct sdma_engine *sdma = sdmac->sdma;
824 dma_cookie_t cookie;
825
826 spin_lock_irq(&sdmac->lock);
827
828 cookie = sdma_assign_cookie(sdmac);
829
Sascha Hauer23889c62011-01-31 10:56:58 +0100830 sdma_enable_channel(sdma, sdmac->channel);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000831
832 spin_unlock_irq(&sdmac->lock);
833
834 return cookie;
835}
836
837static int sdma_alloc_chan_resources(struct dma_chan *chan)
838{
839 struct sdma_channel *sdmac = to_sdma_chan(chan);
840 struct imx_dma_data *data = chan->private;
841 int prio, ret;
842
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000843 if (!data)
844 return -EINVAL;
845
846 switch (data->priority) {
847 case DMA_PRIO_HIGH:
848 prio = 3;
849 break;
850 case DMA_PRIO_MEDIUM:
851 prio = 2;
852 break;
853 case DMA_PRIO_LOW:
854 default:
855 prio = 1;
856 break;
857 }
858
859 sdmac->peripheral_type = data->peripheral_type;
860 sdmac->event_id0 = data->dma_request;
861 ret = sdma_set_channel_priority(sdmac, prio);
862 if (ret)
863 return ret;
864
865 ret = sdma_request_channel(sdmac);
866 if (ret)
867 return ret;
868
869 dma_async_tx_descriptor_init(&sdmac->desc, chan);
870 sdmac->desc.tx_submit = sdma_tx_submit;
871 /* txd.flags will be overwritten in prep funcs */
872 sdmac->desc.flags = DMA_CTRL_ACK;
873
874 return 0;
875}
876
877static void sdma_free_chan_resources(struct dma_chan *chan)
878{
879 struct sdma_channel *sdmac = to_sdma_chan(chan);
880 struct sdma_engine *sdma = sdmac->sdma;
881
882 sdma_disable_channel(sdmac);
883
884 if (sdmac->event_id0)
885 sdma_event_disable(sdmac, sdmac->event_id0);
886 if (sdmac->event_id1)
887 sdma_event_disable(sdmac, sdmac->event_id1);
888
889 sdmac->event_id0 = 0;
890 sdmac->event_id1 = 0;
891
892 sdma_set_channel_priority(sdmac, 0);
893
894 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
895
896 clk_disable(sdma->clk);
897}
898
899static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
900 struct dma_chan *chan, struct scatterlist *sgl,
901 unsigned int sg_len, enum dma_data_direction direction,
902 unsigned long flags)
903{
904 struct sdma_channel *sdmac = to_sdma_chan(chan);
905 struct sdma_engine *sdma = sdmac->sdma;
906 int ret, i, count;
Sascha Hauer23889c62011-01-31 10:56:58 +0100907 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000908 struct scatterlist *sg;
909
910 if (sdmac->status == DMA_IN_PROGRESS)
911 return NULL;
912 sdmac->status = DMA_IN_PROGRESS;
913
914 sdmac->flags = 0;
915
916 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
917 sg_len, channel);
918
919 sdmac->direction = direction;
920 ret = sdma_load_context(sdmac);
921 if (ret)
922 goto err_out;
923
924 if (sg_len > NUM_BD) {
925 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
926 channel, sg_len, NUM_BD);
927 ret = -EINVAL;
928 goto err_out;
929 }
930
931 for_each_sg(sgl, sg, sg_len, i) {
932 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
933 int param;
934
Anatolij Gustschind2f5c272010-11-22 18:35:18 +0100935 bd->buffer_addr = sg->dma_address;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000936
937 count = sg->length;
938
939 if (count > 0xffff) {
940 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
941 channel, count, 0xffff);
942 ret = -EINVAL;
943 goto err_out;
944 }
945
946 bd->mode.count = count;
947
948 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
949 ret = -EINVAL;
950 goto err_out;
951 }
Sascha Hauer1fa81c22011-01-12 13:02:28 +0100952
953 switch (sdmac->word_size) {
954 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000955 bd->mode.command = 0;
Sascha Hauer1fa81c22011-01-12 13:02:28 +0100956 if (count & 3 || sg->dma_address & 3)
957 return NULL;
958 break;
959 case DMA_SLAVE_BUSWIDTH_2_BYTES:
960 bd->mode.command = 2;
961 if (count & 1 || sg->dma_address & 1)
962 return NULL;
963 break;
964 case DMA_SLAVE_BUSWIDTH_1_BYTE:
965 bd->mode.command = 1;
966 break;
967 default:
968 return NULL;
969 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000970
971 param = BD_DONE | BD_EXTD | BD_CONT;
972
Shawn Guo341b9412011-01-20 05:50:39 +0800973 if (i + 1 == sg_len) {
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000974 param |= BD_INTR;
Shawn Guo341b9412011-01-20 05:50:39 +0800975 param |= BD_LAST;
976 param &= ~BD_CONT;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000977 }
978
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000979 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
980 i, count, sg->dma_address,
981 param & BD_WRAP ? "wrap" : "",
982 param & BD_INTR ? " intr" : "");
983
984 bd->mode.status = param;
985 }
986
987 sdmac->num_bd = sg_len;
988 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
989
990 return &sdmac->desc;
991err_out:
Shawn Guo4b2ce9d2011-01-20 05:50:36 +0800992 sdmac->status = DMA_ERROR;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000993 return NULL;
994}
995
996static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
997 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
998 size_t period_len, enum dma_data_direction direction)
999{
1000 struct sdma_channel *sdmac = to_sdma_chan(chan);
1001 struct sdma_engine *sdma = sdmac->sdma;
1002 int num_periods = buf_len / period_len;
Sascha Hauer23889c62011-01-31 10:56:58 +01001003 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001004 int ret, i = 0, buf = 0;
1005
1006 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1007
1008 if (sdmac->status == DMA_IN_PROGRESS)
1009 return NULL;
1010
1011 sdmac->status = DMA_IN_PROGRESS;
1012
1013 sdmac->flags |= IMX_DMA_SG_LOOP;
1014 sdmac->direction = direction;
1015 ret = sdma_load_context(sdmac);
1016 if (ret)
1017 goto err_out;
1018
1019 if (num_periods > NUM_BD) {
1020 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1021 channel, num_periods, NUM_BD);
1022 goto err_out;
1023 }
1024
1025 if (period_len > 0xffff) {
1026 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
1027 channel, period_len, 0xffff);
1028 goto err_out;
1029 }
1030
1031 while (buf < buf_len) {
1032 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1033 int param;
1034
1035 bd->buffer_addr = dma_addr;
1036
1037 bd->mode.count = period_len;
1038
1039 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1040 goto err_out;
1041 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1042 bd->mode.command = 0;
1043 else
1044 bd->mode.command = sdmac->word_size;
1045
1046 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1047 if (i + 1 == num_periods)
1048 param |= BD_WRAP;
1049
1050 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
1051 i, period_len, dma_addr,
1052 param & BD_WRAP ? "wrap" : "",
1053 param & BD_INTR ? " intr" : "");
1054
1055 bd->mode.status = param;
1056
1057 dma_addr += period_len;
1058 buf += period_len;
1059
1060 i++;
1061 }
1062
1063 sdmac->num_bd = num_periods;
1064 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1065
1066 return &sdmac->desc;
1067err_out:
1068 sdmac->status = DMA_ERROR;
1069 return NULL;
1070}
1071
1072static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1073 unsigned long arg)
1074{
1075 struct sdma_channel *sdmac = to_sdma_chan(chan);
1076 struct dma_slave_config *dmaengine_cfg = (void *)arg;
1077
1078 switch (cmd) {
1079 case DMA_TERMINATE_ALL:
1080 sdma_disable_channel(sdmac);
1081 return 0;
1082 case DMA_SLAVE_CONFIG:
1083 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
1084 sdmac->per_address = dmaengine_cfg->src_addr;
1085 sdmac->watermark_level = dmaengine_cfg->src_maxburst;
1086 sdmac->word_size = dmaengine_cfg->src_addr_width;
1087 } else {
1088 sdmac->per_address = dmaengine_cfg->dst_addr;
1089 sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
1090 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1091 }
1092 return sdma_config_channel(sdmac);
1093 default:
1094 return -ENOSYS;
1095 }
1096
1097 return -EINVAL;
1098}
1099
1100static enum dma_status sdma_tx_status(struct dma_chan *chan,
1101 dma_cookie_t cookie,
1102 struct dma_tx_state *txstate)
1103{
1104 struct sdma_channel *sdmac = to_sdma_chan(chan);
1105 dma_cookie_t last_used;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001106
1107 last_used = chan->cookie;
1108
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001109 dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
1110
Shawn Guo8a965912011-01-20 05:50:37 +08001111 return sdmac->status;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001112}
1113
1114static void sdma_issue_pending(struct dma_chan *chan)
1115{
1116 /*
1117 * Nothing to do. We only have a single descriptor
1118 */
1119}
1120
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001121#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1122
1123static void sdma_add_scripts(struct sdma_engine *sdma,
1124 const struct sdma_script_start_addrs *addr)
1125{
1126 s32 *addr_arr = (u32 *)addr;
1127 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1128 int i;
1129
1130 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1131 if (addr_arr[i] > 0)
1132 saddr_arr[i] = addr_arr[i];
1133}
1134
1135static int __init sdma_get_firmware(struct sdma_engine *sdma,
Shawn Guo2e534b22011-06-22 22:41:31 +08001136 const char *fw_name)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001137{
1138 const struct firmware *fw;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001139 const struct sdma_firmware_header *header;
1140 int ret;
1141 const struct sdma_script_start_addrs *addr;
1142 unsigned short *ram_code;
1143
Shawn Guo40ad5b32011-07-15 17:25:28 +08001144 ret = request_firmware(&fw, fw_name, sdma->dev);
1145 if (ret)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001146 return ret;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001147
1148 if (fw->size < sizeof(*header))
1149 goto err_firmware;
1150
1151 header = (struct sdma_firmware_header *)fw->data;
1152
1153 if (header->magic != SDMA_FIRMWARE_MAGIC)
1154 goto err_firmware;
1155 if (header->ram_code_start + header->ram_code_size > fw->size)
1156 goto err_firmware;
1157
1158 addr = (void *)header + header->script_addrs_start;
1159 ram_code = (void *)header + header->ram_code_start;
1160
1161 clk_enable(sdma->clk);
1162 /* download the RAM image for SDMA */
1163 sdma_load_script(sdma, ram_code,
1164 header->ram_code_size,
Sascha Hauer6866fd32011-01-12 11:18:14 +01001165 addr->ram_code_start_addr);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001166 clk_disable(sdma->clk);
1167
1168 sdma_add_scripts(sdma, addr);
1169
1170 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1171 header->version_major,
1172 header->version_minor);
1173
1174err_firmware:
1175 release_firmware(fw);
1176
1177 return ret;
1178}
1179
1180static int __init sdma_init(struct sdma_engine *sdma)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001181{
1182 int i, ret;
1183 dma_addr_t ccb_phys;
1184
Shawn Guo62550cd2011-07-13 21:33:17 +08001185 switch (sdma->devtype) {
1186 case IMX31_SDMA:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001187 sdma->num_events = 32;
1188 break;
Shawn Guo62550cd2011-07-13 21:33:17 +08001189 case IMX35_SDMA:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001190 sdma->num_events = 48;
1191 break;
1192 default:
Shawn Guo62550cd2011-07-13 21:33:17 +08001193 dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
1194 sdma->devtype);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001195 return -ENODEV;
1196 }
1197
1198 clk_enable(sdma->clk);
1199
1200 /* Be sure SDMA has not started yet */
1201 __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
1202
1203 sdma->channel_control = dma_alloc_coherent(NULL,
1204 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1205 sizeof(struct sdma_context_data),
1206 &ccb_phys, GFP_KERNEL);
1207
1208 if (!sdma->channel_control) {
1209 ret = -ENOMEM;
1210 goto err_dma_alloc;
1211 }
1212
1213 sdma->context = (void *)sdma->channel_control +
1214 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1215 sdma->context_phys = ccb_phys +
1216 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1217
1218 /* Zero-out the CCB structures array just allocated */
1219 memset(sdma->channel_control, 0,
1220 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1221
1222 /* disable all channels */
1223 for (i = 0; i < sdma->num_events; i++)
1224 __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
1225
1226 /* All channels have priority 0 */
1227 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1228 __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1229
1230 ret = sdma_request_channel(&sdma->channel[0]);
1231 if (ret)
1232 goto err_dma_alloc;
1233
1234 sdma_config_ownership(&sdma->channel[0], false, true, false);
1235
1236 /* Set Command Channel (Channel Zero) */
1237 __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
1238
1239 /* Set bits of CONFIG register but with static context switching */
1240 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1241 __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
1242
1243 __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1244
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001245 /* Set bits of CONFIG register with given context switching mode */
1246 __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
1247
1248 /* Initializes channel's priorities */
1249 sdma_set_channel_priority(&sdma->channel[0], 7);
1250
1251 clk_disable(sdma->clk);
1252
1253 return 0;
1254
1255err_dma_alloc:
1256 clk_disable(sdma->clk);
1257 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1258 return ret;
1259}
1260
1261static int __init sdma_probe(struct platform_device *pdev)
1262{
Shawn Guo580975d2011-07-14 08:35:48 +08001263 const struct of_device_id *of_id =
1264 of_match_device(sdma_dt_ids, &pdev->dev);
1265 struct device_node *np = pdev->dev.of_node;
1266 const char *fw_name;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001267 int ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001268 int irq;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001269 struct resource *iores;
1270 struct sdma_platform_data *pdata = pdev->dev.platform_data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001271 int i;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001272 struct sdma_engine *sdma;
1273
1274 sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
1275 if (!sdma)
1276 return -ENOMEM;
1277
1278 sdma->dev = &pdev->dev;
1279
1280 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1281 irq = platform_get_irq(pdev, 0);
Shawn Guo580975d2011-07-14 08:35:48 +08001282 if (!iores || irq < 0) {
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001283 ret = -EINVAL;
1284 goto err_irq;
1285 }
1286
1287 if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
1288 ret = -EBUSY;
1289 goto err_request_region;
1290 }
1291
1292 sdma->clk = clk_get(&pdev->dev, NULL);
1293 if (IS_ERR(sdma->clk)) {
1294 ret = PTR_ERR(sdma->clk);
1295 goto err_clk;
1296 }
1297
1298 sdma->regs = ioremap(iores->start, resource_size(iores));
1299 if (!sdma->regs) {
1300 ret = -ENOMEM;
1301 goto err_ioremap;
1302 }
1303
1304 ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
1305 if (ret)
1306 goto err_request_irq;
1307
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001308 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
Axel Lin1c1d9542011-07-12 21:00:13 +08001309 if (!sdma->script_addrs) {
1310 ret = -ENOMEM;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001311 goto err_alloc;
Axel Lin1c1d9542011-07-12 21:00:13 +08001312 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001313
Shawn Guo580975d2011-07-14 08:35:48 +08001314 if (of_id)
1315 pdev->id_entry = of_id->data;
Shawn Guo62550cd2011-07-13 21:33:17 +08001316 sdma->devtype = pdev->id_entry->driver_data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001317
Sascha Hauer7214a8b2011-01-31 10:21:35 +01001318 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1319 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1320
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001321 INIT_LIST_HEAD(&sdma->dma_device.channels);
1322 /* Initialize channel parameters */
1323 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1324 struct sdma_channel *sdmac = &sdma->channel[i];
1325
1326 sdmac->sdma = sdma;
1327 spin_lock_init(&sdmac->lock);
1328
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001329 sdmac->chan.device = &sdma->dma_device;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001330 sdmac->channel = i;
1331
Sascha Hauer23889c62011-01-31 10:56:58 +01001332 /*
1333 * Add the channel to the DMAC list. Do not add channel 0 though
1334 * because we need it internally in the SDMA driver. This also means
1335 * that channel 0 in dmaengine counting matches sdma channel 1.
1336 */
1337 if (i)
1338 list_add_tail(&sdmac->chan.device_node,
1339 &sdma->dma_device.channels);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001340 }
1341
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001342 ret = sdma_init(sdma);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001343 if (ret)
1344 goto err_init;
1345
Shawn Guo580975d2011-07-14 08:35:48 +08001346 if (pdata && pdata->script_addrs)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001347 sdma_add_scripts(sdma, pdata->script_addrs);
1348
Shawn Guo580975d2011-07-14 08:35:48 +08001349 if (pdata) {
1350 sdma_get_firmware(sdma, pdata->fw_name);
1351 } else {
1352 /*
1353 * Because that device tree does not encode ROM script address,
1354 * the RAM script in firmware is mandatory for device tree
1355 * probe, otherwise it fails.
1356 */
1357 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1358 &fw_name);
1359 if (ret) {
1360 dev_err(&pdev->dev, "failed to get firmware name\n");
1361 goto err_init;
1362 }
1363
1364 ret = sdma_get_firmware(sdma, fw_name);
1365 if (ret) {
1366 dev_err(&pdev->dev, "failed to get firmware\n");
1367 goto err_init;
1368 }
1369 }
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001370
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001371 sdma->dma_device.dev = &pdev->dev;
1372
1373 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1374 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1375 sdma->dma_device.device_tx_status = sdma_tx_status;
1376 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1377 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1378 sdma->dma_device.device_control = sdma_control;
1379 sdma->dma_device.device_issue_pending = sdma_issue_pending;
Sascha Hauerb9b3f822011-01-12 12:12:31 +01001380 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1381 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001382
1383 ret = dma_async_device_register(&sdma->dma_device);
1384 if (ret) {
1385 dev_err(&pdev->dev, "unable to register\n");
1386 goto err_init;
1387 }
1388
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001389 dev_info(sdma->dev, "initialized\n");
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001390
1391 return 0;
1392
1393err_init:
1394 kfree(sdma->script_addrs);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001395err_alloc:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001396 free_irq(irq, sdma);
1397err_request_irq:
1398 iounmap(sdma->regs);
1399err_ioremap:
1400 clk_put(sdma->clk);
1401err_clk:
1402 release_mem_region(iores->start, resource_size(iores));
1403err_request_region:
1404err_irq:
1405 kfree(sdma);
Shawn Guo939fd4f2011-01-19 19:13:06 +08001406 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001407}
1408
1409static int __exit sdma_remove(struct platform_device *pdev)
1410{
1411 return -EBUSY;
1412}
1413
1414static struct platform_driver sdma_driver = {
1415 .driver = {
1416 .name = "imx-sdma",
Shawn Guo580975d2011-07-14 08:35:48 +08001417 .of_match_table = sdma_dt_ids,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001418 },
Shawn Guo62550cd2011-07-13 21:33:17 +08001419 .id_table = sdma_devtypes,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001420 .remove = __exit_p(sdma_remove),
1421};
1422
1423static int __init sdma_module_init(void)
1424{
1425 return platform_driver_probe(&sdma_driver, sdma_probe);
1426}
Sascha Hauerc989a7f2010-12-06 11:09:57 +01001427module_init(sdma_module_init);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001428
1429MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1430MODULE_DESCRIPTION("i.MX SDMA driver");
1431MODULE_LICENSE("GPL");