1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Ericsson AB 2007-2008
4 * Copyright (C) ST-Ericsson SA 2008-2010
5 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
6 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
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/log2.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/err.h>
22 #include <linux/of_address.h>
23 #include <linux/of_dma.h>
24 #include <linux/amba/bus.h>
25 #include <linux/regulator/consumer.h>
27 #include "dmaengine.h"
28 #include "ste_dma40.h"
29 #include "ste_dma40_ll.h"
32 * struct stedma40_platform_data - Configuration struct for the dma device.
34 * @dev_tx: mapping between destination event line and io address
35 * @dev_rx: mapping between source event line and io address
36 * @disabled_channels: A vector, ending with -1, that marks physical channels
37 * that are for different reasons not available for the driver.
38 * @soft_lli_chans: A vector, that marks physical channels will use LLI by SW
39 * which avoids HW bug that exists in some versions of the controller.
40 * SoftLLI introduces relink overhead that could impact performace for
42 * @num_of_soft_lli_chans: The number of channels that needs to be configured
44 * @use_esram_lcla: flag for mapping the lcla into esram region
45 * @num_of_memcpy_chans: The number of channels reserved for memcpy.
46 * @num_of_phy_chans: The number of physical channels implemented in HW.
47 * 0 means reading the number of channels from DMA HW but this is only valid
48 * for 'multiple of 4' channels, like 8.
50 struct stedma40_platform_data {
51 int disabled_channels[STEDMA40_MAX_PHYS];
53 int num_of_soft_lli_chans;
55 int num_of_memcpy_chans;
59 #define D40_NAME "dma40"
61 #define D40_PHY_CHAN -1
63 /* For masking out/in 2 bit channel positions */
64 #define D40_CHAN_POS(chan) (2 * (chan / 2))
65 #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
67 /* Maximum iterations taken before giving up suspending a channel */
68 #define D40_SUSPEND_MAX_IT 500
71 #define DMA40_AUTOSUSPEND_DELAY 100
73 /* Hardware requirement on LCLA alignment */
74 #define LCLA_ALIGNMENT 0x40000
76 /* Max number of links per event group */
77 #define D40_LCLA_LINK_PER_EVENT_GRP 128
78 #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
80 /* Max number of logical channels per physical channel */
81 #define D40_MAX_LOG_CHAN_PER_PHY 32
83 /* Attempts before giving up to trying to get pages that are aligned */
84 #define MAX_LCLA_ALLOC_ATTEMPTS 256
86 /* Bit markings for allocation map */
87 #define D40_ALLOC_FREE BIT(31)
88 #define D40_ALLOC_PHY BIT(30)
89 #define D40_ALLOC_LOG_FREE 0
91 #define D40_MEMCPY_MAX_CHANS 8
93 /* Reserved event lines for memcpy only. */
94 #define DB8500_DMA_MEMCPY_EV_0 51
95 #define DB8500_DMA_MEMCPY_EV_1 56
96 #define DB8500_DMA_MEMCPY_EV_2 57
97 #define DB8500_DMA_MEMCPY_EV_3 58
98 #define DB8500_DMA_MEMCPY_EV_4 59
99 #define DB8500_DMA_MEMCPY_EV_5 60
101 static int dma40_memcpy_channels[] = {
102 DB8500_DMA_MEMCPY_EV_0,
103 DB8500_DMA_MEMCPY_EV_1,
104 DB8500_DMA_MEMCPY_EV_2,
105 DB8500_DMA_MEMCPY_EV_3,
106 DB8500_DMA_MEMCPY_EV_4,
107 DB8500_DMA_MEMCPY_EV_5,
110 /* Default configuration for physical memcpy */
111 static const struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
112 .mode = STEDMA40_MODE_PHYSICAL,
113 .dir = DMA_MEM_TO_MEM,
115 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
116 .src_info.psize = STEDMA40_PSIZE_PHY_1,
117 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
119 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
120 .dst_info.psize = STEDMA40_PSIZE_PHY_1,
121 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
124 /* Default configuration for logical memcpy */
125 static const struct stedma40_chan_cfg dma40_memcpy_conf_log = {
126 .mode = STEDMA40_MODE_LOGICAL,
127 .dir = DMA_MEM_TO_MEM,
129 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
130 .src_info.psize = STEDMA40_PSIZE_LOG_1,
131 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
133 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
134 .dst_info.psize = STEDMA40_PSIZE_LOG_1,
135 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
139 * enum d40_command - The different commands and/or statuses.
141 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
142 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
143 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
144 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
149 D40_DMA_SUSPEND_REQ = 2,
150 D40_DMA_SUSPENDED = 3
154 * enum d40_events - The different Event Enables for the event lines.
156 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
157 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
158 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
159 * @D40_ROUND_EVENTLINE: Status check for event line.
163 D40_DEACTIVATE_EVENTLINE = 0,
164 D40_ACTIVATE_EVENTLINE = 1,
165 D40_SUSPEND_REQ_EVENTLINE = 2,
166 D40_ROUND_EVENTLINE = 3
170 * These are the registers that has to be saved and later restored
171 * when the DMA hw is powered off.
172 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
174 static __maybe_unused u32 d40_backup_regs[] = {
183 #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
186 * since 9540 and 8540 has the same HW revision
187 * use v4a for 9540 or ealier
188 * use v4b for 8540 or later
190 * DB8500ed has revision 0
191 * DB8500v1 has revision 2
192 * DB8500v2 has revision 3
193 * AP9540v1 has revision 4
194 * DB8540v1 has revision 4
195 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
197 static u32 d40_backup_regs_v4a[] = {
216 #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
218 static u32 d40_backup_regs_v4b[] = {
241 #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
243 static __maybe_unused u32 d40_backup_regs_chan[] = {
254 #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
255 BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
258 * struct d40_interrupt_lookup - lookup table for interrupt handler
260 * @src: Interrupt mask register.
261 * @clr: Interrupt clear register.
262 * @is_error: true if this is an error interrupt.
263 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
264 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
266 struct d40_interrupt_lookup {
274 static struct d40_interrupt_lookup il_v4a[] = {
275 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
276 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
277 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
278 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
279 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
280 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
281 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
282 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
283 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
284 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
287 static struct d40_interrupt_lookup il_v4b[] = {
288 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
289 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
290 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
291 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
292 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
293 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
294 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
295 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
296 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
297 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
298 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
299 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
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 __initdata struct d40_reg_val dma_init_reg_v4a[] = {
314 /* Clock every part of the DMA block from start */
315 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
317 /* Interrupts on all logical channels */
318 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
319 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
320 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
321 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
322 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
323 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
324 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
325 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
326 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
327 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
328 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
329 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
331 static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
332 /* Clock every part of the DMA block from start */
333 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
335 /* Interrupts on all logical channels */
336 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
337 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
338 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
339 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
340 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
341 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
342 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
343 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
344 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
345 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
346 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
347 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
348 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
349 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
350 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
354 * struct d40_lli_pool - Structure for keeping LLIs in memory
356 * @base: Pointer to memory area when the pre_alloc_lli's are not large
357 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
358 * pre_alloc_lli is used.
359 * @dma_addr: DMA address, if mapped
360 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
361 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
362 * one buffer to one buffer.
364 struct d40_lli_pool {
368 /* Space for dst and src, plus an extra for padding */
369 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
373 * struct d40_desc - A descriptor is one DMA job.
375 * @lli_phy: LLI settings for physical channel. Both src and dst=
376 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
377 * lli_len equals one.
378 * @lli_log: Same as above but for logical channels.
379 * @lli_pool: The pool with two entries pre-allocated.
380 * @lli_len: Number of llis of current descriptor.
381 * @lli_current: Number of transferred llis.
382 * @lcla_alloc: Number of LCLA entries allocated.
383 * @txd: DMA engine struct. Used for among other things for communication
386 * @is_in_client_list: true if the client owns this descriptor.
387 * @cyclic: true if this is a cyclic job
389 * This descriptor is used for both logical and physical transfers.
393 struct d40_phy_lli_bidir lli_phy;
395 struct d40_log_lli_bidir lli_log;
397 struct d40_lli_pool lli_pool;
402 struct dma_async_tx_descriptor txd;
403 struct list_head node;
405 bool is_in_client_list;
410 * struct d40_lcla_pool - LCLA pool settings and data.
412 * @base: The virtual address of LCLA. 18 bit aligned.
413 * @dma_addr: DMA address, if mapped
414 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
415 * This pointer is only there for clean-up on error.
416 * @pages: The number of pages needed for all physical channels.
417 * Only used later for clean-up on error
418 * @lock: Lock to protect the content in this struct.
419 * @alloc_map: big map over which LCLA entry is own by which job.
421 struct d40_lcla_pool {
424 void *base_unaligned;
427 struct d40_desc **alloc_map;
431 * struct d40_phy_res - struct for handling eventlines mapped to physical
434 * @lock: A lock protection this entity.
435 * @reserved: True if used by secure world or otherwise.
436 * @num: The physical channel number of this entity.
437 * @allocated_src: Bit mapped to show which src event line's are mapped to
438 * this physical channel. Can also be free or physically allocated.
439 * @allocated_dst: Same as for src but is dst.
440 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
442 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
456 * struct d40_chan - Struct that describes a channel.
458 * @lock: A spinlock to protect this struct.
459 * @log_num: The logical number, if any of this channel.
460 * @pending_tx: The number of pending transfers. Used between interrupt handler
462 * @busy: Set to true when transfer is ongoing on this channel.
463 * @phy_chan: Pointer to physical channel which this instance runs on. If this
464 * point is NULL, then the channel is not allocated.
465 * @chan: DMA engine handle.
466 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
467 * transfer and call client callback.
468 * @client: Cliented owned descriptor list.
469 * @pending_queue: Submitted jobs, to be issued by issue_pending()
470 * @active: Active descriptor.
471 * @done: Completed jobs
472 * @queue: Queued jobs.
473 * @prepare_queue: Prepared jobs.
474 * @dma_cfg: The client configuration of this dma channel.
475 * @slave_config: DMA slave configuration.
476 * @configured: whether the dma_cfg configuration is valid
477 * @base: Pointer to the device instance struct.
478 * @src_def_cfg: Default cfg register setting for src.
479 * @dst_def_cfg: Default cfg register setting for dst.
480 * @log_def: Default logical channel settings.
481 * @lcpa: Pointer to dst and src lcpa settings.
482 * @runtime_addr: runtime configured address.
483 * @runtime_direction: runtime configured direction.
485 * This struct can either "be" a logical or a physical channel.
492 struct d40_phy_res *phy_chan;
493 struct dma_chan chan;
494 struct tasklet_struct tasklet;
495 struct list_head client;
496 struct list_head pending_queue;
497 struct list_head active;
498 struct list_head done;
499 struct list_head queue;
500 struct list_head prepare_queue;
501 struct stedma40_chan_cfg dma_cfg;
502 struct dma_slave_config slave_config;
504 struct d40_base *base;
505 /* Default register configurations */
508 struct d40_def_lcsp log_def;
509 struct d40_log_lli_full *lcpa;
510 /* Runtime reconfiguration */
511 dma_addr_t runtime_addr;
512 enum dma_transfer_direction runtime_direction;
516 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
519 * @backup: the pointer to the registers address array for backup
520 * @backup_size: the size of the registers address array for backup
521 * @realtime_en: the realtime enable register
522 * @realtime_clear: the realtime clear register
523 * @high_prio_en: the high priority enable register
524 * @high_prio_clear: the high priority clear register
525 * @interrupt_en: the interrupt enable register
526 * @interrupt_clear: the interrupt clear register
527 * @il: the pointer to struct d40_interrupt_lookup
528 * @il_size: the size of d40_interrupt_lookup array
529 * @init_reg: the pointer to the struct d40_reg_val
530 * @init_reg_size: the size of d40_reg_val array
532 struct d40_gen_dmac {
541 struct d40_interrupt_lookup *il;
543 struct d40_reg_val *init_reg;
548 * struct d40_base - The big global struct, one for each probe'd instance.
550 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
551 * @execmd_lock: Lock for execute command usage since several channels share
552 * the same physical register.
553 * @dev: The device structure.
554 * @virtbase: The virtual base address of the DMA's register.
555 * @rev: silicon revision detected.
556 * @clk: Pointer to the DMA clock structure.
557 * @irq: The IRQ number.
558 * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
560 * @num_phy_chans: The number of physical channels. Read from HW. This
561 * is the number of available channels for this driver, not counting "Secure
562 * mode" allocated physical channels.
563 * @num_log_chans: The number of logical channels. Calculated from
565 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
566 * @dma_slave: dma_device channels that can do only do slave transfers.
567 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
568 * @phy_chans: Room for all possible physical channels in system.
569 * @log_chans: Room for all possible logical channels in system.
570 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
571 * to log_chans entries.
572 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
573 * to phy_chans entries.
574 * @plat_data: Pointer to provided platform_data which is the driver
576 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
577 * @phy_res: Vector containing all physical channels.
578 * @lcla_pool: lcla pool settings and data.
579 * @lcpa_base: The virtual mapped address of LCPA.
580 * @phy_lcpa: The physical address of the LCPA.
581 * @lcpa_size: The size of the LCPA area.
582 * @desc_slab: cache for descriptors.
583 * @reg_val_backup: Here the values of some hardware registers are stored
584 * before the DMA is powered off. They are restored when the power is back on.
585 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
587 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
588 * @regs_interrupt: Scratch space for registers during interrupt.
589 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
590 * @gen_dmac: the struct for generic registers values to represent u8500/8540
594 spinlock_t interrupt_lock;
595 spinlock_t execmd_lock;
597 void __iomem *virtbase;
601 int num_memcpy_chans;
604 struct dma_device dma_both;
605 struct dma_device dma_slave;
606 struct dma_device dma_memcpy;
607 struct d40_chan *phy_chans;
608 struct d40_chan *log_chans;
609 struct d40_chan **lookup_log_chans;
610 struct d40_chan **lookup_phy_chans;
611 struct stedma40_platform_data *plat_data;
612 struct regulator *lcpa_regulator;
613 /* Physical half channels */
614 struct d40_phy_res *phy_res;
615 struct d40_lcla_pool lcla_pool;
618 resource_size_t lcpa_size;
619 struct kmem_cache *desc_slab;
620 u32 reg_val_backup[BACKUP_REGS_SZ];
621 u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
622 u32 *reg_val_backup_chan;
624 u16 gcc_pwr_off_mask;
625 struct d40_gen_dmac gen_dmac;
628 static struct device *chan2dev(struct d40_chan *d40c)
630 return &d40c->chan.dev->device;
633 static bool chan_is_physical(struct d40_chan *chan)
635 return chan->log_num == D40_PHY_CHAN;
638 static bool chan_is_logical(struct d40_chan *chan)
640 return !chan_is_physical(chan);
643 static void __iomem *chan_base(struct d40_chan *chan)
645 return chan->base->virtbase + D40_DREG_PCBASE +
646 chan->phy_chan->num * D40_DREG_PCDELTA;
649 #define d40_err(dev, format, arg...) \
650 dev_err(dev, "[%s] " format, __func__, ## arg)
652 #define chan_err(d40c, format, arg...) \
653 d40_err(chan2dev(d40c), format, ## arg)
655 static int d40_set_runtime_config_write(struct dma_chan *chan,
656 struct dma_slave_config *config,
657 enum dma_transfer_direction direction);
659 static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
662 bool is_log = chan_is_logical(d40c);
667 align = sizeof(struct d40_log_lli);
669 align = sizeof(struct d40_phy_lli);
672 base = d40d->lli_pool.pre_alloc_lli;
673 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
674 d40d->lli_pool.base = NULL;
676 d40d->lli_pool.size = lli_len * 2 * align;
678 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
679 d40d->lli_pool.base = base;
681 if (d40d->lli_pool.base == NULL)
686 d40d->lli_log.src = PTR_ALIGN(base, align);
687 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
689 d40d->lli_pool.dma_addr = 0;
691 d40d->lli_phy.src = PTR_ALIGN(base, align);
692 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
694 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
699 if (dma_mapping_error(d40c->base->dev,
700 d40d->lli_pool.dma_addr)) {
701 kfree(d40d->lli_pool.base);
702 d40d->lli_pool.base = NULL;
703 d40d->lli_pool.dma_addr = 0;
711 static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
713 if (d40d->lli_pool.dma_addr)
714 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
715 d40d->lli_pool.size, DMA_TO_DEVICE);
717 kfree(d40d->lli_pool.base);
718 d40d->lli_pool.base = NULL;
719 d40d->lli_pool.size = 0;
720 d40d->lli_log.src = NULL;
721 d40d->lli_log.dst = NULL;
722 d40d->lli_phy.src = NULL;
723 d40d->lli_phy.dst = NULL;
726 static int d40_lcla_alloc_one(struct d40_chan *d40c,
727 struct d40_desc *d40d)
733 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
736 * Allocate both src and dst at the same time, therefore the half
737 * start on 1 since 0 can't be used since zero is used as end marker.
739 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
740 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
742 if (!d40c->base->lcla_pool.alloc_map[idx]) {
743 d40c->base->lcla_pool.alloc_map[idx] = d40d;
750 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
755 static int d40_lcla_free_all(struct d40_chan *d40c,
756 struct d40_desc *d40d)
762 if (chan_is_physical(d40c))
765 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
767 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
768 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
770 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
771 d40c->base->lcla_pool.alloc_map[idx] = NULL;
773 if (d40d->lcla_alloc == 0) {
780 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
786 static void d40_desc_remove(struct d40_desc *d40d)
788 list_del(&d40d->node);
791 static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
793 struct d40_desc *desc = NULL;
795 if (!list_empty(&d40c->client)) {
799 list_for_each_entry_safe(d, _d, &d40c->client, node) {
800 if (async_tx_test_ack(&d->txd)) {
803 memset(desc, 0, sizeof(*desc));
810 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
813 INIT_LIST_HEAD(&desc->node);
818 static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
821 d40_pool_lli_free(d40c, d40d);
822 d40_lcla_free_all(d40c, d40d);
823 kmem_cache_free(d40c->base->desc_slab, d40d);
826 static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
828 list_add_tail(&desc->node, &d40c->active);
831 static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
833 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
834 struct d40_phy_lli *lli_src = desc->lli_phy.src;
835 void __iomem *base = chan_base(chan);
837 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
838 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
839 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
840 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
842 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
843 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
844 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
845 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
848 static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
850 list_add_tail(&desc->node, &d40c->done);
853 static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
855 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
856 struct d40_log_lli_bidir *lli = &desc->lli_log;
857 int lli_current = desc->lli_current;
858 int lli_len = desc->lli_len;
859 bool cyclic = desc->cyclic;
860 int curr_lcla = -EINVAL;
862 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
866 * We may have partially running cyclic transfers, in case we did't get
867 * enough LCLA entries.
869 linkback = cyclic && lli_current == 0;
872 * For linkback, we need one LCLA even with only one link, because we
873 * can't link back to the one in LCPA space
875 if (linkback || (lli_len - lli_current > 1)) {
877 * If the channel is expected to use only soft_lli don't
878 * allocate a lcla. This is to avoid a HW issue that exists
879 * in some controller during a peripheral to memory transfer
880 * that uses linked lists.
882 if (!(chan->phy_chan->use_soft_lli &&
883 chan->dma_cfg.dir == DMA_DEV_TO_MEM))
884 curr_lcla = d40_lcla_alloc_one(chan, desc);
886 first_lcla = curr_lcla;
890 * For linkback, we normally load the LCPA in the loop since we need to
891 * link it to the second LCLA and not the first. However, if we
892 * couldn't even get a first LCLA, then we have to run in LCPA and
895 if (!linkback || curr_lcla == -EINVAL) {
896 unsigned int flags = 0;
898 if (curr_lcla == -EINVAL)
899 flags |= LLI_TERM_INT;
901 d40_log_lli_lcpa_write(chan->lcpa,
902 &lli->dst[lli_current],
903 &lli->src[lli_current],
912 for (; lli_current < lli_len; lli_current++) {
913 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
915 struct d40_log_lli *lcla = pool->base + lcla_offset;
916 unsigned int flags = 0;
919 if (lli_current + 1 < lli_len)
920 next_lcla = d40_lcla_alloc_one(chan, desc);
922 next_lcla = linkback ? first_lcla : -EINVAL;
924 if (cyclic || next_lcla == -EINVAL)
925 flags |= LLI_TERM_INT;
927 if (linkback && curr_lcla == first_lcla) {
928 /* First link goes in both LCPA and LCLA */
929 d40_log_lli_lcpa_write(chan->lcpa,
930 &lli->dst[lli_current],
931 &lli->src[lli_current],
936 * One unused LCLA in the cyclic case if the very first
939 d40_log_lli_lcla_write(lcla,
940 &lli->dst[lli_current],
941 &lli->src[lli_current],
945 * Cache maintenance is not needed if lcla is
948 if (!use_esram_lcla) {
949 dma_sync_single_range_for_device(chan->base->dev,
950 pool->dma_addr, lcla_offset,
951 2 * sizeof(struct d40_log_lli),
954 curr_lcla = next_lcla;
956 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
962 desc->lli_current = lli_current;
965 static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
967 if (chan_is_physical(d40c)) {
968 d40_phy_lli_load(d40c, d40d);
969 d40d->lli_current = d40d->lli_len;
971 d40_log_lli_to_lcxa(d40c, d40d);
974 static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
976 return list_first_entry_or_null(&d40c->active, struct d40_desc, node);
979 /* remove desc from current queue and add it to the pending_queue */
980 static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
982 d40_desc_remove(desc);
983 desc->is_in_client_list = false;
984 list_add_tail(&desc->node, &d40c->pending_queue);
987 static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
989 return list_first_entry_or_null(&d40c->pending_queue, struct d40_desc,
993 static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
995 return list_first_entry_or_null(&d40c->queue, struct d40_desc, node);
998 static struct d40_desc *d40_first_done(struct d40_chan *d40c)
1000 return list_first_entry_or_null(&d40c->done, struct d40_desc, node);
1003 static int d40_psize_2_burst_size(bool is_log, int psize)
1006 if (psize == STEDMA40_PSIZE_LOG_1)
1009 if (psize == STEDMA40_PSIZE_PHY_1)
1017 * The dma only supports transmitting packages up to
1018 * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
1020 * Calculate the total number of dma elements required to send the entire sg list.
1022 static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1025 u32 max_w = max(data_width1, data_width2);
1026 u32 min_w = min(data_width1, data_width2);
1027 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1029 if (seg_max > STEDMA40_MAX_SEG_SIZE)
1032 if (!IS_ALIGNED(size, max_w))
1035 if (size <= seg_max)
1038 dmalen = size / seg_max;
1039 if (dmalen * seg_max < size)
1045 static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1046 u32 data_width1, u32 data_width2)
1048 struct scatterlist *sg;
1053 for_each_sg(sgl, sg, sg_len, i) {
1054 ret = d40_size_2_dmalen(sg_dma_len(sg),
1055 data_width1, data_width2);
1063 static int __d40_execute_command_phy(struct d40_chan *d40c,
1064 enum d40_command command)
1068 void __iomem *active_reg;
1070 unsigned long flags;
1073 if (command == D40_DMA_STOP) {
1074 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1079 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1081 if (d40c->phy_chan->num % 2 == 0)
1082 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1084 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1086 if (command == D40_DMA_SUSPEND_REQ) {
1087 status = (readl(active_reg) &
1088 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1089 D40_CHAN_POS(d40c->phy_chan->num);
1091 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1095 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1096 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1099 if (command == D40_DMA_SUSPEND_REQ) {
1101 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1102 status = (readl(active_reg) &
1103 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1104 D40_CHAN_POS(d40c->phy_chan->num);
1108 * Reduce the number of bus accesses while
1109 * waiting for the DMA to suspend.
1113 if (status == D40_DMA_STOP ||
1114 status == D40_DMA_SUSPENDED)
1118 if (i == D40_SUSPEND_MAX_IT) {
1120 "unable to suspend the chl %d (log: %d) status %x\n",
1121 d40c->phy_chan->num, d40c->log_num,
1129 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1133 static void d40_term_all(struct d40_chan *d40c)
1135 struct d40_desc *d40d;
1136 struct d40_desc *_d;
1138 /* Release completed descriptors */
1139 while ((d40d = d40_first_done(d40c))) {
1140 d40_desc_remove(d40d);
1141 d40_desc_free(d40c, d40d);
1144 /* Release active descriptors */
1145 while ((d40d = d40_first_active_get(d40c))) {
1146 d40_desc_remove(d40d);
1147 d40_desc_free(d40c, d40d);
1150 /* Release queued descriptors waiting for transfer */
1151 while ((d40d = d40_first_queued(d40c))) {
1152 d40_desc_remove(d40d);
1153 d40_desc_free(d40c, d40d);
1156 /* Release pending descriptors */
1157 while ((d40d = d40_first_pending(d40c))) {
1158 d40_desc_remove(d40d);
1159 d40_desc_free(d40c, d40d);
1162 /* Release client owned descriptors */
1163 if (!list_empty(&d40c->client))
1164 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1165 d40_desc_remove(d40d);
1166 d40_desc_free(d40c, d40d);
1169 /* Release descriptors in prepare queue */
1170 if (!list_empty(&d40c->prepare_queue))
1171 list_for_each_entry_safe(d40d, _d,
1172 &d40c->prepare_queue, node) {
1173 d40_desc_remove(d40d);
1174 d40_desc_free(d40c, d40d);
1177 d40c->pending_tx = 0;
1180 static void __d40_config_set_event(struct d40_chan *d40c,
1181 enum d40_events event_type, u32 event,
1184 void __iomem *addr = chan_base(d40c) + reg;
1188 switch (event_type) {
1190 case D40_DEACTIVATE_EVENTLINE:
1192 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1193 | ~D40_EVENTLINE_MASK(event), addr);
1196 case D40_SUSPEND_REQ_EVENTLINE:
1197 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1198 D40_EVENTLINE_POS(event);
1200 if (status == D40_DEACTIVATE_EVENTLINE ||
1201 status == D40_SUSPEND_REQ_EVENTLINE)
1204 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1205 | ~D40_EVENTLINE_MASK(event), addr);
1207 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1209 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1210 D40_EVENTLINE_POS(event);
1214 * Reduce the number of bus accesses while
1215 * waiting for the DMA to suspend.
1219 if (status == D40_DEACTIVATE_EVENTLINE)
1223 if (tries == D40_SUSPEND_MAX_IT) {
1225 "unable to stop the event_line chl %d (log: %d)"
1226 "status %x\n", d40c->phy_chan->num,
1227 d40c->log_num, status);
1231 case D40_ACTIVATE_EVENTLINE:
1233 * The hardware sometimes doesn't register the enable when src and dst
1234 * event lines are active on the same logical channel. Retry to ensure
1235 * it does. Usually only one retry is sufficient.
1239 writel((D40_ACTIVATE_EVENTLINE <<
1240 D40_EVENTLINE_POS(event)) |
1241 ~D40_EVENTLINE_MASK(event), addr);
1243 if (readl(addr) & D40_EVENTLINE_MASK(event))
1248 dev_dbg(chan2dev(d40c),
1249 "[%s] workaround enable S%cLNK (%d tries)\n",
1250 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1256 case D40_ROUND_EVENTLINE:
1263 static void d40_config_set_event(struct d40_chan *d40c,
1264 enum d40_events event_type)
1266 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
1268 /* Enable event line connected to device (or memcpy) */
1269 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
1270 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
1271 __d40_config_set_event(d40c, event_type, event,
1272 D40_CHAN_REG_SSLNK);
1274 if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
1275 __d40_config_set_event(d40c, event_type, event,
1276 D40_CHAN_REG_SDLNK);
1279 static u32 d40_chan_has_events(struct d40_chan *d40c)
1281 void __iomem *chanbase = chan_base(d40c);
1284 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1285 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
1291 __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1293 unsigned long flags;
1296 void __iomem *active_reg;
1298 if (d40c->phy_chan->num % 2 == 0)
1299 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1301 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1304 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1308 case D40_DMA_SUSPEND_REQ:
1310 active_status = (readl(active_reg) &
1311 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1312 D40_CHAN_POS(d40c->phy_chan->num);
1314 if (active_status == D40_DMA_RUN)
1315 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1317 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1319 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1320 ret = __d40_execute_command_phy(d40c, command);
1326 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1327 ret = __d40_execute_command_phy(d40c, command);
1330 case D40_DMA_SUSPENDED:
1335 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1339 static int d40_channel_execute_command(struct d40_chan *d40c,
1340 enum d40_command command)
1342 if (chan_is_logical(d40c))
1343 return __d40_execute_command_log(d40c, command);
1345 return __d40_execute_command_phy(d40c, command);
1348 static u32 d40_get_prmo(struct d40_chan *d40c)
1350 static const unsigned int phy_map[] = {
1351 [STEDMA40_PCHAN_BASIC_MODE]
1352 = D40_DREG_PRMO_PCHAN_BASIC,
1353 [STEDMA40_PCHAN_MODULO_MODE]
1354 = D40_DREG_PRMO_PCHAN_MODULO,
1355 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1356 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1358 static const unsigned int log_map[] = {
1359 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1360 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1361 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1362 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1363 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1364 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1367 if (chan_is_physical(d40c))
1368 return phy_map[d40c->dma_cfg.mode_opt];
1370 return log_map[d40c->dma_cfg.mode_opt];
1373 static void d40_config_write(struct d40_chan *d40c)
1378 /* Odd addresses are even addresses + 4 */
1379 addr_base = (d40c->phy_chan->num % 2) * 4;
1380 /* Setup channel mode to logical or physical */
1381 var = ((u32)(chan_is_logical(d40c)) + 1) <<
1382 D40_CHAN_POS(d40c->phy_chan->num);
1383 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1385 /* Setup operational mode option register */
1386 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
1388 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1390 if (chan_is_logical(d40c)) {
1391 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1392 & D40_SREG_ELEM_LOG_LIDX_MASK;
1393 void __iomem *chanbase = chan_base(d40c);
1395 /* Set default config for CFG reg */
1396 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1397 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
1399 /* Set LIDX for lcla */
1400 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1401 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1403 /* Clear LNK which will be used by d40_chan_has_events() */
1404 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1405 writel(0, chanbase + D40_CHAN_REG_SDLNK);
1409 static u32 d40_residue(struct d40_chan *d40c)
1413 if (chan_is_logical(d40c))
1414 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1415 >> D40_MEM_LCSP2_ECNT_POS;
1417 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1418 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1419 >> D40_SREG_ELEM_PHY_ECNT_POS;
1422 return num_elt * d40c->dma_cfg.dst_info.data_width;
1425 static bool d40_tx_is_linked(struct d40_chan *d40c)
1429 if (chan_is_logical(d40c))
1430 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1432 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1433 & D40_SREG_LNK_PHYS_LNK_MASK;
1438 static int d40_pause(struct dma_chan *chan)
1440 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1442 unsigned long flags;
1444 if (d40c->phy_chan == NULL) {
1445 chan_err(d40c, "Channel is not allocated!\n");
1452 spin_lock_irqsave(&d40c->lock, flags);
1453 pm_runtime_get_sync(d40c->base->dev);
1455 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1457 pm_runtime_mark_last_busy(d40c->base->dev);
1458 pm_runtime_put_autosuspend(d40c->base->dev);
1459 spin_unlock_irqrestore(&d40c->lock, flags);
1463 static int d40_resume(struct dma_chan *chan)
1465 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1467 unsigned long flags;
1469 if (d40c->phy_chan == NULL) {
1470 chan_err(d40c, "Channel is not allocated!\n");
1477 spin_lock_irqsave(&d40c->lock, flags);
1478 pm_runtime_get_sync(d40c->base->dev);
1480 /* If bytes left to transfer or linked tx resume job */
1481 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1482 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1484 pm_runtime_mark_last_busy(d40c->base->dev);
1485 pm_runtime_put_autosuspend(d40c->base->dev);
1486 spin_unlock_irqrestore(&d40c->lock, flags);
1490 static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1492 struct d40_chan *d40c = container_of(tx->chan,
1495 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1496 unsigned long flags;
1497 dma_cookie_t cookie;
1499 spin_lock_irqsave(&d40c->lock, flags);
1500 cookie = dma_cookie_assign(tx);
1501 d40_desc_queue(d40c, d40d);
1502 spin_unlock_irqrestore(&d40c->lock, flags);
1507 static int d40_start(struct d40_chan *d40c)
1509 return d40_channel_execute_command(d40c, D40_DMA_RUN);
1512 static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1514 struct d40_desc *d40d;
1517 /* Start queued jobs, if any */
1518 d40d = d40_first_queued(d40c);
1523 pm_runtime_get_sync(d40c->base->dev);
1526 /* Remove from queue */
1527 d40_desc_remove(d40d);
1529 /* Add to active queue */
1530 d40_desc_submit(d40c, d40d);
1532 /* Initiate DMA job */
1533 d40_desc_load(d40c, d40d);
1536 err = d40_start(d40c);
1545 /* called from interrupt context */
1546 static void dma_tc_handle(struct d40_chan *d40c)
1548 struct d40_desc *d40d;
1550 /* Get first active entry from list */
1551 d40d = d40_first_active_get(d40c);
1558 * If this was a paritially loaded list, we need to reloaded
1559 * it, and only when the list is completed. We need to check
1560 * for done because the interrupt will hit for every link, and
1561 * not just the last one.
1563 if (d40d->lli_current < d40d->lli_len
1564 && !d40_tx_is_linked(d40c)
1565 && !d40_residue(d40c)) {
1566 d40_lcla_free_all(d40c, d40d);
1567 d40_desc_load(d40c, d40d);
1568 (void) d40_start(d40c);
1570 if (d40d->lli_current == d40d->lli_len)
1571 d40d->lli_current = 0;
1574 d40_lcla_free_all(d40c, d40d);
1576 if (d40d->lli_current < d40d->lli_len) {
1577 d40_desc_load(d40c, d40d);
1579 (void) d40_start(d40c);
1583 if (d40_queue_start(d40c) == NULL) {
1586 pm_runtime_mark_last_busy(d40c->base->dev);
1587 pm_runtime_put_autosuspend(d40c->base->dev);
1590 d40_desc_remove(d40d);
1591 d40_desc_done(d40c, d40d);
1595 tasklet_schedule(&d40c->tasklet);
1599 static void dma_tasklet(struct tasklet_struct *t)
1601 struct d40_chan *d40c = from_tasklet(d40c, t, tasklet);
1602 struct d40_desc *d40d;
1603 unsigned long flags;
1604 bool callback_active;
1605 struct dmaengine_desc_callback cb;
1607 spin_lock_irqsave(&d40c->lock, flags);
1609 /* Get first entry from the done list */
1610 d40d = d40_first_done(d40c);
1612 /* Check if we have reached here for cyclic job */
1613 d40d = d40_first_active_get(d40c);
1614 if (d40d == NULL || !d40d->cyclic)
1615 goto check_pending_tx;
1619 dma_cookie_complete(&d40d->txd);
1622 * If terminating a channel pending_tx is set to zero.
1623 * This prevents any finished active jobs to return to the client.
1625 if (d40c->pending_tx == 0) {
1626 spin_unlock_irqrestore(&d40c->lock, flags);
1630 /* Callback to client */
1631 callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
1632 dmaengine_desc_get_callback(&d40d->txd, &cb);
1634 if (!d40d->cyclic) {
1635 if (async_tx_test_ack(&d40d->txd)) {
1636 d40_desc_remove(d40d);
1637 d40_desc_free(d40c, d40d);
1638 } else if (!d40d->is_in_client_list) {
1639 d40_desc_remove(d40d);
1640 d40_lcla_free_all(d40c, d40d);
1641 list_add_tail(&d40d->node, &d40c->client);
1642 d40d->is_in_client_list = true;
1648 if (d40c->pending_tx)
1649 tasklet_schedule(&d40c->tasklet);
1651 spin_unlock_irqrestore(&d40c->lock, flags);
1653 if (callback_active)
1654 dmaengine_desc_callback_invoke(&cb, NULL);
1658 /* Rescue manouver if receiving double interrupts */
1659 if (d40c->pending_tx > 0)
1661 spin_unlock_irqrestore(&d40c->lock, flags);
1664 static irqreturn_t d40_handle_interrupt(int irq, void *data)
1670 struct d40_chan *d40c;
1671 struct d40_base *base = data;
1672 u32 *regs = base->regs_interrupt;
1673 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1674 u32 il_size = base->gen_dmac.il_size;
1676 spin_lock(&base->interrupt_lock);
1678 /* Read interrupt status of both logical and physical channels */
1679 for (i = 0; i < il_size; i++)
1680 regs[i] = readl(base->virtbase + il[i].src);
1684 chan = find_next_bit((unsigned long *)regs,
1685 BITS_PER_LONG * il_size, chan + 1);
1687 /* No more set bits found? */
1688 if (chan == BITS_PER_LONG * il_size)
1691 row = chan / BITS_PER_LONG;
1692 idx = chan & (BITS_PER_LONG - 1);
1694 if (il[row].offset == D40_PHY_CHAN)
1695 d40c = base->lookup_phy_chans[idx];
1697 d40c = base->lookup_log_chans[il[row].offset + idx];
1701 * No error because this can happen if something else
1702 * in the system is using the channel.
1708 writel(BIT(idx), base->virtbase + il[row].clr);
1710 spin_lock(&d40c->lock);
1712 if (!il[row].is_error)
1713 dma_tc_handle(d40c);
1715 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1716 chan, il[row].offset, idx);
1718 spin_unlock(&d40c->lock);
1721 spin_unlock(&base->interrupt_lock);
1726 static int d40_validate_conf(struct d40_chan *d40c,
1727 struct stedma40_chan_cfg *conf)
1730 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
1733 chan_err(d40c, "Invalid direction.\n");
1737 if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
1738 (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
1739 (conf->dev_type < 0)) {
1740 chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
1744 if (conf->dir == DMA_DEV_TO_DEV) {
1746 * DMAC HW supports it. Will be added to this driver,
1747 * in case any dma client requires it.
1749 chan_err(d40c, "periph to periph not supported\n");
1753 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1754 conf->src_info.data_width !=
1755 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1756 conf->dst_info.data_width) {
1758 * The DMAC hardware only supports
1759 * src (burst x width) == dst (burst x width)
1762 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1769 static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1770 bool is_src, int log_event_line, bool is_log,
1773 unsigned long flags;
1774 spin_lock_irqsave(&phy->lock, flags);
1776 *first_user = ((phy->allocated_src | phy->allocated_dst)
1780 /* Physical interrupts are masked per physical full channel */
1781 if (phy->allocated_src == D40_ALLOC_FREE &&
1782 phy->allocated_dst == D40_ALLOC_FREE) {
1783 phy->allocated_dst = D40_ALLOC_PHY;
1784 phy->allocated_src = D40_ALLOC_PHY;
1787 goto not_found_unlock;
1790 /* Logical channel */
1792 if (phy->allocated_src == D40_ALLOC_PHY)
1793 goto not_found_unlock;
1795 if (phy->allocated_src == D40_ALLOC_FREE)
1796 phy->allocated_src = D40_ALLOC_LOG_FREE;
1798 if (!(phy->allocated_src & BIT(log_event_line))) {
1799 phy->allocated_src |= BIT(log_event_line);
1802 goto not_found_unlock;
1804 if (phy->allocated_dst == D40_ALLOC_PHY)
1805 goto not_found_unlock;
1807 if (phy->allocated_dst == D40_ALLOC_FREE)
1808 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1810 if (!(phy->allocated_dst & BIT(log_event_line))) {
1811 phy->allocated_dst |= BIT(log_event_line);
1816 spin_unlock_irqrestore(&phy->lock, flags);
1819 spin_unlock_irqrestore(&phy->lock, flags);
1823 static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1826 unsigned long flags;
1827 bool is_free = false;
1829 spin_lock_irqsave(&phy->lock, flags);
1830 if (!log_event_line) {
1831 phy->allocated_dst = D40_ALLOC_FREE;
1832 phy->allocated_src = D40_ALLOC_FREE;
1837 /* Logical channel */
1839 phy->allocated_src &= ~BIT(log_event_line);
1840 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1841 phy->allocated_src = D40_ALLOC_FREE;
1843 phy->allocated_dst &= ~BIT(log_event_line);
1844 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1845 phy->allocated_dst = D40_ALLOC_FREE;
1848 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1851 spin_unlock_irqrestore(&phy->lock, flags);
1856 static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
1858 int dev_type = d40c->dma_cfg.dev_type;
1861 struct d40_phy_res *phys;
1867 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
1869 phys = d40c->base->phy_res;
1870 num_phy_chans = d40c->base->num_phy_chans;
1872 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
1873 log_num = 2 * dev_type;
1875 } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
1876 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1877 /* dst event lines are used for logical memcpy */
1878 log_num = 2 * dev_type + 1;
1883 event_group = D40_TYPE_TO_GROUP(dev_type);
1884 event_line = D40_TYPE_TO_EVENT(dev_type);
1887 if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1888 /* Find physical half channel */
1889 if (d40c->dma_cfg.use_fixed_channel) {
1890 i = d40c->dma_cfg.phy_channel;
1891 if (d40_alloc_mask_set(&phys[i], is_src,
1896 for (i = 0; i < num_phy_chans; i++) {
1897 if (d40_alloc_mask_set(&phys[i], is_src,
1904 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1905 int phy_num = j + event_group * 2;
1906 for (i = phy_num; i < phy_num + 2; i++) {
1907 if (d40_alloc_mask_set(&phys[i],
1917 d40c->phy_chan = &phys[i];
1918 d40c->log_num = D40_PHY_CHAN;
1924 /* Find logical channel */
1925 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1926 int phy_num = j + event_group * 2;
1928 if (d40c->dma_cfg.use_fixed_channel) {
1929 i = d40c->dma_cfg.phy_channel;
1931 if ((i != phy_num) && (i != phy_num + 1)) {
1932 dev_err(chan2dev(d40c),
1933 "invalid fixed phy channel %d\n", i);
1937 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1938 is_log, first_phy_user))
1941 dev_err(chan2dev(d40c),
1942 "could not allocate fixed phy channel %d\n", i);
1947 * Spread logical channels across all available physical rather
1948 * than pack every logical channel at the first available phy
1952 for (i = phy_num; i < phy_num + 2; i++) {
1953 if (d40_alloc_mask_set(&phys[i], is_src,
1959 for (i = phy_num + 1; i >= phy_num; i--) {
1960 if (d40_alloc_mask_set(&phys[i], is_src,
1970 d40c->phy_chan = &phys[i];
1971 d40c->log_num = log_num;
1975 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1977 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1983 static int d40_config_memcpy(struct d40_chan *d40c)
1985 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1987 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1988 d40c->dma_cfg = dma40_memcpy_conf_log;
1989 d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
1991 d40_log_cfg(&d40c->dma_cfg,
1992 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1994 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1995 dma_has_cap(DMA_SLAVE, cap)) {
1996 d40c->dma_cfg = dma40_memcpy_conf_phy;
1998 /* Generate interrupt at end of transfer or relink. */
1999 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
2001 /* Generate interrupt on error. */
2002 d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2003 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2006 chan_err(d40c, "No memcpy\n");
2013 static int d40_free_dma(struct d40_chan *d40c)
2017 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2018 struct d40_phy_res *phy = d40c->phy_chan;
2021 /* Terminate all queued and active transfers */
2025 chan_err(d40c, "phy == null\n");
2029 if (phy->allocated_src == D40_ALLOC_FREE &&
2030 phy->allocated_dst == D40_ALLOC_FREE) {
2031 chan_err(d40c, "channel already free\n");
2035 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2036 d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
2038 else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2041 chan_err(d40c, "Unknown direction\n");
2045 pm_runtime_get_sync(d40c->base->dev);
2046 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2048 chan_err(d40c, "stop failed\n");
2049 goto mark_last_busy;
2052 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2054 if (chan_is_logical(d40c))
2055 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2057 d40c->base->lookup_phy_chans[phy->num] = NULL;
2060 pm_runtime_mark_last_busy(d40c->base->dev);
2061 pm_runtime_put_autosuspend(d40c->base->dev);
2065 d40c->phy_chan = NULL;
2066 d40c->configured = false;
2068 pm_runtime_mark_last_busy(d40c->base->dev);
2069 pm_runtime_put_autosuspend(d40c->base->dev);
2073 static bool d40_is_paused(struct d40_chan *d40c)
2075 void __iomem *chanbase = chan_base(d40c);
2076 bool is_paused = false;
2077 unsigned long flags;
2078 void __iomem *active_reg;
2080 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2082 spin_lock_irqsave(&d40c->lock, flags);
2084 if (chan_is_physical(d40c)) {
2085 if (d40c->phy_chan->num % 2 == 0)
2086 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2088 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2090 status = (readl(active_reg) &
2091 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2092 D40_CHAN_POS(d40c->phy_chan->num);
2093 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2098 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2099 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
2100 status = readl(chanbase + D40_CHAN_REG_SDLNK);
2101 } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
2102 status = readl(chanbase + D40_CHAN_REG_SSLNK);
2104 chan_err(d40c, "Unknown direction\n");
2108 status = (status & D40_EVENTLINE_MASK(event)) >>
2109 D40_EVENTLINE_POS(event);
2111 if (status != D40_DMA_RUN)
2114 spin_unlock_irqrestore(&d40c->lock, flags);
2119 static u32 stedma40_residue(struct dma_chan *chan)
2121 struct d40_chan *d40c =
2122 container_of(chan, struct d40_chan, chan);
2124 unsigned long flags;
2126 spin_lock_irqsave(&d40c->lock, flags);
2127 bytes_left = d40_residue(d40c);
2128 spin_unlock_irqrestore(&d40c->lock, flags);
2134 d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2135 struct scatterlist *sg_src, struct scatterlist *sg_dst,
2136 unsigned int sg_len, dma_addr_t src_dev_addr,
2137 dma_addr_t dst_dev_addr)
2139 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2140 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2141 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
2144 ret = d40_log_sg_to_lli(sg_src, sg_len,
2147 chan->log_def.lcsp1,
2148 src_info->data_width,
2149 dst_info->data_width);
2151 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2154 chan->log_def.lcsp3,
2155 dst_info->data_width,
2156 src_info->data_width);
2158 return ret < 0 ? ret : 0;
2162 d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2163 struct scatterlist *sg_src, struct scatterlist *sg_dst,
2164 unsigned int sg_len, dma_addr_t src_dev_addr,
2165 dma_addr_t dst_dev_addr)
2167 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2168 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2169 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
2170 unsigned long flags = 0;
2174 flags |= LLI_CYCLIC | LLI_TERM_INT;
2176 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2178 virt_to_phys(desc->lli_phy.src),
2180 src_info, dst_info, flags);
2182 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2184 virt_to_phys(desc->lli_phy.dst),
2186 dst_info, src_info, flags);
2188 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2189 desc->lli_pool.size, DMA_TO_DEVICE);
2191 return ret < 0 ? ret : 0;
2194 static struct d40_desc *
2195 d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2196 unsigned int sg_len, unsigned long dma_flags)
2198 struct stedma40_chan_cfg *cfg;
2199 struct d40_desc *desc;
2202 desc = d40_desc_get(chan);
2206 cfg = &chan->dma_cfg;
2207 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2208 cfg->dst_info.data_width);
2209 if (desc->lli_len < 0) {
2210 chan_err(chan, "Unaligned size\n");
2214 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2216 chan_err(chan, "Could not allocate lli\n");
2220 desc->lli_current = 0;
2221 desc->txd.flags = dma_flags;
2222 desc->txd.tx_submit = d40_tx_submit;
2224 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2228 d40_desc_free(chan, desc);
2232 static struct dma_async_tx_descriptor *
2233 d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2234 struct scatterlist *sg_dst, unsigned int sg_len,
2235 enum dma_transfer_direction direction, unsigned long dma_flags)
2237 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2238 dma_addr_t src_dev_addr;
2239 dma_addr_t dst_dev_addr;
2240 struct d40_desc *desc;
2241 unsigned long flags;
2244 if (!chan->phy_chan) {
2245 chan_err(chan, "Cannot prepare unallocated channel\n");
2249 d40_set_runtime_config_write(dchan, &chan->slave_config, direction);
2251 spin_lock_irqsave(&chan->lock, flags);
2253 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2257 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2258 desc->cyclic = true;
2262 if (direction == DMA_DEV_TO_MEM)
2263 src_dev_addr = chan->runtime_addr;
2264 else if (direction == DMA_MEM_TO_DEV)
2265 dst_dev_addr = chan->runtime_addr;
2267 if (chan_is_logical(chan))
2268 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2269 sg_len, src_dev_addr, dst_dev_addr);
2271 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2272 sg_len, src_dev_addr, dst_dev_addr);
2275 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2276 chan_is_logical(chan) ? "log" : "phy", ret);
2281 * add descriptor to the prepare queue in order to be able
2282 * to free them later in terminate_all
2284 list_add_tail(&desc->node, &chan->prepare_queue);
2286 spin_unlock_irqrestore(&chan->lock, flags);
2290 d40_desc_free(chan, desc);
2292 spin_unlock_irqrestore(&chan->lock, flags);
2296 static bool stedma40_filter(struct dma_chan *chan, void *data)
2298 struct stedma40_chan_cfg *info = data;
2299 struct d40_chan *d40c =
2300 container_of(chan, struct d40_chan, chan);
2304 err = d40_validate_conf(d40c, info);
2306 d40c->dma_cfg = *info;
2308 err = d40_config_memcpy(d40c);
2311 d40c->configured = true;
2316 static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2318 bool realtime = d40c->dma_cfg.realtime;
2319 bool highprio = d40c->dma_cfg.high_priority;
2321 u32 event = D40_TYPE_TO_EVENT(dev_type);
2322 u32 group = D40_TYPE_TO_GROUP(dev_type);
2323 u32 bit = BIT(event);
2325 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2327 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2329 * Due to a hardware bug, in some cases a logical channel triggered by
2330 * a high priority destination event line can generate extra packet
2333 * The workaround is to not set the high priority level for the
2334 * destination event lines that trigger logical channels.
2336 if (!src && chan_is_logical(d40c))
2339 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2341 /* Destination event lines are stored in the upper halfword */
2345 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2346 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2349 static void d40_set_prio_realtime(struct d40_chan *d40c)
2351 if (d40c->base->rev < 3)
2354 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
2355 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2356 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2358 if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
2359 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2360 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2363 #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2364 #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2365 #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2366 #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2367 #define D40_DT_FLAGS_HIGH_PRIO(flags) ((flags >> 4) & 0x1)
2369 static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2370 struct of_dma *ofdma)
2372 struct stedma40_chan_cfg cfg;
2376 memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2379 dma_cap_set(DMA_SLAVE, cap);
2381 cfg.dev_type = dma_spec->args[0];
2382 flags = dma_spec->args[2];
2384 switch (D40_DT_FLAGS_MODE(flags)) {
2385 case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2386 case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2389 switch (D40_DT_FLAGS_DIR(flags)) {
2391 cfg.dir = DMA_MEM_TO_DEV;
2392 cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2395 cfg.dir = DMA_DEV_TO_MEM;
2396 cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2400 if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2401 cfg.phy_channel = dma_spec->args[1];
2402 cfg.use_fixed_channel = true;
2405 if (D40_DT_FLAGS_HIGH_PRIO(flags))
2406 cfg.high_priority = true;
2408 return dma_request_channel(cap, stedma40_filter, &cfg);
2411 /* DMA ENGINE functions */
2412 static int d40_alloc_chan_resources(struct dma_chan *chan)
2415 unsigned long flags;
2416 struct d40_chan *d40c =
2417 container_of(chan, struct d40_chan, chan);
2419 spin_lock_irqsave(&d40c->lock, flags);
2421 dma_cookie_init(chan);
2423 /* If no dma configuration is set use default configuration (memcpy) */
2424 if (!d40c->configured) {
2425 err = d40_config_memcpy(d40c);
2427 chan_err(d40c, "Failed to configure memcpy channel\n");
2428 goto mark_last_busy;
2432 err = d40_allocate_channel(d40c, &is_free_phy);
2434 chan_err(d40c, "Failed to allocate channel\n");
2435 d40c->configured = false;
2436 goto mark_last_busy;
2439 pm_runtime_get_sync(d40c->base->dev);
2441 d40_set_prio_realtime(d40c);
2443 if (chan_is_logical(d40c)) {
2444 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2445 d40c->lcpa = d40c->base->lcpa_base +
2446 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2448 d40c->lcpa = d40c->base->lcpa_base +
2449 d40c->dma_cfg.dev_type *
2450 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2452 /* Unmask the Global Interrupt Mask. */
2453 d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2454 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2457 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2458 chan_is_logical(d40c) ? "logical" : "physical",
2459 d40c->phy_chan->num,
2460 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2464 * Only write channel configuration to the DMA if the physical
2465 * resource is free. In case of multiple logical channels
2466 * on the same physical resource, only the first write is necessary.
2469 d40_config_write(d40c);
2471 pm_runtime_mark_last_busy(d40c->base->dev);
2472 pm_runtime_put_autosuspend(d40c->base->dev);
2473 spin_unlock_irqrestore(&d40c->lock, flags);
2477 static void d40_free_chan_resources(struct dma_chan *chan)
2479 struct d40_chan *d40c =
2480 container_of(chan, struct d40_chan, chan);
2482 unsigned long flags;
2484 if (d40c->phy_chan == NULL) {
2485 chan_err(d40c, "Cannot free unallocated channel\n");
2489 spin_lock_irqsave(&d40c->lock, flags);
2491 err = d40_free_dma(d40c);
2494 chan_err(d40c, "Failed to free channel\n");
2495 spin_unlock_irqrestore(&d40c->lock, flags);
2498 static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2502 unsigned long dma_flags)
2504 struct scatterlist dst_sg;
2505 struct scatterlist src_sg;
2507 sg_init_table(&dst_sg, 1);
2508 sg_init_table(&src_sg, 1);
2510 sg_dma_address(&dst_sg) = dst;
2511 sg_dma_address(&src_sg) = src;
2513 sg_dma_len(&dst_sg) = size;
2514 sg_dma_len(&src_sg) = size;
2516 return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
2517 DMA_MEM_TO_MEM, dma_flags);
2520 static struct dma_async_tx_descriptor *
2521 d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2522 unsigned int sg_len, enum dma_transfer_direction direction,
2523 unsigned long dma_flags, void *context)
2525 if (!is_slave_direction(direction))
2528 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
2531 static struct dma_async_tx_descriptor *
2532 dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2533 size_t buf_len, size_t period_len,
2534 enum dma_transfer_direction direction, unsigned long flags)
2536 unsigned int periods = buf_len / period_len;
2537 struct dma_async_tx_descriptor *txd;
2538 struct scatterlist *sg;
2541 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
2545 for (i = 0; i < periods; i++) {
2546 sg_dma_address(&sg[i]) = dma_addr;
2547 sg_dma_len(&sg[i]) = period_len;
2548 dma_addr += period_len;
2551 sg_chain(sg, periods + 1, sg);
2553 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2554 DMA_PREP_INTERRUPT);
2561 static enum dma_status d40_tx_status(struct dma_chan *chan,
2562 dma_cookie_t cookie,
2563 struct dma_tx_state *txstate)
2565 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2566 enum dma_status ret;
2568 if (d40c->phy_chan == NULL) {
2569 chan_err(d40c, "Cannot read status of unallocated channel\n");
2573 ret = dma_cookie_status(chan, cookie, txstate);
2574 if (ret != DMA_COMPLETE && txstate)
2575 dma_set_residue(txstate, stedma40_residue(chan));
2577 if (d40_is_paused(d40c))
2583 static void d40_issue_pending(struct dma_chan *chan)
2585 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2586 unsigned long flags;
2588 if (d40c->phy_chan == NULL) {
2589 chan_err(d40c, "Channel is not allocated!\n");
2593 spin_lock_irqsave(&d40c->lock, flags);
2595 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2597 /* Busy means that queued jobs are already being processed */
2599 (void) d40_queue_start(d40c);
2601 spin_unlock_irqrestore(&d40c->lock, flags);
2604 static int d40_terminate_all(struct dma_chan *chan)
2606 unsigned long flags;
2607 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2610 if (d40c->phy_chan == NULL) {
2611 chan_err(d40c, "Channel is not allocated!\n");
2615 spin_lock_irqsave(&d40c->lock, flags);
2617 pm_runtime_get_sync(d40c->base->dev);
2618 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2620 chan_err(d40c, "Failed to stop channel\n");
2623 pm_runtime_mark_last_busy(d40c->base->dev);
2624 pm_runtime_put_autosuspend(d40c->base->dev);
2626 pm_runtime_mark_last_busy(d40c->base->dev);
2627 pm_runtime_put_autosuspend(d40c->base->dev);
2631 spin_unlock_irqrestore(&d40c->lock, flags);
2636 dma40_config_to_halfchannel(struct d40_chan *d40c,
2637 struct stedma40_half_channel_info *info,
2642 if (chan_is_logical(d40c)) {
2644 psize = STEDMA40_PSIZE_LOG_16;
2645 else if (maxburst >= 8)
2646 psize = STEDMA40_PSIZE_LOG_8;
2647 else if (maxburst >= 4)
2648 psize = STEDMA40_PSIZE_LOG_4;
2650 psize = STEDMA40_PSIZE_LOG_1;
2653 psize = STEDMA40_PSIZE_PHY_16;
2654 else if (maxburst >= 8)
2655 psize = STEDMA40_PSIZE_PHY_8;
2656 else if (maxburst >= 4)
2657 psize = STEDMA40_PSIZE_PHY_4;
2659 psize = STEDMA40_PSIZE_PHY_1;
2662 info->psize = psize;
2663 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2668 static int d40_set_runtime_config(struct dma_chan *chan,
2669 struct dma_slave_config *config)
2671 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2673 memcpy(&d40c->slave_config, config, sizeof(*config));
2678 /* Runtime reconfiguration extension */
2679 static int d40_set_runtime_config_write(struct dma_chan *chan,
2680 struct dma_slave_config *config,
2681 enum dma_transfer_direction direction)
2683 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2684 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2685 enum dma_slave_buswidth src_addr_width, dst_addr_width;
2686 dma_addr_t config_addr;
2687 u32 src_maxburst, dst_maxburst;
2690 if (d40c->phy_chan == NULL) {
2691 chan_err(d40c, "Channel is not allocated!\n");
2695 src_addr_width = config->src_addr_width;
2696 src_maxburst = config->src_maxburst;
2697 dst_addr_width = config->dst_addr_width;
2698 dst_maxburst = config->dst_maxburst;
2700 if (direction == DMA_DEV_TO_MEM) {
2701 config_addr = config->src_addr;
2703 if (cfg->dir != DMA_DEV_TO_MEM)
2704 dev_dbg(d40c->base->dev,
2705 "channel was not configured for peripheral "
2706 "to memory transfer (%d) overriding\n",
2708 cfg->dir = DMA_DEV_TO_MEM;
2710 /* Configure the memory side */
2711 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2712 dst_addr_width = src_addr_width;
2713 if (dst_maxburst == 0)
2714 dst_maxburst = src_maxburst;
2716 } else if (direction == DMA_MEM_TO_DEV) {
2717 config_addr = config->dst_addr;
2719 if (cfg->dir != DMA_MEM_TO_DEV)
2720 dev_dbg(d40c->base->dev,
2721 "channel was not configured for memory "
2722 "to peripheral transfer (%d) overriding\n",
2724 cfg->dir = DMA_MEM_TO_DEV;
2726 /* Configure the memory side */
2727 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2728 src_addr_width = dst_addr_width;
2729 if (src_maxburst == 0)
2730 src_maxburst = dst_maxburst;
2732 dev_err(d40c->base->dev,
2733 "unrecognized channel direction %d\n",
2738 if (config_addr <= 0) {
2739 dev_err(d40c->base->dev, "no address supplied\n");
2743 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
2744 dev_err(d40c->base->dev,
2745 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2753 if (src_maxburst > 16) {
2755 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2756 } else if (dst_maxburst > 16) {
2758 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2761 /* Only valid widths are; 1, 2, 4 and 8. */
2762 if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2763 src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2764 dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2765 dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2766 !is_power_of_2(src_addr_width) ||
2767 !is_power_of_2(dst_addr_width))
2770 cfg->src_info.data_width = src_addr_width;
2771 cfg->dst_info.data_width = dst_addr_width;
2773 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2778 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2783 /* Fill in register values */
2784 if (chan_is_logical(d40c))
2785 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2787 d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2789 /* These settings will take precedence later */
2790 d40c->runtime_addr = config_addr;
2791 d40c->runtime_direction = direction;
2792 dev_dbg(d40c->base->dev,
2793 "configured channel %s for %s, data width %d/%d, "
2794 "maxburst %d/%d elements, LE, no flow control\n",
2795 dma_chan_name(chan),
2796 (direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
2797 src_addr_width, dst_addr_width,
2798 src_maxburst, dst_maxburst);
2803 /* Initialization functions */
2805 static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2806 struct d40_chan *chans, int offset,
2810 struct d40_chan *d40c;
2812 INIT_LIST_HEAD(&dma->channels);
2814 for (i = offset; i < offset + num_chans; i++) {
2817 d40c->chan.device = dma;
2819 spin_lock_init(&d40c->lock);
2821 d40c->log_num = D40_PHY_CHAN;
2823 INIT_LIST_HEAD(&d40c->done);
2824 INIT_LIST_HEAD(&d40c->active);
2825 INIT_LIST_HEAD(&d40c->queue);
2826 INIT_LIST_HEAD(&d40c->pending_queue);
2827 INIT_LIST_HEAD(&d40c->client);
2828 INIT_LIST_HEAD(&d40c->prepare_queue);
2830 tasklet_setup(&d40c->tasklet, dma_tasklet);
2832 list_add_tail(&d40c->chan.device_node,
2837 static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2839 if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) {
2840 dev->device_prep_slave_sg = d40_prep_slave_sg;
2841 dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2844 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2845 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2846 dev->directions = BIT(DMA_MEM_TO_MEM);
2848 * This controller can only access address at even
2849 * 32bit boundaries, i.e. 2^2
2851 dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
2854 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2855 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2857 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2858 dev->device_free_chan_resources = d40_free_chan_resources;
2859 dev->device_issue_pending = d40_issue_pending;
2860 dev->device_tx_status = d40_tx_status;
2861 dev->device_config = d40_set_runtime_config;
2862 dev->device_pause = d40_pause;
2863 dev->device_resume = d40_resume;
2864 dev->device_terminate_all = d40_terminate_all;
2865 dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
2866 dev->dev = base->dev;
2869 static int __init d40_dmaengine_init(struct d40_base *base,
2870 int num_reserved_chans)
2874 d40_chan_init(base, &base->dma_slave, base->log_chans,
2875 0, base->num_log_chans);
2877 dma_cap_zero(base->dma_slave.cap_mask);
2878 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2879 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2881 d40_ops_init(base, &base->dma_slave);
2883 err = dmaenginem_async_device_register(&base->dma_slave);
2886 d40_err(base->dev, "Failed to register slave channels\n");
2890 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2891 base->num_log_chans, base->num_memcpy_chans);
2893 dma_cap_zero(base->dma_memcpy.cap_mask);
2894 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
2896 d40_ops_init(base, &base->dma_memcpy);
2898 err = dmaenginem_async_device_register(&base->dma_memcpy);
2902 "Failed to register memcpy only channels\n");
2906 d40_chan_init(base, &base->dma_both, base->phy_chans,
2907 0, num_reserved_chans);
2909 dma_cap_zero(base->dma_both.cap_mask);
2910 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2911 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
2912 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2914 d40_ops_init(base, &base->dma_both);
2915 err = dmaenginem_async_device_register(&base->dma_both);
2919 "Failed to register logical and physical capable channels\n");
2927 /* Suspend resume functionality */
2928 #ifdef CONFIG_PM_SLEEP
2929 static int dma40_suspend(struct device *dev)
2931 struct d40_base *base = dev_get_drvdata(dev);
2934 ret = pm_runtime_force_suspend(dev);
2938 if (base->lcpa_regulator)
2939 ret = regulator_disable(base->lcpa_regulator);
2943 static int dma40_resume(struct device *dev)
2945 struct d40_base *base = dev_get_drvdata(dev);
2948 if (base->lcpa_regulator) {
2949 ret = regulator_enable(base->lcpa_regulator);
2954 return pm_runtime_force_resume(dev);
2959 static void dma40_backup(void __iomem *baseaddr, u32 *backup,
2960 u32 *regaddr, int num, bool save)
2964 for (i = 0; i < num; i++) {
2965 void __iomem *addr = baseaddr + regaddr[i];
2968 backup[i] = readl_relaxed(addr);
2970 writel_relaxed(backup[i], addr);
2974 static void d40_save_restore_registers(struct d40_base *base, bool save)
2978 /* Save/Restore channel specific registers */
2979 for (i = 0; i < base->num_phy_chans; i++) {
2983 if (base->phy_res[i].reserved)
2986 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
2987 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
2989 dma40_backup(addr, &base->reg_val_backup_chan[idx],
2990 d40_backup_regs_chan,
2991 ARRAY_SIZE(d40_backup_regs_chan),
2995 /* Save/Restore global registers */
2996 dma40_backup(base->virtbase, base->reg_val_backup,
2997 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
3000 /* Save/Restore registers only existing on dma40 v3 and later */
3001 if (base->gen_dmac.backup)
3002 dma40_backup(base->virtbase, base->reg_val_backup_v4,
3003 base->gen_dmac.backup,
3004 base->gen_dmac.backup_size,
3008 static int dma40_runtime_suspend(struct device *dev)
3010 struct d40_base *base = dev_get_drvdata(dev);
3012 d40_save_restore_registers(base, true);
3014 /* Don't disable/enable clocks for v1 due to HW bugs */
3016 writel_relaxed(base->gcc_pwr_off_mask,
3017 base->virtbase + D40_DREG_GCC);
3022 static int dma40_runtime_resume(struct device *dev)
3024 struct d40_base *base = dev_get_drvdata(dev);
3026 d40_save_restore_registers(base, false);
3028 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3029 base->virtbase + D40_DREG_GCC);
3034 static const struct dev_pm_ops dma40_pm_ops = {
3035 SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
3036 SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
3037 dma40_runtime_resume,
3041 /* Initialization functions. */
3043 static int __init d40_phy_res_init(struct d40_base *base)
3046 int num_phy_chans_avail = 0;
3048 int odd_even_bit = -2;
3049 int gcc = D40_DREG_GCC_ENA;
3051 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3052 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3054 for (i = 0; i < base->num_phy_chans; i++) {
3055 base->phy_res[i].num = i;
3056 odd_even_bit += 2 * ((i % 2) == 0);
3057 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3058 /* Mark security only channels as occupied */
3059 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3060 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
3061 base->phy_res[i].reserved = true;
3062 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3064 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3069 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3070 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
3071 base->phy_res[i].reserved = false;
3072 num_phy_chans_avail++;
3074 spin_lock_init(&base->phy_res[i].lock);
3077 /* Mark disabled channels as occupied */
3078 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3079 int chan = base->plat_data->disabled_channels[i];
3081 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3082 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
3083 base->phy_res[chan].reserved = true;
3084 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3086 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3088 num_phy_chans_avail--;
3091 /* Mark soft_lli channels */
3092 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3093 int chan = base->plat_data->soft_lli_chans[i];
3095 base->phy_res[chan].use_soft_lli = true;
3098 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3099 num_phy_chans_avail, base->num_phy_chans);
3101 /* Verify settings extended vs standard */
3102 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3104 for (i = 0; i < base->num_phy_chans; i++) {
3106 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3107 (val[0] & 0x3) != 1)
3109 "[%s] INFO: channel %d is misconfigured (%d)\n",
3110 __func__, i, val[0] & 0x3);
3112 val[0] = val[0] >> 2;
3116 * To keep things simple, Enable all clocks initially.
3117 * The clocks will get managed later post channel allocation.
3118 * The clocks for the event lines on which reserved channels exists
3119 * are not managed here.
3121 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3122 base->gcc_pwr_off_mask = gcc;
3124 return num_phy_chans_avail;
3127 /* Called from the registered devm action */
3128 static void d40_drop_kmem_cache_action(void *d)
3130 struct kmem_cache *desc_slab = d;
3132 kmem_cache_destroy(desc_slab);
3135 static int __init d40_hw_detect_init(struct platform_device *pdev,
3136 struct d40_base **retbase)
3138 struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
3139 struct device *dev = &pdev->dev;
3141 void __iomem *virtbase;
3142 struct d40_base *base;
3145 int num_memcpy_chans;
3152 clk = devm_clk_get_enabled(dev, NULL);
3154 return PTR_ERR(clk);
3156 /* Get IO for DMAC base address */
3157 virtbase = devm_platform_ioremap_resource_byname(pdev, "base");
3158 if (IS_ERR(virtbase))
3159 return PTR_ERR(virtbase);
3161 /* This is just a regular AMBA PrimeCell ID actually */
3162 for (pid = 0, i = 0; i < 4; i++)
3163 pid |= (readl(virtbase + SZ_4K - 0x20 + 4 * i)
3165 for (cid = 0, i = 0; i < 4; i++)
3166 cid |= (readl(virtbase + SZ_4K - 0x10 + 4 * i)
3169 if (cid != AMBA_CID) {
3170 d40_err(dev, "Unknown hardware! No PrimeCell ID\n");
3173 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3174 d40_err(dev, "Unknown designer! Got %x wanted %x\n",
3175 AMBA_MANF_BITS(pid),
3181 * DB8500ed has revision 0
3183 * DB8500v1 has revision 2
3184 * DB8500v2 has revision 3
3185 * AP9540v1 has revision 4
3186 * DB8540v1 has revision 4
3188 rev = AMBA_REV_BITS(pid);
3190 d40_err(dev, "hardware revision: %d is not supported", rev);
3194 /* The number of physical channels on this HW */
3195 if (plat_data->num_of_phy_chans)
3196 num_phy_chans = plat_data->num_of_phy_chans;
3198 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3200 /* The number of channels used for memcpy */
3201 if (plat_data->num_of_memcpy_chans)
3202 num_memcpy_chans = plat_data->num_of_memcpy_chans;
3204 num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
3206 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3209 "hardware rev: %d with %d physical and %d logical channels\n",
3210 rev, num_phy_chans, num_log_chans);
3212 base = devm_kzalloc(dev,
3213 ALIGN(sizeof(struct d40_base), 4) +
3214 (num_phy_chans + num_log_chans + num_memcpy_chans) *
3215 sizeof(struct d40_chan), GFP_KERNEL);
3222 base->num_memcpy_chans = num_memcpy_chans;
3223 base->num_phy_chans = num_phy_chans;
3224 base->num_log_chans = num_log_chans;
3225 base->virtbase = virtbase;
3226 base->plat_data = plat_data;
3228 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3229 base->log_chans = &base->phy_chans[num_phy_chans];
3231 if (base->plat_data->num_of_phy_chans == 14) {
3232 base->gen_dmac.backup = d40_backup_regs_v4b;
3233 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3234 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3235 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3236 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3237 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3238 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3239 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3240 base->gen_dmac.il = il_v4b;
3241 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3242 base->gen_dmac.init_reg = dma_init_reg_v4b;
3243 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3245 if (base->rev >= 3) {
3246 base->gen_dmac.backup = d40_backup_regs_v4a;
3247 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3249 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3250 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3251 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3252 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3253 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3254 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3255 base->gen_dmac.il = il_v4a;
3256 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3257 base->gen_dmac.init_reg = dma_init_reg_v4a;
3258 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3261 base->phy_res = devm_kcalloc(dev, num_phy_chans,
3262 sizeof(*base->phy_res),
3267 base->lookup_phy_chans = devm_kcalloc(dev, num_phy_chans,
3268 sizeof(*base->lookup_phy_chans),
3270 if (!base->lookup_phy_chans)
3273 base->lookup_log_chans = devm_kcalloc(dev, num_log_chans,
3274 sizeof(*base->lookup_log_chans),
3276 if (!base->lookup_log_chans)
3279 base->reg_val_backup_chan = devm_kmalloc_array(dev, base->num_phy_chans,
3280 sizeof(d40_backup_regs_chan),
3282 if (!base->reg_val_backup_chan)
3285 base->lcla_pool.alloc_map = devm_kcalloc(dev, num_phy_chans
3286 * D40_LCLA_LINK_PER_EVENT_GRP,
3287 sizeof(*base->lcla_pool.alloc_map),
3289 if (!base->lcla_pool.alloc_map)
3292 base->regs_interrupt = devm_kmalloc_array(dev, base->gen_dmac.il_size,
3293 sizeof(*base->regs_interrupt),
3295 if (!base->regs_interrupt)
3298 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3299 0, SLAB_HWCACHE_ALIGN,
3301 if (!base->desc_slab)
3304 ret = devm_add_action_or_reset(dev, d40_drop_kmem_cache_action,
3314 static void __init d40_hw_init(struct d40_base *base)
3318 u32 prmseo[2] = {0, 0};
3319 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3322 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3323 u32 reg_size = base->gen_dmac.init_reg_size;
3325 for (i = 0; i < reg_size; i++)
3326 writel(dma_init_reg[i].val,
3327 base->virtbase + dma_init_reg[i].reg);
3329 /* Configure all our dma channels to default settings */
3330 for (i = 0; i < base->num_phy_chans; i++) {
3332 activeo[i % 2] = activeo[i % 2] << 2;
3334 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3336 activeo[i % 2] |= 3;
3340 /* Enable interrupt # */
3341 pcmis = (pcmis << 1) | 1;
3343 /* Clear interrupt # */
3344 pcicr = (pcicr << 1) | 1;
3346 /* Set channel to physical mode */
3347 prmseo[i % 2] = prmseo[i % 2] << 2;
3352 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3353 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3354 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3355 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3357 /* Write which interrupt to enable */
3358 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
3360 /* Write which interrupt to clear */
3361 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
3363 /* These are __initdata and cannot be accessed after init */
3364 base->gen_dmac.init_reg = NULL;
3365 base->gen_dmac.init_reg_size = 0;
3368 static int __init d40_lcla_allocate(struct d40_base *base)
3370 struct d40_lcla_pool *pool = &base->lcla_pool;
3371 unsigned long *page_list;
3376 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3377 * To full fill this hardware requirement without wasting 256 kb
3378 * we allocate pages until we get an aligned one.
3380 page_list = kmalloc_array(MAX_LCLA_ALLOC_ATTEMPTS,
3386 /* Calculating how many pages that are required */
3387 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3389 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3390 page_list[i] = __get_free_pages(GFP_KERNEL,
3391 base->lcla_pool.pages);
3392 if (!page_list[i]) {
3394 d40_err(base->dev, "Failed to allocate %d pages.\n",
3395 base->lcla_pool.pages);
3398 for (j = 0; j < i; j++)
3399 free_pages(page_list[j], base->lcla_pool.pages);
3400 goto free_page_list;
3403 if ((virt_to_phys((void *)page_list[i]) &
3404 (LCLA_ALIGNMENT - 1)) == 0)
3408 for (j = 0; j < i; j++)
3409 free_pages(page_list[j], base->lcla_pool.pages);
3411 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3412 base->lcla_pool.base = (void *)page_list[i];
3415 * After many attempts and no succees with finding the correct
3416 * alignment, try with allocating a big buffer.
3419 "[%s] Failed to get %d pages @ 18 bit align.\n",
3420 __func__, base->lcla_pool.pages);
3421 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3422 base->num_phy_chans +
3425 if (!base->lcla_pool.base_unaligned) {
3427 goto free_page_list;
3430 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3434 pool->dma_addr = dma_map_single(base->dev, pool->base,
3435 SZ_1K * base->num_phy_chans,
3437 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3440 goto free_page_list;
3443 writel(virt_to_phys(base->lcla_pool.base),
3444 base->virtbase + D40_DREG_LCLA);
3451 static int __init d40_of_probe(struct device *dev,
3452 struct device_node *np)
3454 struct stedma40_platform_data *pdata;
3455 int num_phy = 0, num_memcpy = 0, num_disabled = 0;
3458 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3462 /* If absent this value will be obtained from h/w. */
3463 of_property_read_u32(np, "dma-channels", &num_phy);
3465 pdata->num_of_phy_chans = num_phy;
3467 list = of_get_property(np, "memcpy-channels", &num_memcpy);
3468 num_memcpy /= sizeof(*list);
3470 if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
3472 "Invalid number of memcpy channels specified (%d)\n",
3476 pdata->num_of_memcpy_chans = num_memcpy;
3478 of_property_read_u32_array(np, "memcpy-channels",
3479 dma40_memcpy_channels,
3482 list = of_get_property(np, "disabled-channels", &num_disabled);
3483 num_disabled /= sizeof(*list);
3485 if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
3487 "Invalid number of disabled channels specified (%d)\n",
3492 of_property_read_u32_array(np, "disabled-channels",
3493 pdata->disabled_channels,
3495 pdata->disabled_channels[num_disabled] = -1;
3497 dev->platform_data = pdata;
3502 static int __init d40_probe(struct platform_device *pdev)
3504 struct device *dev = &pdev->dev;
3505 struct device_node *np = pdev->dev.of_node;
3506 struct device_node *np_lcpa;
3507 struct d40_base *base;
3508 struct resource *res;
3509 struct resource res_lcpa;
3510 int num_reserved_chans;
3514 if (d40_of_probe(dev, np)) {
3516 goto report_failure;
3519 ret = d40_hw_detect_init(pdev, &base);
3521 goto report_failure;
3523 num_reserved_chans = d40_phy_res_init(base);
3525 platform_set_drvdata(pdev, base);
3527 spin_lock_init(&base->interrupt_lock);
3528 spin_lock_init(&base->execmd_lock);
3530 /* Get IO for logical channel parameter address (LCPA) */
3531 np_lcpa = of_parse_phandle(np, "sram", 0);
3533 dev_err(dev, "no LCPA SRAM node\n");
3535 goto report_failure;
3537 /* This is no device so read the address directly from the node */
3538 ret = of_address_to_resource(np_lcpa, 0, &res_lcpa);
3540 dev_err(dev, "no LCPA SRAM resource\n");
3541 goto report_failure;
3543 base->lcpa_size = resource_size(&res_lcpa);
3544 base->phy_lcpa = res_lcpa.start;
3545 dev_info(dev, "found LCPA SRAM at %pad, size %pa\n",
3546 &base->phy_lcpa, &base->lcpa_size);
3548 /* We make use of ESRAM memory for this. */
3549 val = readl(base->virtbase + D40_DREG_LCPA);
3550 if (base->phy_lcpa != val && val != 0) {
3552 "[%s] Mismatch LCPA dma 0x%x, def %08x\n",
3553 __func__, val, (u32)base->phy_lcpa);
3555 writel(base->phy_lcpa, base->virtbase + D40_DREG_LCPA);
3557 base->lcpa_base = devm_ioremap(dev, base->phy_lcpa, base->lcpa_size);
3558 if (!base->lcpa_base) {
3560 d40_err(dev, "Failed to ioremap LCPA region\n");
3561 goto report_failure;
3563 /* If lcla has to be located in ESRAM we don't need to allocate */
3564 if (base->plat_data->use_esram_lcla) {
3565 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3570 "No \"lcla_esram\" memory resource\n");
3571 goto report_failure;
3573 base->lcla_pool.base = devm_ioremap(dev, res->start,
3574 resource_size(res));
3575 if (!base->lcla_pool.base) {
3577 d40_err(dev, "Failed to ioremap LCLA region\n");
3578 goto report_failure;
3580 writel(res->start, base->virtbase + D40_DREG_LCLA);
3583 ret = d40_lcla_allocate(base);
3585 d40_err(dev, "Failed to allocate LCLA area\n");
3590 spin_lock_init(&base->lcla_pool.lock);
3592 base->irq = platform_get_irq(pdev, 0);
3593 if (base->irq < 0) {
3598 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
3600 d40_err(dev, "No IRQ defined\n");
3604 if (base->plat_data->use_esram_lcla) {
3606 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3607 if (IS_ERR(base->lcpa_regulator)) {
3608 d40_err(dev, "Failed to get lcpa_regulator\n");
3609 ret = PTR_ERR(base->lcpa_regulator);
3610 base->lcpa_regulator = NULL;
3614 ret = regulator_enable(base->lcpa_regulator);
3617 "Failed to enable lcpa_regulator\n");
3618 regulator_put(base->lcpa_regulator);
3619 base->lcpa_regulator = NULL;
3624 writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3626 pm_runtime_irq_safe(base->dev);
3627 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3628 pm_runtime_use_autosuspend(base->dev);
3629 pm_runtime_mark_last_busy(base->dev);
3630 pm_runtime_set_active(base->dev);
3631 pm_runtime_enable(base->dev);
3633 ret = d40_dmaengine_init(base, num_reserved_chans);
3637 ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3639 d40_err(dev, "Failed to set dma max seg size\n");
3645 ret = of_dma_controller_register(np, d40_xlate, NULL);
3648 "could not register of_dma_controller\n");
3652 dev_info(base->dev, "initialized\n");
3656 if (base->lcla_pool.dma_addr)
3657 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3658 SZ_1K * base->num_phy_chans,
3661 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3662 free_pages((unsigned long)base->lcla_pool.base,
3663 base->lcla_pool.pages);
3665 kfree(base->lcla_pool.base_unaligned);
3667 if (base->lcpa_regulator) {
3668 regulator_disable(base->lcpa_regulator);
3669 regulator_put(base->lcpa_regulator);
3673 d40_err(dev, "probe failed\n");
3677 static const struct of_device_id d40_match[] = {
3678 { .compatible = "stericsson,dma40", },
3682 static struct platform_driver d40_driver = {
3685 .pm = &dma40_pm_ops,
3686 .of_match_table = d40_match,
3690 static int __init stedma40_init(void)
3692 return platform_driver_probe(&d40_driver, d40_probe);
3694 subsys_initcall(stedma40_init);