Linux 3.12.70
[linux/fpc-iii.git] / drivers / dma / ste_dma40.c
blob20f22ecb712e062f533a5363d3c68c02d32da502
1 /*
2 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
4 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
6 * License terms: GNU General Public License (GPL) version 2
7 */
9 #include <linux/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/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/err.h>
20 #include <linux/of.h>
21 #include <linux/of_dma.h>
22 #include <linux/amba/bus.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/platform_data/dma-ste-dma40.h>
26 #include "dmaengine.h"
27 #include "ste_dma40_ll.h"
29 #define D40_NAME "dma40"
31 #define D40_PHY_CHAN -1
33 /* For masking out/in 2 bit channel positions */
34 #define D40_CHAN_POS(chan) (2 * (chan / 2))
35 #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
37 /* Maximum iterations taken before giving up suspending a channel */
38 #define D40_SUSPEND_MAX_IT 500
40 /* Milliseconds */
41 #define DMA40_AUTOSUSPEND_DELAY 100
43 /* Hardware requirement on LCLA alignment */
44 #define LCLA_ALIGNMENT 0x40000
46 /* Max number of links per event group */
47 #define D40_LCLA_LINK_PER_EVENT_GRP 128
48 #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
50 /* Max number of logical channels per physical channel */
51 #define D40_MAX_LOG_CHAN_PER_PHY 32
53 /* Attempts before giving up to trying to get pages that are aligned */
54 #define MAX_LCLA_ALLOC_ATTEMPTS 256
56 /* Bit markings for allocation map */
57 #define D40_ALLOC_FREE BIT(31)
58 #define D40_ALLOC_PHY BIT(30)
59 #define D40_ALLOC_LOG_FREE 0
61 #define D40_MEMCPY_MAX_CHANS 8
63 /* Reserved event lines for memcpy only. */
64 #define DB8500_DMA_MEMCPY_EV_0 51
65 #define DB8500_DMA_MEMCPY_EV_1 56
66 #define DB8500_DMA_MEMCPY_EV_2 57
67 #define DB8500_DMA_MEMCPY_EV_3 58
68 #define DB8500_DMA_MEMCPY_EV_4 59
69 #define DB8500_DMA_MEMCPY_EV_5 60
71 static int dma40_memcpy_channels[] = {
72 DB8500_DMA_MEMCPY_EV_0,
73 DB8500_DMA_MEMCPY_EV_1,
74 DB8500_DMA_MEMCPY_EV_2,
75 DB8500_DMA_MEMCPY_EV_3,
76 DB8500_DMA_MEMCPY_EV_4,
77 DB8500_DMA_MEMCPY_EV_5,
80 /* Default configuration for physcial memcpy */
81 static struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
82 .mode = STEDMA40_MODE_PHYSICAL,
83 .dir = DMA_MEM_TO_MEM,
85 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
86 .src_info.psize = STEDMA40_PSIZE_PHY_1,
87 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
89 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
90 .dst_info.psize = STEDMA40_PSIZE_PHY_1,
91 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
94 /* Default configuration for logical memcpy */
95 static struct stedma40_chan_cfg dma40_memcpy_conf_log = {
96 .mode = STEDMA40_MODE_LOGICAL,
97 .dir = DMA_MEM_TO_MEM,
99 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
100 .src_info.psize = STEDMA40_PSIZE_LOG_1,
101 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
103 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
104 .dst_info.psize = STEDMA40_PSIZE_LOG_1,
105 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
109 * enum 40_command - The different commands and/or statuses.
111 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
112 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
113 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
114 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
116 enum d40_command {
117 D40_DMA_STOP = 0,
118 D40_DMA_RUN = 1,
119 D40_DMA_SUSPEND_REQ = 2,
120 D40_DMA_SUSPENDED = 3
124 * enum d40_events - The different Event Enables for the event lines.
126 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
127 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
128 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
129 * @D40_ROUND_EVENTLINE: Status check for event line.
132 enum d40_events {
133 D40_DEACTIVATE_EVENTLINE = 0,
134 D40_ACTIVATE_EVENTLINE = 1,
135 D40_SUSPEND_REQ_EVENTLINE = 2,
136 D40_ROUND_EVENTLINE = 3
140 * These are the registers that has to be saved and later restored
141 * when the DMA hw is powered off.
142 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
144 static u32 d40_backup_regs[] = {
145 D40_DREG_LCPA,
146 D40_DREG_LCLA,
147 D40_DREG_PRMSE,
148 D40_DREG_PRMSO,
149 D40_DREG_PRMOE,
150 D40_DREG_PRMOO,
153 #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
156 * since 9540 and 8540 has the same HW revision
157 * use v4a for 9540 or ealier
158 * use v4b for 8540 or later
159 * HW revision:
160 * DB8500ed has revision 0
161 * DB8500v1 has revision 2
162 * DB8500v2 has revision 3
163 * AP9540v1 has revision 4
164 * DB8540v1 has revision 4
165 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
167 static u32 d40_backup_regs_v4a[] = {
168 D40_DREG_PSEG1,
169 D40_DREG_PSEG2,
170 D40_DREG_PSEG3,
171 D40_DREG_PSEG4,
172 D40_DREG_PCEG1,
173 D40_DREG_PCEG2,
174 D40_DREG_PCEG3,
175 D40_DREG_PCEG4,
176 D40_DREG_RSEG1,
177 D40_DREG_RSEG2,
178 D40_DREG_RSEG3,
179 D40_DREG_RSEG4,
180 D40_DREG_RCEG1,
181 D40_DREG_RCEG2,
182 D40_DREG_RCEG3,
183 D40_DREG_RCEG4,
186 #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
188 static u32 d40_backup_regs_v4b[] = {
189 D40_DREG_CPSEG1,
190 D40_DREG_CPSEG2,
191 D40_DREG_CPSEG3,
192 D40_DREG_CPSEG4,
193 D40_DREG_CPSEG5,
194 D40_DREG_CPCEG1,
195 D40_DREG_CPCEG2,
196 D40_DREG_CPCEG3,
197 D40_DREG_CPCEG4,
198 D40_DREG_CPCEG5,
199 D40_DREG_CRSEG1,
200 D40_DREG_CRSEG2,
201 D40_DREG_CRSEG3,
202 D40_DREG_CRSEG4,
203 D40_DREG_CRSEG5,
204 D40_DREG_CRCEG1,
205 D40_DREG_CRCEG2,
206 D40_DREG_CRCEG3,
207 D40_DREG_CRCEG4,
208 D40_DREG_CRCEG5,
211 #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
213 static u32 d40_backup_regs_chan[] = {
214 D40_CHAN_REG_SSCFG,
215 D40_CHAN_REG_SSELT,
216 D40_CHAN_REG_SSPTR,
217 D40_CHAN_REG_SSLNK,
218 D40_CHAN_REG_SDCFG,
219 D40_CHAN_REG_SDELT,
220 D40_CHAN_REG_SDPTR,
221 D40_CHAN_REG_SDLNK,
224 #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
225 BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
228 * struct d40_interrupt_lookup - lookup table for interrupt handler
230 * @src: Interrupt mask register.
231 * @clr: Interrupt clear register.
232 * @is_error: true if this is an error interrupt.
233 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
234 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
236 struct d40_interrupt_lookup {
237 u32 src;
238 u32 clr;
239 bool is_error;
240 int offset;
244 static struct d40_interrupt_lookup il_v4a[] = {
245 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
246 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
247 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
248 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
249 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
250 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
251 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
252 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
253 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
254 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
257 static struct d40_interrupt_lookup il_v4b[] = {
258 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
259 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
260 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
261 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
262 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
263 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
264 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
265 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
266 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
267 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
268 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
269 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
273 * struct d40_reg_val - simple lookup struct
275 * @reg: The register.
276 * @val: The value that belongs to the register in reg.
278 struct d40_reg_val {
279 unsigned int reg;
280 unsigned int val;
283 static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
284 /* Clock every part of the DMA block from start */
285 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
287 /* Interrupts on all logical channels */
288 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
289 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
290 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
291 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
292 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
293 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
294 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
295 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
296 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
297 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
298 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
299 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
301 static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
302 /* Clock every part of the DMA block from start */
303 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
305 /* Interrupts on all logical channels */
306 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
307 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
308 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
309 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
310 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
311 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
312 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
313 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
314 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
315 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
316 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
317 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
318 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
319 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
320 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
324 * struct d40_lli_pool - Structure for keeping LLIs in memory
326 * @base: Pointer to memory area when the pre_alloc_lli's are not large
327 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
328 * pre_alloc_lli is used.
329 * @dma_addr: DMA address, if mapped
330 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
331 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
332 * one buffer to one buffer.
334 struct d40_lli_pool {
335 void *base;
336 int size;
337 dma_addr_t dma_addr;
338 /* Space for dst and src, plus an extra for padding */
339 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
343 * struct d40_desc - A descriptor is one DMA job.
345 * @lli_phy: LLI settings for physical channel. Both src and dst=
346 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
347 * lli_len equals one.
348 * @lli_log: Same as above but for logical channels.
349 * @lli_pool: The pool with two entries pre-allocated.
350 * @lli_len: Number of llis of current descriptor.
351 * @lli_current: Number of transferred llis.
352 * @lcla_alloc: Number of LCLA entries allocated.
353 * @txd: DMA engine struct. Used for among other things for communication
354 * during a transfer.
355 * @node: List entry.
356 * @is_in_client_list: true if the client owns this descriptor.
357 * @cyclic: true if this is a cyclic job
359 * This descriptor is used for both logical and physical transfers.
361 struct d40_desc {
362 /* LLI physical */
363 struct d40_phy_lli_bidir lli_phy;
364 /* LLI logical */
365 struct d40_log_lli_bidir lli_log;
367 struct d40_lli_pool lli_pool;
368 int lli_len;
369 int lli_current;
370 int lcla_alloc;
372 struct dma_async_tx_descriptor txd;
373 struct list_head node;
375 bool is_in_client_list;
376 bool cyclic;
380 * struct d40_lcla_pool - LCLA pool settings and data.
382 * @base: The virtual address of LCLA. 18 bit aligned.
383 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
384 * This pointer is only there for clean-up on error.
385 * @pages: The number of pages needed for all physical channels.
386 * Only used later for clean-up on error
387 * @lock: Lock to protect the content in this struct.
388 * @alloc_map: big map over which LCLA entry is own by which job.
390 struct d40_lcla_pool {
391 void *base;
392 dma_addr_t dma_addr;
393 void *base_unaligned;
394 int pages;
395 spinlock_t lock;
396 struct d40_desc **alloc_map;
400 * struct d40_phy_res - struct for handling eventlines mapped to physical
401 * channels.
403 * @lock: A lock protection this entity.
404 * @reserved: True if used by secure world or otherwise.
405 * @num: The physical channel number of this entity.
406 * @allocated_src: Bit mapped to show which src event line's are mapped to
407 * this physical channel. Can also be free or physically allocated.
408 * @allocated_dst: Same as for src but is dst.
409 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
410 * event line number.
411 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
413 struct d40_phy_res {
414 spinlock_t lock;
415 bool reserved;
416 int num;
417 u32 allocated_src;
418 u32 allocated_dst;
419 bool use_soft_lli;
422 struct d40_base;
425 * struct d40_chan - Struct that describes a channel.
427 * @lock: A spinlock to protect this struct.
428 * @log_num: The logical number, if any of this channel.
429 * @pending_tx: The number of pending transfers. Used between interrupt handler
430 * and tasklet.
431 * @busy: Set to true when transfer is ongoing on this channel.
432 * @phy_chan: Pointer to physical channel which this instance runs on. If this
433 * point is NULL, then the channel is not allocated.
434 * @chan: DMA engine handle.
435 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
436 * transfer and call client callback.
437 * @client: Cliented owned descriptor list.
438 * @pending_queue: Submitted jobs, to be issued by issue_pending()
439 * @active: Active descriptor.
440 * @done: Completed jobs
441 * @queue: Queued jobs.
442 * @prepare_queue: Prepared jobs.
443 * @dma_cfg: The client configuration of this dma channel.
444 * @configured: whether the dma_cfg configuration is valid
445 * @base: Pointer to the device instance struct.
446 * @src_def_cfg: Default cfg register setting for src.
447 * @dst_def_cfg: Default cfg register setting for dst.
448 * @log_def: Default logical channel settings.
449 * @lcpa: Pointer to dst and src lcpa settings.
450 * @runtime_addr: runtime configured address.
451 * @runtime_direction: runtime configured direction.
453 * This struct can either "be" a logical or a physical channel.
455 struct d40_chan {
456 spinlock_t lock;
457 int log_num;
458 int pending_tx;
459 bool busy;
460 struct d40_phy_res *phy_chan;
461 struct dma_chan chan;
462 struct tasklet_struct tasklet;
463 struct list_head client;
464 struct list_head pending_queue;
465 struct list_head active;
466 struct list_head done;
467 struct list_head queue;
468 struct list_head prepare_queue;
469 struct stedma40_chan_cfg dma_cfg;
470 bool configured;
471 struct d40_base *base;
472 /* Default register configurations */
473 u32 src_def_cfg;
474 u32 dst_def_cfg;
475 struct d40_def_lcsp log_def;
476 struct d40_log_lli_full *lcpa;
477 /* Runtime reconfiguration */
478 dma_addr_t runtime_addr;
479 enum dma_transfer_direction runtime_direction;
483 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
484 * controller
486 * @backup: the pointer to the registers address array for backup
487 * @backup_size: the size of the registers address array for backup
488 * @realtime_en: the realtime enable register
489 * @realtime_clear: the realtime clear register
490 * @high_prio_en: the high priority enable register
491 * @high_prio_clear: the high priority clear register
492 * @interrupt_en: the interrupt enable register
493 * @interrupt_clear: the interrupt clear register
494 * @il: the pointer to struct d40_interrupt_lookup
495 * @il_size: the size of d40_interrupt_lookup array
496 * @init_reg: the pointer to the struct d40_reg_val
497 * @init_reg_size: the size of d40_reg_val array
499 struct d40_gen_dmac {
500 u32 *backup;
501 u32 backup_size;
502 u32 realtime_en;
503 u32 realtime_clear;
504 u32 high_prio_en;
505 u32 high_prio_clear;
506 u32 interrupt_en;
507 u32 interrupt_clear;
508 struct d40_interrupt_lookup *il;
509 u32 il_size;
510 struct d40_reg_val *init_reg;
511 u32 init_reg_size;
515 * struct d40_base - The big global struct, one for each probe'd instance.
517 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
518 * @execmd_lock: Lock for execute command usage since several channels share
519 * the same physical register.
520 * @dev: The device structure.
521 * @virtbase: The virtual base address of the DMA's register.
522 * @rev: silicon revision detected.
523 * @clk: Pointer to the DMA clock structure.
524 * @phy_start: Physical memory start of the DMA registers.
525 * @phy_size: Size of the DMA register map.
526 * @irq: The IRQ number.
527 * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
528 * transfers).
529 * @num_phy_chans: The number of physical channels. Read from HW. This
530 * is the number of available channels for this driver, not counting "Secure
531 * mode" allocated physical channels.
532 * @num_log_chans: The number of logical channels. Calculated from
533 * num_phy_chans.
534 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
535 * @dma_slave: dma_device channels that can do only do slave transfers.
536 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
537 * @phy_chans: Room for all possible physical channels in system.
538 * @log_chans: Room for all possible logical channels in system.
539 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
540 * to log_chans entries.
541 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
542 * to phy_chans entries.
543 * @plat_data: Pointer to provided platform_data which is the driver
544 * configuration.
545 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
546 * @phy_res: Vector containing all physical channels.
547 * @lcla_pool: lcla pool settings and data.
548 * @lcpa_base: The virtual mapped address of LCPA.
549 * @phy_lcpa: The physical address of the LCPA.
550 * @lcpa_size: The size of the LCPA area.
551 * @desc_slab: cache for descriptors.
552 * @reg_val_backup: Here the values of some hardware registers are stored
553 * before the DMA is powered off. They are restored when the power is back on.
554 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
555 * later
556 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
557 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
558 * @initialized: true if the dma has been initialized
559 * @gen_dmac: the struct for generic registers values to represent u8500/8540
560 * DMA controller
562 struct d40_base {
563 spinlock_t interrupt_lock;
564 spinlock_t execmd_lock;
565 struct device *dev;
566 void __iomem *virtbase;
567 u8 rev:4;
568 struct clk *clk;
569 phys_addr_t phy_start;
570 resource_size_t phy_size;
571 int irq;
572 int num_memcpy_chans;
573 int num_phy_chans;
574 int num_log_chans;
575 struct device_dma_parameters dma_parms;
576 struct dma_device dma_both;
577 struct dma_device dma_slave;
578 struct dma_device dma_memcpy;
579 struct d40_chan *phy_chans;
580 struct d40_chan *log_chans;
581 struct d40_chan **lookup_log_chans;
582 struct d40_chan **lookup_phy_chans;
583 struct stedma40_platform_data *plat_data;
584 struct regulator *lcpa_regulator;
585 /* Physical half channels */
586 struct d40_phy_res *phy_res;
587 struct d40_lcla_pool lcla_pool;
588 void *lcpa_base;
589 dma_addr_t phy_lcpa;
590 resource_size_t lcpa_size;
591 struct kmem_cache *desc_slab;
592 u32 reg_val_backup[BACKUP_REGS_SZ];
593 u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
594 u32 *reg_val_backup_chan;
595 u16 gcc_pwr_off_mask;
596 bool initialized;
597 struct d40_gen_dmac gen_dmac;
600 static struct device *chan2dev(struct d40_chan *d40c)
602 return &d40c->chan.dev->device;
605 static bool chan_is_physical(struct d40_chan *chan)
607 return chan->log_num == D40_PHY_CHAN;
610 static bool chan_is_logical(struct d40_chan *chan)
612 return !chan_is_physical(chan);
615 static void __iomem *chan_base(struct d40_chan *chan)
617 return chan->base->virtbase + D40_DREG_PCBASE +
618 chan->phy_chan->num * D40_DREG_PCDELTA;
621 #define d40_err(dev, format, arg...) \
622 dev_err(dev, "[%s] " format, __func__, ## arg)
624 #define chan_err(d40c, format, arg...) \
625 d40_err(chan2dev(d40c), format, ## arg)
627 static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
628 int lli_len)
630 bool is_log = chan_is_logical(d40c);
631 u32 align;
632 void *base;
634 if (is_log)
635 align = sizeof(struct d40_log_lli);
636 else
637 align = sizeof(struct d40_phy_lli);
639 if (lli_len == 1) {
640 base = d40d->lli_pool.pre_alloc_lli;
641 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
642 d40d->lli_pool.base = NULL;
643 } else {
644 d40d->lli_pool.size = lli_len * 2 * align;
646 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
647 d40d->lli_pool.base = base;
649 if (d40d->lli_pool.base == NULL)
650 return -ENOMEM;
653 if (is_log) {
654 d40d->lli_log.src = PTR_ALIGN(base, align);
655 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
657 d40d->lli_pool.dma_addr = 0;
658 } else {
659 d40d->lli_phy.src = PTR_ALIGN(base, align);
660 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
662 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
663 d40d->lli_phy.src,
664 d40d->lli_pool.size,
665 DMA_TO_DEVICE);
667 if (dma_mapping_error(d40c->base->dev,
668 d40d->lli_pool.dma_addr)) {
669 kfree(d40d->lli_pool.base);
670 d40d->lli_pool.base = NULL;
671 d40d->lli_pool.dma_addr = 0;
672 return -ENOMEM;
676 return 0;
679 static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
681 if (d40d->lli_pool.dma_addr)
682 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
683 d40d->lli_pool.size, DMA_TO_DEVICE);
685 kfree(d40d->lli_pool.base);
686 d40d->lli_pool.base = NULL;
687 d40d->lli_pool.size = 0;
688 d40d->lli_log.src = NULL;
689 d40d->lli_log.dst = NULL;
690 d40d->lli_phy.src = NULL;
691 d40d->lli_phy.dst = NULL;
694 static int d40_lcla_alloc_one(struct d40_chan *d40c,
695 struct d40_desc *d40d)
697 unsigned long flags;
698 int i;
699 int ret = -EINVAL;
701 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
704 * Allocate both src and dst at the same time, therefore the half
705 * start on 1 since 0 can't be used since zero is used as end marker.
707 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
708 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
710 if (!d40c->base->lcla_pool.alloc_map[idx]) {
711 d40c->base->lcla_pool.alloc_map[idx] = d40d;
712 d40d->lcla_alloc++;
713 ret = i;
714 break;
718 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
720 return ret;
723 static int d40_lcla_free_all(struct d40_chan *d40c,
724 struct d40_desc *d40d)
726 unsigned long flags;
727 int i;
728 int ret = -EINVAL;
730 if (chan_is_physical(d40c))
731 return 0;
733 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
735 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
736 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
738 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
739 d40c->base->lcla_pool.alloc_map[idx] = NULL;
740 d40d->lcla_alloc--;
741 if (d40d->lcla_alloc == 0) {
742 ret = 0;
743 break;
748 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
750 return ret;
754 static void d40_desc_remove(struct d40_desc *d40d)
756 list_del(&d40d->node);
759 static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
761 struct d40_desc *desc = NULL;
763 if (!list_empty(&d40c->client)) {
764 struct d40_desc *d;
765 struct d40_desc *_d;
767 list_for_each_entry_safe(d, _d, &d40c->client, node) {
768 if (async_tx_test_ack(&d->txd)) {
769 d40_desc_remove(d);
770 desc = d;
771 memset(desc, 0, sizeof(*desc));
772 break;
777 if (!desc)
778 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
780 if (desc)
781 INIT_LIST_HEAD(&desc->node);
783 return desc;
786 static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
789 d40_pool_lli_free(d40c, d40d);
790 d40_lcla_free_all(d40c, d40d);
791 kmem_cache_free(d40c->base->desc_slab, d40d);
794 static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
796 list_add_tail(&desc->node, &d40c->active);
799 static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
801 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
802 struct d40_phy_lli *lli_src = desc->lli_phy.src;
803 void __iomem *base = chan_base(chan);
805 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
806 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
807 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
808 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
810 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
811 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
812 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
813 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
816 static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
818 list_add_tail(&desc->node, &d40c->done);
821 static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
823 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
824 struct d40_log_lli_bidir *lli = &desc->lli_log;
825 int lli_current = desc->lli_current;
826 int lli_len = desc->lli_len;
827 bool cyclic = desc->cyclic;
828 int curr_lcla = -EINVAL;
829 int first_lcla = 0;
830 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
831 bool linkback;
834 * We may have partially running cyclic transfers, in case we did't get
835 * enough LCLA entries.
837 linkback = cyclic && lli_current == 0;
840 * For linkback, we need one LCLA even with only one link, because we
841 * can't link back to the one in LCPA space
843 if (linkback || (lli_len - lli_current > 1)) {
845 * If the channel is expected to use only soft_lli don't
846 * allocate a lcla. This is to avoid a HW issue that exists
847 * in some controller during a peripheral to memory transfer
848 * that uses linked lists.
850 if (!(chan->phy_chan->use_soft_lli &&
851 chan->dma_cfg.dir == DMA_DEV_TO_MEM))
852 curr_lcla = d40_lcla_alloc_one(chan, desc);
854 first_lcla = curr_lcla;
858 * For linkback, we normally load the LCPA in the loop since we need to
859 * link it to the second LCLA and not the first. However, if we
860 * couldn't even get a first LCLA, then we have to run in LCPA and
861 * reload manually.
863 if (!linkback || curr_lcla == -EINVAL) {
864 unsigned int flags = 0;
866 if (curr_lcla == -EINVAL)
867 flags |= LLI_TERM_INT;
869 d40_log_lli_lcpa_write(chan->lcpa,
870 &lli->dst[lli_current],
871 &lli->src[lli_current],
872 curr_lcla,
873 flags);
874 lli_current++;
877 if (curr_lcla < 0)
878 goto out;
880 for (; lli_current < lli_len; lli_current++) {
881 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
882 8 * curr_lcla * 2;
883 struct d40_log_lli *lcla = pool->base + lcla_offset;
884 unsigned int flags = 0;
885 int next_lcla;
887 if (lli_current + 1 < lli_len)
888 next_lcla = d40_lcla_alloc_one(chan, desc);
889 else
890 next_lcla = linkback ? first_lcla : -EINVAL;
892 if (cyclic || next_lcla == -EINVAL)
893 flags |= LLI_TERM_INT;
895 if (linkback && curr_lcla == first_lcla) {
896 /* First link goes in both LCPA and LCLA */
897 d40_log_lli_lcpa_write(chan->lcpa,
898 &lli->dst[lli_current],
899 &lli->src[lli_current],
900 next_lcla, flags);
904 * One unused LCLA in the cyclic case if the very first
905 * next_lcla fails...
907 d40_log_lli_lcla_write(lcla,
908 &lli->dst[lli_current],
909 &lli->src[lli_current],
910 next_lcla, flags);
913 * Cache maintenance is not needed if lcla is
914 * mapped in esram
916 if (!use_esram_lcla) {
917 dma_sync_single_range_for_device(chan->base->dev,
918 pool->dma_addr, lcla_offset,
919 2 * sizeof(struct d40_log_lli),
920 DMA_TO_DEVICE);
922 curr_lcla = next_lcla;
924 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
925 lli_current++;
926 break;
930 out:
931 desc->lli_current = lli_current;
934 static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
936 if (chan_is_physical(d40c)) {
937 d40_phy_lli_load(d40c, d40d);
938 d40d->lli_current = d40d->lli_len;
939 } else
940 d40_log_lli_to_lcxa(d40c, d40d);
943 static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
945 struct d40_desc *d;
947 if (list_empty(&d40c->active))
948 return NULL;
950 d = list_first_entry(&d40c->active,
951 struct d40_desc,
952 node);
953 return d;
956 /* remove desc from current queue and add it to the pending_queue */
957 static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
959 d40_desc_remove(desc);
960 desc->is_in_client_list = false;
961 list_add_tail(&desc->node, &d40c->pending_queue);
964 static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
966 struct d40_desc *d;
968 if (list_empty(&d40c->pending_queue))
969 return NULL;
971 d = list_first_entry(&d40c->pending_queue,
972 struct d40_desc,
973 node);
974 return d;
977 static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
979 struct d40_desc *d;
981 if (list_empty(&d40c->queue))
982 return NULL;
984 d = list_first_entry(&d40c->queue,
985 struct d40_desc,
986 node);
987 return d;
990 static struct d40_desc *d40_first_done(struct d40_chan *d40c)
992 if (list_empty(&d40c->done))
993 return NULL;
995 return list_first_entry(&d40c->done, struct d40_desc, node);
998 static int d40_psize_2_burst_size(bool is_log, int psize)
1000 if (is_log) {
1001 if (psize == STEDMA40_PSIZE_LOG_1)
1002 return 1;
1003 } else {
1004 if (psize == STEDMA40_PSIZE_PHY_1)
1005 return 1;
1008 return 2 << psize;
1012 * The dma only supports transmitting packages up to
1013 * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
1015 * Calculate the total number of dma elements required to send the entire sg list.
1017 static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1019 int dmalen;
1020 u32 max_w = max(data_width1, data_width2);
1021 u32 min_w = min(data_width1, data_width2);
1022 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1024 if (seg_max > STEDMA40_MAX_SEG_SIZE)
1025 seg_max -= max_w;
1027 if (!IS_ALIGNED(size, max_w))
1028 return -EINVAL;
1030 if (size <= seg_max)
1031 dmalen = 1;
1032 else {
1033 dmalen = size / seg_max;
1034 if (dmalen * seg_max < size)
1035 dmalen++;
1037 return dmalen;
1040 static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1041 u32 data_width1, u32 data_width2)
1043 struct scatterlist *sg;
1044 int i;
1045 int len = 0;
1046 int ret;
1048 for_each_sg(sgl, sg, sg_len, i) {
1049 ret = d40_size_2_dmalen(sg_dma_len(sg),
1050 data_width1, data_width2);
1051 if (ret < 0)
1052 return ret;
1053 len += ret;
1055 return len;
1059 #ifdef CONFIG_PM
1060 static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1061 u32 *regaddr, int num, bool save)
1063 int i;
1065 for (i = 0; i < num; i++) {
1066 void __iomem *addr = baseaddr + regaddr[i];
1068 if (save)
1069 backup[i] = readl_relaxed(addr);
1070 else
1071 writel_relaxed(backup[i], addr);
1075 static void d40_save_restore_registers(struct d40_base *base, bool save)
1077 int i;
1079 /* Save/Restore channel specific registers */
1080 for (i = 0; i < base->num_phy_chans; i++) {
1081 void __iomem *addr;
1082 int idx;
1084 if (base->phy_res[i].reserved)
1085 continue;
1087 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1088 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1090 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1091 d40_backup_regs_chan,
1092 ARRAY_SIZE(d40_backup_regs_chan),
1093 save);
1096 /* Save/Restore global registers */
1097 dma40_backup(base->virtbase, base->reg_val_backup,
1098 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1099 save);
1101 /* Save/Restore registers only existing on dma40 v3 and later */
1102 if (base->gen_dmac.backup)
1103 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1104 base->gen_dmac.backup,
1105 base->gen_dmac.backup_size,
1106 save);
1108 #else
1109 static void d40_save_restore_registers(struct d40_base *base, bool save)
1112 #endif
1114 static int __d40_execute_command_phy(struct d40_chan *d40c,
1115 enum d40_command command)
1117 u32 status;
1118 int i;
1119 void __iomem *active_reg;
1120 int ret = 0;
1121 unsigned long flags;
1122 u32 wmask;
1124 if (command == D40_DMA_STOP) {
1125 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1126 if (ret)
1127 return ret;
1130 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1132 if (d40c->phy_chan->num % 2 == 0)
1133 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1134 else
1135 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1137 if (command == D40_DMA_SUSPEND_REQ) {
1138 status = (readl(active_reg) &
1139 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1140 D40_CHAN_POS(d40c->phy_chan->num);
1142 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1143 goto done;
1146 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1147 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1148 active_reg);
1150 if (command == D40_DMA_SUSPEND_REQ) {
1152 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1153 status = (readl(active_reg) &
1154 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1155 D40_CHAN_POS(d40c->phy_chan->num);
1157 cpu_relax();
1159 * Reduce the number of bus accesses while
1160 * waiting for the DMA to suspend.
1162 udelay(3);
1164 if (status == D40_DMA_STOP ||
1165 status == D40_DMA_SUSPENDED)
1166 break;
1169 if (i == D40_SUSPEND_MAX_IT) {
1170 chan_err(d40c,
1171 "unable to suspend the chl %d (log: %d) status %x\n",
1172 d40c->phy_chan->num, d40c->log_num,
1173 status);
1174 dump_stack();
1175 ret = -EBUSY;
1179 done:
1180 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1181 return ret;
1184 static void d40_term_all(struct d40_chan *d40c)
1186 struct d40_desc *d40d;
1187 struct d40_desc *_d;
1189 /* Release completed descriptors */
1190 while ((d40d = d40_first_done(d40c))) {
1191 d40_desc_remove(d40d);
1192 d40_desc_free(d40c, d40d);
1195 /* Release active descriptors */
1196 while ((d40d = d40_first_active_get(d40c))) {
1197 d40_desc_remove(d40d);
1198 d40_desc_free(d40c, d40d);
1201 /* Release queued descriptors waiting for transfer */
1202 while ((d40d = d40_first_queued(d40c))) {
1203 d40_desc_remove(d40d);
1204 d40_desc_free(d40c, d40d);
1207 /* Release pending descriptors */
1208 while ((d40d = d40_first_pending(d40c))) {
1209 d40_desc_remove(d40d);
1210 d40_desc_free(d40c, d40d);
1213 /* Release client owned descriptors */
1214 if (!list_empty(&d40c->client))
1215 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1216 d40_desc_remove(d40d);
1217 d40_desc_free(d40c, d40d);
1220 /* Release descriptors in prepare queue */
1221 if (!list_empty(&d40c->prepare_queue))
1222 list_for_each_entry_safe(d40d, _d,
1223 &d40c->prepare_queue, node) {
1224 d40_desc_remove(d40d);
1225 d40_desc_free(d40c, d40d);
1228 d40c->pending_tx = 0;
1231 static void __d40_config_set_event(struct d40_chan *d40c,
1232 enum d40_events event_type, u32 event,
1233 int reg)
1235 void __iomem *addr = chan_base(d40c) + reg;
1236 int tries;
1237 u32 status;
1239 switch (event_type) {
1241 case D40_DEACTIVATE_EVENTLINE:
1243 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1244 | ~D40_EVENTLINE_MASK(event), addr);
1245 break;
1247 case D40_SUSPEND_REQ_EVENTLINE:
1248 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1249 D40_EVENTLINE_POS(event);
1251 if (status == D40_DEACTIVATE_EVENTLINE ||
1252 status == D40_SUSPEND_REQ_EVENTLINE)
1253 break;
1255 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1256 | ~D40_EVENTLINE_MASK(event), addr);
1258 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1260 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1261 D40_EVENTLINE_POS(event);
1263 cpu_relax();
1265 * Reduce the number of bus accesses while
1266 * waiting for the DMA to suspend.
1268 udelay(3);
1270 if (status == D40_DEACTIVATE_EVENTLINE)
1271 break;
1274 if (tries == D40_SUSPEND_MAX_IT) {
1275 chan_err(d40c,
1276 "unable to stop the event_line chl %d (log: %d)"
1277 "status %x\n", d40c->phy_chan->num,
1278 d40c->log_num, status);
1280 break;
1282 case D40_ACTIVATE_EVENTLINE:
1284 * The hardware sometimes doesn't register the enable when src and dst
1285 * event lines are active on the same logical channel. Retry to ensure
1286 * it does. Usually only one retry is sufficient.
1288 tries = 100;
1289 while (--tries) {
1290 writel((D40_ACTIVATE_EVENTLINE <<
1291 D40_EVENTLINE_POS(event)) |
1292 ~D40_EVENTLINE_MASK(event), addr);
1294 if (readl(addr) & D40_EVENTLINE_MASK(event))
1295 break;
1298 if (tries != 99)
1299 dev_dbg(chan2dev(d40c),
1300 "[%s] workaround enable S%cLNK (%d tries)\n",
1301 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1302 100 - tries);
1304 WARN_ON(!tries);
1305 break;
1307 case D40_ROUND_EVENTLINE:
1308 BUG();
1309 break;
1314 static void d40_config_set_event(struct d40_chan *d40c,
1315 enum d40_events event_type)
1317 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
1319 /* Enable event line connected to device (or memcpy) */
1320 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
1321 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
1322 __d40_config_set_event(d40c, event_type, event,
1323 D40_CHAN_REG_SSLNK);
1325 if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
1326 __d40_config_set_event(d40c, event_type, event,
1327 D40_CHAN_REG_SDLNK);
1330 static u32 d40_chan_has_events(struct d40_chan *d40c)
1332 void __iomem *chanbase = chan_base(d40c);
1333 u32 val;
1335 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1336 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
1338 return val;
1341 static int
1342 __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1344 unsigned long flags;
1345 int ret = 0;
1346 u32 active_status;
1347 void __iomem *active_reg;
1349 if (d40c->phy_chan->num % 2 == 0)
1350 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1351 else
1352 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1355 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1357 switch (command) {
1358 case D40_DMA_STOP:
1359 case D40_DMA_SUSPEND_REQ:
1361 active_status = (readl(active_reg) &
1362 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1363 D40_CHAN_POS(d40c->phy_chan->num);
1365 if (active_status == D40_DMA_RUN)
1366 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1367 else
1368 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1370 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1371 ret = __d40_execute_command_phy(d40c, command);
1373 break;
1375 case D40_DMA_RUN:
1377 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1378 ret = __d40_execute_command_phy(d40c, command);
1379 break;
1381 case D40_DMA_SUSPENDED:
1382 BUG();
1383 break;
1386 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1387 return ret;
1390 static int d40_channel_execute_command(struct d40_chan *d40c,
1391 enum d40_command command)
1393 if (chan_is_logical(d40c))
1394 return __d40_execute_command_log(d40c, command);
1395 else
1396 return __d40_execute_command_phy(d40c, command);
1399 static u32 d40_get_prmo(struct d40_chan *d40c)
1401 static const unsigned int phy_map[] = {
1402 [STEDMA40_PCHAN_BASIC_MODE]
1403 = D40_DREG_PRMO_PCHAN_BASIC,
1404 [STEDMA40_PCHAN_MODULO_MODE]
1405 = D40_DREG_PRMO_PCHAN_MODULO,
1406 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1407 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1409 static const unsigned int log_map[] = {
1410 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1411 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1412 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1413 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1414 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1415 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1418 if (chan_is_physical(d40c))
1419 return phy_map[d40c->dma_cfg.mode_opt];
1420 else
1421 return log_map[d40c->dma_cfg.mode_opt];
1424 static void d40_config_write(struct d40_chan *d40c)
1426 u32 addr_base;
1427 u32 var;
1429 /* Odd addresses are even addresses + 4 */
1430 addr_base = (d40c->phy_chan->num % 2) * 4;
1431 /* Setup channel mode to logical or physical */
1432 var = ((u32)(chan_is_logical(d40c)) + 1) <<
1433 D40_CHAN_POS(d40c->phy_chan->num);
1434 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1436 /* Setup operational mode option register */
1437 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
1439 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1441 if (chan_is_logical(d40c)) {
1442 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1443 & D40_SREG_ELEM_LOG_LIDX_MASK;
1444 void __iomem *chanbase = chan_base(d40c);
1446 /* Set default config for CFG reg */
1447 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1448 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
1450 /* Set LIDX for lcla */
1451 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1452 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1454 /* Clear LNK which will be used by d40_chan_has_events() */
1455 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1456 writel(0, chanbase + D40_CHAN_REG_SDLNK);
1460 static u32 d40_residue(struct d40_chan *d40c)
1462 u32 num_elt;
1464 if (chan_is_logical(d40c))
1465 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1466 >> D40_MEM_LCSP2_ECNT_POS;
1467 else {
1468 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1469 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1470 >> D40_SREG_ELEM_PHY_ECNT_POS;
1473 return num_elt * d40c->dma_cfg.dst_info.data_width;
1476 static bool d40_tx_is_linked(struct d40_chan *d40c)
1478 bool is_link;
1480 if (chan_is_logical(d40c))
1481 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1482 else
1483 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1484 & D40_SREG_LNK_PHYS_LNK_MASK;
1486 return is_link;
1489 static int d40_pause(struct d40_chan *d40c)
1491 int res = 0;
1492 unsigned long flags;
1494 if (!d40c->busy)
1495 return 0;
1497 pm_runtime_get_sync(d40c->base->dev);
1498 spin_lock_irqsave(&d40c->lock, flags);
1500 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1502 pm_runtime_mark_last_busy(d40c->base->dev);
1503 pm_runtime_put_autosuspend(d40c->base->dev);
1504 spin_unlock_irqrestore(&d40c->lock, flags);
1505 return res;
1508 static int d40_resume(struct d40_chan *d40c)
1510 int res = 0;
1511 unsigned long flags;
1513 if (!d40c->busy)
1514 return 0;
1516 spin_lock_irqsave(&d40c->lock, flags);
1517 pm_runtime_get_sync(d40c->base->dev);
1519 /* If bytes left to transfer or linked tx resume job */
1520 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1521 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1523 pm_runtime_mark_last_busy(d40c->base->dev);
1524 pm_runtime_put_autosuspend(d40c->base->dev);
1525 spin_unlock_irqrestore(&d40c->lock, flags);
1526 return res;
1529 static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1531 struct d40_chan *d40c = container_of(tx->chan,
1532 struct d40_chan,
1533 chan);
1534 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1535 unsigned long flags;
1536 dma_cookie_t cookie;
1538 spin_lock_irqsave(&d40c->lock, flags);
1539 cookie = dma_cookie_assign(tx);
1540 d40_desc_queue(d40c, d40d);
1541 spin_unlock_irqrestore(&d40c->lock, flags);
1543 return cookie;
1546 static int d40_start(struct d40_chan *d40c)
1548 return d40_channel_execute_command(d40c, D40_DMA_RUN);
1551 static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1553 struct d40_desc *d40d;
1554 int err;
1556 /* Start queued jobs, if any */
1557 d40d = d40_first_queued(d40c);
1559 if (d40d != NULL) {
1560 if (!d40c->busy) {
1561 d40c->busy = true;
1562 pm_runtime_get_sync(d40c->base->dev);
1565 /* Remove from queue */
1566 d40_desc_remove(d40d);
1568 /* Add to active queue */
1569 d40_desc_submit(d40c, d40d);
1571 /* Initiate DMA job */
1572 d40_desc_load(d40c, d40d);
1574 /* Start dma job */
1575 err = d40_start(d40c);
1577 if (err)
1578 return NULL;
1581 return d40d;
1584 /* called from interrupt context */
1585 static void dma_tc_handle(struct d40_chan *d40c)
1587 struct d40_desc *d40d;
1589 /* Get first active entry from list */
1590 d40d = d40_first_active_get(d40c);
1592 if (d40d == NULL)
1593 return;
1595 if (d40d->cyclic) {
1597 * If this was a paritially loaded list, we need to reloaded
1598 * it, and only when the list is completed. We need to check
1599 * for done because the interrupt will hit for every link, and
1600 * not just the last one.
1602 if (d40d->lli_current < d40d->lli_len
1603 && !d40_tx_is_linked(d40c)
1604 && !d40_residue(d40c)) {
1605 d40_lcla_free_all(d40c, d40d);
1606 d40_desc_load(d40c, d40d);
1607 (void) d40_start(d40c);
1609 if (d40d->lli_current == d40d->lli_len)
1610 d40d->lli_current = 0;
1612 } else {
1613 d40_lcla_free_all(d40c, d40d);
1615 if (d40d->lli_current < d40d->lli_len) {
1616 d40_desc_load(d40c, d40d);
1617 /* Start dma job */
1618 (void) d40_start(d40c);
1619 return;
1622 if (d40_queue_start(d40c) == NULL) {
1623 d40c->busy = false;
1625 pm_runtime_mark_last_busy(d40c->base->dev);
1626 pm_runtime_put_autosuspend(d40c->base->dev);
1629 d40_desc_remove(d40d);
1630 d40_desc_done(d40c, d40d);
1633 d40c->pending_tx++;
1634 tasklet_schedule(&d40c->tasklet);
1638 static void dma_tasklet(unsigned long data)
1640 struct d40_chan *d40c = (struct d40_chan *) data;
1641 struct d40_desc *d40d;
1642 unsigned long flags;
1643 bool callback_active;
1644 dma_async_tx_callback callback;
1645 void *callback_param;
1647 spin_lock_irqsave(&d40c->lock, flags);
1649 /* Get first entry from the done list */
1650 d40d = d40_first_done(d40c);
1651 if (d40d == NULL) {
1652 /* Check if we have reached here for cyclic job */
1653 d40d = d40_first_active_get(d40c);
1654 if (d40d == NULL || !d40d->cyclic)
1655 goto err;
1658 if (!d40d->cyclic)
1659 dma_cookie_complete(&d40d->txd);
1662 * If terminating a channel pending_tx is set to zero.
1663 * This prevents any finished active jobs to return to the client.
1665 if (d40c->pending_tx == 0) {
1666 spin_unlock_irqrestore(&d40c->lock, flags);
1667 return;
1670 /* Callback to client */
1671 callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
1672 callback = d40d->txd.callback;
1673 callback_param = d40d->txd.callback_param;
1675 if (!d40d->cyclic) {
1676 if (async_tx_test_ack(&d40d->txd)) {
1677 d40_desc_remove(d40d);
1678 d40_desc_free(d40c, d40d);
1679 } else if (!d40d->is_in_client_list) {
1680 d40_desc_remove(d40d);
1681 d40_lcla_free_all(d40c, d40d);
1682 list_add_tail(&d40d->node, &d40c->client);
1683 d40d->is_in_client_list = true;
1687 d40c->pending_tx--;
1689 if (d40c->pending_tx)
1690 tasklet_schedule(&d40c->tasklet);
1692 spin_unlock_irqrestore(&d40c->lock, flags);
1694 if (callback_active && callback)
1695 callback(callback_param);
1697 return;
1699 err:
1700 /* Rescue manouver if receiving double interrupts */
1701 if (d40c->pending_tx > 0)
1702 d40c->pending_tx--;
1703 spin_unlock_irqrestore(&d40c->lock, flags);
1706 static irqreturn_t d40_handle_interrupt(int irq, void *data)
1708 int i;
1709 u32 idx;
1710 u32 row;
1711 long chan = -1;
1712 struct d40_chan *d40c;
1713 unsigned long flags;
1714 struct d40_base *base = data;
1715 u32 regs[base->gen_dmac.il_size];
1716 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1717 u32 il_size = base->gen_dmac.il_size;
1719 spin_lock_irqsave(&base->interrupt_lock, flags);
1721 /* Read interrupt status of both logical and physical channels */
1722 for (i = 0; i < il_size; i++)
1723 regs[i] = readl(base->virtbase + il[i].src);
1725 for (;;) {
1727 chan = find_next_bit((unsigned long *)regs,
1728 BITS_PER_LONG * il_size, chan + 1);
1730 /* No more set bits found? */
1731 if (chan == BITS_PER_LONG * il_size)
1732 break;
1734 row = chan / BITS_PER_LONG;
1735 idx = chan & (BITS_PER_LONG - 1);
1737 if (il[row].offset == D40_PHY_CHAN)
1738 d40c = base->lookup_phy_chans[idx];
1739 else
1740 d40c = base->lookup_log_chans[il[row].offset + idx];
1742 if (!d40c) {
1744 * No error because this can happen if something else
1745 * in the system is using the channel.
1747 continue;
1750 /* ACK interrupt */
1751 writel(BIT(idx), base->virtbase + il[row].clr);
1753 spin_lock(&d40c->lock);
1755 if (!il[row].is_error)
1756 dma_tc_handle(d40c);
1757 else
1758 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1759 chan, il[row].offset, idx);
1761 spin_unlock(&d40c->lock);
1764 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1766 return IRQ_HANDLED;
1769 static int d40_validate_conf(struct d40_chan *d40c,
1770 struct stedma40_chan_cfg *conf)
1772 int res = 0;
1773 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
1775 if (!conf->dir) {
1776 chan_err(d40c, "Invalid direction.\n");
1777 res = -EINVAL;
1780 if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
1781 (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
1782 (conf->dev_type < 0)) {
1783 chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
1784 res = -EINVAL;
1787 if (conf->dir == DMA_DEV_TO_DEV) {
1789 * DMAC HW supports it. Will be added to this driver,
1790 * in case any dma client requires it.
1792 chan_err(d40c, "periph to periph not supported\n");
1793 res = -EINVAL;
1796 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1797 conf->src_info.data_width !=
1798 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1799 conf->dst_info.data_width) {
1801 * The DMAC hardware only supports
1802 * src (burst x width) == dst (burst x width)
1805 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1806 res = -EINVAL;
1809 return res;
1812 static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1813 bool is_src, int log_event_line, bool is_log,
1814 bool *first_user)
1816 unsigned long flags;
1817 spin_lock_irqsave(&phy->lock, flags);
1819 *first_user = ((phy->allocated_src | phy->allocated_dst)
1820 == D40_ALLOC_FREE);
1822 if (!is_log) {
1823 /* Physical interrupts are masked per physical full channel */
1824 if (phy->allocated_src == D40_ALLOC_FREE &&
1825 phy->allocated_dst == D40_ALLOC_FREE) {
1826 phy->allocated_dst = D40_ALLOC_PHY;
1827 phy->allocated_src = D40_ALLOC_PHY;
1828 goto found;
1829 } else
1830 goto not_found;
1833 /* Logical channel */
1834 if (is_src) {
1835 if (phy->allocated_src == D40_ALLOC_PHY)
1836 goto not_found;
1838 if (phy->allocated_src == D40_ALLOC_FREE)
1839 phy->allocated_src = D40_ALLOC_LOG_FREE;
1841 if (!(phy->allocated_src & BIT(log_event_line))) {
1842 phy->allocated_src |= BIT(log_event_line);
1843 goto found;
1844 } else
1845 goto not_found;
1846 } else {
1847 if (phy->allocated_dst == D40_ALLOC_PHY)
1848 goto not_found;
1850 if (phy->allocated_dst == D40_ALLOC_FREE)
1851 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1853 if (!(phy->allocated_dst & BIT(log_event_line))) {
1854 phy->allocated_dst |= BIT(log_event_line);
1855 goto found;
1856 } else
1857 goto not_found;
1860 not_found:
1861 spin_unlock_irqrestore(&phy->lock, flags);
1862 return false;
1863 found:
1864 spin_unlock_irqrestore(&phy->lock, flags);
1865 return true;
1868 static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1869 int log_event_line)
1871 unsigned long flags;
1872 bool is_free = false;
1874 spin_lock_irqsave(&phy->lock, flags);
1875 if (!log_event_line) {
1876 phy->allocated_dst = D40_ALLOC_FREE;
1877 phy->allocated_src = D40_ALLOC_FREE;
1878 is_free = true;
1879 goto out;
1882 /* Logical channel */
1883 if (is_src) {
1884 phy->allocated_src &= ~BIT(log_event_line);
1885 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1886 phy->allocated_src = D40_ALLOC_FREE;
1887 } else {
1888 phy->allocated_dst &= ~BIT(log_event_line);
1889 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1890 phy->allocated_dst = D40_ALLOC_FREE;
1893 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1894 D40_ALLOC_FREE);
1896 out:
1897 spin_unlock_irqrestore(&phy->lock, flags);
1899 return is_free;
1902 static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
1904 int dev_type = d40c->dma_cfg.dev_type;
1905 int event_group;
1906 int event_line;
1907 struct d40_phy_res *phys;
1908 int i;
1909 int j;
1910 int log_num;
1911 int num_phy_chans;
1912 bool is_src;
1913 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
1915 phys = d40c->base->phy_res;
1916 num_phy_chans = d40c->base->num_phy_chans;
1918 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
1919 log_num = 2 * dev_type;
1920 is_src = true;
1921 } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
1922 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1923 /* dst event lines are used for logical memcpy */
1924 log_num = 2 * dev_type + 1;
1925 is_src = false;
1926 } else
1927 return -EINVAL;
1929 event_group = D40_TYPE_TO_GROUP(dev_type);
1930 event_line = D40_TYPE_TO_EVENT(dev_type);
1932 if (!is_log) {
1933 if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1934 /* Find physical half channel */
1935 if (d40c->dma_cfg.use_fixed_channel) {
1936 i = d40c->dma_cfg.phy_channel;
1937 if (d40_alloc_mask_set(&phys[i], is_src,
1938 0, is_log,
1939 first_phy_user))
1940 goto found_phy;
1941 } else {
1942 for (i = 0; i < num_phy_chans; i++) {
1943 if (d40_alloc_mask_set(&phys[i], is_src,
1944 0, is_log,
1945 first_phy_user))
1946 goto found_phy;
1949 } else
1950 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1951 int phy_num = j + event_group * 2;
1952 for (i = phy_num; i < phy_num + 2; i++) {
1953 if (d40_alloc_mask_set(&phys[i],
1954 is_src,
1956 is_log,
1957 first_phy_user))
1958 goto found_phy;
1961 return -EINVAL;
1962 found_phy:
1963 d40c->phy_chan = &phys[i];
1964 d40c->log_num = D40_PHY_CHAN;
1965 goto out;
1967 if (dev_type == -1)
1968 return -EINVAL;
1970 /* Find logical channel */
1971 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1972 int phy_num = j + event_group * 2;
1974 if (d40c->dma_cfg.use_fixed_channel) {
1975 i = d40c->dma_cfg.phy_channel;
1977 if ((i != phy_num) && (i != phy_num + 1)) {
1978 dev_err(chan2dev(d40c),
1979 "invalid fixed phy channel %d\n", i);
1980 return -EINVAL;
1983 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1984 is_log, first_phy_user))
1985 goto found_log;
1987 dev_err(chan2dev(d40c),
1988 "could not allocate fixed phy channel %d\n", i);
1989 return -EINVAL;
1993 * Spread logical channels across all available physical rather
1994 * than pack every logical channel at the first available phy
1995 * channels.
1997 if (is_src) {
1998 for (i = phy_num; i < phy_num + 2; i++) {
1999 if (d40_alloc_mask_set(&phys[i], is_src,
2000 event_line, is_log,
2001 first_phy_user))
2002 goto found_log;
2004 } else {
2005 for (i = phy_num + 1; i >= phy_num; i--) {
2006 if (d40_alloc_mask_set(&phys[i], is_src,
2007 event_line, is_log,
2008 first_phy_user))
2009 goto found_log;
2013 return -EINVAL;
2015 found_log:
2016 d40c->phy_chan = &phys[i];
2017 d40c->log_num = log_num;
2018 out:
2020 if (is_log)
2021 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2022 else
2023 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2025 return 0;
2029 static int d40_config_memcpy(struct d40_chan *d40c)
2031 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2033 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
2034 d40c->dma_cfg = dma40_memcpy_conf_log;
2035 d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
2037 d40_log_cfg(&d40c->dma_cfg,
2038 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2040 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2041 dma_has_cap(DMA_SLAVE, cap)) {
2042 d40c->dma_cfg = dma40_memcpy_conf_phy;
2044 /* Generate interrrupt at end of transfer or relink. */
2045 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
2047 /* Generate interrupt on error. */
2048 d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2049 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2051 } else {
2052 chan_err(d40c, "No memcpy\n");
2053 return -EINVAL;
2056 return 0;
2059 static int d40_free_dma(struct d40_chan *d40c)
2062 int res = 0;
2063 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2064 struct d40_phy_res *phy = d40c->phy_chan;
2065 bool is_src;
2067 /* Terminate all queued and active transfers */
2068 d40_term_all(d40c);
2070 if (phy == NULL) {
2071 chan_err(d40c, "phy == null\n");
2072 return -EINVAL;
2075 if (phy->allocated_src == D40_ALLOC_FREE &&
2076 phy->allocated_dst == D40_ALLOC_FREE) {
2077 chan_err(d40c, "channel already free\n");
2078 return -EINVAL;
2081 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2082 d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
2083 is_src = false;
2084 else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2085 is_src = true;
2086 else {
2087 chan_err(d40c, "Unknown direction\n");
2088 return -EINVAL;
2091 pm_runtime_get_sync(d40c->base->dev);
2092 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2093 if (res) {
2094 chan_err(d40c, "stop failed\n");
2095 goto out;
2098 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2100 if (chan_is_logical(d40c))
2101 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2102 else
2103 d40c->base->lookup_phy_chans[phy->num] = NULL;
2105 if (d40c->busy) {
2106 pm_runtime_mark_last_busy(d40c->base->dev);
2107 pm_runtime_put_autosuspend(d40c->base->dev);
2110 d40c->busy = false;
2111 d40c->phy_chan = NULL;
2112 d40c->configured = false;
2113 out:
2115 pm_runtime_mark_last_busy(d40c->base->dev);
2116 pm_runtime_put_autosuspend(d40c->base->dev);
2117 return res;
2120 static bool d40_is_paused(struct d40_chan *d40c)
2122 void __iomem *chanbase = chan_base(d40c);
2123 bool is_paused = false;
2124 unsigned long flags;
2125 void __iomem *active_reg;
2126 u32 status;
2127 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2129 spin_lock_irqsave(&d40c->lock, flags);
2131 if (chan_is_physical(d40c)) {
2132 if (d40c->phy_chan->num % 2 == 0)
2133 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2134 else
2135 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2137 status = (readl(active_reg) &
2138 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2139 D40_CHAN_POS(d40c->phy_chan->num);
2140 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2141 is_paused = true;
2143 goto _exit;
2146 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2147 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
2148 status = readl(chanbase + D40_CHAN_REG_SDLNK);
2149 } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
2150 status = readl(chanbase + D40_CHAN_REG_SSLNK);
2151 } else {
2152 chan_err(d40c, "Unknown direction\n");
2153 goto _exit;
2156 status = (status & D40_EVENTLINE_MASK(event)) >>
2157 D40_EVENTLINE_POS(event);
2159 if (status != D40_DMA_RUN)
2160 is_paused = true;
2161 _exit:
2162 spin_unlock_irqrestore(&d40c->lock, flags);
2163 return is_paused;
2167 static u32 stedma40_residue(struct dma_chan *chan)
2169 struct d40_chan *d40c =
2170 container_of(chan, struct d40_chan, chan);
2171 u32 bytes_left;
2172 unsigned long flags;
2174 spin_lock_irqsave(&d40c->lock, flags);
2175 bytes_left = d40_residue(d40c);
2176 spin_unlock_irqrestore(&d40c->lock, flags);
2178 return bytes_left;
2181 static int
2182 d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2183 struct scatterlist *sg_src, struct scatterlist *sg_dst,
2184 unsigned int sg_len, dma_addr_t src_dev_addr,
2185 dma_addr_t dst_dev_addr)
2187 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2188 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2189 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
2190 int ret;
2192 ret = d40_log_sg_to_lli(sg_src, sg_len,
2193 src_dev_addr,
2194 desc->lli_log.src,
2195 chan->log_def.lcsp1,
2196 src_info->data_width,
2197 dst_info->data_width);
2199 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2200 dst_dev_addr,
2201 desc->lli_log.dst,
2202 chan->log_def.lcsp3,
2203 dst_info->data_width,
2204 src_info->data_width);
2206 return ret < 0 ? ret : 0;
2209 static int
2210 d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2211 struct scatterlist *sg_src, struct scatterlist *sg_dst,
2212 unsigned int sg_len, dma_addr_t src_dev_addr,
2213 dma_addr_t dst_dev_addr)
2215 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2216 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2217 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
2218 unsigned long flags = 0;
2219 int ret;
2221 if (desc->cyclic)
2222 flags |= LLI_CYCLIC | LLI_TERM_INT;
2224 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2225 desc->lli_phy.src,
2226 virt_to_phys(desc->lli_phy.src),
2227 chan->src_def_cfg,
2228 src_info, dst_info, flags);
2230 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2231 desc->lli_phy.dst,
2232 virt_to_phys(desc->lli_phy.dst),
2233 chan->dst_def_cfg,
2234 dst_info, src_info, flags);
2236 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2237 desc->lli_pool.size, DMA_TO_DEVICE);
2239 return ret < 0 ? ret : 0;
2242 static struct d40_desc *
2243 d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2244 unsigned int sg_len, unsigned long dma_flags)
2246 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2247 struct d40_desc *desc;
2248 int ret;
2250 desc = d40_desc_get(chan);
2251 if (!desc)
2252 return NULL;
2254 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2255 cfg->dst_info.data_width);
2256 if (desc->lli_len < 0) {
2257 chan_err(chan, "Unaligned size\n");
2258 goto err;
2261 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2262 if (ret < 0) {
2263 chan_err(chan, "Could not allocate lli\n");
2264 goto err;
2267 desc->lli_current = 0;
2268 desc->txd.flags = dma_flags;
2269 desc->txd.tx_submit = d40_tx_submit;
2271 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2273 return desc;
2275 err:
2276 d40_desc_free(chan, desc);
2277 return NULL;
2280 static struct dma_async_tx_descriptor *
2281 d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2282 struct scatterlist *sg_dst, unsigned int sg_len,
2283 enum dma_transfer_direction direction, unsigned long dma_flags)
2285 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2286 dma_addr_t src_dev_addr = 0;
2287 dma_addr_t dst_dev_addr = 0;
2288 struct d40_desc *desc;
2289 unsigned long flags;
2290 int ret;
2292 if (!chan->phy_chan) {
2293 chan_err(chan, "Cannot prepare unallocated channel\n");
2294 return NULL;
2297 spin_lock_irqsave(&chan->lock, flags);
2299 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2300 if (desc == NULL)
2301 goto err;
2303 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2304 desc->cyclic = true;
2306 if (direction == DMA_DEV_TO_MEM)
2307 src_dev_addr = chan->runtime_addr;
2308 else if (direction == DMA_MEM_TO_DEV)
2309 dst_dev_addr = chan->runtime_addr;
2311 if (chan_is_logical(chan))
2312 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2313 sg_len, src_dev_addr, dst_dev_addr);
2314 else
2315 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2316 sg_len, src_dev_addr, dst_dev_addr);
2318 if (ret) {
2319 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2320 chan_is_logical(chan) ? "log" : "phy", ret);
2321 goto err;
2325 * add descriptor to the prepare queue in order to be able
2326 * to free them later in terminate_all
2328 list_add_tail(&desc->node, &chan->prepare_queue);
2330 spin_unlock_irqrestore(&chan->lock, flags);
2332 return &desc->txd;
2334 err:
2335 if (desc)
2336 d40_desc_free(chan, desc);
2337 spin_unlock_irqrestore(&chan->lock, flags);
2338 return NULL;
2341 bool stedma40_filter(struct dma_chan *chan, void *data)
2343 struct stedma40_chan_cfg *info = data;
2344 struct d40_chan *d40c =
2345 container_of(chan, struct d40_chan, chan);
2346 int err;
2348 if (data) {
2349 err = d40_validate_conf(d40c, info);
2350 if (!err)
2351 d40c->dma_cfg = *info;
2352 } else
2353 err = d40_config_memcpy(d40c);
2355 if (!err)
2356 d40c->configured = true;
2358 return err == 0;
2360 EXPORT_SYMBOL(stedma40_filter);
2362 static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2364 bool realtime = d40c->dma_cfg.realtime;
2365 bool highprio = d40c->dma_cfg.high_priority;
2366 u32 rtreg;
2367 u32 event = D40_TYPE_TO_EVENT(dev_type);
2368 u32 group = D40_TYPE_TO_GROUP(dev_type);
2369 u32 bit = BIT(event);
2370 u32 prioreg;
2371 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2373 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2375 * Due to a hardware bug, in some cases a logical channel triggered by
2376 * a high priority destination event line can generate extra packet
2377 * transactions.
2379 * The workaround is to not set the high priority level for the
2380 * destination event lines that trigger logical channels.
2382 if (!src && chan_is_logical(d40c))
2383 highprio = false;
2385 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2387 /* Destination event lines are stored in the upper halfword */
2388 if (!src)
2389 bit <<= 16;
2391 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2392 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2395 static void d40_set_prio_realtime(struct d40_chan *d40c)
2397 if (d40c->base->rev < 3)
2398 return;
2400 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
2401 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2402 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2404 if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
2405 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2406 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2409 #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2410 #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2411 #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2412 #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2414 static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2415 struct of_dma *ofdma)
2417 struct stedma40_chan_cfg cfg;
2418 dma_cap_mask_t cap;
2419 u32 flags;
2421 memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2423 dma_cap_zero(cap);
2424 dma_cap_set(DMA_SLAVE, cap);
2426 cfg.dev_type = dma_spec->args[0];
2427 flags = dma_spec->args[2];
2429 switch (D40_DT_FLAGS_MODE(flags)) {
2430 case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2431 case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2434 switch (D40_DT_FLAGS_DIR(flags)) {
2435 case 0:
2436 cfg.dir = DMA_MEM_TO_DEV;
2437 cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2438 break;
2439 case 1:
2440 cfg.dir = DMA_DEV_TO_MEM;
2441 cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2442 break;
2445 if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2446 cfg.phy_channel = dma_spec->args[1];
2447 cfg.use_fixed_channel = true;
2450 return dma_request_channel(cap, stedma40_filter, &cfg);
2453 /* DMA ENGINE functions */
2454 static int d40_alloc_chan_resources(struct dma_chan *chan)
2456 int err;
2457 unsigned long flags;
2458 struct d40_chan *d40c =
2459 container_of(chan, struct d40_chan, chan);
2460 bool is_free_phy;
2461 spin_lock_irqsave(&d40c->lock, flags);
2463 dma_cookie_init(chan);
2465 /* If no dma configuration is set use default configuration (memcpy) */
2466 if (!d40c->configured) {
2467 err = d40_config_memcpy(d40c);
2468 if (err) {
2469 chan_err(d40c, "Failed to configure memcpy channel\n");
2470 goto fail;
2474 err = d40_allocate_channel(d40c, &is_free_phy);
2475 if (err) {
2476 chan_err(d40c, "Failed to allocate channel\n");
2477 d40c->configured = false;
2478 goto fail;
2481 pm_runtime_get_sync(d40c->base->dev);
2483 d40_set_prio_realtime(d40c);
2485 if (chan_is_logical(d40c)) {
2486 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2487 d40c->lcpa = d40c->base->lcpa_base +
2488 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2489 else
2490 d40c->lcpa = d40c->base->lcpa_base +
2491 d40c->dma_cfg.dev_type *
2492 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2494 /* Unmask the Global Interrupt Mask. */
2495 d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2496 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2499 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2500 chan_is_logical(d40c) ? "logical" : "physical",
2501 d40c->phy_chan->num,
2502 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2506 * Only write channel configuration to the DMA if the physical
2507 * resource is free. In case of multiple logical channels
2508 * on the same physical resource, only the first write is necessary.
2510 if (is_free_phy)
2511 d40_config_write(d40c);
2512 fail:
2513 pm_runtime_mark_last_busy(d40c->base->dev);
2514 pm_runtime_put_autosuspend(d40c->base->dev);
2515 spin_unlock_irqrestore(&d40c->lock, flags);
2516 return err;
2519 static void d40_free_chan_resources(struct dma_chan *chan)
2521 struct d40_chan *d40c =
2522 container_of(chan, struct d40_chan, chan);
2523 int err;
2524 unsigned long flags;
2526 if (d40c->phy_chan == NULL) {
2527 chan_err(d40c, "Cannot free unallocated channel\n");
2528 return;
2531 spin_lock_irqsave(&d40c->lock, flags);
2533 err = d40_free_dma(d40c);
2535 if (err)
2536 chan_err(d40c, "Failed to free channel\n");
2537 spin_unlock_irqrestore(&d40c->lock, flags);
2540 static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2541 dma_addr_t dst,
2542 dma_addr_t src,
2543 size_t size,
2544 unsigned long dma_flags)
2546 struct scatterlist dst_sg;
2547 struct scatterlist src_sg;
2549 sg_init_table(&dst_sg, 1);
2550 sg_init_table(&src_sg, 1);
2552 sg_dma_address(&dst_sg) = dst;
2553 sg_dma_address(&src_sg) = src;
2555 sg_dma_len(&dst_sg) = size;
2556 sg_dma_len(&src_sg) = size;
2558 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
2561 static struct dma_async_tx_descriptor *
2562 d40_prep_memcpy_sg(struct dma_chan *chan,
2563 struct scatterlist *dst_sg, unsigned int dst_nents,
2564 struct scatterlist *src_sg, unsigned int src_nents,
2565 unsigned long dma_flags)
2567 if (dst_nents != src_nents)
2568 return NULL;
2570 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
2573 static struct dma_async_tx_descriptor *
2574 d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2575 unsigned int sg_len, enum dma_transfer_direction direction,
2576 unsigned long dma_flags, void *context)
2578 if (!is_slave_direction(direction))
2579 return NULL;
2581 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
2584 static struct dma_async_tx_descriptor *
2585 dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2586 size_t buf_len, size_t period_len,
2587 enum dma_transfer_direction direction, unsigned long flags,
2588 void *context)
2590 unsigned int periods = buf_len / period_len;
2591 struct dma_async_tx_descriptor *txd;
2592 struct scatterlist *sg;
2593 int i;
2595 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
2596 if (!sg)
2597 return NULL;
2599 for (i = 0; i < periods; i++) {
2600 sg_dma_address(&sg[i]) = dma_addr;
2601 sg_dma_len(&sg[i]) = period_len;
2602 dma_addr += period_len;
2605 sg[periods].offset = 0;
2606 sg_dma_len(&sg[periods]) = 0;
2607 sg[periods].page_link =
2608 ((unsigned long)sg | 0x01) & ~0x02;
2610 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2611 DMA_PREP_INTERRUPT);
2613 kfree(sg);
2615 return txd;
2618 static enum dma_status d40_tx_status(struct dma_chan *chan,
2619 dma_cookie_t cookie,
2620 struct dma_tx_state *txstate)
2622 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2623 enum dma_status ret;
2625 if (d40c->phy_chan == NULL) {
2626 chan_err(d40c, "Cannot read status of unallocated channel\n");
2627 return -EINVAL;
2630 ret = dma_cookie_status(chan, cookie, txstate);
2631 if (ret != DMA_SUCCESS)
2632 dma_set_residue(txstate, stedma40_residue(chan));
2634 if (d40_is_paused(d40c))
2635 ret = DMA_PAUSED;
2637 return ret;
2640 static void d40_issue_pending(struct dma_chan *chan)
2642 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2643 unsigned long flags;
2645 if (d40c->phy_chan == NULL) {
2646 chan_err(d40c, "Channel is not allocated!\n");
2647 return;
2650 spin_lock_irqsave(&d40c->lock, flags);
2652 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2654 /* Busy means that queued jobs are already being processed */
2655 if (!d40c->busy)
2656 (void) d40_queue_start(d40c);
2658 spin_unlock_irqrestore(&d40c->lock, flags);
2661 static void d40_terminate_all(struct dma_chan *chan)
2663 unsigned long flags;
2664 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2665 int ret;
2667 spin_lock_irqsave(&d40c->lock, flags);
2669 pm_runtime_get_sync(d40c->base->dev);
2670 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2671 if (ret)
2672 chan_err(d40c, "Failed to stop channel\n");
2674 d40_term_all(d40c);
2675 pm_runtime_mark_last_busy(d40c->base->dev);
2676 pm_runtime_put_autosuspend(d40c->base->dev);
2677 if (d40c->busy) {
2678 pm_runtime_mark_last_busy(d40c->base->dev);
2679 pm_runtime_put_autosuspend(d40c->base->dev);
2681 d40c->busy = false;
2683 spin_unlock_irqrestore(&d40c->lock, flags);
2686 static int
2687 dma40_config_to_halfchannel(struct d40_chan *d40c,
2688 struct stedma40_half_channel_info *info,
2689 u32 maxburst)
2691 int psize;
2693 if (chan_is_logical(d40c)) {
2694 if (maxburst >= 16)
2695 psize = STEDMA40_PSIZE_LOG_16;
2696 else if (maxburst >= 8)
2697 psize = STEDMA40_PSIZE_LOG_8;
2698 else if (maxburst >= 4)
2699 psize = STEDMA40_PSIZE_LOG_4;
2700 else
2701 psize = STEDMA40_PSIZE_LOG_1;
2702 } else {
2703 if (maxburst >= 16)
2704 psize = STEDMA40_PSIZE_PHY_16;
2705 else if (maxburst >= 8)
2706 psize = STEDMA40_PSIZE_PHY_8;
2707 else if (maxburst >= 4)
2708 psize = STEDMA40_PSIZE_PHY_4;
2709 else
2710 psize = STEDMA40_PSIZE_PHY_1;
2713 info->psize = psize;
2714 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2716 return 0;
2719 /* Runtime reconfiguration extension */
2720 static int d40_set_runtime_config(struct dma_chan *chan,
2721 struct dma_slave_config *config)
2723 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2724 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2725 enum dma_slave_buswidth src_addr_width, dst_addr_width;
2726 dma_addr_t config_addr;
2727 u32 src_maxburst, dst_maxburst;
2728 int ret;
2730 src_addr_width = config->src_addr_width;
2731 src_maxburst = config->src_maxburst;
2732 dst_addr_width = config->dst_addr_width;
2733 dst_maxburst = config->dst_maxburst;
2735 if (config->direction == DMA_DEV_TO_MEM) {
2736 config_addr = config->src_addr;
2738 if (cfg->dir != DMA_DEV_TO_MEM)
2739 dev_dbg(d40c->base->dev,
2740 "channel was not configured for peripheral "
2741 "to memory transfer (%d) overriding\n",
2742 cfg->dir);
2743 cfg->dir = DMA_DEV_TO_MEM;
2745 /* Configure the memory side */
2746 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2747 dst_addr_width = src_addr_width;
2748 if (dst_maxburst == 0)
2749 dst_maxburst = src_maxburst;
2751 } else if (config->direction == DMA_MEM_TO_DEV) {
2752 config_addr = config->dst_addr;
2754 if (cfg->dir != DMA_MEM_TO_DEV)
2755 dev_dbg(d40c->base->dev,
2756 "channel was not configured for memory "
2757 "to peripheral transfer (%d) overriding\n",
2758 cfg->dir);
2759 cfg->dir = DMA_MEM_TO_DEV;
2761 /* Configure the memory side */
2762 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2763 src_addr_width = dst_addr_width;
2764 if (src_maxburst == 0)
2765 src_maxburst = dst_maxburst;
2766 } else {
2767 dev_err(d40c->base->dev,
2768 "unrecognized channel direction %d\n",
2769 config->direction);
2770 return -EINVAL;
2773 if (config_addr <= 0) {
2774 dev_err(d40c->base->dev, "no address supplied\n");
2775 return -EINVAL;
2778 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
2779 dev_err(d40c->base->dev,
2780 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2781 src_maxburst,
2782 src_addr_width,
2783 dst_maxburst,
2784 dst_addr_width);
2785 return -EINVAL;
2788 if (src_maxburst > 16) {
2789 src_maxburst = 16;
2790 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2791 } else if (dst_maxburst > 16) {
2792 dst_maxburst = 16;
2793 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2796 /* Only valid widths are; 1, 2, 4 and 8. */
2797 if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2798 src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2799 dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2800 dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2801 ((src_addr_width > 1) && (src_addr_width & 1)) ||
2802 ((dst_addr_width > 1) && (dst_addr_width & 1)))
2803 return -EINVAL;
2805 cfg->src_info.data_width = src_addr_width;
2806 cfg->dst_info.data_width = dst_addr_width;
2808 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2809 src_maxburst);
2810 if (ret)
2811 return ret;
2813 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2814 dst_maxburst);
2815 if (ret)
2816 return ret;
2818 /* Fill in register values */
2819 if (chan_is_logical(d40c))
2820 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2821 else
2822 d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2824 /* These settings will take precedence later */
2825 d40c->runtime_addr = config_addr;
2826 d40c->runtime_direction = config->direction;
2827 dev_dbg(d40c->base->dev,
2828 "configured channel %s for %s, data width %d/%d, "
2829 "maxburst %d/%d elements, LE, no flow control\n",
2830 dma_chan_name(chan),
2831 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
2832 src_addr_width, dst_addr_width,
2833 src_maxburst, dst_maxburst);
2835 return 0;
2838 static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2839 unsigned long arg)
2841 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2843 if (d40c->phy_chan == NULL) {
2844 chan_err(d40c, "Channel is not allocated!\n");
2845 return -EINVAL;
2848 switch (cmd) {
2849 case DMA_TERMINATE_ALL:
2850 d40_terminate_all(chan);
2851 return 0;
2852 case DMA_PAUSE:
2853 return d40_pause(d40c);
2854 case DMA_RESUME:
2855 return d40_resume(d40c);
2856 case DMA_SLAVE_CONFIG:
2857 return d40_set_runtime_config(chan,
2858 (struct dma_slave_config *) arg);
2859 default:
2860 break;
2863 /* Other commands are unimplemented */
2864 return -ENXIO;
2867 /* Initialization functions */
2869 static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2870 struct d40_chan *chans, int offset,
2871 int num_chans)
2873 int i = 0;
2874 struct d40_chan *d40c;
2876 INIT_LIST_HEAD(&dma->channels);
2878 for (i = offset; i < offset + num_chans; i++) {
2879 d40c = &chans[i];
2880 d40c->base = base;
2881 d40c->chan.device = dma;
2883 spin_lock_init(&d40c->lock);
2885 d40c->log_num = D40_PHY_CHAN;
2887 INIT_LIST_HEAD(&d40c->done);
2888 INIT_LIST_HEAD(&d40c->active);
2889 INIT_LIST_HEAD(&d40c->queue);
2890 INIT_LIST_HEAD(&d40c->pending_queue);
2891 INIT_LIST_HEAD(&d40c->client);
2892 INIT_LIST_HEAD(&d40c->prepare_queue);
2894 tasklet_init(&d40c->tasklet, dma_tasklet,
2895 (unsigned long) d40c);
2897 list_add_tail(&d40c->chan.device_node,
2898 &dma->channels);
2902 static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2904 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2905 dev->device_prep_slave_sg = d40_prep_slave_sg;
2907 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2908 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2911 * This controller can only access address at even
2912 * 32bit boundaries, i.e. 2^2
2914 dev->copy_align = 2;
2917 if (dma_has_cap(DMA_SG, dev->cap_mask))
2918 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2920 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2921 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2923 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2924 dev->device_free_chan_resources = d40_free_chan_resources;
2925 dev->device_issue_pending = d40_issue_pending;
2926 dev->device_tx_status = d40_tx_status;
2927 dev->device_control = d40_control;
2928 dev->dev = base->dev;
2931 static int __init d40_dmaengine_init(struct d40_base *base,
2932 int num_reserved_chans)
2934 int err ;
2936 d40_chan_init(base, &base->dma_slave, base->log_chans,
2937 0, base->num_log_chans);
2939 dma_cap_zero(base->dma_slave.cap_mask);
2940 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2941 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2943 d40_ops_init(base, &base->dma_slave);
2945 err = dma_async_device_register(&base->dma_slave);
2947 if (err) {
2948 d40_err(base->dev, "Failed to register slave channels\n");
2949 goto failure1;
2952 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2953 base->num_log_chans, base->num_memcpy_chans);
2955 dma_cap_zero(base->dma_memcpy.cap_mask);
2956 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
2957 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
2959 d40_ops_init(base, &base->dma_memcpy);
2961 err = dma_async_device_register(&base->dma_memcpy);
2963 if (err) {
2964 d40_err(base->dev,
2965 "Failed to regsiter memcpy only channels\n");
2966 goto failure2;
2969 d40_chan_init(base, &base->dma_both, base->phy_chans,
2970 0, num_reserved_chans);
2972 dma_cap_zero(base->dma_both.cap_mask);
2973 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2974 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
2975 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
2976 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2978 d40_ops_init(base, &base->dma_both);
2979 err = dma_async_device_register(&base->dma_both);
2981 if (err) {
2982 d40_err(base->dev,
2983 "Failed to register logical and physical capable channels\n");
2984 goto failure3;
2986 return 0;
2987 failure3:
2988 dma_async_device_unregister(&base->dma_memcpy);
2989 failure2:
2990 dma_async_device_unregister(&base->dma_slave);
2991 failure1:
2992 return err;
2995 /* Suspend resume functionality */
2996 #ifdef CONFIG_PM
2997 static int dma40_pm_suspend(struct device *dev)
2999 struct platform_device *pdev = to_platform_device(dev);
3000 struct d40_base *base = platform_get_drvdata(pdev);
3001 int ret = 0;
3003 if (base->lcpa_regulator)
3004 ret = regulator_disable(base->lcpa_regulator);
3005 return ret;
3008 static int dma40_runtime_suspend(struct device *dev)
3010 struct platform_device *pdev = to_platform_device(dev);
3011 struct d40_base *base = platform_get_drvdata(pdev);
3013 d40_save_restore_registers(base, true);
3015 /* Don't disable/enable clocks for v1 due to HW bugs */
3016 if (base->rev != 1)
3017 writel_relaxed(base->gcc_pwr_off_mask,
3018 base->virtbase + D40_DREG_GCC);
3020 return 0;
3023 static int dma40_runtime_resume(struct device *dev)
3025 struct platform_device *pdev = to_platform_device(dev);
3026 struct d40_base *base = platform_get_drvdata(pdev);
3028 if (base->initialized)
3029 d40_save_restore_registers(base, false);
3031 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3032 base->virtbase + D40_DREG_GCC);
3033 return 0;
3036 static int dma40_resume(struct device *dev)
3038 struct platform_device *pdev = to_platform_device(dev);
3039 struct d40_base *base = platform_get_drvdata(pdev);
3040 int ret = 0;
3042 if (base->lcpa_regulator)
3043 ret = regulator_enable(base->lcpa_regulator);
3045 return ret;
3048 static const struct dev_pm_ops dma40_pm_ops = {
3049 .suspend = dma40_pm_suspend,
3050 .runtime_suspend = dma40_runtime_suspend,
3051 .runtime_resume = dma40_runtime_resume,
3052 .resume = dma40_resume,
3054 #define DMA40_PM_OPS (&dma40_pm_ops)
3055 #else
3056 #define DMA40_PM_OPS NULL
3057 #endif
3059 /* Initialization functions. */
3061 static int __init d40_phy_res_init(struct d40_base *base)
3063 int i;
3064 int num_phy_chans_avail = 0;
3065 u32 val[2];
3066 int odd_even_bit = -2;
3067 int gcc = D40_DREG_GCC_ENA;
3069 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3070 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3072 for (i = 0; i < base->num_phy_chans; i++) {
3073 base->phy_res[i].num = i;
3074 odd_even_bit += 2 * ((i % 2) == 0);
3075 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3076 /* Mark security only channels as occupied */
3077 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3078 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
3079 base->phy_res[i].reserved = true;
3080 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3081 D40_DREG_GCC_SRC);
3082 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3083 D40_DREG_GCC_DST);
3086 } else {
3087 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3088 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
3089 base->phy_res[i].reserved = false;
3090 num_phy_chans_avail++;
3092 spin_lock_init(&base->phy_res[i].lock);
3095 /* Mark disabled channels as occupied */
3096 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3097 int chan = base->plat_data->disabled_channels[i];
3099 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3100 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
3101 base->phy_res[chan].reserved = true;
3102 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3103 D40_DREG_GCC_SRC);
3104 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3105 D40_DREG_GCC_DST);
3106 num_phy_chans_avail--;
3109 /* Mark soft_lli channels */
3110 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3111 int chan = base->plat_data->soft_lli_chans[i];
3113 base->phy_res[chan].use_soft_lli = true;
3116 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3117 num_phy_chans_avail, base->num_phy_chans);
3119 /* Verify settings extended vs standard */
3120 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3122 for (i = 0; i < base->num_phy_chans; i++) {
3124 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3125 (val[0] & 0x3) != 1)
3126 dev_info(base->dev,
3127 "[%s] INFO: channel %d is misconfigured (%d)\n",
3128 __func__, i, val[0] & 0x3);
3130 val[0] = val[0] >> 2;
3134 * To keep things simple, Enable all clocks initially.
3135 * The clocks will get managed later post channel allocation.
3136 * The clocks for the event lines on which reserved channels exists
3137 * are not managed here.
3139 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3140 base->gcc_pwr_off_mask = gcc;
3142 return num_phy_chans_avail;
3145 static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3147 struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
3148 struct clk *clk = NULL;
3149 void __iomem *virtbase = NULL;
3150 struct resource *res = NULL;
3151 struct d40_base *base = NULL;
3152 int num_log_chans = 0;
3153 int num_phy_chans;
3154 int num_memcpy_chans;
3155 int clk_ret = -EINVAL;
3156 int i;
3157 u32 pid;
3158 u32 cid;
3159 u8 rev;
3161 clk = clk_get(&pdev->dev, NULL);
3162 if (IS_ERR(clk)) {
3163 d40_err(&pdev->dev, "No matching clock found\n");
3164 goto failure;
3167 clk_ret = clk_prepare_enable(clk);
3168 if (clk_ret) {
3169 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3170 goto failure;
3173 /* Get IO for DMAC base address */
3174 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3175 if (!res)
3176 goto failure;
3178 if (request_mem_region(res->start, resource_size(res),
3179 D40_NAME " I/O base") == NULL)
3180 goto failure;
3182 virtbase = ioremap(res->start, resource_size(res));
3183 if (!virtbase)
3184 goto failure;
3186 /* This is just a regular AMBA PrimeCell ID actually */
3187 for (pid = 0, i = 0; i < 4; i++)
3188 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3189 & 255) << (i * 8);
3190 for (cid = 0, i = 0; i < 4; i++)
3191 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3192 & 255) << (i * 8);
3194 if (cid != AMBA_CID) {
3195 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
3196 goto failure;
3198 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3199 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3200 AMBA_MANF_BITS(pid),
3201 AMBA_VENDOR_ST);
3202 goto failure;
3205 * HW revision:
3206 * DB8500ed has revision 0
3207 * ? has revision 1
3208 * DB8500v1 has revision 2
3209 * DB8500v2 has revision 3
3210 * AP9540v1 has revision 4
3211 * DB8540v1 has revision 4
3213 rev = AMBA_REV_BITS(pid);
3214 if (rev < 2) {
3215 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3216 goto failure;
3219 /* The number of physical channels on this HW */
3220 if (plat_data->num_of_phy_chans)
3221 num_phy_chans = plat_data->num_of_phy_chans;
3222 else
3223 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3225 /* The number of channels used for memcpy */
3226 if (plat_data->num_of_memcpy_chans)
3227 num_memcpy_chans = plat_data->num_of_memcpy_chans;
3228 else
3229 num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
3231 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3233 dev_info(&pdev->dev,
3234 "hardware rev: %d @ %pa with %d physical and %d logical channels\n",
3235 rev, &res->start, num_phy_chans, num_log_chans);
3237 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
3238 (num_phy_chans + num_log_chans + num_memcpy_chans) *
3239 sizeof(struct d40_chan), GFP_KERNEL);
3241 if (base == NULL) {
3242 d40_err(&pdev->dev, "Out of memory\n");
3243 goto failure;
3246 base->rev = rev;
3247 base->clk = clk;
3248 base->num_memcpy_chans = num_memcpy_chans;
3249 base->num_phy_chans = num_phy_chans;
3250 base->num_log_chans = num_log_chans;
3251 base->phy_start = res->start;
3252 base->phy_size = resource_size(res);
3253 base->virtbase = virtbase;
3254 base->plat_data = plat_data;
3255 base->dev = &pdev->dev;
3256 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3257 base->log_chans = &base->phy_chans[num_phy_chans];
3259 if (base->plat_data->num_of_phy_chans == 14) {
3260 base->gen_dmac.backup = d40_backup_regs_v4b;
3261 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3262 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3263 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3264 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3265 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3266 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3267 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3268 base->gen_dmac.il = il_v4b;
3269 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3270 base->gen_dmac.init_reg = dma_init_reg_v4b;
3271 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3272 } else {
3273 if (base->rev >= 3) {
3274 base->gen_dmac.backup = d40_backup_regs_v4a;
3275 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3277 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3278 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3279 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3280 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3281 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3282 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3283 base->gen_dmac.il = il_v4a;
3284 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3285 base->gen_dmac.init_reg = dma_init_reg_v4a;
3286 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3289 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3290 GFP_KERNEL);
3291 if (!base->phy_res)
3292 goto failure;
3294 base->lookup_phy_chans = kzalloc(num_phy_chans *
3295 sizeof(struct d40_chan *),
3296 GFP_KERNEL);
3297 if (!base->lookup_phy_chans)
3298 goto failure;
3300 base->lookup_log_chans = kzalloc(num_log_chans *
3301 sizeof(struct d40_chan *),
3302 GFP_KERNEL);
3303 if (!base->lookup_log_chans)
3304 goto failure;
3306 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3307 sizeof(d40_backup_regs_chan),
3308 GFP_KERNEL);
3309 if (!base->reg_val_backup_chan)
3310 goto failure;
3312 base->lcla_pool.alloc_map =
3313 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3314 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
3315 if (!base->lcla_pool.alloc_map)
3316 goto failure;
3318 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3319 0, SLAB_HWCACHE_ALIGN,
3320 NULL);
3321 if (base->desc_slab == NULL)
3322 goto failure;
3324 return base;
3326 failure:
3327 if (!clk_ret)
3328 clk_disable_unprepare(clk);
3329 if (!IS_ERR(clk))
3330 clk_put(clk);
3331 if (virtbase)
3332 iounmap(virtbase);
3333 if (res)
3334 release_mem_region(res->start,
3335 resource_size(res));
3336 if (virtbase)
3337 iounmap(virtbase);
3339 if (base) {
3340 kfree(base->lcla_pool.alloc_map);
3341 kfree(base->reg_val_backup_chan);
3342 kfree(base->lookup_log_chans);
3343 kfree(base->lookup_phy_chans);
3344 kfree(base->phy_res);
3345 kfree(base);
3348 return NULL;
3351 static void __init d40_hw_init(struct d40_base *base)
3354 int i;
3355 u32 prmseo[2] = {0, 0};
3356 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3357 u32 pcmis = 0;
3358 u32 pcicr = 0;
3359 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3360 u32 reg_size = base->gen_dmac.init_reg_size;
3362 for (i = 0; i < reg_size; i++)
3363 writel(dma_init_reg[i].val,
3364 base->virtbase + dma_init_reg[i].reg);
3366 /* Configure all our dma channels to default settings */
3367 for (i = 0; i < base->num_phy_chans; i++) {
3369 activeo[i % 2] = activeo[i % 2] << 2;
3371 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3372 == D40_ALLOC_PHY) {
3373 activeo[i % 2] |= 3;
3374 continue;
3377 /* Enable interrupt # */
3378 pcmis = (pcmis << 1) | 1;
3380 /* Clear interrupt # */
3381 pcicr = (pcicr << 1) | 1;
3383 /* Set channel to physical mode */
3384 prmseo[i % 2] = prmseo[i % 2] << 2;
3385 prmseo[i % 2] |= 1;
3389 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3390 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3391 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3392 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3394 /* Write which interrupt to enable */
3395 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
3397 /* Write which interrupt to clear */
3398 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
3400 /* These are __initdata and cannot be accessed after init */
3401 base->gen_dmac.init_reg = NULL;
3402 base->gen_dmac.init_reg_size = 0;
3405 static int __init d40_lcla_allocate(struct d40_base *base)
3407 struct d40_lcla_pool *pool = &base->lcla_pool;
3408 unsigned long *page_list;
3409 int i, j;
3410 int ret = 0;
3413 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3414 * To full fill this hardware requirement without wasting 256 kb
3415 * we allocate pages until we get an aligned one.
3417 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3418 GFP_KERNEL);
3420 if (!page_list) {
3421 ret = -ENOMEM;
3422 goto failure;
3425 /* Calculating how many pages that are required */
3426 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3428 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3429 page_list[i] = __get_free_pages(GFP_KERNEL,
3430 base->lcla_pool.pages);
3431 if (!page_list[i]) {
3433 d40_err(base->dev, "Failed to allocate %d pages.\n",
3434 base->lcla_pool.pages);
3436 for (j = 0; j < i; j++)
3437 free_pages(page_list[j], base->lcla_pool.pages);
3438 goto failure;
3441 if ((virt_to_phys((void *)page_list[i]) &
3442 (LCLA_ALIGNMENT - 1)) == 0)
3443 break;
3446 for (j = 0; j < i; j++)
3447 free_pages(page_list[j], base->lcla_pool.pages);
3449 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3450 base->lcla_pool.base = (void *)page_list[i];
3451 } else {
3453 * After many attempts and no succees with finding the correct
3454 * alignment, try with allocating a big buffer.
3456 dev_warn(base->dev,
3457 "[%s] Failed to get %d pages @ 18 bit align.\n",
3458 __func__, base->lcla_pool.pages);
3459 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3460 base->num_phy_chans +
3461 LCLA_ALIGNMENT,
3462 GFP_KERNEL);
3463 if (!base->lcla_pool.base_unaligned) {
3464 ret = -ENOMEM;
3465 goto failure;
3468 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3469 LCLA_ALIGNMENT);
3472 pool->dma_addr = dma_map_single(base->dev, pool->base,
3473 SZ_1K * base->num_phy_chans,
3474 DMA_TO_DEVICE);
3475 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3476 pool->dma_addr = 0;
3477 ret = -ENOMEM;
3478 goto failure;
3481 writel(virt_to_phys(base->lcla_pool.base),
3482 base->virtbase + D40_DREG_LCLA);
3483 failure:
3484 kfree(page_list);
3485 return ret;
3488 static int __init d40_of_probe(struct platform_device *pdev,
3489 struct device_node *np)
3491 struct stedma40_platform_data *pdata;
3492 int num_phy = 0, num_memcpy = 0, num_disabled = 0;
3493 const __be32 *list;
3495 pdata = devm_kzalloc(&pdev->dev,
3496 sizeof(struct stedma40_platform_data),
3497 GFP_KERNEL);
3498 if (!pdata)
3499 return -ENOMEM;
3501 /* If absent this value will be obtained from h/w. */
3502 of_property_read_u32(np, "dma-channels", &num_phy);
3503 if (num_phy > 0)
3504 pdata->num_of_phy_chans = num_phy;
3506 list = of_get_property(np, "memcpy-channels", &num_memcpy);
3507 num_memcpy /= sizeof(*list);
3509 if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
3510 d40_err(&pdev->dev,
3511 "Invalid number of memcpy channels specified (%d)\n",
3512 num_memcpy);
3513 return -EINVAL;
3515 pdata->num_of_memcpy_chans = num_memcpy;
3517 of_property_read_u32_array(np, "memcpy-channels",
3518 dma40_memcpy_channels,
3519 num_memcpy);
3521 list = of_get_property(np, "disabled-channels", &num_disabled);
3522 num_disabled /= sizeof(*list);
3524 if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
3525 d40_err(&pdev->dev,
3526 "Invalid number of disabled channels specified (%d)\n",
3527 num_disabled);
3528 return -EINVAL;
3531 of_property_read_u32_array(np, "disabled-channels",
3532 pdata->disabled_channels,
3533 num_disabled);
3534 pdata->disabled_channels[num_disabled] = -1;
3536 pdev->dev.platform_data = pdata;
3538 return 0;
3541 static int __init d40_probe(struct platform_device *pdev)
3543 struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
3544 struct device_node *np = pdev->dev.of_node;
3545 int ret = -ENOENT;
3546 struct d40_base *base = NULL;
3547 struct resource *res = NULL;
3548 int num_reserved_chans;
3549 u32 val;
3551 if (!plat_data) {
3552 if (np) {
3553 if(d40_of_probe(pdev, np)) {
3554 ret = -ENOMEM;
3555 goto failure;
3557 } else {
3558 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3559 goto failure;
3563 base = d40_hw_detect_init(pdev);
3564 if (!base)
3565 goto failure;
3567 num_reserved_chans = d40_phy_res_init(base);
3569 platform_set_drvdata(pdev, base);
3571 spin_lock_init(&base->interrupt_lock);
3572 spin_lock_init(&base->execmd_lock);
3574 /* Get IO for logical channel parameter address */
3575 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3576 if (!res) {
3577 ret = -ENOENT;
3578 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
3579 goto failure;
3581 base->lcpa_size = resource_size(res);
3582 base->phy_lcpa = res->start;
3584 if (request_mem_region(res->start, resource_size(res),
3585 D40_NAME " I/O lcpa") == NULL) {
3586 ret = -EBUSY;
3587 d40_err(&pdev->dev, "Failed to request LCPA region %pR\n", res);
3588 goto failure;
3591 /* We make use of ESRAM memory for this. */
3592 val = readl(base->virtbase + D40_DREG_LCPA);
3593 if (res->start != val && val != 0) {
3594 dev_warn(&pdev->dev,
3595 "[%s] Mismatch LCPA dma 0x%x, def %pa\n",
3596 __func__, val, &res->start);
3597 } else
3598 writel(res->start, base->virtbase + D40_DREG_LCPA);
3600 base->lcpa_base = ioremap(res->start, resource_size(res));
3601 if (!base->lcpa_base) {
3602 ret = -ENOMEM;
3603 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
3604 goto failure;
3606 /* If lcla has to be located in ESRAM we don't need to allocate */
3607 if (base->plat_data->use_esram_lcla) {
3608 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3609 "lcla_esram");
3610 if (!res) {
3611 ret = -ENOENT;
3612 d40_err(&pdev->dev,
3613 "No \"lcla_esram\" memory resource\n");
3614 goto failure;
3616 base->lcla_pool.base = ioremap(res->start,
3617 resource_size(res));
3618 if (!base->lcla_pool.base) {
3619 ret = -ENOMEM;
3620 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3621 goto failure;
3623 writel(res->start, base->virtbase + D40_DREG_LCLA);
3625 } else {
3626 ret = d40_lcla_allocate(base);
3627 if (ret) {
3628 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3629 goto failure;
3633 spin_lock_init(&base->lcla_pool.lock);
3635 base->irq = platform_get_irq(pdev, 0);
3637 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
3638 if (ret) {
3639 d40_err(&pdev->dev, "No IRQ defined\n");
3640 goto failure;
3643 pm_runtime_irq_safe(base->dev);
3644 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3645 pm_runtime_use_autosuspend(base->dev);
3646 pm_runtime_enable(base->dev);
3647 pm_runtime_resume(base->dev);
3649 if (base->plat_data->use_esram_lcla) {
3651 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3652 if (IS_ERR(base->lcpa_regulator)) {
3653 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3654 ret = PTR_ERR(base->lcpa_regulator);
3655 base->lcpa_regulator = NULL;
3656 goto failure;
3659 ret = regulator_enable(base->lcpa_regulator);
3660 if (ret) {
3661 d40_err(&pdev->dev,
3662 "Failed to enable lcpa_regulator\n");
3663 regulator_put(base->lcpa_regulator);
3664 base->lcpa_regulator = NULL;
3665 goto failure;
3669 base->initialized = true;
3670 ret = d40_dmaengine_init(base, num_reserved_chans);
3671 if (ret)
3672 goto failure;
3674 base->dev->dma_parms = &base->dma_parms;
3675 ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3676 if (ret) {
3677 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3678 goto failure;
3681 d40_hw_init(base);
3683 if (np) {
3684 ret = of_dma_controller_register(np, d40_xlate, NULL);
3685 if (ret)
3686 dev_err(&pdev->dev,
3687 "could not register of_dma_controller\n");
3690 dev_info(base->dev, "initialized\n");
3691 return 0;
3693 failure:
3694 if (base) {
3695 if (base->desc_slab)
3696 kmem_cache_destroy(base->desc_slab);
3697 if (base->virtbase)
3698 iounmap(base->virtbase);
3700 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3701 iounmap(base->lcla_pool.base);
3702 base->lcla_pool.base = NULL;
3705 if (base->lcla_pool.dma_addr)
3706 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3707 SZ_1K * base->num_phy_chans,
3708 DMA_TO_DEVICE);
3710 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3711 free_pages((unsigned long)base->lcla_pool.base,
3712 base->lcla_pool.pages);
3714 kfree(base->lcla_pool.base_unaligned);
3716 if (base->phy_lcpa)
3717 release_mem_region(base->phy_lcpa,
3718 base->lcpa_size);
3719 if (base->phy_start)
3720 release_mem_region(base->phy_start,
3721 base->phy_size);
3722 if (base->clk) {
3723 clk_disable_unprepare(base->clk);
3724 clk_put(base->clk);
3727 if (base->lcpa_regulator) {
3728 regulator_disable(base->lcpa_regulator);
3729 regulator_put(base->lcpa_regulator);
3732 kfree(base->lcla_pool.alloc_map);
3733 kfree(base->lookup_log_chans);
3734 kfree(base->lookup_phy_chans);
3735 kfree(base->phy_res);
3736 kfree(base);
3739 d40_err(&pdev->dev, "probe failed\n");
3740 return ret;
3743 static const struct of_device_id d40_match[] = {
3744 { .compatible = "stericsson,dma40", },
3748 static struct platform_driver d40_driver = {
3749 .driver = {
3750 .owner = THIS_MODULE,
3751 .name = D40_NAME,
3752 .pm = DMA40_PM_OPS,
3753 .of_match_table = d40_match,
3757 static int __init stedma40_init(void)
3759 return platform_driver_probe(&d40_driver, d40_probe);
3761 subsys_initcall(stedma40_init);