blob: 13259cad0ceb61df6a593af758ebf1aec4ec877b [file] [log] [blame]
Linus Walleij8d318a52010-03-30 15:33:42 +02001/*
Per Forlind49278e2010-12-20 18:31:38 +01002 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
Per Forlin661385f2010-10-06 09:05:28 +00004 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
Jonas Aaberg767a9672010-08-09 12:08:34 +00005 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
Linus Walleij8d318a52010-03-30 15:33:42 +02006 * License terms: GNU General Public License (GPL) version 2
Linus Walleij8d318a52010-03-30 15:33:42 +02007 */
8
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +00009#include <linux/dma-mapping.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020010#include <linux/kernel.h>
11#include <linux/slab.h>
Paul Gortmakerf492b212011-07-31 16:17:36 -040012#include <linux/export.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020013#include <linux/dmaengine.h>
14#include <linux/platform_device.h>
15#include <linux/clk.h>
16#include <linux/delay.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000017#include <linux/err.h>
Linus Walleijf4b89762011-06-27 11:33:46 +020018#include <linux/amba/bus.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020019
20#include <plat/ste_dma40.h>
21
22#include "ste_dma40_ll.h"
23
24#define D40_NAME "dma40"
25
26#define D40_PHY_CHAN -1
27
28/* For masking out/in 2 bit channel positions */
29#define D40_CHAN_POS(chan) (2 * (chan / 2))
30#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
31
32/* Maximum iterations taken before giving up suspending a channel */
33#define D40_SUSPEND_MAX_IT 500
34
Linus Walleij508849a2010-06-20 21:26:07 +000035/* Hardware requirement on LCLA alignment */
36#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000037
38/* Max number of links per event group */
39#define D40_LCLA_LINK_PER_EVENT_GRP 128
40#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
41
Linus Walleij508849a2010-06-20 21:26:07 +000042/* Attempts before giving up to trying to get pages that are aligned */
43#define MAX_LCLA_ALLOC_ATTEMPTS 256
44
45/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020046#define D40_ALLOC_FREE (1 << 31)
47#define D40_ALLOC_PHY (1 << 30)
48#define D40_ALLOC_LOG_FREE 0
49
Linus Walleij8d318a52010-03-30 15:33:42 +020050/**
51 * enum 40_command - The different commands and/or statuses.
52 *
53 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
54 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
55 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
56 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
57 */
58enum d40_command {
59 D40_DMA_STOP = 0,
60 D40_DMA_RUN = 1,
61 D40_DMA_SUSPEND_REQ = 2,
62 D40_DMA_SUSPENDED = 3
63};
64
65/**
66 * struct d40_lli_pool - Structure for keeping LLIs in memory
67 *
68 * @base: Pointer to memory area when the pre_alloc_lli's are not large
69 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
70 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +010071 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +020072 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
73 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
74 * one buffer to one buffer.
75 */
76struct d40_lli_pool {
77 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000078 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +010079 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +020080 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000081 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020082};
83
84/**
85 * struct d40_desc - A descriptor is one DMA job.
86 *
87 * @lli_phy: LLI settings for physical channel. Both src and dst=
88 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
89 * lli_len equals one.
90 * @lli_log: Same as above but for logical channels.
91 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000092 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030093 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +000094 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020095 * @txd: DMA engine struct. Used for among other things for communication
96 * during a transfer.
97 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020098 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000099 * the previous one.
Linus Walleij8d318a52010-03-30 15:33:42 +0200100 *
101 * This descriptor is used for both logical and physical transfers.
102 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200103struct d40_desc {
104 /* LLI physical */
105 struct d40_phy_lli_bidir lli_phy;
106 /* LLI logical */
107 struct d40_log_lli_bidir lli_log;
108
109 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000110 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000111 int lli_current;
112 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200113
114 struct dma_async_tx_descriptor txd;
115 struct list_head node;
116
Linus Walleij8d318a52010-03-30 15:33:42 +0200117 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100118 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200119};
120
121/**
122 * struct d40_lcla_pool - LCLA pool settings and data.
123 *
Linus Walleij508849a2010-06-20 21:26:07 +0000124 * @base: The virtual address of LCLA. 18 bit aligned.
125 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
126 * This pointer is only there for clean-up on error.
127 * @pages: The number of pages needed for all physical channels.
128 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200129 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000130 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200131 */
132struct d40_lcla_pool {
133 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100134 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000135 void *base_unaligned;
136 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200137 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000138 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200139};
140
141/**
142 * struct d40_phy_res - struct for handling eventlines mapped to physical
143 * channels.
144 *
145 * @lock: A lock protection this entity.
146 * @num: The physical channel number of this entity.
147 * @allocated_src: Bit mapped to show which src event line's are mapped to
148 * this physical channel. Can also be free or physically allocated.
149 * @allocated_dst: Same as for src but is dst.
150 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000151 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200152 */
153struct d40_phy_res {
154 spinlock_t lock;
155 int num;
156 u32 allocated_src;
157 u32 allocated_dst;
158};
159
160struct d40_base;
161
162/**
163 * struct d40_chan - Struct that describes a channel.
164 *
165 * @lock: A spinlock to protect this struct.
166 * @log_num: The logical number, if any of this channel.
167 * @completed: Starts with 1, after first interrupt it is set to dma engine's
168 * current cookie.
169 * @pending_tx: The number of pending transfers. Used between interrupt handler
170 * and tasklet.
171 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000172 * @phy_chan: Pointer to physical channel which this instance runs on. If this
173 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200174 * @chan: DMA engine handle.
175 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
176 * transfer and call client callback.
177 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200178 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200179 * @active: Active descriptor.
180 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200181 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200182 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000183 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200184 * @base: Pointer to the device instance struct.
185 * @src_def_cfg: Default cfg register setting for src.
186 * @dst_def_cfg: Default cfg register setting for dst.
187 * @log_def: Default logical channel settings.
188 * @lcla: Space for one dst src pair for logical channel transfers.
189 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200190 * @runtime_addr: runtime configured address.
191 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200192 *
193 * This struct can either "be" a logical or a physical channel.
194 */
195struct d40_chan {
196 spinlock_t lock;
197 int log_num;
198 /* ID of the most recent completed transfer */
199 int completed;
200 int pending_tx;
201 bool busy;
202 struct d40_phy_res *phy_chan;
203 struct dma_chan chan;
204 struct tasklet_struct tasklet;
205 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200206 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200207 struct list_head active;
208 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200209 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200210 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000211 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200212 struct d40_base *base;
213 /* Default register configurations */
214 u32 src_def_cfg;
215 u32 dst_def_cfg;
216 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200217 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200218 /* Runtime reconfiguration */
219 dma_addr_t runtime_addr;
220 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200221};
222
223/**
224 * struct d40_base - The big global struct, one for each probe'd instance.
225 *
226 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
227 * @execmd_lock: Lock for execute command usage since several channels share
228 * the same physical register.
229 * @dev: The device structure.
230 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700231 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200232 * @clk: Pointer to the DMA clock structure.
233 * @phy_start: Physical memory start of the DMA registers.
234 * @phy_size: Size of the DMA register map.
235 * @irq: The IRQ number.
236 * @num_phy_chans: The number of physical channels. Read from HW. This
237 * is the number of available channels for this driver, not counting "Secure
238 * mode" allocated physical channels.
239 * @num_log_chans: The number of logical channels. Calculated from
240 * num_phy_chans.
241 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
242 * @dma_slave: dma_device channels that can do only do slave transfers.
243 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200244 * @log_chans: Room for all possible logical channels in system.
245 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
246 * to log_chans entries.
247 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
248 * to phy_chans entries.
249 * @plat_data: Pointer to provided platform_data which is the driver
250 * configuration.
251 * @phy_res: Vector containing all physical channels.
252 * @lcla_pool: lcla pool settings and data.
253 * @lcpa_base: The virtual mapped address of LCPA.
254 * @phy_lcpa: The physical address of the LCPA.
255 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000256 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200257 */
258struct d40_base {
259 spinlock_t interrupt_lock;
260 spinlock_t execmd_lock;
261 struct device *dev;
262 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700263 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200264 struct clk *clk;
265 phys_addr_t phy_start;
266 resource_size_t phy_size;
267 int irq;
268 int num_phy_chans;
269 int num_log_chans;
270 struct dma_device dma_both;
271 struct dma_device dma_slave;
272 struct dma_device dma_memcpy;
273 struct d40_chan *phy_chans;
274 struct d40_chan *log_chans;
275 struct d40_chan **lookup_log_chans;
276 struct d40_chan **lookup_phy_chans;
277 struct stedma40_platform_data *plat_data;
278 /* Physical half channels */
279 struct d40_phy_res *phy_res;
280 struct d40_lcla_pool lcla_pool;
281 void *lcpa_base;
282 dma_addr_t phy_lcpa;
283 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000284 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200285};
286
287/**
288 * struct d40_interrupt_lookup - lookup table for interrupt handler
289 *
290 * @src: Interrupt mask register.
291 * @clr: Interrupt clear register.
292 * @is_error: true if this is an error interrupt.
293 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
294 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
295 */
296struct d40_interrupt_lookup {
297 u32 src;
298 u32 clr;
299 bool is_error;
300 int offset;
301};
302
303/**
304 * struct d40_reg_val - simple lookup struct
305 *
306 * @reg: The register.
307 * @val: The value that belongs to the register in reg.
308 */
309struct d40_reg_val {
310 unsigned int reg;
311 unsigned int val;
312};
313
Rabin Vincent262d2912011-01-25 11:18:05 +0100314static struct device *chan2dev(struct d40_chan *d40c)
315{
316 return &d40c->chan.dev->device;
317}
318
Rabin Vincent724a8572011-01-25 11:18:08 +0100319static bool chan_is_physical(struct d40_chan *chan)
320{
321 return chan->log_num == D40_PHY_CHAN;
322}
323
324static bool chan_is_logical(struct d40_chan *chan)
325{
326 return !chan_is_physical(chan);
327}
328
Rabin Vincent8ca84682011-01-25 11:18:07 +0100329static void __iomem *chan_base(struct d40_chan *chan)
330{
331 return chan->base->virtbase + D40_DREG_PCBASE +
332 chan->phy_chan->num * D40_DREG_PCDELTA;
333}
334
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100335#define d40_err(dev, format, arg...) \
336 dev_err(dev, "[%s] " format, __func__, ## arg)
337
338#define chan_err(d40c, format, arg...) \
339 d40_err(chan2dev(d40c), format, ## arg)
340
Rabin Vincentb00f9382011-01-25 11:18:15 +0100341static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100342 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200343{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100344 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200345 u32 align;
346 void *base;
347
348 if (is_log)
349 align = sizeof(struct d40_log_lli);
350 else
351 align = sizeof(struct d40_phy_lli);
352
353 if (lli_len == 1) {
354 base = d40d->lli_pool.pre_alloc_lli;
355 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
356 d40d->lli_pool.base = NULL;
357 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100358 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200359
360 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
361 d40d->lli_pool.base = base;
362
363 if (d40d->lli_pool.base == NULL)
364 return -ENOMEM;
365 }
366
367 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100368 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100369 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100370
371 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200372 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100373 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100374 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100375
376 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
377 d40d->lli_phy.src,
378 d40d->lli_pool.size,
379 DMA_TO_DEVICE);
380
381 if (dma_mapping_error(d40c->base->dev,
382 d40d->lli_pool.dma_addr)) {
383 kfree(d40d->lli_pool.base);
384 d40d->lli_pool.base = NULL;
385 d40d->lli_pool.dma_addr = 0;
386 return -ENOMEM;
387 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200388 }
389
390 return 0;
391}
392
Rabin Vincentb00f9382011-01-25 11:18:15 +0100393static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200394{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100395 if (d40d->lli_pool.dma_addr)
396 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
397 d40d->lli_pool.size, DMA_TO_DEVICE);
398
Linus Walleij8d318a52010-03-30 15:33:42 +0200399 kfree(d40d->lli_pool.base);
400 d40d->lli_pool.base = NULL;
401 d40d->lli_pool.size = 0;
402 d40d->lli_log.src = NULL;
403 d40d->lli_log.dst = NULL;
404 d40d->lli_phy.src = NULL;
405 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200406}
407
Jonas Aaberg698e4732010-08-09 12:08:56 +0000408static int d40_lcla_alloc_one(struct d40_chan *d40c,
409 struct d40_desc *d40d)
410{
411 unsigned long flags;
412 int i;
413 int ret = -EINVAL;
414 int p;
415
416 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
417
418 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
419
420 /*
421 * Allocate both src and dst at the same time, therefore the half
422 * start on 1 since 0 can't be used since zero is used as end marker.
423 */
424 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
425 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
426 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
427 d40d->lcla_alloc++;
428 ret = i;
429 break;
430 }
431 }
432
433 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
434
435 return ret;
436}
437
438static int d40_lcla_free_all(struct d40_chan *d40c,
439 struct d40_desc *d40d)
440{
441 unsigned long flags;
442 int i;
443 int ret = -EINVAL;
444
Rabin Vincent724a8572011-01-25 11:18:08 +0100445 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000446 return 0;
447
448 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
449
450 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
451 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
452 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
453 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
454 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
455 d40d->lcla_alloc--;
456 if (d40d->lcla_alloc == 0) {
457 ret = 0;
458 break;
459 }
460 }
461 }
462
463 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
464
465 return ret;
466
467}
468
Linus Walleij8d318a52010-03-30 15:33:42 +0200469static void d40_desc_remove(struct d40_desc *d40d)
470{
471 list_del(&d40d->node);
472}
473
474static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
475{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000476 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200477
478 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000479 struct d40_desc *d;
480 struct d40_desc *_d;
481
Linus Walleij8d318a52010-03-30 15:33:42 +0200482 list_for_each_entry_safe(d, _d, &d40c->client, node)
483 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200484 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000485 desc = d;
486 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000487 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200488 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200489 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000490
491 if (!desc)
492 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
493
494 if (desc)
495 INIT_LIST_HEAD(&desc->node);
496
497 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200498}
499
500static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
501{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000502
Rabin Vincentb00f9382011-01-25 11:18:15 +0100503 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000504 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000505 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200506}
507
508static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
509{
510 list_add_tail(&desc->node, &d40c->active);
511}
512
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100513static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
514{
515 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
516 struct d40_phy_lli *lli_src = desc->lli_phy.src;
517 void __iomem *base = chan_base(chan);
518
519 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
520 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
521 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
522 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
523
524 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
525 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
526 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
527 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
528}
529
Rabin Vincente65889c2011-01-25 11:18:31 +0100530static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
531{
532 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
533 struct d40_log_lli_bidir *lli = &desc->lli_log;
534 int lli_current = desc->lli_current;
535 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100536 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100537 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100538 int first_lcla = 0;
539 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100540
Rabin Vincent0c842b52011-01-25 11:18:35 +0100541 /*
542 * We may have partially running cyclic transfers, in case we did't get
543 * enough LCLA entries.
544 */
545 linkback = cyclic && lli_current == 0;
546
547 /*
548 * For linkback, we need one LCLA even with only one link, because we
549 * can't link back to the one in LCPA space
550 */
551 if (linkback || (lli_len - lli_current > 1)) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100552 curr_lcla = d40_lcla_alloc_one(chan, desc);
Rabin Vincent0c842b52011-01-25 11:18:35 +0100553 first_lcla = curr_lcla;
554 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100555
Rabin Vincent0c842b52011-01-25 11:18:35 +0100556 /*
557 * For linkback, we normally load the LCPA in the loop since we need to
558 * link it to the second LCLA and not the first. However, if we
559 * couldn't even get a first LCLA, then we have to run in LCPA and
560 * reload manually.
561 */
562 if (!linkback || curr_lcla == -EINVAL) {
563 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100564
Rabin Vincent0c842b52011-01-25 11:18:35 +0100565 if (curr_lcla == -EINVAL)
566 flags |= LLI_TERM_INT;
567
568 d40_log_lli_lcpa_write(chan->lcpa,
569 &lli->dst[lli_current],
570 &lli->src[lli_current],
571 curr_lcla,
572 flags);
573 lli_current++;
574 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100575
576 if (curr_lcla < 0)
577 goto out;
578
Rabin Vincente65889c2011-01-25 11:18:31 +0100579 for (; lli_current < lli_len; lli_current++) {
580 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
581 8 * curr_lcla * 2;
582 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100583 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100584 int next_lcla;
585
586 if (lli_current + 1 < lli_len)
587 next_lcla = d40_lcla_alloc_one(chan, desc);
588 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100589 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100590
Rabin Vincent0c842b52011-01-25 11:18:35 +0100591 if (cyclic || next_lcla == -EINVAL)
592 flags |= LLI_TERM_INT;
593
594 if (linkback && curr_lcla == first_lcla) {
595 /* First link goes in both LCPA and LCLA */
596 d40_log_lli_lcpa_write(chan->lcpa,
597 &lli->dst[lli_current],
598 &lli->src[lli_current],
599 next_lcla, flags);
600 }
601
602 /*
603 * One unused LCLA in the cyclic case if the very first
604 * next_lcla fails...
605 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100606 d40_log_lli_lcla_write(lcla,
607 &lli->dst[lli_current],
608 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100609 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100610
611 dma_sync_single_range_for_device(chan->base->dev,
612 pool->dma_addr, lcla_offset,
613 2 * sizeof(struct d40_log_lli),
614 DMA_TO_DEVICE);
615
616 curr_lcla = next_lcla;
617
Rabin Vincent0c842b52011-01-25 11:18:35 +0100618 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100619 lli_current++;
620 break;
621 }
622 }
623
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100624out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100625 desc->lli_current = lli_current;
626}
627
Jonas Aaberg698e4732010-08-09 12:08:56 +0000628static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
629{
Rabin Vincent724a8572011-01-25 11:18:08 +0100630 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100631 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000632 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100633 } else
634 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000635}
636
Linus Walleij8d318a52010-03-30 15:33:42 +0200637static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
638{
639 struct d40_desc *d;
640
641 if (list_empty(&d40c->active))
642 return NULL;
643
644 d = list_first_entry(&d40c->active,
645 struct d40_desc,
646 node);
647 return d;
648}
649
Per Forlin74043682011-08-29 13:33:34 +0200650/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200651static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
652{
Per Forlin74043682011-08-29 13:33:34 +0200653 d40_desc_remove(desc);
654 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200655 list_add_tail(&desc->node, &d40c->pending_queue);
656}
657
658static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
659{
660 struct d40_desc *d;
661
662 if (list_empty(&d40c->pending_queue))
663 return NULL;
664
665 d = list_first_entry(&d40c->pending_queue,
666 struct d40_desc,
667 node);
668 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200669}
670
671static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
672{
673 struct d40_desc *d;
674
675 if (list_empty(&d40c->queue))
676 return NULL;
677
678 d = list_first_entry(&d40c->queue,
679 struct d40_desc,
680 node);
681 return d;
682}
683
Per Forlind49278e2010-12-20 18:31:38 +0100684static int d40_psize_2_burst_size(bool is_log, int psize)
685{
686 if (is_log) {
687 if (psize == STEDMA40_PSIZE_LOG_1)
688 return 1;
689 } else {
690 if (psize == STEDMA40_PSIZE_PHY_1)
691 return 1;
692 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200693
Per Forlind49278e2010-12-20 18:31:38 +0100694 return 2 << psize;
695}
696
697/*
698 * The dma only supports transmitting packages up to
699 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
700 * dma elements required to send the entire sg list
701 */
702static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
703{
704 int dmalen;
705 u32 max_w = max(data_width1, data_width2);
706 u32 min_w = min(data_width1, data_width2);
707 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
708
709 if (seg_max > STEDMA40_MAX_SEG_SIZE)
710 seg_max -= (1 << max_w);
711
712 if (!IS_ALIGNED(size, 1 << max_w))
713 return -EINVAL;
714
715 if (size <= seg_max)
716 dmalen = 1;
717 else {
718 dmalen = size / seg_max;
719 if (dmalen * seg_max < size)
720 dmalen++;
721 }
722 return dmalen;
723}
724
725static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
726 u32 data_width1, u32 data_width2)
727{
728 struct scatterlist *sg;
729 int i;
730 int len = 0;
731 int ret;
732
733 for_each_sg(sgl, sg, sg_len, i) {
734 ret = d40_size_2_dmalen(sg_dma_len(sg),
735 data_width1, data_width2);
736 if (ret < 0)
737 return ret;
738 len += ret;
739 }
740 return len;
741}
742
743/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200744
745static int d40_channel_execute_command(struct d40_chan *d40c,
746 enum d40_command command)
747{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000748 u32 status;
749 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200750 void __iomem *active_reg;
751 int ret = 0;
752 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000753 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200754
755 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
756
757 if (d40c->phy_chan->num % 2 == 0)
758 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
759 else
760 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
761
762 if (command == D40_DMA_SUSPEND_REQ) {
763 status = (readl(active_reg) &
764 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
765 D40_CHAN_POS(d40c->phy_chan->num);
766
767 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
768 goto done;
769 }
770
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000771 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
772 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
773 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200774
775 if (command == D40_DMA_SUSPEND_REQ) {
776
777 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
778 status = (readl(active_reg) &
779 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
780 D40_CHAN_POS(d40c->phy_chan->num);
781
782 cpu_relax();
783 /*
784 * Reduce the number of bus accesses while
785 * waiting for the DMA to suspend.
786 */
787 udelay(3);
788
789 if (status == D40_DMA_STOP ||
790 status == D40_DMA_SUSPENDED)
791 break;
792 }
793
794 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100795 chan_err(d40c,
796 "unable to suspend the chl %d (log: %d) status %x\n",
797 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200798 status);
799 dump_stack();
800 ret = -EBUSY;
801 }
802
803 }
804done:
805 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
806 return ret;
807}
808
809static void d40_term_all(struct d40_chan *d40c)
810{
811 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +0200812 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200813
814 /* Release active descriptors */
815 while ((d40d = d40_first_active_get(d40c))) {
816 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200817 d40_desc_free(d40c, d40d);
818 }
819
820 /* Release queued descriptors waiting for transfer */
821 while ((d40d = d40_first_queued(d40c))) {
822 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200823 d40_desc_free(d40c, d40d);
824 }
825
Per Forlina8f30672011-06-26 23:29:52 +0200826 /* Release pending descriptors */
827 while ((d40d = d40_first_pending(d40c))) {
828 d40_desc_remove(d40d);
829 d40_desc_free(d40c, d40d);
830 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200831
Per Forlin74043682011-08-29 13:33:34 +0200832 /* Release client owned descriptors */
833 if (!list_empty(&d40c->client))
834 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
835 d40_desc_remove(d40d);
836 d40_desc_free(d40c, d40d);
837 }
838
Per Forlin82babbb362011-08-29 13:33:35 +0200839 /* Release descriptors in prepare queue */
840 if (!list_empty(&d40c->prepare_queue))
841 list_for_each_entry_safe(d40d, _d,
842 &d40c->prepare_queue, node) {
843 d40_desc_remove(d40d);
844 d40_desc_free(d40c, d40d);
845 }
Per Forlin74043682011-08-29 13:33:34 +0200846
Linus Walleij8d318a52010-03-30 15:33:42 +0200847 d40c->pending_tx = 0;
848 d40c->busy = false;
849}
850
Rabin Vincent262d2912011-01-25 11:18:05 +0100851static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
852 u32 event, int reg)
853{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100854 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100855 int tries;
856
857 if (!enable) {
858 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
859 | ~D40_EVENTLINE_MASK(event), addr);
860 return;
861 }
862
863 /*
864 * The hardware sometimes doesn't register the enable when src and dst
865 * event lines are active on the same logical channel. Retry to ensure
866 * it does. Usually only one retry is sufficient.
867 */
868 tries = 100;
869 while (--tries) {
870 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
871 | ~D40_EVENTLINE_MASK(event), addr);
872
873 if (readl(addr) & D40_EVENTLINE_MASK(event))
874 break;
875 }
876
877 if (tries != 99)
878 dev_dbg(chan2dev(d40c),
879 "[%s] workaround enable S%cLNK (%d tries)\n",
880 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
881 100 - tries);
882
883 WARN_ON(!tries);
884}
885
Linus Walleij8d318a52010-03-30 15:33:42 +0200886static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
887{
Linus Walleij8d318a52010-03-30 15:33:42 +0200888 unsigned long flags;
889
Linus Walleij8d318a52010-03-30 15:33:42 +0200890 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
891
892 /* Enable event line connected to device (or memcpy) */
893 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
894 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
895 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
896
Rabin Vincent262d2912011-01-25 11:18:05 +0100897 __d40_config_set_event(d40c, do_enable, event,
898 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200899 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100900
Linus Walleij8d318a52010-03-30 15:33:42 +0200901 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
902 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
903
Rabin Vincent262d2912011-01-25 11:18:05 +0100904 __d40_config_set_event(d40c, do_enable, event,
905 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200906 }
907
908 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
909}
910
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200911static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200912{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100913 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000914 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200915
Rabin Vincent8ca84682011-01-25 11:18:07 +0100916 val = readl(chanbase + D40_CHAN_REG_SSLNK);
917 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200918
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200919 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200920}
921
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000922static u32 d40_get_prmo(struct d40_chan *d40c)
923{
924 static const unsigned int phy_map[] = {
925 [STEDMA40_PCHAN_BASIC_MODE]
926 = D40_DREG_PRMO_PCHAN_BASIC,
927 [STEDMA40_PCHAN_MODULO_MODE]
928 = D40_DREG_PRMO_PCHAN_MODULO,
929 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
930 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
931 };
932 static const unsigned int log_map[] = {
933 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
934 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
935 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
936 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
937 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
938 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
939 };
940
Rabin Vincent724a8572011-01-25 11:18:08 +0100941 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000942 return phy_map[d40c->dma_cfg.mode_opt];
943 else
944 return log_map[d40c->dma_cfg.mode_opt];
945}
946
Jonas Aabergb55912c2010-08-09 12:08:02 +0000947static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200948{
949 u32 addr_base;
950 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200951
952 /* Odd addresses are even addresses + 4 */
953 addr_base = (d40c->phy_chan->num % 2) * 4;
954 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100955 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200956 D40_CHAN_POS(d40c->phy_chan->num);
957 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
958
959 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000960 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200961
962 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
963
Rabin Vincent724a8572011-01-25 11:18:08 +0100964 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100965 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
966 & D40_SREG_ELEM_LOG_LIDX_MASK;
967 void __iomem *chanbase = chan_base(d40c);
968
Linus Walleij8d318a52010-03-30 15:33:42 +0200969 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100970 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
971 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200972
Jonas Aabergb55912c2010-08-09 12:08:02 +0000973 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100974 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
975 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200976 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200977}
978
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000979static u32 d40_residue(struct d40_chan *d40c)
980{
981 u32 num_elt;
982
Rabin Vincent724a8572011-01-25 11:18:08 +0100983 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000984 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
985 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100986 else {
987 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
988 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
989 >> D40_SREG_ELEM_PHY_ECNT_POS;
990 }
991
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000992 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
993}
994
995static bool d40_tx_is_linked(struct d40_chan *d40c)
996{
997 bool is_link;
998
Rabin Vincent724a8572011-01-25 11:18:08 +0100999 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001000 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1001 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001002 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1003 & D40_SREG_LNK_PHYS_LNK_MASK;
1004
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001005 return is_link;
1006}
1007
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001008static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001009{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001010 int res = 0;
1011 unsigned long flags;
1012
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001013 if (!d40c->busy)
1014 return 0;
1015
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001016 spin_lock_irqsave(&d40c->lock, flags);
1017
1018 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1019 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +01001020 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001021 d40_config_set_event(d40c, false);
1022 /* Resume the other logical channels if any */
1023 if (d40_chan_has_events(d40c))
1024 res = d40_channel_execute_command(d40c,
1025 D40_DMA_RUN);
1026 }
1027 }
1028
1029 spin_unlock_irqrestore(&d40c->lock, flags);
1030 return res;
1031}
1032
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001033static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001034{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001035 int res = 0;
1036 unsigned long flags;
1037
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001038 if (!d40c->busy)
1039 return 0;
1040
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001041 spin_lock_irqsave(&d40c->lock, flags);
1042
1043 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +01001044 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001045 res = d40_channel_execute_command(d40c,
1046 D40_DMA_SUSPEND_REQ);
1047 goto no_suspend;
1048 }
1049
1050 /* If bytes left to transfer or linked tx resume job */
1051 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
1052
Rabin Vincent724a8572011-01-25 11:18:08 +01001053 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001054 d40_config_set_event(d40c, true);
1055
1056 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1057 }
1058
1059no_suspend:
1060 spin_unlock_irqrestore(&d40c->lock, flags);
1061 return res;
1062}
1063
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001064static int d40_terminate_all(struct d40_chan *chan)
1065{
1066 unsigned long flags;
1067 int ret = 0;
1068
1069 ret = d40_pause(chan);
1070 if (!ret && chan_is_physical(chan))
1071 ret = d40_channel_execute_command(chan, D40_DMA_STOP);
1072
1073 spin_lock_irqsave(&chan->lock, flags);
1074 d40_term_all(chan);
1075 spin_unlock_irqrestore(&chan->lock, flags);
1076
1077 return ret;
1078}
1079
Linus Walleij8d318a52010-03-30 15:33:42 +02001080static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1081{
1082 struct d40_chan *d40c = container_of(tx->chan,
1083 struct d40_chan,
1084 chan);
1085 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1086 unsigned long flags;
1087
1088 spin_lock_irqsave(&d40c->lock, flags);
1089
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001090 d40c->chan.cookie++;
1091
1092 if (d40c->chan.cookie < 0)
1093 d40c->chan.cookie = 1;
1094
1095 d40d->txd.cookie = d40c->chan.cookie;
1096
Linus Walleij8d318a52010-03-30 15:33:42 +02001097 d40_desc_queue(d40c, d40d);
1098
1099 spin_unlock_irqrestore(&d40c->lock, flags);
1100
1101 return tx->cookie;
1102}
1103
1104static int d40_start(struct d40_chan *d40c)
1105{
Linus Walleijf4185592010-06-22 18:06:42 -07001106 if (d40c->base->rev == 0) {
1107 int err;
1108
Rabin Vincent724a8572011-01-25 11:18:08 +01001109 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -07001110 err = d40_channel_execute_command(d40c,
1111 D40_DMA_SUSPEND_REQ);
1112 if (err)
1113 return err;
1114 }
1115 }
1116
Rabin Vincent724a8572011-01-25 11:18:08 +01001117 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001118 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001119
Jonas Aaberg0c322692010-06-20 21:25:46 +00001120 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001121}
1122
1123static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1124{
1125 struct d40_desc *d40d;
1126 int err;
1127
1128 /* Start queued jobs, if any */
1129 d40d = d40_first_queued(d40c);
1130
1131 if (d40d != NULL) {
1132 d40c->busy = true;
1133
1134 /* Remove from queue */
1135 d40_desc_remove(d40d);
1136
1137 /* Add to active queue */
1138 d40_desc_submit(d40c, d40d);
1139
Rabin Vincent7d83a852011-01-25 11:18:06 +01001140 /* Initiate DMA job */
1141 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001142
Rabin Vincent7d83a852011-01-25 11:18:06 +01001143 /* Start dma job */
1144 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001145
Rabin Vincent7d83a852011-01-25 11:18:06 +01001146 if (err)
1147 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001148 }
1149
1150 return d40d;
1151}
1152
1153/* called from interrupt context */
1154static void dma_tc_handle(struct d40_chan *d40c)
1155{
1156 struct d40_desc *d40d;
1157
Linus Walleij8d318a52010-03-30 15:33:42 +02001158 /* Get first active entry from list */
1159 d40d = d40_first_active_get(d40c);
1160
1161 if (d40d == NULL)
1162 return;
1163
Rabin Vincent0c842b52011-01-25 11:18:35 +01001164 if (d40d->cyclic) {
1165 /*
1166 * If this was a paritially loaded list, we need to reloaded
1167 * it, and only when the list is completed. We need to check
1168 * for done because the interrupt will hit for every link, and
1169 * not just the last one.
1170 */
1171 if (d40d->lli_current < d40d->lli_len
1172 && !d40_tx_is_linked(d40c)
1173 && !d40_residue(d40c)) {
1174 d40_lcla_free_all(d40c, d40d);
1175 d40_desc_load(d40c, d40d);
1176 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001177
Rabin Vincent0c842b52011-01-25 11:18:35 +01001178 if (d40d->lli_current == d40d->lli_len)
1179 d40d->lli_current = 0;
1180 }
1181 } else {
1182 d40_lcla_free_all(d40c, d40d);
1183
1184 if (d40d->lli_current < d40d->lli_len) {
1185 d40_desc_load(d40c, d40d);
1186 /* Start dma job */
1187 (void) d40_start(d40c);
1188 return;
1189 }
1190
1191 if (d40_queue_start(d40c) == NULL)
1192 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001193 }
1194
Linus Walleij8d318a52010-03-30 15:33:42 +02001195 d40c->pending_tx++;
1196 tasklet_schedule(&d40c->tasklet);
1197
1198}
1199
1200static void dma_tasklet(unsigned long data)
1201{
1202 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001203 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001204 unsigned long flags;
1205 dma_async_tx_callback callback;
1206 void *callback_param;
1207
1208 spin_lock_irqsave(&d40c->lock, flags);
1209
1210 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001211 d40d = d40_first_active_get(d40c);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001212 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001213 goto err;
1214
Rabin Vincent0c842b52011-01-25 11:18:35 +01001215 if (!d40d->cyclic)
1216 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001217
1218 /*
1219 * If terminating a channel pending_tx is set to zero.
1220 * This prevents any finished active jobs to return to the client.
1221 */
1222 if (d40c->pending_tx == 0) {
1223 spin_unlock_irqrestore(&d40c->lock, flags);
1224 return;
1225 }
1226
1227 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001228 callback = d40d->txd.callback;
1229 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001230
Rabin Vincent0c842b52011-01-25 11:18:35 +01001231 if (!d40d->cyclic) {
1232 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001233 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001234 d40_desc_free(d40c, d40d);
1235 } else {
1236 if (!d40d->is_in_client_list) {
1237 d40_desc_remove(d40d);
1238 d40_lcla_free_all(d40c, d40d);
1239 list_add_tail(&d40d->node, &d40c->client);
1240 d40d->is_in_client_list = true;
1241 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001242 }
1243 }
1244
1245 d40c->pending_tx--;
1246
1247 if (d40c->pending_tx)
1248 tasklet_schedule(&d40c->tasklet);
1249
1250 spin_unlock_irqrestore(&d40c->lock, flags);
1251
Jonas Aaberg767a9672010-08-09 12:08:34 +00001252 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001253 callback(callback_param);
1254
1255 return;
1256
1257 err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001258 /* Rescue manoeuvre if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001259 if (d40c->pending_tx > 0)
1260 d40c->pending_tx--;
1261 spin_unlock_irqrestore(&d40c->lock, flags);
1262}
1263
1264static irqreturn_t d40_handle_interrupt(int irq, void *data)
1265{
1266 static const struct d40_interrupt_lookup il[] = {
1267 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1268 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1269 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1270 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1271 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1272 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1273 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1274 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1275 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1276 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1277 };
1278
1279 int i;
1280 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001281 u32 idx;
1282 u32 row;
1283 long chan = -1;
1284 struct d40_chan *d40c;
1285 unsigned long flags;
1286 struct d40_base *base = data;
1287
1288 spin_lock_irqsave(&base->interrupt_lock, flags);
1289
1290 /* Read interrupt status of both logical and physical channels */
1291 for (i = 0; i < ARRAY_SIZE(il); i++)
1292 regs[i] = readl(base->virtbase + il[i].src);
1293
1294 for (;;) {
1295
1296 chan = find_next_bit((unsigned long *)regs,
1297 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1298
1299 /* No more set bits found? */
1300 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1301 break;
1302
1303 row = chan / BITS_PER_LONG;
1304 idx = chan & (BITS_PER_LONG - 1);
1305
1306 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001307 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001308
1309 if (il[row].offset == D40_PHY_CHAN)
1310 d40c = base->lookup_phy_chans[idx];
1311 else
1312 d40c = base->lookup_log_chans[il[row].offset + idx];
1313 spin_lock(&d40c->lock);
1314
1315 if (!il[row].is_error)
1316 dma_tc_handle(d40c);
1317 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001318 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1319 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001320
1321 spin_unlock(&d40c->lock);
1322 }
1323
1324 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1325
1326 return IRQ_HANDLED;
1327}
1328
Linus Walleij8d318a52010-03-30 15:33:42 +02001329static int d40_validate_conf(struct d40_chan *d40c,
1330 struct stedma40_chan_cfg *conf)
1331{
1332 int res = 0;
1333 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1334 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001335 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001336
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001337 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001338 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001339 res = -EINVAL;
1340 }
1341
1342 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1343 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1344 d40c->runtime_addr == 0) {
1345
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001346 chan_err(d40c, "Invalid TX channel address (%d)\n",
1347 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001348 res = -EINVAL;
1349 }
1350
1351 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1352 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1353 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001354 chan_err(d40c, "Invalid RX channel address (%d)\n",
1355 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001356 res = -EINVAL;
1357 }
1358
1359 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001360 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001361 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001362 res = -EINVAL;
1363 }
1364
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001365 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001366 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001367 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001368 res = -EINVAL;
1369 }
1370
1371 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1372 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001373 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001374 res = -EINVAL;
1375 }
1376
1377 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1378 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001379 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001380 res = -EINVAL;
1381 }
1382
1383 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1384 /*
1385 * DMAC HW supports it. Will be added to this driver,
1386 * in case any dma client requires it.
1387 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001388 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001389 res = -EINVAL;
1390 }
1391
Per Forlind49278e2010-12-20 18:31:38 +01001392 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1393 (1 << conf->src_info.data_width) !=
1394 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1395 (1 << conf->dst_info.data_width)) {
1396 /*
1397 * The DMAC hardware only supports
1398 * src (burst x width) == dst (burst x width)
1399 */
1400
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001401 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001402 res = -EINVAL;
1403 }
1404
Linus Walleij8d318a52010-03-30 15:33:42 +02001405 return res;
1406}
1407
1408static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001409 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001410{
1411 unsigned long flags;
1412 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001413 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001414 /* Physical interrupts are masked per physical full channel */
1415 if (phy->allocated_src == D40_ALLOC_FREE &&
1416 phy->allocated_dst == D40_ALLOC_FREE) {
1417 phy->allocated_dst = D40_ALLOC_PHY;
1418 phy->allocated_src = D40_ALLOC_PHY;
1419 goto found;
1420 } else
1421 goto not_found;
1422 }
1423
1424 /* Logical channel */
1425 if (is_src) {
1426 if (phy->allocated_src == D40_ALLOC_PHY)
1427 goto not_found;
1428
1429 if (phy->allocated_src == D40_ALLOC_FREE)
1430 phy->allocated_src = D40_ALLOC_LOG_FREE;
1431
1432 if (!(phy->allocated_src & (1 << log_event_line))) {
1433 phy->allocated_src |= 1 << log_event_line;
1434 goto found;
1435 } else
1436 goto not_found;
1437 } else {
1438 if (phy->allocated_dst == D40_ALLOC_PHY)
1439 goto not_found;
1440
1441 if (phy->allocated_dst == D40_ALLOC_FREE)
1442 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1443
1444 if (!(phy->allocated_dst & (1 << log_event_line))) {
1445 phy->allocated_dst |= 1 << log_event_line;
1446 goto found;
1447 } else
1448 goto not_found;
1449 }
1450
1451not_found:
1452 spin_unlock_irqrestore(&phy->lock, flags);
1453 return false;
1454found:
1455 spin_unlock_irqrestore(&phy->lock, flags);
1456 return true;
1457}
1458
1459static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1460 int log_event_line)
1461{
1462 unsigned long flags;
1463 bool is_free = false;
1464
1465 spin_lock_irqsave(&phy->lock, flags);
1466 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001467 phy->allocated_dst = D40_ALLOC_FREE;
1468 phy->allocated_src = D40_ALLOC_FREE;
1469 is_free = true;
1470 goto out;
1471 }
1472
1473 /* Logical channel */
1474 if (is_src) {
1475 phy->allocated_src &= ~(1 << log_event_line);
1476 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1477 phy->allocated_src = D40_ALLOC_FREE;
1478 } else {
1479 phy->allocated_dst &= ~(1 << log_event_line);
1480 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1481 phy->allocated_dst = D40_ALLOC_FREE;
1482 }
1483
1484 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1485 D40_ALLOC_FREE);
1486
1487out:
1488 spin_unlock_irqrestore(&phy->lock, flags);
1489
1490 return is_free;
1491}
1492
1493static int d40_allocate_channel(struct d40_chan *d40c)
1494{
1495 int dev_type;
1496 int event_group;
1497 int event_line;
1498 struct d40_phy_res *phys;
1499 int i;
1500 int j;
1501 int log_num;
1502 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001503 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001504
1505 phys = d40c->base->phy_res;
1506
1507 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1508 dev_type = d40c->dma_cfg.src_dev_type;
1509 log_num = 2 * dev_type;
1510 is_src = true;
1511 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1512 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1513 /* dst event lines are used for logical memcpy */
1514 dev_type = d40c->dma_cfg.dst_dev_type;
1515 log_num = 2 * dev_type + 1;
1516 is_src = false;
1517 } else
1518 return -EINVAL;
1519
1520 event_group = D40_TYPE_TO_GROUP(dev_type);
1521 event_line = D40_TYPE_TO_EVENT(dev_type);
1522
1523 if (!is_log) {
1524 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1525 /* Find physical half channel */
1526 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1527
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001528 if (d40_alloc_mask_set(&phys[i], is_src,
1529 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001530 goto found_phy;
1531 }
1532 } else
1533 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1534 int phy_num = j + event_group * 2;
1535 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001536 if (d40_alloc_mask_set(&phys[i],
1537 is_src,
1538 0,
1539 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001540 goto found_phy;
1541 }
1542 }
1543 return -EINVAL;
1544found_phy:
1545 d40c->phy_chan = &phys[i];
1546 d40c->log_num = D40_PHY_CHAN;
1547 goto out;
1548 }
1549 if (dev_type == -1)
1550 return -EINVAL;
1551
1552 /* Find logical channel */
1553 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1554 int phy_num = j + event_group * 2;
1555 /*
1556 * Spread logical channels across all available physical rather
1557 * than pack every logical channel at the first available phy
1558 * channels.
1559 */
1560 if (is_src) {
1561 for (i = phy_num; i < phy_num + 2; i++) {
1562 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001563 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001564 goto found_log;
1565 }
1566 } else {
1567 for (i = phy_num + 1; i >= phy_num; i--) {
1568 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001569 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001570 goto found_log;
1571 }
1572 }
1573 }
1574 return -EINVAL;
1575
1576found_log:
1577 d40c->phy_chan = &phys[i];
1578 d40c->log_num = log_num;
1579out:
1580
1581 if (is_log)
1582 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1583 else
1584 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1585
1586 return 0;
1587
1588}
1589
Linus Walleij8d318a52010-03-30 15:33:42 +02001590static int d40_config_memcpy(struct d40_chan *d40c)
1591{
1592 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1593
1594 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1595 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1596 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1597 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1598 memcpy[d40c->chan.chan_id];
1599
1600 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1601 dma_has_cap(DMA_SLAVE, cap)) {
1602 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1603 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001604 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001605 return -EINVAL;
1606 }
1607
1608 return 0;
1609}
1610
1611
1612static int d40_free_dma(struct d40_chan *d40c)
1613{
1614
1615 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001616 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001617 struct d40_phy_res *phy = d40c->phy_chan;
1618 bool is_src;
1619
1620 /* Terminate all queued and active transfers */
1621 d40_term_all(d40c);
1622
1623 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001624 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001625 return -EINVAL;
1626 }
1627
1628 if (phy->allocated_src == D40_ALLOC_FREE &&
1629 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001630 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001631 return -EINVAL;
1632 }
1633
Linus Walleij8d318a52010-03-30 15:33:42 +02001634 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1635 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1636 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001637 is_src = false;
1638 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1639 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001640 is_src = true;
1641 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001642 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001643 return -EINVAL;
1644 }
1645
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001646 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1647 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001648 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001649 return res;
1650 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001651
Rabin Vincent724a8572011-01-25 11:18:08 +01001652 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001653 /* Release logical channel, deactivate the event line */
1654
1655 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001656 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1657
1658 /*
1659 * Check if there are more logical allocation
1660 * on this phy channel.
1661 */
1662 if (!d40_alloc_mask_free(phy, is_src, event)) {
1663 /* Resume the other logical channels if any */
1664 if (d40_chan_has_events(d40c)) {
1665 res = d40_channel_execute_command(d40c,
1666 D40_DMA_RUN);
1667 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001668 chan_err(d40c,
1669 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001670 return res;
1671 }
1672 }
1673 return 0;
1674 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001675 } else {
1676 (void) d40_alloc_mask_free(phy, is_src, 0);
1677 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001678
1679 /* Release physical channel */
1680 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1681 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001682 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001683 return res;
1684 }
1685 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001686 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001687 d40c->base->lookup_phy_chans[phy->num] = NULL;
1688
1689 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001690}
1691
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001692static bool d40_is_paused(struct d40_chan *d40c)
1693{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001694 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001695 bool is_paused = false;
1696 unsigned long flags;
1697 void __iomem *active_reg;
1698 u32 status;
1699 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001700
1701 spin_lock_irqsave(&d40c->lock, flags);
1702
Rabin Vincent724a8572011-01-25 11:18:08 +01001703 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001704 if (d40c->phy_chan->num % 2 == 0)
1705 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1706 else
1707 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1708
1709 status = (readl(active_reg) &
1710 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1711 D40_CHAN_POS(d40c->phy_chan->num);
1712 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1713 is_paused = true;
1714
1715 goto _exit;
1716 }
1717
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001718 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001719 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001720 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001721 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001722 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001723 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001724 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001725 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001726 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001727 goto _exit;
1728 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001729
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001730 status = (status & D40_EVENTLINE_MASK(event)) >>
1731 D40_EVENTLINE_POS(event);
1732
1733 if (status != D40_DMA_RUN)
1734 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001735_exit:
1736 spin_unlock_irqrestore(&d40c->lock, flags);
1737 return is_paused;
1738
1739}
1740
1741
Linus Walleij8d318a52010-03-30 15:33:42 +02001742static u32 stedma40_residue(struct dma_chan *chan)
1743{
1744 struct d40_chan *d40c =
1745 container_of(chan, struct d40_chan, chan);
1746 u32 bytes_left;
1747 unsigned long flags;
1748
1749 spin_lock_irqsave(&d40c->lock, flags);
1750 bytes_left = d40_residue(d40c);
1751 spin_unlock_irqrestore(&d40c->lock, flags);
1752
1753 return bytes_left;
1754}
1755
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001756static int
1757d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1758 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001759 unsigned int sg_len, dma_addr_t src_dev_addr,
1760 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001761{
1762 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1763 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1764 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001765 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001766
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001767 ret = d40_log_sg_to_lli(sg_src, sg_len,
1768 src_dev_addr,
1769 desc->lli_log.src,
1770 chan->log_def.lcsp1,
1771 src_info->data_width,
1772 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001773
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001774 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1775 dst_dev_addr,
1776 desc->lli_log.dst,
1777 chan->log_def.lcsp3,
1778 dst_info->data_width,
1779 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001780
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001781 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001782}
1783
1784static int
1785d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1786 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001787 unsigned int sg_len, dma_addr_t src_dev_addr,
1788 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001789{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001790 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1791 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1792 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01001793 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001794 int ret;
1795
Rabin Vincent0c842b52011-01-25 11:18:35 +01001796 if (desc->cyclic)
1797 flags |= LLI_CYCLIC | LLI_TERM_INT;
1798
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001799 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1800 desc->lli_phy.src,
1801 virt_to_phys(desc->lli_phy.src),
1802 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001803 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001804
1805 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1806 desc->lli_phy.dst,
1807 virt_to_phys(desc->lli_phy.dst),
1808 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001809 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001810
1811 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1812 desc->lli_pool.size, DMA_TO_DEVICE);
1813
1814 return ret < 0 ? ret : 0;
1815}
1816
1817
Rabin Vincent5f811582011-01-25 11:18:18 +01001818static struct d40_desc *
1819d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1820 unsigned int sg_len, unsigned long dma_flags)
1821{
1822 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1823 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001824 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01001825
1826 desc = d40_desc_get(chan);
1827 if (!desc)
1828 return NULL;
1829
1830 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1831 cfg->dst_info.data_width);
1832 if (desc->lli_len < 0) {
1833 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01001834 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01001835 }
1836
Rabin Vincentdbd88782011-01-25 11:18:19 +01001837 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1838 if (ret < 0) {
1839 chan_err(chan, "Could not allocate lli\n");
1840 goto err;
1841 }
1842
1843
Rabin Vincent5f811582011-01-25 11:18:18 +01001844 desc->lli_current = 0;
1845 desc->txd.flags = dma_flags;
1846 desc->txd.tx_submit = d40_tx_submit;
1847
1848 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1849
1850 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001851
1852err:
1853 d40_desc_free(chan, desc);
1854 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01001855}
1856
Rabin Vincentcade1d32011-01-25 11:18:23 +01001857static dma_addr_t
1858d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02001859{
Rabin Vincentcade1d32011-01-25 11:18:23 +01001860 struct stedma40_platform_data *plat = chan->base->plat_data;
1861 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02001862 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001863
Rabin Vincentcade1d32011-01-25 11:18:23 +01001864 if (chan->runtime_addr)
1865 return chan->runtime_addr;
1866
1867 if (direction == DMA_FROM_DEVICE)
1868 addr = plat->dev_rx[cfg->src_dev_type];
1869 else if (direction == DMA_TO_DEVICE)
1870 addr = plat->dev_tx[cfg->dst_dev_type];
1871
1872 return addr;
1873}
1874
1875static struct dma_async_tx_descriptor *
1876d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
1877 struct scatterlist *sg_dst, unsigned int sg_len,
1878 enum dma_data_direction direction, unsigned long dma_flags)
1879{
1880 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01001881 dma_addr_t src_dev_addr = 0;
1882 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01001883 struct d40_desc *desc;
1884 unsigned long flags;
1885 int ret;
1886
1887 if (!chan->phy_chan) {
1888 chan_err(chan, "Cannot prepare unallocated channel\n");
1889 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001890 }
1891
Rabin Vincent0c842b52011-01-25 11:18:35 +01001892
Rabin Vincentcade1d32011-01-25 11:18:23 +01001893 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001894
Rabin Vincentcade1d32011-01-25 11:18:23 +01001895 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
1896 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001897 goto err;
1898
Rabin Vincent0c842b52011-01-25 11:18:35 +01001899 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
1900 desc->cyclic = true;
1901
Rabin Vincent822c5672011-01-25 11:18:28 +01001902 if (direction != DMA_NONE) {
1903 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
1904
1905 if (direction == DMA_FROM_DEVICE)
1906 src_dev_addr = dev_addr;
1907 else if (direction == DMA_TO_DEVICE)
1908 dst_dev_addr = dev_addr;
1909 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01001910
1911 if (chan_is_logical(chan))
1912 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001913 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001914 else
1915 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001916 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001917
1918 if (ret) {
1919 chan_err(chan, "Failed to prepare %s sg job: %d\n",
1920 chan_is_logical(chan) ? "log" : "phy", ret);
1921 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001922 }
1923
Per Forlin82babbb362011-08-29 13:33:35 +02001924 /*
1925 * add descriptor to the prepare queue in order to be able
1926 * to free them later in terminate_all
1927 */
1928 list_add_tail(&desc->node, &chan->prepare_queue);
1929
Rabin Vincentcade1d32011-01-25 11:18:23 +01001930 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001931
Rabin Vincentcade1d32011-01-25 11:18:23 +01001932 return &desc->txd;
1933
Linus Walleij8d318a52010-03-30 15:33:42 +02001934err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01001935 if (desc)
1936 d40_desc_free(chan, desc);
1937 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001938 return NULL;
1939}
Linus Walleij8d318a52010-03-30 15:33:42 +02001940
1941bool stedma40_filter(struct dma_chan *chan, void *data)
1942{
1943 struct stedma40_chan_cfg *info = data;
1944 struct d40_chan *d40c =
1945 container_of(chan, struct d40_chan, chan);
1946 int err;
1947
1948 if (data) {
1949 err = d40_validate_conf(d40c, info);
1950 if (!err)
1951 d40c->dma_cfg = *info;
1952 } else
1953 err = d40_config_memcpy(d40c);
1954
Rabin Vincentce2ca122010-10-12 13:00:49 +00001955 if (!err)
1956 d40c->configured = true;
1957
Linus Walleij8d318a52010-03-30 15:33:42 +02001958 return err == 0;
1959}
1960EXPORT_SYMBOL(stedma40_filter);
1961
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001962static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1963{
1964 bool realtime = d40c->dma_cfg.realtime;
1965 bool highprio = d40c->dma_cfg.high_priority;
1966 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1967 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1968 u32 event = D40_TYPE_TO_EVENT(dev_type);
1969 u32 group = D40_TYPE_TO_GROUP(dev_type);
1970 u32 bit = 1 << event;
1971
1972 /* Destination event lines are stored in the upper halfword */
1973 if (!src)
1974 bit <<= 16;
1975
1976 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1977 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1978}
1979
1980static void d40_set_prio_realtime(struct d40_chan *d40c)
1981{
1982 if (d40c->base->rev < 3)
1983 return;
1984
1985 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1986 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1987 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1988
1989 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1990 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1991 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1992}
1993
Linus Walleij8d318a52010-03-30 15:33:42 +02001994/* DMA ENGINE functions */
1995static int d40_alloc_chan_resources(struct dma_chan *chan)
1996{
1997 int err;
1998 unsigned long flags;
1999 struct d40_chan *d40c =
2000 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002001 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002002 spin_lock_irqsave(&d40c->lock, flags);
2003
2004 d40c->completed = chan->cookie = 1;
2005
Rabin Vincentce2ca122010-10-12 13:00:49 +00002006 /* If no dma configuration is set use default configuration (memcpy) */
2007 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002008 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002009 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002010 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002011 goto fail;
2012 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002013 }
Linus Walleijef1872e2010-06-20 21:24:52 +00002014 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02002015
2016 err = d40_allocate_channel(d40c);
2017 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002018 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002019 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002020 }
2021
Linus Walleijef1872e2010-06-20 21:24:52 +00002022 /* Fill in basic CFG register values */
2023 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002024 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002025
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002026 d40_set_prio_realtime(d40c);
2027
Rabin Vincent724a8572011-01-25 11:18:08 +01002028 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002029 d40_log_cfg(&d40c->dma_cfg,
2030 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2031
2032 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2033 d40c->lcpa = d40c->base->lcpa_base +
2034 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2035 else
2036 d40c->lcpa = d40c->base->lcpa_base +
2037 d40c->dma_cfg.dst_dev_type *
2038 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2039 }
2040
2041 /*
2042 * Only write channel configuration to the DMA if the physical
2043 * resource is free. In case of multiple logical channels
2044 * on the same physical resource, only the first write is necessary.
2045 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002046 if (is_free_phy)
2047 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002048fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02002049 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002050 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002051}
2052
2053static void d40_free_chan_resources(struct dma_chan *chan)
2054{
2055 struct d40_chan *d40c =
2056 container_of(chan, struct d40_chan, chan);
2057 int err;
2058 unsigned long flags;
2059
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002060 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002061 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002062 return;
2063 }
2064
2065
Linus Walleij8d318a52010-03-30 15:33:42 +02002066 spin_lock_irqsave(&d40c->lock, flags);
2067
2068 err = d40_free_dma(d40c);
2069
2070 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002071 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002072 spin_unlock_irqrestore(&d40c->lock, flags);
2073}
2074
2075static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2076 dma_addr_t dst,
2077 dma_addr_t src,
2078 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002079 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002080{
Rabin Vincent95944c62011-01-25 11:18:17 +01002081 struct scatterlist dst_sg;
2082 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002083
Rabin Vincent95944c62011-01-25 11:18:17 +01002084 sg_init_table(&dst_sg, 1);
2085 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002086
Rabin Vincent95944c62011-01-25 11:18:17 +01002087 sg_dma_address(&dst_sg) = dst;
2088 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002089
Rabin Vincent95944c62011-01-25 11:18:17 +01002090 sg_dma_len(&dst_sg) = size;
2091 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002092
Rabin Vincentcade1d32011-01-25 11:18:23 +01002093 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002094}
2095
Ira Snyder0d688662010-09-30 11:46:47 +00002096static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002097d40_prep_memcpy_sg(struct dma_chan *chan,
2098 struct scatterlist *dst_sg, unsigned int dst_nents,
2099 struct scatterlist *src_sg, unsigned int src_nents,
2100 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002101{
2102 if (dst_nents != src_nents)
2103 return NULL;
2104
Rabin Vincentcade1d32011-01-25 11:18:23 +01002105 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002106}
2107
Linus Walleij8d318a52010-03-30 15:33:42 +02002108static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2109 struct scatterlist *sgl,
2110 unsigned int sg_len,
2111 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002112 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002113{
Rabin Vincent00ac0342011-01-25 11:18:20 +01002114 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
2115 return NULL;
2116
Rabin Vincentcade1d32011-01-25 11:18:23 +01002117 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002118}
2119
Rabin Vincent0c842b52011-01-25 11:18:35 +01002120static struct dma_async_tx_descriptor *
2121dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2122 size_t buf_len, size_t period_len,
2123 enum dma_data_direction direction)
2124{
2125 unsigned int periods = buf_len / period_len;
2126 struct dma_async_tx_descriptor *txd;
2127 struct scatterlist *sg;
2128 int i;
2129
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002130 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002131 for (i = 0; i < periods; i++) {
2132 sg_dma_address(&sg[i]) = dma_addr;
2133 sg_dma_len(&sg[i]) = period_len;
2134 dma_addr += period_len;
2135 }
2136
2137 sg[periods].offset = 0;
2138 sg[periods].length = 0;
2139 sg[periods].page_link =
2140 ((unsigned long)sg | 0x01) & ~0x02;
2141
2142 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2143 DMA_PREP_INTERRUPT);
2144
2145 kfree(sg);
2146
2147 return txd;
2148}
2149
Linus Walleij8d318a52010-03-30 15:33:42 +02002150static enum dma_status d40_tx_status(struct dma_chan *chan,
2151 dma_cookie_t cookie,
2152 struct dma_tx_state *txstate)
2153{
2154 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2155 dma_cookie_t last_used;
2156 dma_cookie_t last_complete;
2157 int ret;
2158
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002159 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002160 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002161 return -EINVAL;
2162 }
2163
Linus Walleij8d318a52010-03-30 15:33:42 +02002164 last_complete = d40c->completed;
2165 last_used = chan->cookie;
2166
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002167 if (d40_is_paused(d40c))
2168 ret = DMA_PAUSED;
2169 else
2170 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002171
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002172 dma_set_tx_state(txstate, last_complete, last_used,
2173 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002174
2175 return ret;
2176}
2177
2178static void d40_issue_pending(struct dma_chan *chan)
2179{
2180 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2181 unsigned long flags;
2182
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002183 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002184 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002185 return;
2186 }
2187
Linus Walleij8d318a52010-03-30 15:33:42 +02002188 spin_lock_irqsave(&d40c->lock, flags);
2189
Per Forlina8f30672011-06-26 23:29:52 +02002190 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2191
2192 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002193 if (!d40c->busy)
2194 (void) d40_queue_start(d40c);
2195
2196 spin_unlock_irqrestore(&d40c->lock, flags);
2197}
2198
Rabin Vincent98ca5282011-06-27 11:33:38 +02002199static int
2200dma40_config_to_halfchannel(struct d40_chan *d40c,
2201 struct stedma40_half_channel_info *info,
2202 enum dma_slave_buswidth width,
2203 u32 maxburst)
2204{
2205 enum stedma40_periph_data_width addr_width;
2206 int psize;
2207
2208 switch (width) {
2209 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2210 addr_width = STEDMA40_BYTE_WIDTH;
2211 break;
2212 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2213 addr_width = STEDMA40_HALFWORD_WIDTH;
2214 break;
2215 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2216 addr_width = STEDMA40_WORD_WIDTH;
2217 break;
2218 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2219 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2220 break;
2221 default:
2222 dev_err(d40c->base->dev,
2223 "illegal peripheral address width "
2224 "requested (%d)\n",
2225 width);
2226 return -EINVAL;
2227 }
2228
2229 if (chan_is_logical(d40c)) {
2230 if (maxburst >= 16)
2231 psize = STEDMA40_PSIZE_LOG_16;
2232 else if (maxburst >= 8)
2233 psize = STEDMA40_PSIZE_LOG_8;
2234 else if (maxburst >= 4)
2235 psize = STEDMA40_PSIZE_LOG_4;
2236 else
2237 psize = STEDMA40_PSIZE_LOG_1;
2238 } else {
2239 if (maxburst >= 16)
2240 psize = STEDMA40_PSIZE_PHY_16;
2241 else if (maxburst >= 8)
2242 psize = STEDMA40_PSIZE_PHY_8;
2243 else if (maxburst >= 4)
2244 psize = STEDMA40_PSIZE_PHY_4;
2245 else
2246 psize = STEDMA40_PSIZE_PHY_1;
2247 }
2248
2249 info->data_width = addr_width;
2250 info->psize = psize;
2251 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2252
2253 return 0;
2254}
2255
Linus Walleij95e14002010-08-04 13:37:45 +02002256/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002257static int d40_set_runtime_config(struct dma_chan *chan,
2258 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002259{
2260 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2261 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002262 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002263 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002264 u32 src_maxburst, dst_maxburst;
2265 int ret;
2266
2267 src_addr_width = config->src_addr_width;
2268 src_maxburst = config->src_maxburst;
2269 dst_addr_width = config->dst_addr_width;
2270 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002271
2272 if (config->direction == DMA_FROM_DEVICE) {
2273 dma_addr_t dev_addr_rx =
2274 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2275
2276 config_addr = config->src_addr;
2277 if (dev_addr_rx)
2278 dev_dbg(d40c->base->dev,
2279 "channel has a pre-wired RX address %08x "
2280 "overriding with %08x\n",
2281 dev_addr_rx, config_addr);
2282 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2283 dev_dbg(d40c->base->dev,
2284 "channel was not configured for peripheral "
2285 "to memory transfer (%d) overriding\n",
2286 cfg->dir);
2287 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2288
Rabin Vincent98ca5282011-06-27 11:33:38 +02002289 /* Configure the memory side */
2290 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2291 dst_addr_width = src_addr_width;
2292 if (dst_maxburst == 0)
2293 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002294
2295 } else if (config->direction == DMA_TO_DEVICE) {
2296 dma_addr_t dev_addr_tx =
2297 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2298
2299 config_addr = config->dst_addr;
2300 if (dev_addr_tx)
2301 dev_dbg(d40c->base->dev,
2302 "channel has a pre-wired TX address %08x "
2303 "overriding with %08x\n",
2304 dev_addr_tx, config_addr);
2305 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2306 dev_dbg(d40c->base->dev,
2307 "channel was not configured for memory "
2308 "to peripheral transfer (%d) overriding\n",
2309 cfg->dir);
2310 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2311
Rabin Vincent98ca5282011-06-27 11:33:38 +02002312 /* Configure the memory side */
2313 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2314 src_addr_width = dst_addr_width;
2315 if (src_maxburst == 0)
2316 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002317 } else {
2318 dev_err(d40c->base->dev,
2319 "unrecognized channel direction %d\n",
2320 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002321 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002322 }
2323
Rabin Vincent98ca5282011-06-27 11:33:38 +02002324 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002325 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002326 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2327 src_maxburst,
2328 src_addr_width,
2329 dst_maxburst,
2330 dst_addr_width);
2331 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002332 }
2333
Rabin Vincent98ca5282011-06-27 11:33:38 +02002334 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2335 src_addr_width,
2336 src_maxburst);
2337 if (ret)
2338 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002339
Rabin Vincent98ca5282011-06-27 11:33:38 +02002340 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2341 dst_addr_width,
2342 dst_maxburst);
2343 if (ret)
2344 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002345
Per Forlina59670a2010-10-06 09:05:27 +00002346 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002347 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002348 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2349 else
2350 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2351 &d40c->dst_def_cfg, false);
2352
Linus Walleij95e14002010-08-04 13:37:45 +02002353 /* These settings will take precedence later */
2354 d40c->runtime_addr = config_addr;
2355 d40c->runtime_direction = config->direction;
2356 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002357 "configured channel %s for %s, data width %d/%d, "
2358 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002359 dma_chan_name(chan),
2360 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002361 src_addr_width, dst_addr_width,
2362 src_maxburst, dst_maxburst);
2363
2364 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002365}
2366
Linus Walleij05827632010-05-17 16:30:42 -07002367static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2368 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002369{
Linus Walleij8d318a52010-03-30 15:33:42 +02002370 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2371
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002372 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002373 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002374 return -EINVAL;
2375 }
2376
Linus Walleij8d318a52010-03-30 15:33:42 +02002377 switch (cmd) {
2378 case DMA_TERMINATE_ALL:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002379 return d40_terminate_all(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002380 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002381 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002382 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002383 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002384 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002385 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002386 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002387 default:
2388 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002389 }
2390
2391 /* Other commands are unimplemented */
2392 return -ENXIO;
2393}
2394
2395/* Initialization functions */
2396
2397static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2398 struct d40_chan *chans, int offset,
2399 int num_chans)
2400{
2401 int i = 0;
2402 struct d40_chan *d40c;
2403
2404 INIT_LIST_HEAD(&dma->channels);
2405
2406 for (i = offset; i < offset + num_chans; i++) {
2407 d40c = &chans[i];
2408 d40c->base = base;
2409 d40c->chan.device = dma;
2410
Linus Walleij8d318a52010-03-30 15:33:42 +02002411 spin_lock_init(&d40c->lock);
2412
2413 d40c->log_num = D40_PHY_CHAN;
2414
Linus Walleij8d318a52010-03-30 15:33:42 +02002415 INIT_LIST_HEAD(&d40c->active);
2416 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002417 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002418 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002419 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002420
Linus Walleij8d318a52010-03-30 15:33:42 +02002421 tasklet_init(&d40c->tasklet, dma_tasklet,
2422 (unsigned long) d40c);
2423
2424 list_add_tail(&d40c->chan.device_node,
2425 &dma->channels);
2426 }
2427}
2428
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002429static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2430{
2431 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2432 dev->device_prep_slave_sg = d40_prep_slave_sg;
2433
2434 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2435 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2436
2437 /*
2438 * This controller can only access address at even
2439 * 32bit boundaries, i.e. 2^2
2440 */
2441 dev->copy_align = 2;
2442 }
2443
2444 if (dma_has_cap(DMA_SG, dev->cap_mask))
2445 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2446
Rabin Vincent0c842b52011-01-25 11:18:35 +01002447 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2448 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2449
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002450 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2451 dev->device_free_chan_resources = d40_free_chan_resources;
2452 dev->device_issue_pending = d40_issue_pending;
2453 dev->device_tx_status = d40_tx_status;
2454 dev->device_control = d40_control;
2455 dev->dev = base->dev;
2456}
2457
Linus Walleij8d318a52010-03-30 15:33:42 +02002458static int __init d40_dmaengine_init(struct d40_base *base,
2459 int num_reserved_chans)
2460{
2461 int err ;
2462
2463 d40_chan_init(base, &base->dma_slave, base->log_chans,
2464 0, base->num_log_chans);
2465
2466 dma_cap_zero(base->dma_slave.cap_mask);
2467 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002468 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002469
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002470 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002471
2472 err = dma_async_device_register(&base->dma_slave);
2473
2474 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002475 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002476 goto failure1;
2477 }
2478
2479 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2480 base->num_log_chans, base->plat_data->memcpy_len);
2481
2482 dma_cap_zero(base->dma_memcpy.cap_mask);
2483 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002484 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002485
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002486 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002487
2488 err = dma_async_device_register(&base->dma_memcpy);
2489
2490 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002491 d40_err(base->dev,
2492 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002493 goto failure2;
2494 }
2495
2496 d40_chan_init(base, &base->dma_both, base->phy_chans,
2497 0, num_reserved_chans);
2498
2499 dma_cap_zero(base->dma_both.cap_mask);
2500 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2501 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002502 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002503 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002504
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002505 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002506 err = dma_async_device_register(&base->dma_both);
2507
2508 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002509 d40_err(base->dev,
2510 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002511 goto failure3;
2512 }
2513 return 0;
2514failure3:
2515 dma_async_device_unregister(&base->dma_memcpy);
2516failure2:
2517 dma_async_device_unregister(&base->dma_slave);
2518failure1:
2519 return err;
2520}
2521
2522/* Initialization functions. */
2523
2524static int __init d40_phy_res_init(struct d40_base *base)
2525{
2526 int i;
2527 int num_phy_chans_avail = 0;
2528 u32 val[2];
2529 int odd_even_bit = -2;
2530
2531 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2532 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2533
2534 for (i = 0; i < base->num_phy_chans; i++) {
2535 base->phy_res[i].num = i;
2536 odd_even_bit += 2 * ((i % 2) == 0);
2537 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2538 /* Mark security only channels as occupied */
2539 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2540 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2541 } else {
2542 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2543 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2544 num_phy_chans_avail++;
2545 }
2546 spin_lock_init(&base->phy_res[i].lock);
2547 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002548
2549 /* Mark disabled channels as occupied */
2550 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002551 int chan = base->plat_data->disabled_channels[i];
2552
2553 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2554 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2555 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002556 }
2557
Linus Walleij8d318a52010-03-30 15:33:42 +02002558 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2559 num_phy_chans_avail, base->num_phy_chans);
2560
2561 /* Verify settings extended vs standard */
2562 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2563
2564 for (i = 0; i < base->num_phy_chans; i++) {
2565
2566 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2567 (val[0] & 0x3) != 1)
2568 dev_info(base->dev,
2569 "[%s] INFO: channel %d is misconfigured (%d)\n",
2570 __func__, i, val[0] & 0x3);
2571
2572 val[0] = val[0] >> 2;
2573 }
2574
2575 return num_phy_chans_avail;
2576}
2577
2578static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2579{
Linus Walleij8d318a52010-03-30 15:33:42 +02002580 struct stedma40_platform_data *plat_data;
2581 struct clk *clk = NULL;
2582 void __iomem *virtbase = NULL;
2583 struct resource *res = NULL;
2584 struct d40_base *base = NULL;
2585 int num_log_chans = 0;
2586 int num_phy_chans;
2587 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02002588 u32 pid;
2589 u32 cid;
2590 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002591
2592 clk = clk_get(&pdev->dev, NULL);
2593
2594 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002595 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002596 goto failure;
2597 }
2598
2599 clk_enable(clk);
2600
2601 /* Get IO for DMAC base address */
2602 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2603 if (!res)
2604 goto failure;
2605
2606 if (request_mem_region(res->start, resource_size(res),
2607 D40_NAME " I/O base") == NULL)
2608 goto failure;
2609
2610 virtbase = ioremap(res->start, resource_size(res));
2611 if (!virtbase)
2612 goto failure;
2613
Linus Walleijf4b89762011-06-27 11:33:46 +02002614 /* This is just a regular AMBA PrimeCell ID actually */
2615 for (pid = 0, i = 0; i < 4; i++)
2616 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
2617 & 255) << (i * 8);
2618 for (cid = 0, i = 0; i < 4; i++)
2619 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
2620 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02002621
Linus Walleijf4b89762011-06-27 11:33:46 +02002622 if (cid != AMBA_CID) {
2623 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002624 goto failure;
2625 }
Linus Walleijf4b89762011-06-27 11:33:46 +02002626 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
2627 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2628 AMBA_MANF_BITS(pid),
2629 AMBA_VENDOR_ST);
2630 goto failure;
2631 }
2632 /*
2633 * HW revision:
2634 * DB8500ed has revision 0
2635 * ? has revision 1
2636 * DB8500v1 has revision 2
2637 * DB8500v2 has revision 3
2638 */
2639 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002640
Linus Walleij8d318a52010-03-30 15:33:42 +02002641 /* The number of physical channels on this HW */
2642 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2643
2644 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002645 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002646
2647 plat_data = pdev->dev.platform_data;
2648
2649 /* Count the number of logical channels in use */
2650 for (i = 0; i < plat_data->dev_len; i++)
2651 if (plat_data->dev_rx[i] != 0)
2652 num_log_chans++;
2653
2654 for (i = 0; i < plat_data->dev_len; i++)
2655 if (plat_data->dev_tx[i] != 0)
2656 num_log_chans++;
2657
2658 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2659 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2660 sizeof(struct d40_chan), GFP_KERNEL);
2661
2662 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002663 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002664 goto failure;
2665 }
2666
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002667 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002668 base->clk = clk;
2669 base->num_phy_chans = num_phy_chans;
2670 base->num_log_chans = num_log_chans;
2671 base->phy_start = res->start;
2672 base->phy_size = resource_size(res);
2673 base->virtbase = virtbase;
2674 base->plat_data = plat_data;
2675 base->dev = &pdev->dev;
2676 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2677 base->log_chans = &base->phy_chans[num_phy_chans];
2678
2679 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2680 GFP_KERNEL);
2681 if (!base->phy_res)
2682 goto failure;
2683
2684 base->lookup_phy_chans = kzalloc(num_phy_chans *
2685 sizeof(struct d40_chan *),
2686 GFP_KERNEL);
2687 if (!base->lookup_phy_chans)
2688 goto failure;
2689
2690 if (num_log_chans + plat_data->memcpy_len) {
2691 /*
2692 * The max number of logical channels are event lines for all
2693 * src devices and dst devices
2694 */
2695 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2696 sizeof(struct d40_chan *),
2697 GFP_KERNEL);
2698 if (!base->lookup_log_chans)
2699 goto failure;
2700 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002701
2702 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2703 sizeof(struct d40_desc *) *
2704 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002705 GFP_KERNEL);
2706 if (!base->lcla_pool.alloc_map)
2707 goto failure;
2708
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002709 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2710 0, SLAB_HWCACHE_ALIGN,
2711 NULL);
2712 if (base->desc_slab == NULL)
2713 goto failure;
2714
Linus Walleij8d318a52010-03-30 15:33:42 +02002715 return base;
2716
2717failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002718 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002719 clk_disable(clk);
2720 clk_put(clk);
2721 }
2722 if (virtbase)
2723 iounmap(virtbase);
2724 if (res)
2725 release_mem_region(res->start,
2726 resource_size(res));
2727 if (virtbase)
2728 iounmap(virtbase);
2729
2730 if (base) {
2731 kfree(base->lcla_pool.alloc_map);
2732 kfree(base->lookup_log_chans);
2733 kfree(base->lookup_phy_chans);
2734 kfree(base->phy_res);
2735 kfree(base);
2736 }
2737
2738 return NULL;
2739}
2740
2741static void __init d40_hw_init(struct d40_base *base)
2742{
2743
2744 static const struct d40_reg_val dma_init_reg[] = {
2745 /* Clock every part of the DMA block from start */
2746 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2747
2748 /* Interrupts on all logical channels */
2749 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2750 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2751 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2752 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2753 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2754 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2755 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2756 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2757 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2758 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2759 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2760 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2761 };
2762 int i;
2763 u32 prmseo[2] = {0, 0};
2764 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2765 u32 pcmis = 0;
2766 u32 pcicr = 0;
2767
2768 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2769 writel(dma_init_reg[i].val,
2770 base->virtbase + dma_init_reg[i].reg);
2771
2772 /* Configure all our dma channels to default settings */
2773 for (i = 0; i < base->num_phy_chans; i++) {
2774
2775 activeo[i % 2] = activeo[i % 2] << 2;
2776
2777 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2778 == D40_ALLOC_PHY) {
2779 activeo[i % 2] |= 3;
2780 continue;
2781 }
2782
2783 /* Enable interrupt # */
2784 pcmis = (pcmis << 1) | 1;
2785
2786 /* Clear interrupt # */
2787 pcicr = (pcicr << 1) | 1;
2788
2789 /* Set channel to physical mode */
2790 prmseo[i % 2] = prmseo[i % 2] << 2;
2791 prmseo[i % 2] |= 1;
2792
2793 }
2794
2795 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2796 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2797 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2798 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2799
2800 /* Write which interrupt to enable */
2801 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2802
2803 /* Write which interrupt to clear */
2804 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2805
2806}
2807
Linus Walleij508849a2010-06-20 21:26:07 +00002808static int __init d40_lcla_allocate(struct d40_base *base)
2809{
Rabin Vincent026cbc42011-01-25 11:18:14 +01002810 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00002811 unsigned long *page_list;
2812 int i, j;
2813 int ret = 0;
2814
2815 /*
2816 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2817 * To full fill this hardware requirement without wasting 256 kb
2818 * we allocate pages until we get an aligned one.
2819 */
2820 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2821 GFP_KERNEL);
2822
2823 if (!page_list) {
2824 ret = -ENOMEM;
2825 goto failure;
2826 }
2827
2828 /* Calculating how many pages that are required */
2829 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2830
2831 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2832 page_list[i] = __get_free_pages(GFP_KERNEL,
2833 base->lcla_pool.pages);
2834 if (!page_list[i]) {
2835
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002836 d40_err(base->dev, "Failed to allocate %d pages.\n",
2837 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002838
2839 for (j = 0; j < i; j++)
2840 free_pages(page_list[j], base->lcla_pool.pages);
2841 goto failure;
2842 }
2843
2844 if ((virt_to_phys((void *)page_list[i]) &
2845 (LCLA_ALIGNMENT - 1)) == 0)
2846 break;
2847 }
2848
2849 for (j = 0; j < i; j++)
2850 free_pages(page_list[j], base->lcla_pool.pages);
2851
2852 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2853 base->lcla_pool.base = (void *)page_list[i];
2854 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002855 /*
2856 * After many attempts and no succees with finding the correct
2857 * alignment, try with allocating a big buffer.
2858 */
Linus Walleij508849a2010-06-20 21:26:07 +00002859 dev_warn(base->dev,
2860 "[%s] Failed to get %d pages @ 18 bit align.\n",
2861 __func__, base->lcla_pool.pages);
2862 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2863 base->num_phy_chans +
2864 LCLA_ALIGNMENT,
2865 GFP_KERNEL);
2866 if (!base->lcla_pool.base_unaligned) {
2867 ret = -ENOMEM;
2868 goto failure;
2869 }
2870
2871 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2872 LCLA_ALIGNMENT);
2873 }
2874
Rabin Vincent026cbc42011-01-25 11:18:14 +01002875 pool->dma_addr = dma_map_single(base->dev, pool->base,
2876 SZ_1K * base->num_phy_chans,
2877 DMA_TO_DEVICE);
2878 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2879 pool->dma_addr = 0;
2880 ret = -ENOMEM;
2881 goto failure;
2882 }
2883
Linus Walleij508849a2010-06-20 21:26:07 +00002884 writel(virt_to_phys(base->lcla_pool.base),
2885 base->virtbase + D40_DREG_LCLA);
2886failure:
2887 kfree(page_list);
2888 return ret;
2889}
2890
Linus Walleij8d318a52010-03-30 15:33:42 +02002891static int __init d40_probe(struct platform_device *pdev)
2892{
2893 int err;
2894 int ret = -ENOENT;
2895 struct d40_base *base;
2896 struct resource *res = NULL;
2897 int num_reserved_chans;
2898 u32 val;
2899
2900 base = d40_hw_detect_init(pdev);
2901
2902 if (!base)
2903 goto failure;
2904
2905 num_reserved_chans = d40_phy_res_init(base);
2906
2907 platform_set_drvdata(pdev, base);
2908
2909 spin_lock_init(&base->interrupt_lock);
2910 spin_lock_init(&base->execmd_lock);
2911
2912 /* Get IO for logical channel parameter address */
2913 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2914 if (!res) {
2915 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002916 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002917 goto failure;
2918 }
2919 base->lcpa_size = resource_size(res);
2920 base->phy_lcpa = res->start;
2921
2922 if (request_mem_region(res->start, resource_size(res),
2923 D40_NAME " I/O lcpa") == NULL) {
2924 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002925 d40_err(&pdev->dev,
2926 "Failed to request LCPA region 0x%x-0x%x\n",
2927 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002928 goto failure;
2929 }
2930
2931 /* We make use of ESRAM memory for this. */
2932 val = readl(base->virtbase + D40_DREG_LCPA);
2933 if (res->start != val && val != 0) {
2934 dev_warn(&pdev->dev,
2935 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2936 __func__, val, res->start);
2937 } else
2938 writel(res->start, base->virtbase + D40_DREG_LCPA);
2939
2940 base->lcpa_base = ioremap(res->start, resource_size(res));
2941 if (!base->lcpa_base) {
2942 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002943 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002944 goto failure;
2945 }
Linus Walleij508849a2010-06-20 21:26:07 +00002946
2947 ret = d40_lcla_allocate(base);
2948 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002949 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002950 goto failure;
2951 }
2952
Linus Walleij8d318a52010-03-30 15:33:42 +02002953 spin_lock_init(&base->lcla_pool.lock);
2954
Linus Walleij8d318a52010-03-30 15:33:42 +02002955 base->irq = platform_get_irq(pdev, 0);
2956
2957 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002958 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002959 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002960 goto failure;
2961 }
2962
2963 err = d40_dmaengine_init(base, num_reserved_chans);
2964 if (err)
2965 goto failure;
2966
2967 d40_hw_init(base);
2968
2969 dev_info(base->dev, "initialized\n");
2970 return 0;
2971
2972failure:
2973 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002974 if (base->desc_slab)
2975 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002976 if (base->virtbase)
2977 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01002978
2979 if (base->lcla_pool.dma_addr)
2980 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2981 SZ_1K * base->num_phy_chans,
2982 DMA_TO_DEVICE);
2983
Linus Walleij508849a2010-06-20 21:26:07 +00002984 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2985 free_pages((unsigned long)base->lcla_pool.base,
2986 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002987
2988 kfree(base->lcla_pool.base_unaligned);
2989
Linus Walleij8d318a52010-03-30 15:33:42 +02002990 if (base->phy_lcpa)
2991 release_mem_region(base->phy_lcpa,
2992 base->lcpa_size);
2993 if (base->phy_start)
2994 release_mem_region(base->phy_start,
2995 base->phy_size);
2996 if (base->clk) {
2997 clk_disable(base->clk);
2998 clk_put(base->clk);
2999 }
3000
3001 kfree(base->lcla_pool.alloc_map);
3002 kfree(base->lookup_log_chans);
3003 kfree(base->lookup_phy_chans);
3004 kfree(base->phy_res);
3005 kfree(base);
3006 }
3007
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003008 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003009 return ret;
3010}
3011
3012static struct platform_driver d40_driver = {
3013 .driver = {
3014 .owner = THIS_MODULE,
3015 .name = D40_NAME,
3016 },
3017};
3018
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003019static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003020{
3021 return platform_driver_probe(&d40_driver, d40_probe);
3022}
Linus Walleija0eb2212011-05-18 14:18:57 +02003023subsys_initcall(stedma40_init);