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/export.h>
13 #include <linux/dmaengine.h>
14 #include <linux/platform_device.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/amba/bus.h>
20 #include <plat/ste_dma40.h>
22 #include "ste_dma40_ll.h"
24 #define D40_NAME "dma40"
26 #define D40_PHY_CHAN -1
28 /* For masking out/in 2 bit channel positions */
29 #define D40_CHAN_POS(chan) (2 * (chan / 2))
30 #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
32 /* Maximum iterations taken before giving up suspending a channel */
33 #define D40_SUSPEND_MAX_IT 500
35 /* Hardware requirement on LCLA alignment */
36 #define LCLA_ALIGNMENT 0x40000
38 /* Max number of links per event group */
39 #define D40_LCLA_LINK_PER_EVENT_GRP 128
40 #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
42 /* Attempts before giving up to trying to get pages that are aligned */
43 #define MAX_LCLA_ALLOC_ATTEMPTS 256
45 /* Bit markings for allocation map */
46 #define D40_ALLOC_FREE (1 << 31)
47 #define D40_ALLOC_PHY (1 << 30)
48 #define D40_ALLOC_LOG_FREE 0
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.
61 D40_DMA_SUSPEND_REQ
= 2,
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.
80 /* Space for dst and src, plus an extra for padding */
81 u8 pre_alloc_lli
[3 * sizeof(struct d40_phy_lli
)];
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
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 transferred llis.
94 * @lcla_alloc: Number of LCLA entries allocated.
95 * @txd: DMA engine struct. Used for among other things for communication
98 * @is_in_client_list: true if the client owns this descriptor.
101 * This descriptor is used for both logical and physical transfers.
105 struct d40_phy_lli_bidir lli_phy
;
107 struct d40_log_lli_bidir lli_log
;
109 struct d40_lli_pool lli_pool
;
114 struct dma_async_tx_descriptor txd
;
115 struct list_head node
;
117 bool is_in_client_list
;
122 * struct d40_lcla_pool - LCLA pool settings and data.
124 * @base: The virtual address of LCLA. 18 bit aligned.
125 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
126 * This pointer is only there for clean-up on error.
127 * @pages: The number of pages needed for all physical channels.
128 * Only used later for clean-up on error
129 * @lock: Lock to protect the content in this struct.
130 * @alloc_map: big map over which LCLA entry is own by which job.
132 struct d40_lcla_pool
{
135 void *base_unaligned
;
138 struct d40_desc
**alloc_map
;
142 * struct d40_phy_res - struct for handling eventlines mapped to physical
145 * @lock: A lock protection this entity.
146 * @num: The physical channel number of this entity.
147 * @allocated_src: Bit mapped to show which src event line's are mapped to
148 * this physical channel. Can also be free or physically allocated.
149 * @allocated_dst: Same as for src but is dst.
150 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
163 * struct d40_chan - Struct that describes a channel.
165 * @lock: A spinlock to protect this struct.
166 * @log_num: The logical number, if any of this channel.
167 * @completed: Starts with 1, after first interrupt it is set to dma engine's
169 * @pending_tx: The number of pending transfers. Used between interrupt handler
171 * @busy: Set to true when transfer is ongoing on this channel.
172 * @phy_chan: Pointer to physical channel which this instance runs on. If this
173 * point is NULL, then the channel is not allocated.
174 * @chan: DMA engine handle.
175 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
176 * transfer and call client callback.
177 * @client: Cliented owned descriptor list.
178 * @active: Active descriptor.
179 * @queue: Queued jobs.
180 * @dma_cfg: The client configuration of this dma channel.
181 * @configured: whether the dma_cfg configuration is valid
182 * @base: Pointer to the device instance struct.
183 * @src_def_cfg: Default cfg register setting for src.
184 * @dst_def_cfg: Default cfg register setting for dst.
185 * @log_def: Default logical channel settings.
186 * @lcla: Space for one dst src pair for logical channel transfers.
187 * @lcpa: Pointer to dst and src lcpa settings.
188 * @runtime_addr: runtime configured address.
189 * @runtime_direction: runtime configured direction.
191 * This struct can either "be" a logical or a physical channel.
196 /* ID of the most recent completed transfer */
200 struct d40_phy_res
*phy_chan
;
201 struct dma_chan chan
;
202 struct tasklet_struct tasklet
;
203 struct list_head client
;
204 struct list_head pending_queue
;
205 struct list_head active
;
206 struct list_head queue
;
207 struct stedma40_chan_cfg dma_cfg
;
209 struct d40_base
*base
;
210 /* Default register configurations */
213 struct d40_def_lcsp log_def
;
214 struct d40_log_lli_full
*lcpa
;
215 /* Runtime reconfiguration */
216 dma_addr_t runtime_addr
;
217 enum dma_data_direction runtime_direction
;
221 * struct d40_base - The big global struct, one for each probe'd instance.
223 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
224 * @execmd_lock: Lock for execute command usage since several channels share
225 * the same physical register.
226 * @dev: The device structure.
227 * @virtbase: The virtual base address of the DMA's register.
228 * @rev: silicon revision detected.
229 * @clk: Pointer to the DMA clock structure.
230 * @phy_start: Physical memory start of the DMA registers.
231 * @phy_size: Size of the DMA register map.
232 * @irq: The IRQ number.
233 * @num_phy_chans: The number of physical channels. Read from HW. This
234 * is the number of available channels for this driver, not counting "Secure
235 * mode" allocated physical channels.
236 * @num_log_chans: The number of logical channels. Calculated from
238 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
239 * @dma_slave: dma_device channels that can do only do slave transfers.
240 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
241 * @log_chans: Room for all possible logical channels in system.
242 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
243 * to log_chans entries.
244 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
245 * to phy_chans entries.
246 * @plat_data: Pointer to provided platform_data which is the driver
248 * @phy_res: Vector containing all physical channels.
249 * @lcla_pool: lcla pool settings and data.
250 * @lcpa_base: The virtual mapped address of LCPA.
251 * @phy_lcpa: The physical address of the LCPA.
252 * @lcpa_size: The size of the LCPA area.
253 * @desc_slab: cache for descriptors.
256 spinlock_t interrupt_lock
;
257 spinlock_t execmd_lock
;
259 void __iomem
*virtbase
;
262 phys_addr_t phy_start
;
263 resource_size_t phy_size
;
267 struct dma_device dma_both
;
268 struct dma_device dma_slave
;
269 struct dma_device dma_memcpy
;
270 struct d40_chan
*phy_chans
;
271 struct d40_chan
*log_chans
;
272 struct d40_chan
**lookup_log_chans
;
273 struct d40_chan
**lookup_phy_chans
;
274 struct stedma40_platform_data
*plat_data
;
275 /* Physical half channels */
276 struct d40_phy_res
*phy_res
;
277 struct d40_lcla_pool lcla_pool
;
280 resource_size_t lcpa_size
;
281 struct kmem_cache
*desc_slab
;
285 * struct d40_interrupt_lookup - lookup table for interrupt handler
287 * @src: Interrupt mask register.
288 * @clr: Interrupt clear register.
289 * @is_error: true if this is an error interrupt.
290 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
291 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
293 struct d40_interrupt_lookup
{
301 * struct d40_reg_val - simple lookup struct
303 * @reg: The register.
304 * @val: The value that belongs to the register in reg.
311 static struct device
*chan2dev(struct d40_chan
*d40c
)
313 return &d40c
->chan
.dev
->device
;
316 static bool chan_is_physical(struct d40_chan
*chan
)
318 return chan
->log_num
== D40_PHY_CHAN
;
321 static bool chan_is_logical(struct d40_chan
*chan
)
323 return !chan_is_physical(chan
);
326 static void __iomem
*chan_base(struct d40_chan
*chan
)
328 return chan
->base
->virtbase
+ D40_DREG_PCBASE
+
329 chan
->phy_chan
->num
* D40_DREG_PCDELTA
;
332 #define d40_err(dev, format, arg...) \
333 dev_err(dev, "[%s] " format, __func__, ## arg)
335 #define chan_err(d40c, format, arg...) \
336 d40_err(chan2dev(d40c), format, ## arg)
338 static int d40_pool_lli_alloc(struct d40_chan
*d40c
, struct d40_desc
*d40d
,
341 bool is_log
= chan_is_logical(d40c
);
346 align
= sizeof(struct d40_log_lli
);
348 align
= sizeof(struct d40_phy_lli
);
351 base
= d40d
->lli_pool
.pre_alloc_lli
;
352 d40d
->lli_pool
.size
= sizeof(d40d
->lli_pool
.pre_alloc_lli
);
353 d40d
->lli_pool
.base
= NULL
;
355 d40d
->lli_pool
.size
= lli_len
* 2 * align
;
357 base
= kmalloc(d40d
->lli_pool
.size
+ align
, GFP_NOWAIT
);
358 d40d
->lli_pool
.base
= base
;
360 if (d40d
->lli_pool
.base
== NULL
)
365 d40d
->lli_log
.src
= PTR_ALIGN(base
, align
);
366 d40d
->lli_log
.dst
= d40d
->lli_log
.src
+ lli_len
;
368 d40d
->lli_pool
.dma_addr
= 0;
370 d40d
->lli_phy
.src
= PTR_ALIGN(base
, align
);
371 d40d
->lli_phy
.dst
= d40d
->lli_phy
.src
+ lli_len
;
373 d40d
->lli_pool
.dma_addr
= dma_map_single(d40c
->base
->dev
,
378 if (dma_mapping_error(d40c
->base
->dev
,
379 d40d
->lli_pool
.dma_addr
)) {
380 kfree(d40d
->lli_pool
.base
);
381 d40d
->lli_pool
.base
= NULL
;
382 d40d
->lli_pool
.dma_addr
= 0;
390 static void d40_pool_lli_free(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
392 if (d40d
->lli_pool
.dma_addr
)
393 dma_unmap_single(d40c
->base
->dev
, d40d
->lli_pool
.dma_addr
,
394 d40d
->lli_pool
.size
, DMA_TO_DEVICE
);
396 kfree(d40d
->lli_pool
.base
);
397 d40d
->lli_pool
.base
= NULL
;
398 d40d
->lli_pool
.size
= 0;
399 d40d
->lli_log
.src
= NULL
;
400 d40d
->lli_log
.dst
= NULL
;
401 d40d
->lli_phy
.src
= NULL
;
402 d40d
->lli_phy
.dst
= NULL
;
405 static int d40_lcla_alloc_one(struct d40_chan
*d40c
,
406 struct d40_desc
*d40d
)
413 spin_lock_irqsave(&d40c
->base
->lcla_pool
.lock
, flags
);
415 p
= d40c
->phy_chan
->num
* D40_LCLA_LINK_PER_EVENT_GRP
;
418 * Allocate both src and dst at the same time, therefore the half
419 * start on 1 since 0 can't be used since zero is used as end marker.
421 for (i
= 1 ; i
< D40_LCLA_LINK_PER_EVENT_GRP
/ 2; i
++) {
422 if (!d40c
->base
->lcla_pool
.alloc_map
[p
+ i
]) {
423 d40c
->base
->lcla_pool
.alloc_map
[p
+ i
] = d40d
;
430 spin_unlock_irqrestore(&d40c
->base
->lcla_pool
.lock
, flags
);
435 static int d40_lcla_free_all(struct d40_chan
*d40c
,
436 struct d40_desc
*d40d
)
442 if (chan_is_physical(d40c
))
445 spin_lock_irqsave(&d40c
->base
->lcla_pool
.lock
, flags
);
447 for (i
= 1 ; i
< D40_LCLA_LINK_PER_EVENT_GRP
/ 2; i
++) {
448 if (d40c
->base
->lcla_pool
.alloc_map
[d40c
->phy_chan
->num
*
449 D40_LCLA_LINK_PER_EVENT_GRP
+ i
] == d40d
) {
450 d40c
->base
->lcla_pool
.alloc_map
[d40c
->phy_chan
->num
*
451 D40_LCLA_LINK_PER_EVENT_GRP
+ i
] = NULL
;
453 if (d40d
->lcla_alloc
== 0) {
460 spin_unlock_irqrestore(&d40c
->base
->lcla_pool
.lock
, flags
);
466 static void d40_desc_remove(struct d40_desc
*d40d
)
468 list_del(&d40d
->node
);
471 static struct d40_desc
*d40_desc_get(struct d40_chan
*d40c
)
473 struct d40_desc
*desc
= NULL
;
475 if (!list_empty(&d40c
->client
)) {
479 list_for_each_entry_safe(d
, _d
, &d40c
->client
, node
)
480 if (async_tx_test_ack(&d
->txd
)) {
481 d40_pool_lli_free(d40c
, d
);
484 memset(desc
, 0, sizeof(*desc
));
490 desc
= kmem_cache_zalloc(d40c
->base
->desc_slab
, GFP_NOWAIT
);
493 INIT_LIST_HEAD(&desc
->node
);
498 static void d40_desc_free(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
501 d40_pool_lli_free(d40c
, d40d
);
502 d40_lcla_free_all(d40c
, d40d
);
503 kmem_cache_free(d40c
->base
->desc_slab
, d40d
);
506 static void d40_desc_submit(struct d40_chan
*d40c
, struct d40_desc
*desc
)
508 list_add_tail(&desc
->node
, &d40c
->active
);
511 static void d40_phy_lli_load(struct d40_chan
*chan
, struct d40_desc
*desc
)
513 struct d40_phy_lli
*lli_dst
= desc
->lli_phy
.dst
;
514 struct d40_phy_lli
*lli_src
= desc
->lli_phy
.src
;
515 void __iomem
*base
= chan_base(chan
);
517 writel(lli_src
->reg_cfg
, base
+ D40_CHAN_REG_SSCFG
);
518 writel(lli_src
->reg_elt
, base
+ D40_CHAN_REG_SSELT
);
519 writel(lli_src
->reg_ptr
, base
+ D40_CHAN_REG_SSPTR
);
520 writel(lli_src
->reg_lnk
, base
+ D40_CHAN_REG_SSLNK
);
522 writel(lli_dst
->reg_cfg
, base
+ D40_CHAN_REG_SDCFG
);
523 writel(lli_dst
->reg_elt
, base
+ D40_CHAN_REG_SDELT
);
524 writel(lli_dst
->reg_ptr
, base
+ D40_CHAN_REG_SDPTR
);
525 writel(lli_dst
->reg_lnk
, base
+ D40_CHAN_REG_SDLNK
);
528 static void d40_log_lli_to_lcxa(struct d40_chan
*chan
, struct d40_desc
*desc
)
530 struct d40_lcla_pool
*pool
= &chan
->base
->lcla_pool
;
531 struct d40_log_lli_bidir
*lli
= &desc
->lli_log
;
532 int lli_current
= desc
->lli_current
;
533 int lli_len
= desc
->lli_len
;
534 bool cyclic
= desc
->cyclic
;
535 int curr_lcla
= -EINVAL
;
540 * We may have partially running cyclic transfers, in case we did't get
541 * enough LCLA entries.
543 linkback
= cyclic
&& lli_current
== 0;
546 * For linkback, we need one LCLA even with only one link, because we
547 * can't link back to the one in LCPA space
549 if (linkback
|| (lli_len
- lli_current
> 1)) {
550 curr_lcla
= d40_lcla_alloc_one(chan
, desc
);
551 first_lcla
= curr_lcla
;
555 * For linkback, we normally load the LCPA in the loop since we need to
556 * link it to the second LCLA and not the first. However, if we
557 * couldn't even get a first LCLA, then we have to run in LCPA and
560 if (!linkback
|| curr_lcla
== -EINVAL
) {
561 unsigned int flags
= 0;
563 if (curr_lcla
== -EINVAL
)
564 flags
|= LLI_TERM_INT
;
566 d40_log_lli_lcpa_write(chan
->lcpa
,
567 &lli
->dst
[lli_current
],
568 &lli
->src
[lli_current
],
577 for (; lli_current
< lli_len
; lli_current
++) {
578 unsigned int lcla_offset
= chan
->phy_chan
->num
* 1024 +
580 struct d40_log_lli
*lcla
= pool
->base
+ lcla_offset
;
581 unsigned int flags
= 0;
584 if (lli_current
+ 1 < lli_len
)
585 next_lcla
= d40_lcla_alloc_one(chan
, desc
);
587 next_lcla
= linkback
? first_lcla
: -EINVAL
;
589 if (cyclic
|| next_lcla
== -EINVAL
)
590 flags
|= LLI_TERM_INT
;
592 if (linkback
&& curr_lcla
== first_lcla
) {
593 /* First link goes in both LCPA and LCLA */
594 d40_log_lli_lcpa_write(chan
->lcpa
,
595 &lli
->dst
[lli_current
],
596 &lli
->src
[lli_current
],
601 * One unused LCLA in the cyclic case if the very first
604 d40_log_lli_lcla_write(lcla
,
605 &lli
->dst
[lli_current
],
606 &lli
->src
[lli_current
],
609 dma_sync_single_range_for_device(chan
->base
->dev
,
610 pool
->dma_addr
, lcla_offset
,
611 2 * sizeof(struct d40_log_lli
),
614 curr_lcla
= next_lcla
;
616 if (curr_lcla
== -EINVAL
|| curr_lcla
== first_lcla
) {
623 desc
->lli_current
= lli_current
;
626 static void d40_desc_load(struct d40_chan
*d40c
, struct d40_desc
*d40d
)
628 if (chan_is_physical(d40c
)) {
629 d40_phy_lli_load(d40c
, d40d
);
630 d40d
->lli_current
= d40d
->lli_len
;
632 d40_log_lli_to_lcxa(d40c
, d40d
);
635 static struct d40_desc
*d40_first_active_get(struct d40_chan
*d40c
)
639 if (list_empty(&d40c
->active
))
642 d
= list_first_entry(&d40c
->active
,
648 static void d40_desc_queue(struct d40_chan
*d40c
, struct d40_desc
*desc
)
650 list_add_tail(&desc
->node
, &d40c
->pending_queue
);
653 static struct d40_desc
*d40_first_pending(struct d40_chan
*d40c
)
657 if (list_empty(&d40c
->pending_queue
))
660 d
= list_first_entry(&d40c
->pending_queue
,
666 static struct d40_desc
*d40_first_queued(struct d40_chan
*d40c
)
670 if (list_empty(&d40c
->queue
))
673 d
= list_first_entry(&d40c
->queue
,
679 static int d40_psize_2_burst_size(bool is_log
, int psize
)
682 if (psize
== STEDMA40_PSIZE_LOG_1
)
685 if (psize
== STEDMA40_PSIZE_PHY_1
)
693 * The dma only supports transmitting packages up to
694 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
695 * dma elements required to send the entire sg list
697 static int d40_size_2_dmalen(int size
, u32 data_width1
, u32 data_width2
)
700 u32 max_w
= max(data_width1
, data_width2
);
701 u32 min_w
= min(data_width1
, data_width2
);
702 u32 seg_max
= ALIGN(STEDMA40_MAX_SEG_SIZE
<< min_w
, 1 << max_w
);
704 if (seg_max
> STEDMA40_MAX_SEG_SIZE
)
705 seg_max
-= (1 << max_w
);
707 if (!IS_ALIGNED(size
, 1 << max_w
))
713 dmalen
= size
/ seg_max
;
714 if (dmalen
* seg_max
< size
)
720 static int d40_sg_2_dmalen(struct scatterlist
*sgl
, int sg_len
,
721 u32 data_width1
, u32 data_width2
)
723 struct scatterlist
*sg
;
728 for_each_sg(sgl
, sg
, sg_len
, i
) {
729 ret
= d40_size_2_dmalen(sg_dma_len(sg
),
730 data_width1
, data_width2
);
738 /* Support functions for logical channels */
740 static int d40_channel_execute_command(struct d40_chan
*d40c
,
741 enum d40_command command
)
745 void __iomem
*active_reg
;
750 spin_lock_irqsave(&d40c
->base
->execmd_lock
, flags
);
752 if (d40c
->phy_chan
->num
% 2 == 0)
753 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVE
;
755 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVO
;
757 if (command
== D40_DMA_SUSPEND_REQ
) {
758 status
= (readl(active_reg
) &
759 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
760 D40_CHAN_POS(d40c
->phy_chan
->num
);
762 if (status
== D40_DMA_SUSPENDED
|| status
== D40_DMA_STOP
)
766 wmask
= 0xffffffff & ~(D40_CHAN_POS_MASK(d40c
->phy_chan
->num
));
767 writel(wmask
| (command
<< D40_CHAN_POS(d40c
->phy_chan
->num
)),
770 if (command
== D40_DMA_SUSPEND_REQ
) {
772 for (i
= 0 ; i
< D40_SUSPEND_MAX_IT
; i
++) {
773 status
= (readl(active_reg
) &
774 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
775 D40_CHAN_POS(d40c
->phy_chan
->num
);
779 * Reduce the number of bus accesses while
780 * waiting for the DMA to suspend.
784 if (status
== D40_DMA_STOP
||
785 status
== D40_DMA_SUSPENDED
)
789 if (i
== D40_SUSPEND_MAX_IT
) {
791 "unable to suspend the chl %d (log: %d) status %x\n",
792 d40c
->phy_chan
->num
, d40c
->log_num
,
800 spin_unlock_irqrestore(&d40c
->base
->execmd_lock
, flags
);
804 static void d40_term_all(struct d40_chan
*d40c
)
806 struct d40_desc
*d40d
;
808 /* Release active descriptors */
809 while ((d40d
= d40_first_active_get(d40c
))) {
810 d40_desc_remove(d40d
);
811 d40_desc_free(d40c
, d40d
);
814 /* Release queued descriptors waiting for transfer */
815 while ((d40d
= d40_first_queued(d40c
))) {
816 d40_desc_remove(d40d
);
817 d40_desc_free(d40c
, d40d
);
820 /* Release pending descriptors */
821 while ((d40d
= d40_first_pending(d40c
))) {
822 d40_desc_remove(d40d
);
823 d40_desc_free(d40c
, d40d
);
826 d40c
->pending_tx
= 0;
830 static void __d40_config_set_event(struct d40_chan
*d40c
, bool enable
,
833 void __iomem
*addr
= chan_base(d40c
) + reg
;
837 writel((D40_DEACTIVATE_EVENTLINE
<< D40_EVENTLINE_POS(event
))
838 | ~D40_EVENTLINE_MASK(event
), addr
);
843 * The hardware sometimes doesn't register the enable when src and dst
844 * event lines are active on the same logical channel. Retry to ensure
845 * it does. Usually only one retry is sufficient.
849 writel((D40_ACTIVATE_EVENTLINE
<< D40_EVENTLINE_POS(event
))
850 | ~D40_EVENTLINE_MASK(event
), addr
);
852 if (readl(addr
) & D40_EVENTLINE_MASK(event
))
857 dev_dbg(chan2dev(d40c
),
858 "[%s] workaround enable S%cLNK (%d tries)\n",
859 __func__
, reg
== D40_CHAN_REG_SSLNK
? 'S' : 'D',
865 static void d40_config_set_event(struct d40_chan
*d40c
, bool do_enable
)
869 spin_lock_irqsave(&d40c
->phy_chan
->lock
, flags
);
871 /* Enable event line connected to device (or memcpy) */
872 if ((d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) ||
873 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
)) {
874 u32 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
876 __d40_config_set_event(d40c
, do_enable
, event
,
880 if (d40c
->dma_cfg
.dir
!= STEDMA40_PERIPH_TO_MEM
) {
881 u32 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
883 __d40_config_set_event(d40c
, do_enable
, event
,
887 spin_unlock_irqrestore(&d40c
->phy_chan
->lock
, flags
);
890 static u32
d40_chan_has_events(struct d40_chan
*d40c
)
892 void __iomem
*chanbase
= chan_base(d40c
);
895 val
= readl(chanbase
+ D40_CHAN_REG_SSLNK
);
896 val
|= readl(chanbase
+ D40_CHAN_REG_SDLNK
);
901 static u32
d40_get_prmo(struct d40_chan
*d40c
)
903 static const unsigned int phy_map
[] = {
904 [STEDMA40_PCHAN_BASIC_MODE
]
905 = D40_DREG_PRMO_PCHAN_BASIC
,
906 [STEDMA40_PCHAN_MODULO_MODE
]
907 = D40_DREG_PRMO_PCHAN_MODULO
,
908 [STEDMA40_PCHAN_DOUBLE_DST_MODE
]
909 = D40_DREG_PRMO_PCHAN_DOUBLE_DST
,
911 static const unsigned int log_map
[] = {
912 [STEDMA40_LCHAN_SRC_PHY_DST_LOG
]
913 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG
,
914 [STEDMA40_LCHAN_SRC_LOG_DST_PHY
]
915 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY
,
916 [STEDMA40_LCHAN_SRC_LOG_DST_LOG
]
917 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG
,
920 if (chan_is_physical(d40c
))
921 return phy_map
[d40c
->dma_cfg
.mode_opt
];
923 return log_map
[d40c
->dma_cfg
.mode_opt
];
926 static void d40_config_write(struct d40_chan
*d40c
)
931 /* Odd addresses are even addresses + 4 */
932 addr_base
= (d40c
->phy_chan
->num
% 2) * 4;
933 /* Setup channel mode to logical or physical */
934 var
= ((u32
)(chan_is_logical(d40c
)) + 1) <<
935 D40_CHAN_POS(d40c
->phy_chan
->num
);
936 writel(var
, d40c
->base
->virtbase
+ D40_DREG_PRMSE
+ addr_base
);
938 /* Setup operational mode option register */
939 var
= d40_get_prmo(d40c
) << D40_CHAN_POS(d40c
->phy_chan
->num
);
941 writel(var
, d40c
->base
->virtbase
+ D40_DREG_PRMOE
+ addr_base
);
943 if (chan_is_logical(d40c
)) {
944 int lidx
= (d40c
->phy_chan
->num
<< D40_SREG_ELEM_LOG_LIDX_POS
)
945 & D40_SREG_ELEM_LOG_LIDX_MASK
;
946 void __iomem
*chanbase
= chan_base(d40c
);
948 /* Set default config for CFG reg */
949 writel(d40c
->src_def_cfg
, chanbase
+ D40_CHAN_REG_SSCFG
);
950 writel(d40c
->dst_def_cfg
, chanbase
+ D40_CHAN_REG_SDCFG
);
952 /* Set LIDX for lcla */
953 writel(lidx
, chanbase
+ D40_CHAN_REG_SSELT
);
954 writel(lidx
, chanbase
+ D40_CHAN_REG_SDELT
);
958 static u32
d40_residue(struct d40_chan
*d40c
)
962 if (chan_is_logical(d40c
))
963 num_elt
= (readl(&d40c
->lcpa
->lcsp2
) & D40_MEM_LCSP2_ECNT_MASK
)
964 >> D40_MEM_LCSP2_ECNT_POS
;
966 u32 val
= readl(chan_base(d40c
) + D40_CHAN_REG_SDELT
);
967 num_elt
= (val
& D40_SREG_ELEM_PHY_ECNT_MASK
)
968 >> D40_SREG_ELEM_PHY_ECNT_POS
;
971 return num_elt
* (1 << d40c
->dma_cfg
.dst_info
.data_width
);
974 static bool d40_tx_is_linked(struct d40_chan
*d40c
)
978 if (chan_is_logical(d40c
))
979 is_link
= readl(&d40c
->lcpa
->lcsp3
) & D40_MEM_LCSP3_DLOS_MASK
;
981 is_link
= readl(chan_base(d40c
) + D40_CHAN_REG_SDLNK
)
982 & D40_SREG_LNK_PHYS_LNK_MASK
;
987 static int d40_pause(struct d40_chan
*d40c
)
995 spin_lock_irqsave(&d40c
->lock
, flags
);
997 res
= d40_channel_execute_command(d40c
, D40_DMA_SUSPEND_REQ
);
999 if (chan_is_logical(d40c
)) {
1000 d40_config_set_event(d40c
, false);
1001 /* Resume the other logical channels if any */
1002 if (d40_chan_has_events(d40c
))
1003 res
= d40_channel_execute_command(d40c
,
1008 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1012 static int d40_resume(struct d40_chan
*d40c
)
1015 unsigned long flags
;
1020 spin_lock_irqsave(&d40c
->lock
, flags
);
1022 if (d40c
->base
->rev
== 0)
1023 if (chan_is_logical(d40c
)) {
1024 res
= d40_channel_execute_command(d40c
,
1025 D40_DMA_SUSPEND_REQ
);
1029 /* If bytes left to transfer or linked tx resume job */
1030 if (d40_residue(d40c
) || d40_tx_is_linked(d40c
)) {
1032 if (chan_is_logical(d40c
))
1033 d40_config_set_event(d40c
, true);
1035 res
= d40_channel_execute_command(d40c
, D40_DMA_RUN
);
1039 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1043 static int d40_terminate_all(struct d40_chan
*chan
)
1045 unsigned long flags
;
1048 ret
= d40_pause(chan
);
1049 if (!ret
&& chan_is_physical(chan
))
1050 ret
= d40_channel_execute_command(chan
, D40_DMA_STOP
);
1052 spin_lock_irqsave(&chan
->lock
, flags
);
1054 spin_unlock_irqrestore(&chan
->lock
, flags
);
1059 static dma_cookie_t
d40_tx_submit(struct dma_async_tx_descriptor
*tx
)
1061 struct d40_chan
*d40c
= container_of(tx
->chan
,
1064 struct d40_desc
*d40d
= container_of(tx
, struct d40_desc
, txd
);
1065 unsigned long flags
;
1067 spin_lock_irqsave(&d40c
->lock
, flags
);
1069 d40c
->chan
.cookie
++;
1071 if (d40c
->chan
.cookie
< 0)
1072 d40c
->chan
.cookie
= 1;
1074 d40d
->txd
.cookie
= d40c
->chan
.cookie
;
1076 d40_desc_queue(d40c
, d40d
);
1078 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1083 static int d40_start(struct d40_chan
*d40c
)
1085 if (d40c
->base
->rev
== 0) {
1088 if (chan_is_logical(d40c
)) {
1089 err
= d40_channel_execute_command(d40c
,
1090 D40_DMA_SUSPEND_REQ
);
1096 if (chan_is_logical(d40c
))
1097 d40_config_set_event(d40c
, true);
1099 return d40_channel_execute_command(d40c
, D40_DMA_RUN
);
1102 static struct d40_desc
*d40_queue_start(struct d40_chan
*d40c
)
1104 struct d40_desc
*d40d
;
1107 /* Start queued jobs, if any */
1108 d40d
= d40_first_queued(d40c
);
1113 /* Remove from queue */
1114 d40_desc_remove(d40d
);
1116 /* Add to active queue */
1117 d40_desc_submit(d40c
, d40d
);
1119 /* Initiate DMA job */
1120 d40_desc_load(d40c
, d40d
);
1123 err
= d40_start(d40c
);
1132 /* called from interrupt context */
1133 static void dma_tc_handle(struct d40_chan
*d40c
)
1135 struct d40_desc
*d40d
;
1137 /* Get first active entry from list */
1138 d40d
= d40_first_active_get(d40c
);
1145 * If this was a paritially loaded list, we need to reloaded
1146 * it, and only when the list is completed. We need to check
1147 * for done because the interrupt will hit for every link, and
1148 * not just the last one.
1150 if (d40d
->lli_current
< d40d
->lli_len
1151 && !d40_tx_is_linked(d40c
)
1152 && !d40_residue(d40c
)) {
1153 d40_lcla_free_all(d40c
, d40d
);
1154 d40_desc_load(d40c
, d40d
);
1155 (void) d40_start(d40c
);
1157 if (d40d
->lli_current
== d40d
->lli_len
)
1158 d40d
->lli_current
= 0;
1161 d40_lcla_free_all(d40c
, d40d
);
1163 if (d40d
->lli_current
< d40d
->lli_len
) {
1164 d40_desc_load(d40c
, d40d
);
1166 (void) d40_start(d40c
);
1170 if (d40_queue_start(d40c
) == NULL
)
1175 tasklet_schedule(&d40c
->tasklet
);
1179 static void dma_tasklet(unsigned long data
)
1181 struct d40_chan
*d40c
= (struct d40_chan
*) data
;
1182 struct d40_desc
*d40d
;
1183 unsigned long flags
;
1184 dma_async_tx_callback callback
;
1185 void *callback_param
;
1187 spin_lock_irqsave(&d40c
->lock
, flags
);
1189 /* Get first active entry from list */
1190 d40d
= d40_first_active_get(d40c
);
1195 d40c
->completed
= d40d
->txd
.cookie
;
1198 * If terminating a channel pending_tx is set to zero.
1199 * This prevents any finished active jobs to return to the client.
1201 if (d40c
->pending_tx
== 0) {
1202 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1206 /* Callback to client */
1207 callback
= d40d
->txd
.callback
;
1208 callback_param
= d40d
->txd
.callback_param
;
1210 if (!d40d
->cyclic
) {
1211 if (async_tx_test_ack(&d40d
->txd
)) {
1212 d40_pool_lli_free(d40c
, d40d
);
1213 d40_desc_remove(d40d
);
1214 d40_desc_free(d40c
, d40d
);
1216 if (!d40d
->is_in_client_list
) {
1217 d40_desc_remove(d40d
);
1218 d40_lcla_free_all(d40c
, d40d
);
1219 list_add_tail(&d40d
->node
, &d40c
->client
);
1220 d40d
->is_in_client_list
= true;
1227 if (d40c
->pending_tx
)
1228 tasklet_schedule(&d40c
->tasklet
);
1230 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1232 if (callback
&& (d40d
->txd
.flags
& DMA_PREP_INTERRUPT
))
1233 callback(callback_param
);
1238 /* Rescue manoeuvre if receiving double interrupts */
1239 if (d40c
->pending_tx
> 0)
1241 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1244 static irqreturn_t
d40_handle_interrupt(int irq
, void *data
)
1246 static const struct d40_interrupt_lookup il
[] = {
1247 {D40_DREG_LCTIS0
, D40_DREG_LCICR0
, false, 0},
1248 {D40_DREG_LCTIS1
, D40_DREG_LCICR1
, false, 32},
1249 {D40_DREG_LCTIS2
, D40_DREG_LCICR2
, false, 64},
1250 {D40_DREG_LCTIS3
, D40_DREG_LCICR3
, false, 96},
1251 {D40_DREG_LCEIS0
, D40_DREG_LCICR0
, true, 0},
1252 {D40_DREG_LCEIS1
, D40_DREG_LCICR1
, true, 32},
1253 {D40_DREG_LCEIS2
, D40_DREG_LCICR2
, true, 64},
1254 {D40_DREG_LCEIS3
, D40_DREG_LCICR3
, true, 96},
1255 {D40_DREG_PCTIS
, D40_DREG_PCICR
, false, D40_PHY_CHAN
},
1256 {D40_DREG_PCEIS
, D40_DREG_PCICR
, true, D40_PHY_CHAN
},
1260 u32 regs
[ARRAY_SIZE(il
)];
1264 struct d40_chan
*d40c
;
1265 unsigned long flags
;
1266 struct d40_base
*base
= data
;
1268 spin_lock_irqsave(&base
->interrupt_lock
, flags
);
1270 /* Read interrupt status of both logical and physical channels */
1271 for (i
= 0; i
< ARRAY_SIZE(il
); i
++)
1272 regs
[i
] = readl(base
->virtbase
+ il
[i
].src
);
1276 chan
= find_next_bit((unsigned long *)regs
,
1277 BITS_PER_LONG
* ARRAY_SIZE(il
), chan
+ 1);
1279 /* No more set bits found? */
1280 if (chan
== BITS_PER_LONG
* ARRAY_SIZE(il
))
1283 row
= chan
/ BITS_PER_LONG
;
1284 idx
= chan
& (BITS_PER_LONG
- 1);
1287 writel(1 << idx
, base
->virtbase
+ il
[row
].clr
);
1289 if (il
[row
].offset
== D40_PHY_CHAN
)
1290 d40c
= base
->lookup_phy_chans
[idx
];
1292 d40c
= base
->lookup_log_chans
[il
[row
].offset
+ idx
];
1293 spin_lock(&d40c
->lock
);
1295 if (!il
[row
].is_error
)
1296 dma_tc_handle(d40c
);
1298 d40_err(base
->dev
, "IRQ chan: %ld offset %d idx %d\n",
1299 chan
, il
[row
].offset
, idx
);
1301 spin_unlock(&d40c
->lock
);
1304 spin_unlock_irqrestore(&base
->interrupt_lock
, flags
);
1309 static int d40_validate_conf(struct d40_chan
*d40c
,
1310 struct stedma40_chan_cfg
*conf
)
1313 u32 dst_event_group
= D40_TYPE_TO_GROUP(conf
->dst_dev_type
);
1314 u32 src_event_group
= D40_TYPE_TO_GROUP(conf
->src_dev_type
);
1315 bool is_log
= conf
->mode
== STEDMA40_MODE_LOGICAL
;
1318 chan_err(d40c
, "Invalid direction.\n");
1322 if (conf
->dst_dev_type
!= STEDMA40_DEV_DST_MEMORY
&&
1323 d40c
->base
->plat_data
->dev_tx
[conf
->dst_dev_type
] == 0 &&
1324 d40c
->runtime_addr
== 0) {
1326 chan_err(d40c
, "Invalid TX channel address (%d)\n",
1327 conf
->dst_dev_type
);
1331 if (conf
->src_dev_type
!= STEDMA40_DEV_SRC_MEMORY
&&
1332 d40c
->base
->plat_data
->dev_rx
[conf
->src_dev_type
] == 0 &&
1333 d40c
->runtime_addr
== 0) {
1334 chan_err(d40c
, "Invalid RX channel address (%d)\n",
1335 conf
->src_dev_type
);
1339 if (conf
->dir
== STEDMA40_MEM_TO_PERIPH
&&
1340 dst_event_group
== STEDMA40_DEV_DST_MEMORY
) {
1341 chan_err(d40c
, "Invalid dst\n");
1345 if (conf
->dir
== STEDMA40_PERIPH_TO_MEM
&&
1346 src_event_group
== STEDMA40_DEV_SRC_MEMORY
) {
1347 chan_err(d40c
, "Invalid src\n");
1351 if (src_event_group
== STEDMA40_DEV_SRC_MEMORY
&&
1352 dst_event_group
== STEDMA40_DEV_DST_MEMORY
&& is_log
) {
1353 chan_err(d40c
, "No event line\n");
1357 if (conf
->dir
== STEDMA40_PERIPH_TO_PERIPH
&&
1358 (src_event_group
!= dst_event_group
)) {
1359 chan_err(d40c
, "Invalid event group\n");
1363 if (conf
->dir
== STEDMA40_PERIPH_TO_PERIPH
) {
1365 * DMAC HW supports it. Will be added to this driver,
1366 * in case any dma client requires it.
1368 chan_err(d40c
, "periph to periph not supported\n");
1372 if (d40_psize_2_burst_size(is_log
, conf
->src_info
.psize
) *
1373 (1 << conf
->src_info
.data_width
) !=
1374 d40_psize_2_burst_size(is_log
, conf
->dst_info
.psize
) *
1375 (1 << conf
->dst_info
.data_width
)) {
1377 * The DMAC hardware only supports
1378 * src (burst x width) == dst (burst x width)
1381 chan_err(d40c
, "src (burst x width) != dst (burst x width)\n");
1388 static bool d40_alloc_mask_set(struct d40_phy_res
*phy
, bool is_src
,
1389 int log_event_line
, bool is_log
)
1391 unsigned long flags
;
1392 spin_lock_irqsave(&phy
->lock
, flags
);
1394 /* Physical interrupts are masked per physical full channel */
1395 if (phy
->allocated_src
== D40_ALLOC_FREE
&&
1396 phy
->allocated_dst
== D40_ALLOC_FREE
) {
1397 phy
->allocated_dst
= D40_ALLOC_PHY
;
1398 phy
->allocated_src
= D40_ALLOC_PHY
;
1404 /* Logical channel */
1406 if (phy
->allocated_src
== D40_ALLOC_PHY
)
1409 if (phy
->allocated_src
== D40_ALLOC_FREE
)
1410 phy
->allocated_src
= D40_ALLOC_LOG_FREE
;
1412 if (!(phy
->allocated_src
& (1 << log_event_line
))) {
1413 phy
->allocated_src
|= 1 << log_event_line
;
1418 if (phy
->allocated_dst
== D40_ALLOC_PHY
)
1421 if (phy
->allocated_dst
== D40_ALLOC_FREE
)
1422 phy
->allocated_dst
= D40_ALLOC_LOG_FREE
;
1424 if (!(phy
->allocated_dst
& (1 << log_event_line
))) {
1425 phy
->allocated_dst
|= 1 << log_event_line
;
1432 spin_unlock_irqrestore(&phy
->lock
, flags
);
1435 spin_unlock_irqrestore(&phy
->lock
, flags
);
1439 static bool d40_alloc_mask_free(struct d40_phy_res
*phy
, bool is_src
,
1442 unsigned long flags
;
1443 bool is_free
= false;
1445 spin_lock_irqsave(&phy
->lock
, flags
);
1446 if (!log_event_line
) {
1447 phy
->allocated_dst
= D40_ALLOC_FREE
;
1448 phy
->allocated_src
= D40_ALLOC_FREE
;
1453 /* Logical channel */
1455 phy
->allocated_src
&= ~(1 << log_event_line
);
1456 if (phy
->allocated_src
== D40_ALLOC_LOG_FREE
)
1457 phy
->allocated_src
= D40_ALLOC_FREE
;
1459 phy
->allocated_dst
&= ~(1 << log_event_line
);
1460 if (phy
->allocated_dst
== D40_ALLOC_LOG_FREE
)
1461 phy
->allocated_dst
= D40_ALLOC_FREE
;
1464 is_free
= ((phy
->allocated_src
| phy
->allocated_dst
) ==
1468 spin_unlock_irqrestore(&phy
->lock
, flags
);
1473 static int d40_allocate_channel(struct d40_chan
*d40c
)
1478 struct d40_phy_res
*phys
;
1483 bool is_log
= d40c
->dma_cfg
.mode
== STEDMA40_MODE_LOGICAL
;
1485 phys
= d40c
->base
->phy_res
;
1487 if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1488 dev_type
= d40c
->dma_cfg
.src_dev_type
;
1489 log_num
= 2 * dev_type
;
1491 } else if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1492 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1493 /* dst event lines are used for logical memcpy */
1494 dev_type
= d40c
->dma_cfg
.dst_dev_type
;
1495 log_num
= 2 * dev_type
+ 1;
1500 event_group
= D40_TYPE_TO_GROUP(dev_type
);
1501 event_line
= D40_TYPE_TO_EVENT(dev_type
);
1504 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1505 /* Find physical half channel */
1506 for (i
= 0; i
< d40c
->base
->num_phy_chans
; i
++) {
1508 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1513 for (j
= 0; j
< d40c
->base
->num_phy_chans
; j
+= 8) {
1514 int phy_num
= j
+ event_group
* 2;
1515 for (i
= phy_num
; i
< phy_num
+ 2; i
++) {
1516 if (d40_alloc_mask_set(&phys
[i
],
1525 d40c
->phy_chan
= &phys
[i
];
1526 d40c
->log_num
= D40_PHY_CHAN
;
1532 /* Find logical channel */
1533 for (j
= 0; j
< d40c
->base
->num_phy_chans
; j
+= 8) {
1534 int phy_num
= j
+ event_group
* 2;
1536 * Spread logical channels across all available physical rather
1537 * than pack every logical channel at the first available phy
1541 for (i
= phy_num
; i
< phy_num
+ 2; i
++) {
1542 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1543 event_line
, is_log
))
1547 for (i
= phy_num
+ 1; i
>= phy_num
; i
--) {
1548 if (d40_alloc_mask_set(&phys
[i
], is_src
,
1549 event_line
, is_log
))
1557 d40c
->phy_chan
= &phys
[i
];
1558 d40c
->log_num
= log_num
;
1562 d40c
->base
->lookup_log_chans
[d40c
->log_num
] = d40c
;
1564 d40c
->base
->lookup_phy_chans
[d40c
->phy_chan
->num
] = d40c
;
1570 static int d40_config_memcpy(struct d40_chan
*d40c
)
1572 dma_cap_mask_t cap
= d40c
->chan
.device
->cap_mask
;
1574 if (dma_has_cap(DMA_MEMCPY
, cap
) && !dma_has_cap(DMA_SLAVE
, cap
)) {
1575 d40c
->dma_cfg
= *d40c
->base
->plat_data
->memcpy_conf_log
;
1576 d40c
->dma_cfg
.src_dev_type
= STEDMA40_DEV_SRC_MEMORY
;
1577 d40c
->dma_cfg
.dst_dev_type
= d40c
->base
->plat_data
->
1578 memcpy
[d40c
->chan
.chan_id
];
1580 } else if (dma_has_cap(DMA_MEMCPY
, cap
) &&
1581 dma_has_cap(DMA_SLAVE
, cap
)) {
1582 d40c
->dma_cfg
= *d40c
->base
->plat_data
->memcpy_conf_phy
;
1584 chan_err(d40c
, "No memcpy\n");
1592 static int d40_free_dma(struct d40_chan
*d40c
)
1597 struct d40_phy_res
*phy
= d40c
->phy_chan
;
1600 struct d40_desc
*_d
;
1603 /* Terminate all queued and active transfers */
1606 /* Release client owned descriptors */
1607 if (!list_empty(&d40c
->client
))
1608 list_for_each_entry_safe(d
, _d
, &d40c
->client
, node
) {
1609 d40_pool_lli_free(d40c
, d
);
1611 d40_desc_free(d40c
, d
);
1615 chan_err(d40c
, "phy == null\n");
1619 if (phy
->allocated_src
== D40_ALLOC_FREE
&&
1620 phy
->allocated_dst
== D40_ALLOC_FREE
) {
1621 chan_err(d40c
, "channel already free\n");
1625 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1626 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1627 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
1629 } else if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1630 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
1633 chan_err(d40c
, "Unknown direction\n");
1637 res
= d40_channel_execute_command(d40c
, D40_DMA_SUSPEND_REQ
);
1639 chan_err(d40c
, "suspend failed\n");
1643 if (chan_is_logical(d40c
)) {
1644 /* Release logical channel, deactivate the event line */
1646 d40_config_set_event(d40c
, false);
1647 d40c
->base
->lookup_log_chans
[d40c
->log_num
] = NULL
;
1650 * Check if there are more logical allocation
1651 * on this phy channel.
1653 if (!d40_alloc_mask_free(phy
, is_src
, event
)) {
1654 /* Resume the other logical channels if any */
1655 if (d40_chan_has_events(d40c
)) {
1656 res
= d40_channel_execute_command(d40c
,
1660 "Executing RUN command\n");
1667 (void) d40_alloc_mask_free(phy
, is_src
, 0);
1670 /* Release physical channel */
1671 res
= d40_channel_execute_command(d40c
, D40_DMA_STOP
);
1673 chan_err(d40c
, "Failed to stop channel\n");
1676 d40c
->phy_chan
= NULL
;
1677 d40c
->configured
= false;
1678 d40c
->base
->lookup_phy_chans
[phy
->num
] = NULL
;
1683 static bool d40_is_paused(struct d40_chan
*d40c
)
1685 void __iomem
*chanbase
= chan_base(d40c
);
1686 bool is_paused
= false;
1687 unsigned long flags
;
1688 void __iomem
*active_reg
;
1692 spin_lock_irqsave(&d40c
->lock
, flags
);
1694 if (chan_is_physical(d40c
)) {
1695 if (d40c
->phy_chan
->num
% 2 == 0)
1696 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVE
;
1698 active_reg
= d40c
->base
->virtbase
+ D40_DREG_ACTIVO
;
1700 status
= (readl(active_reg
) &
1701 D40_CHAN_POS_MASK(d40c
->phy_chan
->num
)) >>
1702 D40_CHAN_POS(d40c
->phy_chan
->num
);
1703 if (status
== D40_DMA_SUSPENDED
|| status
== D40_DMA_STOP
)
1709 if (d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
||
1710 d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_MEM
) {
1711 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.dst_dev_type
);
1712 status
= readl(chanbase
+ D40_CHAN_REG_SDLNK
);
1713 } else if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) {
1714 event
= D40_TYPE_TO_EVENT(d40c
->dma_cfg
.src_dev_type
);
1715 status
= readl(chanbase
+ D40_CHAN_REG_SSLNK
);
1717 chan_err(d40c
, "Unknown direction\n");
1721 status
= (status
& D40_EVENTLINE_MASK(event
)) >>
1722 D40_EVENTLINE_POS(event
);
1724 if (status
!= D40_DMA_RUN
)
1727 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1733 static u32
stedma40_residue(struct dma_chan
*chan
)
1735 struct d40_chan
*d40c
=
1736 container_of(chan
, struct d40_chan
, chan
);
1738 unsigned long flags
;
1740 spin_lock_irqsave(&d40c
->lock
, flags
);
1741 bytes_left
= d40_residue(d40c
);
1742 spin_unlock_irqrestore(&d40c
->lock
, flags
);
1748 d40_prep_sg_log(struct d40_chan
*chan
, struct d40_desc
*desc
,
1749 struct scatterlist
*sg_src
, struct scatterlist
*sg_dst
,
1750 unsigned int sg_len
, dma_addr_t src_dev_addr
,
1751 dma_addr_t dst_dev_addr
)
1753 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1754 struct stedma40_half_channel_info
*src_info
= &cfg
->src_info
;
1755 struct stedma40_half_channel_info
*dst_info
= &cfg
->dst_info
;
1758 ret
= d40_log_sg_to_lli(sg_src
, sg_len
,
1761 chan
->log_def
.lcsp1
,
1762 src_info
->data_width
,
1763 dst_info
->data_width
);
1765 ret
= d40_log_sg_to_lli(sg_dst
, sg_len
,
1768 chan
->log_def
.lcsp3
,
1769 dst_info
->data_width
,
1770 src_info
->data_width
);
1772 return ret
< 0 ? ret
: 0;
1776 d40_prep_sg_phy(struct d40_chan
*chan
, struct d40_desc
*desc
,
1777 struct scatterlist
*sg_src
, struct scatterlist
*sg_dst
,
1778 unsigned int sg_len
, dma_addr_t src_dev_addr
,
1779 dma_addr_t dst_dev_addr
)
1781 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1782 struct stedma40_half_channel_info
*src_info
= &cfg
->src_info
;
1783 struct stedma40_half_channel_info
*dst_info
= &cfg
->dst_info
;
1784 unsigned long flags
= 0;
1788 flags
|= LLI_CYCLIC
| LLI_TERM_INT
;
1790 ret
= d40_phy_sg_to_lli(sg_src
, sg_len
, src_dev_addr
,
1792 virt_to_phys(desc
->lli_phy
.src
),
1794 src_info
, dst_info
, flags
);
1796 ret
= d40_phy_sg_to_lli(sg_dst
, sg_len
, dst_dev_addr
,
1798 virt_to_phys(desc
->lli_phy
.dst
),
1800 dst_info
, src_info
, flags
);
1802 dma_sync_single_for_device(chan
->base
->dev
, desc
->lli_pool
.dma_addr
,
1803 desc
->lli_pool
.size
, DMA_TO_DEVICE
);
1805 return ret
< 0 ? ret
: 0;
1809 static struct d40_desc
*
1810 d40_prep_desc(struct d40_chan
*chan
, struct scatterlist
*sg
,
1811 unsigned int sg_len
, unsigned long dma_flags
)
1813 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1814 struct d40_desc
*desc
;
1817 desc
= d40_desc_get(chan
);
1821 desc
->lli_len
= d40_sg_2_dmalen(sg
, sg_len
, cfg
->src_info
.data_width
,
1822 cfg
->dst_info
.data_width
);
1823 if (desc
->lli_len
< 0) {
1824 chan_err(chan
, "Unaligned size\n");
1828 ret
= d40_pool_lli_alloc(chan
, desc
, desc
->lli_len
);
1830 chan_err(chan
, "Could not allocate lli\n");
1835 desc
->lli_current
= 0;
1836 desc
->txd
.flags
= dma_flags
;
1837 desc
->txd
.tx_submit
= d40_tx_submit
;
1839 dma_async_tx_descriptor_init(&desc
->txd
, &chan
->chan
);
1844 d40_desc_free(chan
, desc
);
1849 d40_get_dev_addr(struct d40_chan
*chan
, enum dma_data_direction direction
)
1851 struct stedma40_platform_data
*plat
= chan
->base
->plat_data
;
1852 struct stedma40_chan_cfg
*cfg
= &chan
->dma_cfg
;
1853 dma_addr_t addr
= 0;
1855 if (chan
->runtime_addr
)
1856 return chan
->runtime_addr
;
1858 if (direction
== DMA_FROM_DEVICE
)
1859 addr
= plat
->dev_rx
[cfg
->src_dev_type
];
1860 else if (direction
== DMA_TO_DEVICE
)
1861 addr
= plat
->dev_tx
[cfg
->dst_dev_type
];
1866 static struct dma_async_tx_descriptor
*
1867 d40_prep_sg(struct dma_chan
*dchan
, struct scatterlist
*sg_src
,
1868 struct scatterlist
*sg_dst
, unsigned int sg_len
,
1869 enum dma_data_direction direction
, unsigned long dma_flags
)
1871 struct d40_chan
*chan
= container_of(dchan
, struct d40_chan
, chan
);
1872 dma_addr_t src_dev_addr
= 0;
1873 dma_addr_t dst_dev_addr
= 0;
1874 struct d40_desc
*desc
;
1875 unsigned long flags
;
1878 if (!chan
->phy_chan
) {
1879 chan_err(chan
, "Cannot prepare unallocated channel\n");
1884 spin_lock_irqsave(&chan
->lock
, flags
);
1886 desc
= d40_prep_desc(chan
, sg_src
, sg_len
, dma_flags
);
1890 if (sg_next(&sg_src
[sg_len
- 1]) == sg_src
)
1891 desc
->cyclic
= true;
1893 if (direction
!= DMA_NONE
) {
1894 dma_addr_t dev_addr
= d40_get_dev_addr(chan
, direction
);
1896 if (direction
== DMA_FROM_DEVICE
)
1897 src_dev_addr
= dev_addr
;
1898 else if (direction
== DMA_TO_DEVICE
)
1899 dst_dev_addr
= dev_addr
;
1902 if (chan_is_logical(chan
))
1903 ret
= d40_prep_sg_log(chan
, desc
, sg_src
, sg_dst
,
1904 sg_len
, src_dev_addr
, dst_dev_addr
);
1906 ret
= d40_prep_sg_phy(chan
, desc
, sg_src
, sg_dst
,
1907 sg_len
, src_dev_addr
, dst_dev_addr
);
1910 chan_err(chan
, "Failed to prepare %s sg job: %d\n",
1911 chan_is_logical(chan
) ? "log" : "phy", ret
);
1915 spin_unlock_irqrestore(&chan
->lock
, flags
);
1921 d40_desc_free(chan
, desc
);
1922 spin_unlock_irqrestore(&chan
->lock
, flags
);
1926 bool stedma40_filter(struct dma_chan
*chan
, void *data
)
1928 struct stedma40_chan_cfg
*info
= data
;
1929 struct d40_chan
*d40c
=
1930 container_of(chan
, struct d40_chan
, chan
);
1934 err
= d40_validate_conf(d40c
, info
);
1936 d40c
->dma_cfg
= *info
;
1938 err
= d40_config_memcpy(d40c
);
1941 d40c
->configured
= true;
1945 EXPORT_SYMBOL(stedma40_filter
);
1947 static void __d40_set_prio_rt(struct d40_chan
*d40c
, int dev_type
, bool src
)
1949 bool realtime
= d40c
->dma_cfg
.realtime
;
1950 bool highprio
= d40c
->dma_cfg
.high_priority
;
1951 u32 prioreg
= highprio
? D40_DREG_PSEG1
: D40_DREG_PCEG1
;
1952 u32 rtreg
= realtime
? D40_DREG_RSEG1
: D40_DREG_RCEG1
;
1953 u32 event
= D40_TYPE_TO_EVENT(dev_type
);
1954 u32 group
= D40_TYPE_TO_GROUP(dev_type
);
1955 u32 bit
= 1 << event
;
1957 /* Destination event lines are stored in the upper halfword */
1961 writel(bit
, d40c
->base
->virtbase
+ prioreg
+ group
* 4);
1962 writel(bit
, d40c
->base
->virtbase
+ rtreg
+ group
* 4);
1965 static void d40_set_prio_realtime(struct d40_chan
*d40c
)
1967 if (d40c
->base
->rev
< 3)
1970 if ((d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
) ||
1971 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
))
1972 __d40_set_prio_rt(d40c
, d40c
->dma_cfg
.src_dev_type
, true);
1974 if ((d40c
->dma_cfg
.dir
== STEDMA40_MEM_TO_PERIPH
) ||
1975 (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_PERIPH
))
1976 __d40_set_prio_rt(d40c
, d40c
->dma_cfg
.dst_dev_type
, false);
1979 /* DMA ENGINE functions */
1980 static int d40_alloc_chan_resources(struct dma_chan
*chan
)
1983 unsigned long flags
;
1984 struct d40_chan
*d40c
=
1985 container_of(chan
, struct d40_chan
, chan
);
1987 spin_lock_irqsave(&d40c
->lock
, flags
);
1989 d40c
->completed
= chan
->cookie
= 1;
1991 /* If no dma configuration is set use default configuration (memcpy) */
1992 if (!d40c
->configured
) {
1993 err
= d40_config_memcpy(d40c
);
1995 chan_err(d40c
, "Failed to configure memcpy channel\n");
1999 is_free_phy
= (d40c
->phy_chan
== NULL
);
2001 err
= d40_allocate_channel(d40c
);
2003 chan_err(d40c
, "Failed to allocate channel\n");
2007 /* Fill in basic CFG register values */
2008 d40_phy_cfg(&d40c
->dma_cfg
, &d40c
->src_def_cfg
,
2009 &d40c
->dst_def_cfg
, chan_is_logical(d40c
));
2011 d40_set_prio_realtime(d40c
);
2013 if (chan_is_logical(d40c
)) {
2014 d40_log_cfg(&d40c
->dma_cfg
,
2015 &d40c
->log_def
.lcsp1
, &d40c
->log_def
.lcsp3
);
2017 if (d40c
->dma_cfg
.dir
== STEDMA40_PERIPH_TO_MEM
)
2018 d40c
->lcpa
= d40c
->base
->lcpa_base
+
2019 d40c
->dma_cfg
.src_dev_type
* D40_LCPA_CHAN_SIZE
;
2021 d40c
->lcpa
= d40c
->base
->lcpa_base
+
2022 d40c
->dma_cfg
.dst_dev_type
*
2023 D40_LCPA_CHAN_SIZE
+ D40_LCPA_CHAN_DST_DELTA
;
2027 * Only write channel configuration to the DMA if the physical
2028 * resource is free. In case of multiple logical channels
2029 * on the same physical resource, only the first write is necessary.
2032 d40_config_write(d40c
);
2034 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2038 static void d40_free_chan_resources(struct dma_chan
*chan
)
2040 struct d40_chan
*d40c
=
2041 container_of(chan
, struct d40_chan
, chan
);
2043 unsigned long flags
;
2045 if (d40c
->phy_chan
== NULL
) {
2046 chan_err(d40c
, "Cannot free unallocated channel\n");
2051 spin_lock_irqsave(&d40c
->lock
, flags
);
2053 err
= d40_free_dma(d40c
);
2056 chan_err(d40c
, "Failed to free channel\n");
2057 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2060 static struct dma_async_tx_descriptor
*d40_prep_memcpy(struct dma_chan
*chan
,
2064 unsigned long dma_flags
)
2066 struct scatterlist dst_sg
;
2067 struct scatterlist src_sg
;
2069 sg_init_table(&dst_sg
, 1);
2070 sg_init_table(&src_sg
, 1);
2072 sg_dma_address(&dst_sg
) = dst
;
2073 sg_dma_address(&src_sg
) = src
;
2075 sg_dma_len(&dst_sg
) = size
;
2076 sg_dma_len(&src_sg
) = size
;
2078 return d40_prep_sg(chan
, &src_sg
, &dst_sg
, 1, DMA_NONE
, dma_flags
);
2081 static struct dma_async_tx_descriptor
*
2082 d40_prep_memcpy_sg(struct dma_chan
*chan
,
2083 struct scatterlist
*dst_sg
, unsigned int dst_nents
,
2084 struct scatterlist
*src_sg
, unsigned int src_nents
,
2085 unsigned long dma_flags
)
2087 if (dst_nents
!= src_nents
)
2090 return d40_prep_sg(chan
, src_sg
, dst_sg
, src_nents
, DMA_NONE
, dma_flags
);
2093 static struct dma_async_tx_descriptor
*d40_prep_slave_sg(struct dma_chan
*chan
,
2094 struct scatterlist
*sgl
,
2095 unsigned int sg_len
,
2096 enum dma_data_direction direction
,
2097 unsigned long dma_flags
)
2099 if (direction
!= DMA_FROM_DEVICE
&& direction
!= DMA_TO_DEVICE
)
2102 return d40_prep_sg(chan
, sgl
, sgl
, sg_len
, direction
, dma_flags
);
2105 static struct dma_async_tx_descriptor
*
2106 dma40_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t dma_addr
,
2107 size_t buf_len
, size_t period_len
,
2108 enum dma_data_direction direction
)
2110 unsigned int periods
= buf_len
/ period_len
;
2111 struct dma_async_tx_descriptor
*txd
;
2112 struct scatterlist
*sg
;
2115 sg
= kcalloc(periods
+ 1, sizeof(struct scatterlist
), GFP_NOWAIT
);
2116 for (i
= 0; i
< periods
; i
++) {
2117 sg_dma_address(&sg
[i
]) = dma_addr
;
2118 sg_dma_len(&sg
[i
]) = period_len
;
2119 dma_addr
+= period_len
;
2122 sg
[periods
].offset
= 0;
2123 sg
[periods
].length
= 0;
2124 sg
[periods
].page_link
=
2125 ((unsigned long)sg
| 0x01) & ~0x02;
2127 txd
= d40_prep_sg(chan
, sg
, sg
, periods
, direction
,
2128 DMA_PREP_INTERRUPT
);
2135 static enum dma_status
d40_tx_status(struct dma_chan
*chan
,
2136 dma_cookie_t cookie
,
2137 struct dma_tx_state
*txstate
)
2139 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2140 dma_cookie_t last_used
;
2141 dma_cookie_t last_complete
;
2144 if (d40c
->phy_chan
== NULL
) {
2145 chan_err(d40c
, "Cannot read status of unallocated channel\n");
2149 last_complete
= d40c
->completed
;
2150 last_used
= chan
->cookie
;
2152 if (d40_is_paused(d40c
))
2155 ret
= dma_async_is_complete(cookie
, last_complete
, last_used
);
2157 dma_set_tx_state(txstate
, last_complete
, last_used
,
2158 stedma40_residue(chan
));
2163 static void d40_issue_pending(struct dma_chan
*chan
)
2165 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2166 unsigned long flags
;
2168 if (d40c
->phy_chan
== NULL
) {
2169 chan_err(d40c
, "Channel is not allocated!\n");
2173 spin_lock_irqsave(&d40c
->lock
, flags
);
2175 list_splice_tail_init(&d40c
->pending_queue
, &d40c
->queue
);
2177 /* Busy means that queued jobs are already being processed */
2179 (void) d40_queue_start(d40c
);
2181 spin_unlock_irqrestore(&d40c
->lock
, flags
);
2185 dma40_config_to_halfchannel(struct d40_chan
*d40c
,
2186 struct stedma40_half_channel_info
*info
,
2187 enum dma_slave_buswidth width
,
2190 enum stedma40_periph_data_width addr_width
;
2194 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
2195 addr_width
= STEDMA40_BYTE_WIDTH
;
2197 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
2198 addr_width
= STEDMA40_HALFWORD_WIDTH
;
2200 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
2201 addr_width
= STEDMA40_WORD_WIDTH
;
2203 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
2204 addr_width
= STEDMA40_DOUBLEWORD_WIDTH
;
2207 dev_err(d40c
->base
->dev
,
2208 "illegal peripheral address width "
2214 if (chan_is_logical(d40c
)) {
2216 psize
= STEDMA40_PSIZE_LOG_16
;
2217 else if (maxburst
>= 8)
2218 psize
= STEDMA40_PSIZE_LOG_8
;
2219 else if (maxburst
>= 4)
2220 psize
= STEDMA40_PSIZE_LOG_4
;
2222 psize
= STEDMA40_PSIZE_LOG_1
;
2225 psize
= STEDMA40_PSIZE_PHY_16
;
2226 else if (maxburst
>= 8)
2227 psize
= STEDMA40_PSIZE_PHY_8
;
2228 else if (maxburst
>= 4)
2229 psize
= STEDMA40_PSIZE_PHY_4
;
2231 psize
= STEDMA40_PSIZE_PHY_1
;
2234 info
->data_width
= addr_width
;
2235 info
->psize
= psize
;
2236 info
->flow_ctrl
= STEDMA40_NO_FLOW_CTRL
;
2241 /* Runtime reconfiguration extension */
2242 static int d40_set_runtime_config(struct dma_chan
*chan
,
2243 struct dma_slave_config
*config
)
2245 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2246 struct stedma40_chan_cfg
*cfg
= &d40c
->dma_cfg
;
2247 enum dma_slave_buswidth src_addr_width
, dst_addr_width
;
2248 dma_addr_t config_addr
;
2249 u32 src_maxburst
, dst_maxburst
;
2252 src_addr_width
= config
->src_addr_width
;
2253 src_maxburst
= config
->src_maxburst
;
2254 dst_addr_width
= config
->dst_addr_width
;
2255 dst_maxburst
= config
->dst_maxburst
;
2257 if (config
->direction
== DMA_FROM_DEVICE
) {
2258 dma_addr_t dev_addr_rx
=
2259 d40c
->base
->plat_data
->dev_rx
[cfg
->src_dev_type
];
2261 config_addr
= config
->src_addr
;
2263 dev_dbg(d40c
->base
->dev
,
2264 "channel has a pre-wired RX address %08x "
2265 "overriding with %08x\n",
2266 dev_addr_rx
, config_addr
);
2267 if (cfg
->dir
!= STEDMA40_PERIPH_TO_MEM
)
2268 dev_dbg(d40c
->base
->dev
,
2269 "channel was not configured for peripheral "
2270 "to memory transfer (%d) overriding\n",
2272 cfg
->dir
= STEDMA40_PERIPH_TO_MEM
;
2274 /* Configure the memory side */
2275 if (dst_addr_width
== DMA_SLAVE_BUSWIDTH_UNDEFINED
)
2276 dst_addr_width
= src_addr_width
;
2277 if (dst_maxburst
== 0)
2278 dst_maxburst
= src_maxburst
;
2280 } else if (config
->direction
== DMA_TO_DEVICE
) {
2281 dma_addr_t dev_addr_tx
=
2282 d40c
->base
->plat_data
->dev_tx
[cfg
->dst_dev_type
];
2284 config_addr
= config
->dst_addr
;
2286 dev_dbg(d40c
->base
->dev
,
2287 "channel has a pre-wired TX address %08x "
2288 "overriding with %08x\n",
2289 dev_addr_tx
, config_addr
);
2290 if (cfg
->dir
!= STEDMA40_MEM_TO_PERIPH
)
2291 dev_dbg(d40c
->base
->dev
,
2292 "channel was not configured for memory "
2293 "to peripheral transfer (%d) overriding\n",
2295 cfg
->dir
= STEDMA40_MEM_TO_PERIPH
;
2297 /* Configure the memory side */
2298 if (src_addr_width
== DMA_SLAVE_BUSWIDTH_UNDEFINED
)
2299 src_addr_width
= dst_addr_width
;
2300 if (src_maxburst
== 0)
2301 src_maxburst
= dst_maxburst
;
2303 dev_err(d40c
->base
->dev
,
2304 "unrecognized channel direction %d\n",
2309 if (src_maxburst
* src_addr_width
!= dst_maxburst
* dst_addr_width
) {
2310 dev_err(d40c
->base
->dev
,
2311 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2319 ret
= dma40_config_to_halfchannel(d40c
, &cfg
->src_info
,
2325 ret
= dma40_config_to_halfchannel(d40c
, &cfg
->dst_info
,
2331 /* Fill in register values */
2332 if (chan_is_logical(d40c
))
2333 d40_log_cfg(cfg
, &d40c
->log_def
.lcsp1
, &d40c
->log_def
.lcsp3
);
2335 d40_phy_cfg(cfg
, &d40c
->src_def_cfg
,
2336 &d40c
->dst_def_cfg
, false);
2338 /* These settings will take precedence later */
2339 d40c
->runtime_addr
= config_addr
;
2340 d40c
->runtime_direction
= config
->direction
;
2341 dev_dbg(d40c
->base
->dev
,
2342 "configured channel %s for %s, data width %d/%d, "
2343 "maxburst %d/%d elements, LE, no flow control\n",
2344 dma_chan_name(chan
),
2345 (config
->direction
== DMA_FROM_DEVICE
) ? "RX" : "TX",
2346 src_addr_width
, dst_addr_width
,
2347 src_maxburst
, dst_maxburst
);
2352 static int d40_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
2355 struct d40_chan
*d40c
= container_of(chan
, struct d40_chan
, chan
);
2357 if (d40c
->phy_chan
== NULL
) {
2358 chan_err(d40c
, "Channel is not allocated!\n");
2363 case DMA_TERMINATE_ALL
:
2364 return d40_terminate_all(d40c
);
2366 return d40_pause(d40c
);
2368 return d40_resume(d40c
);
2369 case DMA_SLAVE_CONFIG
:
2370 return d40_set_runtime_config(chan
,
2371 (struct dma_slave_config
*) arg
);
2376 /* Other commands are unimplemented */
2380 /* Initialization functions */
2382 static void __init
d40_chan_init(struct d40_base
*base
, struct dma_device
*dma
,
2383 struct d40_chan
*chans
, int offset
,
2387 struct d40_chan
*d40c
;
2389 INIT_LIST_HEAD(&dma
->channels
);
2391 for (i
= offset
; i
< offset
+ num_chans
; i
++) {
2394 d40c
->chan
.device
= dma
;
2396 spin_lock_init(&d40c
->lock
);
2398 d40c
->log_num
= D40_PHY_CHAN
;
2400 INIT_LIST_HEAD(&d40c
->active
);
2401 INIT_LIST_HEAD(&d40c
->queue
);
2402 INIT_LIST_HEAD(&d40c
->pending_queue
);
2403 INIT_LIST_HEAD(&d40c
->client
);
2405 tasklet_init(&d40c
->tasklet
, dma_tasklet
,
2406 (unsigned long) d40c
);
2408 list_add_tail(&d40c
->chan
.device_node
,
2413 static void d40_ops_init(struct d40_base
*base
, struct dma_device
*dev
)
2415 if (dma_has_cap(DMA_SLAVE
, dev
->cap_mask
))
2416 dev
->device_prep_slave_sg
= d40_prep_slave_sg
;
2418 if (dma_has_cap(DMA_MEMCPY
, dev
->cap_mask
)) {
2419 dev
->device_prep_dma_memcpy
= d40_prep_memcpy
;
2422 * This controller can only access address at even
2423 * 32bit boundaries, i.e. 2^2
2425 dev
->copy_align
= 2;
2428 if (dma_has_cap(DMA_SG
, dev
->cap_mask
))
2429 dev
->device_prep_dma_sg
= d40_prep_memcpy_sg
;
2431 if (dma_has_cap(DMA_CYCLIC
, dev
->cap_mask
))
2432 dev
->device_prep_dma_cyclic
= dma40_prep_dma_cyclic
;
2434 dev
->device_alloc_chan_resources
= d40_alloc_chan_resources
;
2435 dev
->device_free_chan_resources
= d40_free_chan_resources
;
2436 dev
->device_issue_pending
= d40_issue_pending
;
2437 dev
->device_tx_status
= d40_tx_status
;
2438 dev
->device_control
= d40_control
;
2439 dev
->dev
= base
->dev
;
2442 static int __init
d40_dmaengine_init(struct d40_base
*base
,
2443 int num_reserved_chans
)
2447 d40_chan_init(base
, &base
->dma_slave
, base
->log_chans
,
2448 0, base
->num_log_chans
);
2450 dma_cap_zero(base
->dma_slave
.cap_mask
);
2451 dma_cap_set(DMA_SLAVE
, base
->dma_slave
.cap_mask
);
2452 dma_cap_set(DMA_CYCLIC
, base
->dma_slave
.cap_mask
);
2454 d40_ops_init(base
, &base
->dma_slave
);
2456 err
= dma_async_device_register(&base
->dma_slave
);
2459 d40_err(base
->dev
, "Failed to register slave channels\n");
2463 d40_chan_init(base
, &base
->dma_memcpy
, base
->log_chans
,
2464 base
->num_log_chans
, base
->plat_data
->memcpy_len
);
2466 dma_cap_zero(base
->dma_memcpy
.cap_mask
);
2467 dma_cap_set(DMA_MEMCPY
, base
->dma_memcpy
.cap_mask
);
2468 dma_cap_set(DMA_SG
, base
->dma_memcpy
.cap_mask
);
2470 d40_ops_init(base
, &base
->dma_memcpy
);
2472 err
= dma_async_device_register(&base
->dma_memcpy
);
2476 "Failed to regsiter memcpy only channels\n");
2480 d40_chan_init(base
, &base
->dma_both
, base
->phy_chans
,
2481 0, num_reserved_chans
);
2483 dma_cap_zero(base
->dma_both
.cap_mask
);
2484 dma_cap_set(DMA_SLAVE
, base
->dma_both
.cap_mask
);
2485 dma_cap_set(DMA_MEMCPY
, base
->dma_both
.cap_mask
);
2486 dma_cap_set(DMA_SG
, base
->dma_both
.cap_mask
);
2487 dma_cap_set(DMA_CYCLIC
, base
->dma_slave
.cap_mask
);
2489 d40_ops_init(base
, &base
->dma_both
);
2490 err
= dma_async_device_register(&base
->dma_both
);
2494 "Failed to register logical and physical capable channels\n");
2499 dma_async_device_unregister(&base
->dma_memcpy
);
2501 dma_async_device_unregister(&base
->dma_slave
);
2506 /* Initialization functions. */
2508 static int __init
d40_phy_res_init(struct d40_base
*base
)
2511 int num_phy_chans_avail
= 0;
2513 int odd_even_bit
= -2;
2515 val
[0] = readl(base
->virtbase
+ D40_DREG_PRSME
);
2516 val
[1] = readl(base
->virtbase
+ D40_DREG_PRSMO
);
2518 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2519 base
->phy_res
[i
].num
= i
;
2520 odd_even_bit
+= 2 * ((i
% 2) == 0);
2521 if (((val
[i
% 2] >> odd_even_bit
) & 3) == 1) {
2522 /* Mark security only channels as occupied */
2523 base
->phy_res
[i
].allocated_src
= D40_ALLOC_PHY
;
2524 base
->phy_res
[i
].allocated_dst
= D40_ALLOC_PHY
;
2526 base
->phy_res
[i
].allocated_src
= D40_ALLOC_FREE
;
2527 base
->phy_res
[i
].allocated_dst
= D40_ALLOC_FREE
;
2528 num_phy_chans_avail
++;
2530 spin_lock_init(&base
->phy_res
[i
].lock
);
2533 /* Mark disabled channels as occupied */
2534 for (i
= 0; base
->plat_data
->disabled_channels
[i
] != -1; i
++) {
2535 int chan
= base
->plat_data
->disabled_channels
[i
];
2537 base
->phy_res
[chan
].allocated_src
= D40_ALLOC_PHY
;
2538 base
->phy_res
[chan
].allocated_dst
= D40_ALLOC_PHY
;
2539 num_phy_chans_avail
--;
2542 dev_info(base
->dev
, "%d of %d physical DMA channels available\n",
2543 num_phy_chans_avail
, base
->num_phy_chans
);
2545 /* Verify settings extended vs standard */
2546 val
[0] = readl(base
->virtbase
+ D40_DREG_PRTYP
);
2548 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2550 if (base
->phy_res
[i
].allocated_src
== D40_ALLOC_FREE
&&
2551 (val
[0] & 0x3) != 1)
2553 "[%s] INFO: channel %d is misconfigured (%d)\n",
2554 __func__
, i
, val
[0] & 0x3);
2556 val
[0] = val
[0] >> 2;
2559 return num_phy_chans_avail
;
2562 static struct d40_base
* __init
d40_hw_detect_init(struct platform_device
*pdev
)
2564 struct stedma40_platform_data
*plat_data
;
2565 struct clk
*clk
= NULL
;
2566 void __iomem
*virtbase
= NULL
;
2567 struct resource
*res
= NULL
;
2568 struct d40_base
*base
= NULL
;
2569 int num_log_chans
= 0;
2576 clk
= clk_get(&pdev
->dev
, NULL
);
2579 d40_err(&pdev
->dev
, "No matching clock found\n");
2585 /* Get IO for DMAC base address */
2586 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "base");
2590 if (request_mem_region(res
->start
, resource_size(res
),
2591 D40_NAME
" I/O base") == NULL
)
2594 virtbase
= ioremap(res
->start
, resource_size(res
));
2598 /* This is just a regular AMBA PrimeCell ID actually */
2599 for (pid
= 0, i
= 0; i
< 4; i
++)
2600 pid
|= (readl(virtbase
+ resource_size(res
) - 0x20 + 4 * i
)
2602 for (cid
= 0, i
= 0; i
< 4; i
++)
2603 cid
|= (readl(virtbase
+ resource_size(res
) - 0x10 + 4 * i
)
2606 if (cid
!= AMBA_CID
) {
2607 d40_err(&pdev
->dev
, "Unknown hardware! No PrimeCell ID\n");
2610 if (AMBA_MANF_BITS(pid
) != AMBA_VENDOR_ST
) {
2611 d40_err(&pdev
->dev
, "Unknown designer! Got %x wanted %x\n",
2612 AMBA_MANF_BITS(pid
),
2618 * DB8500ed has revision 0
2620 * DB8500v1 has revision 2
2621 * DB8500v2 has revision 3
2623 rev
= AMBA_REV_BITS(pid
);
2625 /* The number of physical channels on this HW */
2626 num_phy_chans
= 4 * (readl(virtbase
+ D40_DREG_ICFG
) & 0x7) + 4;
2628 dev_info(&pdev
->dev
, "hardware revision: %d @ 0x%x\n",
2631 plat_data
= pdev
->dev
.platform_data
;
2633 /* Count the number of logical channels in use */
2634 for (i
= 0; i
< plat_data
->dev_len
; i
++)
2635 if (plat_data
->dev_rx
[i
] != 0)
2638 for (i
= 0; i
< plat_data
->dev_len
; i
++)
2639 if (plat_data
->dev_tx
[i
] != 0)
2642 base
= kzalloc(ALIGN(sizeof(struct d40_base
), 4) +
2643 (num_phy_chans
+ num_log_chans
+ plat_data
->memcpy_len
) *
2644 sizeof(struct d40_chan
), GFP_KERNEL
);
2647 d40_err(&pdev
->dev
, "Out of memory\n");
2653 base
->num_phy_chans
= num_phy_chans
;
2654 base
->num_log_chans
= num_log_chans
;
2655 base
->phy_start
= res
->start
;
2656 base
->phy_size
= resource_size(res
);
2657 base
->virtbase
= virtbase
;
2658 base
->plat_data
= plat_data
;
2659 base
->dev
= &pdev
->dev
;
2660 base
->phy_chans
= ((void *)base
) + ALIGN(sizeof(struct d40_base
), 4);
2661 base
->log_chans
= &base
->phy_chans
[num_phy_chans
];
2663 base
->phy_res
= kzalloc(num_phy_chans
* sizeof(struct d40_phy_res
),
2668 base
->lookup_phy_chans
= kzalloc(num_phy_chans
*
2669 sizeof(struct d40_chan
*),
2671 if (!base
->lookup_phy_chans
)
2674 if (num_log_chans
+ plat_data
->memcpy_len
) {
2676 * The max number of logical channels are event lines for all
2677 * src devices and dst devices
2679 base
->lookup_log_chans
= kzalloc(plat_data
->dev_len
* 2 *
2680 sizeof(struct d40_chan
*),
2682 if (!base
->lookup_log_chans
)
2686 base
->lcla_pool
.alloc_map
= kzalloc(num_phy_chans
*
2687 sizeof(struct d40_desc
*) *
2688 D40_LCLA_LINK_PER_EVENT_GRP
,
2690 if (!base
->lcla_pool
.alloc_map
)
2693 base
->desc_slab
= kmem_cache_create(D40_NAME
, sizeof(struct d40_desc
),
2694 0, SLAB_HWCACHE_ALIGN
,
2696 if (base
->desc_slab
== NULL
)
2709 release_mem_region(res
->start
,
2710 resource_size(res
));
2715 kfree(base
->lcla_pool
.alloc_map
);
2716 kfree(base
->lookup_log_chans
);
2717 kfree(base
->lookup_phy_chans
);
2718 kfree(base
->phy_res
);
2725 static void __init
d40_hw_init(struct d40_base
*base
)
2728 static const struct d40_reg_val dma_init_reg
[] = {
2729 /* Clock every part of the DMA block from start */
2730 { .reg
= D40_DREG_GCC
, .val
= 0x0000ff01},
2732 /* Interrupts on all logical channels */
2733 { .reg
= D40_DREG_LCMIS0
, .val
= 0xFFFFFFFF},
2734 { .reg
= D40_DREG_LCMIS1
, .val
= 0xFFFFFFFF},
2735 { .reg
= D40_DREG_LCMIS2
, .val
= 0xFFFFFFFF},
2736 { .reg
= D40_DREG_LCMIS3
, .val
= 0xFFFFFFFF},
2737 { .reg
= D40_DREG_LCICR0
, .val
= 0xFFFFFFFF},
2738 { .reg
= D40_DREG_LCICR1
, .val
= 0xFFFFFFFF},
2739 { .reg
= D40_DREG_LCICR2
, .val
= 0xFFFFFFFF},
2740 { .reg
= D40_DREG_LCICR3
, .val
= 0xFFFFFFFF},
2741 { .reg
= D40_DREG_LCTIS0
, .val
= 0xFFFFFFFF},
2742 { .reg
= D40_DREG_LCTIS1
, .val
= 0xFFFFFFFF},
2743 { .reg
= D40_DREG_LCTIS2
, .val
= 0xFFFFFFFF},
2744 { .reg
= D40_DREG_LCTIS3
, .val
= 0xFFFFFFFF}
2747 u32 prmseo
[2] = {0, 0};
2748 u32 activeo
[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2752 for (i
= 0; i
< ARRAY_SIZE(dma_init_reg
); i
++)
2753 writel(dma_init_reg
[i
].val
,
2754 base
->virtbase
+ dma_init_reg
[i
].reg
);
2756 /* Configure all our dma channels to default settings */
2757 for (i
= 0; i
< base
->num_phy_chans
; i
++) {
2759 activeo
[i
% 2] = activeo
[i
% 2] << 2;
2761 if (base
->phy_res
[base
->num_phy_chans
- i
- 1].allocated_src
2763 activeo
[i
% 2] |= 3;
2767 /* Enable interrupt # */
2768 pcmis
= (pcmis
<< 1) | 1;
2770 /* Clear interrupt # */
2771 pcicr
= (pcicr
<< 1) | 1;
2773 /* Set channel to physical mode */
2774 prmseo
[i
% 2] = prmseo
[i
% 2] << 2;
2779 writel(prmseo
[1], base
->virtbase
+ D40_DREG_PRMSE
);
2780 writel(prmseo
[0], base
->virtbase
+ D40_DREG_PRMSO
);
2781 writel(activeo
[1], base
->virtbase
+ D40_DREG_ACTIVE
);
2782 writel(activeo
[0], base
->virtbase
+ D40_DREG_ACTIVO
);
2784 /* Write which interrupt to enable */
2785 writel(pcmis
, base
->virtbase
+ D40_DREG_PCMIS
);
2787 /* Write which interrupt to clear */
2788 writel(pcicr
, base
->virtbase
+ D40_DREG_PCICR
);
2792 static int __init
d40_lcla_allocate(struct d40_base
*base
)
2794 struct d40_lcla_pool
*pool
= &base
->lcla_pool
;
2795 unsigned long *page_list
;
2800 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2801 * To full fill this hardware requirement without wasting 256 kb
2802 * we allocate pages until we get an aligned one.
2804 page_list
= kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS
,
2812 /* Calculating how many pages that are required */
2813 base
->lcla_pool
.pages
= SZ_1K
* base
->num_phy_chans
/ PAGE_SIZE
;
2815 for (i
= 0; i
< MAX_LCLA_ALLOC_ATTEMPTS
; i
++) {
2816 page_list
[i
] = __get_free_pages(GFP_KERNEL
,
2817 base
->lcla_pool
.pages
);
2818 if (!page_list
[i
]) {
2820 d40_err(base
->dev
, "Failed to allocate %d pages.\n",
2821 base
->lcla_pool
.pages
);
2823 for (j
= 0; j
< i
; j
++)
2824 free_pages(page_list
[j
], base
->lcla_pool
.pages
);
2828 if ((virt_to_phys((void *)page_list
[i
]) &
2829 (LCLA_ALIGNMENT
- 1)) == 0)
2833 for (j
= 0; j
< i
; j
++)
2834 free_pages(page_list
[j
], base
->lcla_pool
.pages
);
2836 if (i
< MAX_LCLA_ALLOC_ATTEMPTS
) {
2837 base
->lcla_pool
.base
= (void *)page_list
[i
];
2840 * After many attempts and no succees with finding the correct
2841 * alignment, try with allocating a big buffer.
2844 "[%s] Failed to get %d pages @ 18 bit align.\n",
2845 __func__
, base
->lcla_pool
.pages
);
2846 base
->lcla_pool
.base_unaligned
= kmalloc(SZ_1K
*
2847 base
->num_phy_chans
+
2850 if (!base
->lcla_pool
.base_unaligned
) {
2855 base
->lcla_pool
.base
= PTR_ALIGN(base
->lcla_pool
.base_unaligned
,
2859 pool
->dma_addr
= dma_map_single(base
->dev
, pool
->base
,
2860 SZ_1K
* base
->num_phy_chans
,
2862 if (dma_mapping_error(base
->dev
, pool
->dma_addr
)) {
2868 writel(virt_to_phys(base
->lcla_pool
.base
),
2869 base
->virtbase
+ D40_DREG_LCLA
);
2875 static int __init
d40_probe(struct platform_device
*pdev
)
2879 struct d40_base
*base
;
2880 struct resource
*res
= NULL
;
2881 int num_reserved_chans
;
2884 base
= d40_hw_detect_init(pdev
);
2889 num_reserved_chans
= d40_phy_res_init(base
);
2891 platform_set_drvdata(pdev
, base
);
2893 spin_lock_init(&base
->interrupt_lock
);
2894 spin_lock_init(&base
->execmd_lock
);
2896 /* Get IO for logical channel parameter address */
2897 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "lcpa");
2900 d40_err(&pdev
->dev
, "No \"lcpa\" memory resource\n");
2903 base
->lcpa_size
= resource_size(res
);
2904 base
->phy_lcpa
= res
->start
;
2906 if (request_mem_region(res
->start
, resource_size(res
),
2907 D40_NAME
" I/O lcpa") == NULL
) {
2910 "Failed to request LCPA region 0x%x-0x%x\n",
2911 res
->start
, res
->end
);
2915 /* We make use of ESRAM memory for this. */
2916 val
= readl(base
->virtbase
+ D40_DREG_LCPA
);
2917 if (res
->start
!= val
&& val
!= 0) {
2918 dev_warn(&pdev
->dev
,
2919 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2920 __func__
, val
, res
->start
);
2922 writel(res
->start
, base
->virtbase
+ D40_DREG_LCPA
);
2924 base
->lcpa_base
= ioremap(res
->start
, resource_size(res
));
2925 if (!base
->lcpa_base
) {
2927 d40_err(&pdev
->dev
, "Failed to ioremap LCPA region\n");
2931 ret
= d40_lcla_allocate(base
);
2933 d40_err(&pdev
->dev
, "Failed to allocate LCLA area\n");
2937 spin_lock_init(&base
->lcla_pool
.lock
);
2939 base
->irq
= platform_get_irq(pdev
, 0);
2941 ret
= request_irq(base
->irq
, d40_handle_interrupt
, 0, D40_NAME
, base
);
2943 d40_err(&pdev
->dev
, "No IRQ defined\n");
2947 err
= d40_dmaengine_init(base
, num_reserved_chans
);
2953 dev_info(base
->dev
, "initialized\n");
2958 if (base
->desc_slab
)
2959 kmem_cache_destroy(base
->desc_slab
);
2961 iounmap(base
->virtbase
);
2963 if (base
->lcla_pool
.dma_addr
)
2964 dma_unmap_single(base
->dev
, base
->lcla_pool
.dma_addr
,
2965 SZ_1K
* base
->num_phy_chans
,
2968 if (!base
->lcla_pool
.base_unaligned
&& base
->lcla_pool
.base
)
2969 free_pages((unsigned long)base
->lcla_pool
.base
,
2970 base
->lcla_pool
.pages
);
2972 kfree(base
->lcla_pool
.base_unaligned
);
2975 release_mem_region(base
->phy_lcpa
,
2977 if (base
->phy_start
)
2978 release_mem_region(base
->phy_start
,
2981 clk_disable(base
->clk
);
2985 kfree(base
->lcla_pool
.alloc_map
);
2986 kfree(base
->lookup_log_chans
);
2987 kfree(base
->lookup_phy_chans
);
2988 kfree(base
->phy_res
);
2992 d40_err(&pdev
->dev
, "probe failed\n");
2996 static struct platform_driver d40_driver
= {
2998 .owner
= THIS_MODULE
,
3003 static int __init
stedma40_init(void)
3005 return platform_driver_probe(&d40_driver
, d40_probe
);
3007 subsys_initcall(stedma40_init
);