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
9 #include <linux/dma-mapping.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/dmaengine.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/amba/bus.h>
19 #include <plat/ste_dma40.h>
21 #include "ste_dma40_ll.h"
23 #define D40_NAME "dma40"
25 #define D40_PHY_CHAN -1
27 /* For masking out/in 2 bit channel positions */
28 #define D40_CHAN_POS(chan) (2 * (chan / 2))
29 #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
31 /* Maximum iterations taken before giving up suspending a channel */
32 #define D40_SUSPEND_MAX_IT 500
34 /* Hardware requirement on LCLA alignment */
35 #define LCLA_ALIGNMENT 0x40000
37 /* Max number of links per event group */
38 #define D40_LCLA_LINK_PER_EVENT_GRP 128
39 #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
41 /* Attempts before giving up to trying to get pages that are aligned */
42 #define MAX_LCLA_ALLOC_ATTEMPTS 256
44 /* Bit markings for allocation map */
45 #define D40_ALLOC_FREE (1 << 31)
46 #define D40_ALLOC_PHY (1 << 30)
47 #define D40_ALLOC_LOG_FREE 0
50 * enum 40_command - The different commands and/or statuses.
52 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
53 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
54 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
55 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
60 D40_DMA_SUSPEND_REQ
= 2,
65 * struct d40_lli_pool - Structure for keeping LLIs in memory
67 * @base: Pointer to memory area when the pre_alloc_lli's are not large
68 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
69 * pre_alloc_lli is used.
70 * @dma_addr: DMA address, if mapped
71 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
72 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
73 * one buffer to one buffer.
79 /* Space for dst and src, plus an extra for padding */
80 u8 pre_alloc_lli
[3 * sizeof(struct d40_phy_lli
)];
84 * struct d40_desc - A descriptor is one DMA job.
86 * @lli_phy: LLI settings for physical channel. Both src and dst=
87 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
89 * @lli_log: Same as above but for logical channels.
90 * @lli_pool: The pool with two entries pre-allocated.
91 * @lli_len: Number of llis of current descriptor.
92 * @lli_current: Number of transferred llis.
93 * @lcla_alloc: Number of LCLA entries allocated.
94 * @txd: DMA engine struct. Used for among other things for communication
97 * @is_in_client_list: true if the client owns this descriptor.
100 * This descriptor is used for both logical and physical transfers.
104 struct d40_phy_lli_bidir lli_phy
;
106 struct d40_log_lli_bidir lli_log
;
108 struct d40_lli_pool lli_pool
;
113 struct dma_async_tx_descriptor txd
;
114 struct list_head node
;
116 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
{
134 void *base_unaligned
;
137 struct d40_desc
**alloc_map
;
141 * struct d40_phy_res - struct for handling eventlines mapped to physical
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
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
168 * @pending_tx: The number of pending transfers. Used between interrupt handler
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 * @pending_queue: Submitted jobs, to be issued by issue_pending()
178 * @active: Active descriptor.
179 * @queue: Queued jobs.
180 * @prepare_queue: Prepared jobs.
181 * @dma_cfg: The client configuration of this dma channel.
182 * @configured: whether the dma_cfg configuration is valid
183 * @base: Pointer to the device instance struct.
184 * @src_def_cfg: Default cfg register setting for src.
185 * @dst_def_cfg: Default cfg register setting for dst.
186 * @log_def: Default logical channel settings.
187 * @lcla: Space for one dst src pair for logical channel transfers.
188 * @lcpa: Pointer to dst and src lcpa settings.
189 * @runtime_addr: runtime configured address.
190 * @runtime_direction: runtime configured direction.
192 * This struct can either "be" a logical or a physical channel.
197 /* ID of the most recent completed transfer */
201 struct d40_phy_res
*phy_chan
;
202 struct dma_chan chan
;
203 struct tasklet_struct tasklet
;
204 struct list_head client
;
205 struct list_head pending_queue
;
206 struct list_head active
;
207 struct list_head queue
;
208 struct list_head prepare_queue
;
209 struct stedma40_chan_cfg dma_cfg
;
211 struct d40_base
*base
;
212 /* Default register configurations */
215 struct d40_def_lcsp log_def
;
216 struct d40_log_lli_full
*lcpa
;
217 /* Runtime reconfiguration */
218 dma_addr_t runtime_addr
;
219 enum dma_data_direction runtime_direction
;
223 * struct d40_base - The big global struct, one for each probe'd instance.
225 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
226 * @execmd_lock: Lock for execute command usage since several channels share
227 * the same physical register.
228 * @dev: The device structure.
229 * @virtbase: The virtual base address of the DMA's register.
230 * @rev: silicon revision detected.
231 * @clk: Pointer to the DMA clock structure.
232 * @phy_start: Physical memory start of the DMA registers.
233 * @phy_size: Size of the DMA register map.
234 * @irq: The IRQ number.
235 * @num_phy_chans: The number of physical channels. Read from HW. This
236 * is the number of available channels for this driver, not counting "Secure
237 * mode" allocated physical channels.
238 * @num_log_chans: The number of logical channels. Calculated from
240 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
241 * @dma_slave: dma_device channels that can do only do slave transfers.
242 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
243 * @log_chans: Room for all possible logical channels in system.
244 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
245 * to log_chans entries.
246 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
247 * to phy_chans entries.
248 * @plat_data: Pointer to provided platform_data which is the driver
250 * @phy_res: Vector containing all physical channels.
251 * @lcla_pool: lcla pool settings and data.
252 * @lcpa_base: The virtual mapped address of LCPA.
253 * @phy_lcpa: The physical address of the LCPA.
254 * @lcpa_size: The size of the LCPA area.
255 * @desc_slab: cache for descriptors.
258 spinlock_t interrupt_lock
;
259 spinlock_t execmd_lock
;
261 void __iomem
*virtbase
;
264 phys_addr_t phy_start
;
265 resource_size_t phy_size
;
269 struct dma_device dma_both
;
270 struct dma_device dma_slave
;
271 struct dma_device dma_memcpy
;
272 struct d40_chan
*phy_chans
;
273 struct d40_chan
*log_chans
;
274 struct d40_chan
**lookup_log_chans
;
275 struct d40_chan
**lookup_phy_chans
;
276 struct stedma40_platform_data
*plat_data
;
277 /* Physical half channels */
278 struct d40_phy_res
*phy_res
;
279 struct d40_lcla_pool lcla_pool
;
282 resource_size_t lcpa_size
;
283 struct kmem_cache
*desc_slab
;
287 * struct d40_interrupt_lookup - lookup table for interrupt handler
289 * @src: Interrupt mask register.
290 * @clr: Interrupt clear register.
291 * @is_error: true if this is an error interrupt.
292 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
293 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
295 struct d40_interrupt_lookup
{
303 * struct d40_reg_val - simple lookup struct
305 * @reg: The register.
306 * @val: The value that belongs to the register in reg.
313 static struct device
*chan2dev(struct d40_chan
*d40c
)
315 return &d40c
->chan
.dev
->device
;
318 static bool chan_is_physical(struct d40_chan
*chan
)
320 return chan
->log_num
== D40_PHY_CHAN
;
323 static bool chan_is_logical(struct d40_chan
*chan
)
325 return !chan_is_physical(chan
);
328 static void __iomem
*chan_base(struct d40_chan
*chan
)
330 return chan
->base
->virtbase
+ D40_DREG_PCBASE
+
331 chan
->phy_chan
->num
* D40_DREG_PCDELTA
;
334 #define d40_err(dev, format, arg...) \
335 dev_err(dev, "[%s] " format, __func__, ## arg)
337 #define chan_err(d40c, format, arg...) \
338 d40_err(chan2dev(d40c), format, ## arg)
340 static int d40_pool_lli_alloc(struct d40_chan
*d40c
, struct d40_desc
*d40d
,
343 bool is_log
= chan_is_logical(d40c
);
348 align
= sizeof(struct d40_log_lli
);
350 align
= sizeof(struct d40_phy_lli
);
353 base
= d40d
->lli_pool
.pre_alloc_lli
;
354 d40d
->lli_pool
.size
= sizeof(d40d
->lli_pool
.pre_alloc_lli
);
355 d40d
->lli_pool
.base
= NULL
;
357 d40d
->lli_pool
.size
= lli_len
* 2 * align
;
359 base
= kmalloc(d40d
->lli_pool
.size
+ align
, GFP_NOWAIT
);
360 d40d
->lli_pool
.base
= base
;
362 if (d40d
->lli_pool
.base
== NULL
)
367 d40d
->lli_log
.src
= PTR_ALIGN(base
, align
);
368 d40d
->lli_log
.dst
= d40d
->lli_log
.src
+ lli_len
;
370 d40d
->lli_pool
.dma_addr
= 0;
372 d40d
->lli_phy
.src
= PTR_ALIGN(base
, align
);
373 d40d
->lli_phy
.dst
= d40d
->lli_phy
.src
+ lli_len
;
375 d40d
->lli_pool
.dma_addr
= dma_map_single(d40c
->base
->dev
,
380 if (dma_mapping_error(d40c
->base
->dev
,
381 d40d
->lli_pool
.dma_addr
)) {
382 kfree(d40d
->lli_pool
.base
);
383 d40d
->lli_pool
.base
= NULL
;
384 d40d
->lli_pool
.dma_addr
= 0;
392 static void d40_pool_lli_free(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
394 if (d40d
->lli_pool
.dma_addr
)
395 dma_unmap_single(d40c
->base
->dev
, d40d
->lli_pool
.dma_addr
,
396 d40d
->lli_pool
.size
, DMA_TO_DEVICE
);
398 kfree(d40d
->lli_pool
.base
);
399 d40d
->lli_pool
.base
= NULL
;
400 d40d
->lli_pool
.size
= 0;
401 d40d
->lli_log
.src
= NULL
;
402 d40d
->lli_log
.dst
= NULL
;
403 d40d
->lli_phy
.src
= NULL
;
404 d40d
->lli_phy
.dst
= NULL
;
407 static int d40_lcla_alloc_one(struct d40_chan
*d40c
,
408 struct d40_desc
*d40d
)
415 spin_lock_irqsave(&d40c
->base
->lcla_pool
.lock
, flags
);
417 p
= d40c
->phy_chan
->num
* D40_LCLA_LINK_PER_EVENT_GRP
;
420 * Allocate both src and dst at the same time, therefore the half
421 * start on 1 since 0 can't be used since zero is used as end marker.
423 for (i
= 1 ; i
< D40_LCLA_LINK_PER_EVENT_GRP
/ 2; i
++) {
424 if (!d40c
->base
->lcla_pool
.alloc_map
[p
+ i
]) {
425 d40c
->base
->lcla_pool
.alloc_map
[p
+ i
] = d40d
;
432 spin_unlock_irqrestore(&d40c
->base
->lcla_pool
.lock
, flags
);
437 static int d40_lcla_free_all(struct d40_chan
*d40c
,
438 struct d40_desc
*d40d
)
444 if (chan_is_physical(d40c
))
447 spin_lock_irqsave(&d40c
->base
->lcla_pool
.lock
, flags
);
449 for (i
= 1 ; i
< D40_LCLA_LINK_PER_EVENT_GRP
/ 2; i
++) {
450 if (d40c
->base
->lcla_pool
.alloc_map
[d40c
->phy_chan
->num
*
451 D40_LCLA_LINK_PER_EVENT_GRP
+ i
] == d40d
) {
452 d40c
->base
->lcla_pool
.alloc_map
[d40c
->phy_chan
->num
*
453 D40_LCLA_LINK_PER_EVENT_GRP
+ i
] = NULL
;
455 if (d40d
->lcla_alloc
== 0) {
462 spin_unlock_irqrestore(&d40c
->base
->lcla_pool
.lock
, flags
);
468 static void d40_desc_remove(struct d40_desc
*d40d
)
470 list_del(&d40d
->node
);
473 static struct d40_desc
*d40_desc_get(struct d40_chan
*d40c
)
475 struct d40_desc
*desc
= NULL
;
477 if (!list_empty(&d40c
->client
)) {
481 list_for_each_entry_safe(d
, _d
, &d40c
->client
, node
)
482 if (async_tx_test_ack(&d
->txd
)) {
485 memset(desc
, 0, sizeof(*desc
));
491 desc
= kmem_cache_zalloc(d40c
->base
->desc_slab
, GFP_NOWAIT
);
494 INIT_LIST_HEAD(&desc
->node
);
499 static void d40_desc_free(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
502 d40_pool_lli_free(d40c
, d40d
);
503 d40_lcla_free_all(d40c
, d40d
);
504 kmem_cache_free(d40c
->base
->desc_slab
, d40d
);
507 static void d40_desc_submit(struct d40_chan
*d40c
, struct d40_desc
*desc
)
509 list_add_tail(&desc
->node
, &d40c
->active
);
512 static void d40_phy_lli_load(struct d40_chan
*chan
, struct d40_desc
*desc
)
514 struct d40_phy_lli
*lli_dst
= desc
->lli_phy
.dst
;
515 struct d40_phy_lli
*lli_src
= desc
->lli_phy
.src
;
516 void __iomem
*base
= chan_base(chan
);
518 writel(lli_src
->reg_cfg
, base
+ D40_CHAN_REG_SSCFG
);
519 writel(lli_src
->reg_elt
, base
+ D40_CHAN_REG_SSELT
);
520 writel(lli_src
->reg_ptr
, base
+ D40_CHAN_REG_SSPTR
);
521 writel(lli_src
->reg_lnk
, base
+ D40_CHAN_REG_SSLNK
);
523 writel(lli_dst
->reg_cfg
, base
+ D40_CHAN_REG_SDCFG
);
524 writel(lli_dst
->reg_elt
, base
+ D40_CHAN_REG_SDELT
);
525 writel(lli_dst
->reg_ptr
, base
+ D40_CHAN_REG_SDPTR
);
526 writel(lli_dst
->reg_lnk
, base
+ D40_CHAN_REG_SDLNK
);
529 static void d40_log_lli_to_lcxa(struct d40_chan
*chan
, struct d40_desc
*desc
)
531 struct d40_lcla_pool
*pool
= &chan
->base
->lcla_pool
;
532 struct d40_log_lli_bidir
*lli
= &desc
->lli_log
;
533 int lli_current
= desc
->lli_current
;
534 int lli_len
= desc
->lli_len
;
535 bool cyclic
= desc
->cyclic
;
536 int curr_lcla
= -EINVAL
;
541 * We may have partially running cyclic transfers, in case we did't get
542 * enough LCLA entries.
544 linkback
= cyclic
&& lli_current
== 0;
547 * For linkback, we need one LCLA even with only one link, because we
548 * can't link back to the one in LCPA space
550 if (linkback
|| (lli_len
- lli_current
> 1)) {
551 curr_lcla
= d40_lcla_alloc_one(chan
, desc
);
552 first_lcla
= curr_lcla
;
556 * For linkback, we normally load the LCPA in the loop since we need to
557 * link it to the second LCLA and not the first. However, if we
558 * couldn't even get a first LCLA, then we have to run in LCPA and
561 if (!linkback
|| curr_lcla
== -EINVAL
) {
562 unsigned int flags
= 0;
564 if (curr_lcla
== -EINVAL
)
565 flags
|= LLI_TERM_INT
;
567 d40_log_lli_lcpa_write(chan
->lcpa
,
568 &lli
->dst
[lli_current
],
569 &lli
->src
[lli_current
],
578 for (; lli_current
< lli_len
; lli_current
++) {
579 unsigned int lcla_offset
= chan
->phy_chan
->num
* 1024 +
581 struct d40_log_lli
*lcla
= pool
->base
+ lcla_offset
;
582 unsigned int flags
= 0;
585 if (lli_current
+ 1 < lli_len
)
586 next_lcla
= d40_lcla_alloc_one(chan
, desc
);
588 next_lcla
= linkback
? first_lcla
: -EINVAL
;
590 if (cyclic
|| next_lcla
== -EINVAL
)
591 flags
|= LLI_TERM_INT
;
593 if (linkback
&& curr_lcla
== first_lcla
) {
594 /* First link goes in both LCPA and LCLA */
595 d40_log_lli_lcpa_write(chan
->lcpa
,
596 &lli
->dst
[lli_current
],
597 &lli
->src
[lli_current
],
602 * One unused LCLA in the cyclic case if the very first
605 d40_log_lli_lcla_write(lcla
,
606 &lli
->dst
[lli_current
],
607 &lli
->src
[lli_current
],
610 dma_sync_single_range_for_device(chan
->base
->dev
,
611 pool
->dma_addr
, lcla_offset
,
612 2 * sizeof(struct d40_log_lli
),
615 curr_lcla
= next_lcla
;
617 if (curr_lcla
== -EINVAL
|| curr_lcla
== first_lcla
) {
624 desc
->lli_current
= lli_current
;
627 static void d40_desc_load(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
629 if (chan_is_physical(d40c
)) {
630 d40_phy_lli_load(d40c
, d40d
);
631 d40d
->lli_current
= d40d
->lli_len
;
633 d40_log_lli_to_lcxa(d40c
, d40d
);
636 static struct d40_desc
*d40_first_active_get(struct d40_chan
*d40c
)
640 if (list_empty(&d40c
->active
))
643 d
= list_first_entry(&d40c
->active
,
649 /* remove desc from current queue and add it to the pending_queue */
650 static void d40_desc_queue(struct d40_chan
*d40c
, struct d40_desc
*desc
)
652 d40_desc_remove(desc
);
653 desc
->is_in_client_list
= false;
654 list_add_tail(&desc
->node
, &d40c
->pending_queue
);
657 static struct d40_desc
*d40_first_pending(struct d40_chan
*d40c
)
661 if (list_empty(&d40c
->pending_queue
))
664 d
= list_first_entry(&d40c
->pending_queue
,
670 static struct d40_desc
*d40_first_queued(struct d40_chan
*d40c
)
674 if (list_empty(&d40c
->queue
))
677 d
= list_first_entry(&d40c
->queue
,
683 static int d40_psize_2_burst_size(bool is_log
, int psize
)
686 if (psize
== STEDMA40_PSIZE_LOG_1
)
689 if (psize
== STEDMA40_PSIZE_PHY_1
)
697 * The dma only supports transmitting packages up to
698 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
699 * dma elements required to send the entire sg list
701 static int d40_size_2_dmalen(int size
, u32 data_width1
, u32 data_width2
)
704 u32 max_w
= max(data_width1
, data_width2
);
705 u32 min_w
= min(data_width1
, data_width2
);
706 u32 seg_max
= ALIGN(STEDMA40_MAX_SEG_SIZE
<< min_w
, 1 << max_w
);
708 if (seg_max
> STEDMA40_MAX_SEG_SIZE
)
709 seg_max
-= (1 << max_w
);
711 if (!IS_ALIGNED(size
, 1 << max_w
))
717 dmalen
= size
/ seg_max
;
718 if (dmalen
* seg_max
< size
)
724 static int d40_sg_2_dmalen(struct scatterlist
*sgl
, int sg_len
,
725 u32 data_width1
, u32 data_width2
)
727 struct scatterlist
*sg
;
732 for_each_sg(sgl
, sg
, sg_len
, i
) {
733 ret
= d40_size_2_dmalen(sg_dma_len(sg
),
734 data_width1
, data_width2
);
742 /* Support functions for logical channels */
744 static int d40_channel_execute_command(struct d40_chan
*d40c
,
745 enum d40_command command
)
749 void __iomem
*active_reg
;
754 spin_lock_irqsave(&d40c
->base
->execmd_lock
, flags
);
756 if (d40c
->phy_chan
->num
% 2 == 0)
757 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVE
;
759 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVO
;
761 if (command
== D40_DMA_SUSPEND_REQ
) {
762 status
= (readl(active_reg
) &
763 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
764 D40_CHAN_POS(d40c
->phy_chan
->num
);
766 if (status
== D40_DMA_SUSPENDED
|| status
== D40_DMA_STOP
)
770 wmask
= 0xffffffff & ~(D40_CHAN_POS_MASK(d40c
->phy_chan
->num
));
771 writel(wmask
| (command
<< D40_CHAN_POS(d40c
->phy_chan
->num
)),
774 if (command
== D40_DMA_SUSPEND_REQ
) {
776 for (i
= 0 ; i
< D40_SUSPEND_MAX_IT
; i
++) {
777 status
= (readl(active_reg
) &
778 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
779 D40_CHAN_POS(d40c
->phy_chan
->num
);
783 * Reduce the number of bus accesses while
784 * waiting for the DMA to suspend.
788 if (status
== D40_DMA_STOP
||
789 status
== D40_DMA_SUSPENDED
)
793 if (i
== D40_SUSPEND_MAX_IT
) {
795 "unable to suspend the chl %d (log: %d) status %x\n",
796 d40c
->phy_chan
->num
, d40c
->log_num
,
804 spin_unlock_irqrestore(&d40c
->base
->execmd_lock
, flags
);
808 static void d40_term_all(struct d40_chan
*d40c
)
810 struct d40_desc
*d40d
;
813 /* Release active descriptors */
814 while ((d40d
= d40_first_active_get(d40c
))) {
815 d40_desc_remove(d40d
);
816 d40_desc_free(d40c
, d40d
);
819 /* Release queued descriptors waiting for transfer */
820 while ((d40d
= d40_first_queued(d40c
))) {
821 d40_desc_remove(d40d
);
822 d40_desc_free(d40c
, d40d
);
825 /* Release pending descriptors */
826 while ((d40d
= d40_first_pending(d40c
))) {
827 d40_desc_remove(d40d
);
828 d40_desc_free(d40c
, d40d
);
831 /* Release client owned descriptors */
832 if (!list_empty(&d40c
->client
))
833 list_for_each_entry_safe(d40d
, _d
, &d40c
->client
, node
) {
834 d40_desc_remove(d40d
);
835 d40_desc_free(d40c
, d40d
);
838 /* Release descriptors in prepare queue */
839 if (!list_empty(&d40c
->prepare_queue
))
840 list_for_each_entry_safe(d40d
, _d
,
841 &d40c
->prepare_queue
, node
) {
842 d40_desc_remove(d40d
);
843 d40_desc_free(d40c
, d40d
);
846 d40c
->pending_tx
= 0;
850 static void __d40_config_set_event(struct d40_chan
*d40c
, bool enable
,
853 void __iomem
*addr
= chan_base(d40c
) + reg
;
857 writel((D40_DEACTIVATE_EVENTLINE
<< D40_EVENTLINE_POS(event
))
858 | ~D40_EVENTLINE_MASK(event
), addr
);
863 * The hardware sometimes doesn't register the enable when src and dst
864 * event lines are active on the same logical channel. Retry to ensure
865 * it does. Usually only one retry is sufficient.
869 writel((D40_ACTIVATE_EVENTLINE
<< D40_EVENTLINE_POS(event
))
870 | ~D40_EVENTLINE_MASK(event
), addr
);
872 if (readl(addr
) & D40_EVENTLINE_MASK(event
))
877 dev_dbg(chan2dev(d40c
),
878 "[%s] workaround enable S%cLNK (%d tries)\n",
879 __func__
, reg
== D40_CHAN_REG_SSLNK
? 'S' : 'D',
885 static void d40_config_set_event(struct d40_chan
*d40c
, bool do_enable
)
889 spin_lock_irqsave(&d40c
->phy_chan
->lock
, flags
);
891 /* Enable event line connected to device (or memcpy) */
892 if ((d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) ||
893 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
)) {
894 u32 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
896 __d40_config_set_event(d40c
, do_enable
, event
,
900 if (d40c
->dma_cfg
.dir
!= STEDMA40_PERIPH_TO_MEM
) {
901 u32 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
903 __d40_config_set_event(d40c
, do_enable
, event
,
907 spin_unlock_irqrestore(&d40c
->phy_chan
->lock
, flags
);
910 static u32
d40_chan_has_events(struct d40_chan
*d40c
)
912 void __iomem
*chanbase
= chan_base(d40c
);
915 val
= readl(chanbase
+ D40_CHAN_REG_SSLNK
);
916 val
|= readl(chanbase
+ D40_CHAN_REG_SDLNK
);
921 static u32
d40_get_prmo(struct d40_chan
*d40c
)
923 static const unsigned int phy_map
[] = {
924 [STEDMA40_PCHAN_BASIC_MODE
]
925 = D40_DREG_PRMO_PCHAN_BASIC
,
926 [STEDMA40_PCHAN_MODULO_MODE
]
927 = D40_DREG_PRMO_PCHAN_MODULO
,
928 [STEDMA40_PCHAN_DOUBLE_DST_MODE
]
929 = D40_DREG_PRMO_PCHAN_DOUBLE_DST
,
931 static const unsigned int log_map
[] = {
932 [STEDMA40_LCHAN_SRC_PHY_DST_LOG
]
933 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG
,
934 [STEDMA40_LCHAN_SRC_LOG_DST_PHY
]
935 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY
,
936 [STEDMA40_LCHAN_SRC_LOG_DST_LOG
]
937 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG
,
940 if (chan_is_physical(d40c
))
941 return phy_map
[d40c
->dma_cfg
.mode_opt
];
943 return log_map
[d40c
->dma_cfg
.mode_opt
];
946 static void d40_config_write(struct d40_chan
*d40c
)
951 /* Odd addresses are even addresses + 4 */
952 addr_base
= (d40c
->phy_chan
->num
% 2) * 4;
953 /* Setup channel mode to logical or physical */
954 var
= ((u32
)(chan_is_logical(d40c
)) + 1) <<
955 D40_CHAN_POS(d40c
->phy_chan
->num
);
956 writel(var
, d40c
->base
->virtbase
+ D40_DREG_PRMSE
+ addr_base
);
958 /* Setup operational mode option register */
959 var
= d40_get_prmo(d40c
) << D40_CHAN_POS(d40c
->phy_chan
->num
);
961 writel(var
, d40c
->base
->virtbase
+ D40_DREG_PRMOE
+ addr_base
);
963 if (chan_is_logical(d40c
)) {
964 int lidx
= (d40c
->phy_chan
->num
<< D40_SREG_ELEM_LOG_LIDX_POS
)
965 & D40_SREG_ELEM_LOG_LIDX_MASK
;
966 void __iomem
*chanbase
= chan_base(d40c
);
968 /* Set default config for CFG reg */
969 writel(d40c
->src_def_cfg
, chanbase
+ D40_CHAN_REG_SSCFG
);
970 writel(d40c
->dst_def_cfg
, chanbase
+ D40_CHAN_REG_SDCFG
);
972 /* Set LIDX for lcla */
973 writel(lidx
, chanbase
+ D40_CHAN_REG_SSELT
);
974 writel(lidx
, chanbase
+ D40_CHAN_REG_SDELT
);
978 static u32
d40_residue(struct d40_chan
*d40c
)
982 if (chan_is_logical(d40c
))
983 num_elt
= (readl(&d40c
->lcpa
->lcsp2
) & D40_MEM_LCSP2_ECNT_MASK
)
984 >> D40_MEM_LCSP2_ECNT_POS
;
986 u32 val
= readl(chan_base(d40c
) + D40_CHAN_REG_SDELT
);
987 num_elt
= (val
& D40_SREG_ELEM_PHY_ECNT_MASK
)
988 >> D40_SREG_ELEM_PHY_ECNT_POS
;
991 return num_elt
* (1 << d40c
->dma_cfg
.dst_info
.data_width
);
994 static bool d40_tx_is_linked(struct d40_chan
*d40c
)
998 if (chan_is_logical(d40c
))
999 is_link
= readl(&d40c
->lcpa
->lcsp3
) & D40_MEM_LCSP3_DLOS_MASK
;
1001 is_link
= readl(chan_base(d40c
) + D40_CHAN_REG_SDLNK
)
1002 & D40_SREG_LNK_PHYS_LNK_MASK
;
1007 static int d40_pause(struct d40_chan
*d40c
)
1010 unsigned long flags
;
1015 spin_lock_irqsave(&d40c
->lock
, flags
);
1017 res
= d40_channel_execute_command(d40c
, D40_DMA_SUSPEND_REQ
);
1019 if (chan_is_logical(d40c
)) {
1020 d40_config_set_event(d40c
, false);
1021 /* Resume the other logical channels if any */
1022 if (d40_chan_has_events(d40c
))
1023 res
= d40_channel_execute_command(d40c
,
1028 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1032 static int d40_resume(struct d40_chan
*d40c
)
1035 unsigned long flags
;
1040 spin_lock_irqsave(&d40c
->lock
, flags
);
1042 if (d40c
->base
->rev
== 0)
1043 if (chan_is_logical(d40c
)) {
1044 res
= d40_channel_execute_command(d40c
,
1045 D40_DMA_SUSPEND_REQ
);
1049 /* If bytes left to transfer or linked tx resume job */
1050 if (d40_residue(d40c
) || d40_tx_is_linked(d40c
)) {
1052 if (chan_is_logical(d40c
))
1053 d40_config_set_event(d40c
, true);
1055 res
= d40_channel_execute_command(d40c
, D40_DMA_RUN
);
1059 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1063 static int d40_terminate_all(struct d40_chan
*chan
)
1065 unsigned long flags
;
1068 ret
= d40_pause(chan
);
1069 if (!ret
&& chan_is_physical(chan
))
1070 ret
= d40_channel_execute_command(chan
, D40_DMA_STOP
);
1072 spin_lock_irqsave(&chan
->lock
, flags
);
1074 spin_unlock_irqrestore(&chan
->lock
, flags
);
1079 static dma_cookie_t
d40_tx_submit(struct dma_async_tx_descriptor
*tx
)
1081 struct d40_chan
*d40c
= container_of(tx
->chan
,
1084 struct d40_desc
*d40d
= container_of(tx
, struct d40_desc
, txd
);
1085 unsigned long flags
;
1087 spin_lock_irqsave(&d40c
->lock
, flags
);
1089 d40c
->chan
.cookie
++;
1091 if (d40c
->chan
.cookie
< 0)
1092 d40c
->chan
.cookie
= 1;
1094 d40d
->txd
.cookie
= d40c
->chan
.cookie
;
1096 d40_desc_queue(d40c
, d40d
);
1098 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1103 static int d40_start(struct d40_chan
*d40c
)
1105 if (d40c
->base
->rev
== 0) {
1108 if (chan_is_logical(d40c
)) {
1109 err
= d40_channel_execute_command(d40c
,
1110 D40_DMA_SUSPEND_REQ
);
1116 if (chan_is_logical(d40c
))
1117 d40_config_set_event(d40c
, true);
1119 return d40_channel_execute_command(d40c
, D40_DMA_RUN
);
1122 static struct d40_desc
*d40_queue_start(struct d40_chan
*d40c
)
1124 struct d40_desc
*d40d
;
1127 /* Start queued jobs, if any */
1128 d40d
= d40_first_queued(d40c
);
1133 /* Remove from queue */
1134 d40_desc_remove(d40d
);
1136 /* Add to active queue */
1137 d40_desc_submit(d40c
, d40d
);
1139 /* Initiate DMA job */
1140 d40_desc_load(d40c
, d40d
);
1143 err
= d40_start(d40c
);
1152 /* called from interrupt context */
1153 static void dma_tc_handle(struct d40_chan
*d40c
)
1155 struct d40_desc
*d40d
;
1157 /* Get first active entry from list */
1158 d40d
= d40_first_active_get(d40c
);
1165 * If this was a paritially loaded list, we need to reloaded
1166 * it, and only when the list is completed. We need to check
1167 * for done because the interrupt will hit for every link, and
1168 * not just the last one.
1170 if (d40d
->lli_current
< d40d
->lli_len
1171 && !d40_tx_is_linked(d40c
)
1172 && !d40_residue(d40c
)) {
1173 d40_lcla_free_all(d40c
, d40d
);
1174 d40_desc_load(d40c
, d40d
);
1175 (void) d40_start(d40c
);
1177 if (d40d
->lli_current
== d40d
->lli_len
)
1178 d40d
->lli_current
= 0;
1181 d40_lcla_free_all(d40c
, d40d
);
1183 if (d40d
->lli_current
< d40d
->lli_len
) {
1184 d40_desc_load(d40c
, d40d
);
1186 (void) d40_start(d40c
);
1190 if (d40_queue_start(d40c
) == NULL
)
1195 tasklet_schedule(&d40c
->tasklet
);
1199 static void dma_tasklet(unsigned long data
)
1201 struct d40_chan
*d40c
= (struct d40_chan
*) data
;
1202 struct d40_desc
*d40d
;
1203 unsigned long flags
;
1204 dma_async_tx_callback callback
;
1205 void *callback_param
;
1207 spin_lock_irqsave(&d40c
->lock
, flags
);
1209 /* Get first active entry from list */
1210 d40d
= d40_first_active_get(d40c
);
1215 d40c
->completed
= d40d
->txd
.cookie
;
1218 * If terminating a channel pending_tx is set to zero.
1219 * This prevents any finished active jobs to return to the client.
1221 if (d40c
->pending_tx
== 0) {
1222 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1226 /* Callback to client */
1227 callback
= d40d
->txd
.callback
;
1228 callback_param
= d40d
->txd
.callback_param
;
1230 if (!d40d
->cyclic
) {
1231 if (async_tx_test_ack(&d40d
->txd
)) {
1232 d40_desc_remove(d40d
);
1233 d40_desc_free(d40c
, d40d
);
1235 if (!d40d
->is_in_client_list
) {
1236 d40_desc_remove(d40d
);
1237 d40_lcla_free_all(d40c
, d40d
);
1238 list_add_tail(&d40d
->node
, &d40c
->client
);
1239 d40d
->is_in_client_list
= true;
1246 if (d40c
->pending_tx
)
1247 tasklet_schedule(&d40c
->tasklet
);
1249 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1251 if (callback
&& (d40d
->txd
.flags
& DMA_PREP_INTERRUPT
))
1252 callback(callback_param
);
1257 /* Rescue manoeuvre if receiving double interrupts */
1258 if (d40c
->pending_tx
> 0)
1260 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1263 static irqreturn_t
d40_handle_interrupt(int irq
, void *data
)
1265 static const struct d40_interrupt_lookup il
[] = {
1266 {D40_DREG_LCTIS0
, D40_DREG_LCICR0
, false, 0},
1267 {D40_DREG_LCTIS1
, D40_DREG_LCICR1
, false, 32},
1268 {D40_DREG_LCTIS2
, D40_DREG_LCICR2
, false, 64},
1269 {D40_DREG_LCTIS3
, D40_DREG_LCICR3
, false, 96},
1270 {D40_DREG_LCEIS0
, D40_DREG_LCICR0
, true, 0},
1271 {D40_DREG_LCEIS1
, D40_DREG_LCICR1
, true, 32},
1272 {D40_DREG_LCEIS2
, D40_DREG_LCICR2
, true, 64},
1273 {D40_DREG_LCEIS3
, D40_DREG_LCICR3
, true, 96},
1274 {D40_DREG_PCTIS
, D40_DREG_PCICR
, false, D40_PHY_CHAN
},
1275 {D40_DREG_PCEIS
, D40_DREG_PCICR
, true, D40_PHY_CHAN
},
1279 u32 regs
[ARRAY_SIZE(il
)];
1283 struct d40_chan
*d40c
;
1284 unsigned long flags
;
1285 struct d40_base
*base
= data
;
1287 spin_lock_irqsave(&base
->interrupt_lock
, flags
);
1289 /* Read interrupt status of both logical and physical channels */
1290 for (i
= 0; i
< ARRAY_SIZE(il
); i
++)
1291 regs
[i
] = readl(base
->virtbase
+ il
[i
].src
);
1295 chan
= find_next_bit((unsigned long *)regs
,
1296 BITS_PER_LONG
* ARRAY_SIZE(il
), chan
+ 1);
1298 /* No more set bits found? */
1299 if (chan
== BITS_PER_LONG
* ARRAY_SIZE(il
))
1302 row
= chan
/ BITS_PER_LONG
;
1303 idx
= chan
& (BITS_PER_LONG
- 1);
1306 writel(1 << idx
, base
->virtbase
+ il
[row
].clr
);
1308 if (il
[row
].offset
== D40_PHY_CHAN
)
1309 d40c
= base
->lookup_phy_chans
[idx
];
1311 d40c
= base
->lookup_log_chans
[il
[row
].offset
+ idx
];
1312 spin_lock(&d40c
->lock
);
1314 if (!il
[row
].is_error
)
1315 dma_tc_handle(d40c
);
1317 d40_err(base
->dev
, "IRQ chan: %ld offset %d idx %d\n",
1318 chan
, il
[row
].offset
, idx
);
1320 spin_unlock(&d40c
->lock
);
1323 spin_unlock_irqrestore(&base
->interrupt_lock
, flags
);
1328 static int d40_validate_conf(struct d40_chan
*d40c
,
1329 struct stedma40_chan_cfg
*conf
)
1332 u32 dst_event_group
= D40_TYPE_TO_GROUP(conf
->dst_dev_type
);
1333 u32 src_event_group
= D40_TYPE_TO_GROUP(conf
->src_dev_type
);
1334 bool is_log
= conf
->mode
== STEDMA40_MODE_LOGICAL
;
1337 chan_err(d40c
, "Invalid direction.\n");
1341 if (conf
->dst_dev_type
!= STEDMA40_DEV_DST_MEMORY
&&
1342 d40c
->base
->plat_data
->dev_tx
[conf
->dst_dev_type
] == 0 &&
1343 d40c
->runtime_addr
== 0) {
1345 chan_err(d40c
, "Invalid TX channel address (%d)\n",
1346 conf
->dst_dev_type
);
1350 if (conf
->src_dev_type
!= STEDMA40_DEV_SRC_MEMORY
&&
1351 d40c
->base
->plat_data
->dev_rx
[conf
->src_dev_type
] == 0 &&
1352 d40c
->runtime_addr
== 0) {
1353 chan_err(d40c
, "Invalid RX channel address (%d)\n",
1354 conf
->src_dev_type
);
1358 if (conf
->dir
== STEDMA40_MEM_TO_PERIPH
&&
1359 dst_event_group
== STEDMA40_DEV_DST_MEMORY
) {
1360 chan_err(d40c
, "Invalid dst\n");
1364 if (conf
->dir
== STEDMA40_PERIPH_TO_MEM
&&
1365 src_event_group
== STEDMA40_DEV_SRC_MEMORY
) {
1366 chan_err(d40c
, "Invalid src\n");
1370 if (src_event_group
== STEDMA40_DEV_SRC_MEMORY
&&
1371 dst_event_group
== STEDMA40_DEV_DST_MEMORY
&& is_log
) {
1372 chan_err(d40c
, "No event line\n");
1376 if (conf
->dir
== STEDMA40_PERIPH_TO_PERIPH
&&
1377 (src_event_group
!= dst_event_group
)) {
1378 chan_err(d40c
, "Invalid event group\n");
1382 if (conf
->dir
== STEDMA40_PERIPH_TO_PERIPH
) {
1384 * DMAC HW supports it. Will be added to this driver,
1385 * in case any dma client requires it.
1387 chan_err(d40c
, "periph to periph not supported\n");
1391 if (d40_psize_2_burst_size(is_log
, conf
->src_info
.psize
) *
1392 (1 << conf
->src_info
.data_width
) !=
1393 d40_psize_2_burst_size(is_log
, conf
->dst_info
.psize
) *
1394 (1 << conf
->dst_info
.data_width
)) {
1396 * The DMAC hardware only supports
1397 * src (burst x width) == dst (burst x width)
1400 chan_err(d40c
, "src (burst x width) != dst (burst x width)\n");
1407 static bool d40_alloc_mask_set(struct d40_phy_res
*phy
, bool is_src
,
1408 int log_event_line
, bool is_log
)
1410 unsigned long flags
;
1411 spin_lock_irqsave(&phy
->lock
, flags
);
1413 /* Physical interrupts are masked per physical full channel */
1414 if (phy
->allocated_src
== D40_ALLOC_FREE
&&
1415 phy
->allocated_dst
== D40_ALLOC_FREE
) {
1416 phy
->allocated_dst
= D40_ALLOC_PHY
;
1417 phy
->allocated_src
= D40_ALLOC_PHY
;
1423 /* Logical channel */
1425 if (phy
->allocated_src
== D40_ALLOC_PHY
)
1428 if (phy
->allocated_src
== D40_ALLOC_FREE
)
1429 phy
->allocated_src
= D40_ALLOC_LOG_FREE
;
1431 if (!(phy
->allocated_src
& (1 << log_event_line
))) {
1432 phy
->allocated_src
|= 1 << log_event_line
;
1437 if (phy
->allocated_dst
== D40_ALLOC_PHY
)
1440 if (phy
->allocated_dst
== D40_ALLOC_FREE
)
1441 phy
->allocated_dst
= D40_ALLOC_LOG_FREE
;
1443 if (!(phy
->allocated_dst
& (1 << log_event_line
))) {
1444 phy
->allocated_dst
|= 1 << log_event_line
;
1451 spin_unlock_irqrestore(&phy
->lock
, flags
);
1454 spin_unlock_irqrestore(&phy
->lock
, flags
);
1458 static bool d40_alloc_mask_free(struct d40_phy_res
*phy
, bool is_src
,
1461 unsigned long flags
;
1462 bool is_free
= false;
1464 spin_lock_irqsave(&phy
->lock
, flags
);
1465 if (!log_event_line
) {
1466 phy
->allocated_dst
= D40_ALLOC_FREE
;
1467 phy
->allocated_src
= D40_ALLOC_FREE
;
1472 /* Logical channel */
1474 phy
->allocated_src
&= ~(1 << log_event_line
);
1475 if (phy
->allocated_src
== D40_ALLOC_LOG_FREE
)
1476 phy
->allocated_src
= D40_ALLOC_FREE
;
1478 phy
->allocated_dst
&= ~(1 << log_event_line
);
1479 if (phy
->allocated_dst
== D40_ALLOC_LOG_FREE
)
1480 phy
->allocated_dst
= D40_ALLOC_FREE
;
1483 is_free
= ((phy
->allocated_src
| phy
->allocated_dst
) ==
1487 spin_unlock_irqrestore(&phy
->lock
, flags
);
1492 static int d40_allocate_channel(struct d40_chan
*d40c
)
1497 struct d40_phy_res
*phys
;
1502 bool is_log
= d40c
->dma_cfg
.mode
== STEDMA40_MODE_LOGICAL
;
1504 phys
= d40c
->base
->phy_res
;
1506 if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1507 dev_type
= d40c
->dma_cfg
.src_dev_type
;
1508 log_num
= 2 * dev_type
;
1510 } else if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1511 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1512 /* dst event lines are used for logical memcpy */
1513 dev_type
= d40c
->dma_cfg
.dst_dev_type
;
1514 log_num
= 2 * dev_type
+ 1;
1519 event_group
= D40_TYPE_TO_GROUP(dev_type
);
1520 event_line
= D40_TYPE_TO_EVENT(dev_type
);
1523 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1524 /* Find physical half channel */
1525 for (i
= 0; i
< d40c
->base
->num_phy_chans
; i
++) {
1527 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1532 for (j
= 0; j
< d40c
->base
->num_phy_chans
; j
+= 8) {
1533 int phy_num
= j
+ event_group
* 2;
1534 for (i
= phy_num
; i
< phy_num
+ 2; i
++) {
1535 if (d40_alloc_mask_set(&phys
[i
],
1544 d40c
->phy_chan
= &phys
[i
];
1545 d40c
->log_num
= D40_PHY_CHAN
;
1551 /* Find logical channel */
1552 for (j
= 0; j
< d40c
->base
->num_phy_chans
; j
+= 8) {
1553 int phy_num
= j
+ event_group
* 2;
1555 * Spread logical channels across all available physical rather
1556 * than pack every logical channel at the first available phy
1560 for (i
= phy_num
; i
< phy_num
+ 2; i
++) {
1561 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1562 event_line
, is_log
))
1566 for (i
= phy_num
+ 1; i
>= phy_num
; i
--) {
1567 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1568 event_line
, is_log
))
1576 d40c
->phy_chan
= &phys
[i
];
1577 d40c
->log_num
= log_num
;
1581 d40c
->base
->lookup_log_chans
[d40c
->log_num
] = d40c
;
1583 d40c
->base
->lookup_phy_chans
[d40c
->phy_chan
->num
] = d40c
;
1589 static int d40_config_memcpy(struct d40_chan
*d40c
)
1591 dma_cap_mask_t cap
= d40c
->chan
.device
->cap_mask
;
1593 if (dma_has_cap(DMA_MEMCPY
, cap
) && !dma_has_cap(DMA_SLAVE
, cap
)) {
1594 d40c
->dma_cfg
= *d40c
->base
->plat_data
->memcpy_conf_log
;
1595 d40c
->dma_cfg
.src_dev_type
= STEDMA40_DEV_SRC_MEMORY
;
1596 d40c
->dma_cfg
.dst_dev_type
= d40c
->base
->plat_data
->
1597 memcpy
[d40c
->chan
.chan_id
];
1599 } else if (dma_has_cap(DMA_MEMCPY
, cap
) &&
1600 dma_has_cap(DMA_SLAVE
, cap
)) {
1601 d40c
->dma_cfg
= *d40c
->base
->plat_data
->memcpy_conf_phy
;
1603 chan_err(d40c
, "No memcpy\n");
1611 static int d40_free_dma(struct d40_chan
*d40c
)
1616 struct d40_phy_res
*phy
= d40c
->phy_chan
;
1619 /* Terminate all queued and active transfers */
1623 chan_err(d40c
, "phy == null\n");
1627 if (phy
->allocated_src
== D40_ALLOC_FREE
&&
1628 phy
->allocated_dst
== D40_ALLOC_FREE
) {
1629 chan_err(d40c
, "channel already free\n");
1633 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1634 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1635 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
1637 } else if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1638 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
1641 chan_err(d40c
, "Unknown direction\n");
1645 res
= d40_channel_execute_command(d40c
, D40_DMA_SUSPEND_REQ
);
1647 chan_err(d40c
, "suspend failed\n");
1651 if (chan_is_logical(d40c
)) {
1652 /* Release logical channel, deactivate the event line */
1654 d40_config_set_event(d40c
, false);
1655 d40c
->base
->lookup_log_chans
[d40c
->log_num
] = NULL
;
1658 * Check if there are more logical allocation
1659 * on this phy channel.
1661 if (!d40_alloc_mask_free(phy
, is_src
, event
)) {
1662 /* Resume the other logical channels if any */
1663 if (d40_chan_has_events(d40c
)) {
1664 res
= d40_channel_execute_command(d40c
,
1668 "Executing RUN command\n");
1675 (void) d40_alloc_mask_free(phy
, is_src
, 0);
1678 /* Release physical channel */
1679 res
= d40_channel_execute_command(d40c
, D40_DMA_STOP
);
1681 chan_err(d40c
, "Failed to stop channel\n");
1684 d40c
->phy_chan
= NULL
;
1685 d40c
->configured
= false;
1686 d40c
->base
->lookup_phy_chans
[phy
->num
] = NULL
;
1691 static bool d40_is_paused(struct d40_chan
*d40c
)
1693 void __iomem
*chanbase
= chan_base(d40c
);
1694 bool is_paused
= false;
1695 unsigned long flags
;
1696 void __iomem
*active_reg
;
1700 spin_lock_irqsave(&d40c
->lock
, flags
);
1702 if (chan_is_physical(d40c
)) {
1703 if (d40c
->phy_chan
->num
% 2 == 0)
1704 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVE
;
1706 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVO
;
1708 status
= (readl(active_reg
) &
1709 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
1710 D40_CHAN_POS(d40c
->phy_chan
->num
);
1711 if (status
== D40_DMA_SUSPENDED
|| status
== D40_DMA_STOP
)
1717 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1718 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1719 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
1720 status
= readl(chanbase
+ D40_CHAN_REG_SDLNK
);
1721 } else if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1722 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
1723 status
= readl(chanbase
+ D40_CHAN_REG_SSLNK
);
1725 chan_err(d40c
, "Unknown direction\n");
1729 status
= (status
& D40_EVENTLINE_MASK(event
)) >>
1730 D40_EVENTLINE_POS(event
);
1732 if (status
!= D40_DMA_RUN
)
1735 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1741 static u32
stedma40_residue(struct dma_chan
*chan
)
1743 struct d40_chan
*d40c
=
1744 container_of(chan
, struct d40_chan
, chan
);
1746 unsigned long flags
;
1748 spin_lock_irqsave(&d40c
->lock
, flags
);
1749 bytes_left
= d40_residue(d40c
);
1750 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1756 d40_prep_sg_log(struct d40_chan
*chan
, struct d40_desc
*desc
,
1757 struct scatterlist
*sg_src
, struct scatterlist
*sg_dst
,
1758 unsigned int sg_len
, dma_addr_t src_dev_addr
,
1759 dma_addr_t dst_dev_addr
)
1761 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1762 struct stedma40_half_channel_info
*src_info
= &cfg
->src_info
;
1763 struct stedma40_half_channel_info
*dst_info
= &cfg
->dst_info
;
1766 ret
= d40_log_sg_to_lli(sg_src
, sg_len
,
1769 chan
->log_def
.lcsp1
,
1770 src_info
->data_width
,
1771 dst_info
->data_width
);
1773 ret
= d40_log_sg_to_lli(sg_dst
, sg_len
,
1776 chan
->log_def
.lcsp3
,
1777 dst_info
->data_width
,
1778 src_info
->data_width
);
1780 return ret
< 0 ? ret
: 0;
1784 d40_prep_sg_phy(struct d40_chan
*chan
, struct d40_desc
*desc
,
1785 struct scatterlist
*sg_src
, struct scatterlist
*sg_dst
,
1786 unsigned int sg_len
, dma_addr_t src_dev_addr
,
1787 dma_addr_t dst_dev_addr
)
1789 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1790 struct stedma40_half_channel_info
*src_info
= &cfg
->src_info
;
1791 struct stedma40_half_channel_info
*dst_info
= &cfg
->dst_info
;
1792 unsigned long flags
= 0;
1796 flags
|= LLI_CYCLIC
| LLI_TERM_INT
;
1798 ret
= d40_phy_sg_to_lli(sg_src
, sg_len
, src_dev_addr
,
1800 virt_to_phys(desc
->lli_phy
.src
),
1802 src_info
, dst_info
, flags
);
1804 ret
= d40_phy_sg_to_lli(sg_dst
, sg_len
, dst_dev_addr
,
1806 virt_to_phys(desc
->lli_phy
.dst
),
1808 dst_info
, src_info
, flags
);
1810 dma_sync_single_for_device(chan
->base
->dev
, desc
->lli_pool
.dma_addr
,
1811 desc
->lli_pool
.size
, DMA_TO_DEVICE
);
1813 return ret
< 0 ? ret
: 0;
1817 static struct d40_desc
*
1818 d40_prep_desc(struct d40_chan
*chan
, struct scatterlist
*sg
,
1819 unsigned int sg_len
, unsigned long dma_flags
)
1821 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1822 struct d40_desc
*desc
;
1825 desc
= d40_desc_get(chan
);
1829 desc
->lli_len
= d40_sg_2_dmalen(sg
, sg_len
, cfg
->src_info
.data_width
,
1830 cfg
->dst_info
.data_width
);
1831 if (desc
->lli_len
< 0) {
1832 chan_err(chan
, "Unaligned size\n");
1836 ret
= d40_pool_lli_alloc(chan
, desc
, desc
->lli_len
);
1838 chan_err(chan
, "Could not allocate lli\n");
1843 desc
->lli_current
= 0;
1844 desc
->txd
.flags
= dma_flags
;
1845 desc
->txd
.tx_submit
= d40_tx_submit
;
1847 dma_async_tx_descriptor_init(&desc
->txd
, &chan
->chan
);
1852 d40_desc_free(chan
, desc
);
1857 d40_get_dev_addr(struct d40_chan
*chan
, enum dma_data_direction direction
)
1859 struct stedma40_platform_data
*plat
= chan
->base
->plat_data
;
1860 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1861 dma_addr_t addr
= 0;
1863 if (chan
->runtime_addr
)
1864 return chan
->runtime_addr
;
1866 if (direction
== DMA_FROM_DEVICE
)
1867 addr
= plat
->dev_rx
[cfg
->src_dev_type
];
1868 else if (direction
== DMA_TO_DEVICE
)
1869 addr
= plat
->dev_tx
[cfg
->dst_dev_type
];
1874 static struct dma_async_tx_descriptor
*
1875 d40_prep_sg(struct dma_chan
*dchan
, struct scatterlist
*sg_src
,
1876 struct scatterlist
*sg_dst
, unsigned int sg_len
,
1877 enum dma_data_direction direction
, unsigned long dma_flags
)
1879 struct d40_chan
*chan
= container_of(dchan
, struct d40_chan
, chan
);
1880 dma_addr_t src_dev_addr
= 0;
1881 dma_addr_t dst_dev_addr
= 0;
1882 struct d40_desc
*desc
;
1883 unsigned long flags
;
1886 if (!chan
->phy_chan
) {
1887 chan_err(chan
, "Cannot prepare unallocated channel\n");
1892 spin_lock_irqsave(&chan
->lock
, flags
);
1894 desc
= d40_prep_desc(chan
, sg_src
, sg_len
, dma_flags
);
1898 if (sg_next(&sg_src
[sg_len
- 1]) == sg_src
)
1899 desc
->cyclic
= true;
1901 if (direction
!= DMA_NONE
) {
1902 dma_addr_t dev_addr
= d40_get_dev_addr(chan
, direction
);
1904 if (direction
== DMA_FROM_DEVICE
)
1905 src_dev_addr
= dev_addr
;
1906 else if (direction
== DMA_TO_DEVICE
)
1907 dst_dev_addr
= dev_addr
;
1910 if (chan_is_logical(chan
))
1911 ret
= d40_prep_sg_log(chan
, desc
, sg_src
, sg_dst
,
1912 sg_len
, src_dev_addr
, dst_dev_addr
);
1914 ret
= d40_prep_sg_phy(chan
, desc
, sg_src
, sg_dst
,
1915 sg_len
, src_dev_addr
, dst_dev_addr
);
1918 chan_err(chan
, "Failed to prepare %s sg job: %d\n",
1919 chan_is_logical(chan
) ? "log" : "phy", ret
);
1924 * add descriptor to the prepare queue in order to be able
1925 * to free them later in terminate_all
1927 list_add_tail(&desc
->node
, &chan
->prepare_queue
);
1929 spin_unlock_irqrestore(&chan
->lock
, flags
);
1935 d40_desc_free(chan
, desc
);
1936 spin_unlock_irqrestore(&chan
->lock
, flags
);
1940 bool stedma40_filter(struct dma_chan
*chan
, void *data
)
1942 struct stedma40_chan_cfg
*info
= data
;
1943 struct d40_chan
*d40c
=
1944 container_of(chan
, struct d40_chan
, chan
);
1948 err
= d40_validate_conf(d40c
, info
);
1950 d40c
->dma_cfg
= *info
;
1952 err
= d40_config_memcpy(d40c
);
1955 d40c
->configured
= true;
1959 EXPORT_SYMBOL(stedma40_filter
);
1961 static void __d40_set_prio_rt(struct d40_chan
*d40c
, int dev_type
, bool src
)
1963 bool realtime
= d40c
->dma_cfg
.realtime
;
1964 bool highprio
= d40c
->dma_cfg
.high_priority
;
1965 u32 prioreg
= highprio
? D40_DREG_PSEG1
: D40_DREG_PCEG1
;
1966 u32 rtreg
= realtime
? D40_DREG_RSEG1
: D40_DREG_RCEG1
;
1967 u32 event
= D40_TYPE_TO_EVENT(dev_type
);
1968 u32 group
= D40_TYPE_TO_GROUP(dev_type
);
1969 u32 bit
= 1 << event
;
1971 /* Destination event lines are stored in the upper halfword */
1975 writel(bit
, d40c
->base
->virtbase
+ prioreg
+ group
* 4);
1976 writel(bit
, d40c
->base
->virtbase
+ rtreg
+ group
* 4);
1979 static void d40_set_prio_realtime(struct d40_chan
*d40c
)
1981 if (d40c
->base
->rev
< 3)
1984 if ((d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) ||
1985 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
))
1986 __d40_set_prio_rt(d40c
, d40c
->dma_cfg
.src_dev_type
, true);
1988 if ((d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
) ||
1989 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
))
1990 __d40_set_prio_rt(d40c
, d40c
->dma_cfg
.dst_dev_type
, false);
1993 /* DMA ENGINE functions */
1994 static int d40_alloc_chan_resources(struct dma_chan
*chan
)
1997 unsigned long flags
;
1998 struct d40_chan
*d40c
=
1999 container_of(chan
, struct d40_chan
, chan
);
2001 spin_lock_irqsave(&d40c
->lock
, flags
);
2003 d40c
->completed
= chan
->cookie
= 1;
2005 /* If no dma configuration is set use default configuration (memcpy) */
2006 if (!d40c
->configured
) {
2007 err
= d40_config_memcpy(d40c
);
2009 chan_err(d40c
, "Failed to configure memcpy channel\n");
2013 is_free_phy
= (d40c
->phy_chan
== NULL
);
2015 err
= d40_allocate_channel(d40c
);
2017 chan_err(d40c
, "Failed to allocate channel\n");
2021 /* Fill in basic CFG register values */
2022 d40_phy_cfg(&d40c
->dma_cfg
, &d40c
->src_def_cfg
,
2023 &d40c
->dst_def_cfg
, chan_is_logical(d40c
));
2025 d40_set_prio_realtime(d40c
);
2027 if (chan_is_logical(d40c
)) {
2028 d40_log_cfg(&d40c
->dma_cfg
,
2029 &d40c
->log_def
.lcsp1
, &d40c
->log_def
.lcsp3
);
2031 if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
)
2032 d40c
->lcpa
= d40c
->base
->lcpa_base
+
2033 d40c
->dma_cfg
.src_dev_type
* D40_LCPA_CHAN_SIZE
;
2035 d40c
->lcpa
= d40c
->base
->lcpa_base
+
2036 d40c
->dma_cfg
.dst_dev_type
*
2037 D40_LCPA_CHAN_SIZE
+ D40_LCPA_CHAN_DST_DELTA
;
2041 * Only write channel configuration to the DMA if the physical
2042 * resource is free. In case of multiple logical channels
2043 * on the same physical resource, only the first write is necessary.
2046 d40_config_write(d40c
);
2048 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2052 static void d40_free_chan_resources(struct dma_chan
*chan
)
2054 struct d40_chan
*d40c
=
2055 container_of(chan
, struct d40_chan
, chan
);
2057 unsigned long flags
;
2059 if (d40c
->phy_chan
== NULL
) {
2060 chan_err(d40c
, "Cannot free unallocated channel\n");
2065 spin_lock_irqsave(&d40c
->lock
, flags
);
2067 err
= d40_free_dma(d40c
);
2070 chan_err(d40c
, "Failed to free channel\n");
2071 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2074 static struct dma_async_tx_descriptor
*d40_prep_memcpy(struct dma_chan
*chan
,
2078 unsigned long dma_flags
)
2080 struct scatterlist dst_sg
;
2081 struct scatterlist src_sg
;
2083 sg_init_table(&dst_sg
, 1);
2084 sg_init_table(&src_sg
, 1);
2086 sg_dma_address(&dst_sg
) = dst
;
2087 sg_dma_address(&src_sg
) = src
;
2089 sg_dma_len(&dst_sg
) = size
;
2090 sg_dma_len(&src_sg
) = size
;
2092 return d40_prep_sg(chan
, &src_sg
, &dst_sg
, 1, DMA_NONE
, dma_flags
);
2095 static struct dma_async_tx_descriptor
*
2096 d40_prep_memcpy_sg(struct dma_chan
*chan
,
2097 struct scatterlist
*dst_sg
, unsigned int dst_nents
,
2098 struct scatterlist
*src_sg
, unsigned int src_nents
,
2099 unsigned long dma_flags
)
2101 if (dst_nents
!= src_nents
)
2104 return d40_prep_sg(chan
, src_sg
, dst_sg
, src_nents
, DMA_NONE
, dma_flags
);
2107 static struct dma_async_tx_descriptor
*d40_prep_slave_sg(struct dma_chan
*chan
,
2108 struct scatterlist
*sgl
,
2109 unsigned int sg_len
,
2110 enum dma_data_direction direction
,
2111 unsigned long dma_flags
)
2113 if (direction
!= DMA_FROM_DEVICE
&& direction
!= DMA_TO_DEVICE
)
2116 return d40_prep_sg(chan
, sgl
, sgl
, sg_len
, direction
, dma_flags
);
2119 static struct dma_async_tx_descriptor
*
2120 dma40_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t dma_addr
,
2121 size_t buf_len
, size_t period_len
,
2122 enum dma_data_direction direction
)
2124 unsigned int periods
= buf_len
/ period_len
;
2125 struct dma_async_tx_descriptor
*txd
;
2126 struct scatterlist
*sg
;
2129 sg
= kcalloc(periods
+ 1, sizeof(struct scatterlist
), GFP_NOWAIT
);
2130 for (i
= 0; i
< periods
; i
++) {
2131 sg_dma_address(&sg
[i
]) = dma_addr
;
2132 sg_dma_len(&sg
[i
]) = period_len
;
2133 dma_addr
+= period_len
;
2136 sg
[periods
].offset
= 0;
2137 sg
[periods
].length
= 0;
2138 sg
[periods
].page_link
=
2139 ((unsigned long)sg
| 0x01) & ~0x02;
2141 txd
= d40_prep_sg(chan
, sg
, sg
, periods
, direction
,
2142 DMA_PREP_INTERRUPT
);
2149 static enum dma_status
d40_tx_status(struct dma_chan
*chan
,
2150 dma_cookie_t cookie
,
2151 struct dma_tx_state
*txstate
)
2153 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2154 dma_cookie_t last_used
;
2155 dma_cookie_t last_complete
;
2158 if (d40c
->phy_chan
== NULL
) {
2159 chan_err(d40c
, "Cannot read status of unallocated channel\n");
2163 last_complete
= d40c
->completed
;
2164 last_used
= chan
->cookie
;
2166 if (d40_is_paused(d40c
))
2169 ret
= dma_async_is_complete(cookie
, last_complete
, last_used
);
2171 dma_set_tx_state(txstate
, last_complete
, last_used
,
2172 stedma40_residue(chan
));
2177 static void d40_issue_pending(struct dma_chan
*chan
)
2179 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2180 unsigned long flags
;
2182 if (d40c
->phy_chan
== NULL
) {
2183 chan_err(d40c
, "Channel is not allocated!\n");
2187 spin_lock_irqsave(&d40c
->lock
, flags
);
2189 list_splice_tail_init(&d40c
->pending_queue
, &d40c
->queue
);
2191 /* Busy means that queued jobs are already being processed */
2193 (void) d40_queue_start(d40c
);
2195 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2199 dma40_config_to_halfchannel(struct d40_chan
*d40c
,
2200 struct stedma40_half_channel_info
*info
,
2201 enum dma_slave_buswidth width
,
2204 enum stedma40_periph_data_width addr_width
;
2208 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
2209 addr_width
= STEDMA40_BYTE_WIDTH
;
2211 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
2212 addr_width
= STEDMA40_HALFWORD_WIDTH
;
2214 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
2215 addr_width
= STEDMA40_WORD_WIDTH
;
2217 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
2218 addr_width
= STEDMA40_DOUBLEWORD_WIDTH
;
2221 dev_err(d40c
->base
->dev
,
2222 "illegal peripheral address width "
2228 if (chan_is_logical(d40c
)) {
2230 psize
= STEDMA40_PSIZE_LOG_16
;
2231 else if (maxburst
>= 8)
2232 psize
= STEDMA40_PSIZE_LOG_8
;
2233 else if (maxburst
>= 4)
2234 psize
= STEDMA40_PSIZE_LOG_4
;
2236 psize
= STEDMA40_PSIZE_LOG_1
;
2239 psize
= STEDMA40_PSIZE_PHY_16
;
2240 else if (maxburst
>= 8)
2241 psize
= STEDMA40_PSIZE_PHY_8
;
2242 else if (maxburst
>= 4)
2243 psize
= STEDMA40_PSIZE_PHY_4
;
2245 psize
= STEDMA40_PSIZE_PHY_1
;
2248 info
->data_width
= addr_width
;
2249 info
->psize
= psize
;
2250 info
->flow_ctrl
= STEDMA40_NO_FLOW_CTRL
;
2255 /* Runtime reconfiguration extension */
2256 static int d40_set_runtime_config(struct dma_chan
*chan
,
2257 struct dma_slave_config
*config
)
2259 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2260 struct stedma40_chan_cfg
*cfg
= &d40c
->dma_cfg
;
2261 enum dma_slave_buswidth src_addr_width
, dst_addr_width
;
2262 dma_addr_t config_addr
;
2263 u32 src_maxburst
, dst_maxburst
;
2266 src_addr_width
= config
->src_addr_width
;
2267 src_maxburst
= config
->src_maxburst
;
2268 dst_addr_width
= config
->dst_addr_width
;
2269 dst_maxburst
= config
->dst_maxburst
;
2271 if (config
->direction
== DMA_FROM_DEVICE
) {
2272 dma_addr_t dev_addr_rx
=
2273 d40c
->base
->plat_data
->dev_rx
[cfg
->src_dev_type
];
2275 config_addr
= config
->src_addr
;
2277 dev_dbg(d40c
->base
->dev
,
2278 "channel has a pre-wired RX address %08x "
2279 "overriding with %08x\n",
2280 dev_addr_rx
, config_addr
);
2281 if (cfg
->dir
!= STEDMA40_PERIPH_TO_MEM
)
2282 dev_dbg(d40c
->base
->dev
,
2283 "channel was not configured for peripheral "
2284 "to memory transfer (%d) overriding\n",
2286 cfg
->dir
= STEDMA40_PERIPH_TO_MEM
;
2288 /* Configure the memory side */
2289 if (dst_addr_width
== DMA_SLAVE_BUSWIDTH_UNDEFINED
)
2290 dst_addr_width
= src_addr_width
;
2291 if (dst_maxburst
== 0)
2292 dst_maxburst
= src_maxburst
;
2294 } else if (config
->direction
== DMA_TO_DEVICE
) {
2295 dma_addr_t dev_addr_tx
=
2296 d40c
->base
->plat_data
->dev_tx
[cfg
->dst_dev_type
];
2298 config_addr
= config
->dst_addr
;
2300 dev_dbg(d40c
->base
->dev
,
2301 "channel has a pre-wired TX address %08x "
2302 "overriding with %08x\n",
2303 dev_addr_tx
, config_addr
);
2304 if (cfg
->dir
!= STEDMA40_MEM_TO_PERIPH
)
2305 dev_dbg(d40c
->base
->dev
,
2306 "channel was not configured for memory "
2307 "to peripheral transfer (%d) overriding\n",
2309 cfg
->dir
= STEDMA40_MEM_TO_PERIPH
;
2311 /* Configure the memory side */
2312 if (src_addr_width
== DMA_SLAVE_BUSWIDTH_UNDEFINED
)
2313 src_addr_width
= dst_addr_width
;
2314 if (src_maxburst
== 0)
2315 src_maxburst
= dst_maxburst
;
2317 dev_err(d40c
->base
->dev
,
2318 "unrecognized channel direction %d\n",
2323 if (src_maxburst
* src_addr_width
!= dst_maxburst
* dst_addr_width
) {
2324 dev_err(d40c
->base
->dev
,
2325 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2333 ret
= dma40_config_to_halfchannel(d40c
, &cfg
->src_info
,
2339 ret
= dma40_config_to_halfchannel(d40c
, &cfg
->dst_info
,
2345 /* Fill in register values */
2346 if (chan_is_logical(d40c
))
2347 d40_log_cfg(cfg
, &d40c
->log_def
.lcsp1
, &d40c
->log_def
.lcsp3
);
2349 d40_phy_cfg(cfg
, &d40c
->src_def_cfg
,
2350 &d40c
->dst_def_cfg
, false);
2352 /* These settings will take precedence later */
2353 d40c
->runtime_addr
= config_addr
;
2354 d40c
->runtime_direction
= config
->direction
;
2355 dev_dbg(d40c
->base
->dev
,
2356 "configured channel %s for %s, data width %d/%d, "
2357 "maxburst %d/%d elements, LE, no flow control\n",
2358 dma_chan_name(chan
),
2359 (config
->direction
== DMA_FROM_DEVICE
) ? "RX" : "TX",
2360 src_addr_width
, dst_addr_width
,
2361 src_maxburst
, dst_maxburst
);
2366 static int d40_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
2369 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2371 if (d40c
->phy_chan
== NULL
) {
2372 chan_err(d40c
, "Channel is not allocated!\n");
2377 case DMA_TERMINATE_ALL
:
2378 return d40_terminate_all(d40c
);
2380 return d40_pause(d40c
);
2382 return d40_resume(d40c
);
2383 case DMA_SLAVE_CONFIG
:
2384 return d40_set_runtime_config(chan
,
2385 (struct dma_slave_config
*) arg
);
2390 /* Other commands are unimplemented */
2394 /* Initialization functions */
2396 static void __init
d40_chan_init(struct d40_base
*base
, struct dma_device
*dma
,
2397 struct d40_chan
*chans
, int offset
,
2401 struct d40_chan
*d40c
;
2403 INIT_LIST_HEAD(&dma
->channels
);
2405 for (i
= offset
; i
< offset
+ num_chans
; i
++) {
2408 d40c
->chan
.device
= dma
;
2410 spin_lock_init(&d40c
->lock
);
2412 d40c
->log_num
= D40_PHY_CHAN
;
2414 INIT_LIST_HEAD(&d40c
->active
);
2415 INIT_LIST_HEAD(&d40c
->queue
);
2416 INIT_LIST_HEAD(&d40c
->pending_queue
);
2417 INIT_LIST_HEAD(&d40c
->client
);
2418 INIT_LIST_HEAD(&d40c
->prepare_queue
);
2420 tasklet_init(&d40c
->tasklet
, dma_tasklet
,
2421 (unsigned long) d40c
);
2423 list_add_tail(&d40c
->chan
.device_node
,
2428 static void d40_ops_init(struct d40_base
*base
, struct dma_device
*dev
)
2430 if (dma_has_cap(DMA_SLAVE
, dev
->cap_mask
))
2431 dev
->device_prep_slave_sg
= d40_prep_slave_sg
;
2433 if (dma_has_cap(DMA_MEMCPY
, dev
->cap_mask
)) {
2434 dev
->device_prep_dma_memcpy
= d40_prep_memcpy
;
2437 * This controller can only access address at even
2438 * 32bit boundaries, i.e. 2^2
2440 dev
->copy_align
= 2;
2443 if (dma_has_cap(DMA_SG
, dev
->cap_mask
))
2444 dev
->device_prep_dma_sg
= d40_prep_memcpy_sg
;
2446 if (dma_has_cap(DMA_CYCLIC
, dev
->cap_mask
))
2447 dev
->device_prep_dma_cyclic
= dma40_prep_dma_cyclic
;
2449 dev
->device_alloc_chan_resources
= d40_alloc_chan_resources
;
2450 dev
->device_free_chan_resources
= d40_free_chan_resources
;
2451 dev
->device_issue_pending
= d40_issue_pending
;
2452 dev
->device_tx_status
= d40_tx_status
;
2453 dev
->device_control
= d40_control
;
2454 dev
->dev
= base
->dev
;
2457 static int __init
d40_dmaengine_init(struct d40_base
*base
,
2458 int num_reserved_chans
)
2462 d40_chan_init(base
, &base
->dma_slave
, base
->log_chans
,
2463 0, base
->num_log_chans
);
2465 dma_cap_zero(base
->dma_slave
.cap_mask
);
2466 dma_cap_set(DMA_SLAVE
, base
->dma_slave
.cap_mask
);
2467 dma_cap_set(DMA_CYCLIC
, base
->dma_slave
.cap_mask
);
2469 d40_ops_init(base
, &base
->dma_slave
);
2471 err
= dma_async_device_register(&base
->dma_slave
);
2474 d40_err(base
->dev
, "Failed to register slave channels\n");
2478 d40_chan_init(base
, &base
->dma_memcpy
, base
->log_chans
,
2479 base
->num_log_chans
, base
->plat_data
->memcpy_len
);
2481 dma_cap_zero(base
->dma_memcpy
.cap_mask
);
2482 dma_cap_set(DMA_MEMCPY
, base
->dma_memcpy
.cap_mask
);
2483 dma_cap_set(DMA_SG
, base
->dma_memcpy
.cap_mask
);
2485 d40_ops_init(base
, &base
->dma_memcpy
);
2487 err
= dma_async_device_register(&base
->dma_memcpy
);
2491 "Failed to regsiter memcpy only channels\n");
2495 d40_chan_init(base
, &base
->dma_both
, base
->phy_chans
,
2496 0, num_reserved_chans
);
2498 dma_cap_zero(base
->dma_both
.cap_mask
);
2499 dma_cap_set(DMA_SLAVE
, base
->dma_both
.cap_mask
);
2500 dma_cap_set(DMA_MEMCPY
, base
->dma_both
.cap_mask
);
2501 dma_cap_set(DMA_SG
, base
->dma_both
.cap_mask
);
2502 dma_cap_set(DMA_CYCLIC
, base
->dma_slave
.cap_mask
);
2504 d40_ops_init(base
, &base
->dma_both
);
2505 err
= dma_async_device_register(&base
->dma_both
);
2509 "Failed to register logical and physical capable channels\n");
2514 dma_async_device_unregister(&base
->dma_memcpy
);
2516 dma_async_device_unregister(&base
->dma_slave
);
2521 /* Initialization functions. */
2523 static int __init
d40_phy_res_init(struct d40_base
*base
)
2526 int num_phy_chans_avail
= 0;
2528 int odd_even_bit
= -2;
2530 val
[0] = readl(base
->virtbase
+ D40_DREG_PRSME
);
2531 val
[1] = readl(base
->virtbase
+ D40_DREG_PRSMO
);
2533 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2534 base
->phy_res
[i
].num
= i
;
2535 odd_even_bit
+= 2 * ((i
% 2) == 0);
2536 if (((val
[i
% 2] >> odd_even_bit
) & 3) == 1) {
2537 /* Mark security only channels as occupied */
2538 base
->phy_res
[i
].allocated_src
= D40_ALLOC_PHY
;
2539 base
->phy_res
[i
].allocated_dst
= D40_ALLOC_PHY
;
2541 base
->phy_res
[i
].allocated_src
= D40_ALLOC_FREE
;
2542 base
->phy_res
[i
].allocated_dst
= D40_ALLOC_FREE
;
2543 num_phy_chans_avail
++;
2545 spin_lock_init(&base
->phy_res
[i
].lock
);
2548 /* Mark disabled channels as occupied */
2549 for (i
= 0; base
->plat_data
->disabled_channels
[i
] != -1; i
++) {
2550 int chan
= base
->plat_data
->disabled_channels
[i
];
2552 base
->phy_res
[chan
].allocated_src
= D40_ALLOC_PHY
;
2553 base
->phy_res
[chan
].allocated_dst
= D40_ALLOC_PHY
;
2554 num_phy_chans_avail
--;
2557 dev_info(base
->dev
, "%d of %d physical DMA channels available\n",
2558 num_phy_chans_avail
, base
->num_phy_chans
);
2560 /* Verify settings extended vs standard */
2561 val
[0] = readl(base
->virtbase
+ D40_DREG_PRTYP
);
2563 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2565 if (base
->phy_res
[i
].allocated_src
== D40_ALLOC_FREE
&&
2566 (val
[0] & 0x3) != 1)
2568 "[%s] INFO: channel %d is misconfigured (%d)\n",
2569 __func__
, i
, val
[0] & 0x3);
2571 val
[0] = val
[0] >> 2;
2574 return num_phy_chans_avail
;
2577 static struct d40_base
* __init
d40_hw_detect_init(struct platform_device
*pdev
)
2579 struct stedma40_platform_data
*plat_data
;
2580 struct clk
*clk
= NULL
;
2581 void __iomem
*virtbase
= NULL
;
2582 struct resource
*res
= NULL
;
2583 struct d40_base
*base
= NULL
;
2584 int num_log_chans
= 0;
2591 clk
= clk_get(&pdev
->dev
, NULL
);
2594 d40_err(&pdev
->dev
, "No matching clock found\n");
2600 /* Get IO for DMAC base address */
2601 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "base");
2605 if (request_mem_region(res
->start
, resource_size(res
),
2606 D40_NAME
" I/O base") == NULL
)
2609 virtbase
= ioremap(res
->start
, resource_size(res
));
2613 /* This is just a regular AMBA PrimeCell ID actually */
2614 for (pid
= 0, i
= 0; i
< 4; i
++)
2615 pid
|= (readl(virtbase
+ resource_size(res
) - 0x20 + 4 * i
)
2617 for (cid
= 0, i
= 0; i
< 4; i
++)
2618 cid
|= (readl(virtbase
+ resource_size(res
) - 0x10 + 4 * i
)
2621 if (cid
!= AMBA_CID
) {
2622 d40_err(&pdev
->dev
, "Unknown hardware! No PrimeCell ID\n");
2625 if (AMBA_MANF_BITS(pid
) != AMBA_VENDOR_ST
) {
2626 d40_err(&pdev
->dev
, "Unknown designer! Got %x wanted %x\n",
2627 AMBA_MANF_BITS(pid
),
2633 * DB8500ed has revision 0
2635 * DB8500v1 has revision 2
2636 * DB8500v2 has revision 3
2638 rev
= AMBA_REV_BITS(pid
);
2640 /* The number of physical channels on this HW */
2641 num_phy_chans
= 4 * (readl(virtbase
+ D40_DREG_ICFG
) & 0x7) + 4;
2643 dev_info(&pdev
->dev
, "hardware revision: %d @ 0x%x\n",
2646 plat_data
= pdev
->dev
.platform_data
;
2648 /* Count the number of logical channels in use */
2649 for (i
= 0; i
< plat_data
->dev_len
; i
++)
2650 if (plat_data
->dev_rx
[i
] != 0)
2653 for (i
= 0; i
< plat_data
->dev_len
; i
++)
2654 if (plat_data
->dev_tx
[i
] != 0)
2657 base
= kzalloc(ALIGN(sizeof(struct d40_base
), 4) +
2658 (num_phy_chans
+ num_log_chans
+ plat_data
->memcpy_len
) *
2659 sizeof(struct d40_chan
), GFP_KERNEL
);
2662 d40_err(&pdev
->dev
, "Out of memory\n");
2668 base
->num_phy_chans
= num_phy_chans
;
2669 base
->num_log_chans
= num_log_chans
;
2670 base
->phy_start
= res
->start
;
2671 base
->phy_size
= resource_size(res
);
2672 base
->virtbase
= virtbase
;
2673 base
->plat_data
= plat_data
;
2674 base
->dev
= &pdev
->dev
;
2675 base
->phy_chans
= ((void *)base
) + ALIGN(sizeof(struct d40_base
), 4);
2676 base
->log_chans
= &base
->phy_chans
[num_phy_chans
];
2678 base
->phy_res
= kzalloc(num_phy_chans
* sizeof(struct d40_phy_res
),
2683 base
->lookup_phy_chans
= kzalloc(num_phy_chans
*
2684 sizeof(struct d40_chan
*),
2686 if (!base
->lookup_phy_chans
)
2689 if (num_log_chans
+ plat_data
->memcpy_len
) {
2691 * The max number of logical channels are event lines for all
2692 * src devices and dst devices
2694 base
->lookup_log_chans
= kzalloc(plat_data
->dev_len
* 2 *
2695 sizeof(struct d40_chan
*),
2697 if (!base
->lookup_log_chans
)
2701 base
->lcla_pool
.alloc_map
= kzalloc(num_phy_chans
*
2702 sizeof(struct d40_desc
*) *
2703 D40_LCLA_LINK_PER_EVENT_GRP
,
2705 if (!base
->lcla_pool
.alloc_map
)
2708 base
->desc_slab
= kmem_cache_create(D40_NAME
, sizeof(struct d40_desc
),
2709 0, SLAB_HWCACHE_ALIGN
,
2711 if (base
->desc_slab
== NULL
)
2724 release_mem_region(res
->start
,
2725 resource_size(res
));
2730 kfree(base
->lcla_pool
.alloc_map
);
2731 kfree(base
->lookup_log_chans
);
2732 kfree(base
->lookup_phy_chans
);
2733 kfree(base
->phy_res
);
2740 static void __init
d40_hw_init(struct d40_base
*base
)
2743 static const struct d40_reg_val dma_init_reg
[] = {
2744 /* Clock every part of the DMA block from start */
2745 { .reg
= D40_DREG_GCC
, .val
= 0x0000ff01},
2747 /* Interrupts on all logical channels */
2748 { .reg
= D40_DREG_LCMIS0
, .val
= 0xFFFFFFFF},
2749 { .reg
= D40_DREG_LCMIS1
, .val
= 0xFFFFFFFF},
2750 { .reg
= D40_DREG_LCMIS2
, .val
= 0xFFFFFFFF},
2751 { .reg
= D40_DREG_LCMIS3
, .val
= 0xFFFFFFFF},
2752 { .reg
= D40_DREG_LCICR0
, .val
= 0xFFFFFFFF},
2753 { .reg
= D40_DREG_LCICR1
, .val
= 0xFFFFFFFF},
2754 { .reg
= D40_DREG_LCICR2
, .val
= 0xFFFFFFFF},
2755 { .reg
= D40_DREG_LCICR3
, .val
= 0xFFFFFFFF},
2756 { .reg
= D40_DREG_LCTIS0
, .val
= 0xFFFFFFFF},
2757 { .reg
= D40_DREG_LCTIS1
, .val
= 0xFFFFFFFF},
2758 { .reg
= D40_DREG_LCTIS2
, .val
= 0xFFFFFFFF},
2759 { .reg
= D40_DREG_LCTIS3
, .val
= 0xFFFFFFFF}
2762 u32 prmseo
[2] = {0, 0};
2763 u32 activeo
[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2767 for (i
= 0; i
< ARRAY_SIZE(dma_init_reg
); i
++)
2768 writel(dma_init_reg
[i
].val
,
2769 base
->virtbase
+ dma_init_reg
[i
].reg
);
2771 /* Configure all our dma channels to default settings */
2772 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2774 activeo
[i
% 2] = activeo
[i
% 2] << 2;
2776 if (base
->phy_res
[base
->num_phy_chans
- i
- 1].allocated_src
2778 activeo
[i
% 2] |= 3;
2782 /* Enable interrupt # */
2783 pcmis
= (pcmis
<< 1) | 1;
2785 /* Clear interrupt # */
2786 pcicr
= (pcicr
<< 1) | 1;
2788 /* Set channel to physical mode */
2789 prmseo
[i
% 2] = prmseo
[i
% 2] << 2;
2794 writel(prmseo
[1], base
->virtbase
+ D40_DREG_PRMSE
);
2795 writel(prmseo
[0], base
->virtbase
+ D40_DREG_PRMSO
);
2796 writel(activeo
[1], base
->virtbase
+ D40_DREG_ACTIVE
);
2797 writel(activeo
[0], base
->virtbase
+ D40_DREG_ACTIVO
);
2799 /* Write which interrupt to enable */
2800 writel(pcmis
, base
->virtbase
+ D40_DREG_PCMIS
);
2802 /* Write which interrupt to clear */
2803 writel(pcicr
, base
->virtbase
+ D40_DREG_PCICR
);
2807 static int __init
d40_lcla_allocate(struct d40_base
*base
)
2809 struct d40_lcla_pool
*pool
= &base
->lcla_pool
;
2810 unsigned long *page_list
;
2815 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2816 * To full fill this hardware requirement without wasting 256 kb
2817 * we allocate pages until we get an aligned one.
2819 page_list
= kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS
,
2827 /* Calculating how many pages that are required */
2828 base
->lcla_pool
.pages
= SZ_1K
* base
->num_phy_chans
/ PAGE_SIZE
;
2830 for (i
= 0; i
< MAX_LCLA_ALLOC_ATTEMPTS
; i
++) {
2831 page_list
[i
] = __get_free_pages(GFP_KERNEL
,
2832 base
->lcla_pool
.pages
);
2833 if (!page_list
[i
]) {
2835 d40_err(base
->dev
, "Failed to allocate %d pages.\n",
2836 base
->lcla_pool
.pages
);
2838 for (j
= 0; j
< i
; j
++)
2839 free_pages(page_list
[j
], base
->lcla_pool
.pages
);
2843 if ((virt_to_phys((void *)page_list
[i
]) &
2844 (LCLA_ALIGNMENT
- 1)) == 0)
2848 for (j
= 0; j
< i
; j
++)
2849 free_pages(page_list
[j
], base
->lcla_pool
.pages
);
2851 if (i
< MAX_LCLA_ALLOC_ATTEMPTS
) {
2852 base
->lcla_pool
.base
= (void *)page_list
[i
];
2855 * After many attempts and no succees with finding the correct
2856 * alignment, try with allocating a big buffer.
2859 "[%s] Failed to get %d pages @ 18 bit align.\n",
2860 __func__
, base
->lcla_pool
.pages
);
2861 base
->lcla_pool
.base_unaligned
= kmalloc(SZ_1K
*
2862 base
->num_phy_chans
+
2865 if (!base
->lcla_pool
.base_unaligned
) {
2870 base
->lcla_pool
.base
= PTR_ALIGN(base
->lcla_pool
.base_unaligned
,
2874 pool
->dma_addr
= dma_map_single(base
->dev
, pool
->base
,
2875 SZ_1K
* base
->num_phy_chans
,
2877 if (dma_mapping_error(base
->dev
, pool
->dma_addr
)) {
2883 writel(virt_to_phys(base
->lcla_pool
.base
),
2884 base
->virtbase
+ D40_DREG_LCLA
);
2890 static int __init
d40_probe(struct platform_device
*pdev
)
2894 struct d40_base
*base
;
2895 struct resource
*res
= NULL
;
2896 int num_reserved_chans
;
2899 base
= d40_hw_detect_init(pdev
);
2904 num_reserved_chans
= d40_phy_res_init(base
);
2906 platform_set_drvdata(pdev
, base
);
2908 spin_lock_init(&base
->interrupt_lock
);
2909 spin_lock_init(&base
->execmd_lock
);
2911 /* Get IO for logical channel parameter address */
2912 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "lcpa");
2915 d40_err(&pdev
->dev
, "No \"lcpa\" memory resource\n");
2918 base
->lcpa_size
= resource_size(res
);
2919 base
->phy_lcpa
= res
->start
;
2921 if (request_mem_region(res
->start
, resource_size(res
),
2922 D40_NAME
" I/O lcpa") == NULL
) {
2925 "Failed to request LCPA region 0x%x-0x%x\n",
2926 res
->start
, res
->end
);
2930 /* We make use of ESRAM memory for this. */
2931 val
= readl(base
->virtbase
+ D40_DREG_LCPA
);
2932 if (res
->start
!= val
&& val
!= 0) {
2933 dev_warn(&pdev
->dev
,
2934 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2935 __func__
, val
, res
->start
);
2937 writel(res
->start
, base
->virtbase
+ D40_DREG_LCPA
);
2939 base
->lcpa_base
= ioremap(res
->start
, resource_size(res
));
2940 if (!base
->lcpa_base
) {
2942 d40_err(&pdev
->dev
, "Failed to ioremap LCPA region\n");
2946 ret
= d40_lcla_allocate(base
);
2948 d40_err(&pdev
->dev
, "Failed to allocate LCLA area\n");
2952 spin_lock_init(&base
->lcla_pool
.lock
);
2954 base
->irq
= platform_get_irq(pdev
, 0);
2956 ret
= request_irq(base
->irq
, d40_handle_interrupt
, 0, D40_NAME
, base
);
2958 d40_err(&pdev
->dev
, "No IRQ defined\n");
2962 err
= d40_dmaengine_init(base
, num_reserved_chans
);
2968 dev_info(base
->dev
, "initialized\n");
2973 if (base
->desc_slab
)
2974 kmem_cache_destroy(base
->desc_slab
);
2976 iounmap(base
->virtbase
);
2978 if (base
->lcla_pool
.dma_addr
)
2979 dma_unmap_single(base
->dev
, base
->lcla_pool
.dma_addr
,
2980 SZ_1K
* base
->num_phy_chans
,
2983 if (!base
->lcla_pool
.base_unaligned
&& base
->lcla_pool
.base
)
2984 free_pages((unsigned long)base
->lcla_pool
.base
,
2985 base
->lcla_pool
.pages
);
2987 kfree(base
->lcla_pool
.base_unaligned
);
2990 release_mem_region(base
->phy_lcpa
,
2992 if (base
->phy_start
)
2993 release_mem_region(base
->phy_start
,
2996 clk_disable(base
->clk
);
3000 kfree(base
->lcla_pool
.alloc_map
);
3001 kfree(base
->lookup_log_chans
);
3002 kfree(base
->lookup_phy_chans
);
3003 kfree(base
->phy_res
);
3007 d40_err(&pdev
->dev
, "probe failed\n");
3011 static struct platform_driver d40_driver
= {
3013 .owner
= THIS_MODULE
,
3018 static int __init
stedma40_init(void)
3020 return platform_driver_probe(&d40_driver
, d40_probe
);
3022 subsys_initcall(stedma40_init
);