]>
Commit | Line | Data |
---|---|---|
8d318a50 | 1 | /* |
d49278e3 PF |
2 | * Copyright (C) Ericsson AB 2007-2008 |
3 | * Copyright (C) ST-Ericsson SA 2008-2010 | |
661385f9 | 4 | * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson |
767a9675 | 5 | * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson |
8d318a50 | 6 | * License terms: GNU General Public License (GPL) version 2 |
8d318a50 LW |
7 | */ |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/dmaengine.h> | |
12 | #include <linux/platform_device.h> | |
13 | #include <linux/clk.h> | |
14 | #include <linux/delay.h> | |
698e4732 | 15 | #include <linux/err.h> |
f4b89764 | 16 | #include <linux/amba/bus.h> |
8d318a50 LW |
17 | |
18 | #include <plat/ste_dma40.h> | |
19 | ||
20 | #include "ste_dma40_ll.h" | |
21 | ||
22 | #define D40_NAME "dma40" | |
23 | ||
24 | #define D40_PHY_CHAN -1 | |
25 | ||
26 | /* For masking out/in 2 bit channel positions */ | |
27 | #define D40_CHAN_POS(chan) (2 * (chan / 2)) | |
28 | #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan)) | |
29 | ||
30 | /* Maximum iterations taken before giving up suspending a channel */ | |
31 | #define D40_SUSPEND_MAX_IT 500 | |
32 | ||
508849ad LW |
33 | /* Hardware requirement on LCLA alignment */ |
34 | #define LCLA_ALIGNMENT 0x40000 | |
698e4732 JA |
35 | |
36 | /* Max number of links per event group */ | |
37 | #define D40_LCLA_LINK_PER_EVENT_GRP 128 | |
38 | #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP | |
39 | ||
508849ad LW |
40 | /* Attempts before giving up to trying to get pages that are aligned */ |
41 | #define MAX_LCLA_ALLOC_ATTEMPTS 256 | |
42 | ||
43 | /* Bit markings for allocation map */ | |
8d318a50 LW |
44 | #define D40_ALLOC_FREE (1 << 31) |
45 | #define D40_ALLOC_PHY (1 << 30) | |
46 | #define D40_ALLOC_LOG_FREE 0 | |
47 | ||
8d318a50 LW |
48 | /** |
49 | * enum 40_command - The different commands and/or statuses. | |
50 | * | |
51 | * @D40_DMA_STOP: DMA channel command STOP or status STOPPED, | |
52 | * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN. | |
53 | * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible. | |
54 | * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED. | |
55 | */ | |
56 | enum d40_command { | |
57 | D40_DMA_STOP = 0, | |
58 | D40_DMA_RUN = 1, | |
59 | D40_DMA_SUSPEND_REQ = 2, | |
60 | D40_DMA_SUSPENDED = 3 | |
61 | }; | |
62 | ||
63 | /** | |
64 | * struct d40_lli_pool - Structure for keeping LLIs in memory | |
65 | * | |
66 | * @base: Pointer to memory area when the pre_alloc_lli's are not large | |
67 | * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if | |
68 | * pre_alloc_lli is used. | |
b00f938c | 69 | * @dma_addr: DMA address, if mapped |
8d318a50 LW |
70 | * @size: The size in bytes of the memory at base or the size of pre_alloc_lli. |
71 | * @pre_alloc_lli: Pre allocated area for the most common case of transfers, | |
72 | * one buffer to one buffer. | |
73 | */ | |
74 | struct d40_lli_pool { | |
75 | void *base; | |
508849ad | 76 | int size; |
b00f938c | 77 | dma_addr_t dma_addr; |
8d318a50 | 78 | /* Space for dst and src, plus an extra for padding */ |
508849ad | 79 | u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)]; |
8d318a50 LW |
80 | }; |
81 | ||
82 | /** | |
83 | * struct d40_desc - A descriptor is one DMA job. | |
84 | * | |
85 | * @lli_phy: LLI settings for physical channel. Both src and dst= | |
86 | * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if | |
87 | * lli_len equals one. | |
88 | * @lli_log: Same as above but for logical channels. | |
89 | * @lli_pool: The pool with two entries pre-allocated. | |
941b77a3 | 90 | * @lli_len: Number of llis of current descriptor. |
25985edc | 91 | * @lli_current: Number of transferred llis. |
698e4732 | 92 | * @lcla_alloc: Number of LCLA entries allocated. |
8d318a50 LW |
93 | * @txd: DMA engine struct. Used for among other things for communication |
94 | * during a transfer. | |
95 | * @node: List entry. | |
8d318a50 | 96 | * @is_in_client_list: true if the client owns this descriptor. |
aa182ae2 | 97 | * the previous one. |
8d318a50 LW |
98 | * |
99 | * This descriptor is used for both logical and physical transfers. | |
100 | */ | |
8d318a50 LW |
101 | struct d40_desc { |
102 | /* LLI physical */ | |
103 | struct d40_phy_lli_bidir lli_phy; | |
104 | /* LLI logical */ | |
105 | struct d40_log_lli_bidir lli_log; | |
106 | ||
107 | struct d40_lli_pool lli_pool; | |
941b77a3 | 108 | int lli_len; |
698e4732 JA |
109 | int lli_current; |
110 | int lcla_alloc; | |
8d318a50 LW |
111 | |
112 | struct dma_async_tx_descriptor txd; | |
113 | struct list_head node; | |
114 | ||
8d318a50 | 115 | bool is_in_client_list; |
0c842b55 | 116 | bool cyclic; |
8d318a50 LW |
117 | }; |
118 | ||
119 | /** | |
120 | * struct d40_lcla_pool - LCLA pool settings and data. | |
121 | * | |
508849ad LW |
122 | * @base: The virtual address of LCLA. 18 bit aligned. |
123 | * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used. | |
124 | * This pointer is only there for clean-up on error. | |
125 | * @pages: The number of pages needed for all physical channels. | |
126 | * Only used later for clean-up on error | |
8d318a50 | 127 | * @lock: Lock to protect the content in this struct. |
698e4732 | 128 | * @alloc_map: big map over which LCLA entry is own by which job. |
8d318a50 LW |
129 | */ |
130 | struct d40_lcla_pool { | |
131 | void *base; | |
026cbc42 | 132 | dma_addr_t dma_addr; |
508849ad LW |
133 | void *base_unaligned; |
134 | int pages; | |
8d318a50 | 135 | spinlock_t lock; |
698e4732 | 136 | struct d40_desc **alloc_map; |
8d318a50 LW |
137 | }; |
138 | ||
139 | /** | |
140 | * struct d40_phy_res - struct for handling eventlines mapped to physical | |
141 | * channels. | |
142 | * | |
143 | * @lock: A lock protection this entity. | |
144 | * @num: The physical channel number of this entity. | |
145 | * @allocated_src: Bit mapped to show which src event line's are mapped to | |
146 | * this physical channel. Can also be free or physically allocated. | |
147 | * @allocated_dst: Same as for src but is dst. | |
148 | * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as | |
767a9675 | 149 | * event line number. |
8d318a50 LW |
150 | */ |
151 | struct d40_phy_res { | |
152 | spinlock_t lock; | |
153 | int num; | |
154 | u32 allocated_src; | |
155 | u32 allocated_dst; | |
156 | }; | |
157 | ||
158 | struct d40_base; | |
159 | ||
160 | /** | |
161 | * struct d40_chan - Struct that describes a channel. | |
162 | * | |
163 | * @lock: A spinlock to protect this struct. | |
164 | * @log_num: The logical number, if any of this channel. | |
165 | * @completed: Starts with 1, after first interrupt it is set to dma engine's | |
166 | * current cookie. | |
167 | * @pending_tx: The number of pending transfers. Used between interrupt handler | |
168 | * and tasklet. | |
169 | * @busy: Set to true when transfer is ongoing on this channel. | |
2a614340 JA |
170 | * @phy_chan: Pointer to physical channel which this instance runs on. If this |
171 | * point is NULL, then the channel is not allocated. | |
8d318a50 LW |
172 | * @chan: DMA engine handle. |
173 | * @tasklet: Tasklet that gets scheduled from interrupt context to complete a | |
174 | * transfer and call client callback. | |
175 | * @client: Cliented owned descriptor list. | |
176 | * @active: Active descriptor. | |
177 | * @queue: Queued jobs. | |
8d318a50 | 178 | * @dma_cfg: The client configuration of this dma channel. |
ce2ca125 | 179 | * @configured: whether the dma_cfg configuration is valid |
8d318a50 LW |
180 | * @base: Pointer to the device instance struct. |
181 | * @src_def_cfg: Default cfg register setting for src. | |
182 | * @dst_def_cfg: Default cfg register setting for dst. | |
183 | * @log_def: Default logical channel settings. | |
184 | * @lcla: Space for one dst src pair for logical channel transfers. | |
185 | * @lcpa: Pointer to dst and src lcpa settings. | |
ae752bf4 | 186 | * @runtime_addr: runtime configured address. |
187 | * @runtime_direction: runtime configured direction. | |
8d318a50 LW |
188 | * |
189 | * This struct can either "be" a logical or a physical channel. | |
190 | */ | |
191 | struct d40_chan { | |
192 | spinlock_t lock; | |
193 | int log_num; | |
194 | /* ID of the most recent completed transfer */ | |
195 | int completed; | |
196 | int pending_tx; | |
197 | bool busy; | |
198 | struct d40_phy_res *phy_chan; | |
199 | struct dma_chan chan; | |
200 | struct tasklet_struct tasklet; | |
201 | struct list_head client; | |
a8f3067b | 202 | struct list_head pending_queue; |
8d318a50 LW |
203 | struct list_head active; |
204 | struct list_head queue; | |
8d318a50 | 205 | struct stedma40_chan_cfg dma_cfg; |
ce2ca125 | 206 | bool configured; |
8d318a50 LW |
207 | struct d40_base *base; |
208 | /* Default register configurations */ | |
209 | u32 src_def_cfg; | |
210 | u32 dst_def_cfg; | |
211 | struct d40_def_lcsp log_def; | |
8d318a50 | 212 | struct d40_log_lli_full *lcpa; |
95e1400f LW |
213 | /* Runtime reconfiguration */ |
214 | dma_addr_t runtime_addr; | |
215 | enum dma_data_direction runtime_direction; | |
8d318a50 LW |
216 | }; |
217 | ||
218 | /** | |
219 | * struct d40_base - The big global struct, one for each probe'd instance. | |
220 | * | |
221 | * @interrupt_lock: Lock used to make sure one interrupt is handle a time. | |
222 | * @execmd_lock: Lock for execute command usage since several channels share | |
223 | * the same physical register. | |
224 | * @dev: The device structure. | |
225 | * @virtbase: The virtual base address of the DMA's register. | |
f4185592 | 226 | * @rev: silicon revision detected. |
8d318a50 LW |
227 | * @clk: Pointer to the DMA clock structure. |
228 | * @phy_start: Physical memory start of the DMA registers. | |
229 | * @phy_size: Size of the DMA register map. | |
230 | * @irq: The IRQ number. | |
231 | * @num_phy_chans: The number of physical channels. Read from HW. This | |
232 | * is the number of available channels for this driver, not counting "Secure | |
233 | * mode" allocated physical channels. | |
234 | * @num_log_chans: The number of logical channels. Calculated from | |
235 | * num_phy_chans. | |
236 | * @dma_both: dma_device channels that can do both memcpy and slave transfers. | |
237 | * @dma_slave: dma_device channels that can do only do slave transfers. | |
238 | * @dma_memcpy: dma_device channels that can do only do memcpy transfers. | |
8d318a50 LW |
239 | * @log_chans: Room for all possible logical channels in system. |
240 | * @lookup_log_chans: Used to map interrupt number to logical channel. Points | |
241 | * to log_chans entries. | |
242 | * @lookup_phy_chans: Used to map interrupt number to physical channel. Points | |
243 | * to phy_chans entries. | |
244 | * @plat_data: Pointer to provided platform_data which is the driver | |
245 | * configuration. | |
246 | * @phy_res: Vector containing all physical channels. | |
247 | * @lcla_pool: lcla pool settings and data. | |
248 | * @lcpa_base: The virtual mapped address of LCPA. | |
249 | * @phy_lcpa: The physical address of the LCPA. | |
250 | * @lcpa_size: The size of the LCPA area. | |
c675b1b4 | 251 | * @desc_slab: cache for descriptors. |
8d318a50 LW |
252 | */ |
253 | struct d40_base { | |
254 | spinlock_t interrupt_lock; | |
255 | spinlock_t execmd_lock; | |
256 | struct device *dev; | |
257 | void __iomem *virtbase; | |
f4185592 | 258 | u8 rev:4; |
8d318a50 LW |
259 | struct clk *clk; |
260 | phys_addr_t phy_start; | |
261 | resource_size_t phy_size; | |
262 | int irq; | |
263 | int num_phy_chans; | |
264 | int num_log_chans; | |
265 | struct dma_device dma_both; | |
266 | struct dma_device dma_slave; | |
267 | struct dma_device dma_memcpy; | |
268 | struct d40_chan *phy_chans; | |
269 | struct d40_chan *log_chans; | |
270 | struct d40_chan **lookup_log_chans; | |
271 | struct d40_chan **lookup_phy_chans; | |
272 | struct stedma40_platform_data *plat_data; | |
273 | /* Physical half channels */ | |
274 | struct d40_phy_res *phy_res; | |
275 | struct d40_lcla_pool lcla_pool; | |
276 | void *lcpa_base; | |
277 | dma_addr_t phy_lcpa; | |
278 | resource_size_t lcpa_size; | |
c675b1b4 | 279 | struct kmem_cache *desc_slab; |
8d318a50 LW |
280 | }; |
281 | ||
282 | /** | |
283 | * struct d40_interrupt_lookup - lookup table for interrupt handler | |
284 | * | |
285 | * @src: Interrupt mask register. | |
286 | * @clr: Interrupt clear register. | |
287 | * @is_error: true if this is an error interrupt. | |
288 | * @offset: start delta in the lookup_log_chans in d40_base. If equals to | |
289 | * D40_PHY_CHAN, the lookup_phy_chans shall be used instead. | |
290 | */ | |
291 | struct d40_interrupt_lookup { | |
292 | u32 src; | |
293 | u32 clr; | |
294 | bool is_error; | |
295 | int offset; | |
296 | }; | |
297 | ||
298 | /** | |
299 | * struct d40_reg_val - simple lookup struct | |
300 | * | |
301 | * @reg: The register. | |
302 | * @val: The value that belongs to the register in reg. | |
303 | */ | |
304 | struct d40_reg_val { | |
305 | unsigned int reg; | |
306 | unsigned int val; | |
307 | }; | |
308 | ||
262d2915 RV |
309 | static struct device *chan2dev(struct d40_chan *d40c) |
310 | { | |
311 | return &d40c->chan.dev->device; | |
312 | } | |
313 | ||
724a8577 RV |
314 | static bool chan_is_physical(struct d40_chan *chan) |
315 | { | |
316 | return chan->log_num == D40_PHY_CHAN; | |
317 | } | |
318 | ||
319 | static bool chan_is_logical(struct d40_chan *chan) | |
320 | { | |
321 | return !chan_is_physical(chan); | |
322 | } | |
323 | ||
8ca84687 RV |
324 | static void __iomem *chan_base(struct d40_chan *chan) |
325 | { | |
326 | return chan->base->virtbase + D40_DREG_PCBASE + | |
327 | chan->phy_chan->num * D40_DREG_PCDELTA; | |
328 | } | |
329 | ||
6db5a8ba RV |
330 | #define d40_err(dev, format, arg...) \ |
331 | dev_err(dev, "[%s] " format, __func__, ## arg) | |
332 | ||
333 | #define chan_err(d40c, format, arg...) \ | |
334 | d40_err(chan2dev(d40c), format, ## arg) | |
335 | ||
b00f938c | 336 | static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d, |
dbd88788 | 337 | int lli_len) |
8d318a50 | 338 | { |
dbd88788 | 339 | bool is_log = chan_is_logical(d40c); |
8d318a50 LW |
340 | u32 align; |
341 | void *base; | |
342 | ||
343 | if (is_log) | |
344 | align = sizeof(struct d40_log_lli); | |
345 | else | |
346 | align = sizeof(struct d40_phy_lli); | |
347 | ||
348 | if (lli_len == 1) { | |
349 | base = d40d->lli_pool.pre_alloc_lli; | |
350 | d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli); | |
351 | d40d->lli_pool.base = NULL; | |
352 | } else { | |
594ece4d | 353 | d40d->lli_pool.size = lli_len * 2 * align; |
8d318a50 LW |
354 | |
355 | base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT); | |
356 | d40d->lli_pool.base = base; | |
357 | ||
358 | if (d40d->lli_pool.base == NULL) | |
359 | return -ENOMEM; | |
360 | } | |
361 | ||
362 | if (is_log) { | |
d924abad | 363 | d40d->lli_log.src = PTR_ALIGN(base, align); |
594ece4d | 364 | d40d->lli_log.dst = d40d->lli_log.src + lli_len; |
b00f938c RV |
365 | |
366 | d40d->lli_pool.dma_addr = 0; | |
8d318a50 | 367 | } else { |
d924abad | 368 | d40d->lli_phy.src = PTR_ALIGN(base, align); |
594ece4d | 369 | d40d->lli_phy.dst = d40d->lli_phy.src + lli_len; |
b00f938c RV |
370 | |
371 | d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev, | |
372 | d40d->lli_phy.src, | |
373 | d40d->lli_pool.size, | |
374 | DMA_TO_DEVICE); | |
375 | ||
376 | if (dma_mapping_error(d40c->base->dev, | |
377 | d40d->lli_pool.dma_addr)) { | |
378 | kfree(d40d->lli_pool.base); | |
379 | d40d->lli_pool.base = NULL; | |
380 | d40d->lli_pool.dma_addr = 0; | |
381 | return -ENOMEM; | |
382 | } | |
8d318a50 LW |
383 | } |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
b00f938c | 388 | static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d) |
8d318a50 | 389 | { |
b00f938c RV |
390 | if (d40d->lli_pool.dma_addr) |
391 | dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr, | |
392 | d40d->lli_pool.size, DMA_TO_DEVICE); | |
393 | ||
8d318a50 LW |
394 | kfree(d40d->lli_pool.base); |
395 | d40d->lli_pool.base = NULL; | |
396 | d40d->lli_pool.size = 0; | |
397 | d40d->lli_log.src = NULL; | |
398 | d40d->lli_log.dst = NULL; | |
399 | d40d->lli_phy.src = NULL; | |
400 | d40d->lli_phy.dst = NULL; | |
8d318a50 LW |
401 | } |
402 | ||
698e4732 JA |
403 | static int d40_lcla_alloc_one(struct d40_chan *d40c, |
404 | struct d40_desc *d40d) | |
405 | { | |
406 | unsigned long flags; | |
407 | int i; | |
408 | int ret = -EINVAL; | |
409 | int p; | |
410 | ||
411 | spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); | |
412 | ||
413 | p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP; | |
414 | ||
415 | /* | |
416 | * Allocate both src and dst at the same time, therefore the half | |
417 | * start on 1 since 0 can't be used since zero is used as end marker. | |
418 | */ | |
419 | for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { | |
420 | if (!d40c->base->lcla_pool.alloc_map[p + i]) { | |
421 | d40c->base->lcla_pool.alloc_map[p + i] = d40d; | |
422 | d40d->lcla_alloc++; | |
423 | ret = i; | |
424 | break; | |
425 | } | |
426 | } | |
427 | ||
428 | spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); | |
429 | ||
430 | return ret; | |
431 | } | |
432 | ||
433 | static int d40_lcla_free_all(struct d40_chan *d40c, | |
434 | struct d40_desc *d40d) | |
435 | { | |
436 | unsigned long flags; | |
437 | int i; | |
438 | int ret = -EINVAL; | |
439 | ||
724a8577 | 440 | if (chan_is_physical(d40c)) |
698e4732 JA |
441 | return 0; |
442 | ||
443 | spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); | |
444 | ||
445 | for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { | |
446 | if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * | |
447 | D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) { | |
448 | d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * | |
449 | D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL; | |
450 | d40d->lcla_alloc--; | |
451 | if (d40d->lcla_alloc == 0) { | |
452 | ret = 0; | |
453 | break; | |
454 | } | |
455 | } | |
456 | } | |
457 | ||
458 | spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); | |
459 | ||
460 | return ret; | |
461 | ||
462 | } | |
463 | ||
8d318a50 LW |
464 | static void d40_desc_remove(struct d40_desc *d40d) |
465 | { | |
466 | list_del(&d40d->node); | |
467 | } | |
468 | ||
469 | static struct d40_desc *d40_desc_get(struct d40_chan *d40c) | |
470 | { | |
a2c15fa4 | 471 | struct d40_desc *desc = NULL; |
8d318a50 LW |
472 | |
473 | if (!list_empty(&d40c->client)) { | |
a2c15fa4 RV |
474 | struct d40_desc *d; |
475 | struct d40_desc *_d; | |
476 | ||
8d318a50 LW |
477 | list_for_each_entry_safe(d, _d, &d40c->client, node) |
478 | if (async_tx_test_ack(&d->txd)) { | |
b00f938c | 479 | d40_pool_lli_free(d40c, d); |
8d318a50 | 480 | d40_desc_remove(d); |
a2c15fa4 RV |
481 | desc = d; |
482 | memset(desc, 0, sizeof(*desc)); | |
c675b1b4 | 483 | break; |
8d318a50 | 484 | } |
8d318a50 | 485 | } |
a2c15fa4 RV |
486 | |
487 | if (!desc) | |
488 | desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT); | |
489 | ||
490 | if (desc) | |
491 | INIT_LIST_HEAD(&desc->node); | |
492 | ||
493 | return desc; | |
8d318a50 LW |
494 | } |
495 | ||
496 | static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d) | |
497 | { | |
698e4732 | 498 | |
b00f938c | 499 | d40_pool_lli_free(d40c, d40d); |
698e4732 | 500 | d40_lcla_free_all(d40c, d40d); |
c675b1b4 | 501 | kmem_cache_free(d40c->base->desc_slab, d40d); |
8d318a50 LW |
502 | } |
503 | ||
504 | static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc) | |
505 | { | |
506 | list_add_tail(&desc->node, &d40c->active); | |
507 | } | |
508 | ||
1c4b0927 RV |
509 | static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc) |
510 | { | |
511 | struct d40_phy_lli *lli_dst = desc->lli_phy.dst; | |
512 | struct d40_phy_lli *lli_src = desc->lli_phy.src; | |
513 | void __iomem *base = chan_base(chan); | |
514 | ||
515 | writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG); | |
516 | writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT); | |
517 | writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR); | |
518 | writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK); | |
519 | ||
520 | writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG); | |
521 | writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT); | |
522 | writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR); | |
523 | writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK); | |
524 | } | |
525 | ||
e65889c7 | 526 | static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc) |
698e4732 | 527 | { |
e65889c7 RV |
528 | struct d40_lcla_pool *pool = &chan->base->lcla_pool; |
529 | struct d40_log_lli_bidir *lli = &desc->lli_log; | |
530 | int lli_current = desc->lli_current; | |
531 | int lli_len = desc->lli_len; | |
0c842b55 | 532 | bool cyclic = desc->cyclic; |
e65889c7 | 533 | int curr_lcla = -EINVAL; |
0c842b55 RV |
534 | int first_lcla = 0; |
535 | bool linkback; | |
e65889c7 | 536 | |
0c842b55 RV |
537 | /* |
538 | * We may have partially running cyclic transfers, in case we did't get | |
539 | * enough LCLA entries. | |
540 | */ | |
541 | linkback = cyclic && lli_current == 0; | |
542 | ||
543 | /* | |
544 | * For linkback, we need one LCLA even with only one link, because we | |
545 | * can't link back to the one in LCPA space | |
546 | */ | |
547 | if (linkback || (lli_len - lli_current > 1)) { | |
e65889c7 | 548 | curr_lcla = d40_lcla_alloc_one(chan, desc); |
0c842b55 RV |
549 | first_lcla = curr_lcla; |
550 | } | |
551 | ||
552 | /* | |
553 | * For linkback, we normally load the LCPA in the loop since we need to | |
554 | * link it to the second LCLA and not the first. However, if we | |
555 | * couldn't even get a first LCLA, then we have to run in LCPA and | |
556 | * reload manually. | |
557 | */ | |
558 | if (!linkback || curr_lcla == -EINVAL) { | |
559 | unsigned int flags = 0; | |
e65889c7 | 560 | |
0c842b55 RV |
561 | if (curr_lcla == -EINVAL) |
562 | flags |= LLI_TERM_INT; | |
e65889c7 | 563 | |
0c842b55 RV |
564 | d40_log_lli_lcpa_write(chan->lcpa, |
565 | &lli->dst[lli_current], | |
566 | &lli->src[lli_current], | |
567 | curr_lcla, | |
568 | flags); | |
569 | lli_current++; | |
570 | } | |
6045f0bb RV |
571 | |
572 | if (curr_lcla < 0) | |
573 | goto out; | |
574 | ||
e65889c7 RV |
575 | for (; lli_current < lli_len; lli_current++) { |
576 | unsigned int lcla_offset = chan->phy_chan->num * 1024 + | |
577 | 8 * curr_lcla * 2; | |
578 | struct d40_log_lli *lcla = pool->base + lcla_offset; | |
0c842b55 | 579 | unsigned int flags = 0; |
e65889c7 RV |
580 | int next_lcla; |
581 | ||
582 | if (lli_current + 1 < lli_len) | |
583 | next_lcla = d40_lcla_alloc_one(chan, desc); | |
584 | else | |
0c842b55 RV |
585 | next_lcla = linkback ? first_lcla : -EINVAL; |
586 | ||
587 | if (cyclic || next_lcla == -EINVAL) | |
588 | flags |= LLI_TERM_INT; | |
e65889c7 | 589 | |
0c842b55 RV |
590 | if (linkback && curr_lcla == first_lcla) { |
591 | /* First link goes in both LCPA and LCLA */ | |
592 | d40_log_lli_lcpa_write(chan->lcpa, | |
593 | &lli->dst[lli_current], | |
594 | &lli->src[lli_current], | |
595 | next_lcla, flags); | |
596 | } | |
597 | ||
598 | /* | |
599 | * One unused LCLA in the cyclic case if the very first | |
600 | * next_lcla fails... | |
601 | */ | |
e65889c7 RV |
602 | d40_log_lli_lcla_write(lcla, |
603 | &lli->dst[lli_current], | |
604 | &lli->src[lli_current], | |
0c842b55 | 605 | next_lcla, flags); |
e65889c7 RV |
606 | |
607 | dma_sync_single_range_for_device(chan->base->dev, | |
608 | pool->dma_addr, lcla_offset, | |
609 | 2 * sizeof(struct d40_log_lli), | |
610 | DMA_TO_DEVICE); | |
611 | ||
612 | curr_lcla = next_lcla; | |
613 | ||
0c842b55 | 614 | if (curr_lcla == -EINVAL || curr_lcla == first_lcla) { |
e65889c7 RV |
615 | lli_current++; |
616 | break; | |
617 | } | |
618 | } | |
619 | ||
6045f0bb | 620 | out: |
e65889c7 RV |
621 | desc->lli_current = lli_current; |
622 | } | |
698e4732 | 623 | |
e65889c7 RV |
624 | static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d) |
625 | { | |
724a8577 | 626 | if (chan_is_physical(d40c)) { |
1c4b0927 | 627 | d40_phy_lli_load(d40c, d40d); |
698e4732 | 628 | d40d->lli_current = d40d->lli_len; |
e65889c7 RV |
629 | } else |
630 | d40_log_lli_to_lcxa(d40c, d40d); | |
698e4732 JA |
631 | } |
632 | ||
8d318a50 LW |
633 | static struct d40_desc *d40_first_active_get(struct d40_chan *d40c) |
634 | { | |
635 | struct d40_desc *d; | |
636 | ||
637 | if (list_empty(&d40c->active)) | |
638 | return NULL; | |
639 | ||
640 | d = list_first_entry(&d40c->active, | |
641 | struct d40_desc, | |
642 | node); | |
643 | return d; | |
644 | } | |
645 | ||
646 | static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc) | |
647 | { | |
a8f3067b PF |
648 | list_add_tail(&desc->node, &d40c->pending_queue); |
649 | } | |
650 | ||
651 | static struct d40_desc *d40_first_pending(struct d40_chan *d40c) | |
652 | { | |
653 | struct d40_desc *d; | |
654 | ||
655 | if (list_empty(&d40c->pending_queue)) | |
656 | return NULL; | |
657 | ||
658 | d = list_first_entry(&d40c->pending_queue, | |
659 | struct d40_desc, | |
660 | node); | |
661 | return d; | |
8d318a50 LW |
662 | } |
663 | ||
664 | static struct d40_desc *d40_first_queued(struct d40_chan *d40c) | |
665 | { | |
666 | struct d40_desc *d; | |
667 | ||
668 | if (list_empty(&d40c->queue)) | |
669 | return NULL; | |
670 | ||
671 | d = list_first_entry(&d40c->queue, | |
672 | struct d40_desc, | |
673 | node); | |
674 | return d; | |
675 | } | |
676 | ||
d49278e3 PF |
677 | static int d40_psize_2_burst_size(bool is_log, int psize) |
678 | { | |
679 | if (is_log) { | |
680 | if (psize == STEDMA40_PSIZE_LOG_1) | |
681 | return 1; | |
682 | } else { | |
683 | if (psize == STEDMA40_PSIZE_PHY_1) | |
684 | return 1; | |
685 | } | |
686 | ||
687 | return 2 << psize; | |
688 | } | |
689 | ||
690 | /* | |
691 | * The dma only supports transmitting packages up to | |
692 | * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of | |
693 | * dma elements required to send the entire sg list | |
694 | */ | |
695 | static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2) | |
696 | { | |
697 | int dmalen; | |
698 | u32 max_w = max(data_width1, data_width2); | |
699 | u32 min_w = min(data_width1, data_width2); | |
700 | u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w); | |
701 | ||
702 | if (seg_max > STEDMA40_MAX_SEG_SIZE) | |
703 | seg_max -= (1 << max_w); | |
704 | ||
705 | if (!IS_ALIGNED(size, 1 << max_w)) | |
706 | return -EINVAL; | |
707 | ||
708 | if (size <= seg_max) | |
709 | dmalen = 1; | |
710 | else { | |
711 | dmalen = size / seg_max; | |
712 | if (dmalen * seg_max < size) | |
713 | dmalen++; | |
714 | } | |
715 | return dmalen; | |
716 | } | |
717 | ||
718 | static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len, | |
719 | u32 data_width1, u32 data_width2) | |
720 | { | |
721 | struct scatterlist *sg; | |
722 | int i; | |
723 | int len = 0; | |
724 | int ret; | |
725 | ||
726 | for_each_sg(sgl, sg, sg_len, i) { | |
727 | ret = d40_size_2_dmalen(sg_dma_len(sg), | |
728 | data_width1, data_width2); | |
729 | if (ret < 0) | |
730 | return ret; | |
731 | len += ret; | |
732 | } | |
733 | return len; | |
734 | } | |
8d318a50 | 735 | |
d49278e3 | 736 | /* Support functions for logical channels */ |
8d318a50 LW |
737 | |
738 | static int d40_channel_execute_command(struct d40_chan *d40c, | |
739 | enum d40_command command) | |
740 | { | |
767a9675 JA |
741 | u32 status; |
742 | int i; | |
8d318a50 LW |
743 | void __iomem *active_reg; |
744 | int ret = 0; | |
745 | unsigned long flags; | |
1d392a7b | 746 | u32 wmask; |
8d318a50 LW |
747 | |
748 | spin_lock_irqsave(&d40c->base->execmd_lock, flags); | |
749 | ||
750 | if (d40c->phy_chan->num % 2 == 0) | |
751 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; | |
752 | else | |
753 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; | |
754 | ||
755 | if (command == D40_DMA_SUSPEND_REQ) { | |
756 | status = (readl(active_reg) & | |
757 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> | |
758 | D40_CHAN_POS(d40c->phy_chan->num); | |
759 | ||
760 | if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) | |
761 | goto done; | |
762 | } | |
763 | ||
1d392a7b JA |
764 | wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num)); |
765 | writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)), | |
766 | active_reg); | |
8d318a50 LW |
767 | |
768 | if (command == D40_DMA_SUSPEND_REQ) { | |
769 | ||
770 | for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) { | |
771 | status = (readl(active_reg) & | |
772 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> | |
773 | D40_CHAN_POS(d40c->phy_chan->num); | |
774 | ||
775 | cpu_relax(); | |
776 | /* | |
777 | * Reduce the number of bus accesses while | |
778 | * waiting for the DMA to suspend. | |
779 | */ | |
780 | udelay(3); | |
781 | ||
782 | if (status == D40_DMA_STOP || | |
783 | status == D40_DMA_SUSPENDED) | |
784 | break; | |
785 | } | |
786 | ||
787 | if (i == D40_SUSPEND_MAX_IT) { | |
6db5a8ba RV |
788 | chan_err(d40c, |
789 | "unable to suspend the chl %d (log: %d) status %x\n", | |
790 | d40c->phy_chan->num, d40c->log_num, | |
8d318a50 LW |
791 | status); |
792 | dump_stack(); | |
793 | ret = -EBUSY; | |
794 | } | |
795 | ||
796 | } | |
797 | done: | |
798 | spin_unlock_irqrestore(&d40c->base->execmd_lock, flags); | |
799 | return ret; | |
800 | } | |
801 | ||
802 | static void d40_term_all(struct d40_chan *d40c) | |
803 | { | |
804 | struct d40_desc *d40d; | |
8d318a50 LW |
805 | |
806 | /* Release active descriptors */ | |
807 | while ((d40d = d40_first_active_get(d40c))) { | |
808 | d40_desc_remove(d40d); | |
8d318a50 LW |
809 | d40_desc_free(d40c, d40d); |
810 | } | |
811 | ||
812 | /* Release queued descriptors waiting for transfer */ | |
813 | while ((d40d = d40_first_queued(d40c))) { | |
814 | d40_desc_remove(d40d); | |
8d318a50 LW |
815 | d40_desc_free(d40c, d40d); |
816 | } | |
817 | ||
a8f3067b PF |
818 | /* Release pending descriptors */ |
819 | while ((d40d = d40_first_pending(d40c))) { | |
820 | d40_desc_remove(d40d); | |
821 | d40_desc_free(d40c, d40d); | |
822 | } | |
8d318a50 LW |
823 | |
824 | d40c->pending_tx = 0; | |
825 | d40c->busy = false; | |
826 | } | |
827 | ||
262d2915 RV |
828 | static void __d40_config_set_event(struct d40_chan *d40c, bool enable, |
829 | u32 event, int reg) | |
830 | { | |
8ca84687 | 831 | void __iomem *addr = chan_base(d40c) + reg; |
262d2915 RV |
832 | int tries; |
833 | ||
834 | if (!enable) { | |
835 | writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) | |
836 | | ~D40_EVENTLINE_MASK(event), addr); | |
837 | return; | |
838 | } | |
839 | ||
840 | /* | |
841 | * The hardware sometimes doesn't register the enable when src and dst | |
842 | * event lines are active on the same logical channel. Retry to ensure | |
843 | * it does. Usually only one retry is sufficient. | |
844 | */ | |
845 | tries = 100; | |
846 | while (--tries) { | |
847 | writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) | |
848 | | ~D40_EVENTLINE_MASK(event), addr); | |
849 | ||
850 | if (readl(addr) & D40_EVENTLINE_MASK(event)) | |
851 | break; | |
852 | } | |
853 | ||
854 | if (tries != 99) | |
855 | dev_dbg(chan2dev(d40c), | |
856 | "[%s] workaround enable S%cLNK (%d tries)\n", | |
857 | __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D', | |
858 | 100 - tries); | |
859 | ||
860 | WARN_ON(!tries); | |
861 | } | |
862 | ||
8d318a50 LW |
863 | static void d40_config_set_event(struct d40_chan *d40c, bool do_enable) |
864 | { | |
8d318a50 LW |
865 | unsigned long flags; |
866 | ||
8d318a50 LW |
867 | spin_lock_irqsave(&d40c->phy_chan->lock, flags); |
868 | ||
869 | /* Enable event line connected to device (or memcpy) */ | |
870 | if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || | |
871 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) { | |
872 | u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); | |
873 | ||
262d2915 RV |
874 | __d40_config_set_event(d40c, do_enable, event, |
875 | D40_CHAN_REG_SSLNK); | |
8d318a50 | 876 | } |
262d2915 | 877 | |
8d318a50 LW |
878 | if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) { |
879 | u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); | |
880 | ||
262d2915 RV |
881 | __d40_config_set_event(d40c, do_enable, event, |
882 | D40_CHAN_REG_SDLNK); | |
8d318a50 LW |
883 | } |
884 | ||
885 | spin_unlock_irqrestore(&d40c->phy_chan->lock, flags); | |
886 | } | |
887 | ||
a5ebca47 | 888 | static u32 d40_chan_has_events(struct d40_chan *d40c) |
8d318a50 | 889 | { |
8ca84687 | 890 | void __iomem *chanbase = chan_base(d40c); |
be8cb7df | 891 | u32 val; |
8d318a50 | 892 | |
8ca84687 RV |
893 | val = readl(chanbase + D40_CHAN_REG_SSLNK); |
894 | val |= readl(chanbase + D40_CHAN_REG_SDLNK); | |
be8cb7df | 895 | |
a5ebca47 | 896 | return val; |
8d318a50 LW |
897 | } |
898 | ||
20a5b6d0 RV |
899 | static u32 d40_get_prmo(struct d40_chan *d40c) |
900 | { | |
901 | static const unsigned int phy_map[] = { | |
902 | [STEDMA40_PCHAN_BASIC_MODE] | |
903 | = D40_DREG_PRMO_PCHAN_BASIC, | |
904 | [STEDMA40_PCHAN_MODULO_MODE] | |
905 | = D40_DREG_PRMO_PCHAN_MODULO, | |
906 | [STEDMA40_PCHAN_DOUBLE_DST_MODE] | |
907 | = D40_DREG_PRMO_PCHAN_DOUBLE_DST, | |
908 | }; | |
909 | static const unsigned int log_map[] = { | |
910 | [STEDMA40_LCHAN_SRC_PHY_DST_LOG] | |
911 | = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG, | |
912 | [STEDMA40_LCHAN_SRC_LOG_DST_PHY] | |
913 | = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY, | |
914 | [STEDMA40_LCHAN_SRC_LOG_DST_LOG] | |
915 | = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG, | |
916 | }; | |
917 | ||
724a8577 | 918 | if (chan_is_physical(d40c)) |
20a5b6d0 RV |
919 | return phy_map[d40c->dma_cfg.mode_opt]; |
920 | else | |
921 | return log_map[d40c->dma_cfg.mode_opt]; | |
922 | } | |
923 | ||
b55912c6 | 924 | static void d40_config_write(struct d40_chan *d40c) |
8d318a50 LW |
925 | { |
926 | u32 addr_base; | |
927 | u32 var; | |
8d318a50 LW |
928 | |
929 | /* Odd addresses are even addresses + 4 */ | |
930 | addr_base = (d40c->phy_chan->num % 2) * 4; | |
931 | /* Setup channel mode to logical or physical */ | |
724a8577 | 932 | var = ((u32)(chan_is_logical(d40c)) + 1) << |
8d318a50 LW |
933 | D40_CHAN_POS(d40c->phy_chan->num); |
934 | writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base); | |
935 | ||
936 | /* Setup operational mode option register */ | |
20a5b6d0 | 937 | var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num); |
8d318a50 LW |
938 | |
939 | writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base); | |
940 | ||
724a8577 | 941 | if (chan_is_logical(d40c)) { |
8ca84687 RV |
942 | int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) |
943 | & D40_SREG_ELEM_LOG_LIDX_MASK; | |
944 | void __iomem *chanbase = chan_base(d40c); | |
945 | ||
8d318a50 | 946 | /* Set default config for CFG reg */ |
8ca84687 RV |
947 | writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG); |
948 | writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG); | |
8d318a50 | 949 | |
b55912c6 | 950 | /* Set LIDX for lcla */ |
8ca84687 RV |
951 | writel(lidx, chanbase + D40_CHAN_REG_SSELT); |
952 | writel(lidx, chanbase + D40_CHAN_REG_SDELT); | |
8d318a50 | 953 | } |
8d318a50 LW |
954 | } |
955 | ||
aa182ae2 JA |
956 | static u32 d40_residue(struct d40_chan *d40c) |
957 | { | |
958 | u32 num_elt; | |
959 | ||
724a8577 | 960 | if (chan_is_logical(d40c)) |
aa182ae2 JA |
961 | num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK) |
962 | >> D40_MEM_LCSP2_ECNT_POS; | |
8ca84687 RV |
963 | else { |
964 | u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT); | |
965 | num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK) | |
966 | >> D40_SREG_ELEM_PHY_ECNT_POS; | |
967 | } | |
968 | ||
aa182ae2 JA |
969 | return num_elt * (1 << d40c->dma_cfg.dst_info.data_width); |
970 | } | |
971 | ||
972 | static bool d40_tx_is_linked(struct d40_chan *d40c) | |
973 | { | |
974 | bool is_link; | |
975 | ||
724a8577 | 976 | if (chan_is_logical(d40c)) |
aa182ae2 JA |
977 | is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK; |
978 | else | |
8ca84687 RV |
979 | is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK) |
980 | & D40_SREG_LNK_PHYS_LNK_MASK; | |
981 | ||
aa182ae2 JA |
982 | return is_link; |
983 | } | |
984 | ||
86eb5fb6 | 985 | static int d40_pause(struct d40_chan *d40c) |
aa182ae2 | 986 | { |
aa182ae2 JA |
987 | int res = 0; |
988 | unsigned long flags; | |
989 | ||
3ac012af JA |
990 | if (!d40c->busy) |
991 | return 0; | |
992 | ||
aa182ae2 JA |
993 | spin_lock_irqsave(&d40c->lock, flags); |
994 | ||
995 | res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); | |
996 | if (res == 0) { | |
724a8577 | 997 | if (chan_is_logical(d40c)) { |
aa182ae2 JA |
998 | d40_config_set_event(d40c, false); |
999 | /* Resume the other logical channels if any */ | |
1000 | if (d40_chan_has_events(d40c)) | |
1001 | res = d40_channel_execute_command(d40c, | |
1002 | D40_DMA_RUN); | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1007 | return res; | |
1008 | } | |
1009 | ||
86eb5fb6 | 1010 | static int d40_resume(struct d40_chan *d40c) |
aa182ae2 | 1011 | { |
aa182ae2 JA |
1012 | int res = 0; |
1013 | unsigned long flags; | |
1014 | ||
3ac012af JA |
1015 | if (!d40c->busy) |
1016 | return 0; | |
1017 | ||
aa182ae2 JA |
1018 | spin_lock_irqsave(&d40c->lock, flags); |
1019 | ||
1020 | if (d40c->base->rev == 0) | |
724a8577 | 1021 | if (chan_is_logical(d40c)) { |
aa182ae2 JA |
1022 | res = d40_channel_execute_command(d40c, |
1023 | D40_DMA_SUSPEND_REQ); | |
1024 | goto no_suspend; | |
1025 | } | |
1026 | ||
1027 | /* If bytes left to transfer or linked tx resume job */ | |
1028 | if (d40_residue(d40c) || d40_tx_is_linked(d40c)) { | |
1029 | ||
724a8577 | 1030 | if (chan_is_logical(d40c)) |
aa182ae2 JA |
1031 | d40_config_set_event(d40c, true); |
1032 | ||
1033 | res = d40_channel_execute_command(d40c, D40_DMA_RUN); | |
1034 | } | |
1035 | ||
1036 | no_suspend: | |
1037 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1038 | return res; | |
1039 | } | |
1040 | ||
86eb5fb6 RV |
1041 | static int d40_terminate_all(struct d40_chan *chan) |
1042 | { | |
1043 | unsigned long flags; | |
1044 | int ret = 0; | |
1045 | ||
1046 | ret = d40_pause(chan); | |
1047 | if (!ret && chan_is_physical(chan)) | |
1048 | ret = d40_channel_execute_command(chan, D40_DMA_STOP); | |
1049 | ||
1050 | spin_lock_irqsave(&chan->lock, flags); | |
1051 | d40_term_all(chan); | |
1052 | spin_unlock_irqrestore(&chan->lock, flags); | |
1053 | ||
1054 | return ret; | |
1055 | } | |
1056 | ||
8d318a50 LW |
1057 | static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx) |
1058 | { | |
1059 | struct d40_chan *d40c = container_of(tx->chan, | |
1060 | struct d40_chan, | |
1061 | chan); | |
1062 | struct d40_desc *d40d = container_of(tx, struct d40_desc, txd); | |
1063 | unsigned long flags; | |
1064 | ||
1065 | spin_lock_irqsave(&d40c->lock, flags); | |
1066 | ||
aa182ae2 JA |
1067 | d40c->chan.cookie++; |
1068 | ||
1069 | if (d40c->chan.cookie < 0) | |
1070 | d40c->chan.cookie = 1; | |
1071 | ||
1072 | d40d->txd.cookie = d40c->chan.cookie; | |
1073 | ||
8d318a50 LW |
1074 | d40_desc_queue(d40c, d40d); |
1075 | ||
1076 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1077 | ||
1078 | return tx->cookie; | |
1079 | } | |
1080 | ||
1081 | static int d40_start(struct d40_chan *d40c) | |
1082 | { | |
f4185592 LW |
1083 | if (d40c->base->rev == 0) { |
1084 | int err; | |
1085 | ||
724a8577 | 1086 | if (chan_is_logical(d40c)) { |
f4185592 LW |
1087 | err = d40_channel_execute_command(d40c, |
1088 | D40_DMA_SUSPEND_REQ); | |
1089 | if (err) | |
1090 | return err; | |
1091 | } | |
1092 | } | |
1093 | ||
724a8577 | 1094 | if (chan_is_logical(d40c)) |
8d318a50 | 1095 | d40_config_set_event(d40c, true); |
8d318a50 | 1096 | |
0c32269d | 1097 | return d40_channel_execute_command(d40c, D40_DMA_RUN); |
8d318a50 LW |
1098 | } |
1099 | ||
1100 | static struct d40_desc *d40_queue_start(struct d40_chan *d40c) | |
1101 | { | |
1102 | struct d40_desc *d40d; | |
1103 | int err; | |
1104 | ||
1105 | /* Start queued jobs, if any */ | |
1106 | d40d = d40_first_queued(d40c); | |
1107 | ||
1108 | if (d40d != NULL) { | |
1109 | d40c->busy = true; | |
1110 | ||
1111 | /* Remove from queue */ | |
1112 | d40_desc_remove(d40d); | |
1113 | ||
1114 | /* Add to active queue */ | |
1115 | d40_desc_submit(d40c, d40d); | |
1116 | ||
7d83a854 RV |
1117 | /* Initiate DMA job */ |
1118 | d40_desc_load(d40c, d40d); | |
8d318a50 | 1119 | |
7d83a854 RV |
1120 | /* Start dma job */ |
1121 | err = d40_start(d40c); | |
8d318a50 | 1122 | |
7d83a854 RV |
1123 | if (err) |
1124 | return NULL; | |
8d318a50 LW |
1125 | } |
1126 | ||
1127 | return d40d; | |
1128 | } | |
1129 | ||
1130 | /* called from interrupt context */ | |
1131 | static void dma_tc_handle(struct d40_chan *d40c) | |
1132 | { | |
1133 | struct d40_desc *d40d; | |
1134 | ||
8d318a50 LW |
1135 | /* Get first active entry from list */ |
1136 | d40d = d40_first_active_get(d40c); | |
1137 | ||
1138 | if (d40d == NULL) | |
1139 | return; | |
1140 | ||
0c842b55 RV |
1141 | if (d40d->cyclic) { |
1142 | /* | |
1143 | * If this was a paritially loaded list, we need to reloaded | |
1144 | * it, and only when the list is completed. We need to check | |
1145 | * for done because the interrupt will hit for every link, and | |
1146 | * not just the last one. | |
1147 | */ | |
1148 | if (d40d->lli_current < d40d->lli_len | |
1149 | && !d40_tx_is_linked(d40c) | |
1150 | && !d40_residue(d40c)) { | |
1151 | d40_lcla_free_all(d40c, d40d); | |
1152 | d40_desc_load(d40c, d40d); | |
1153 | (void) d40_start(d40c); | |
8d318a50 | 1154 | |
0c842b55 RV |
1155 | if (d40d->lli_current == d40d->lli_len) |
1156 | d40d->lli_current = 0; | |
1157 | } | |
1158 | } else { | |
1159 | d40_lcla_free_all(d40c, d40d); | |
8d318a50 | 1160 | |
0c842b55 RV |
1161 | if (d40d->lli_current < d40d->lli_len) { |
1162 | d40_desc_load(d40c, d40d); | |
1163 | /* Start dma job */ | |
1164 | (void) d40_start(d40c); | |
1165 | return; | |
1166 | } | |
1167 | ||
1168 | if (d40_queue_start(d40c) == NULL) | |
1169 | d40c->busy = false; | |
1170 | } | |
8d318a50 LW |
1171 | |
1172 | d40c->pending_tx++; | |
1173 | tasklet_schedule(&d40c->tasklet); | |
1174 | ||
1175 | } | |
1176 | ||
1177 | static void dma_tasklet(unsigned long data) | |
1178 | { | |
1179 | struct d40_chan *d40c = (struct d40_chan *) data; | |
767a9675 | 1180 | struct d40_desc *d40d; |
8d318a50 LW |
1181 | unsigned long flags; |
1182 | dma_async_tx_callback callback; | |
1183 | void *callback_param; | |
1184 | ||
1185 | spin_lock_irqsave(&d40c->lock, flags); | |
1186 | ||
1187 | /* Get first active entry from list */ | |
767a9675 | 1188 | d40d = d40_first_active_get(d40c); |
767a9675 | 1189 | if (d40d == NULL) |
8d318a50 LW |
1190 | goto err; |
1191 | ||
0c842b55 RV |
1192 | if (!d40d->cyclic) |
1193 | d40c->completed = d40d->txd.cookie; | |
8d318a50 LW |
1194 | |
1195 | /* | |
1196 | * If terminating a channel pending_tx is set to zero. | |
1197 | * This prevents any finished active jobs to return to the client. | |
1198 | */ | |
1199 | if (d40c->pending_tx == 0) { | |
1200 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1201 | return; | |
1202 | } | |
1203 | ||
1204 | /* Callback to client */ | |
767a9675 JA |
1205 | callback = d40d->txd.callback; |
1206 | callback_param = d40d->txd.callback_param; | |
1207 | ||
0c842b55 RV |
1208 | if (!d40d->cyclic) { |
1209 | if (async_tx_test_ack(&d40d->txd)) { | |
1210 | d40_pool_lli_free(d40c, d40d); | |
767a9675 | 1211 | d40_desc_remove(d40d); |
0c842b55 RV |
1212 | d40_desc_free(d40c, d40d); |
1213 | } else { | |
1214 | if (!d40d->is_in_client_list) { | |
1215 | d40_desc_remove(d40d); | |
1216 | d40_lcla_free_all(d40c, d40d); | |
1217 | list_add_tail(&d40d->node, &d40c->client); | |
1218 | d40d->is_in_client_list = true; | |
1219 | } | |
8d318a50 LW |
1220 | } |
1221 | } | |
1222 | ||
1223 | d40c->pending_tx--; | |
1224 | ||
1225 | if (d40c->pending_tx) | |
1226 | tasklet_schedule(&d40c->tasklet); | |
1227 | ||
1228 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1229 | ||
767a9675 | 1230 | if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT)) |
8d318a50 LW |
1231 | callback(callback_param); |
1232 | ||
1233 | return; | |
1234 | ||
1235 | err: | |
25985edc | 1236 | /* Rescue manoeuvre if receiving double interrupts */ |
8d318a50 LW |
1237 | if (d40c->pending_tx > 0) |
1238 | d40c->pending_tx--; | |
1239 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1240 | } | |
1241 | ||
1242 | static irqreturn_t d40_handle_interrupt(int irq, void *data) | |
1243 | { | |
1244 | static const struct d40_interrupt_lookup il[] = { | |
1245 | {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0}, | |
1246 | {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32}, | |
1247 | {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64}, | |
1248 | {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96}, | |
1249 | {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0}, | |
1250 | {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32}, | |
1251 | {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64}, | |
1252 | {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96}, | |
1253 | {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN}, | |
1254 | {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN}, | |
1255 | }; | |
1256 | ||
1257 | int i; | |
1258 | u32 regs[ARRAY_SIZE(il)]; | |
8d318a50 LW |
1259 | u32 idx; |
1260 | u32 row; | |
1261 | long chan = -1; | |
1262 | struct d40_chan *d40c; | |
1263 | unsigned long flags; | |
1264 | struct d40_base *base = data; | |
1265 | ||
1266 | spin_lock_irqsave(&base->interrupt_lock, flags); | |
1267 | ||
1268 | /* Read interrupt status of both logical and physical channels */ | |
1269 | for (i = 0; i < ARRAY_SIZE(il); i++) | |
1270 | regs[i] = readl(base->virtbase + il[i].src); | |
1271 | ||
1272 | for (;;) { | |
1273 | ||
1274 | chan = find_next_bit((unsigned long *)regs, | |
1275 | BITS_PER_LONG * ARRAY_SIZE(il), chan + 1); | |
1276 | ||
1277 | /* No more set bits found? */ | |
1278 | if (chan == BITS_PER_LONG * ARRAY_SIZE(il)) | |
1279 | break; | |
1280 | ||
1281 | row = chan / BITS_PER_LONG; | |
1282 | idx = chan & (BITS_PER_LONG - 1); | |
1283 | ||
1284 | /* ACK interrupt */ | |
1b00348d | 1285 | writel(1 << idx, base->virtbase + il[row].clr); |
8d318a50 LW |
1286 | |
1287 | if (il[row].offset == D40_PHY_CHAN) | |
1288 | d40c = base->lookup_phy_chans[idx]; | |
1289 | else | |
1290 | d40c = base->lookup_log_chans[il[row].offset + idx]; | |
1291 | spin_lock(&d40c->lock); | |
1292 | ||
1293 | if (!il[row].is_error) | |
1294 | dma_tc_handle(d40c); | |
1295 | else | |
6db5a8ba RV |
1296 | d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n", |
1297 | chan, il[row].offset, idx); | |
8d318a50 LW |
1298 | |
1299 | spin_unlock(&d40c->lock); | |
1300 | } | |
1301 | ||
1302 | spin_unlock_irqrestore(&base->interrupt_lock, flags); | |
1303 | ||
1304 | return IRQ_HANDLED; | |
1305 | } | |
1306 | ||
8d318a50 LW |
1307 | static int d40_validate_conf(struct d40_chan *d40c, |
1308 | struct stedma40_chan_cfg *conf) | |
1309 | { | |
1310 | int res = 0; | |
1311 | u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type); | |
1312 | u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type); | |
38bdbf02 | 1313 | bool is_log = conf->mode == STEDMA40_MODE_LOGICAL; |
8d318a50 | 1314 | |
0747c7ba | 1315 | if (!conf->dir) { |
6db5a8ba | 1316 | chan_err(d40c, "Invalid direction.\n"); |
0747c7ba LW |
1317 | res = -EINVAL; |
1318 | } | |
1319 | ||
1320 | if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY && | |
1321 | d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 && | |
1322 | d40c->runtime_addr == 0) { | |
1323 | ||
6db5a8ba RV |
1324 | chan_err(d40c, "Invalid TX channel address (%d)\n", |
1325 | conf->dst_dev_type); | |
0747c7ba LW |
1326 | res = -EINVAL; |
1327 | } | |
1328 | ||
1329 | if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY && | |
1330 | d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 && | |
1331 | d40c->runtime_addr == 0) { | |
6db5a8ba RV |
1332 | chan_err(d40c, "Invalid RX channel address (%d)\n", |
1333 | conf->src_dev_type); | |
0747c7ba LW |
1334 | res = -EINVAL; |
1335 | } | |
1336 | ||
1337 | if (conf->dir == STEDMA40_MEM_TO_PERIPH && | |
8d318a50 | 1338 | dst_event_group == STEDMA40_DEV_DST_MEMORY) { |
6db5a8ba | 1339 | chan_err(d40c, "Invalid dst\n"); |
8d318a50 LW |
1340 | res = -EINVAL; |
1341 | } | |
1342 | ||
0747c7ba | 1343 | if (conf->dir == STEDMA40_PERIPH_TO_MEM && |
8d318a50 | 1344 | src_event_group == STEDMA40_DEV_SRC_MEMORY) { |
6db5a8ba | 1345 | chan_err(d40c, "Invalid src\n"); |
8d318a50 LW |
1346 | res = -EINVAL; |
1347 | } | |
1348 | ||
1349 | if (src_event_group == STEDMA40_DEV_SRC_MEMORY && | |
1350 | dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) { | |
6db5a8ba | 1351 | chan_err(d40c, "No event line\n"); |
8d318a50 LW |
1352 | res = -EINVAL; |
1353 | } | |
1354 | ||
1355 | if (conf->dir == STEDMA40_PERIPH_TO_PERIPH && | |
1356 | (src_event_group != dst_event_group)) { | |
6db5a8ba | 1357 | chan_err(d40c, "Invalid event group\n"); |
8d318a50 LW |
1358 | res = -EINVAL; |
1359 | } | |
1360 | ||
1361 | if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) { | |
1362 | /* | |
1363 | * DMAC HW supports it. Will be added to this driver, | |
1364 | * in case any dma client requires it. | |
1365 | */ | |
6db5a8ba | 1366 | chan_err(d40c, "periph to periph not supported\n"); |
8d318a50 LW |
1367 | res = -EINVAL; |
1368 | } | |
1369 | ||
d49278e3 PF |
1370 | if (d40_psize_2_burst_size(is_log, conf->src_info.psize) * |
1371 | (1 << conf->src_info.data_width) != | |
1372 | d40_psize_2_burst_size(is_log, conf->dst_info.psize) * | |
1373 | (1 << conf->dst_info.data_width)) { | |
1374 | /* | |
1375 | * The DMAC hardware only supports | |
1376 | * src (burst x width) == dst (burst x width) | |
1377 | */ | |
1378 | ||
6db5a8ba | 1379 | chan_err(d40c, "src (burst x width) != dst (burst x width)\n"); |
d49278e3 PF |
1380 | res = -EINVAL; |
1381 | } | |
1382 | ||
8d318a50 LW |
1383 | return res; |
1384 | } | |
1385 | ||
1386 | static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src, | |
4aed79b2 | 1387 | int log_event_line, bool is_log) |
8d318a50 LW |
1388 | { |
1389 | unsigned long flags; | |
1390 | spin_lock_irqsave(&phy->lock, flags); | |
4aed79b2 | 1391 | if (!is_log) { |
8d318a50 LW |
1392 | /* Physical interrupts are masked per physical full channel */ |
1393 | if (phy->allocated_src == D40_ALLOC_FREE && | |
1394 | phy->allocated_dst == D40_ALLOC_FREE) { | |
1395 | phy->allocated_dst = D40_ALLOC_PHY; | |
1396 | phy->allocated_src = D40_ALLOC_PHY; | |
1397 | goto found; | |
1398 | } else | |
1399 | goto not_found; | |
1400 | } | |
1401 | ||
1402 | /* Logical channel */ | |
1403 | if (is_src) { | |
1404 | if (phy->allocated_src == D40_ALLOC_PHY) | |
1405 | goto not_found; | |
1406 | ||
1407 | if (phy->allocated_src == D40_ALLOC_FREE) | |
1408 | phy->allocated_src = D40_ALLOC_LOG_FREE; | |
1409 | ||
1410 | if (!(phy->allocated_src & (1 << log_event_line))) { | |
1411 | phy->allocated_src |= 1 << log_event_line; | |
1412 | goto found; | |
1413 | } else | |
1414 | goto not_found; | |
1415 | } else { | |
1416 | if (phy->allocated_dst == D40_ALLOC_PHY) | |
1417 | goto not_found; | |
1418 | ||
1419 | if (phy->allocated_dst == D40_ALLOC_FREE) | |
1420 | phy->allocated_dst = D40_ALLOC_LOG_FREE; | |
1421 | ||
1422 | if (!(phy->allocated_dst & (1 << log_event_line))) { | |
1423 | phy->allocated_dst |= 1 << log_event_line; | |
1424 | goto found; | |
1425 | } else | |
1426 | goto not_found; | |
1427 | } | |
1428 | ||
1429 | not_found: | |
1430 | spin_unlock_irqrestore(&phy->lock, flags); | |
1431 | return false; | |
1432 | found: | |
1433 | spin_unlock_irqrestore(&phy->lock, flags); | |
1434 | return true; | |
1435 | } | |
1436 | ||
1437 | static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src, | |
1438 | int log_event_line) | |
1439 | { | |
1440 | unsigned long flags; | |
1441 | bool is_free = false; | |
1442 | ||
1443 | spin_lock_irqsave(&phy->lock, flags); | |
1444 | if (!log_event_line) { | |
8d318a50 LW |
1445 | phy->allocated_dst = D40_ALLOC_FREE; |
1446 | phy->allocated_src = D40_ALLOC_FREE; | |
1447 | is_free = true; | |
1448 | goto out; | |
1449 | } | |
1450 | ||
1451 | /* Logical channel */ | |
1452 | if (is_src) { | |
1453 | phy->allocated_src &= ~(1 << log_event_line); | |
1454 | if (phy->allocated_src == D40_ALLOC_LOG_FREE) | |
1455 | phy->allocated_src = D40_ALLOC_FREE; | |
1456 | } else { | |
1457 | phy->allocated_dst &= ~(1 << log_event_line); | |
1458 | if (phy->allocated_dst == D40_ALLOC_LOG_FREE) | |
1459 | phy->allocated_dst = D40_ALLOC_FREE; | |
1460 | } | |
1461 | ||
1462 | is_free = ((phy->allocated_src | phy->allocated_dst) == | |
1463 | D40_ALLOC_FREE); | |
1464 | ||
1465 | out: | |
1466 | spin_unlock_irqrestore(&phy->lock, flags); | |
1467 | ||
1468 | return is_free; | |
1469 | } | |
1470 | ||
1471 | static int d40_allocate_channel(struct d40_chan *d40c) | |
1472 | { | |
1473 | int dev_type; | |
1474 | int event_group; | |
1475 | int event_line; | |
1476 | struct d40_phy_res *phys; | |
1477 | int i; | |
1478 | int j; | |
1479 | int log_num; | |
1480 | bool is_src; | |
38bdbf02 | 1481 | bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL; |
8d318a50 LW |
1482 | |
1483 | phys = d40c->base->phy_res; | |
1484 | ||
1485 | if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { | |
1486 | dev_type = d40c->dma_cfg.src_dev_type; | |
1487 | log_num = 2 * dev_type; | |
1488 | is_src = true; | |
1489 | } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || | |
1490 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { | |
1491 | /* dst event lines are used for logical memcpy */ | |
1492 | dev_type = d40c->dma_cfg.dst_dev_type; | |
1493 | log_num = 2 * dev_type + 1; | |
1494 | is_src = false; | |
1495 | } else | |
1496 | return -EINVAL; | |
1497 | ||
1498 | event_group = D40_TYPE_TO_GROUP(dev_type); | |
1499 | event_line = D40_TYPE_TO_EVENT(dev_type); | |
1500 | ||
1501 | if (!is_log) { | |
1502 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { | |
1503 | /* Find physical half channel */ | |
1504 | for (i = 0; i < d40c->base->num_phy_chans; i++) { | |
1505 | ||
4aed79b2 MM |
1506 | if (d40_alloc_mask_set(&phys[i], is_src, |
1507 | 0, is_log)) | |
8d318a50 LW |
1508 | goto found_phy; |
1509 | } | |
1510 | } else | |
1511 | for (j = 0; j < d40c->base->num_phy_chans; j += 8) { | |
1512 | int phy_num = j + event_group * 2; | |
1513 | for (i = phy_num; i < phy_num + 2; i++) { | |
508849ad LW |
1514 | if (d40_alloc_mask_set(&phys[i], |
1515 | is_src, | |
1516 | 0, | |
1517 | is_log)) | |
8d318a50 LW |
1518 | goto found_phy; |
1519 | } | |
1520 | } | |
1521 | return -EINVAL; | |
1522 | found_phy: | |
1523 | d40c->phy_chan = &phys[i]; | |
1524 | d40c->log_num = D40_PHY_CHAN; | |
1525 | goto out; | |
1526 | } | |
1527 | if (dev_type == -1) | |
1528 | return -EINVAL; | |
1529 | ||
1530 | /* Find logical channel */ | |
1531 | for (j = 0; j < d40c->base->num_phy_chans; j += 8) { | |
1532 | int phy_num = j + event_group * 2; | |
1533 | /* | |
1534 | * Spread logical channels across all available physical rather | |
1535 | * than pack every logical channel at the first available phy | |
1536 | * channels. | |
1537 | */ | |
1538 | if (is_src) { | |
1539 | for (i = phy_num; i < phy_num + 2; i++) { | |
1540 | if (d40_alloc_mask_set(&phys[i], is_src, | |
4aed79b2 | 1541 | event_line, is_log)) |
8d318a50 LW |
1542 | goto found_log; |
1543 | } | |
1544 | } else { | |
1545 | for (i = phy_num + 1; i >= phy_num; i--) { | |
1546 | if (d40_alloc_mask_set(&phys[i], is_src, | |
4aed79b2 | 1547 | event_line, is_log)) |
8d318a50 LW |
1548 | goto found_log; |
1549 | } | |
1550 | } | |
1551 | } | |
1552 | return -EINVAL; | |
1553 | ||
1554 | found_log: | |
1555 | d40c->phy_chan = &phys[i]; | |
1556 | d40c->log_num = log_num; | |
1557 | out: | |
1558 | ||
1559 | if (is_log) | |
1560 | d40c->base->lookup_log_chans[d40c->log_num] = d40c; | |
1561 | else | |
1562 | d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c; | |
1563 | ||
1564 | return 0; | |
1565 | ||
1566 | } | |
1567 | ||
8d318a50 LW |
1568 | static int d40_config_memcpy(struct d40_chan *d40c) |
1569 | { | |
1570 | dma_cap_mask_t cap = d40c->chan.device->cap_mask; | |
1571 | ||
1572 | if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) { | |
1573 | d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log; | |
1574 | d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY; | |
1575 | d40c->dma_cfg.dst_dev_type = d40c->base->plat_data-> | |
1576 | memcpy[d40c->chan.chan_id]; | |
1577 | ||
1578 | } else if (dma_has_cap(DMA_MEMCPY, cap) && | |
1579 | dma_has_cap(DMA_SLAVE, cap)) { | |
1580 | d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy; | |
1581 | } else { | |
6db5a8ba | 1582 | chan_err(d40c, "No memcpy\n"); |
8d318a50 LW |
1583 | return -EINVAL; |
1584 | } | |
1585 | ||
1586 | return 0; | |
1587 | } | |
1588 | ||
1589 | ||
1590 | static int d40_free_dma(struct d40_chan *d40c) | |
1591 | { | |
1592 | ||
1593 | int res = 0; | |
d181b3a8 | 1594 | u32 event; |
8d318a50 LW |
1595 | struct d40_phy_res *phy = d40c->phy_chan; |
1596 | bool is_src; | |
a8be8627 PF |
1597 | struct d40_desc *d; |
1598 | struct d40_desc *_d; | |
1599 | ||
8d318a50 LW |
1600 | |
1601 | /* Terminate all queued and active transfers */ | |
1602 | d40_term_all(d40c); | |
1603 | ||
a8be8627 PF |
1604 | /* Release client owned descriptors */ |
1605 | if (!list_empty(&d40c->client)) | |
1606 | list_for_each_entry_safe(d, _d, &d40c->client, node) { | |
b00f938c | 1607 | d40_pool_lli_free(d40c, d); |
a8be8627 | 1608 | d40_desc_remove(d); |
a8be8627 PF |
1609 | d40_desc_free(d40c, d); |
1610 | } | |
1611 | ||
8d318a50 | 1612 | if (phy == NULL) { |
6db5a8ba | 1613 | chan_err(d40c, "phy == null\n"); |
8d318a50 LW |
1614 | return -EINVAL; |
1615 | } | |
1616 | ||
1617 | if (phy->allocated_src == D40_ALLOC_FREE && | |
1618 | phy->allocated_dst == D40_ALLOC_FREE) { | |
6db5a8ba | 1619 | chan_err(d40c, "channel already free\n"); |
8d318a50 LW |
1620 | return -EINVAL; |
1621 | } | |
1622 | ||
8d318a50 LW |
1623 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || |
1624 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { | |
1625 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); | |
8d318a50 LW |
1626 | is_src = false; |
1627 | } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { | |
1628 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); | |
8d318a50 LW |
1629 | is_src = true; |
1630 | } else { | |
6db5a8ba | 1631 | chan_err(d40c, "Unknown direction\n"); |
8d318a50 LW |
1632 | return -EINVAL; |
1633 | } | |
1634 | ||
d181b3a8 JA |
1635 | res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); |
1636 | if (res) { | |
6db5a8ba | 1637 | chan_err(d40c, "suspend failed\n"); |
d181b3a8 JA |
1638 | return res; |
1639 | } | |
1640 | ||
724a8577 | 1641 | if (chan_is_logical(d40c)) { |
d181b3a8 | 1642 | /* Release logical channel, deactivate the event line */ |
8d318a50 | 1643 | |
d181b3a8 | 1644 | d40_config_set_event(d40c, false); |
8d318a50 LW |
1645 | d40c->base->lookup_log_chans[d40c->log_num] = NULL; |
1646 | ||
1647 | /* | |
1648 | * Check if there are more logical allocation | |
1649 | * on this phy channel. | |
1650 | */ | |
1651 | if (!d40_alloc_mask_free(phy, is_src, event)) { | |
1652 | /* Resume the other logical channels if any */ | |
1653 | if (d40_chan_has_events(d40c)) { | |
1654 | res = d40_channel_execute_command(d40c, | |
1655 | D40_DMA_RUN); | |
1656 | if (res) { | |
6db5a8ba RV |
1657 | chan_err(d40c, |
1658 | "Executing RUN command\n"); | |
8d318a50 LW |
1659 | return res; |
1660 | } | |
1661 | } | |
1662 | return 0; | |
1663 | } | |
d181b3a8 JA |
1664 | } else { |
1665 | (void) d40_alloc_mask_free(phy, is_src, 0); | |
1666 | } | |
8d318a50 LW |
1667 | |
1668 | /* Release physical channel */ | |
1669 | res = d40_channel_execute_command(d40c, D40_DMA_STOP); | |
1670 | if (res) { | |
6db5a8ba | 1671 | chan_err(d40c, "Failed to stop channel\n"); |
8d318a50 LW |
1672 | return res; |
1673 | } | |
1674 | d40c->phy_chan = NULL; | |
ce2ca125 | 1675 | d40c->configured = false; |
8d318a50 LW |
1676 | d40c->base->lookup_phy_chans[phy->num] = NULL; |
1677 | ||
1678 | return 0; | |
8d318a50 LW |
1679 | } |
1680 | ||
a5ebca47 JA |
1681 | static bool d40_is_paused(struct d40_chan *d40c) |
1682 | { | |
8ca84687 | 1683 | void __iomem *chanbase = chan_base(d40c); |
a5ebca47 JA |
1684 | bool is_paused = false; |
1685 | unsigned long flags; | |
1686 | void __iomem *active_reg; | |
1687 | u32 status; | |
1688 | u32 event; | |
a5ebca47 JA |
1689 | |
1690 | spin_lock_irqsave(&d40c->lock, flags); | |
1691 | ||
724a8577 | 1692 | if (chan_is_physical(d40c)) { |
a5ebca47 JA |
1693 | if (d40c->phy_chan->num % 2 == 0) |
1694 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; | |
1695 | else | |
1696 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; | |
1697 | ||
1698 | status = (readl(active_reg) & | |
1699 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> | |
1700 | D40_CHAN_POS(d40c->phy_chan->num); | |
1701 | if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) | |
1702 | is_paused = true; | |
1703 | ||
1704 | goto _exit; | |
1705 | } | |
1706 | ||
a5ebca47 | 1707 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || |
9dbfbd35 | 1708 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { |
a5ebca47 | 1709 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); |
8ca84687 | 1710 | status = readl(chanbase + D40_CHAN_REG_SDLNK); |
9dbfbd35 | 1711 | } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { |
a5ebca47 | 1712 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); |
8ca84687 | 1713 | status = readl(chanbase + D40_CHAN_REG_SSLNK); |
9dbfbd35 | 1714 | } else { |
6db5a8ba | 1715 | chan_err(d40c, "Unknown direction\n"); |
a5ebca47 JA |
1716 | goto _exit; |
1717 | } | |
9dbfbd35 | 1718 | |
a5ebca47 JA |
1719 | status = (status & D40_EVENTLINE_MASK(event)) >> |
1720 | D40_EVENTLINE_POS(event); | |
1721 | ||
1722 | if (status != D40_DMA_RUN) | |
1723 | is_paused = true; | |
a5ebca47 JA |
1724 | _exit: |
1725 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1726 | return is_paused; | |
1727 | ||
1728 | } | |
1729 | ||
1730 | ||
8d318a50 LW |
1731 | static u32 stedma40_residue(struct dma_chan *chan) |
1732 | { | |
1733 | struct d40_chan *d40c = | |
1734 | container_of(chan, struct d40_chan, chan); | |
1735 | u32 bytes_left; | |
1736 | unsigned long flags; | |
1737 | ||
1738 | spin_lock_irqsave(&d40c->lock, flags); | |
1739 | bytes_left = d40_residue(d40c); | |
1740 | spin_unlock_irqrestore(&d40c->lock, flags); | |
1741 | ||
1742 | return bytes_left; | |
1743 | } | |
1744 | ||
3e3a0763 RV |
1745 | static int |
1746 | d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc, | |
1747 | struct scatterlist *sg_src, struct scatterlist *sg_dst, | |
822c5676 RV |
1748 | unsigned int sg_len, dma_addr_t src_dev_addr, |
1749 | dma_addr_t dst_dev_addr) | |
3e3a0763 RV |
1750 | { |
1751 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; | |
1752 | struct stedma40_half_channel_info *src_info = &cfg->src_info; | |
1753 | struct stedma40_half_channel_info *dst_info = &cfg->dst_info; | |
5ed04b85 | 1754 | int ret; |
3e3a0763 | 1755 | |
5ed04b85 RV |
1756 | ret = d40_log_sg_to_lli(sg_src, sg_len, |
1757 | src_dev_addr, | |
1758 | desc->lli_log.src, | |
1759 | chan->log_def.lcsp1, | |
1760 | src_info->data_width, | |
1761 | dst_info->data_width); | |
1762 | ||
1763 | ret = d40_log_sg_to_lli(sg_dst, sg_len, | |
1764 | dst_dev_addr, | |
1765 | desc->lli_log.dst, | |
1766 | chan->log_def.lcsp3, | |
1767 | dst_info->data_width, | |
1768 | src_info->data_width); | |
1769 | ||
1770 | return ret < 0 ? ret : 0; | |
3e3a0763 RV |
1771 | } |
1772 | ||
1773 | static int | |
1774 | d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc, | |
1775 | struct scatterlist *sg_src, struct scatterlist *sg_dst, | |
822c5676 RV |
1776 | unsigned int sg_len, dma_addr_t src_dev_addr, |
1777 | dma_addr_t dst_dev_addr) | |
3e3a0763 | 1778 | { |
3e3a0763 RV |
1779 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; |
1780 | struct stedma40_half_channel_info *src_info = &cfg->src_info; | |
1781 | struct stedma40_half_channel_info *dst_info = &cfg->dst_info; | |
0c842b55 | 1782 | unsigned long flags = 0; |
3e3a0763 RV |
1783 | int ret; |
1784 | ||
0c842b55 RV |
1785 | if (desc->cyclic) |
1786 | flags |= LLI_CYCLIC | LLI_TERM_INT; | |
1787 | ||
3e3a0763 RV |
1788 | ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr, |
1789 | desc->lli_phy.src, | |
1790 | virt_to_phys(desc->lli_phy.src), | |
1791 | chan->src_def_cfg, | |
0c842b55 | 1792 | src_info, dst_info, flags); |
3e3a0763 RV |
1793 | |
1794 | ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr, | |
1795 | desc->lli_phy.dst, | |
1796 | virt_to_phys(desc->lli_phy.dst), | |
1797 | chan->dst_def_cfg, | |
0c842b55 | 1798 | dst_info, src_info, flags); |
3e3a0763 RV |
1799 | |
1800 | dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr, | |
1801 | desc->lli_pool.size, DMA_TO_DEVICE); | |
1802 | ||
1803 | return ret < 0 ? ret : 0; | |
1804 | } | |
1805 | ||
1806 | ||
5f81158f RV |
1807 | static struct d40_desc * |
1808 | d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg, | |
1809 | unsigned int sg_len, unsigned long dma_flags) | |
1810 | { | |
1811 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; | |
1812 | struct d40_desc *desc; | |
dbd88788 | 1813 | int ret; |
5f81158f RV |
1814 | |
1815 | desc = d40_desc_get(chan); | |
1816 | if (!desc) | |
1817 | return NULL; | |
1818 | ||
1819 | desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width, | |
1820 | cfg->dst_info.data_width); | |
1821 | if (desc->lli_len < 0) { | |
1822 | chan_err(chan, "Unaligned size\n"); | |
dbd88788 RV |
1823 | goto err; |
1824 | } | |
5f81158f | 1825 | |
dbd88788 RV |
1826 | ret = d40_pool_lli_alloc(chan, desc, desc->lli_len); |
1827 | if (ret < 0) { | |
1828 | chan_err(chan, "Could not allocate lli\n"); | |
1829 | goto err; | |
5f81158f RV |
1830 | } |
1831 | ||
dbd88788 | 1832 | |
5f81158f RV |
1833 | desc->lli_current = 0; |
1834 | desc->txd.flags = dma_flags; | |
1835 | desc->txd.tx_submit = d40_tx_submit; | |
1836 | ||
1837 | dma_async_tx_descriptor_init(&desc->txd, &chan->chan); | |
1838 | ||
1839 | return desc; | |
dbd88788 RV |
1840 | |
1841 | err: | |
1842 | d40_desc_free(chan, desc); | |
1843 | return NULL; | |
5f81158f RV |
1844 | } |
1845 | ||
cade1d30 RV |
1846 | static dma_addr_t |
1847 | d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction) | |
8d318a50 | 1848 | { |
cade1d30 RV |
1849 | struct stedma40_platform_data *plat = chan->base->plat_data; |
1850 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; | |
711b9cea | 1851 | dma_addr_t addr = 0; |
cade1d30 RV |
1852 | |
1853 | if (chan->runtime_addr) | |
1854 | return chan->runtime_addr; | |
1855 | ||
1856 | if (direction == DMA_FROM_DEVICE) | |
1857 | addr = plat->dev_rx[cfg->src_dev_type]; | |
1858 | else if (direction == DMA_TO_DEVICE) | |
1859 | addr = plat->dev_tx[cfg->dst_dev_type]; | |
1860 | ||
1861 | return addr; | |
1862 | } | |
1863 | ||
1864 | static struct dma_async_tx_descriptor * | |
1865 | d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src, | |
1866 | struct scatterlist *sg_dst, unsigned int sg_len, | |
1867 | enum dma_data_direction direction, unsigned long dma_flags) | |
1868 | { | |
1869 | struct d40_chan *chan = container_of(dchan, struct d40_chan, chan); | |
822c5676 RV |
1870 | dma_addr_t src_dev_addr = 0; |
1871 | dma_addr_t dst_dev_addr = 0; | |
cade1d30 | 1872 | struct d40_desc *desc; |
2a614340 | 1873 | unsigned long flags; |
cade1d30 | 1874 | int ret; |
8d318a50 | 1875 | |
cade1d30 RV |
1876 | if (!chan->phy_chan) { |
1877 | chan_err(chan, "Cannot prepare unallocated channel\n"); | |
1878 | return NULL; | |
0d0f6b8b JA |
1879 | } |
1880 | ||
0c842b55 | 1881 | |
cade1d30 | 1882 | spin_lock_irqsave(&chan->lock, flags); |
8d318a50 | 1883 | |
cade1d30 RV |
1884 | desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags); |
1885 | if (desc == NULL) | |
8d318a50 LW |
1886 | goto err; |
1887 | ||
0c842b55 RV |
1888 | if (sg_next(&sg_src[sg_len - 1]) == sg_src) |
1889 | desc->cyclic = true; | |
1890 | ||
822c5676 RV |
1891 | if (direction != DMA_NONE) { |
1892 | dma_addr_t dev_addr = d40_get_dev_addr(chan, direction); | |
1893 | ||
1894 | if (direction == DMA_FROM_DEVICE) | |
1895 | src_dev_addr = dev_addr; | |
1896 | else if (direction == DMA_TO_DEVICE) | |
1897 | dst_dev_addr = dev_addr; | |
1898 | } | |
cade1d30 RV |
1899 | |
1900 | if (chan_is_logical(chan)) | |
1901 | ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst, | |
822c5676 | 1902 | sg_len, src_dev_addr, dst_dev_addr); |
cade1d30 RV |
1903 | else |
1904 | ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst, | |
822c5676 | 1905 | sg_len, src_dev_addr, dst_dev_addr); |
cade1d30 RV |
1906 | |
1907 | if (ret) { | |
1908 | chan_err(chan, "Failed to prepare %s sg job: %d\n", | |
1909 | chan_is_logical(chan) ? "log" : "phy", ret); | |
1910 | goto err; | |
8d318a50 LW |
1911 | } |
1912 | ||
cade1d30 RV |
1913 | spin_unlock_irqrestore(&chan->lock, flags); |
1914 | ||
1915 | return &desc->txd; | |
8d318a50 | 1916 | |
8d318a50 | 1917 | err: |
cade1d30 RV |
1918 | if (desc) |
1919 | d40_desc_free(chan, desc); | |
1920 | spin_unlock_irqrestore(&chan->lock, flags); | |
8d318a50 LW |
1921 | return NULL; |
1922 | } | |
8d318a50 LW |
1923 | |
1924 | bool stedma40_filter(struct dma_chan *chan, void *data) | |
1925 | { | |
1926 | struct stedma40_chan_cfg *info = data; | |
1927 | struct d40_chan *d40c = | |
1928 | container_of(chan, struct d40_chan, chan); | |
1929 | int err; | |
1930 | ||
1931 | if (data) { | |
1932 | err = d40_validate_conf(d40c, info); | |
1933 | if (!err) | |
1934 | d40c->dma_cfg = *info; | |
1935 | } else | |
1936 | err = d40_config_memcpy(d40c); | |
1937 | ||
ce2ca125 RV |
1938 | if (!err) |
1939 | d40c->configured = true; | |
1940 | ||
8d318a50 LW |
1941 | return err == 0; |
1942 | } | |
1943 | EXPORT_SYMBOL(stedma40_filter); | |
1944 | ||
ac2c0a38 RV |
1945 | static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src) |
1946 | { | |
1947 | bool realtime = d40c->dma_cfg.realtime; | |
1948 | bool highprio = d40c->dma_cfg.high_priority; | |
1949 | u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1; | |
1950 | u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1; | |
1951 | u32 event = D40_TYPE_TO_EVENT(dev_type); | |
1952 | u32 group = D40_TYPE_TO_GROUP(dev_type); | |
1953 | u32 bit = 1 << event; | |
1954 | ||
1955 | /* Destination event lines are stored in the upper halfword */ | |
1956 | if (!src) | |
1957 | bit <<= 16; | |
1958 | ||
1959 | writel(bit, d40c->base->virtbase + prioreg + group * 4); | |
1960 | writel(bit, d40c->base->virtbase + rtreg + group * 4); | |
1961 | } | |
1962 | ||
1963 | static void d40_set_prio_realtime(struct d40_chan *d40c) | |
1964 | { | |
1965 | if (d40c->base->rev < 3) | |
1966 | return; | |
1967 | ||
1968 | if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || | |
1969 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) | |
1970 | __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true); | |
1971 | ||
1972 | if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) || | |
1973 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) | |
1974 | __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false); | |
1975 | } | |
1976 | ||
8d318a50 LW |
1977 | /* DMA ENGINE functions */ |
1978 | static int d40_alloc_chan_resources(struct dma_chan *chan) | |
1979 | { | |
1980 | int err; | |
1981 | unsigned long flags; | |
1982 | struct d40_chan *d40c = | |
1983 | container_of(chan, struct d40_chan, chan); | |
ef1872ec | 1984 | bool is_free_phy; |
8d318a50 LW |
1985 | spin_lock_irqsave(&d40c->lock, flags); |
1986 | ||
1987 | d40c->completed = chan->cookie = 1; | |
1988 | ||
ce2ca125 RV |
1989 | /* If no dma configuration is set use default configuration (memcpy) */ |
1990 | if (!d40c->configured) { | |
8d318a50 | 1991 | err = d40_config_memcpy(d40c); |
ff0b12ba | 1992 | if (err) { |
6db5a8ba | 1993 | chan_err(d40c, "Failed to configure memcpy channel\n"); |
ff0b12ba JA |
1994 | goto fail; |
1995 | } | |
8d318a50 | 1996 | } |
ef1872ec | 1997 | is_free_phy = (d40c->phy_chan == NULL); |
8d318a50 LW |
1998 | |
1999 | err = d40_allocate_channel(d40c); | |
2000 | if (err) { | |
6db5a8ba | 2001 | chan_err(d40c, "Failed to allocate channel\n"); |
ff0b12ba | 2002 | goto fail; |
8d318a50 LW |
2003 | } |
2004 | ||
ef1872ec LW |
2005 | /* Fill in basic CFG register values */ |
2006 | d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg, | |
724a8577 | 2007 | &d40c->dst_def_cfg, chan_is_logical(d40c)); |
ef1872ec | 2008 | |
ac2c0a38 RV |
2009 | d40_set_prio_realtime(d40c); |
2010 | ||
724a8577 | 2011 | if (chan_is_logical(d40c)) { |
ef1872ec LW |
2012 | d40_log_cfg(&d40c->dma_cfg, |
2013 | &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); | |
2014 | ||
2015 | if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) | |
2016 | d40c->lcpa = d40c->base->lcpa_base + | |
2017 | d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE; | |
2018 | else | |
2019 | d40c->lcpa = d40c->base->lcpa_base + | |
2020 | d40c->dma_cfg.dst_dev_type * | |
2021 | D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA; | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * Only write channel configuration to the DMA if the physical | |
2026 | * resource is free. In case of multiple logical channels | |
2027 | * on the same physical resource, only the first write is necessary. | |
2028 | */ | |
b55912c6 JA |
2029 | if (is_free_phy) |
2030 | d40_config_write(d40c); | |
ff0b12ba | 2031 | fail: |
8d318a50 | 2032 | spin_unlock_irqrestore(&d40c->lock, flags); |
ff0b12ba | 2033 | return err; |
8d318a50 LW |
2034 | } |
2035 | ||
2036 | static void d40_free_chan_resources(struct dma_chan *chan) | |
2037 | { | |
2038 | struct d40_chan *d40c = | |
2039 | container_of(chan, struct d40_chan, chan); | |
2040 | int err; | |
2041 | unsigned long flags; | |
2042 | ||
0d0f6b8b | 2043 | if (d40c->phy_chan == NULL) { |
6db5a8ba | 2044 | chan_err(d40c, "Cannot free unallocated channel\n"); |
0d0f6b8b JA |
2045 | return; |
2046 | } | |
2047 | ||
2048 | ||
8d318a50 LW |
2049 | spin_lock_irqsave(&d40c->lock, flags); |
2050 | ||
2051 | err = d40_free_dma(d40c); | |
2052 | ||
2053 | if (err) | |
6db5a8ba | 2054 | chan_err(d40c, "Failed to free channel\n"); |
8d318a50 LW |
2055 | spin_unlock_irqrestore(&d40c->lock, flags); |
2056 | } | |
2057 | ||
2058 | static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan, | |
2059 | dma_addr_t dst, | |
2060 | dma_addr_t src, | |
2061 | size_t size, | |
2a614340 | 2062 | unsigned long dma_flags) |
8d318a50 | 2063 | { |
95944c6e RV |
2064 | struct scatterlist dst_sg; |
2065 | struct scatterlist src_sg; | |
8d318a50 | 2066 | |
95944c6e RV |
2067 | sg_init_table(&dst_sg, 1); |
2068 | sg_init_table(&src_sg, 1); | |
8d318a50 | 2069 | |
95944c6e RV |
2070 | sg_dma_address(&dst_sg) = dst; |
2071 | sg_dma_address(&src_sg) = src; | |
8d318a50 | 2072 | |
95944c6e RV |
2073 | sg_dma_len(&dst_sg) = size; |
2074 | sg_dma_len(&src_sg) = size; | |
8d318a50 | 2075 | |
cade1d30 | 2076 | return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags); |
8d318a50 LW |
2077 | } |
2078 | ||
0d688662 | 2079 | static struct dma_async_tx_descriptor * |
cade1d30 RV |
2080 | d40_prep_memcpy_sg(struct dma_chan *chan, |
2081 | struct scatterlist *dst_sg, unsigned int dst_nents, | |
2082 | struct scatterlist *src_sg, unsigned int src_nents, | |
2083 | unsigned long dma_flags) | |
0d688662 IS |
2084 | { |
2085 | if (dst_nents != src_nents) | |
2086 | return NULL; | |
2087 | ||
cade1d30 | 2088 | return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags); |
00ac0341 RV |
2089 | } |
2090 | ||
8d318a50 LW |
2091 | static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan, |
2092 | struct scatterlist *sgl, | |
2093 | unsigned int sg_len, | |
2094 | enum dma_data_direction direction, | |
2a614340 | 2095 | unsigned long dma_flags) |
8d318a50 | 2096 | { |
00ac0341 RV |
2097 | if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) |
2098 | return NULL; | |
2099 | ||
cade1d30 | 2100 | return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags); |
8d318a50 LW |
2101 | } |
2102 | ||
0c842b55 RV |
2103 | static struct dma_async_tx_descriptor * |
2104 | dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr, | |
2105 | size_t buf_len, size_t period_len, | |
2106 | enum dma_data_direction direction) | |
2107 | { | |
2108 | unsigned int periods = buf_len / period_len; | |
2109 | struct dma_async_tx_descriptor *txd; | |
2110 | struct scatterlist *sg; | |
2111 | int i; | |
2112 | ||
79ca7ec3 | 2113 | sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT); |
0c842b55 RV |
2114 | for (i = 0; i < periods; i++) { |
2115 | sg_dma_address(&sg[i]) = dma_addr; | |
2116 | sg_dma_len(&sg[i]) = period_len; | |
2117 | dma_addr += period_len; | |
2118 | } | |
2119 | ||
2120 | sg[periods].offset = 0; | |
2121 | sg[periods].length = 0; | |
2122 | sg[periods].page_link = | |
2123 | ((unsigned long)sg | 0x01) & ~0x02; | |
2124 | ||
2125 | txd = d40_prep_sg(chan, sg, sg, periods, direction, | |
2126 | DMA_PREP_INTERRUPT); | |
2127 | ||
2128 | kfree(sg); | |
2129 | ||
2130 | return txd; | |
2131 | } | |
2132 | ||
8d318a50 LW |
2133 | static enum dma_status d40_tx_status(struct dma_chan *chan, |
2134 | dma_cookie_t cookie, | |
2135 | struct dma_tx_state *txstate) | |
2136 | { | |
2137 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); | |
2138 | dma_cookie_t last_used; | |
2139 | dma_cookie_t last_complete; | |
2140 | int ret; | |
2141 | ||
0d0f6b8b | 2142 | if (d40c->phy_chan == NULL) { |
6db5a8ba | 2143 | chan_err(d40c, "Cannot read status of unallocated channel\n"); |
0d0f6b8b JA |
2144 | return -EINVAL; |
2145 | } | |
2146 | ||
8d318a50 LW |
2147 | last_complete = d40c->completed; |
2148 | last_used = chan->cookie; | |
2149 | ||
a5ebca47 JA |
2150 | if (d40_is_paused(d40c)) |
2151 | ret = DMA_PAUSED; | |
2152 | else | |
2153 | ret = dma_async_is_complete(cookie, last_complete, last_used); | |
8d318a50 | 2154 | |
a5ebca47 JA |
2155 | dma_set_tx_state(txstate, last_complete, last_used, |
2156 | stedma40_residue(chan)); | |
8d318a50 LW |
2157 | |
2158 | return ret; | |
2159 | } | |
2160 | ||
2161 | static void d40_issue_pending(struct dma_chan *chan) | |
2162 | { | |
2163 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); | |
2164 | unsigned long flags; | |
2165 | ||
0d0f6b8b | 2166 | if (d40c->phy_chan == NULL) { |
6db5a8ba | 2167 | chan_err(d40c, "Channel is not allocated!\n"); |
0d0f6b8b JA |
2168 | return; |
2169 | } | |
2170 | ||
8d318a50 LW |
2171 | spin_lock_irqsave(&d40c->lock, flags); |
2172 | ||
a8f3067b PF |
2173 | list_splice_tail_init(&d40c->pending_queue, &d40c->queue); |
2174 | ||
2175 | /* Busy means that queued jobs are already being processed */ | |
8d318a50 LW |
2176 | if (!d40c->busy) |
2177 | (void) d40_queue_start(d40c); | |
2178 | ||
2179 | spin_unlock_irqrestore(&d40c->lock, flags); | |
2180 | } | |
2181 | ||
95e1400f LW |
2182 | /* Runtime reconfiguration extension */ |
2183 | static void d40_set_runtime_config(struct dma_chan *chan, | |
2184 | struct dma_slave_config *config) | |
2185 | { | |
2186 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); | |
2187 | struct stedma40_chan_cfg *cfg = &d40c->dma_cfg; | |
2188 | enum dma_slave_buswidth config_addr_width; | |
2189 | dma_addr_t config_addr; | |
2190 | u32 config_maxburst; | |
2191 | enum stedma40_periph_data_width addr_width; | |
2192 | int psize; | |
2193 | ||
2194 | if (config->direction == DMA_FROM_DEVICE) { | |
2195 | dma_addr_t dev_addr_rx = | |
2196 | d40c->base->plat_data->dev_rx[cfg->src_dev_type]; | |
2197 | ||
2198 | config_addr = config->src_addr; | |
2199 | if (dev_addr_rx) | |
2200 | dev_dbg(d40c->base->dev, | |
2201 | "channel has a pre-wired RX address %08x " | |
2202 | "overriding with %08x\n", | |
2203 | dev_addr_rx, config_addr); | |
2204 | if (cfg->dir != STEDMA40_PERIPH_TO_MEM) | |
2205 | dev_dbg(d40c->base->dev, | |
2206 | "channel was not configured for peripheral " | |
2207 | "to memory transfer (%d) overriding\n", | |
2208 | cfg->dir); | |
2209 | cfg->dir = STEDMA40_PERIPH_TO_MEM; | |
2210 | ||
2211 | config_addr_width = config->src_addr_width; | |
2212 | config_maxburst = config->src_maxburst; | |
2213 | ||
2214 | } else if (config->direction == DMA_TO_DEVICE) { | |
2215 | dma_addr_t dev_addr_tx = | |
2216 | d40c->base->plat_data->dev_tx[cfg->dst_dev_type]; | |
2217 | ||
2218 | config_addr = config->dst_addr; | |
2219 | if (dev_addr_tx) | |
2220 | dev_dbg(d40c->base->dev, | |
2221 | "channel has a pre-wired TX address %08x " | |
2222 | "overriding with %08x\n", | |
2223 | dev_addr_tx, config_addr); | |
2224 | if (cfg->dir != STEDMA40_MEM_TO_PERIPH) | |
2225 | dev_dbg(d40c->base->dev, | |
2226 | "channel was not configured for memory " | |
2227 | "to peripheral transfer (%d) overriding\n", | |
2228 | cfg->dir); | |
2229 | cfg->dir = STEDMA40_MEM_TO_PERIPH; | |
2230 | ||
2231 | config_addr_width = config->dst_addr_width; | |
2232 | config_maxburst = config->dst_maxburst; | |
2233 | ||
2234 | } else { | |
2235 | dev_err(d40c->base->dev, | |
2236 | "unrecognized channel direction %d\n", | |
2237 | config->direction); | |
2238 | return; | |
2239 | } | |
2240 | ||
2241 | switch (config_addr_width) { | |
2242 | case DMA_SLAVE_BUSWIDTH_1_BYTE: | |
2243 | addr_width = STEDMA40_BYTE_WIDTH; | |
2244 | break; | |
2245 | case DMA_SLAVE_BUSWIDTH_2_BYTES: | |
2246 | addr_width = STEDMA40_HALFWORD_WIDTH; | |
2247 | break; | |
2248 | case DMA_SLAVE_BUSWIDTH_4_BYTES: | |
2249 | addr_width = STEDMA40_WORD_WIDTH; | |
2250 | break; | |
2251 | case DMA_SLAVE_BUSWIDTH_8_BYTES: | |
2252 | addr_width = STEDMA40_DOUBLEWORD_WIDTH; | |
2253 | break; | |
2254 | default: | |
2255 | dev_err(d40c->base->dev, | |
2256 | "illegal peripheral address width " | |
2257 | "requested (%d)\n", | |
2258 | config->src_addr_width); | |
2259 | return; | |
2260 | } | |
2261 | ||
724a8577 | 2262 | if (chan_is_logical(d40c)) { |
a59670a4 PF |
2263 | if (config_maxburst >= 16) |
2264 | psize = STEDMA40_PSIZE_LOG_16; | |
2265 | else if (config_maxburst >= 8) | |
2266 | psize = STEDMA40_PSIZE_LOG_8; | |
2267 | else if (config_maxburst >= 4) | |
2268 | psize = STEDMA40_PSIZE_LOG_4; | |
2269 | else | |
2270 | psize = STEDMA40_PSIZE_LOG_1; | |
2271 | } else { | |
2272 | if (config_maxburst >= 16) | |
2273 | psize = STEDMA40_PSIZE_PHY_16; | |
2274 | else if (config_maxburst >= 8) | |
2275 | psize = STEDMA40_PSIZE_PHY_8; | |
2276 | else if (config_maxburst >= 4) | |
2277 | psize = STEDMA40_PSIZE_PHY_4; | |
d49278e3 PF |
2278 | else if (config_maxburst >= 2) |
2279 | psize = STEDMA40_PSIZE_PHY_2; | |
a59670a4 PF |
2280 | else |
2281 | psize = STEDMA40_PSIZE_PHY_1; | |
2282 | } | |
95e1400f LW |
2283 | |
2284 | /* Set up all the endpoint configs */ | |
2285 | cfg->src_info.data_width = addr_width; | |
2286 | cfg->src_info.psize = psize; | |
51f5d744 | 2287 | cfg->src_info.big_endian = false; |
95e1400f LW |
2288 | cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL; |
2289 | cfg->dst_info.data_width = addr_width; | |
2290 | cfg->dst_info.psize = psize; | |
51f5d744 | 2291 | cfg->dst_info.big_endian = false; |
95e1400f LW |
2292 | cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL; |
2293 | ||
a59670a4 | 2294 | /* Fill in register values */ |
724a8577 | 2295 | if (chan_is_logical(d40c)) |
a59670a4 PF |
2296 | d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); |
2297 | else | |
2298 | d40_phy_cfg(cfg, &d40c->src_def_cfg, | |
2299 | &d40c->dst_def_cfg, false); | |
2300 | ||
95e1400f LW |
2301 | /* These settings will take precedence later */ |
2302 | d40c->runtime_addr = config_addr; | |
2303 | d40c->runtime_direction = config->direction; | |
2304 | dev_dbg(d40c->base->dev, | |
2305 | "configured channel %s for %s, data width %d, " | |
2306 | "maxburst %d bytes, LE, no flow control\n", | |
2307 | dma_chan_name(chan), | |
2308 | (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX", | |
2309 | config_addr_width, | |
2310 | config_maxburst); | |
2311 | } | |
2312 | ||
05827630 LW |
2313 | static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
2314 | unsigned long arg) | |
8d318a50 | 2315 | { |
8d318a50 LW |
2316 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); |
2317 | ||
0d0f6b8b | 2318 | if (d40c->phy_chan == NULL) { |
6db5a8ba | 2319 | chan_err(d40c, "Channel is not allocated!\n"); |
0d0f6b8b JA |
2320 | return -EINVAL; |
2321 | } | |
2322 | ||
8d318a50 LW |
2323 | switch (cmd) { |
2324 | case DMA_TERMINATE_ALL: | |
86eb5fb6 | 2325 | return d40_terminate_all(d40c); |
8d318a50 | 2326 | case DMA_PAUSE: |
86eb5fb6 | 2327 | return d40_pause(d40c); |
8d318a50 | 2328 | case DMA_RESUME: |
86eb5fb6 | 2329 | return d40_resume(d40c); |
95e1400f LW |
2330 | case DMA_SLAVE_CONFIG: |
2331 | d40_set_runtime_config(chan, | |
2332 | (struct dma_slave_config *) arg); | |
2333 | return 0; | |
2334 | default: | |
2335 | break; | |
8d318a50 LW |
2336 | } |
2337 | ||
2338 | /* Other commands are unimplemented */ | |
2339 | return -ENXIO; | |
2340 | } | |
2341 | ||
2342 | /* Initialization functions */ | |
2343 | ||
2344 | static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma, | |
2345 | struct d40_chan *chans, int offset, | |
2346 | int num_chans) | |
2347 | { | |
2348 | int i = 0; | |
2349 | struct d40_chan *d40c; | |
2350 | ||
2351 | INIT_LIST_HEAD(&dma->channels); | |
2352 | ||
2353 | for (i = offset; i < offset + num_chans; i++) { | |
2354 | d40c = &chans[i]; | |
2355 | d40c->base = base; | |
2356 | d40c->chan.device = dma; | |
2357 | ||
8d318a50 LW |
2358 | spin_lock_init(&d40c->lock); |
2359 | ||
2360 | d40c->log_num = D40_PHY_CHAN; | |
2361 | ||
8d318a50 LW |
2362 | INIT_LIST_HEAD(&d40c->active); |
2363 | INIT_LIST_HEAD(&d40c->queue); | |
a8f3067b | 2364 | INIT_LIST_HEAD(&d40c->pending_queue); |
8d318a50 LW |
2365 | INIT_LIST_HEAD(&d40c->client); |
2366 | ||
8d318a50 LW |
2367 | tasklet_init(&d40c->tasklet, dma_tasklet, |
2368 | (unsigned long) d40c); | |
2369 | ||
2370 | list_add_tail(&d40c->chan.device_node, | |
2371 | &dma->channels); | |
2372 | } | |
2373 | } | |
2374 | ||
7ad74a7c RV |
2375 | static void d40_ops_init(struct d40_base *base, struct dma_device *dev) |
2376 | { | |
2377 | if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) | |
2378 | dev->device_prep_slave_sg = d40_prep_slave_sg; | |
2379 | ||
2380 | if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) { | |
2381 | dev->device_prep_dma_memcpy = d40_prep_memcpy; | |
2382 | ||
2383 | /* | |
2384 | * This controller can only access address at even | |
2385 | * 32bit boundaries, i.e. 2^2 | |
2386 | */ | |
2387 | dev->copy_align = 2; | |
2388 | } | |
2389 | ||
2390 | if (dma_has_cap(DMA_SG, dev->cap_mask)) | |
2391 | dev->device_prep_dma_sg = d40_prep_memcpy_sg; | |
2392 | ||
0c842b55 RV |
2393 | if (dma_has_cap(DMA_CYCLIC, dev->cap_mask)) |
2394 | dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic; | |
2395 | ||
7ad74a7c RV |
2396 | dev->device_alloc_chan_resources = d40_alloc_chan_resources; |
2397 | dev->device_free_chan_resources = d40_free_chan_resources; | |
2398 | dev->device_issue_pending = d40_issue_pending; | |
2399 | dev->device_tx_status = d40_tx_status; | |
2400 | dev->device_control = d40_control; | |
2401 | dev->dev = base->dev; | |
2402 | } | |
2403 | ||
8d318a50 LW |
2404 | static int __init d40_dmaengine_init(struct d40_base *base, |
2405 | int num_reserved_chans) | |
2406 | { | |
2407 | int err ; | |
2408 | ||
2409 | d40_chan_init(base, &base->dma_slave, base->log_chans, | |
2410 | 0, base->num_log_chans); | |
2411 | ||
2412 | dma_cap_zero(base->dma_slave.cap_mask); | |
2413 | dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask); | |
0c842b55 | 2414 | dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); |
8d318a50 | 2415 | |
7ad74a7c | 2416 | d40_ops_init(base, &base->dma_slave); |
8d318a50 LW |
2417 | |
2418 | err = dma_async_device_register(&base->dma_slave); | |
2419 | ||
2420 | if (err) { | |
6db5a8ba | 2421 | d40_err(base->dev, "Failed to register slave channels\n"); |
8d318a50 LW |
2422 | goto failure1; |
2423 | } | |
2424 | ||
2425 | d40_chan_init(base, &base->dma_memcpy, base->log_chans, | |
2426 | base->num_log_chans, base->plat_data->memcpy_len); | |
2427 | ||
2428 | dma_cap_zero(base->dma_memcpy.cap_mask); | |
2429 | dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask); | |
7ad74a7c RV |
2430 | dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask); |
2431 | ||
2432 | d40_ops_init(base, &base->dma_memcpy); | |
8d318a50 LW |
2433 | |
2434 | err = dma_async_device_register(&base->dma_memcpy); | |
2435 | ||
2436 | if (err) { | |
6db5a8ba RV |
2437 | d40_err(base->dev, |
2438 | "Failed to regsiter memcpy only channels\n"); | |
8d318a50 LW |
2439 | goto failure2; |
2440 | } | |
2441 | ||
2442 | d40_chan_init(base, &base->dma_both, base->phy_chans, | |
2443 | 0, num_reserved_chans); | |
2444 | ||
2445 | dma_cap_zero(base->dma_both.cap_mask); | |
2446 | dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask); | |
2447 | dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask); | |
7ad74a7c | 2448 | dma_cap_set(DMA_SG, base->dma_both.cap_mask); |
0c842b55 | 2449 | dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); |
7ad74a7c RV |
2450 | |
2451 | d40_ops_init(base, &base->dma_both); | |
8d318a50 LW |
2452 | err = dma_async_device_register(&base->dma_both); |
2453 | ||
2454 | if (err) { | |
6db5a8ba RV |
2455 | d40_err(base->dev, |
2456 | "Failed to register logical and physical capable channels\n"); | |
8d318a50 LW |
2457 | goto failure3; |
2458 | } | |
2459 | return 0; | |
2460 | failure3: | |
2461 | dma_async_device_unregister(&base->dma_memcpy); | |
2462 | failure2: | |
2463 | dma_async_device_unregister(&base->dma_slave); | |
2464 | failure1: | |
2465 | return err; | |
2466 | } | |
2467 | ||
2468 | /* Initialization functions. */ | |
2469 | ||
2470 | static int __init d40_phy_res_init(struct d40_base *base) | |
2471 | { | |
2472 | int i; | |
2473 | int num_phy_chans_avail = 0; | |
2474 | u32 val[2]; | |
2475 | int odd_even_bit = -2; | |
2476 | ||
2477 | val[0] = readl(base->virtbase + D40_DREG_PRSME); | |
2478 | val[1] = readl(base->virtbase + D40_DREG_PRSMO); | |
2479 | ||
2480 | for (i = 0; i < base->num_phy_chans; i++) { | |
2481 | base->phy_res[i].num = i; | |
2482 | odd_even_bit += 2 * ((i % 2) == 0); | |
2483 | if (((val[i % 2] >> odd_even_bit) & 3) == 1) { | |
2484 | /* Mark security only channels as occupied */ | |
2485 | base->phy_res[i].allocated_src = D40_ALLOC_PHY; | |
2486 | base->phy_res[i].allocated_dst = D40_ALLOC_PHY; | |
2487 | } else { | |
2488 | base->phy_res[i].allocated_src = D40_ALLOC_FREE; | |
2489 | base->phy_res[i].allocated_dst = D40_ALLOC_FREE; | |
2490 | num_phy_chans_avail++; | |
2491 | } | |
2492 | spin_lock_init(&base->phy_res[i].lock); | |
2493 | } | |
6b7acd84 JA |
2494 | |
2495 | /* Mark disabled channels as occupied */ | |
2496 | for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) { | |
f57b407c RV |
2497 | int chan = base->plat_data->disabled_channels[i]; |
2498 | ||
2499 | base->phy_res[chan].allocated_src = D40_ALLOC_PHY; | |
2500 | base->phy_res[chan].allocated_dst = D40_ALLOC_PHY; | |
2501 | num_phy_chans_avail--; | |
6b7acd84 JA |
2502 | } |
2503 | ||
8d318a50 LW |
2504 | dev_info(base->dev, "%d of %d physical DMA channels available\n", |
2505 | num_phy_chans_avail, base->num_phy_chans); | |
2506 | ||
2507 | /* Verify settings extended vs standard */ | |
2508 | val[0] = readl(base->virtbase + D40_DREG_PRTYP); | |
2509 | ||
2510 | for (i = 0; i < base->num_phy_chans; i++) { | |
2511 | ||
2512 | if (base->phy_res[i].allocated_src == D40_ALLOC_FREE && | |
2513 | (val[0] & 0x3) != 1) | |
2514 | dev_info(base->dev, | |
2515 | "[%s] INFO: channel %d is misconfigured (%d)\n", | |
2516 | __func__, i, val[0] & 0x3); | |
2517 | ||
2518 | val[0] = val[0] >> 2; | |
2519 | } | |
2520 | ||
2521 | return num_phy_chans_avail; | |
2522 | } | |
2523 | ||
2524 | static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev) | |
2525 | { | |
8d318a50 LW |
2526 | struct stedma40_platform_data *plat_data; |
2527 | struct clk *clk = NULL; | |
2528 | void __iomem *virtbase = NULL; | |
2529 | struct resource *res = NULL; | |
2530 | struct d40_base *base = NULL; | |
2531 | int num_log_chans = 0; | |
2532 | int num_phy_chans; | |
2533 | int i; | |
f4b89764 LW |
2534 | u32 pid; |
2535 | u32 cid; | |
2536 | u8 rev; | |
8d318a50 LW |
2537 | |
2538 | clk = clk_get(&pdev->dev, NULL); | |
2539 | ||
2540 | if (IS_ERR(clk)) { | |
6db5a8ba | 2541 | d40_err(&pdev->dev, "No matching clock found\n"); |
8d318a50 LW |
2542 | goto failure; |
2543 | } | |
2544 | ||
2545 | clk_enable(clk); | |
2546 | ||
2547 | /* Get IO for DMAC base address */ | |
2548 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base"); | |
2549 | if (!res) | |
2550 | goto failure; | |
2551 | ||
2552 | if (request_mem_region(res->start, resource_size(res), | |
2553 | D40_NAME " I/O base") == NULL) | |
2554 | goto failure; | |
2555 | ||
2556 | virtbase = ioremap(res->start, resource_size(res)); | |
2557 | if (!virtbase) | |
2558 | goto failure; | |
2559 | ||
f4b89764 LW |
2560 | /* This is just a regular AMBA PrimeCell ID actually */ |
2561 | for (pid = 0, i = 0; i < 4; i++) | |
2562 | pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i) | |
2563 | & 255) << (i * 8); | |
2564 | for (cid = 0, i = 0; i < 4; i++) | |
2565 | cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i) | |
2566 | & 255) << (i * 8); | |
8d318a50 | 2567 | |
f4b89764 LW |
2568 | if (cid != AMBA_CID) { |
2569 | d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n"); | |
2570 | goto failure; | |
2571 | } | |
2572 | if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) { | |
6db5a8ba | 2573 | d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n", |
f4b89764 LW |
2574 | AMBA_MANF_BITS(pid), |
2575 | AMBA_VENDOR_ST); | |
8d318a50 LW |
2576 | goto failure; |
2577 | } | |
f4b89764 LW |
2578 | /* |
2579 | * HW revision: | |
2580 | * DB8500ed has revision 0 | |
2581 | * ? has revision 1 | |
2582 | * DB8500v1 has revision 2 | |
2583 | * DB8500v2 has revision 3 | |
2584 | */ | |
2585 | rev = AMBA_REV_BITS(pid); | |
3ae0267f | 2586 | |
8d318a50 LW |
2587 | /* The number of physical channels on this HW */ |
2588 | num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4; | |
2589 | ||
2590 | dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n", | |
3ae0267f | 2591 | rev, res->start); |
8d318a50 LW |
2592 | |
2593 | plat_data = pdev->dev.platform_data; | |
2594 | ||
2595 | /* Count the number of logical channels in use */ | |
2596 | for (i = 0; i < plat_data->dev_len; i++) | |
2597 | if (plat_data->dev_rx[i] != 0) | |
2598 | num_log_chans++; | |
2599 | ||
2600 | for (i = 0; i < plat_data->dev_len; i++) | |
2601 | if (plat_data->dev_tx[i] != 0) | |
2602 | num_log_chans++; | |
2603 | ||
2604 | base = kzalloc(ALIGN(sizeof(struct d40_base), 4) + | |
2605 | (num_phy_chans + num_log_chans + plat_data->memcpy_len) * | |
2606 | sizeof(struct d40_chan), GFP_KERNEL); | |
2607 | ||
2608 | if (base == NULL) { | |
6db5a8ba | 2609 | d40_err(&pdev->dev, "Out of memory\n"); |
8d318a50 LW |
2610 | goto failure; |
2611 | } | |
2612 | ||
3ae0267f | 2613 | base->rev = rev; |
8d318a50 LW |
2614 | base->clk = clk; |
2615 | base->num_phy_chans = num_phy_chans; | |
2616 | base->num_log_chans = num_log_chans; | |
2617 | base->phy_start = res->start; | |
2618 | base->phy_size = resource_size(res); | |
2619 | base->virtbase = virtbase; | |
2620 | base->plat_data = plat_data; | |
2621 | base->dev = &pdev->dev; | |
2622 | base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4); | |
2623 | base->log_chans = &base->phy_chans[num_phy_chans]; | |
2624 | ||
2625 | base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res), | |
2626 | GFP_KERNEL); | |
2627 | if (!base->phy_res) | |
2628 | goto failure; | |
2629 | ||
2630 | base->lookup_phy_chans = kzalloc(num_phy_chans * | |
2631 | sizeof(struct d40_chan *), | |
2632 | GFP_KERNEL); | |
2633 | if (!base->lookup_phy_chans) | |
2634 | goto failure; | |
2635 | ||
2636 | if (num_log_chans + plat_data->memcpy_len) { | |
2637 | /* | |
2638 | * The max number of logical channels are event lines for all | |
2639 | * src devices and dst devices | |
2640 | */ | |
2641 | base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 * | |
2642 | sizeof(struct d40_chan *), | |
2643 | GFP_KERNEL); | |
2644 | if (!base->lookup_log_chans) | |
2645 | goto failure; | |
2646 | } | |
698e4732 JA |
2647 | |
2648 | base->lcla_pool.alloc_map = kzalloc(num_phy_chans * | |
2649 | sizeof(struct d40_desc *) * | |
2650 | D40_LCLA_LINK_PER_EVENT_GRP, | |
8d318a50 LW |
2651 | GFP_KERNEL); |
2652 | if (!base->lcla_pool.alloc_map) | |
2653 | goto failure; | |
2654 | ||
c675b1b4 JA |
2655 | base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc), |
2656 | 0, SLAB_HWCACHE_ALIGN, | |
2657 | NULL); | |
2658 | if (base->desc_slab == NULL) | |
2659 | goto failure; | |
2660 | ||
8d318a50 LW |
2661 | return base; |
2662 | ||
2663 | failure: | |
c6134c96 | 2664 | if (!IS_ERR(clk)) { |
8d318a50 LW |
2665 | clk_disable(clk); |
2666 | clk_put(clk); | |
2667 | } | |
2668 | if (virtbase) | |
2669 | iounmap(virtbase); | |
2670 | if (res) | |
2671 | release_mem_region(res->start, | |
2672 | resource_size(res)); | |
2673 | if (virtbase) | |
2674 | iounmap(virtbase); | |
2675 | ||
2676 | if (base) { | |
2677 | kfree(base->lcla_pool.alloc_map); | |
2678 | kfree(base->lookup_log_chans); | |
2679 | kfree(base->lookup_phy_chans); | |
2680 | kfree(base->phy_res); | |
2681 | kfree(base); | |
2682 | } | |
2683 | ||
2684 | return NULL; | |
2685 | } | |
2686 | ||
2687 | static void __init d40_hw_init(struct d40_base *base) | |
2688 | { | |
2689 | ||
2690 | static const struct d40_reg_val dma_init_reg[] = { | |
2691 | /* Clock every part of the DMA block from start */ | |
2692 | { .reg = D40_DREG_GCC, .val = 0x0000ff01}, | |
2693 | ||
2694 | /* Interrupts on all logical channels */ | |
2695 | { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF}, | |
2696 | { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF}, | |
2697 | { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF}, | |
2698 | { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF}, | |
2699 | { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF}, | |
2700 | { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF}, | |
2701 | { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF}, | |
2702 | { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF}, | |
2703 | { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF}, | |
2704 | { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF}, | |
2705 | { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF}, | |
2706 | { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF} | |
2707 | }; | |
2708 | int i; | |
2709 | u32 prmseo[2] = {0, 0}; | |
2710 | u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF}; | |
2711 | u32 pcmis = 0; | |
2712 | u32 pcicr = 0; | |
2713 | ||
2714 | for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++) | |
2715 | writel(dma_init_reg[i].val, | |
2716 | base->virtbase + dma_init_reg[i].reg); | |
2717 | ||
2718 | /* Configure all our dma channels to default settings */ | |
2719 | for (i = 0; i < base->num_phy_chans; i++) { | |
2720 | ||
2721 | activeo[i % 2] = activeo[i % 2] << 2; | |
2722 | ||
2723 | if (base->phy_res[base->num_phy_chans - i - 1].allocated_src | |
2724 | == D40_ALLOC_PHY) { | |
2725 | activeo[i % 2] |= 3; | |
2726 | continue; | |
2727 | } | |
2728 | ||
2729 | /* Enable interrupt # */ | |
2730 | pcmis = (pcmis << 1) | 1; | |
2731 | ||
2732 | /* Clear interrupt # */ | |
2733 | pcicr = (pcicr << 1) | 1; | |
2734 | ||
2735 | /* Set channel to physical mode */ | |
2736 | prmseo[i % 2] = prmseo[i % 2] << 2; | |
2737 | prmseo[i % 2] |= 1; | |
2738 | ||
2739 | } | |
2740 | ||
2741 | writel(prmseo[1], base->virtbase + D40_DREG_PRMSE); | |
2742 | writel(prmseo[0], base->virtbase + D40_DREG_PRMSO); | |
2743 | writel(activeo[1], base->virtbase + D40_DREG_ACTIVE); | |
2744 | writel(activeo[0], base->virtbase + D40_DREG_ACTIVO); | |
2745 | ||
2746 | /* Write which interrupt to enable */ | |
2747 | writel(pcmis, base->virtbase + D40_DREG_PCMIS); | |
2748 | ||
2749 | /* Write which interrupt to clear */ | |
2750 | writel(pcicr, base->virtbase + D40_DREG_PCICR); | |
2751 | ||
2752 | } | |
2753 | ||
508849ad LW |
2754 | static int __init d40_lcla_allocate(struct d40_base *base) |
2755 | { | |
026cbc42 | 2756 | struct d40_lcla_pool *pool = &base->lcla_pool; |
508849ad LW |
2757 | unsigned long *page_list; |
2758 | int i, j; | |
2759 | int ret = 0; | |
2760 | ||
2761 | /* | |
2762 | * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned, | |
2763 | * To full fill this hardware requirement without wasting 256 kb | |
2764 | * we allocate pages until we get an aligned one. | |
2765 | */ | |
2766 | page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS, | |
2767 | GFP_KERNEL); | |
2768 | ||
2769 | if (!page_list) { | |
2770 | ret = -ENOMEM; | |
2771 | goto failure; | |
2772 | } | |
2773 | ||
2774 | /* Calculating how many pages that are required */ | |
2775 | base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE; | |
2776 | ||
2777 | for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) { | |
2778 | page_list[i] = __get_free_pages(GFP_KERNEL, | |
2779 | base->lcla_pool.pages); | |
2780 | if (!page_list[i]) { | |
2781 | ||
6db5a8ba RV |
2782 | d40_err(base->dev, "Failed to allocate %d pages.\n", |
2783 | base->lcla_pool.pages); | |
508849ad LW |
2784 | |
2785 | for (j = 0; j < i; j++) | |
2786 | free_pages(page_list[j], base->lcla_pool.pages); | |
2787 | goto failure; | |
2788 | } | |
2789 | ||
2790 | if ((virt_to_phys((void *)page_list[i]) & | |
2791 | (LCLA_ALIGNMENT - 1)) == 0) | |
2792 | break; | |
2793 | } | |
2794 | ||
2795 | for (j = 0; j < i; j++) | |
2796 | free_pages(page_list[j], base->lcla_pool.pages); | |
2797 | ||
2798 | if (i < MAX_LCLA_ALLOC_ATTEMPTS) { | |
2799 | base->lcla_pool.base = (void *)page_list[i]; | |
2800 | } else { | |
767a9675 JA |
2801 | /* |
2802 | * After many attempts and no succees with finding the correct | |
2803 | * alignment, try with allocating a big buffer. | |
2804 | */ | |
508849ad LW |
2805 | dev_warn(base->dev, |
2806 | "[%s] Failed to get %d pages @ 18 bit align.\n", | |
2807 | __func__, base->lcla_pool.pages); | |
2808 | base->lcla_pool.base_unaligned = kmalloc(SZ_1K * | |
2809 | base->num_phy_chans + | |
2810 | LCLA_ALIGNMENT, | |
2811 | GFP_KERNEL); | |
2812 | if (!base->lcla_pool.base_unaligned) { | |
2813 | ret = -ENOMEM; | |
2814 | goto failure; | |
2815 | } | |
2816 | ||
2817 | base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned, | |
2818 | LCLA_ALIGNMENT); | |
2819 | } | |
2820 | ||
026cbc42 RV |
2821 | pool->dma_addr = dma_map_single(base->dev, pool->base, |
2822 | SZ_1K * base->num_phy_chans, | |
2823 | DMA_TO_DEVICE); | |
2824 | if (dma_mapping_error(base->dev, pool->dma_addr)) { | |
2825 | pool->dma_addr = 0; | |
2826 | ret = -ENOMEM; | |
2827 | goto failure; | |
2828 | } | |
2829 | ||
508849ad LW |
2830 | writel(virt_to_phys(base->lcla_pool.base), |
2831 | base->virtbase + D40_DREG_LCLA); | |
2832 | failure: | |
2833 | kfree(page_list); | |
2834 | return ret; | |
2835 | } | |
2836 | ||
8d318a50 LW |
2837 | static int __init d40_probe(struct platform_device *pdev) |
2838 | { | |
2839 | int err; | |
2840 | int ret = -ENOENT; | |
2841 | struct d40_base *base; | |
2842 | struct resource *res = NULL; | |
2843 | int num_reserved_chans; | |
2844 | u32 val; | |
2845 | ||
2846 | base = d40_hw_detect_init(pdev); | |
2847 | ||
2848 | if (!base) | |
2849 | goto failure; | |
2850 | ||
2851 | num_reserved_chans = d40_phy_res_init(base); | |
2852 | ||
2853 | platform_set_drvdata(pdev, base); | |
2854 | ||
2855 | spin_lock_init(&base->interrupt_lock); | |
2856 | spin_lock_init(&base->execmd_lock); | |
2857 | ||
2858 | /* Get IO for logical channel parameter address */ | |
2859 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa"); | |
2860 | if (!res) { | |
2861 | ret = -ENOENT; | |
6db5a8ba | 2862 | d40_err(&pdev->dev, "No \"lcpa\" memory resource\n"); |
8d318a50 LW |
2863 | goto failure; |
2864 | } | |
2865 | base->lcpa_size = resource_size(res); | |
2866 | base->phy_lcpa = res->start; | |
2867 | ||
2868 | if (request_mem_region(res->start, resource_size(res), | |
2869 | D40_NAME " I/O lcpa") == NULL) { | |
2870 | ret = -EBUSY; | |
6db5a8ba RV |
2871 | d40_err(&pdev->dev, |
2872 | "Failed to request LCPA region 0x%x-0x%x\n", | |
2873 | res->start, res->end); | |
8d318a50 LW |
2874 | goto failure; |
2875 | } | |
2876 | ||
2877 | /* We make use of ESRAM memory for this. */ | |
2878 | val = readl(base->virtbase + D40_DREG_LCPA); | |
2879 | if (res->start != val && val != 0) { | |
2880 | dev_warn(&pdev->dev, | |
2881 | "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n", | |
2882 | __func__, val, res->start); | |
2883 | } else | |
2884 | writel(res->start, base->virtbase + D40_DREG_LCPA); | |
2885 | ||
2886 | base->lcpa_base = ioremap(res->start, resource_size(res)); | |
2887 | if (!base->lcpa_base) { | |
2888 | ret = -ENOMEM; | |
6db5a8ba | 2889 | d40_err(&pdev->dev, "Failed to ioremap LCPA region\n"); |
8d318a50 LW |
2890 | goto failure; |
2891 | } | |
8d318a50 | 2892 | |
508849ad LW |
2893 | ret = d40_lcla_allocate(base); |
2894 | if (ret) { | |
6db5a8ba | 2895 | d40_err(&pdev->dev, "Failed to allocate LCLA area\n"); |
8d318a50 LW |
2896 | goto failure; |
2897 | } | |
2898 | ||
2899 | spin_lock_init(&base->lcla_pool.lock); | |
2900 | ||
8d318a50 LW |
2901 | base->irq = platform_get_irq(pdev, 0); |
2902 | ||
2903 | ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base); | |
8d318a50 | 2904 | if (ret) { |
6db5a8ba | 2905 | d40_err(&pdev->dev, "No IRQ defined\n"); |
8d318a50 LW |
2906 | goto failure; |
2907 | } | |
2908 | ||
2909 | err = d40_dmaengine_init(base, num_reserved_chans); | |
2910 | if (err) | |
2911 | goto failure; | |
2912 | ||
2913 | d40_hw_init(base); | |
2914 | ||
2915 | dev_info(base->dev, "initialized\n"); | |
2916 | return 0; | |
2917 | ||
2918 | failure: | |
2919 | if (base) { | |
c675b1b4 JA |
2920 | if (base->desc_slab) |
2921 | kmem_cache_destroy(base->desc_slab); | |
8d318a50 LW |
2922 | if (base->virtbase) |
2923 | iounmap(base->virtbase); | |
026cbc42 RV |
2924 | |
2925 | if (base->lcla_pool.dma_addr) | |
2926 | dma_unmap_single(base->dev, base->lcla_pool.dma_addr, | |
2927 | SZ_1K * base->num_phy_chans, | |
2928 | DMA_TO_DEVICE); | |
2929 | ||
508849ad LW |
2930 | if (!base->lcla_pool.base_unaligned && base->lcla_pool.base) |
2931 | free_pages((unsigned long)base->lcla_pool.base, | |
2932 | base->lcla_pool.pages); | |
767a9675 JA |
2933 | |
2934 | kfree(base->lcla_pool.base_unaligned); | |
2935 | ||
8d318a50 LW |
2936 | if (base->phy_lcpa) |
2937 | release_mem_region(base->phy_lcpa, | |
2938 | base->lcpa_size); | |
2939 | if (base->phy_start) | |
2940 | release_mem_region(base->phy_start, | |
2941 | base->phy_size); | |
2942 | if (base->clk) { | |
2943 | clk_disable(base->clk); | |
2944 | clk_put(base->clk); | |
2945 | } | |
2946 | ||
2947 | kfree(base->lcla_pool.alloc_map); | |
2948 | kfree(base->lookup_log_chans); | |
2949 | kfree(base->lookup_phy_chans); | |
2950 | kfree(base->phy_res); | |
2951 | kfree(base); | |
2952 | } | |
2953 | ||
6db5a8ba | 2954 | d40_err(&pdev->dev, "probe failed\n"); |
8d318a50 LW |
2955 | return ret; |
2956 | } | |
2957 | ||
2958 | static struct platform_driver d40_driver = { | |
2959 | .driver = { | |
2960 | .owner = THIS_MODULE, | |
2961 | .name = D40_NAME, | |
2962 | }, | |
2963 | }; | |
2964 | ||
cb9ab2d8 | 2965 | static int __init stedma40_init(void) |
8d318a50 LW |
2966 | { |
2967 | return platform_driver_probe(&d40_driver, d40_probe); | |
2968 | } | |
a0eb221a | 2969 | subsys_initcall(stedma40_init); |