dma40: combine duplicated d40_pool_lli_alloc() calls
[linux-2.6.git] / drivers / dma / ste_dma40.c
blob495f9eb0a4b6ecd10b458f3d2a8ef6c47a8f07e2
1 /*
2 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
4 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
6 * License terms: GNU General Public License (GPL) version 2
7 */
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/dmaengine.h>
12 #include <linux/platform_device.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
17 #include <plat/ste_dma40.h>
19 #include "ste_dma40_ll.h"
21 #define D40_NAME "dma40"
23 #define D40_PHY_CHAN -1
25 /* For masking out/in 2 bit channel positions */
26 #define D40_CHAN_POS(chan) (2 * (chan / 2))
27 #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
29 /* Maximum iterations taken before giving up suspending a channel */
30 #define D40_SUSPEND_MAX_IT 500
32 /* Hardware requirement on LCLA alignment */
33 #define LCLA_ALIGNMENT 0x40000
35 /* Max number of links per event group */
36 #define D40_LCLA_LINK_PER_EVENT_GRP 128
37 #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
39 /* Attempts before giving up to trying to get pages that are aligned */
40 #define MAX_LCLA_ALLOC_ATTEMPTS 256
42 /* Bit markings for allocation map */
43 #define D40_ALLOC_FREE (1 << 31)
44 #define D40_ALLOC_PHY (1 << 30)
45 #define D40_ALLOC_LOG_FREE 0
47 /* Hardware designer of the block */
48 #define D40_HW_DESIGNER 0x8
50 /**
51 * enum 40_command - The different commands and/or statuses.
53 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
54 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
55 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
56 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
58 enum d40_command {
59 D40_DMA_STOP = 0,
60 D40_DMA_RUN = 1,
61 D40_DMA_SUSPEND_REQ = 2,
62 D40_DMA_SUSPENDED = 3
65 /**
66 * struct d40_lli_pool - Structure for keeping LLIs in memory
68 * @base: Pointer to memory area when the pre_alloc_lli's are not large
69 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
70 * pre_alloc_lli is used.
71 * @dma_addr: DMA address, if mapped
72 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
73 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
74 * one buffer to one buffer.
76 struct d40_lli_pool {
77 void *base;
78 int size;
79 dma_addr_t dma_addr;
80 /* Space for dst and src, plus an extra for padding */
81 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
84 /**
85 * struct d40_desc - A descriptor is one DMA job.
87 * @lli_phy: LLI settings for physical channel. Both src and dst=
88 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
89 * lli_len equals one.
90 * @lli_log: Same as above but for logical channels.
91 * @lli_pool: The pool with two entries pre-allocated.
92 * @lli_len: Number of llis of current descriptor.
93 * @lli_current: Number of transfered llis.
94 * @lcla_alloc: Number of LCLA entries allocated.
95 * @txd: DMA engine struct. Used for among other things for communication
96 * during a transfer.
97 * @node: List entry.
98 * @is_in_client_list: true if the client owns this descriptor.
99 * the previous one.
101 * This descriptor is used for both logical and physical transfers.
103 struct d40_desc {
104 /* LLI physical */
105 struct d40_phy_lli_bidir lli_phy;
106 /* LLI logical */
107 struct d40_log_lli_bidir lli_log;
109 struct d40_lli_pool lli_pool;
110 int lli_len;
111 int lli_current;
112 int lcla_alloc;
114 struct dma_async_tx_descriptor txd;
115 struct list_head node;
117 bool is_in_client_list;
121 * struct d40_lcla_pool - LCLA pool settings and data.
123 * @base: The virtual address of LCLA. 18 bit aligned.
124 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
125 * This pointer is only there for clean-up on error.
126 * @pages: The number of pages needed for all physical channels.
127 * Only used later for clean-up on error
128 * @lock: Lock to protect the content in this struct.
129 * @alloc_map: big map over which LCLA entry is own by which job.
131 struct d40_lcla_pool {
132 void *base;
133 dma_addr_t dma_addr;
134 void *base_unaligned;
135 int pages;
136 spinlock_t lock;
137 struct d40_desc **alloc_map;
141 * struct d40_phy_res - struct for handling eventlines mapped to physical
142 * channels.
144 * @lock: A lock protection this entity.
145 * @num: The physical channel number of this entity.
146 * @allocated_src: Bit mapped to show which src event line's are mapped to
147 * this physical channel. Can also be free or physically allocated.
148 * @allocated_dst: Same as for src but is dst.
149 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
150 * event line number.
152 struct d40_phy_res {
153 spinlock_t lock;
154 int num;
155 u32 allocated_src;
156 u32 allocated_dst;
159 struct d40_base;
162 * struct d40_chan - Struct that describes a channel.
164 * @lock: A spinlock to protect this struct.
165 * @log_num: The logical number, if any of this channel.
166 * @completed: Starts with 1, after first interrupt it is set to dma engine's
167 * current cookie.
168 * @pending_tx: The number of pending transfers. Used between interrupt handler
169 * and tasklet.
170 * @busy: Set to true when transfer is ongoing on this channel.
171 * @phy_chan: Pointer to physical channel which this instance runs on. If this
172 * point is NULL, then the channel is not allocated.
173 * @chan: DMA engine handle.
174 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
175 * transfer and call client callback.
176 * @client: Cliented owned descriptor list.
177 * @active: Active descriptor.
178 * @queue: Queued jobs.
179 * @dma_cfg: The client configuration of this dma channel.
180 * @configured: whether the dma_cfg configuration is valid
181 * @base: Pointer to the device instance struct.
182 * @src_def_cfg: Default cfg register setting for src.
183 * @dst_def_cfg: Default cfg register setting for dst.
184 * @log_def: Default logical channel settings.
185 * @lcla: Space for one dst src pair for logical channel transfers.
186 * @lcpa: Pointer to dst and src lcpa settings.
188 * This struct can either "be" a logical or a physical channel.
190 struct d40_chan {
191 spinlock_t lock;
192 int log_num;
193 /* ID of the most recent completed transfer */
194 int completed;
195 int pending_tx;
196 bool busy;
197 struct d40_phy_res *phy_chan;
198 struct dma_chan chan;
199 struct tasklet_struct tasklet;
200 struct list_head client;
201 struct list_head active;
202 struct list_head queue;
203 struct stedma40_chan_cfg dma_cfg;
204 bool configured;
205 struct d40_base *base;
206 /* Default register configurations */
207 u32 src_def_cfg;
208 u32 dst_def_cfg;
209 struct d40_def_lcsp log_def;
210 struct d40_log_lli_full *lcpa;
211 /* Runtime reconfiguration */
212 dma_addr_t runtime_addr;
213 enum dma_data_direction runtime_direction;
217 * struct d40_base - The big global struct, one for each probe'd instance.
219 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
220 * @execmd_lock: Lock for execute command usage since several channels share
221 * the same physical register.
222 * @dev: The device structure.
223 * @virtbase: The virtual base address of the DMA's register.
224 * @rev: silicon revision detected.
225 * @clk: Pointer to the DMA clock structure.
226 * @phy_start: Physical memory start of the DMA registers.
227 * @phy_size: Size of the DMA register map.
228 * @irq: The IRQ number.
229 * @num_phy_chans: The number of physical channels. Read from HW. This
230 * is the number of available channels for this driver, not counting "Secure
231 * mode" allocated physical channels.
232 * @num_log_chans: The number of logical channels. Calculated from
233 * num_phy_chans.
234 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
235 * @dma_slave: dma_device channels that can do only do slave transfers.
236 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
237 * @log_chans: Room for all possible logical channels in system.
238 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
239 * to log_chans entries.
240 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
241 * to phy_chans entries.
242 * @plat_data: Pointer to provided platform_data which is the driver
243 * configuration.
244 * @phy_res: Vector containing all physical channels.
245 * @lcla_pool: lcla pool settings and data.
246 * @lcpa_base: The virtual mapped address of LCPA.
247 * @phy_lcpa: The physical address of the LCPA.
248 * @lcpa_size: The size of the LCPA area.
249 * @desc_slab: cache for descriptors.
251 struct d40_base {
252 spinlock_t interrupt_lock;
253 spinlock_t execmd_lock;
254 struct device *dev;
255 void __iomem *virtbase;
256 u8 rev:4;
257 struct clk *clk;
258 phys_addr_t phy_start;
259 resource_size_t phy_size;
260 int irq;
261 int num_phy_chans;
262 int num_log_chans;
263 struct dma_device dma_both;
264 struct dma_device dma_slave;
265 struct dma_device dma_memcpy;
266 struct d40_chan *phy_chans;
267 struct d40_chan *log_chans;
268 struct d40_chan **lookup_log_chans;
269 struct d40_chan **lookup_phy_chans;
270 struct stedma40_platform_data *plat_data;
271 /* Physical half channels */
272 struct d40_phy_res *phy_res;
273 struct d40_lcla_pool lcla_pool;
274 void *lcpa_base;
275 dma_addr_t phy_lcpa;
276 resource_size_t lcpa_size;
277 struct kmem_cache *desc_slab;
281 * struct d40_interrupt_lookup - lookup table for interrupt handler
283 * @src: Interrupt mask register.
284 * @clr: Interrupt clear register.
285 * @is_error: true if this is an error interrupt.
286 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
287 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
289 struct d40_interrupt_lookup {
290 u32 src;
291 u32 clr;
292 bool is_error;
293 int offset;
297 * struct d40_reg_val - simple lookup struct
299 * @reg: The register.
300 * @val: The value that belongs to the register in reg.
302 struct d40_reg_val {
303 unsigned int reg;
304 unsigned int val;
307 static struct device *chan2dev(struct d40_chan *d40c)
309 return &d40c->chan.dev->device;
312 static bool chan_is_physical(struct d40_chan *chan)
314 return chan->log_num == D40_PHY_CHAN;
317 static bool chan_is_logical(struct d40_chan *chan)
319 return !chan_is_physical(chan);
322 static void __iomem *chan_base(struct d40_chan *chan)
324 return chan->base->virtbase + D40_DREG_PCBASE +
325 chan->phy_chan->num * D40_DREG_PCDELTA;
328 #define d40_err(dev, format, arg...) \
329 dev_err(dev, "[%s] " format, __func__, ## arg)
331 #define chan_err(d40c, format, arg...) \
332 d40_err(chan2dev(d40c), format, ## arg)
334 static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
335 int lli_len)
337 bool is_log = chan_is_logical(d40c);
338 u32 align;
339 void *base;
341 if (is_log)
342 align = sizeof(struct d40_log_lli);
343 else
344 align = sizeof(struct d40_phy_lli);
346 if (lli_len == 1) {
347 base = d40d->lli_pool.pre_alloc_lli;
348 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
349 d40d->lli_pool.base = NULL;
350 } else {
351 d40d->lli_pool.size = lli_len * 2 * align;
353 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
354 d40d->lli_pool.base = base;
356 if (d40d->lli_pool.base == NULL)
357 return -ENOMEM;
360 if (is_log) {
361 d40d->lli_log.src = PTR_ALIGN(base, align);
362 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
364 d40d->lli_pool.dma_addr = 0;
365 } else {
366 d40d->lli_phy.src = PTR_ALIGN(base, align);
367 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
369 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
370 d40d->lli_phy.src,
371 d40d->lli_pool.size,
372 DMA_TO_DEVICE);
374 if (dma_mapping_error(d40c->base->dev,
375 d40d->lli_pool.dma_addr)) {
376 kfree(d40d->lli_pool.base);
377 d40d->lli_pool.base = NULL;
378 d40d->lli_pool.dma_addr = 0;
379 return -ENOMEM;
383 return 0;
386 static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
388 if (d40d->lli_pool.dma_addr)
389 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
390 d40d->lli_pool.size, DMA_TO_DEVICE);
392 kfree(d40d->lli_pool.base);
393 d40d->lli_pool.base = NULL;
394 d40d->lli_pool.size = 0;
395 d40d->lli_log.src = NULL;
396 d40d->lli_log.dst = NULL;
397 d40d->lli_phy.src = NULL;
398 d40d->lli_phy.dst = NULL;
401 static int d40_lcla_alloc_one(struct d40_chan *d40c,
402 struct d40_desc *d40d)
404 unsigned long flags;
405 int i;
406 int ret = -EINVAL;
407 int p;
409 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
411 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
414 * Allocate both src and dst at the same time, therefore the half
415 * start on 1 since 0 can't be used since zero is used as end marker.
417 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
418 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
419 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
420 d40d->lcla_alloc++;
421 ret = i;
422 break;
426 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
428 return ret;
431 static int d40_lcla_free_all(struct d40_chan *d40c,
432 struct d40_desc *d40d)
434 unsigned long flags;
435 int i;
436 int ret = -EINVAL;
438 if (chan_is_physical(d40c))
439 return 0;
441 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
443 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
444 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
445 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
446 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
447 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
448 d40d->lcla_alloc--;
449 if (d40d->lcla_alloc == 0) {
450 ret = 0;
451 break;
456 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
458 return ret;
462 static void d40_desc_remove(struct d40_desc *d40d)
464 list_del(&d40d->node);
467 static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
469 struct d40_desc *desc = NULL;
471 if (!list_empty(&d40c->client)) {
472 struct d40_desc *d;
473 struct d40_desc *_d;
475 list_for_each_entry_safe(d, _d, &d40c->client, node)
476 if (async_tx_test_ack(&d->txd)) {
477 d40_pool_lli_free(d40c, d);
478 d40_desc_remove(d);
479 desc = d;
480 memset(desc, 0, sizeof(*desc));
481 break;
485 if (!desc)
486 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
488 if (desc)
489 INIT_LIST_HEAD(&desc->node);
491 return desc;
494 static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
497 d40_pool_lli_free(d40c, d40d);
498 d40_lcla_free_all(d40c, d40d);
499 kmem_cache_free(d40c->base->desc_slab, d40d);
502 static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
504 list_add_tail(&desc->node, &d40c->active);
507 static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
509 int curr_lcla = -EINVAL, next_lcla;
511 if (chan_is_physical(d40c)) {
512 d40_phy_lli_write(d40c->base->virtbase,
513 d40c->phy_chan->num,
514 d40d->lli_phy.dst,
515 d40d->lli_phy.src);
516 d40d->lli_current = d40d->lli_len;
517 } else {
519 if ((d40d->lli_len - d40d->lli_current) > 1)
520 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
522 d40_log_lli_lcpa_write(d40c->lcpa,
523 &d40d->lli_log.dst[d40d->lli_current],
524 &d40d->lli_log.src[d40d->lli_current],
525 curr_lcla);
527 d40d->lli_current++;
528 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
529 unsigned int lcla_offset = d40c->phy_chan->num * 1024 +
530 8 * curr_lcla * 2;
531 struct d40_lcla_pool *pool = &d40c->base->lcla_pool;
532 struct d40_log_lli *lcla = pool->base + lcla_offset;
534 if (d40d->lli_current + 1 < d40d->lli_len)
535 next_lcla = d40_lcla_alloc_one(d40c, d40d);
536 else
537 next_lcla = -EINVAL;
539 d40_log_lli_lcla_write(lcla,
540 &d40d->lli_log.dst[d40d->lli_current],
541 &d40d->lli_log.src[d40d->lli_current],
542 next_lcla);
544 dma_sync_single_range_for_device(d40c->base->dev,
545 pool->dma_addr, lcla_offset,
546 2 * sizeof(struct d40_log_lli),
547 DMA_TO_DEVICE);
549 curr_lcla = next_lcla;
551 if (curr_lcla == -EINVAL) {
552 d40d->lli_current++;
553 break;
560 static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
562 struct d40_desc *d;
564 if (list_empty(&d40c->active))
565 return NULL;
567 d = list_first_entry(&d40c->active,
568 struct d40_desc,
569 node);
570 return d;
573 static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
575 list_add_tail(&desc->node, &d40c->queue);
578 static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
580 struct d40_desc *d;
582 if (list_empty(&d40c->queue))
583 return NULL;
585 d = list_first_entry(&d40c->queue,
586 struct d40_desc,
587 node);
588 return d;
591 static int d40_psize_2_burst_size(bool is_log, int psize)
593 if (is_log) {
594 if (psize == STEDMA40_PSIZE_LOG_1)
595 return 1;
596 } else {
597 if (psize == STEDMA40_PSIZE_PHY_1)
598 return 1;
601 return 2 << psize;
605 * The dma only supports transmitting packages up to
606 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
607 * dma elements required to send the entire sg list
609 static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
611 int dmalen;
612 u32 max_w = max(data_width1, data_width2);
613 u32 min_w = min(data_width1, data_width2);
614 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
616 if (seg_max > STEDMA40_MAX_SEG_SIZE)
617 seg_max -= (1 << max_w);
619 if (!IS_ALIGNED(size, 1 << max_w))
620 return -EINVAL;
622 if (size <= seg_max)
623 dmalen = 1;
624 else {
625 dmalen = size / seg_max;
626 if (dmalen * seg_max < size)
627 dmalen++;
629 return dmalen;
632 static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
633 u32 data_width1, u32 data_width2)
635 struct scatterlist *sg;
636 int i;
637 int len = 0;
638 int ret;
640 for_each_sg(sgl, sg, sg_len, i) {
641 ret = d40_size_2_dmalen(sg_dma_len(sg),
642 data_width1, data_width2);
643 if (ret < 0)
644 return ret;
645 len += ret;
647 return len;
650 /* Support functions for logical channels */
652 static int d40_channel_execute_command(struct d40_chan *d40c,
653 enum d40_command command)
655 u32 status;
656 int i;
657 void __iomem *active_reg;
658 int ret = 0;
659 unsigned long flags;
660 u32 wmask;
662 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
664 if (d40c->phy_chan->num % 2 == 0)
665 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
666 else
667 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
669 if (command == D40_DMA_SUSPEND_REQ) {
670 status = (readl(active_reg) &
671 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
672 D40_CHAN_POS(d40c->phy_chan->num);
674 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
675 goto done;
678 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
679 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
680 active_reg);
682 if (command == D40_DMA_SUSPEND_REQ) {
684 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
685 status = (readl(active_reg) &
686 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
687 D40_CHAN_POS(d40c->phy_chan->num);
689 cpu_relax();
691 * Reduce the number of bus accesses while
692 * waiting for the DMA to suspend.
694 udelay(3);
696 if (status == D40_DMA_STOP ||
697 status == D40_DMA_SUSPENDED)
698 break;
701 if (i == D40_SUSPEND_MAX_IT) {
702 chan_err(d40c,
703 "unable to suspend the chl %d (log: %d) status %x\n",
704 d40c->phy_chan->num, d40c->log_num,
705 status);
706 dump_stack();
707 ret = -EBUSY;
711 done:
712 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
713 return ret;
716 static void d40_term_all(struct d40_chan *d40c)
718 struct d40_desc *d40d;
720 /* Release active descriptors */
721 while ((d40d = d40_first_active_get(d40c))) {
722 d40_desc_remove(d40d);
723 d40_desc_free(d40c, d40d);
726 /* Release queued descriptors waiting for transfer */
727 while ((d40d = d40_first_queued(d40c))) {
728 d40_desc_remove(d40d);
729 d40_desc_free(d40c, d40d);
733 d40c->pending_tx = 0;
734 d40c->busy = false;
737 static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
738 u32 event, int reg)
740 void __iomem *addr = chan_base(d40c) + reg;
741 int tries;
743 if (!enable) {
744 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
745 | ~D40_EVENTLINE_MASK(event), addr);
746 return;
750 * The hardware sometimes doesn't register the enable when src and dst
751 * event lines are active on the same logical channel. Retry to ensure
752 * it does. Usually only one retry is sufficient.
754 tries = 100;
755 while (--tries) {
756 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
757 | ~D40_EVENTLINE_MASK(event), addr);
759 if (readl(addr) & D40_EVENTLINE_MASK(event))
760 break;
763 if (tries != 99)
764 dev_dbg(chan2dev(d40c),
765 "[%s] workaround enable S%cLNK (%d tries)\n",
766 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
767 100 - tries);
769 WARN_ON(!tries);
772 static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
774 unsigned long flags;
776 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
778 /* Enable event line connected to device (or memcpy) */
779 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
780 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
781 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
783 __d40_config_set_event(d40c, do_enable, event,
784 D40_CHAN_REG_SSLNK);
787 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
788 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
790 __d40_config_set_event(d40c, do_enable, event,
791 D40_CHAN_REG_SDLNK);
794 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
797 static u32 d40_chan_has_events(struct d40_chan *d40c)
799 void __iomem *chanbase = chan_base(d40c);
800 u32 val;
802 val = readl(chanbase + D40_CHAN_REG_SSLNK);
803 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
805 return val;
808 static u32 d40_get_prmo(struct d40_chan *d40c)
810 static const unsigned int phy_map[] = {
811 [STEDMA40_PCHAN_BASIC_MODE]
812 = D40_DREG_PRMO_PCHAN_BASIC,
813 [STEDMA40_PCHAN_MODULO_MODE]
814 = D40_DREG_PRMO_PCHAN_MODULO,
815 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
816 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
818 static const unsigned int log_map[] = {
819 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
820 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
821 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
822 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
823 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
824 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
827 if (chan_is_physical(d40c))
828 return phy_map[d40c->dma_cfg.mode_opt];
829 else
830 return log_map[d40c->dma_cfg.mode_opt];
833 static void d40_config_write(struct d40_chan *d40c)
835 u32 addr_base;
836 u32 var;
838 /* Odd addresses are even addresses + 4 */
839 addr_base = (d40c->phy_chan->num % 2) * 4;
840 /* Setup channel mode to logical or physical */
841 var = ((u32)(chan_is_logical(d40c)) + 1) <<
842 D40_CHAN_POS(d40c->phy_chan->num);
843 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
845 /* Setup operational mode option register */
846 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
848 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
850 if (chan_is_logical(d40c)) {
851 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
852 & D40_SREG_ELEM_LOG_LIDX_MASK;
853 void __iomem *chanbase = chan_base(d40c);
855 /* Set default config for CFG reg */
856 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
857 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
859 /* Set LIDX for lcla */
860 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
861 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
865 static u32 d40_residue(struct d40_chan *d40c)
867 u32 num_elt;
869 if (chan_is_logical(d40c))
870 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
871 >> D40_MEM_LCSP2_ECNT_POS;
872 else {
873 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
874 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
875 >> D40_SREG_ELEM_PHY_ECNT_POS;
878 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
881 static bool d40_tx_is_linked(struct d40_chan *d40c)
883 bool is_link;
885 if (chan_is_logical(d40c))
886 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
887 else
888 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
889 & D40_SREG_LNK_PHYS_LNK_MASK;
891 return is_link;
894 static int d40_pause(struct dma_chan *chan)
896 struct d40_chan *d40c =
897 container_of(chan, struct d40_chan, chan);
898 int res = 0;
899 unsigned long flags;
901 if (!d40c->busy)
902 return 0;
904 spin_lock_irqsave(&d40c->lock, flags);
906 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
907 if (res == 0) {
908 if (chan_is_logical(d40c)) {
909 d40_config_set_event(d40c, false);
910 /* Resume the other logical channels if any */
911 if (d40_chan_has_events(d40c))
912 res = d40_channel_execute_command(d40c,
913 D40_DMA_RUN);
917 spin_unlock_irqrestore(&d40c->lock, flags);
918 return res;
921 static int d40_resume(struct dma_chan *chan)
923 struct d40_chan *d40c =
924 container_of(chan, struct d40_chan, chan);
925 int res = 0;
926 unsigned long flags;
928 if (!d40c->busy)
929 return 0;
931 spin_lock_irqsave(&d40c->lock, flags);
933 if (d40c->base->rev == 0)
934 if (chan_is_logical(d40c)) {
935 res = d40_channel_execute_command(d40c,
936 D40_DMA_SUSPEND_REQ);
937 goto no_suspend;
940 /* If bytes left to transfer or linked tx resume job */
941 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
943 if (chan_is_logical(d40c))
944 d40_config_set_event(d40c, true);
946 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
949 no_suspend:
950 spin_unlock_irqrestore(&d40c->lock, flags);
951 return res;
954 static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
956 struct d40_chan *d40c = container_of(tx->chan,
957 struct d40_chan,
958 chan);
959 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
960 unsigned long flags;
962 spin_lock_irqsave(&d40c->lock, flags);
964 d40c->chan.cookie++;
966 if (d40c->chan.cookie < 0)
967 d40c->chan.cookie = 1;
969 d40d->txd.cookie = d40c->chan.cookie;
971 d40_desc_queue(d40c, d40d);
973 spin_unlock_irqrestore(&d40c->lock, flags);
975 return tx->cookie;
978 static int d40_start(struct d40_chan *d40c)
980 if (d40c->base->rev == 0) {
981 int err;
983 if (chan_is_logical(d40c)) {
984 err = d40_channel_execute_command(d40c,
985 D40_DMA_SUSPEND_REQ);
986 if (err)
987 return err;
991 if (chan_is_logical(d40c))
992 d40_config_set_event(d40c, true);
994 return d40_channel_execute_command(d40c, D40_DMA_RUN);
997 static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
999 struct d40_desc *d40d;
1000 int err;
1002 /* Start queued jobs, if any */
1003 d40d = d40_first_queued(d40c);
1005 if (d40d != NULL) {
1006 d40c->busy = true;
1008 /* Remove from queue */
1009 d40_desc_remove(d40d);
1011 /* Add to active queue */
1012 d40_desc_submit(d40c, d40d);
1014 /* Initiate DMA job */
1015 d40_desc_load(d40c, d40d);
1017 /* Start dma job */
1018 err = d40_start(d40c);
1020 if (err)
1021 return NULL;
1024 return d40d;
1027 /* called from interrupt context */
1028 static void dma_tc_handle(struct d40_chan *d40c)
1030 struct d40_desc *d40d;
1032 /* Get first active entry from list */
1033 d40d = d40_first_active_get(d40c);
1035 if (d40d == NULL)
1036 return;
1038 d40_lcla_free_all(d40c, d40d);
1040 if (d40d->lli_current < d40d->lli_len) {
1041 d40_desc_load(d40c, d40d);
1042 /* Start dma job */
1043 (void) d40_start(d40c);
1044 return;
1047 if (d40_queue_start(d40c) == NULL)
1048 d40c->busy = false;
1050 d40c->pending_tx++;
1051 tasklet_schedule(&d40c->tasklet);
1055 static void dma_tasklet(unsigned long data)
1057 struct d40_chan *d40c = (struct d40_chan *) data;
1058 struct d40_desc *d40d;
1059 unsigned long flags;
1060 dma_async_tx_callback callback;
1061 void *callback_param;
1063 spin_lock_irqsave(&d40c->lock, flags);
1065 /* Get first active entry from list */
1066 d40d = d40_first_active_get(d40c);
1068 if (d40d == NULL)
1069 goto err;
1071 d40c->completed = d40d->txd.cookie;
1074 * If terminating a channel pending_tx is set to zero.
1075 * This prevents any finished active jobs to return to the client.
1077 if (d40c->pending_tx == 0) {
1078 spin_unlock_irqrestore(&d40c->lock, flags);
1079 return;
1082 /* Callback to client */
1083 callback = d40d->txd.callback;
1084 callback_param = d40d->txd.callback_param;
1086 if (async_tx_test_ack(&d40d->txd)) {
1087 d40_pool_lli_free(d40c, d40d);
1088 d40_desc_remove(d40d);
1089 d40_desc_free(d40c, d40d);
1090 } else {
1091 if (!d40d->is_in_client_list) {
1092 d40_desc_remove(d40d);
1093 d40_lcla_free_all(d40c, d40d);
1094 list_add_tail(&d40d->node, &d40c->client);
1095 d40d->is_in_client_list = true;
1099 d40c->pending_tx--;
1101 if (d40c->pending_tx)
1102 tasklet_schedule(&d40c->tasklet);
1104 spin_unlock_irqrestore(&d40c->lock, flags);
1106 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
1107 callback(callback_param);
1109 return;
1111 err:
1112 /* Rescue manouver if receiving double interrupts */
1113 if (d40c->pending_tx > 0)
1114 d40c->pending_tx--;
1115 spin_unlock_irqrestore(&d40c->lock, flags);
1118 static irqreturn_t d40_handle_interrupt(int irq, void *data)
1120 static const struct d40_interrupt_lookup il[] = {
1121 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1122 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1123 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1124 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1125 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1126 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1127 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1128 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1129 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1130 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1133 int i;
1134 u32 regs[ARRAY_SIZE(il)];
1135 u32 idx;
1136 u32 row;
1137 long chan = -1;
1138 struct d40_chan *d40c;
1139 unsigned long flags;
1140 struct d40_base *base = data;
1142 spin_lock_irqsave(&base->interrupt_lock, flags);
1144 /* Read interrupt status of both logical and physical channels */
1145 for (i = 0; i < ARRAY_SIZE(il); i++)
1146 regs[i] = readl(base->virtbase + il[i].src);
1148 for (;;) {
1150 chan = find_next_bit((unsigned long *)regs,
1151 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1153 /* No more set bits found? */
1154 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1155 break;
1157 row = chan / BITS_PER_LONG;
1158 idx = chan & (BITS_PER_LONG - 1);
1160 /* ACK interrupt */
1161 writel(1 << idx, base->virtbase + il[row].clr);
1163 if (il[row].offset == D40_PHY_CHAN)
1164 d40c = base->lookup_phy_chans[idx];
1165 else
1166 d40c = base->lookup_log_chans[il[row].offset + idx];
1167 spin_lock(&d40c->lock);
1169 if (!il[row].is_error)
1170 dma_tc_handle(d40c);
1171 else
1172 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1173 chan, il[row].offset, idx);
1175 spin_unlock(&d40c->lock);
1178 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1180 return IRQ_HANDLED;
1183 static int d40_validate_conf(struct d40_chan *d40c,
1184 struct stedma40_chan_cfg *conf)
1186 int res = 0;
1187 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1188 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
1189 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
1191 if (!conf->dir) {
1192 chan_err(d40c, "Invalid direction.\n");
1193 res = -EINVAL;
1196 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1197 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1198 d40c->runtime_addr == 0) {
1200 chan_err(d40c, "Invalid TX channel address (%d)\n",
1201 conf->dst_dev_type);
1202 res = -EINVAL;
1205 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1206 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1207 d40c->runtime_addr == 0) {
1208 chan_err(d40c, "Invalid RX channel address (%d)\n",
1209 conf->src_dev_type);
1210 res = -EINVAL;
1213 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
1214 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
1215 chan_err(d40c, "Invalid dst\n");
1216 res = -EINVAL;
1219 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
1220 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
1221 chan_err(d40c, "Invalid src\n");
1222 res = -EINVAL;
1225 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1226 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
1227 chan_err(d40c, "No event line\n");
1228 res = -EINVAL;
1231 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1232 (src_event_group != dst_event_group)) {
1233 chan_err(d40c, "Invalid event group\n");
1234 res = -EINVAL;
1237 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1239 * DMAC HW supports it. Will be added to this driver,
1240 * in case any dma client requires it.
1242 chan_err(d40c, "periph to periph not supported\n");
1243 res = -EINVAL;
1246 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1247 (1 << conf->src_info.data_width) !=
1248 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1249 (1 << conf->dst_info.data_width)) {
1251 * The DMAC hardware only supports
1252 * src (burst x width) == dst (burst x width)
1255 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1256 res = -EINVAL;
1259 return res;
1262 static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
1263 int log_event_line, bool is_log)
1265 unsigned long flags;
1266 spin_lock_irqsave(&phy->lock, flags);
1267 if (!is_log) {
1268 /* Physical interrupts are masked per physical full channel */
1269 if (phy->allocated_src == D40_ALLOC_FREE &&
1270 phy->allocated_dst == D40_ALLOC_FREE) {
1271 phy->allocated_dst = D40_ALLOC_PHY;
1272 phy->allocated_src = D40_ALLOC_PHY;
1273 goto found;
1274 } else
1275 goto not_found;
1278 /* Logical channel */
1279 if (is_src) {
1280 if (phy->allocated_src == D40_ALLOC_PHY)
1281 goto not_found;
1283 if (phy->allocated_src == D40_ALLOC_FREE)
1284 phy->allocated_src = D40_ALLOC_LOG_FREE;
1286 if (!(phy->allocated_src & (1 << log_event_line))) {
1287 phy->allocated_src |= 1 << log_event_line;
1288 goto found;
1289 } else
1290 goto not_found;
1291 } else {
1292 if (phy->allocated_dst == D40_ALLOC_PHY)
1293 goto not_found;
1295 if (phy->allocated_dst == D40_ALLOC_FREE)
1296 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1298 if (!(phy->allocated_dst & (1 << log_event_line))) {
1299 phy->allocated_dst |= 1 << log_event_line;
1300 goto found;
1301 } else
1302 goto not_found;
1305 not_found:
1306 spin_unlock_irqrestore(&phy->lock, flags);
1307 return false;
1308 found:
1309 spin_unlock_irqrestore(&phy->lock, flags);
1310 return true;
1313 static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1314 int log_event_line)
1316 unsigned long flags;
1317 bool is_free = false;
1319 spin_lock_irqsave(&phy->lock, flags);
1320 if (!log_event_line) {
1321 phy->allocated_dst = D40_ALLOC_FREE;
1322 phy->allocated_src = D40_ALLOC_FREE;
1323 is_free = true;
1324 goto out;
1327 /* Logical channel */
1328 if (is_src) {
1329 phy->allocated_src &= ~(1 << log_event_line);
1330 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1331 phy->allocated_src = D40_ALLOC_FREE;
1332 } else {
1333 phy->allocated_dst &= ~(1 << log_event_line);
1334 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1335 phy->allocated_dst = D40_ALLOC_FREE;
1338 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1339 D40_ALLOC_FREE);
1341 out:
1342 spin_unlock_irqrestore(&phy->lock, flags);
1344 return is_free;
1347 static int d40_allocate_channel(struct d40_chan *d40c)
1349 int dev_type;
1350 int event_group;
1351 int event_line;
1352 struct d40_phy_res *phys;
1353 int i;
1354 int j;
1355 int log_num;
1356 bool is_src;
1357 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
1359 phys = d40c->base->phy_res;
1361 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1362 dev_type = d40c->dma_cfg.src_dev_type;
1363 log_num = 2 * dev_type;
1364 is_src = true;
1365 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1366 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1367 /* dst event lines are used for logical memcpy */
1368 dev_type = d40c->dma_cfg.dst_dev_type;
1369 log_num = 2 * dev_type + 1;
1370 is_src = false;
1371 } else
1372 return -EINVAL;
1374 event_group = D40_TYPE_TO_GROUP(dev_type);
1375 event_line = D40_TYPE_TO_EVENT(dev_type);
1377 if (!is_log) {
1378 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1379 /* Find physical half channel */
1380 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1382 if (d40_alloc_mask_set(&phys[i], is_src,
1383 0, is_log))
1384 goto found_phy;
1386 } else
1387 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1388 int phy_num = j + event_group * 2;
1389 for (i = phy_num; i < phy_num + 2; i++) {
1390 if (d40_alloc_mask_set(&phys[i],
1391 is_src,
1393 is_log))
1394 goto found_phy;
1397 return -EINVAL;
1398 found_phy:
1399 d40c->phy_chan = &phys[i];
1400 d40c->log_num = D40_PHY_CHAN;
1401 goto out;
1403 if (dev_type == -1)
1404 return -EINVAL;
1406 /* Find logical channel */
1407 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1408 int phy_num = j + event_group * 2;
1410 * Spread logical channels across all available physical rather
1411 * than pack every logical channel at the first available phy
1412 * channels.
1414 if (is_src) {
1415 for (i = phy_num; i < phy_num + 2; i++) {
1416 if (d40_alloc_mask_set(&phys[i], is_src,
1417 event_line, is_log))
1418 goto found_log;
1420 } else {
1421 for (i = phy_num + 1; i >= phy_num; i--) {
1422 if (d40_alloc_mask_set(&phys[i], is_src,
1423 event_line, is_log))
1424 goto found_log;
1428 return -EINVAL;
1430 found_log:
1431 d40c->phy_chan = &phys[i];
1432 d40c->log_num = log_num;
1433 out:
1435 if (is_log)
1436 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1437 else
1438 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1440 return 0;
1444 static int d40_config_memcpy(struct d40_chan *d40c)
1446 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1448 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1449 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1450 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1451 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1452 memcpy[d40c->chan.chan_id];
1454 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1455 dma_has_cap(DMA_SLAVE, cap)) {
1456 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1457 } else {
1458 chan_err(d40c, "No memcpy\n");
1459 return -EINVAL;
1462 return 0;
1466 static int d40_free_dma(struct d40_chan *d40c)
1469 int res = 0;
1470 u32 event;
1471 struct d40_phy_res *phy = d40c->phy_chan;
1472 bool is_src;
1473 struct d40_desc *d;
1474 struct d40_desc *_d;
1477 /* Terminate all queued and active transfers */
1478 d40_term_all(d40c);
1480 /* Release client owned descriptors */
1481 if (!list_empty(&d40c->client))
1482 list_for_each_entry_safe(d, _d, &d40c->client, node) {
1483 d40_pool_lli_free(d40c, d);
1484 d40_desc_remove(d);
1485 d40_desc_free(d40c, d);
1488 if (phy == NULL) {
1489 chan_err(d40c, "phy == null\n");
1490 return -EINVAL;
1493 if (phy->allocated_src == D40_ALLOC_FREE &&
1494 phy->allocated_dst == D40_ALLOC_FREE) {
1495 chan_err(d40c, "channel already free\n");
1496 return -EINVAL;
1499 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1500 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1501 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1502 is_src = false;
1503 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1504 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1505 is_src = true;
1506 } else {
1507 chan_err(d40c, "Unknown direction\n");
1508 return -EINVAL;
1511 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1512 if (res) {
1513 chan_err(d40c, "suspend failed\n");
1514 return res;
1517 if (chan_is_logical(d40c)) {
1518 /* Release logical channel, deactivate the event line */
1520 d40_config_set_event(d40c, false);
1521 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1524 * Check if there are more logical allocation
1525 * on this phy channel.
1527 if (!d40_alloc_mask_free(phy, is_src, event)) {
1528 /* Resume the other logical channels if any */
1529 if (d40_chan_has_events(d40c)) {
1530 res = d40_channel_execute_command(d40c,
1531 D40_DMA_RUN);
1532 if (res) {
1533 chan_err(d40c,
1534 "Executing RUN command\n");
1535 return res;
1538 return 0;
1540 } else {
1541 (void) d40_alloc_mask_free(phy, is_src, 0);
1544 /* Release physical channel */
1545 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1546 if (res) {
1547 chan_err(d40c, "Failed to stop channel\n");
1548 return res;
1550 d40c->phy_chan = NULL;
1551 d40c->configured = false;
1552 d40c->base->lookup_phy_chans[phy->num] = NULL;
1554 return 0;
1557 static bool d40_is_paused(struct d40_chan *d40c)
1559 void __iomem *chanbase = chan_base(d40c);
1560 bool is_paused = false;
1561 unsigned long flags;
1562 void __iomem *active_reg;
1563 u32 status;
1564 u32 event;
1566 spin_lock_irqsave(&d40c->lock, flags);
1568 if (chan_is_physical(d40c)) {
1569 if (d40c->phy_chan->num % 2 == 0)
1570 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1571 else
1572 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1574 status = (readl(active_reg) &
1575 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1576 D40_CHAN_POS(d40c->phy_chan->num);
1577 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1578 is_paused = true;
1580 goto _exit;
1583 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1584 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1585 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1586 status = readl(chanbase + D40_CHAN_REG_SDLNK);
1587 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1588 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1589 status = readl(chanbase + D40_CHAN_REG_SSLNK);
1590 } else {
1591 chan_err(d40c, "Unknown direction\n");
1592 goto _exit;
1595 status = (status & D40_EVENTLINE_MASK(event)) >>
1596 D40_EVENTLINE_POS(event);
1598 if (status != D40_DMA_RUN)
1599 is_paused = true;
1600 _exit:
1601 spin_unlock_irqrestore(&d40c->lock, flags);
1602 return is_paused;
1607 static u32 stedma40_residue(struct dma_chan *chan)
1609 struct d40_chan *d40c =
1610 container_of(chan, struct d40_chan, chan);
1611 u32 bytes_left;
1612 unsigned long flags;
1614 spin_lock_irqsave(&d40c->lock, flags);
1615 bytes_left = d40_residue(d40c);
1616 spin_unlock_irqrestore(&d40c->lock, flags);
1618 return bytes_left;
1621 static struct d40_desc *
1622 d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1623 unsigned int sg_len, unsigned long dma_flags)
1625 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1626 struct d40_desc *desc;
1627 int ret;
1629 desc = d40_desc_get(chan);
1630 if (!desc)
1631 return NULL;
1633 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1634 cfg->dst_info.data_width);
1635 if (desc->lli_len < 0) {
1636 chan_err(chan, "Unaligned size\n");
1637 goto err;
1640 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1641 if (ret < 0) {
1642 chan_err(chan, "Could not allocate lli\n");
1643 goto err;
1647 desc->lli_current = 0;
1648 desc->txd.flags = dma_flags;
1649 desc->txd.tx_submit = d40_tx_submit;
1651 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1653 return desc;
1655 err:
1656 d40_desc_free(chan, desc);
1657 return NULL;
1660 struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
1661 struct scatterlist *sgl_dst,
1662 struct scatterlist *sgl_src,
1663 unsigned int sgl_len,
1664 unsigned long dma_flags)
1666 int res;
1667 struct d40_desc *d40d;
1668 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1669 chan);
1670 unsigned long flags;
1672 if (d40c->phy_chan == NULL) {
1673 chan_err(d40c, "Unallocated channel.\n");
1674 return ERR_PTR(-EINVAL);
1677 spin_lock_irqsave(&d40c->lock, flags);
1679 d40d = d40_prep_desc(d40c, sgl_dst, sgl_len, dma_flags);
1680 if (!d40d)
1681 goto err;
1683 if (chan_is_logical(d40c)) {
1684 (void) d40_log_sg_to_lli(sgl_src,
1685 sgl_len,
1686 d40d->lli_log.src,
1687 d40c->log_def.lcsp1,
1688 d40c->dma_cfg.src_info.data_width,
1689 d40c->dma_cfg.dst_info.data_width);
1691 (void) d40_log_sg_to_lli(sgl_dst,
1692 sgl_len,
1693 d40d->lli_log.dst,
1694 d40c->log_def.lcsp3,
1695 d40c->dma_cfg.dst_info.data_width,
1696 d40c->dma_cfg.src_info.data_width);
1697 } else {
1698 res = d40_phy_sg_to_lli(sgl_src,
1699 sgl_len,
1701 d40d->lli_phy.src,
1702 virt_to_phys(d40d->lli_phy.src),
1703 d40c->src_def_cfg,
1704 d40c->dma_cfg.src_info.data_width,
1705 d40c->dma_cfg.dst_info.data_width,
1706 d40c->dma_cfg.src_info.psize);
1708 if (res < 0)
1709 goto err;
1711 res = d40_phy_sg_to_lli(sgl_dst,
1712 sgl_len,
1714 d40d->lli_phy.dst,
1715 virt_to_phys(d40d->lli_phy.dst),
1716 d40c->dst_def_cfg,
1717 d40c->dma_cfg.dst_info.data_width,
1718 d40c->dma_cfg.src_info.data_width,
1719 d40c->dma_cfg.dst_info.psize);
1721 if (res < 0)
1722 goto err;
1724 dma_sync_single_for_device(d40c->base->dev,
1725 d40d->lli_pool.dma_addr,
1726 d40d->lli_pool.size, DMA_TO_DEVICE);
1729 spin_unlock_irqrestore(&d40c->lock, flags);
1731 return &d40d->txd;
1732 err:
1733 if (d40d)
1734 d40_desc_free(d40c, d40d);
1735 spin_unlock_irqrestore(&d40c->lock, flags);
1736 return NULL;
1738 EXPORT_SYMBOL(stedma40_memcpy_sg);
1740 bool stedma40_filter(struct dma_chan *chan, void *data)
1742 struct stedma40_chan_cfg *info = data;
1743 struct d40_chan *d40c =
1744 container_of(chan, struct d40_chan, chan);
1745 int err;
1747 if (data) {
1748 err = d40_validate_conf(d40c, info);
1749 if (!err)
1750 d40c->dma_cfg = *info;
1751 } else
1752 err = d40_config_memcpy(d40c);
1754 if (!err)
1755 d40c->configured = true;
1757 return err == 0;
1759 EXPORT_SYMBOL(stedma40_filter);
1761 static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1763 bool realtime = d40c->dma_cfg.realtime;
1764 bool highprio = d40c->dma_cfg.high_priority;
1765 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1766 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1767 u32 event = D40_TYPE_TO_EVENT(dev_type);
1768 u32 group = D40_TYPE_TO_GROUP(dev_type);
1769 u32 bit = 1 << event;
1771 /* Destination event lines are stored in the upper halfword */
1772 if (!src)
1773 bit <<= 16;
1775 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1776 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1779 static void d40_set_prio_realtime(struct d40_chan *d40c)
1781 if (d40c->base->rev < 3)
1782 return;
1784 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1785 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1786 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1788 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1789 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1790 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1793 /* DMA ENGINE functions */
1794 static int d40_alloc_chan_resources(struct dma_chan *chan)
1796 int err;
1797 unsigned long flags;
1798 struct d40_chan *d40c =
1799 container_of(chan, struct d40_chan, chan);
1800 bool is_free_phy;
1801 spin_lock_irqsave(&d40c->lock, flags);
1803 d40c->completed = chan->cookie = 1;
1805 /* If no dma configuration is set use default configuration (memcpy) */
1806 if (!d40c->configured) {
1807 err = d40_config_memcpy(d40c);
1808 if (err) {
1809 chan_err(d40c, "Failed to configure memcpy channel\n");
1810 goto fail;
1813 is_free_phy = (d40c->phy_chan == NULL);
1815 err = d40_allocate_channel(d40c);
1816 if (err) {
1817 chan_err(d40c, "Failed to allocate channel\n");
1818 goto fail;
1821 /* Fill in basic CFG register values */
1822 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
1823 &d40c->dst_def_cfg, chan_is_logical(d40c));
1825 d40_set_prio_realtime(d40c);
1827 if (chan_is_logical(d40c)) {
1828 d40_log_cfg(&d40c->dma_cfg,
1829 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1831 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1832 d40c->lcpa = d40c->base->lcpa_base +
1833 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1834 else
1835 d40c->lcpa = d40c->base->lcpa_base +
1836 d40c->dma_cfg.dst_dev_type *
1837 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1841 * Only write channel configuration to the DMA if the physical
1842 * resource is free. In case of multiple logical channels
1843 * on the same physical resource, only the first write is necessary.
1845 if (is_free_phy)
1846 d40_config_write(d40c);
1847 fail:
1848 spin_unlock_irqrestore(&d40c->lock, flags);
1849 return err;
1852 static void d40_free_chan_resources(struct dma_chan *chan)
1854 struct d40_chan *d40c =
1855 container_of(chan, struct d40_chan, chan);
1856 int err;
1857 unsigned long flags;
1859 if (d40c->phy_chan == NULL) {
1860 chan_err(d40c, "Cannot free unallocated channel\n");
1861 return;
1865 spin_lock_irqsave(&d40c->lock, flags);
1867 err = d40_free_dma(d40c);
1869 if (err)
1870 chan_err(d40c, "Failed to free channel\n");
1871 spin_unlock_irqrestore(&d40c->lock, flags);
1874 static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1875 dma_addr_t dst,
1876 dma_addr_t src,
1877 size_t size,
1878 unsigned long dma_flags)
1880 struct scatterlist dst_sg;
1881 struct scatterlist src_sg;
1883 sg_init_table(&dst_sg, 1);
1884 sg_init_table(&src_sg, 1);
1886 sg_dma_address(&dst_sg) = dst;
1887 sg_dma_address(&src_sg) = src;
1889 sg_dma_len(&dst_sg) = size;
1890 sg_dma_len(&src_sg) = size;
1892 return stedma40_memcpy_sg(chan, &dst_sg, &src_sg, 1, dma_flags);
1895 static struct dma_async_tx_descriptor *
1896 d40_prep_sg(struct dma_chan *chan,
1897 struct scatterlist *dst_sg, unsigned int dst_nents,
1898 struct scatterlist *src_sg, unsigned int src_nents,
1899 unsigned long dma_flags)
1901 if (dst_nents != src_nents)
1902 return NULL;
1904 return stedma40_memcpy_sg(chan, dst_sg, src_sg, dst_nents, dma_flags);
1907 static int d40_prep_slave_sg_log(struct d40_desc *d40d,
1908 struct d40_chan *d40c,
1909 struct scatterlist *sgl,
1910 unsigned int sg_len,
1911 enum dma_data_direction direction,
1912 unsigned long dma_flags)
1914 dma_addr_t dev_addr = 0;
1915 int total_size;
1917 if (direction == DMA_FROM_DEVICE)
1918 if (d40c->runtime_addr)
1919 dev_addr = d40c->runtime_addr;
1920 else
1921 dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
1922 else if (direction == DMA_TO_DEVICE)
1923 if (d40c->runtime_addr)
1924 dev_addr = d40c->runtime_addr;
1925 else
1926 dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
1928 else
1929 return -EINVAL;
1931 total_size = d40_log_sg_to_dev(sgl, sg_len,
1932 &d40d->lli_log,
1933 &d40c->log_def,
1934 d40c->dma_cfg.src_info.data_width,
1935 d40c->dma_cfg.dst_info.data_width,
1936 direction,
1937 dev_addr);
1939 if (total_size < 0)
1940 return -EINVAL;
1942 return 0;
1945 static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
1946 struct d40_chan *d40c,
1947 struct scatterlist *sgl,
1948 unsigned int sgl_len,
1949 enum dma_data_direction direction,
1950 unsigned long dma_flags)
1952 dma_addr_t src_dev_addr;
1953 dma_addr_t dst_dev_addr;
1954 int res;
1956 if (direction == DMA_FROM_DEVICE) {
1957 dst_dev_addr = 0;
1958 if (d40c->runtime_addr)
1959 src_dev_addr = d40c->runtime_addr;
1960 else
1961 src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
1962 } else if (direction == DMA_TO_DEVICE) {
1963 if (d40c->runtime_addr)
1964 dst_dev_addr = d40c->runtime_addr;
1965 else
1966 dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
1967 src_dev_addr = 0;
1968 } else
1969 return -EINVAL;
1971 res = d40_phy_sg_to_lli(sgl,
1972 sgl_len,
1973 src_dev_addr,
1974 d40d->lli_phy.src,
1975 virt_to_phys(d40d->lli_phy.src),
1976 d40c->src_def_cfg,
1977 d40c->dma_cfg.src_info.data_width,
1978 d40c->dma_cfg.dst_info.data_width,
1979 d40c->dma_cfg.src_info.psize);
1980 if (res < 0)
1981 return res;
1983 res = d40_phy_sg_to_lli(sgl,
1984 sgl_len,
1985 dst_dev_addr,
1986 d40d->lli_phy.dst,
1987 virt_to_phys(d40d->lli_phy.dst),
1988 d40c->dst_def_cfg,
1989 d40c->dma_cfg.dst_info.data_width,
1990 d40c->dma_cfg.src_info.data_width,
1991 d40c->dma_cfg.dst_info.psize);
1992 if (res < 0)
1993 return res;
1995 dma_sync_single_for_device(d40c->base->dev, d40d->lli_pool.dma_addr,
1996 d40d->lli_pool.size, DMA_TO_DEVICE);
1997 return 0;
2000 static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2001 struct scatterlist *sgl,
2002 unsigned int sg_len,
2003 enum dma_data_direction direction,
2004 unsigned long dma_flags)
2006 struct d40_desc *d40d;
2007 struct d40_chan *d40c = container_of(chan, struct d40_chan,
2008 chan);
2009 unsigned long flags;
2010 int err;
2012 if (d40c->phy_chan == NULL) {
2013 chan_err(d40c, "Cannot prepare unallocated channel\n");
2014 return ERR_PTR(-EINVAL);
2017 spin_lock_irqsave(&d40c->lock, flags);
2019 d40d = d40_prep_desc(d40c, sgl, sg_len, dma_flags);
2020 if (d40d == NULL)
2021 goto err;
2023 if (chan_is_logical(d40c))
2024 err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
2025 direction, dma_flags);
2026 else
2027 err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
2028 direction, dma_flags);
2029 if (err) {
2030 chan_err(d40c, "Failed to prepare %s slave sg job: %d\n",
2031 chan_is_logical(d40c) ? "log" : "phy", err);
2032 goto err;
2035 spin_unlock_irqrestore(&d40c->lock, flags);
2036 return &d40d->txd;
2038 err:
2039 if (d40d)
2040 d40_desc_free(d40c, d40d);
2041 spin_unlock_irqrestore(&d40c->lock, flags);
2042 return NULL;
2045 static enum dma_status d40_tx_status(struct dma_chan *chan,
2046 dma_cookie_t cookie,
2047 struct dma_tx_state *txstate)
2049 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2050 dma_cookie_t last_used;
2051 dma_cookie_t last_complete;
2052 int ret;
2054 if (d40c->phy_chan == NULL) {
2055 chan_err(d40c, "Cannot read status of unallocated channel\n");
2056 return -EINVAL;
2059 last_complete = d40c->completed;
2060 last_used = chan->cookie;
2062 if (d40_is_paused(d40c))
2063 ret = DMA_PAUSED;
2064 else
2065 ret = dma_async_is_complete(cookie, last_complete, last_used);
2067 dma_set_tx_state(txstate, last_complete, last_used,
2068 stedma40_residue(chan));
2070 return ret;
2073 static void d40_issue_pending(struct dma_chan *chan)
2075 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2076 unsigned long flags;
2078 if (d40c->phy_chan == NULL) {
2079 chan_err(d40c, "Channel is not allocated!\n");
2080 return;
2083 spin_lock_irqsave(&d40c->lock, flags);
2085 /* Busy means that pending jobs are already being processed */
2086 if (!d40c->busy)
2087 (void) d40_queue_start(d40c);
2089 spin_unlock_irqrestore(&d40c->lock, flags);
2092 /* Runtime reconfiguration extension */
2093 static void d40_set_runtime_config(struct dma_chan *chan,
2094 struct dma_slave_config *config)
2096 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2097 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2098 enum dma_slave_buswidth config_addr_width;
2099 dma_addr_t config_addr;
2100 u32 config_maxburst;
2101 enum stedma40_periph_data_width addr_width;
2102 int psize;
2104 if (config->direction == DMA_FROM_DEVICE) {
2105 dma_addr_t dev_addr_rx =
2106 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2108 config_addr = config->src_addr;
2109 if (dev_addr_rx)
2110 dev_dbg(d40c->base->dev,
2111 "channel has a pre-wired RX address %08x "
2112 "overriding with %08x\n",
2113 dev_addr_rx, config_addr);
2114 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2115 dev_dbg(d40c->base->dev,
2116 "channel was not configured for peripheral "
2117 "to memory transfer (%d) overriding\n",
2118 cfg->dir);
2119 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2121 config_addr_width = config->src_addr_width;
2122 config_maxburst = config->src_maxburst;
2124 } else if (config->direction == DMA_TO_DEVICE) {
2125 dma_addr_t dev_addr_tx =
2126 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2128 config_addr = config->dst_addr;
2129 if (dev_addr_tx)
2130 dev_dbg(d40c->base->dev,
2131 "channel has a pre-wired TX address %08x "
2132 "overriding with %08x\n",
2133 dev_addr_tx, config_addr);
2134 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2135 dev_dbg(d40c->base->dev,
2136 "channel was not configured for memory "
2137 "to peripheral transfer (%d) overriding\n",
2138 cfg->dir);
2139 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2141 config_addr_width = config->dst_addr_width;
2142 config_maxburst = config->dst_maxburst;
2144 } else {
2145 dev_err(d40c->base->dev,
2146 "unrecognized channel direction %d\n",
2147 config->direction);
2148 return;
2151 switch (config_addr_width) {
2152 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2153 addr_width = STEDMA40_BYTE_WIDTH;
2154 break;
2155 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2156 addr_width = STEDMA40_HALFWORD_WIDTH;
2157 break;
2158 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2159 addr_width = STEDMA40_WORD_WIDTH;
2160 break;
2161 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2162 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2163 break;
2164 default:
2165 dev_err(d40c->base->dev,
2166 "illegal peripheral address width "
2167 "requested (%d)\n",
2168 config->src_addr_width);
2169 return;
2172 if (chan_is_logical(d40c)) {
2173 if (config_maxburst >= 16)
2174 psize = STEDMA40_PSIZE_LOG_16;
2175 else if (config_maxburst >= 8)
2176 psize = STEDMA40_PSIZE_LOG_8;
2177 else if (config_maxburst >= 4)
2178 psize = STEDMA40_PSIZE_LOG_4;
2179 else
2180 psize = STEDMA40_PSIZE_LOG_1;
2181 } else {
2182 if (config_maxburst >= 16)
2183 psize = STEDMA40_PSIZE_PHY_16;
2184 else if (config_maxburst >= 8)
2185 psize = STEDMA40_PSIZE_PHY_8;
2186 else if (config_maxburst >= 4)
2187 psize = STEDMA40_PSIZE_PHY_4;
2188 else if (config_maxburst >= 2)
2189 psize = STEDMA40_PSIZE_PHY_2;
2190 else
2191 psize = STEDMA40_PSIZE_PHY_1;
2194 /* Set up all the endpoint configs */
2195 cfg->src_info.data_width = addr_width;
2196 cfg->src_info.psize = psize;
2197 cfg->src_info.big_endian = false;
2198 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2199 cfg->dst_info.data_width = addr_width;
2200 cfg->dst_info.psize = psize;
2201 cfg->dst_info.big_endian = false;
2202 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2204 /* Fill in register values */
2205 if (chan_is_logical(d40c))
2206 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2207 else
2208 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2209 &d40c->dst_def_cfg, false);
2211 /* These settings will take precedence later */
2212 d40c->runtime_addr = config_addr;
2213 d40c->runtime_direction = config->direction;
2214 dev_dbg(d40c->base->dev,
2215 "configured channel %s for %s, data width %d, "
2216 "maxburst %d bytes, LE, no flow control\n",
2217 dma_chan_name(chan),
2218 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2219 config_addr_width,
2220 config_maxburst);
2223 static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2224 unsigned long arg)
2226 unsigned long flags;
2227 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2229 if (d40c->phy_chan == NULL) {
2230 chan_err(d40c, "Channel is not allocated!\n");
2231 return -EINVAL;
2234 switch (cmd) {
2235 case DMA_TERMINATE_ALL:
2236 spin_lock_irqsave(&d40c->lock, flags);
2237 d40_term_all(d40c);
2238 spin_unlock_irqrestore(&d40c->lock, flags);
2239 return 0;
2240 case DMA_PAUSE:
2241 return d40_pause(chan);
2242 case DMA_RESUME:
2243 return d40_resume(chan);
2244 case DMA_SLAVE_CONFIG:
2245 d40_set_runtime_config(chan,
2246 (struct dma_slave_config *) arg);
2247 return 0;
2248 default:
2249 break;
2252 /* Other commands are unimplemented */
2253 return -ENXIO;
2256 /* Initialization functions */
2258 static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2259 struct d40_chan *chans, int offset,
2260 int num_chans)
2262 int i = 0;
2263 struct d40_chan *d40c;
2265 INIT_LIST_HEAD(&dma->channels);
2267 for (i = offset; i < offset + num_chans; i++) {
2268 d40c = &chans[i];
2269 d40c->base = base;
2270 d40c->chan.device = dma;
2272 spin_lock_init(&d40c->lock);
2274 d40c->log_num = D40_PHY_CHAN;
2276 INIT_LIST_HEAD(&d40c->active);
2277 INIT_LIST_HEAD(&d40c->queue);
2278 INIT_LIST_HEAD(&d40c->client);
2280 tasklet_init(&d40c->tasklet, dma_tasklet,
2281 (unsigned long) d40c);
2283 list_add_tail(&d40c->chan.device_node,
2284 &dma->channels);
2288 static int __init d40_dmaengine_init(struct d40_base *base,
2289 int num_reserved_chans)
2291 int err ;
2293 d40_chan_init(base, &base->dma_slave, base->log_chans,
2294 0, base->num_log_chans);
2296 dma_cap_zero(base->dma_slave.cap_mask);
2297 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2299 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2300 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2301 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
2302 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
2303 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2304 base->dma_slave.device_tx_status = d40_tx_status;
2305 base->dma_slave.device_issue_pending = d40_issue_pending;
2306 base->dma_slave.device_control = d40_control;
2307 base->dma_slave.dev = base->dev;
2309 err = dma_async_device_register(&base->dma_slave);
2311 if (err) {
2312 d40_err(base->dev, "Failed to register slave channels\n");
2313 goto failure1;
2316 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2317 base->num_log_chans, base->plat_data->memcpy_len);
2319 dma_cap_zero(base->dma_memcpy.cap_mask);
2320 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
2321 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
2323 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2324 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2325 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
2326 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
2327 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2328 base->dma_memcpy.device_tx_status = d40_tx_status;
2329 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2330 base->dma_memcpy.device_control = d40_control;
2331 base->dma_memcpy.dev = base->dev;
2333 * This controller can only access address at even
2334 * 32bit boundaries, i.e. 2^2
2336 base->dma_memcpy.copy_align = 2;
2338 err = dma_async_device_register(&base->dma_memcpy);
2340 if (err) {
2341 d40_err(base->dev,
2342 "Failed to regsiter memcpy only channels\n");
2343 goto failure2;
2346 d40_chan_init(base, &base->dma_both, base->phy_chans,
2347 0, num_reserved_chans);
2349 dma_cap_zero(base->dma_both.cap_mask);
2350 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2351 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
2352 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
2354 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2355 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2356 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
2357 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
2358 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2359 base->dma_both.device_tx_status = d40_tx_status;
2360 base->dma_both.device_issue_pending = d40_issue_pending;
2361 base->dma_both.device_control = d40_control;
2362 base->dma_both.dev = base->dev;
2363 base->dma_both.copy_align = 2;
2364 err = dma_async_device_register(&base->dma_both);
2366 if (err) {
2367 d40_err(base->dev,
2368 "Failed to register logical and physical capable channels\n");
2369 goto failure3;
2371 return 0;
2372 failure3:
2373 dma_async_device_unregister(&base->dma_memcpy);
2374 failure2:
2375 dma_async_device_unregister(&base->dma_slave);
2376 failure1:
2377 return err;
2380 /* Initialization functions. */
2382 static int __init d40_phy_res_init(struct d40_base *base)
2384 int i;
2385 int num_phy_chans_avail = 0;
2386 u32 val[2];
2387 int odd_even_bit = -2;
2389 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2390 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2392 for (i = 0; i < base->num_phy_chans; i++) {
2393 base->phy_res[i].num = i;
2394 odd_even_bit += 2 * ((i % 2) == 0);
2395 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2396 /* Mark security only channels as occupied */
2397 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2398 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2399 } else {
2400 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2401 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2402 num_phy_chans_avail++;
2404 spin_lock_init(&base->phy_res[i].lock);
2407 /* Mark disabled channels as occupied */
2408 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
2409 int chan = base->plat_data->disabled_channels[i];
2411 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2412 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2413 num_phy_chans_avail--;
2416 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2417 num_phy_chans_avail, base->num_phy_chans);
2419 /* Verify settings extended vs standard */
2420 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2422 for (i = 0; i < base->num_phy_chans; i++) {
2424 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2425 (val[0] & 0x3) != 1)
2426 dev_info(base->dev,
2427 "[%s] INFO: channel %d is misconfigured (%d)\n",
2428 __func__, i, val[0] & 0x3);
2430 val[0] = val[0] >> 2;
2433 return num_phy_chans_avail;
2436 static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2438 static const struct d40_reg_val dma_id_regs[] = {
2439 /* Peripheral Id */
2440 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2441 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2443 * D40_DREG_PERIPHID2 Depends on HW revision:
2444 * DB8500ed has 0x0008,
2445 * ? has 0x0018,
2446 * DB8500v1 has 0x0028
2447 * DB8500v2 has 0x0038
2449 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2451 /* PCell Id */
2452 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2453 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2454 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2455 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2457 struct stedma40_platform_data *plat_data;
2458 struct clk *clk = NULL;
2459 void __iomem *virtbase = NULL;
2460 struct resource *res = NULL;
2461 struct d40_base *base = NULL;
2462 int num_log_chans = 0;
2463 int num_phy_chans;
2464 int i;
2465 u32 val;
2466 u32 rev;
2468 clk = clk_get(&pdev->dev, NULL);
2470 if (IS_ERR(clk)) {
2471 d40_err(&pdev->dev, "No matching clock found\n");
2472 goto failure;
2475 clk_enable(clk);
2477 /* Get IO for DMAC base address */
2478 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2479 if (!res)
2480 goto failure;
2482 if (request_mem_region(res->start, resource_size(res),
2483 D40_NAME " I/O base") == NULL)
2484 goto failure;
2486 virtbase = ioremap(res->start, resource_size(res));
2487 if (!virtbase)
2488 goto failure;
2490 /* HW version check */
2491 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2492 if (dma_id_regs[i].val !=
2493 readl(virtbase + dma_id_regs[i].reg)) {
2494 d40_err(&pdev->dev,
2495 "Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
2496 dma_id_regs[i].val,
2497 dma_id_regs[i].reg,
2498 readl(virtbase + dma_id_regs[i].reg));
2499 goto failure;
2503 /* Get silicon revision and designer */
2504 val = readl(virtbase + D40_DREG_PERIPHID2);
2506 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2507 D40_HW_DESIGNER) {
2508 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2509 val & D40_DREG_PERIPHID2_DESIGNER_MASK,
2510 D40_HW_DESIGNER);
2511 goto failure;
2514 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2515 D40_DREG_PERIPHID2_REV_POS;
2517 /* The number of physical channels on this HW */
2518 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2520 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
2521 rev, res->start);
2523 plat_data = pdev->dev.platform_data;
2525 /* Count the number of logical channels in use */
2526 for (i = 0; i < plat_data->dev_len; i++)
2527 if (plat_data->dev_rx[i] != 0)
2528 num_log_chans++;
2530 for (i = 0; i < plat_data->dev_len; i++)
2531 if (plat_data->dev_tx[i] != 0)
2532 num_log_chans++;
2534 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2535 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2536 sizeof(struct d40_chan), GFP_KERNEL);
2538 if (base == NULL) {
2539 d40_err(&pdev->dev, "Out of memory\n");
2540 goto failure;
2543 base->rev = rev;
2544 base->clk = clk;
2545 base->num_phy_chans = num_phy_chans;
2546 base->num_log_chans = num_log_chans;
2547 base->phy_start = res->start;
2548 base->phy_size = resource_size(res);
2549 base->virtbase = virtbase;
2550 base->plat_data = plat_data;
2551 base->dev = &pdev->dev;
2552 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2553 base->log_chans = &base->phy_chans[num_phy_chans];
2555 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2556 GFP_KERNEL);
2557 if (!base->phy_res)
2558 goto failure;
2560 base->lookup_phy_chans = kzalloc(num_phy_chans *
2561 sizeof(struct d40_chan *),
2562 GFP_KERNEL);
2563 if (!base->lookup_phy_chans)
2564 goto failure;
2566 if (num_log_chans + plat_data->memcpy_len) {
2568 * The max number of logical channels are event lines for all
2569 * src devices and dst devices
2571 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2572 sizeof(struct d40_chan *),
2573 GFP_KERNEL);
2574 if (!base->lookup_log_chans)
2575 goto failure;
2578 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2579 sizeof(struct d40_desc *) *
2580 D40_LCLA_LINK_PER_EVENT_GRP,
2581 GFP_KERNEL);
2582 if (!base->lcla_pool.alloc_map)
2583 goto failure;
2585 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2586 0, SLAB_HWCACHE_ALIGN,
2587 NULL);
2588 if (base->desc_slab == NULL)
2589 goto failure;
2591 return base;
2593 failure:
2594 if (!IS_ERR(clk)) {
2595 clk_disable(clk);
2596 clk_put(clk);
2598 if (virtbase)
2599 iounmap(virtbase);
2600 if (res)
2601 release_mem_region(res->start,
2602 resource_size(res));
2603 if (virtbase)
2604 iounmap(virtbase);
2606 if (base) {
2607 kfree(base->lcla_pool.alloc_map);
2608 kfree(base->lookup_log_chans);
2609 kfree(base->lookup_phy_chans);
2610 kfree(base->phy_res);
2611 kfree(base);
2614 return NULL;
2617 static void __init d40_hw_init(struct d40_base *base)
2620 static const struct d40_reg_val dma_init_reg[] = {
2621 /* Clock every part of the DMA block from start */
2622 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2624 /* Interrupts on all logical channels */
2625 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2626 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2627 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2628 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2629 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2630 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2631 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2632 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2633 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2634 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2635 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2636 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2638 int i;
2639 u32 prmseo[2] = {0, 0};
2640 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2641 u32 pcmis = 0;
2642 u32 pcicr = 0;
2644 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2645 writel(dma_init_reg[i].val,
2646 base->virtbase + dma_init_reg[i].reg);
2648 /* Configure all our dma channels to default settings */
2649 for (i = 0; i < base->num_phy_chans; i++) {
2651 activeo[i % 2] = activeo[i % 2] << 2;
2653 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2654 == D40_ALLOC_PHY) {
2655 activeo[i % 2] |= 3;
2656 continue;
2659 /* Enable interrupt # */
2660 pcmis = (pcmis << 1) | 1;
2662 /* Clear interrupt # */
2663 pcicr = (pcicr << 1) | 1;
2665 /* Set channel to physical mode */
2666 prmseo[i % 2] = prmseo[i % 2] << 2;
2667 prmseo[i % 2] |= 1;
2671 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2672 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2673 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2674 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2676 /* Write which interrupt to enable */
2677 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2679 /* Write which interrupt to clear */
2680 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2684 static int __init d40_lcla_allocate(struct d40_base *base)
2686 struct d40_lcla_pool *pool = &base->lcla_pool;
2687 unsigned long *page_list;
2688 int i, j;
2689 int ret = 0;
2692 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2693 * To full fill this hardware requirement without wasting 256 kb
2694 * we allocate pages until we get an aligned one.
2696 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2697 GFP_KERNEL);
2699 if (!page_list) {
2700 ret = -ENOMEM;
2701 goto failure;
2704 /* Calculating how many pages that are required */
2705 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2707 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2708 page_list[i] = __get_free_pages(GFP_KERNEL,
2709 base->lcla_pool.pages);
2710 if (!page_list[i]) {
2712 d40_err(base->dev, "Failed to allocate %d pages.\n",
2713 base->lcla_pool.pages);
2715 for (j = 0; j < i; j++)
2716 free_pages(page_list[j], base->lcla_pool.pages);
2717 goto failure;
2720 if ((virt_to_phys((void *)page_list[i]) &
2721 (LCLA_ALIGNMENT - 1)) == 0)
2722 break;
2725 for (j = 0; j < i; j++)
2726 free_pages(page_list[j], base->lcla_pool.pages);
2728 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2729 base->lcla_pool.base = (void *)page_list[i];
2730 } else {
2732 * After many attempts and no succees with finding the correct
2733 * alignment, try with allocating a big buffer.
2735 dev_warn(base->dev,
2736 "[%s] Failed to get %d pages @ 18 bit align.\n",
2737 __func__, base->lcla_pool.pages);
2738 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2739 base->num_phy_chans +
2740 LCLA_ALIGNMENT,
2741 GFP_KERNEL);
2742 if (!base->lcla_pool.base_unaligned) {
2743 ret = -ENOMEM;
2744 goto failure;
2747 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2748 LCLA_ALIGNMENT);
2751 pool->dma_addr = dma_map_single(base->dev, pool->base,
2752 SZ_1K * base->num_phy_chans,
2753 DMA_TO_DEVICE);
2754 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2755 pool->dma_addr = 0;
2756 ret = -ENOMEM;
2757 goto failure;
2760 writel(virt_to_phys(base->lcla_pool.base),
2761 base->virtbase + D40_DREG_LCLA);
2762 failure:
2763 kfree(page_list);
2764 return ret;
2767 static int __init d40_probe(struct platform_device *pdev)
2769 int err;
2770 int ret = -ENOENT;
2771 struct d40_base *base;
2772 struct resource *res = NULL;
2773 int num_reserved_chans;
2774 u32 val;
2776 base = d40_hw_detect_init(pdev);
2778 if (!base)
2779 goto failure;
2781 num_reserved_chans = d40_phy_res_init(base);
2783 platform_set_drvdata(pdev, base);
2785 spin_lock_init(&base->interrupt_lock);
2786 spin_lock_init(&base->execmd_lock);
2788 /* Get IO for logical channel parameter address */
2789 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2790 if (!res) {
2791 ret = -ENOENT;
2792 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
2793 goto failure;
2795 base->lcpa_size = resource_size(res);
2796 base->phy_lcpa = res->start;
2798 if (request_mem_region(res->start, resource_size(res),
2799 D40_NAME " I/O lcpa") == NULL) {
2800 ret = -EBUSY;
2801 d40_err(&pdev->dev,
2802 "Failed to request LCPA region 0x%x-0x%x\n",
2803 res->start, res->end);
2804 goto failure;
2807 /* We make use of ESRAM memory for this. */
2808 val = readl(base->virtbase + D40_DREG_LCPA);
2809 if (res->start != val && val != 0) {
2810 dev_warn(&pdev->dev,
2811 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2812 __func__, val, res->start);
2813 } else
2814 writel(res->start, base->virtbase + D40_DREG_LCPA);
2816 base->lcpa_base = ioremap(res->start, resource_size(res));
2817 if (!base->lcpa_base) {
2818 ret = -ENOMEM;
2819 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
2820 goto failure;
2823 ret = d40_lcla_allocate(base);
2824 if (ret) {
2825 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
2826 goto failure;
2829 spin_lock_init(&base->lcla_pool.lock);
2831 base->irq = platform_get_irq(pdev, 0);
2833 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
2834 if (ret) {
2835 d40_err(&pdev->dev, "No IRQ defined\n");
2836 goto failure;
2839 err = d40_dmaengine_init(base, num_reserved_chans);
2840 if (err)
2841 goto failure;
2843 d40_hw_init(base);
2845 dev_info(base->dev, "initialized\n");
2846 return 0;
2848 failure:
2849 if (base) {
2850 if (base->desc_slab)
2851 kmem_cache_destroy(base->desc_slab);
2852 if (base->virtbase)
2853 iounmap(base->virtbase);
2855 if (base->lcla_pool.dma_addr)
2856 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2857 SZ_1K * base->num_phy_chans,
2858 DMA_TO_DEVICE);
2860 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2861 free_pages((unsigned long)base->lcla_pool.base,
2862 base->lcla_pool.pages);
2864 kfree(base->lcla_pool.base_unaligned);
2866 if (base->phy_lcpa)
2867 release_mem_region(base->phy_lcpa,
2868 base->lcpa_size);
2869 if (base->phy_start)
2870 release_mem_region(base->phy_start,
2871 base->phy_size);
2872 if (base->clk) {
2873 clk_disable(base->clk);
2874 clk_put(base->clk);
2877 kfree(base->lcla_pool.alloc_map);
2878 kfree(base->lookup_log_chans);
2879 kfree(base->lookup_phy_chans);
2880 kfree(base->phy_res);
2881 kfree(base);
2884 d40_err(&pdev->dev, "probe failed\n");
2885 return ret;
2888 static struct platform_driver d40_driver = {
2889 .driver = {
2890 .owner = THIS_MODULE,
2891 .name = D40_NAME,
2895 static int __init stedma40_init(void)
2897 return platform_driver_probe(&d40_driver, d40_probe);
2899 arch_initcall(stedma40_init);