blob: d32a9ac86084a4203e522e2bb0a7d4b44245f4ef [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
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/dmaengine.h>
12#include <linux/platform_device.h>
13#include <linux/clk.h>
14#include <linux/delay.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000015#include <linux/err.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020016
17#include <plat/ste_dma40.h>
18
19#include "ste_dma40_ll.h"
20
21#define D40_NAME "dma40"
22
23#define D40_PHY_CHAN -1
24
25/* For masking out/in 2 bit channel positions */
26#define D40_CHAN_POS(chan) (2 * (chan / 2))
27#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
28
29/* Maximum iterations taken before giving up suspending a channel */
30#define D40_SUSPEND_MAX_IT 500
31
Linus Walleij508849a2010-06-20 21:26:07 +000032/* Hardware requirement on LCLA alignment */
33#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000034
35/* Max number of links per event group */
36#define D40_LCLA_LINK_PER_EVENT_GRP 128
37#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
38
Linus Walleij508849a2010-06-20 21:26:07 +000039/* Attempts before giving up to trying to get pages that are aligned */
40#define MAX_LCLA_ALLOC_ATTEMPTS 256
41
42/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020043#define D40_ALLOC_FREE (1 << 31)
44#define D40_ALLOC_PHY (1 << 30)
45#define D40_ALLOC_LOG_FREE 0
46
Linus Walleij8d318a52010-03-30 15:33:42 +020047/* Hardware designer of the block */
Jonas Aaberg3ae02672010-08-09 12:08:18 +000048#define D40_HW_DESIGNER 0x8
Linus Walleij8d318a52010-03-30 15:33:42 +020049
50/**
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.
71 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
72 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
73 * one buffer to one buffer.
74 */
75struct d40_lli_pool {
76 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000077 int size;
Linus Walleij8d318a52010-03-30 15:33:42 +020078 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000079 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020080};
81
82/**
83 * struct d40_desc - A descriptor is one DMA job.
84 *
85 * @lli_phy: LLI settings for physical channel. Both src and dst=
86 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
87 * lli_len equals one.
88 * @lli_log: Same as above but for logical channels.
89 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000090 * @lli_len: Number of llis of current descriptor.
Jonas Aaberg698e4732010-08-09 12:08:56 +000091 * @lli_current: Number of transfered llis.
92 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020093 * @txd: DMA engine struct. Used for among other things for communication
94 * during a transfer.
95 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020096 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000097 * the previous one.
Linus Walleij8d318a52010-03-30 15:33:42 +020098 *
99 * This descriptor is used for both logical and physical transfers.
100 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200101struct d40_desc {
102 /* LLI physical */
103 struct d40_phy_lli_bidir lli_phy;
104 /* LLI logical */
105 struct d40_log_lli_bidir lli_log;
106
107 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000108 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000109 int lli_current;
110 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200111
112 struct dma_async_tx_descriptor txd;
113 struct list_head node;
114
Linus Walleij8d318a52010-03-30 15:33:42 +0200115 bool is_in_client_list;
116};
117
118/**
119 * struct d40_lcla_pool - LCLA pool settings and data.
120 *
Linus Walleij508849a2010-06-20 21:26:07 +0000121 * @base: The virtual address of LCLA. 18 bit aligned.
122 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
123 * This pointer is only there for clean-up on error.
124 * @pages: The number of pages needed for all physical channels.
125 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200126 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000127 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200128 */
129struct d40_lcla_pool {
130 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000131 void *base_unaligned;
132 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200133 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000134 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200135};
136
137/**
138 * struct d40_phy_res - struct for handling eventlines mapped to physical
139 * channels.
140 *
141 * @lock: A lock protection this entity.
142 * @num: The physical channel number of this entity.
143 * @allocated_src: Bit mapped to show which src event line's are mapped to
144 * this physical channel. Can also be free or physically allocated.
145 * @allocated_dst: Same as for src but is dst.
146 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000147 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200148 */
149struct d40_phy_res {
150 spinlock_t lock;
151 int num;
152 u32 allocated_src;
153 u32 allocated_dst;
154};
155
156struct d40_base;
157
158/**
159 * struct d40_chan - Struct that describes a channel.
160 *
161 * @lock: A spinlock to protect this struct.
162 * @log_num: The logical number, if any of this channel.
163 * @completed: Starts with 1, after first interrupt it is set to dma engine's
164 * current cookie.
165 * @pending_tx: The number of pending transfers. Used between interrupt handler
166 * and tasklet.
167 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000168 * @phy_chan: Pointer to physical channel which this instance runs on. If this
169 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200170 * @chan: DMA engine handle.
171 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
172 * transfer and call client callback.
173 * @client: Cliented owned descriptor list.
174 * @active: Active descriptor.
175 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200176 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000177 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200178 * @base: Pointer to the device instance struct.
179 * @src_def_cfg: Default cfg register setting for src.
180 * @dst_def_cfg: Default cfg register setting for dst.
181 * @log_def: Default logical channel settings.
182 * @lcla: Space for one dst src pair for logical channel transfers.
183 * @lcpa: Pointer to dst and src lcpa settings.
184 *
185 * This struct can either "be" a logical or a physical channel.
186 */
187struct d40_chan {
188 spinlock_t lock;
189 int log_num;
190 /* ID of the most recent completed transfer */
191 int completed;
192 int pending_tx;
193 bool busy;
194 struct d40_phy_res *phy_chan;
195 struct dma_chan chan;
196 struct tasklet_struct tasklet;
197 struct list_head client;
198 struct list_head active;
199 struct list_head queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200200 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000201 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200202 struct d40_base *base;
203 /* Default register configurations */
204 u32 src_def_cfg;
205 u32 dst_def_cfg;
206 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200207 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200208 /* Runtime reconfiguration */
209 dma_addr_t runtime_addr;
210 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200211};
212
213/**
214 * struct d40_base - The big global struct, one for each probe'd instance.
215 *
216 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
217 * @execmd_lock: Lock for execute command usage since several channels share
218 * the same physical register.
219 * @dev: The device structure.
220 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700221 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200222 * @clk: Pointer to the DMA clock structure.
223 * @phy_start: Physical memory start of the DMA registers.
224 * @phy_size: Size of the DMA register map.
225 * @irq: The IRQ number.
226 * @num_phy_chans: The number of physical channels. Read from HW. This
227 * is the number of available channels for this driver, not counting "Secure
228 * mode" allocated physical channels.
229 * @num_log_chans: The number of logical channels. Calculated from
230 * num_phy_chans.
231 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
232 * @dma_slave: dma_device channels that can do only do slave transfers.
233 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200234 * @log_chans: Room for all possible logical channels in system.
235 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
236 * to log_chans entries.
237 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
238 * to phy_chans entries.
239 * @plat_data: Pointer to provided platform_data which is the driver
240 * configuration.
241 * @phy_res: Vector containing all physical channels.
242 * @lcla_pool: lcla pool settings and data.
243 * @lcpa_base: The virtual mapped address of LCPA.
244 * @phy_lcpa: The physical address of the LCPA.
245 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000246 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200247 */
248struct d40_base {
249 spinlock_t interrupt_lock;
250 spinlock_t execmd_lock;
251 struct device *dev;
252 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700253 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200254 struct clk *clk;
255 phys_addr_t phy_start;
256 resource_size_t phy_size;
257 int irq;
258 int num_phy_chans;
259 int num_log_chans;
260 struct dma_device dma_both;
261 struct dma_device dma_slave;
262 struct dma_device dma_memcpy;
263 struct d40_chan *phy_chans;
264 struct d40_chan *log_chans;
265 struct d40_chan **lookup_log_chans;
266 struct d40_chan **lookup_phy_chans;
267 struct stedma40_platform_data *plat_data;
268 /* Physical half channels */
269 struct d40_phy_res *phy_res;
270 struct d40_lcla_pool lcla_pool;
271 void *lcpa_base;
272 dma_addr_t phy_lcpa;
273 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000274 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200275};
276
277/**
278 * struct d40_interrupt_lookup - lookup table for interrupt handler
279 *
280 * @src: Interrupt mask register.
281 * @clr: Interrupt clear register.
282 * @is_error: true if this is an error interrupt.
283 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
284 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
285 */
286struct d40_interrupt_lookup {
287 u32 src;
288 u32 clr;
289 bool is_error;
290 int offset;
291};
292
293/**
294 * struct d40_reg_val - simple lookup struct
295 *
296 * @reg: The register.
297 * @val: The value that belongs to the register in reg.
298 */
299struct d40_reg_val {
300 unsigned int reg;
301 unsigned int val;
302};
303
Rabin Vincent262d2912011-01-25 11:18:05 +0100304static struct device *chan2dev(struct d40_chan *d40c)
305{
306 return &d40c->chan.dev->device;
307}
308
Rabin Vincent724a8572011-01-25 11:18:08 +0100309static bool chan_is_physical(struct d40_chan *chan)
310{
311 return chan->log_num == D40_PHY_CHAN;
312}
313
314static bool chan_is_logical(struct d40_chan *chan)
315{
316 return !chan_is_physical(chan);
317}
318
Rabin Vincent8ca84682011-01-25 11:18:07 +0100319static void __iomem *chan_base(struct d40_chan *chan)
320{
321 return chan->base->virtbase + D40_DREG_PCBASE +
322 chan->phy_chan->num * D40_DREG_PCDELTA;
323}
324
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100325#define d40_err(dev, format, arg...) \
326 dev_err(dev, "[%s] " format, __func__, ## arg)
327
328#define chan_err(d40c, format, arg...) \
329 d40_err(chan2dev(d40c), format, ## arg)
330
Linus Walleij8d318a52010-03-30 15:33:42 +0200331static int d40_pool_lli_alloc(struct d40_desc *d40d,
332 int lli_len, bool is_log)
333{
334 u32 align;
335 void *base;
336
337 if (is_log)
338 align = sizeof(struct d40_log_lli);
339 else
340 align = sizeof(struct d40_phy_lli);
341
342 if (lli_len == 1) {
343 base = d40d->lli_pool.pre_alloc_lli;
344 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
345 d40d->lli_pool.base = NULL;
346 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100347 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200348
349 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
350 d40d->lli_pool.base = base;
351
352 if (d40d->lli_pool.base == NULL)
353 return -ENOMEM;
354 }
355
356 if (is_log) {
357 d40d->lli_log.src = PTR_ALIGN((struct d40_log_lli *) base,
358 align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100359 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Linus Walleij8d318a52010-03-30 15:33:42 +0200360 } else {
361 d40d->lli_phy.src = PTR_ALIGN((struct d40_phy_lli *)base,
362 align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100363 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Linus Walleij8d318a52010-03-30 15:33:42 +0200364 }
365
366 return 0;
367}
368
369static void d40_pool_lli_free(struct d40_desc *d40d)
370{
371 kfree(d40d->lli_pool.base);
372 d40d->lli_pool.base = NULL;
373 d40d->lli_pool.size = 0;
374 d40d->lli_log.src = NULL;
375 d40d->lli_log.dst = NULL;
376 d40d->lli_phy.src = NULL;
377 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200378}
379
Jonas Aaberg698e4732010-08-09 12:08:56 +0000380static int d40_lcla_alloc_one(struct d40_chan *d40c,
381 struct d40_desc *d40d)
382{
383 unsigned long flags;
384 int i;
385 int ret = -EINVAL;
386 int p;
387
388 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
389
390 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
391
392 /*
393 * Allocate both src and dst at the same time, therefore the half
394 * start on 1 since 0 can't be used since zero is used as end marker.
395 */
396 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
397 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
398 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
399 d40d->lcla_alloc++;
400 ret = i;
401 break;
402 }
403 }
404
405 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
406
407 return ret;
408}
409
410static int d40_lcla_free_all(struct d40_chan *d40c,
411 struct d40_desc *d40d)
412{
413 unsigned long flags;
414 int i;
415 int ret = -EINVAL;
416
Rabin Vincent724a8572011-01-25 11:18:08 +0100417 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000418 return 0;
419
420 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
421
422 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
423 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
424 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
425 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
426 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
427 d40d->lcla_alloc--;
428 if (d40d->lcla_alloc == 0) {
429 ret = 0;
430 break;
431 }
432 }
433 }
434
435 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
436
437 return ret;
438
439}
440
Linus Walleij8d318a52010-03-30 15:33:42 +0200441static void d40_desc_remove(struct d40_desc *d40d)
442{
443 list_del(&d40d->node);
444}
445
446static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
447{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000448 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200449
450 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000451 struct d40_desc *d;
452 struct d40_desc *_d;
453
Linus Walleij8d318a52010-03-30 15:33:42 +0200454 list_for_each_entry_safe(d, _d, &d40c->client, node)
455 if (async_tx_test_ack(&d->txd)) {
456 d40_pool_lli_free(d);
457 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000458 desc = d;
459 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000460 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200461 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200462 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000463
464 if (!desc)
465 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
466
467 if (desc)
468 INIT_LIST_HEAD(&desc->node);
469
470 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200471}
472
473static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
474{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000475
476 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000477 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200478}
479
480static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
481{
482 list_add_tail(&desc->node, &d40c->active);
483}
484
Jonas Aaberg698e4732010-08-09 12:08:56 +0000485static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
486{
487 int curr_lcla = -EINVAL, next_lcla;
488
Rabin Vincent724a8572011-01-25 11:18:08 +0100489 if (chan_is_physical(d40c)) {
Jonas Aaberg698e4732010-08-09 12:08:56 +0000490 d40_phy_lli_write(d40c->base->virtbase,
491 d40c->phy_chan->num,
492 d40d->lli_phy.dst,
493 d40d->lli_phy.src);
494 d40d->lli_current = d40d->lli_len;
495 } else {
496
497 if ((d40d->lli_len - d40d->lli_current) > 1)
498 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
499
500 d40_log_lli_lcpa_write(d40c->lcpa,
501 &d40d->lli_log.dst[d40d->lli_current],
502 &d40d->lli_log.src[d40d->lli_current],
503 curr_lcla);
504
505 d40d->lli_current++;
506 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
507 struct d40_log_lli *lcla;
508
509 if (d40d->lli_current + 1 < d40d->lli_len)
510 next_lcla = d40_lcla_alloc_one(d40c, d40d);
511 else
512 next_lcla = -EINVAL;
513
514 lcla = d40c->base->lcla_pool.base +
515 d40c->phy_chan->num * 1024 +
516 8 * curr_lcla * 2;
517
518 d40_log_lli_lcla_write(lcla,
519 &d40d->lli_log.dst[d40d->lli_current],
520 &d40d->lli_log.src[d40d->lli_current],
521 next_lcla);
522
523 (void) dma_map_single(d40c->base->dev, lcla,
524 2 * sizeof(struct d40_log_lli),
525 DMA_TO_DEVICE);
526
527 curr_lcla = next_lcla;
528
529 if (curr_lcla == -EINVAL) {
530 d40d->lli_current++;
531 break;
532 }
533
534 }
535 }
536}
537
Linus Walleij8d318a52010-03-30 15:33:42 +0200538static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
539{
540 struct d40_desc *d;
541
542 if (list_empty(&d40c->active))
543 return NULL;
544
545 d = list_first_entry(&d40c->active,
546 struct d40_desc,
547 node);
548 return d;
549}
550
551static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
552{
553 list_add_tail(&desc->node, &d40c->queue);
554}
555
556static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
557{
558 struct d40_desc *d;
559
560 if (list_empty(&d40c->queue))
561 return NULL;
562
563 d = list_first_entry(&d40c->queue,
564 struct d40_desc,
565 node);
566 return d;
567}
568
Per Forlind49278e2010-12-20 18:31:38 +0100569static int d40_psize_2_burst_size(bool is_log, int psize)
570{
571 if (is_log) {
572 if (psize == STEDMA40_PSIZE_LOG_1)
573 return 1;
574 } else {
575 if (psize == STEDMA40_PSIZE_PHY_1)
576 return 1;
577 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200578
Per Forlind49278e2010-12-20 18:31:38 +0100579 return 2 << psize;
580}
581
582/*
583 * The dma only supports transmitting packages up to
584 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
585 * dma elements required to send the entire sg list
586 */
587static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
588{
589 int dmalen;
590 u32 max_w = max(data_width1, data_width2);
591 u32 min_w = min(data_width1, data_width2);
592 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
593
594 if (seg_max > STEDMA40_MAX_SEG_SIZE)
595 seg_max -= (1 << max_w);
596
597 if (!IS_ALIGNED(size, 1 << max_w))
598 return -EINVAL;
599
600 if (size <= seg_max)
601 dmalen = 1;
602 else {
603 dmalen = size / seg_max;
604 if (dmalen * seg_max < size)
605 dmalen++;
606 }
607 return dmalen;
608}
609
610static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
611 u32 data_width1, u32 data_width2)
612{
613 struct scatterlist *sg;
614 int i;
615 int len = 0;
616 int ret;
617
618 for_each_sg(sgl, sg, sg_len, i) {
619 ret = d40_size_2_dmalen(sg_dma_len(sg),
620 data_width1, data_width2);
621 if (ret < 0)
622 return ret;
623 len += ret;
624 }
625 return len;
626}
627
628/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200629
630static int d40_channel_execute_command(struct d40_chan *d40c,
631 enum d40_command command)
632{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000633 u32 status;
634 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200635 void __iomem *active_reg;
636 int ret = 0;
637 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000638 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200639
640 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
641
642 if (d40c->phy_chan->num % 2 == 0)
643 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
644 else
645 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
646
647 if (command == D40_DMA_SUSPEND_REQ) {
648 status = (readl(active_reg) &
649 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
650 D40_CHAN_POS(d40c->phy_chan->num);
651
652 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
653 goto done;
654 }
655
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000656 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
657 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
658 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200659
660 if (command == D40_DMA_SUSPEND_REQ) {
661
662 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
663 status = (readl(active_reg) &
664 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
665 D40_CHAN_POS(d40c->phy_chan->num);
666
667 cpu_relax();
668 /*
669 * Reduce the number of bus accesses while
670 * waiting for the DMA to suspend.
671 */
672 udelay(3);
673
674 if (status == D40_DMA_STOP ||
675 status == D40_DMA_SUSPENDED)
676 break;
677 }
678
679 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100680 chan_err(d40c,
681 "unable to suspend the chl %d (log: %d) status %x\n",
682 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200683 status);
684 dump_stack();
685 ret = -EBUSY;
686 }
687
688 }
689done:
690 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
691 return ret;
692}
693
694static void d40_term_all(struct d40_chan *d40c)
695{
696 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200697
698 /* Release active descriptors */
699 while ((d40d = d40_first_active_get(d40c))) {
700 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200701 d40_desc_free(d40c, d40d);
702 }
703
704 /* Release queued descriptors waiting for transfer */
705 while ((d40d = d40_first_queued(d40c))) {
706 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200707 d40_desc_free(d40c, d40d);
708 }
709
Linus Walleij8d318a52010-03-30 15:33:42 +0200710
711 d40c->pending_tx = 0;
712 d40c->busy = false;
713}
714
Rabin Vincent262d2912011-01-25 11:18:05 +0100715static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
716 u32 event, int reg)
717{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100718 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100719 int tries;
720
721 if (!enable) {
722 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
723 | ~D40_EVENTLINE_MASK(event), addr);
724 return;
725 }
726
727 /*
728 * The hardware sometimes doesn't register the enable when src and dst
729 * event lines are active on the same logical channel. Retry to ensure
730 * it does. Usually only one retry is sufficient.
731 */
732 tries = 100;
733 while (--tries) {
734 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
735 | ~D40_EVENTLINE_MASK(event), addr);
736
737 if (readl(addr) & D40_EVENTLINE_MASK(event))
738 break;
739 }
740
741 if (tries != 99)
742 dev_dbg(chan2dev(d40c),
743 "[%s] workaround enable S%cLNK (%d tries)\n",
744 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
745 100 - tries);
746
747 WARN_ON(!tries);
748}
749
Linus Walleij8d318a52010-03-30 15:33:42 +0200750static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
751{
Linus Walleij8d318a52010-03-30 15:33:42 +0200752 unsigned long flags;
753
Linus Walleij8d318a52010-03-30 15:33:42 +0200754 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
755
756 /* Enable event line connected to device (or memcpy) */
757 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
758 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
759 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
760
Rabin Vincent262d2912011-01-25 11:18:05 +0100761 __d40_config_set_event(d40c, do_enable, event,
762 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200763 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100764
Linus Walleij8d318a52010-03-30 15:33:42 +0200765 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
766 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
767
Rabin Vincent262d2912011-01-25 11:18:05 +0100768 __d40_config_set_event(d40c, do_enable, event,
769 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200770 }
771
772 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
773}
774
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200775static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200776{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100777 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000778 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200779
Rabin Vincent8ca84682011-01-25 11:18:07 +0100780 val = readl(chanbase + D40_CHAN_REG_SSLNK);
781 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200782
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200783 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200784}
785
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000786static u32 d40_get_prmo(struct d40_chan *d40c)
787{
788 static const unsigned int phy_map[] = {
789 [STEDMA40_PCHAN_BASIC_MODE]
790 = D40_DREG_PRMO_PCHAN_BASIC,
791 [STEDMA40_PCHAN_MODULO_MODE]
792 = D40_DREG_PRMO_PCHAN_MODULO,
793 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
794 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
795 };
796 static const unsigned int log_map[] = {
797 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
798 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
799 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
800 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
801 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
802 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
803 };
804
Rabin Vincent724a8572011-01-25 11:18:08 +0100805 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000806 return phy_map[d40c->dma_cfg.mode_opt];
807 else
808 return log_map[d40c->dma_cfg.mode_opt];
809}
810
Jonas Aabergb55912c2010-08-09 12:08:02 +0000811static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200812{
813 u32 addr_base;
814 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200815
816 /* Odd addresses are even addresses + 4 */
817 addr_base = (d40c->phy_chan->num % 2) * 4;
818 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100819 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200820 D40_CHAN_POS(d40c->phy_chan->num);
821 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
822
823 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000824 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200825
826 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
827
Rabin Vincent724a8572011-01-25 11:18:08 +0100828 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100829 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
830 & D40_SREG_ELEM_LOG_LIDX_MASK;
831 void __iomem *chanbase = chan_base(d40c);
832
Linus Walleij8d318a52010-03-30 15:33:42 +0200833 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100834 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
835 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200836
Jonas Aabergb55912c2010-08-09 12:08:02 +0000837 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100838 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
839 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200840 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200841}
842
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000843static u32 d40_residue(struct d40_chan *d40c)
844{
845 u32 num_elt;
846
Rabin Vincent724a8572011-01-25 11:18:08 +0100847 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000848 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
849 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100850 else {
851 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
852 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
853 >> D40_SREG_ELEM_PHY_ECNT_POS;
854 }
855
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000856 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
857}
858
859static bool d40_tx_is_linked(struct d40_chan *d40c)
860{
861 bool is_link;
862
Rabin Vincent724a8572011-01-25 11:18:08 +0100863 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000864 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
865 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100866 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
867 & D40_SREG_LNK_PHYS_LNK_MASK;
868
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000869 return is_link;
870}
871
872static int d40_pause(struct dma_chan *chan)
873{
874 struct d40_chan *d40c =
875 container_of(chan, struct d40_chan, chan);
876 int res = 0;
877 unsigned long flags;
878
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000879 if (!d40c->busy)
880 return 0;
881
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000882 spin_lock_irqsave(&d40c->lock, flags);
883
884 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
885 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +0100886 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000887 d40_config_set_event(d40c, false);
888 /* Resume the other logical channels if any */
889 if (d40_chan_has_events(d40c))
890 res = d40_channel_execute_command(d40c,
891 D40_DMA_RUN);
892 }
893 }
894
895 spin_unlock_irqrestore(&d40c->lock, flags);
896 return res;
897}
898
899static int d40_resume(struct dma_chan *chan)
900{
901 struct d40_chan *d40c =
902 container_of(chan, struct d40_chan, chan);
903 int res = 0;
904 unsigned long flags;
905
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000906 if (!d40c->busy)
907 return 0;
908
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000909 spin_lock_irqsave(&d40c->lock, flags);
910
911 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +0100912 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000913 res = d40_channel_execute_command(d40c,
914 D40_DMA_SUSPEND_REQ);
915 goto no_suspend;
916 }
917
918 /* If bytes left to transfer or linked tx resume job */
919 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
920
Rabin Vincent724a8572011-01-25 11:18:08 +0100921 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000922 d40_config_set_event(d40c, true);
923
924 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
925 }
926
927no_suspend:
928 spin_unlock_irqrestore(&d40c->lock, flags);
929 return res;
930}
931
Linus Walleij8d318a52010-03-30 15:33:42 +0200932static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
933{
934 struct d40_chan *d40c = container_of(tx->chan,
935 struct d40_chan,
936 chan);
937 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
938 unsigned long flags;
939
940 spin_lock_irqsave(&d40c->lock, flags);
941
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000942 d40c->chan.cookie++;
943
944 if (d40c->chan.cookie < 0)
945 d40c->chan.cookie = 1;
946
947 d40d->txd.cookie = d40c->chan.cookie;
948
Linus Walleij8d318a52010-03-30 15:33:42 +0200949 d40_desc_queue(d40c, d40d);
950
951 spin_unlock_irqrestore(&d40c->lock, flags);
952
953 return tx->cookie;
954}
955
956static int d40_start(struct d40_chan *d40c)
957{
Linus Walleijf4185592010-06-22 18:06:42 -0700958 if (d40c->base->rev == 0) {
959 int err;
960
Rabin Vincent724a8572011-01-25 11:18:08 +0100961 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -0700962 err = d40_channel_execute_command(d40c,
963 D40_DMA_SUSPEND_REQ);
964 if (err)
965 return err;
966 }
967 }
968
Rabin Vincent724a8572011-01-25 11:18:08 +0100969 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +0200970 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +0200971
Jonas Aaberg0c322692010-06-20 21:25:46 +0000972 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +0200973}
974
975static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
976{
977 struct d40_desc *d40d;
978 int err;
979
980 /* Start queued jobs, if any */
981 d40d = d40_first_queued(d40c);
982
983 if (d40d != NULL) {
984 d40c->busy = true;
985
986 /* Remove from queue */
987 d40_desc_remove(d40d);
988
989 /* Add to active queue */
990 d40_desc_submit(d40c, d40d);
991
Rabin Vincent7d83a852011-01-25 11:18:06 +0100992 /* Initiate DMA job */
993 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000994
Rabin Vincent7d83a852011-01-25 11:18:06 +0100995 /* Start dma job */
996 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200997
Rabin Vincent7d83a852011-01-25 11:18:06 +0100998 if (err)
999 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001000 }
1001
1002 return d40d;
1003}
1004
1005/* called from interrupt context */
1006static void dma_tc_handle(struct d40_chan *d40c)
1007{
1008 struct d40_desc *d40d;
1009
Linus Walleij8d318a52010-03-30 15:33:42 +02001010 /* Get first active entry from list */
1011 d40d = d40_first_active_get(d40c);
1012
1013 if (d40d == NULL)
1014 return;
1015
Jonas Aaberg698e4732010-08-09 12:08:56 +00001016 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001017
Jonas Aaberg698e4732010-08-09 12:08:56 +00001018 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001019 d40_desc_load(d40c, d40d);
1020 /* Start dma job */
1021 (void) d40_start(d40c);
1022 return;
1023 }
1024
1025 if (d40_queue_start(d40c) == NULL)
1026 d40c->busy = false;
1027
1028 d40c->pending_tx++;
1029 tasklet_schedule(&d40c->tasklet);
1030
1031}
1032
1033static void dma_tasklet(unsigned long data)
1034{
1035 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001036 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001037 unsigned long flags;
1038 dma_async_tx_callback callback;
1039 void *callback_param;
1040
1041 spin_lock_irqsave(&d40c->lock, flags);
1042
1043 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001044 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001045
Jonas Aaberg767a9672010-08-09 12:08:34 +00001046 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001047 goto err;
1048
Jonas Aaberg767a9672010-08-09 12:08:34 +00001049 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001050
1051 /*
1052 * If terminating a channel pending_tx is set to zero.
1053 * This prevents any finished active jobs to return to the client.
1054 */
1055 if (d40c->pending_tx == 0) {
1056 spin_unlock_irqrestore(&d40c->lock, flags);
1057 return;
1058 }
1059
1060 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001061 callback = d40d->txd.callback;
1062 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001063
Jonas Aaberg767a9672010-08-09 12:08:34 +00001064 if (async_tx_test_ack(&d40d->txd)) {
1065 d40_pool_lli_free(d40d);
1066 d40_desc_remove(d40d);
1067 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001068 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001069 if (!d40d->is_in_client_list) {
1070 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001071 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001072 list_add_tail(&d40d->node, &d40c->client);
1073 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001074 }
1075 }
1076
1077 d40c->pending_tx--;
1078
1079 if (d40c->pending_tx)
1080 tasklet_schedule(&d40c->tasklet);
1081
1082 spin_unlock_irqrestore(&d40c->lock, flags);
1083
Jonas Aaberg767a9672010-08-09 12:08:34 +00001084 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001085 callback(callback_param);
1086
1087 return;
1088
1089 err:
1090 /* Rescue manouver if receiving double interrupts */
1091 if (d40c->pending_tx > 0)
1092 d40c->pending_tx--;
1093 spin_unlock_irqrestore(&d40c->lock, flags);
1094}
1095
1096static irqreturn_t d40_handle_interrupt(int irq, void *data)
1097{
1098 static const struct d40_interrupt_lookup il[] = {
1099 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1100 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1101 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1102 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1103 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1104 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1105 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1106 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1107 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1108 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1109 };
1110
1111 int i;
1112 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001113 u32 idx;
1114 u32 row;
1115 long chan = -1;
1116 struct d40_chan *d40c;
1117 unsigned long flags;
1118 struct d40_base *base = data;
1119
1120 spin_lock_irqsave(&base->interrupt_lock, flags);
1121
1122 /* Read interrupt status of both logical and physical channels */
1123 for (i = 0; i < ARRAY_SIZE(il); i++)
1124 regs[i] = readl(base->virtbase + il[i].src);
1125
1126 for (;;) {
1127
1128 chan = find_next_bit((unsigned long *)regs,
1129 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1130
1131 /* No more set bits found? */
1132 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1133 break;
1134
1135 row = chan / BITS_PER_LONG;
1136 idx = chan & (BITS_PER_LONG - 1);
1137
1138 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001139 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001140
1141 if (il[row].offset == D40_PHY_CHAN)
1142 d40c = base->lookup_phy_chans[idx];
1143 else
1144 d40c = base->lookup_log_chans[il[row].offset + idx];
1145 spin_lock(&d40c->lock);
1146
1147 if (!il[row].is_error)
1148 dma_tc_handle(d40c);
1149 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001150 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1151 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001152
1153 spin_unlock(&d40c->lock);
1154 }
1155
1156 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1157
1158 return IRQ_HANDLED;
1159}
1160
Linus Walleij8d318a52010-03-30 15:33:42 +02001161static int d40_validate_conf(struct d40_chan *d40c,
1162 struct stedma40_chan_cfg *conf)
1163{
1164 int res = 0;
1165 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1166 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001167 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001168
Linus Walleij0747c7b2010-08-09 12:07:36 +00001169 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001170 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7b2010-08-09 12:07:36 +00001171 res = -EINVAL;
1172 }
1173
1174 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1175 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1176 d40c->runtime_addr == 0) {
1177
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001178 chan_err(d40c, "Invalid TX channel address (%d)\n",
1179 conf->dst_dev_type);
Linus Walleij0747c7b2010-08-09 12:07:36 +00001180 res = -EINVAL;
1181 }
1182
1183 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1184 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1185 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001186 chan_err(d40c, "Invalid RX channel address (%d)\n",
1187 conf->src_dev_type);
Linus Walleij0747c7b2010-08-09 12:07:36 +00001188 res = -EINVAL;
1189 }
1190
1191 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001192 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001193 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001194 res = -EINVAL;
1195 }
1196
Linus Walleij0747c7b2010-08-09 12:07:36 +00001197 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001198 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001199 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001200 res = -EINVAL;
1201 }
1202
1203 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1204 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001205 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001206 res = -EINVAL;
1207 }
1208
1209 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1210 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001211 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001212 res = -EINVAL;
1213 }
1214
1215 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1216 /*
1217 * DMAC HW supports it. Will be added to this driver,
1218 * in case any dma client requires it.
1219 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001220 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001221 res = -EINVAL;
1222 }
1223
Per Forlind49278e2010-12-20 18:31:38 +01001224 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1225 (1 << conf->src_info.data_width) !=
1226 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1227 (1 << conf->dst_info.data_width)) {
1228 /*
1229 * The DMAC hardware only supports
1230 * src (burst x width) == dst (burst x width)
1231 */
1232
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001233 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001234 res = -EINVAL;
1235 }
1236
Linus Walleij8d318a52010-03-30 15:33:42 +02001237 return res;
1238}
1239
1240static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001241 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001242{
1243 unsigned long flags;
1244 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001245 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001246 /* Physical interrupts are masked per physical full channel */
1247 if (phy->allocated_src == D40_ALLOC_FREE &&
1248 phy->allocated_dst == D40_ALLOC_FREE) {
1249 phy->allocated_dst = D40_ALLOC_PHY;
1250 phy->allocated_src = D40_ALLOC_PHY;
1251 goto found;
1252 } else
1253 goto not_found;
1254 }
1255
1256 /* Logical channel */
1257 if (is_src) {
1258 if (phy->allocated_src == D40_ALLOC_PHY)
1259 goto not_found;
1260
1261 if (phy->allocated_src == D40_ALLOC_FREE)
1262 phy->allocated_src = D40_ALLOC_LOG_FREE;
1263
1264 if (!(phy->allocated_src & (1 << log_event_line))) {
1265 phy->allocated_src |= 1 << log_event_line;
1266 goto found;
1267 } else
1268 goto not_found;
1269 } else {
1270 if (phy->allocated_dst == D40_ALLOC_PHY)
1271 goto not_found;
1272
1273 if (phy->allocated_dst == D40_ALLOC_FREE)
1274 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1275
1276 if (!(phy->allocated_dst & (1 << log_event_line))) {
1277 phy->allocated_dst |= 1 << log_event_line;
1278 goto found;
1279 } else
1280 goto not_found;
1281 }
1282
1283not_found:
1284 spin_unlock_irqrestore(&phy->lock, flags);
1285 return false;
1286found:
1287 spin_unlock_irqrestore(&phy->lock, flags);
1288 return true;
1289}
1290
1291static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1292 int log_event_line)
1293{
1294 unsigned long flags;
1295 bool is_free = false;
1296
1297 spin_lock_irqsave(&phy->lock, flags);
1298 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001299 phy->allocated_dst = D40_ALLOC_FREE;
1300 phy->allocated_src = D40_ALLOC_FREE;
1301 is_free = true;
1302 goto out;
1303 }
1304
1305 /* Logical channel */
1306 if (is_src) {
1307 phy->allocated_src &= ~(1 << log_event_line);
1308 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1309 phy->allocated_src = D40_ALLOC_FREE;
1310 } else {
1311 phy->allocated_dst &= ~(1 << log_event_line);
1312 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1313 phy->allocated_dst = D40_ALLOC_FREE;
1314 }
1315
1316 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1317 D40_ALLOC_FREE);
1318
1319out:
1320 spin_unlock_irqrestore(&phy->lock, flags);
1321
1322 return is_free;
1323}
1324
1325static int d40_allocate_channel(struct d40_chan *d40c)
1326{
1327 int dev_type;
1328 int event_group;
1329 int event_line;
1330 struct d40_phy_res *phys;
1331 int i;
1332 int j;
1333 int log_num;
1334 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001335 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001336
1337 phys = d40c->base->phy_res;
1338
1339 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1340 dev_type = d40c->dma_cfg.src_dev_type;
1341 log_num = 2 * dev_type;
1342 is_src = true;
1343 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1344 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1345 /* dst event lines are used for logical memcpy */
1346 dev_type = d40c->dma_cfg.dst_dev_type;
1347 log_num = 2 * dev_type + 1;
1348 is_src = false;
1349 } else
1350 return -EINVAL;
1351
1352 event_group = D40_TYPE_TO_GROUP(dev_type);
1353 event_line = D40_TYPE_TO_EVENT(dev_type);
1354
1355 if (!is_log) {
1356 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1357 /* Find physical half channel */
1358 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1359
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001360 if (d40_alloc_mask_set(&phys[i], is_src,
1361 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001362 goto found_phy;
1363 }
1364 } else
1365 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1366 int phy_num = j + event_group * 2;
1367 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001368 if (d40_alloc_mask_set(&phys[i],
1369 is_src,
1370 0,
1371 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001372 goto found_phy;
1373 }
1374 }
1375 return -EINVAL;
1376found_phy:
1377 d40c->phy_chan = &phys[i];
1378 d40c->log_num = D40_PHY_CHAN;
1379 goto out;
1380 }
1381 if (dev_type == -1)
1382 return -EINVAL;
1383
1384 /* Find logical channel */
1385 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1386 int phy_num = j + event_group * 2;
1387 /*
1388 * Spread logical channels across all available physical rather
1389 * than pack every logical channel at the first available phy
1390 * channels.
1391 */
1392 if (is_src) {
1393 for (i = phy_num; i < phy_num + 2; i++) {
1394 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001395 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001396 goto found_log;
1397 }
1398 } else {
1399 for (i = phy_num + 1; i >= phy_num; i--) {
1400 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001401 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001402 goto found_log;
1403 }
1404 }
1405 }
1406 return -EINVAL;
1407
1408found_log:
1409 d40c->phy_chan = &phys[i];
1410 d40c->log_num = log_num;
1411out:
1412
1413 if (is_log)
1414 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1415 else
1416 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1417
1418 return 0;
1419
1420}
1421
Linus Walleij8d318a52010-03-30 15:33:42 +02001422static int d40_config_memcpy(struct d40_chan *d40c)
1423{
1424 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1425
1426 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1427 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1428 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1429 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1430 memcpy[d40c->chan.chan_id];
1431
1432 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1433 dma_has_cap(DMA_SLAVE, cap)) {
1434 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1435 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001436 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001437 return -EINVAL;
1438 }
1439
1440 return 0;
1441}
1442
1443
1444static int d40_free_dma(struct d40_chan *d40c)
1445{
1446
1447 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001448 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001449 struct d40_phy_res *phy = d40c->phy_chan;
1450 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001451 struct d40_desc *d;
1452 struct d40_desc *_d;
1453
Linus Walleij8d318a52010-03-30 15:33:42 +02001454
1455 /* Terminate all queued and active transfers */
1456 d40_term_all(d40c);
1457
Per Fridena8be8622010-06-20 21:24:59 +00001458 /* Release client owned descriptors */
1459 if (!list_empty(&d40c->client))
1460 list_for_each_entry_safe(d, _d, &d40c->client, node) {
1461 d40_pool_lli_free(d);
1462 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001463 d40_desc_free(d40c, d);
1464 }
1465
Linus Walleij8d318a52010-03-30 15:33:42 +02001466 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001467 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001468 return -EINVAL;
1469 }
1470
1471 if (phy->allocated_src == D40_ALLOC_FREE &&
1472 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001473 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001474 return -EINVAL;
1475 }
1476
Linus Walleij8d318a52010-03-30 15:33:42 +02001477 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1478 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1479 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001480 is_src = false;
1481 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1482 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001483 is_src = true;
1484 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001485 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001486 return -EINVAL;
1487 }
1488
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001489 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1490 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001491 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001492 return res;
1493 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001494
Rabin Vincent724a8572011-01-25 11:18:08 +01001495 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001496 /* Release logical channel, deactivate the event line */
1497
1498 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001499 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1500
1501 /*
1502 * Check if there are more logical allocation
1503 * on this phy channel.
1504 */
1505 if (!d40_alloc_mask_free(phy, is_src, event)) {
1506 /* Resume the other logical channels if any */
1507 if (d40_chan_has_events(d40c)) {
1508 res = d40_channel_execute_command(d40c,
1509 D40_DMA_RUN);
1510 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001511 chan_err(d40c,
1512 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001513 return res;
1514 }
1515 }
1516 return 0;
1517 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001518 } else {
1519 (void) d40_alloc_mask_free(phy, is_src, 0);
1520 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001521
1522 /* Release physical channel */
1523 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1524 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001525 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001526 return res;
1527 }
1528 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001529 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001530 d40c->base->lookup_phy_chans[phy->num] = NULL;
1531
1532 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001533}
1534
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001535static bool d40_is_paused(struct d40_chan *d40c)
1536{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001537 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001538 bool is_paused = false;
1539 unsigned long flags;
1540 void __iomem *active_reg;
1541 u32 status;
1542 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001543
1544 spin_lock_irqsave(&d40c->lock, flags);
1545
Rabin Vincent724a8572011-01-25 11:18:08 +01001546 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001547 if (d40c->phy_chan->num % 2 == 0)
1548 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1549 else
1550 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1551
1552 status = (readl(active_reg) &
1553 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1554 D40_CHAN_POS(d40c->phy_chan->num);
1555 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1556 is_paused = true;
1557
1558 goto _exit;
1559 }
1560
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001561 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001562 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001563 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001564 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001565 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001566 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001567 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001568 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001569 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001570 goto _exit;
1571 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001572
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001573 status = (status & D40_EVENTLINE_MASK(event)) >>
1574 D40_EVENTLINE_POS(event);
1575
1576 if (status != D40_DMA_RUN)
1577 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001578_exit:
1579 spin_unlock_irqrestore(&d40c->lock, flags);
1580 return is_paused;
1581
1582}
1583
1584
Linus Walleij8d318a52010-03-30 15:33:42 +02001585static u32 stedma40_residue(struct dma_chan *chan)
1586{
1587 struct d40_chan *d40c =
1588 container_of(chan, struct d40_chan, chan);
1589 u32 bytes_left;
1590 unsigned long flags;
1591
1592 spin_lock_irqsave(&d40c->lock, flags);
1593 bytes_left = d40_residue(d40c);
1594 spin_unlock_irqrestore(&d40c->lock, flags);
1595
1596 return bytes_left;
1597}
1598
Linus Walleij8d318a52010-03-30 15:33:42 +02001599struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
1600 struct scatterlist *sgl_dst,
1601 struct scatterlist *sgl_src,
1602 unsigned int sgl_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001603 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001604{
1605 int res;
1606 struct d40_desc *d40d;
1607 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1608 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001609 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001610
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001611 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001612 chan_err(d40c, "Unallocated channel.\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001613 return ERR_PTR(-EINVAL);
1614 }
1615
Jonas Aaberg2a614342010-06-20 21:25:24 +00001616 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001617 d40d = d40_desc_get(d40c);
1618
1619 if (d40d == NULL)
1620 goto err;
1621
Per Forlind49278e2010-12-20 18:31:38 +01001622 d40d->lli_len = d40_sg_2_dmalen(sgl_dst, sgl_len,
1623 d40c->dma_cfg.src_info.data_width,
1624 d40c->dma_cfg.dst_info.data_width);
1625 if (d40d->lli_len < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001626 chan_err(d40c, "Unaligned size\n");
Per Forlind49278e2010-12-20 18:31:38 +01001627 goto err;
1628 }
1629
Jonas Aaberg698e4732010-08-09 12:08:56 +00001630 d40d->lli_current = 0;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001631 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001632
Rabin Vincent724a8572011-01-25 11:18:08 +01001633 if (chan_is_logical(d40c)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001634
Per Forlind49278e2010-12-20 18:31:38 +01001635 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001636 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001637 goto err;
1638 }
1639
Jonas Aaberg698e4732010-08-09 12:08:56 +00001640 (void) d40_log_sg_to_lli(sgl_src,
Linus Walleij8d318a52010-03-30 15:33:42 +02001641 sgl_len,
1642 d40d->lli_log.src,
1643 d40c->log_def.lcsp1,
Per Forlind49278e2010-12-20 18:31:38 +01001644 d40c->dma_cfg.src_info.data_width,
1645 d40c->dma_cfg.dst_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001646
Jonas Aaberg698e4732010-08-09 12:08:56 +00001647 (void) d40_log_sg_to_lli(sgl_dst,
Linus Walleij8d318a52010-03-30 15:33:42 +02001648 sgl_len,
1649 d40d->lli_log.dst,
1650 d40c->log_def.lcsp3,
Per Forlind49278e2010-12-20 18:31:38 +01001651 d40c->dma_cfg.dst_info.data_width,
1652 d40c->dma_cfg.src_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001653 } else {
Per Forlind49278e2010-12-20 18:31:38 +01001654 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001655 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001656 goto err;
1657 }
1658
1659 res = d40_phy_sg_to_lli(sgl_src,
1660 sgl_len,
1661 0,
1662 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001663 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02001664 d40c->src_def_cfg,
1665 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001666 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001667 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001668
1669 if (res < 0)
1670 goto err;
1671
1672 res = d40_phy_sg_to_lli(sgl_dst,
1673 sgl_len,
1674 0,
1675 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001676 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02001677 d40c->dst_def_cfg,
1678 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001679 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001680 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001681
1682 if (res < 0)
1683 goto err;
1684
1685 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1686 d40d->lli_pool.size, DMA_TO_DEVICE);
1687 }
1688
1689 dma_async_tx_descriptor_init(&d40d->txd, chan);
1690
1691 d40d->txd.tx_submit = d40_tx_submit;
1692
Jonas Aaberg2a614342010-06-20 21:25:24 +00001693 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001694
1695 return &d40d->txd;
1696err:
Rabin Vincent819504f2010-10-06 08:20:38 +00001697 if (d40d)
1698 d40_desc_free(d40c, d40d);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001699 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001700 return NULL;
1701}
1702EXPORT_SYMBOL(stedma40_memcpy_sg);
1703
1704bool stedma40_filter(struct dma_chan *chan, void *data)
1705{
1706 struct stedma40_chan_cfg *info = data;
1707 struct d40_chan *d40c =
1708 container_of(chan, struct d40_chan, chan);
1709 int err;
1710
1711 if (data) {
1712 err = d40_validate_conf(d40c, info);
1713 if (!err)
1714 d40c->dma_cfg = *info;
1715 } else
1716 err = d40_config_memcpy(d40c);
1717
Rabin Vincentce2ca122010-10-12 13:00:49 +00001718 if (!err)
1719 d40c->configured = true;
1720
Linus Walleij8d318a52010-03-30 15:33:42 +02001721 return err == 0;
1722}
1723EXPORT_SYMBOL(stedma40_filter);
1724
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001725static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1726{
1727 bool realtime = d40c->dma_cfg.realtime;
1728 bool highprio = d40c->dma_cfg.high_priority;
1729 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1730 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1731 u32 event = D40_TYPE_TO_EVENT(dev_type);
1732 u32 group = D40_TYPE_TO_GROUP(dev_type);
1733 u32 bit = 1 << event;
1734
1735 /* Destination event lines are stored in the upper halfword */
1736 if (!src)
1737 bit <<= 16;
1738
1739 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1740 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1741}
1742
1743static void d40_set_prio_realtime(struct d40_chan *d40c)
1744{
1745 if (d40c->base->rev < 3)
1746 return;
1747
1748 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1749 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1750 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1751
1752 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1753 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1754 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1755}
1756
Linus Walleij8d318a52010-03-30 15:33:42 +02001757/* DMA ENGINE functions */
1758static int d40_alloc_chan_resources(struct dma_chan *chan)
1759{
1760 int err;
1761 unsigned long flags;
1762 struct d40_chan *d40c =
1763 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001764 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001765 spin_lock_irqsave(&d40c->lock, flags);
1766
1767 d40c->completed = chan->cookie = 1;
1768
Rabin Vincentce2ca122010-10-12 13:00:49 +00001769 /* If no dma configuration is set use default configuration (memcpy) */
1770 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001771 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001772 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001773 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001774 goto fail;
1775 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001776 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001777 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001778
1779 err = d40_allocate_channel(d40c);
1780 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001781 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001782 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001783 }
1784
Linus Walleijef1872e2010-06-20 21:24:52 +00001785 /* Fill in basic CFG register values */
1786 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01001787 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00001788
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001789 d40_set_prio_realtime(d40c);
1790
Rabin Vincent724a8572011-01-25 11:18:08 +01001791 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00001792 d40_log_cfg(&d40c->dma_cfg,
1793 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1794
1795 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1796 d40c->lcpa = d40c->base->lcpa_base +
1797 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1798 else
1799 d40c->lcpa = d40c->base->lcpa_base +
1800 d40c->dma_cfg.dst_dev_type *
1801 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1802 }
1803
1804 /*
1805 * Only write channel configuration to the DMA if the physical
1806 * resource is free. In case of multiple logical channels
1807 * on the same physical resource, only the first write is necessary.
1808 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001809 if (is_free_phy)
1810 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001811fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001812 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001813 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001814}
1815
1816static void d40_free_chan_resources(struct dma_chan *chan)
1817{
1818 struct d40_chan *d40c =
1819 container_of(chan, struct d40_chan, chan);
1820 int err;
1821 unsigned long flags;
1822
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001823 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001824 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001825 return;
1826 }
1827
1828
Linus Walleij8d318a52010-03-30 15:33:42 +02001829 spin_lock_irqsave(&d40c->lock, flags);
1830
1831 err = d40_free_dma(d40c);
1832
1833 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001834 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001835 spin_unlock_irqrestore(&d40c->lock, flags);
1836}
1837
1838static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1839 dma_addr_t dst,
1840 dma_addr_t src,
1841 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001842 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001843{
1844 struct d40_desc *d40d;
1845 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1846 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001847 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001848
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001849 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001850 chan_err(d40c, "Channel is not allocated.\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001851 return ERR_PTR(-EINVAL);
1852 }
1853
Jonas Aaberg2a614342010-06-20 21:25:24 +00001854 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001855 d40d = d40_desc_get(d40c);
1856
1857 if (d40d == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001858 chan_err(d40c, "Descriptor is NULL\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001859 goto err;
1860 }
1861
Jonas Aaberg2a614342010-06-20 21:25:24 +00001862 d40d->txd.flags = dma_flags;
Per Forlind49278e2010-12-20 18:31:38 +01001863 d40d->lli_len = d40_size_2_dmalen(size,
1864 d40c->dma_cfg.src_info.data_width,
1865 d40c->dma_cfg.dst_info.data_width);
1866 if (d40d->lli_len < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001867 chan_err(d40c, "Unaligned size\n");
Per Forlind49278e2010-12-20 18:31:38 +01001868 goto err;
1869 }
1870
Linus Walleij8d318a52010-03-30 15:33:42 +02001871
1872 dma_async_tx_descriptor_init(&d40d->txd, chan);
1873
1874 d40d->txd.tx_submit = d40_tx_submit;
1875
Rabin Vincent724a8572011-01-25 11:18:08 +01001876 if (chan_is_logical(d40c)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001877
Per Forlind49278e2010-12-20 18:31:38 +01001878 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001879 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001880 goto err;
1881 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00001882 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001883
Per Forlind49278e2010-12-20 18:31:38 +01001884 if (d40_log_buf_to_lli(d40d->lli_log.src,
1885 src,
1886 size,
1887 d40c->log_def.lcsp1,
1888 d40c->dma_cfg.src_info.data_width,
1889 d40c->dma_cfg.dst_info.data_width,
1890 true) == NULL)
1891 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001892
Per Forlind49278e2010-12-20 18:31:38 +01001893 if (d40_log_buf_to_lli(d40d->lli_log.dst,
1894 dst,
1895 size,
1896 d40c->log_def.lcsp3,
1897 d40c->dma_cfg.dst_info.data_width,
1898 d40c->dma_cfg.src_info.data_width,
1899 true) == NULL)
1900 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001901
1902 } else {
1903
Per Forlind49278e2010-12-20 18:31:38 +01001904 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001905 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001906 goto err;
1907 }
1908
Per Forlind49278e2010-12-20 18:31:38 +01001909 if (d40_phy_buf_to_lli(d40d->lli_phy.src,
Linus Walleij8d318a52010-03-30 15:33:42 +02001910 src,
1911 size,
1912 d40c->dma_cfg.src_info.psize,
1913 0,
1914 d40c->src_def_cfg,
1915 true,
1916 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001917 d40c->dma_cfg.dst_info.data_width,
1918 false) == NULL)
1919 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001920
Per Forlind49278e2010-12-20 18:31:38 +01001921 if (d40_phy_buf_to_lli(d40d->lli_phy.dst,
Linus Walleij8d318a52010-03-30 15:33:42 +02001922 dst,
1923 size,
1924 d40c->dma_cfg.dst_info.psize,
1925 0,
1926 d40c->dst_def_cfg,
1927 true,
1928 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001929 d40c->dma_cfg.src_info.data_width,
1930 false) == NULL)
1931 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001932
1933 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1934 d40d->lli_pool.size, DMA_TO_DEVICE);
1935 }
1936
Jonas Aaberg2a614342010-06-20 21:25:24 +00001937 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001938 return &d40d->txd;
1939
Linus Walleij8d318a52010-03-30 15:33:42 +02001940err:
Rabin Vincent819504f2010-10-06 08:20:38 +00001941 if (d40d)
1942 d40_desc_free(d40c, d40d);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001943 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001944 return NULL;
1945}
1946
Ira Snyder0d688662010-09-30 11:46:47 +00001947static struct dma_async_tx_descriptor *
1948d40_prep_sg(struct dma_chan *chan,
1949 struct scatterlist *dst_sg, unsigned int dst_nents,
1950 struct scatterlist *src_sg, unsigned int src_nents,
1951 unsigned long dma_flags)
1952{
1953 if (dst_nents != src_nents)
1954 return NULL;
1955
1956 return stedma40_memcpy_sg(chan, dst_sg, src_sg, dst_nents, dma_flags);
1957}
1958
Linus Walleij8d318a52010-03-30 15:33:42 +02001959static int d40_prep_slave_sg_log(struct d40_desc *d40d,
1960 struct d40_chan *d40c,
1961 struct scatterlist *sgl,
1962 unsigned int sg_len,
1963 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001964 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001965{
1966 dma_addr_t dev_addr = 0;
1967 int total_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001968
Per Forlind49278e2010-12-20 18:31:38 +01001969 d40d->lli_len = d40_sg_2_dmalen(sgl, sg_len,
1970 d40c->dma_cfg.src_info.data_width,
1971 d40c->dma_cfg.dst_info.data_width);
1972 if (d40d->lli_len < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001973 chan_err(d40c, "Unaligned size\n");
Per Forlind49278e2010-12-20 18:31:38 +01001974 return -EINVAL;
1975 }
1976
1977 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001978 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001979 return -ENOMEM;
1980 }
1981
Jonas Aaberg698e4732010-08-09 12:08:56 +00001982 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001983
Jonas Aaberg2a614342010-06-20 21:25:24 +00001984 if (direction == DMA_FROM_DEVICE)
Linus Walleij95e14002010-08-04 13:37:45 +02001985 if (d40c->runtime_addr)
1986 dev_addr = d40c->runtime_addr;
1987 else
1988 dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Jonas Aaberg2a614342010-06-20 21:25:24 +00001989 else if (direction == DMA_TO_DEVICE)
Linus Walleij95e14002010-08-04 13:37:45 +02001990 if (d40c->runtime_addr)
1991 dev_addr = d40c->runtime_addr;
1992 else
1993 dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
1994
Jonas Aaberg2a614342010-06-20 21:25:24 +00001995 else
Linus Walleij8d318a52010-03-30 15:33:42 +02001996 return -EINVAL;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001997
Jonas Aaberg698e4732010-08-09 12:08:56 +00001998 total_size = d40_log_sg_to_dev(sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001999 &d40d->lli_log,
2000 &d40c->log_def,
2001 d40c->dma_cfg.src_info.data_width,
2002 d40c->dma_cfg.dst_info.data_width,
2003 direction,
Jonas Aaberg698e4732010-08-09 12:08:56 +00002004 dev_addr);
Jonas Aaberg2a614342010-06-20 21:25:24 +00002005
Linus Walleij8d318a52010-03-30 15:33:42 +02002006 if (total_size < 0)
2007 return -EINVAL;
2008
2009 return 0;
2010}
2011
2012static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
2013 struct d40_chan *d40c,
2014 struct scatterlist *sgl,
2015 unsigned int sgl_len,
2016 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002017 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002018{
2019 dma_addr_t src_dev_addr;
2020 dma_addr_t dst_dev_addr;
2021 int res;
2022
Per Forlind49278e2010-12-20 18:31:38 +01002023 d40d->lli_len = d40_sg_2_dmalen(sgl, sgl_len,
2024 d40c->dma_cfg.src_info.data_width,
2025 d40c->dma_cfg.dst_info.data_width);
2026 if (d40d->lli_len < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002027 chan_err(d40c, "Unaligned size\n");
Per Forlind49278e2010-12-20 18:31:38 +01002028 return -EINVAL;
2029 }
2030
2031 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002032 chan_err(d40c, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002033 return -ENOMEM;
2034 }
2035
Jonas Aaberg698e4732010-08-09 12:08:56 +00002036 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002037
2038 if (direction == DMA_FROM_DEVICE) {
2039 dst_dev_addr = 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002040 if (d40c->runtime_addr)
2041 src_dev_addr = d40c->runtime_addr;
2042 else
2043 src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002044 } else if (direction == DMA_TO_DEVICE) {
Linus Walleij95e14002010-08-04 13:37:45 +02002045 if (d40c->runtime_addr)
2046 dst_dev_addr = d40c->runtime_addr;
2047 else
2048 dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002049 src_dev_addr = 0;
2050 } else
2051 return -EINVAL;
2052
2053 res = d40_phy_sg_to_lli(sgl,
2054 sgl_len,
2055 src_dev_addr,
2056 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002057 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02002058 d40c->src_def_cfg,
2059 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01002060 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002061 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002062 if (res < 0)
2063 return res;
2064
2065 res = d40_phy_sg_to_lli(sgl,
2066 sgl_len,
2067 dst_dev_addr,
2068 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002069 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02002070 d40c->dst_def_cfg,
2071 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01002072 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002073 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002074 if (res < 0)
2075 return res;
2076
2077 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
2078 d40d->lli_pool.size, DMA_TO_DEVICE);
2079 return 0;
2080}
2081
2082static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2083 struct scatterlist *sgl,
2084 unsigned int sg_len,
2085 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002086 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002087{
2088 struct d40_desc *d40d;
2089 struct d40_chan *d40c = container_of(chan, struct d40_chan,
2090 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00002091 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002092 int err;
2093
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002094 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002095 chan_err(d40c, "Cannot prepare unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002096 return ERR_PTR(-EINVAL);
2097 }
2098
Jonas Aaberg2a614342010-06-20 21:25:24 +00002099 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002100 d40d = d40_desc_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002101
2102 if (d40d == NULL)
Rabin Vincent819504f2010-10-06 08:20:38 +00002103 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002104
Rabin Vincent724a8572011-01-25 11:18:08 +01002105 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02002106 err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002107 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002108 else
2109 err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002110 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002111 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002112 chan_err(d40c, "Failed to prepare %s slave sg job: %d\n",
Rabin Vincent724a8572011-01-25 11:18:08 +01002113 chan_is_logical(d40c) ? "log" : "phy", err);
Rabin Vincent819504f2010-10-06 08:20:38 +00002114 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002115 }
2116
Jonas Aaberg2a614342010-06-20 21:25:24 +00002117 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002118
2119 dma_async_tx_descriptor_init(&d40d->txd, chan);
2120
2121 d40d->txd.tx_submit = d40_tx_submit;
2122
Rabin Vincent819504f2010-10-06 08:20:38 +00002123 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002124 return &d40d->txd;
Rabin Vincent819504f2010-10-06 08:20:38 +00002125
2126err:
2127 if (d40d)
2128 d40_desc_free(d40c, d40d);
2129 spin_unlock_irqrestore(&d40c->lock, flags);
2130 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02002131}
2132
2133static enum dma_status d40_tx_status(struct dma_chan *chan,
2134 dma_cookie_t cookie,
2135 struct dma_tx_state *txstate)
2136{
2137 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2138 dma_cookie_t last_used;
2139 dma_cookie_t last_complete;
2140 int ret;
2141
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002142 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002143 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002144 return -EINVAL;
2145 }
2146
Linus Walleij8d318a52010-03-30 15:33:42 +02002147 last_complete = d40c->completed;
2148 last_used = chan->cookie;
2149
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002150 if (d40_is_paused(d40c))
2151 ret = DMA_PAUSED;
2152 else
2153 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002154
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002155 dma_set_tx_state(txstate, last_complete, last_used,
2156 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002157
2158 return ret;
2159}
2160
2161static void d40_issue_pending(struct dma_chan *chan)
2162{
2163 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2164 unsigned long flags;
2165
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002166 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002167 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002168 return;
2169 }
2170
Linus Walleij8d318a52010-03-30 15:33:42 +02002171 spin_lock_irqsave(&d40c->lock, flags);
2172
2173 /* Busy means that pending jobs are already being processed */
2174 if (!d40c->busy)
2175 (void) d40_queue_start(d40c);
2176
2177 spin_unlock_irqrestore(&d40c->lock, flags);
2178}
2179
Linus Walleij95e14002010-08-04 13:37:45 +02002180/* Runtime reconfiguration extension */
2181static void d40_set_runtime_config(struct dma_chan *chan,
2182 struct dma_slave_config *config)
2183{
2184 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2185 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2186 enum dma_slave_buswidth config_addr_width;
2187 dma_addr_t config_addr;
2188 u32 config_maxburst;
2189 enum stedma40_periph_data_width addr_width;
2190 int psize;
2191
2192 if (config->direction == DMA_FROM_DEVICE) {
2193 dma_addr_t dev_addr_rx =
2194 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2195
2196 config_addr = config->src_addr;
2197 if (dev_addr_rx)
2198 dev_dbg(d40c->base->dev,
2199 "channel has a pre-wired RX address %08x "
2200 "overriding with %08x\n",
2201 dev_addr_rx, config_addr);
2202 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2203 dev_dbg(d40c->base->dev,
2204 "channel was not configured for peripheral "
2205 "to memory transfer (%d) overriding\n",
2206 cfg->dir);
2207 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2208
2209 config_addr_width = config->src_addr_width;
2210 config_maxburst = config->src_maxburst;
2211
2212 } else if (config->direction == DMA_TO_DEVICE) {
2213 dma_addr_t dev_addr_tx =
2214 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2215
2216 config_addr = config->dst_addr;
2217 if (dev_addr_tx)
2218 dev_dbg(d40c->base->dev,
2219 "channel has a pre-wired TX address %08x "
2220 "overriding with %08x\n",
2221 dev_addr_tx, config_addr);
2222 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2223 dev_dbg(d40c->base->dev,
2224 "channel was not configured for memory "
2225 "to peripheral transfer (%d) overriding\n",
2226 cfg->dir);
2227 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2228
2229 config_addr_width = config->dst_addr_width;
2230 config_maxburst = config->dst_maxburst;
2231
2232 } else {
2233 dev_err(d40c->base->dev,
2234 "unrecognized channel direction %d\n",
2235 config->direction);
2236 return;
2237 }
2238
2239 switch (config_addr_width) {
2240 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2241 addr_width = STEDMA40_BYTE_WIDTH;
2242 break;
2243 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2244 addr_width = STEDMA40_HALFWORD_WIDTH;
2245 break;
2246 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2247 addr_width = STEDMA40_WORD_WIDTH;
2248 break;
2249 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2250 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2251 break;
2252 default:
2253 dev_err(d40c->base->dev,
2254 "illegal peripheral address width "
2255 "requested (%d)\n",
2256 config->src_addr_width);
2257 return;
2258 }
2259
Rabin Vincent724a8572011-01-25 11:18:08 +01002260 if (chan_is_logical(d40c)) {
Per Forlina59670a2010-10-06 09:05:27 +00002261 if (config_maxburst >= 16)
2262 psize = STEDMA40_PSIZE_LOG_16;
2263 else if (config_maxburst >= 8)
2264 psize = STEDMA40_PSIZE_LOG_8;
2265 else if (config_maxburst >= 4)
2266 psize = STEDMA40_PSIZE_LOG_4;
2267 else
2268 psize = STEDMA40_PSIZE_LOG_1;
2269 } else {
2270 if (config_maxburst >= 16)
2271 psize = STEDMA40_PSIZE_PHY_16;
2272 else if (config_maxburst >= 8)
2273 psize = STEDMA40_PSIZE_PHY_8;
2274 else if (config_maxburst >= 4)
2275 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002276 else if (config_maxburst >= 2)
2277 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002278 else
2279 psize = STEDMA40_PSIZE_PHY_1;
2280 }
Linus Walleij95e14002010-08-04 13:37:45 +02002281
2282 /* Set up all the endpoint configs */
2283 cfg->src_info.data_width = addr_width;
2284 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002285 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002286 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2287 cfg->dst_info.data_width = addr_width;
2288 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002289 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002290 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2291
Per Forlina59670a2010-10-06 09:05:27 +00002292 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002293 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002294 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2295 else
2296 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2297 &d40c->dst_def_cfg, false);
2298
Linus Walleij95e14002010-08-04 13:37:45 +02002299 /* These settings will take precedence later */
2300 d40c->runtime_addr = config_addr;
2301 d40c->runtime_direction = config->direction;
2302 dev_dbg(d40c->base->dev,
2303 "configured channel %s for %s, data width %d, "
2304 "maxburst %d bytes, LE, no flow control\n",
2305 dma_chan_name(chan),
2306 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2307 config_addr_width,
2308 config_maxburst);
2309}
2310
Linus Walleij05827632010-05-17 16:30:42 -07002311static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2312 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002313{
2314 unsigned long flags;
2315 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2316
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002317 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002318 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002319 return -EINVAL;
2320 }
2321
Linus Walleij8d318a52010-03-30 15:33:42 +02002322 switch (cmd) {
2323 case DMA_TERMINATE_ALL:
2324 spin_lock_irqsave(&d40c->lock, flags);
2325 d40_term_all(d40c);
2326 spin_unlock_irqrestore(&d40c->lock, flags);
2327 return 0;
2328 case DMA_PAUSE:
2329 return d40_pause(chan);
2330 case DMA_RESUME:
2331 return d40_resume(chan);
Linus Walleij95e14002010-08-04 13:37:45 +02002332 case DMA_SLAVE_CONFIG:
2333 d40_set_runtime_config(chan,
2334 (struct dma_slave_config *) arg);
2335 return 0;
2336 default:
2337 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002338 }
2339
2340 /* Other commands are unimplemented */
2341 return -ENXIO;
2342}
2343
2344/* Initialization functions */
2345
2346static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2347 struct d40_chan *chans, int offset,
2348 int num_chans)
2349{
2350 int i = 0;
2351 struct d40_chan *d40c;
2352
2353 INIT_LIST_HEAD(&dma->channels);
2354
2355 for (i = offset; i < offset + num_chans; i++) {
2356 d40c = &chans[i];
2357 d40c->base = base;
2358 d40c->chan.device = dma;
2359
Linus Walleij8d318a52010-03-30 15:33:42 +02002360 spin_lock_init(&d40c->lock);
2361
2362 d40c->log_num = D40_PHY_CHAN;
2363
Linus Walleij8d318a52010-03-30 15:33:42 +02002364 INIT_LIST_HEAD(&d40c->active);
2365 INIT_LIST_HEAD(&d40c->queue);
2366 INIT_LIST_HEAD(&d40c->client);
2367
Linus Walleij8d318a52010-03-30 15:33:42 +02002368 tasklet_init(&d40c->tasklet, dma_tasklet,
2369 (unsigned long) d40c);
2370
2371 list_add_tail(&d40c->chan.device_node,
2372 &dma->channels);
2373 }
2374}
2375
2376static int __init d40_dmaengine_init(struct d40_base *base,
2377 int num_reserved_chans)
2378{
2379 int err ;
2380
2381 d40_chan_init(base, &base->dma_slave, base->log_chans,
2382 0, base->num_log_chans);
2383
2384 dma_cap_zero(base->dma_slave.cap_mask);
2385 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2386
2387 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2388 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2389 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002390 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002391 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2392 base->dma_slave.device_tx_status = d40_tx_status;
2393 base->dma_slave.device_issue_pending = d40_issue_pending;
2394 base->dma_slave.device_control = d40_control;
2395 base->dma_slave.dev = base->dev;
2396
2397 err = dma_async_device_register(&base->dma_slave);
2398
2399 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002400 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002401 goto failure1;
2402 }
2403
2404 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2405 base->num_log_chans, base->plat_data->memcpy_len);
2406
2407 dma_cap_zero(base->dma_memcpy.cap_mask);
2408 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002409 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002410
2411 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2412 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2413 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002414 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002415 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2416 base->dma_memcpy.device_tx_status = d40_tx_status;
2417 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2418 base->dma_memcpy.device_control = d40_control;
2419 base->dma_memcpy.dev = base->dev;
2420 /*
2421 * This controller can only access address at even
2422 * 32bit boundaries, i.e. 2^2
2423 */
2424 base->dma_memcpy.copy_align = 2;
2425
2426 err = dma_async_device_register(&base->dma_memcpy);
2427
2428 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002429 d40_err(base->dev,
2430 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002431 goto failure2;
2432 }
2433
2434 d40_chan_init(base, &base->dma_both, base->phy_chans,
2435 0, num_reserved_chans);
2436
2437 dma_cap_zero(base->dma_both.cap_mask);
2438 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2439 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002440 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002441
2442 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2443 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2444 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002445 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002446 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2447 base->dma_both.device_tx_status = d40_tx_status;
2448 base->dma_both.device_issue_pending = d40_issue_pending;
2449 base->dma_both.device_control = d40_control;
2450 base->dma_both.dev = base->dev;
2451 base->dma_both.copy_align = 2;
2452 err = dma_async_device_register(&base->dma_both);
2453
2454 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002455 d40_err(base->dev,
2456 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002457 goto failure3;
2458 }
2459 return 0;
2460failure3:
2461 dma_async_device_unregister(&base->dma_memcpy);
2462failure2:
2463 dma_async_device_unregister(&base->dma_slave);
2464failure1:
2465 return err;
2466}
2467
2468/* Initialization functions. */
2469
2470static int __init d40_phy_res_init(struct d40_base *base)
2471{
2472 int i;
2473 int num_phy_chans_avail = 0;
2474 u32 val[2];
2475 int odd_even_bit = -2;
2476
2477 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2478 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2479
2480 for (i = 0; i < base->num_phy_chans; i++) {
2481 base->phy_res[i].num = i;
2482 odd_even_bit += 2 * ((i % 2) == 0);
2483 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2484 /* Mark security only channels as occupied */
2485 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2486 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2487 } else {
2488 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2489 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2490 num_phy_chans_avail++;
2491 }
2492 spin_lock_init(&base->phy_res[i].lock);
2493 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002494
2495 /* Mark disabled channels as occupied */
2496 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002497 int chan = base->plat_data->disabled_channels[i];
2498
2499 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2500 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2501 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002502 }
2503
Linus Walleij8d318a52010-03-30 15:33:42 +02002504 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2505 num_phy_chans_avail, base->num_phy_chans);
2506
2507 /* Verify settings extended vs standard */
2508 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2509
2510 for (i = 0; i < base->num_phy_chans; i++) {
2511
2512 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2513 (val[0] & 0x3) != 1)
2514 dev_info(base->dev,
2515 "[%s] INFO: channel %d is misconfigured (%d)\n",
2516 __func__, i, val[0] & 0x3);
2517
2518 val[0] = val[0] >> 2;
2519 }
2520
2521 return num_phy_chans_avail;
2522}
2523
2524static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2525{
2526 static const struct d40_reg_val dma_id_regs[] = {
2527 /* Peripheral Id */
2528 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2529 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2530 /*
2531 * D40_DREG_PERIPHID2 Depends on HW revision:
Rabin Vincent4d594902011-01-25 11:18:10 +01002532 * DB8500ed has 0x0008,
Linus Walleij8d318a52010-03-30 15:33:42 +02002533 * ? has 0x0018,
Rabin Vincent4d594902011-01-25 11:18:10 +01002534 * DB8500v1 has 0x0028
2535 * DB8500v2 has 0x0038
Linus Walleij8d318a52010-03-30 15:33:42 +02002536 */
2537 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2538
2539 /* PCell Id */
2540 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2541 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2542 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2543 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2544 };
2545 struct stedma40_platform_data *plat_data;
2546 struct clk *clk = NULL;
2547 void __iomem *virtbase = NULL;
2548 struct resource *res = NULL;
2549 struct d40_base *base = NULL;
2550 int num_log_chans = 0;
2551 int num_phy_chans;
2552 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002553 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002554 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002555
2556 clk = clk_get(&pdev->dev, NULL);
2557
2558 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002559 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002560 goto failure;
2561 }
2562
2563 clk_enable(clk);
2564
2565 /* Get IO for DMAC base address */
2566 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2567 if (!res)
2568 goto failure;
2569
2570 if (request_mem_region(res->start, resource_size(res),
2571 D40_NAME " I/O base") == NULL)
2572 goto failure;
2573
2574 virtbase = ioremap(res->start, resource_size(res));
2575 if (!virtbase)
2576 goto failure;
2577
2578 /* HW version check */
2579 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2580 if (dma_id_regs[i].val !=
2581 readl(virtbase + dma_id_regs[i].reg)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002582 d40_err(&pdev->dev,
2583 "Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002584 dma_id_regs[i].val,
2585 dma_id_regs[i].reg,
2586 readl(virtbase + dma_id_regs[i].reg));
2587 goto failure;
2588 }
2589 }
2590
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002591 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002592 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002593
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002594 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2595 D40_HW_DESIGNER) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002596 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2597 val & D40_DREG_PERIPHID2_DESIGNER_MASK,
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002598 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002599 goto failure;
2600 }
2601
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002602 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2603 D40_DREG_PERIPHID2_REV_POS;
2604
Linus Walleij8d318a52010-03-30 15:33:42 +02002605 /* The number of physical channels on this HW */
2606 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2607
2608 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002609 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002610
2611 plat_data = pdev->dev.platform_data;
2612
2613 /* Count the number of logical channels in use */
2614 for (i = 0; i < plat_data->dev_len; i++)
2615 if (plat_data->dev_rx[i] != 0)
2616 num_log_chans++;
2617
2618 for (i = 0; i < plat_data->dev_len; i++)
2619 if (plat_data->dev_tx[i] != 0)
2620 num_log_chans++;
2621
2622 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2623 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2624 sizeof(struct d40_chan), GFP_KERNEL);
2625
2626 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002627 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002628 goto failure;
2629 }
2630
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002631 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002632 base->clk = clk;
2633 base->num_phy_chans = num_phy_chans;
2634 base->num_log_chans = num_log_chans;
2635 base->phy_start = res->start;
2636 base->phy_size = resource_size(res);
2637 base->virtbase = virtbase;
2638 base->plat_data = plat_data;
2639 base->dev = &pdev->dev;
2640 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2641 base->log_chans = &base->phy_chans[num_phy_chans];
2642
2643 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2644 GFP_KERNEL);
2645 if (!base->phy_res)
2646 goto failure;
2647
2648 base->lookup_phy_chans = kzalloc(num_phy_chans *
2649 sizeof(struct d40_chan *),
2650 GFP_KERNEL);
2651 if (!base->lookup_phy_chans)
2652 goto failure;
2653
2654 if (num_log_chans + plat_data->memcpy_len) {
2655 /*
2656 * The max number of logical channels are event lines for all
2657 * src devices and dst devices
2658 */
2659 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2660 sizeof(struct d40_chan *),
2661 GFP_KERNEL);
2662 if (!base->lookup_log_chans)
2663 goto failure;
2664 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002665
2666 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2667 sizeof(struct d40_desc *) *
2668 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002669 GFP_KERNEL);
2670 if (!base->lcla_pool.alloc_map)
2671 goto failure;
2672
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002673 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2674 0, SLAB_HWCACHE_ALIGN,
2675 NULL);
2676 if (base->desc_slab == NULL)
2677 goto failure;
2678
Linus Walleij8d318a52010-03-30 15:33:42 +02002679 return base;
2680
2681failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002682 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002683 clk_disable(clk);
2684 clk_put(clk);
2685 }
2686 if (virtbase)
2687 iounmap(virtbase);
2688 if (res)
2689 release_mem_region(res->start,
2690 resource_size(res));
2691 if (virtbase)
2692 iounmap(virtbase);
2693
2694 if (base) {
2695 kfree(base->lcla_pool.alloc_map);
2696 kfree(base->lookup_log_chans);
2697 kfree(base->lookup_phy_chans);
2698 kfree(base->phy_res);
2699 kfree(base);
2700 }
2701
2702 return NULL;
2703}
2704
2705static void __init d40_hw_init(struct d40_base *base)
2706{
2707
2708 static const struct d40_reg_val dma_init_reg[] = {
2709 /* Clock every part of the DMA block from start */
2710 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2711
2712 /* Interrupts on all logical channels */
2713 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2714 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2715 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2716 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2717 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2718 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2719 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2720 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2721 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2722 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2723 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2724 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2725 };
2726 int i;
2727 u32 prmseo[2] = {0, 0};
2728 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2729 u32 pcmis = 0;
2730 u32 pcicr = 0;
2731
2732 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2733 writel(dma_init_reg[i].val,
2734 base->virtbase + dma_init_reg[i].reg);
2735
2736 /* Configure all our dma channels to default settings */
2737 for (i = 0; i < base->num_phy_chans; i++) {
2738
2739 activeo[i % 2] = activeo[i % 2] << 2;
2740
2741 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2742 == D40_ALLOC_PHY) {
2743 activeo[i % 2] |= 3;
2744 continue;
2745 }
2746
2747 /* Enable interrupt # */
2748 pcmis = (pcmis << 1) | 1;
2749
2750 /* Clear interrupt # */
2751 pcicr = (pcicr << 1) | 1;
2752
2753 /* Set channel to physical mode */
2754 prmseo[i % 2] = prmseo[i % 2] << 2;
2755 prmseo[i % 2] |= 1;
2756
2757 }
2758
2759 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2760 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2761 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2762 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2763
2764 /* Write which interrupt to enable */
2765 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2766
2767 /* Write which interrupt to clear */
2768 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2769
2770}
2771
Linus Walleij508849a2010-06-20 21:26:07 +00002772static int __init d40_lcla_allocate(struct d40_base *base)
2773{
2774 unsigned long *page_list;
2775 int i, j;
2776 int ret = 0;
2777
2778 /*
2779 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2780 * To full fill this hardware requirement without wasting 256 kb
2781 * we allocate pages until we get an aligned one.
2782 */
2783 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2784 GFP_KERNEL);
2785
2786 if (!page_list) {
2787 ret = -ENOMEM;
2788 goto failure;
2789 }
2790
2791 /* Calculating how many pages that are required */
2792 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2793
2794 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2795 page_list[i] = __get_free_pages(GFP_KERNEL,
2796 base->lcla_pool.pages);
2797 if (!page_list[i]) {
2798
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002799 d40_err(base->dev, "Failed to allocate %d pages.\n",
2800 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002801
2802 for (j = 0; j < i; j++)
2803 free_pages(page_list[j], base->lcla_pool.pages);
2804 goto failure;
2805 }
2806
2807 if ((virt_to_phys((void *)page_list[i]) &
2808 (LCLA_ALIGNMENT - 1)) == 0)
2809 break;
2810 }
2811
2812 for (j = 0; j < i; j++)
2813 free_pages(page_list[j], base->lcla_pool.pages);
2814
2815 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2816 base->lcla_pool.base = (void *)page_list[i];
2817 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002818 /*
2819 * After many attempts and no succees with finding the correct
2820 * alignment, try with allocating a big buffer.
2821 */
Linus Walleij508849a2010-06-20 21:26:07 +00002822 dev_warn(base->dev,
2823 "[%s] Failed to get %d pages @ 18 bit align.\n",
2824 __func__, base->lcla_pool.pages);
2825 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2826 base->num_phy_chans +
2827 LCLA_ALIGNMENT,
2828 GFP_KERNEL);
2829 if (!base->lcla_pool.base_unaligned) {
2830 ret = -ENOMEM;
2831 goto failure;
2832 }
2833
2834 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2835 LCLA_ALIGNMENT);
2836 }
2837
2838 writel(virt_to_phys(base->lcla_pool.base),
2839 base->virtbase + D40_DREG_LCLA);
2840failure:
2841 kfree(page_list);
2842 return ret;
2843}
2844
Linus Walleij8d318a52010-03-30 15:33:42 +02002845static int __init d40_probe(struct platform_device *pdev)
2846{
2847 int err;
2848 int ret = -ENOENT;
2849 struct d40_base *base;
2850 struct resource *res = NULL;
2851 int num_reserved_chans;
2852 u32 val;
2853
2854 base = d40_hw_detect_init(pdev);
2855
2856 if (!base)
2857 goto failure;
2858
2859 num_reserved_chans = d40_phy_res_init(base);
2860
2861 platform_set_drvdata(pdev, base);
2862
2863 spin_lock_init(&base->interrupt_lock);
2864 spin_lock_init(&base->execmd_lock);
2865
2866 /* Get IO for logical channel parameter address */
2867 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2868 if (!res) {
2869 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002870 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002871 goto failure;
2872 }
2873 base->lcpa_size = resource_size(res);
2874 base->phy_lcpa = res->start;
2875
2876 if (request_mem_region(res->start, resource_size(res),
2877 D40_NAME " I/O lcpa") == NULL) {
2878 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002879 d40_err(&pdev->dev,
2880 "Failed to request LCPA region 0x%x-0x%x\n",
2881 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002882 goto failure;
2883 }
2884
2885 /* We make use of ESRAM memory for this. */
2886 val = readl(base->virtbase + D40_DREG_LCPA);
2887 if (res->start != val && val != 0) {
2888 dev_warn(&pdev->dev,
2889 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2890 __func__, val, res->start);
2891 } else
2892 writel(res->start, base->virtbase + D40_DREG_LCPA);
2893
2894 base->lcpa_base = ioremap(res->start, resource_size(res));
2895 if (!base->lcpa_base) {
2896 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002897 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002898 goto failure;
2899 }
Linus Walleij508849a2010-06-20 21:26:07 +00002900
2901 ret = d40_lcla_allocate(base);
2902 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002903 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002904 goto failure;
2905 }
2906
Linus Walleij8d318a52010-03-30 15:33:42 +02002907 spin_lock_init(&base->lcla_pool.lock);
2908
Linus Walleij8d318a52010-03-30 15:33:42 +02002909 base->irq = platform_get_irq(pdev, 0);
2910
2911 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002912 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002913 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002914 goto failure;
2915 }
2916
2917 err = d40_dmaengine_init(base, num_reserved_chans);
2918 if (err)
2919 goto failure;
2920
2921 d40_hw_init(base);
2922
2923 dev_info(base->dev, "initialized\n");
2924 return 0;
2925
2926failure:
2927 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002928 if (base->desc_slab)
2929 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002930 if (base->virtbase)
2931 iounmap(base->virtbase);
Linus Walleij508849a2010-06-20 21:26:07 +00002932 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2933 free_pages((unsigned long)base->lcla_pool.base,
2934 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002935
2936 kfree(base->lcla_pool.base_unaligned);
2937
Linus Walleij8d318a52010-03-30 15:33:42 +02002938 if (base->phy_lcpa)
2939 release_mem_region(base->phy_lcpa,
2940 base->lcpa_size);
2941 if (base->phy_start)
2942 release_mem_region(base->phy_start,
2943 base->phy_size);
2944 if (base->clk) {
2945 clk_disable(base->clk);
2946 clk_put(base->clk);
2947 }
2948
2949 kfree(base->lcla_pool.alloc_map);
2950 kfree(base->lookup_log_chans);
2951 kfree(base->lookup_phy_chans);
2952 kfree(base->phy_res);
2953 kfree(base);
2954 }
2955
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002956 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002957 return ret;
2958}
2959
2960static struct platform_driver d40_driver = {
2961 .driver = {
2962 .owner = THIS_MODULE,
2963 .name = D40_NAME,
2964 },
2965};
2966
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002967static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002968{
2969 return platform_driver_probe(&d40_driver, d40_probe);
2970}
2971arch_initcall(stedma40_init);