blob: df400691f9a067fc4e1d80622071e5518e80eb8d [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>
35
36#include <asm/irq.h>
37#include <mach/sdma.h>
38#include <mach/dma.h>
39#include <mach/hardware.h>
40
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
Shawn Guo62550cd2011-07-13 21:33:17 +080068#define SDMA_CHNENBL0_IMX35 0x200
69#define SDMA_CHNENBL0_IMX31 0x080
Sascha Hauer1ec1e822010-09-30 13:56:34 +000070#define SDMA_CHNPRI_0 0x100
71
72/*
73 * Buffer descriptor status values.
74 */
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
82
83/*
84 * Data Node descriptor status values.
85 */
86#define DND_END_OF_FRAME 0x80
87#define DND_END_OF_XFER 0x40
88#define DND_DONE 0x20
89#define DND_UNUSED 0x01
90
91/*
92 * IPCV2 descriptor status values.
93 */
94#define BD_IPCV2_END_OF_FRAME 0x40
95
96#define IPCV2_MAX_NODES 50
97/*
98 * Error bit set in the CCB status field by the SDMA,
99 * in setbd routine, in case of a transfer error
100 */
101#define DATA_ERROR 0x10000000
102
103/*
104 * Buffer descriptor commands.
105 */
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
115/*
116 * Change endianness indicator in the BD command field
117 */
118#define CHANGE_ENDIANNESS 0x80
119
120/*
121 * Mode/Count of data node descriptors - IPCv2
122 */
123struct 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 */
127};
128
129/*
130 * Buffer descriptor
131 */
132struct 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));
137
138/**
139 * struct sdma_channel_control - Channel control Block
140 *
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
145 */
146struct sdma_channel_control {
147 u32 current_bd_ptr;
148 u32 base_bd_ptr;
149 u32 unused[2];
150} __attribute__ ((packed));
151
152/**
153 * struct sdma_state_registers - SDMA context for a channel
154 *
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
163 */
164struct 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));
177
178/**
179 * struct sdma_context_data - sdma context specific to a channel
180 *
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
197 */
198struct 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));
224
225#define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
226
227struct sdma_engine;
228
229/**
230 * struct sdma_channel - housekeeping for a SDMA channel
231 *
232 * @sdma pointer to the SDMA engine for this channel
Sascha Hauer23889c62011-01-31 10:56:58 +0100233 * @channel the channel number, matches dmaengine chan_id + 1
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000234 * @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
242 */
243struct 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;
267};
268
269#define IMX_DMA_SG_LOOP (1 << 0)
270
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
275
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000276#define SDMA_FIRMWARE_MAGIC 0x414d4453
277
278/**
279 * struct sdma_firmware_header - Layout of the firmware image
280 *
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)
291 */
292struct 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;
300};
301
Shawn Guo62550cd2011-07-13 21:33:17 +0800302enum sdma_devtype {
303 IMX31_SDMA, /* runs on i.mx31 */
304 IMX35_SDMA, /* runs on i.mx35 and later */
305};
306
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000307struct sdma_engine {
308 struct device *dev;
Sascha Hauerb9b3f822011-01-12 12:12:31 +0100309 struct device_dma_parameters dma_parms;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000310 struct sdma_channel channel[MAX_DMA_CHANNELS];
311 struct sdma_channel_control *channel_control;
312 void __iomem *regs;
Shawn Guo62550cd2011-07-13 21:33:17 +0800313 enum sdma_devtype devtype;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000314 unsigned int num_events;
315 struct sdma_context_data *context;
316 dma_addr_t context_phys;
317 struct dma_device dma_device;
318 struct clk *clk;
319 struct sdma_script_start_addrs *script_addrs;
320};
321
Shawn Guo62550cd2011-07-13 21:33:17 +0800322static struct platform_device_id sdma_devtypes[] = {
323 {
324 .name = "imx31-sdma",
325 .driver_data = IMX31_SDMA,
326 }, {
327 .name = "imx35-sdma",
328 .driver_data = IMX35_SDMA,
329 }, {
330 /* sentinel */
331 }
332};
333MODULE_DEVICE_TABLE(platform, sdma_devtypes);
334
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000335#define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
336#define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
337#define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
338#define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
339
340static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
341{
Shawn Guo62550cd2011-07-13 21:33:17 +0800342 u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
343 SDMA_CHNENBL0_IMX35);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000344 return chnenbl0 + event * 4;
345}
346
347static int sdma_config_ownership(struct sdma_channel *sdmac,
348 bool event_override, bool mcu_override, bool dsp_override)
349{
350 struct sdma_engine *sdma = sdmac->sdma;
351 int channel = sdmac->channel;
352 u32 evt, mcu, dsp;
353
354 if (event_override && mcu_override && dsp_override)
355 return -EINVAL;
356
357 evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
358 mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
359 dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
360
361 if (dsp_override)
362 dsp &= ~(1 << channel);
363 else
364 dsp |= (1 << channel);
365
366 if (event_override)
367 evt &= ~(1 << channel);
368 else
369 evt |= (1 << channel);
370
371 if (mcu_override)
372 mcu &= ~(1 << channel);
373 else
374 mcu |= (1 << channel);
375
376 __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
377 __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
378 __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
379
380 return 0;
381}
382
383/*
384 * sdma_run_channel - run a channel and wait till it's done
385 */
386static int sdma_run_channel(struct sdma_channel *sdmac)
387{
388 struct sdma_engine *sdma = sdmac->sdma;
389 int channel = sdmac->channel;
390 int ret;
391
392 init_completion(&sdmac->done);
393
394 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
395
396 ret = wait_for_completion_timeout(&sdmac->done, HZ);
397
398 return ret ? 0 : -ETIMEDOUT;
399}
400
401static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
402 u32 address)
403{
404 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
405 void *buf_virt;
406 dma_addr_t buf_phys;
407 int ret;
408
409 buf_virt = dma_alloc_coherent(NULL,
410 size,
411 &buf_phys, GFP_KERNEL);
412 if (!buf_virt)
413 return -ENOMEM;
414
415 bd0->mode.command = C0_SETPM;
416 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
417 bd0->mode.count = size / 2;
418 bd0->buffer_addr = buf_phys;
419 bd0->ext_buffer_addr = address;
420
421 memcpy(buf_virt, buf, size);
422
423 ret = sdma_run_channel(&sdma->channel[0]);
424
425 dma_free_coherent(NULL, size, buf_virt, buf_phys);
426
427 return ret;
428}
429
430static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
431{
432 struct sdma_engine *sdma = sdmac->sdma;
433 int channel = sdmac->channel;
434 u32 val;
435 u32 chnenbl = chnenbl_ofs(sdma, event);
436
437 val = __raw_readl(sdma->regs + chnenbl);
438 val |= (1 << channel);
439 __raw_writel(val, sdma->regs + chnenbl);
440}
441
442static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
443{
444 struct sdma_engine *sdma = sdmac->sdma;
445 int channel = sdmac->channel;
446 u32 chnenbl = chnenbl_ofs(sdma, event);
447 u32 val;
448
449 val = __raw_readl(sdma->regs + chnenbl);
450 val &= ~(1 << channel);
451 __raw_writel(val, sdma->regs + chnenbl);
452}
453
454static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
455{
456 struct sdma_buffer_descriptor *bd;
457
458 /*
459 * loop mode. Iterate over descriptors, re-setup them and
460 * call callback function.
461 */
462 while (1) {
463 bd = &sdmac->bd[sdmac->buf_tail];
464
465 if (bd->mode.status & BD_DONE)
466 break;
467
468 if (bd->mode.status & BD_RROR)
469 sdmac->status = DMA_ERROR;
470 else
Shawn Guo1e9cebb2011-01-20 05:50:38 +0800471 sdmac->status = DMA_IN_PROGRESS;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000472
473 bd->mode.status |= BD_DONE;
474 sdmac->buf_tail++;
475 sdmac->buf_tail %= sdmac->num_bd;
476
477 if (sdmac->desc.callback)
478 sdmac->desc.callback(sdmac->desc.callback_param);
479 }
480}
481
482static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
483{
484 struct sdma_buffer_descriptor *bd;
485 int i, error = 0;
486
487 /*
488 * non loop mode. Iterate over all descriptors, collect
489 * errors and call callback function
490 */
491 for (i = 0; i < sdmac->num_bd; i++) {
492 bd = &sdmac->bd[i];
493
494 if (bd->mode.status & (BD_DONE | BD_RROR))
495 error = -EIO;
496 }
497
498 if (error)
499 sdmac->status = DMA_ERROR;
500 else
501 sdmac->status = DMA_SUCCESS;
502
503 if (sdmac->desc.callback)
504 sdmac->desc.callback(sdmac->desc.callback_param);
505 sdmac->last_completed = sdmac->desc.cookie;
506}
507
508static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
509{
510 complete(&sdmac->done);
511
512 /* not interested in channel 0 interrupts */
513 if (sdmac->channel == 0)
514 return;
515
516 if (sdmac->flags & IMX_DMA_SG_LOOP)
517 sdma_handle_channel_loop(sdmac);
518 else
519 mxc_sdma_handle_channel_normal(sdmac);
520}
521
522static irqreturn_t sdma_int_handler(int irq, void *dev_id)
523{
524 struct sdma_engine *sdma = dev_id;
525 u32 stat;
526
527 stat = __raw_readl(sdma->regs + SDMA_H_INTR);
528 __raw_writel(stat, sdma->regs + SDMA_H_INTR);
529
530 while (stat) {
531 int channel = fls(stat) - 1;
532 struct sdma_channel *sdmac = &sdma->channel[channel];
533
534 mxc_sdma_handle_channel(sdmac);
535
536 stat &= ~(1 << channel);
537 }
538
539 return IRQ_HANDLED;
540}
541
542/*
543 * sets the pc of SDMA script according to the peripheral type
544 */
545static void sdma_get_pc(struct sdma_channel *sdmac,
546 enum sdma_peripheral_type peripheral_type)
547{
548 struct sdma_engine *sdma = sdmac->sdma;
549 int per_2_emi = 0, emi_2_per = 0;
550 /*
551 * These are needed once we start to support transfers between
552 * two peripherals or memory-to-memory transfers
553 */
554 int per_2_per = 0, emi_2_emi = 0;
555
556 sdmac->pc_from_device = 0;
557 sdmac->pc_to_device = 0;
558
559 switch (peripheral_type) {
560 case IMX_DMATYPE_MEMORY:
561 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
562 break;
563 case IMX_DMATYPE_DSP:
564 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
565 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
566 break;
567 case IMX_DMATYPE_FIRI:
568 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
569 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
570 break;
571 case IMX_DMATYPE_UART:
572 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
573 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
574 break;
575 case IMX_DMATYPE_UART_SP:
576 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
577 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
578 break;
579 case IMX_DMATYPE_ATA:
580 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
581 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
582 break;
583 case IMX_DMATYPE_CSPI:
584 case IMX_DMATYPE_EXT:
585 case IMX_DMATYPE_SSI:
586 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
587 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
588 break;
589 case IMX_DMATYPE_SSI_SP:
590 case IMX_DMATYPE_MMC:
591 case IMX_DMATYPE_SDHC:
592 case IMX_DMATYPE_CSPI_SP:
593 case IMX_DMATYPE_ESAI:
594 case IMX_DMATYPE_MSHC_SP:
595 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
596 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
597 break;
598 case IMX_DMATYPE_ASRC:
599 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
600 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
601 per_2_per = sdma->script_addrs->per_2_per_addr;
602 break;
603 case IMX_DMATYPE_MSHC:
604 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
605 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
606 break;
607 case IMX_DMATYPE_CCM:
608 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
609 break;
610 case IMX_DMATYPE_SPDIF:
611 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
612 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
613 break;
614 case IMX_DMATYPE_IPU_MEMORY:
615 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
616 break;
617 default:
618 break;
619 }
620
621 sdmac->pc_from_device = per_2_emi;
622 sdmac->pc_to_device = emi_2_per;
623}
624
625static int sdma_load_context(struct sdma_channel *sdmac)
626{
627 struct sdma_engine *sdma = sdmac->sdma;
628 int channel = sdmac->channel;
629 int load_address;
630 struct sdma_context_data *context = sdma->context;
631 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
632 int ret;
633
634 if (sdmac->direction == DMA_FROM_DEVICE) {
635 load_address = sdmac->pc_from_device;
636 } else {
637 load_address = sdmac->pc_to_device;
638 }
639
640 if (load_address < 0)
641 return load_address;
642
643 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
644 dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
645 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
646 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
647 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
648 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
649
650 memset(context, 0, sizeof(*context));
651 context->channel_state.pc = load_address;
652
653 /* Send by context the event mask,base address for peripheral
654 * and watermark level
655 */
656 context->gReg[0] = sdmac->event_mask1;
657 context->gReg[1] = sdmac->event_mask0;
658 context->gReg[2] = sdmac->per_addr;
659 context->gReg[6] = sdmac->shp_addr;
660 context->gReg[7] = sdmac->watermark_level;
661
662 bd0->mode.command = C0_SETDM;
663 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
664 bd0->mode.count = sizeof(*context) / 4;
665 bd0->buffer_addr = sdma->context_phys;
666 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
667
668 ret = sdma_run_channel(&sdma->channel[0]);
669
670 return ret;
671}
672
673static void sdma_disable_channel(struct sdma_channel *sdmac)
674{
675 struct sdma_engine *sdma = sdmac->sdma;
676 int channel = sdmac->channel;
677
678 __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
679 sdmac->status = DMA_ERROR;
680}
681
682static int sdma_config_channel(struct sdma_channel *sdmac)
683{
684 int ret;
685
686 sdma_disable_channel(sdmac);
687
688 sdmac->event_mask0 = 0;
689 sdmac->event_mask1 = 0;
690 sdmac->shp_addr = 0;
691 sdmac->per_addr = 0;
692
693 if (sdmac->event_id0) {
694 if (sdmac->event_id0 > 32)
695 return -EINVAL;
696 sdma_event_enable(sdmac, sdmac->event_id0);
697 }
698
699 switch (sdmac->peripheral_type) {
700 case IMX_DMATYPE_DSP:
701 sdma_config_ownership(sdmac, false, true, true);
702 break;
703 case IMX_DMATYPE_MEMORY:
704 sdma_config_ownership(sdmac, false, true, false);
705 break;
706 default:
707 sdma_config_ownership(sdmac, true, true, false);
708 break;
709 }
710
711 sdma_get_pc(sdmac, sdmac->peripheral_type);
712
713 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
714 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
715 /* Handle multiple event channels differently */
716 if (sdmac->event_id1) {
717 sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
718 if (sdmac->event_id1 > 31)
719 sdmac->watermark_level |= 1 << 31;
720 sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
721 if (sdmac->event_id0 > 31)
722 sdmac->watermark_level |= 1 << 30;
723 } else {
724 sdmac->event_mask0 = 1 << sdmac->event_id0;
725 sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
726 }
727 /* Watermark Level */
728 sdmac->watermark_level |= sdmac->watermark_level;
729 /* Address */
730 sdmac->shp_addr = sdmac->per_address;
731 } else {
732 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
733 }
734
735 ret = sdma_load_context(sdmac);
736
737 return ret;
738}
739
740static int sdma_set_channel_priority(struct sdma_channel *sdmac,
741 unsigned int priority)
742{
743 struct sdma_engine *sdma = sdmac->sdma;
744 int channel = sdmac->channel;
745
746 if (priority < MXC_SDMA_MIN_PRIORITY
747 || priority > MXC_SDMA_MAX_PRIORITY) {
748 return -EINVAL;
749 }
750
751 __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
752
753 return 0;
754}
755
756static int sdma_request_channel(struct sdma_channel *sdmac)
757{
758 struct sdma_engine *sdma = sdmac->sdma;
759 int channel = sdmac->channel;
760 int ret = -EBUSY;
761
762 sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
763 if (!sdmac->bd) {
764 ret = -ENOMEM;
765 goto out;
766 }
767
768 memset(sdmac->bd, 0, PAGE_SIZE);
769
770 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
771 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
772
773 clk_enable(sdma->clk);
774
775 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
776
777 init_completion(&sdmac->done);
778
779 sdmac->buf_tail = 0;
780
781 return 0;
782out:
783
784 return ret;
785}
786
787static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
788{
789 __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
790}
791
Shawn Guod718f4e2011-01-17 22:39:24 +0800792static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000793{
Shawn Guod718f4e2011-01-17 22:39:24 +0800794 dma_cookie_t cookie = sdmac->chan.cookie;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000795
796 if (++cookie < 0)
797 cookie = 1;
798
Shawn Guod718f4e2011-01-17 22:39:24 +0800799 sdmac->chan.cookie = cookie;
800 sdmac->desc.cookie = cookie;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000801
802 return cookie;
803}
804
805static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
806{
807 return container_of(chan, struct sdma_channel, chan);
808}
809
810static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
811{
812 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
813 struct sdma_engine *sdma = sdmac->sdma;
814 dma_cookie_t cookie;
815
816 spin_lock_irq(&sdmac->lock);
817
818 cookie = sdma_assign_cookie(sdmac);
819
Sascha Hauer23889c62011-01-31 10:56:58 +0100820 sdma_enable_channel(sdma, sdmac->channel);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000821
822 spin_unlock_irq(&sdmac->lock);
823
824 return cookie;
825}
826
827static int sdma_alloc_chan_resources(struct dma_chan *chan)
828{
829 struct sdma_channel *sdmac = to_sdma_chan(chan);
830 struct imx_dma_data *data = chan->private;
831 int prio, ret;
832
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000833 if (!data)
834 return -EINVAL;
835
836 switch (data->priority) {
837 case DMA_PRIO_HIGH:
838 prio = 3;
839 break;
840 case DMA_PRIO_MEDIUM:
841 prio = 2;
842 break;
843 case DMA_PRIO_LOW:
844 default:
845 prio = 1;
846 break;
847 }
848
849 sdmac->peripheral_type = data->peripheral_type;
850 sdmac->event_id0 = data->dma_request;
851 ret = sdma_set_channel_priority(sdmac, prio);
852 if (ret)
853 return ret;
854
855 ret = sdma_request_channel(sdmac);
856 if (ret)
857 return ret;
858
859 dma_async_tx_descriptor_init(&sdmac->desc, chan);
860 sdmac->desc.tx_submit = sdma_tx_submit;
861 /* txd.flags will be overwritten in prep funcs */
862 sdmac->desc.flags = DMA_CTRL_ACK;
863
864 return 0;
865}
866
867static void sdma_free_chan_resources(struct dma_chan *chan)
868{
869 struct sdma_channel *sdmac = to_sdma_chan(chan);
870 struct sdma_engine *sdma = sdmac->sdma;
871
872 sdma_disable_channel(sdmac);
873
874 if (sdmac->event_id0)
875 sdma_event_disable(sdmac, sdmac->event_id0);
876 if (sdmac->event_id1)
877 sdma_event_disable(sdmac, sdmac->event_id1);
878
879 sdmac->event_id0 = 0;
880 sdmac->event_id1 = 0;
881
882 sdma_set_channel_priority(sdmac, 0);
883
884 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
885
886 clk_disable(sdma->clk);
887}
888
889static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
890 struct dma_chan *chan, struct scatterlist *sgl,
891 unsigned int sg_len, enum dma_data_direction direction,
892 unsigned long flags)
893{
894 struct sdma_channel *sdmac = to_sdma_chan(chan);
895 struct sdma_engine *sdma = sdmac->sdma;
896 int ret, i, count;
Sascha Hauer23889c62011-01-31 10:56:58 +0100897 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000898 struct scatterlist *sg;
899
900 if (sdmac->status == DMA_IN_PROGRESS)
901 return NULL;
902 sdmac->status = DMA_IN_PROGRESS;
903
904 sdmac->flags = 0;
905
906 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
907 sg_len, channel);
908
909 sdmac->direction = direction;
910 ret = sdma_load_context(sdmac);
911 if (ret)
912 goto err_out;
913
914 if (sg_len > NUM_BD) {
915 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
916 channel, sg_len, NUM_BD);
917 ret = -EINVAL;
918 goto err_out;
919 }
920
921 for_each_sg(sgl, sg, sg_len, i) {
922 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
923 int param;
924
Anatolij Gustschind2f5c272010-11-22 18:35:18 +0100925 bd->buffer_addr = sg->dma_address;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000926
927 count = sg->length;
928
929 if (count > 0xffff) {
930 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
931 channel, count, 0xffff);
932 ret = -EINVAL;
933 goto err_out;
934 }
935
936 bd->mode.count = count;
937
938 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
939 ret = -EINVAL;
940 goto err_out;
941 }
Sascha Hauer1fa81c22011-01-12 13:02:28 +0100942
943 switch (sdmac->word_size) {
944 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000945 bd->mode.command = 0;
Sascha Hauer1fa81c22011-01-12 13:02:28 +0100946 if (count & 3 || sg->dma_address & 3)
947 return NULL;
948 break;
949 case DMA_SLAVE_BUSWIDTH_2_BYTES:
950 bd->mode.command = 2;
951 if (count & 1 || sg->dma_address & 1)
952 return NULL;
953 break;
954 case DMA_SLAVE_BUSWIDTH_1_BYTE:
955 bd->mode.command = 1;
956 break;
957 default:
958 return NULL;
959 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000960
961 param = BD_DONE | BD_EXTD | BD_CONT;
962
Shawn Guo341b9412011-01-20 05:50:39 +0800963 if (i + 1 == sg_len) {
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000964 param |= BD_INTR;
Shawn Guo341b9412011-01-20 05:50:39 +0800965 param |= BD_LAST;
966 param &= ~BD_CONT;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000967 }
968
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000969 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
970 i, count, sg->dma_address,
971 param & BD_WRAP ? "wrap" : "",
972 param & BD_INTR ? " intr" : "");
973
974 bd->mode.status = param;
975 }
976
977 sdmac->num_bd = sg_len;
978 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
979
980 return &sdmac->desc;
981err_out:
Shawn Guo4b2ce9d2011-01-20 05:50:36 +0800982 sdmac->status = DMA_ERROR;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000983 return NULL;
984}
985
986static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
987 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
988 size_t period_len, enum dma_data_direction direction)
989{
990 struct sdma_channel *sdmac = to_sdma_chan(chan);
991 struct sdma_engine *sdma = sdmac->sdma;
992 int num_periods = buf_len / period_len;
Sascha Hauer23889c62011-01-31 10:56:58 +0100993 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000994 int ret, i = 0, buf = 0;
995
996 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
997
998 if (sdmac->status == DMA_IN_PROGRESS)
999 return NULL;
1000
1001 sdmac->status = DMA_IN_PROGRESS;
1002
1003 sdmac->flags |= IMX_DMA_SG_LOOP;
1004 sdmac->direction = direction;
1005 ret = sdma_load_context(sdmac);
1006 if (ret)
1007 goto err_out;
1008
1009 if (num_periods > NUM_BD) {
1010 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1011 channel, num_periods, NUM_BD);
1012 goto err_out;
1013 }
1014
1015 if (period_len > 0xffff) {
1016 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
1017 channel, period_len, 0xffff);
1018 goto err_out;
1019 }
1020
1021 while (buf < buf_len) {
1022 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1023 int param;
1024
1025 bd->buffer_addr = dma_addr;
1026
1027 bd->mode.count = period_len;
1028
1029 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1030 goto err_out;
1031 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1032 bd->mode.command = 0;
1033 else
1034 bd->mode.command = sdmac->word_size;
1035
1036 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1037 if (i + 1 == num_periods)
1038 param |= BD_WRAP;
1039
1040 dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
1041 i, period_len, dma_addr,
1042 param & BD_WRAP ? "wrap" : "",
1043 param & BD_INTR ? " intr" : "");
1044
1045 bd->mode.status = param;
1046
1047 dma_addr += period_len;
1048 buf += period_len;
1049
1050 i++;
1051 }
1052
1053 sdmac->num_bd = num_periods;
1054 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1055
1056 return &sdmac->desc;
1057err_out:
1058 sdmac->status = DMA_ERROR;
1059 return NULL;
1060}
1061
1062static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1063 unsigned long arg)
1064{
1065 struct sdma_channel *sdmac = to_sdma_chan(chan);
1066 struct dma_slave_config *dmaengine_cfg = (void *)arg;
1067
1068 switch (cmd) {
1069 case DMA_TERMINATE_ALL:
1070 sdma_disable_channel(sdmac);
1071 return 0;
1072 case DMA_SLAVE_CONFIG:
1073 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
1074 sdmac->per_address = dmaengine_cfg->src_addr;
1075 sdmac->watermark_level = dmaengine_cfg->src_maxburst;
1076 sdmac->word_size = dmaengine_cfg->src_addr_width;
1077 } else {
1078 sdmac->per_address = dmaengine_cfg->dst_addr;
1079 sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
1080 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1081 }
1082 return sdma_config_channel(sdmac);
1083 default:
1084 return -ENOSYS;
1085 }
1086
1087 return -EINVAL;
1088}
1089
1090static enum dma_status sdma_tx_status(struct dma_chan *chan,
1091 dma_cookie_t cookie,
1092 struct dma_tx_state *txstate)
1093{
1094 struct sdma_channel *sdmac = to_sdma_chan(chan);
1095 dma_cookie_t last_used;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001096
1097 last_used = chan->cookie;
1098
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001099 dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
1100
Shawn Guo8a965912011-01-20 05:50:37 +08001101 return sdmac->status;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001102}
1103
1104static void sdma_issue_pending(struct dma_chan *chan)
1105{
1106 /*
1107 * Nothing to do. We only have a single descriptor
1108 */
1109}
1110
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001111#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1112
1113static void sdma_add_scripts(struct sdma_engine *sdma,
1114 const struct sdma_script_start_addrs *addr)
1115{
1116 s32 *addr_arr = (u32 *)addr;
1117 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1118 int i;
1119
1120 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1121 if (addr_arr[i] > 0)
1122 saddr_arr[i] = addr_arr[i];
1123}
1124
1125static int __init sdma_get_firmware(struct sdma_engine *sdma,
Shawn Guo2e534b22011-06-22 22:41:31 +08001126 const char *fw_name)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001127{
1128 const struct firmware *fw;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001129 const struct sdma_firmware_header *header;
1130 int ret;
1131 const struct sdma_script_start_addrs *addr;
1132 unsigned short *ram_code;
1133
Shawn Guo40ad5b32011-07-15 17:25:28 +08001134 ret = request_firmware(&fw, fw_name, sdma->dev);
1135 if (ret)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001136 return ret;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001137
1138 if (fw->size < sizeof(*header))
1139 goto err_firmware;
1140
1141 header = (struct sdma_firmware_header *)fw->data;
1142
1143 if (header->magic != SDMA_FIRMWARE_MAGIC)
1144 goto err_firmware;
1145 if (header->ram_code_start + header->ram_code_size > fw->size)
1146 goto err_firmware;
1147
1148 addr = (void *)header + header->script_addrs_start;
1149 ram_code = (void *)header + header->ram_code_start;
1150
1151 clk_enable(sdma->clk);
1152 /* download the RAM image for SDMA */
1153 sdma_load_script(sdma, ram_code,
1154 header->ram_code_size,
Sascha Hauer6866fd32011-01-12 11:18:14 +01001155 addr->ram_code_start_addr);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001156 clk_disable(sdma->clk);
1157
1158 sdma_add_scripts(sdma, addr);
1159
1160 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1161 header->version_major,
1162 header->version_minor);
1163
1164err_firmware:
1165 release_firmware(fw);
1166
1167 return ret;
1168}
1169
1170static int __init sdma_init(struct sdma_engine *sdma)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001171{
1172 int i, ret;
1173 dma_addr_t ccb_phys;
1174
Shawn Guo62550cd2011-07-13 21:33:17 +08001175 switch (sdma->devtype) {
1176 case IMX31_SDMA:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001177 sdma->num_events = 32;
1178 break;
Shawn Guo62550cd2011-07-13 21:33:17 +08001179 case IMX35_SDMA:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001180 sdma->num_events = 48;
1181 break;
1182 default:
Shawn Guo62550cd2011-07-13 21:33:17 +08001183 dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
1184 sdma->devtype);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001185 return -ENODEV;
1186 }
1187
1188 clk_enable(sdma->clk);
1189
1190 /* Be sure SDMA has not started yet */
1191 __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
1192
1193 sdma->channel_control = dma_alloc_coherent(NULL,
1194 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1195 sizeof(struct sdma_context_data),
1196 &ccb_phys, GFP_KERNEL);
1197
1198 if (!sdma->channel_control) {
1199 ret = -ENOMEM;
1200 goto err_dma_alloc;
1201 }
1202
1203 sdma->context = (void *)sdma->channel_control +
1204 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1205 sdma->context_phys = ccb_phys +
1206 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1207
1208 /* Zero-out the CCB structures array just allocated */
1209 memset(sdma->channel_control, 0,
1210 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1211
1212 /* disable all channels */
1213 for (i = 0; i < sdma->num_events; i++)
1214 __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
1215
1216 /* All channels have priority 0 */
1217 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1218 __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1219
1220 ret = sdma_request_channel(&sdma->channel[0]);
1221 if (ret)
1222 goto err_dma_alloc;
1223
1224 sdma_config_ownership(&sdma->channel[0], false, true, false);
1225
1226 /* Set Command Channel (Channel Zero) */
1227 __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
1228
1229 /* Set bits of CONFIG register but with static context switching */
1230 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1231 __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
1232
1233 __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1234
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001235 /* Set bits of CONFIG register with given context switching mode */
1236 __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
1237
1238 /* Initializes channel's priorities */
1239 sdma_set_channel_priority(&sdma->channel[0], 7);
1240
1241 clk_disable(sdma->clk);
1242
1243 return 0;
1244
1245err_dma_alloc:
1246 clk_disable(sdma->clk);
1247 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1248 return ret;
1249}
1250
1251static int __init sdma_probe(struct platform_device *pdev)
1252{
1253 int ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001254 int irq;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001255 struct resource *iores;
1256 struct sdma_platform_data *pdata = pdev->dev.platform_data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001257 int i;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001258 struct sdma_engine *sdma;
1259
1260 sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
1261 if (!sdma)
1262 return -ENOMEM;
1263
1264 sdma->dev = &pdev->dev;
1265
1266 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1267 irq = platform_get_irq(pdev, 0);
1268 if (!iores || irq < 0 || !pdata) {
1269 ret = -EINVAL;
1270 goto err_irq;
1271 }
1272
1273 if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
1274 ret = -EBUSY;
1275 goto err_request_region;
1276 }
1277
1278 sdma->clk = clk_get(&pdev->dev, NULL);
1279 if (IS_ERR(sdma->clk)) {
1280 ret = PTR_ERR(sdma->clk);
1281 goto err_clk;
1282 }
1283
1284 sdma->regs = ioremap(iores->start, resource_size(iores));
1285 if (!sdma->regs) {
1286 ret = -ENOMEM;
1287 goto err_ioremap;
1288 }
1289
1290 ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
1291 if (ret)
1292 goto err_request_irq;
1293
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001294 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001295 if (!sdma->script_addrs)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001296 goto err_alloc;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001297
Shawn Guo62550cd2011-07-13 21:33:17 +08001298 sdma->devtype = pdev->id_entry->driver_data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001299
Sascha Hauer7214a8b2011-01-31 10:21:35 +01001300 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1301 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1302
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001303 INIT_LIST_HEAD(&sdma->dma_device.channels);
1304 /* Initialize channel parameters */
1305 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1306 struct sdma_channel *sdmac = &sdma->channel[i];
1307
1308 sdmac->sdma = sdma;
1309 spin_lock_init(&sdmac->lock);
1310
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001311 sdmac->chan.device = &sdma->dma_device;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001312 sdmac->channel = i;
1313
Sascha Hauer23889c62011-01-31 10:56:58 +01001314 /*
1315 * Add the channel to the DMAC list. Do not add channel 0 though
1316 * because we need it internally in the SDMA driver. This also means
1317 * that channel 0 in dmaengine counting matches sdma channel 1.
1318 */
1319 if (i)
1320 list_add_tail(&sdmac->chan.device_node,
1321 &sdma->dma_device.channels);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001322 }
1323
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001324 ret = sdma_init(sdma);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001325 if (ret)
1326 goto err_init;
1327
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001328 if (pdata->script_addrs)
1329 sdma_add_scripts(sdma, pdata->script_addrs);
1330
Shawn Guo2e534b22011-06-22 22:41:31 +08001331 sdma_get_firmware(sdma, pdata->fw_name);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001332
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001333 sdma->dma_device.dev = &pdev->dev;
1334
1335 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1336 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1337 sdma->dma_device.device_tx_status = sdma_tx_status;
1338 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1339 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1340 sdma->dma_device.device_control = sdma_control;
1341 sdma->dma_device.device_issue_pending = sdma_issue_pending;
Sascha Hauerb9b3f822011-01-12 12:12:31 +01001342 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1343 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001344
1345 ret = dma_async_device_register(&sdma->dma_device);
1346 if (ret) {
1347 dev_err(&pdev->dev, "unable to register\n");
1348 goto err_init;
1349 }
1350
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001351 dev_info(sdma->dev, "initialized\n");
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001352
1353 return 0;
1354
1355err_init:
1356 kfree(sdma->script_addrs);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001357err_alloc:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001358 free_irq(irq, sdma);
1359err_request_irq:
1360 iounmap(sdma->regs);
1361err_ioremap:
1362 clk_put(sdma->clk);
1363err_clk:
1364 release_mem_region(iores->start, resource_size(iores));
1365err_request_region:
1366err_irq:
1367 kfree(sdma);
Shawn Guo939fd4f2011-01-19 19:13:06 +08001368 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001369}
1370
1371static int __exit sdma_remove(struct platform_device *pdev)
1372{
1373 return -EBUSY;
1374}
1375
1376static struct platform_driver sdma_driver = {
1377 .driver = {
1378 .name = "imx-sdma",
1379 },
Shawn Guo62550cd2011-07-13 21:33:17 +08001380 .id_table = sdma_devtypes,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001381 .remove = __exit_p(sdma_remove),
1382};
1383
1384static int __init sdma_module_init(void)
1385{
1386 return platform_driver_probe(&sdma_driver, sdma_probe);
1387}
Sascha Hauerc989a7f2010-12-06 11:09:57 +01001388module_init(sdma_module_init);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001389
1390MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1391MODULE_DESCRIPTION("i.MX SDMA driver");
1392MODULE_LICENSE("GPL");