blob: 91d5ed7c79ba183c3c7ce2bf624b19fe07157329 [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.
Rabin Vincentb00f9382011-01-25 11:18:15 +010071 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +020072 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
73 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
74 * one buffer to one buffer.
75 */
76struct d40_lli_pool {
77 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000078 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +010079 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +020080 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000081 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020082};
83
84/**
85 * struct d40_desc - A descriptor is one DMA job.
86 *
87 * @lli_phy: LLI settings for physical channel. Both src and dst=
88 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
89 * lli_len equals one.
90 * @lli_log: Same as above but for logical channels.
91 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000092 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030093 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +000094 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020095 * @txd: DMA engine struct. Used for among other things for communication
96 * during a transfer.
97 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020098 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000099 * the previous one.
Linus Walleij8d318a52010-03-30 15:33:42 +0200100 *
101 * This descriptor is used for both logical and physical transfers.
102 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200103struct d40_desc {
104 /* LLI physical */
105 struct d40_phy_lli_bidir lli_phy;
106 /* LLI logical */
107 struct d40_log_lli_bidir lli_log;
108
109 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000110 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000111 int lli_current;
112 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200113
114 struct dma_async_tx_descriptor txd;
115 struct list_head node;
116
Linus Walleij8d318a52010-03-30 15:33:42 +0200117 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100118 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200119};
120
121/**
122 * struct d40_lcla_pool - LCLA pool settings and data.
123 *
Linus Walleij508849a2010-06-20 21:26:07 +0000124 * @base: The virtual address of LCLA. 18 bit aligned.
125 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
126 * This pointer is only there for clean-up on error.
127 * @pages: The number of pages needed for all physical channels.
128 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200129 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000130 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200131 */
132struct d40_lcla_pool {
133 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100134 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000135 void *base_unaligned;
136 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200137 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000138 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200139};
140
141/**
142 * struct d40_phy_res - struct for handling eventlines mapped to physical
143 * channels.
144 *
145 * @lock: A lock protection this entity.
146 * @num: The physical channel number of this entity.
147 * @allocated_src: Bit mapped to show which src event line's are mapped to
148 * this physical channel. Can also be free or physically allocated.
149 * @allocated_dst: Same as for src but is dst.
150 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000151 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200152 */
153struct d40_phy_res {
154 spinlock_t lock;
155 int num;
156 u32 allocated_src;
157 u32 allocated_dst;
158};
159
160struct d40_base;
161
162/**
163 * struct d40_chan - Struct that describes a channel.
164 *
165 * @lock: A spinlock to protect this struct.
166 * @log_num: The logical number, if any of this channel.
167 * @completed: Starts with 1, after first interrupt it is set to dma engine's
168 * current cookie.
169 * @pending_tx: The number of pending transfers. Used between interrupt handler
170 * and tasklet.
171 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000172 * @phy_chan: Pointer to physical channel which this instance runs on. If this
173 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200174 * @chan: DMA engine handle.
175 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
176 * transfer and call client callback.
177 * @client: Cliented owned descriptor list.
178 * @active: Active descriptor.
179 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200180 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000181 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200182 * @base: Pointer to the device instance struct.
183 * @src_def_cfg: Default cfg register setting for src.
184 * @dst_def_cfg: Default cfg register setting for dst.
185 * @log_def: Default logical channel settings.
186 * @lcla: Space for one dst src pair for logical channel transfers.
187 * @lcpa: Pointer to dst and src lcpa settings.
188 *
189 * This struct can either "be" a logical or a physical channel.
190 */
191struct d40_chan {
192 spinlock_t lock;
193 int log_num;
194 /* ID of the most recent completed transfer */
195 int completed;
196 int pending_tx;
197 bool busy;
198 struct d40_phy_res *phy_chan;
199 struct dma_chan chan;
200 struct tasklet_struct tasklet;
201 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200202 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200203 struct list_head active;
204 struct list_head queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200205 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000206 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200207 struct d40_base *base;
208 /* Default register configurations */
209 u32 src_def_cfg;
210 u32 dst_def_cfg;
211 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200212 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200213 /* Runtime reconfiguration */
214 dma_addr_t runtime_addr;
215 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200216};
217
218/**
219 * struct d40_base - The big global struct, one for each probe'd instance.
220 *
221 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
222 * @execmd_lock: Lock for execute command usage since several channels share
223 * the same physical register.
224 * @dev: The device structure.
225 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700226 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200227 * @clk: Pointer to the DMA clock structure.
228 * @phy_start: Physical memory start of the DMA registers.
229 * @phy_size: Size of the DMA register map.
230 * @irq: The IRQ number.
231 * @num_phy_chans: The number of physical channels. Read from HW. This
232 * is the number of available channels for this driver, not counting "Secure
233 * mode" allocated physical channels.
234 * @num_log_chans: The number of logical channels. Calculated from
235 * num_phy_chans.
236 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
237 * @dma_slave: dma_device channels that can do only do slave transfers.
238 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200239 * @log_chans: Room for all possible logical channels in system.
240 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
241 * to log_chans entries.
242 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
243 * to phy_chans entries.
244 * @plat_data: Pointer to provided platform_data which is the driver
245 * configuration.
246 * @phy_res: Vector containing all physical channels.
247 * @lcla_pool: lcla pool settings and data.
248 * @lcpa_base: The virtual mapped address of LCPA.
249 * @phy_lcpa: The physical address of the LCPA.
250 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000251 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200252 */
253struct d40_base {
254 spinlock_t interrupt_lock;
255 spinlock_t execmd_lock;
256 struct device *dev;
257 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700258 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200259 struct clk *clk;
260 phys_addr_t phy_start;
261 resource_size_t phy_size;
262 int irq;
263 int num_phy_chans;
264 int num_log_chans;
265 struct dma_device dma_both;
266 struct dma_device dma_slave;
267 struct dma_device dma_memcpy;
268 struct d40_chan *phy_chans;
269 struct d40_chan *log_chans;
270 struct d40_chan **lookup_log_chans;
271 struct d40_chan **lookup_phy_chans;
272 struct stedma40_platform_data *plat_data;
273 /* Physical half channels */
274 struct d40_phy_res *phy_res;
275 struct d40_lcla_pool lcla_pool;
276 void *lcpa_base;
277 dma_addr_t phy_lcpa;
278 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000279 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200280};
281
282/**
283 * struct d40_interrupt_lookup - lookup table for interrupt handler
284 *
285 * @src: Interrupt mask register.
286 * @clr: Interrupt clear register.
287 * @is_error: true if this is an error interrupt.
288 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
289 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
290 */
291struct d40_interrupt_lookup {
292 u32 src;
293 u32 clr;
294 bool is_error;
295 int offset;
296};
297
298/**
299 * struct d40_reg_val - simple lookup struct
300 *
301 * @reg: The register.
302 * @val: The value that belongs to the register in reg.
303 */
304struct d40_reg_val {
305 unsigned int reg;
306 unsigned int val;
307};
308
Rabin Vincent262d2912011-01-25 11:18:05 +0100309static struct device *chan2dev(struct d40_chan *d40c)
310{
311 return &d40c->chan.dev->device;
312}
313
Rabin Vincent724a8572011-01-25 11:18:08 +0100314static bool chan_is_physical(struct d40_chan *chan)
315{
316 return chan->log_num == D40_PHY_CHAN;
317}
318
319static bool chan_is_logical(struct d40_chan *chan)
320{
321 return !chan_is_physical(chan);
322}
323
Rabin Vincent8ca84682011-01-25 11:18:07 +0100324static void __iomem *chan_base(struct d40_chan *chan)
325{
326 return chan->base->virtbase + D40_DREG_PCBASE +
327 chan->phy_chan->num * D40_DREG_PCDELTA;
328}
329
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100330#define d40_err(dev, format, arg...) \
331 dev_err(dev, "[%s] " format, __func__, ## arg)
332
333#define chan_err(d40c, format, arg...) \
334 d40_err(chan2dev(d40c), format, ## arg)
335
Rabin Vincentb00f9382011-01-25 11:18:15 +0100336static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100337 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200338{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100339 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200340 u32 align;
341 void *base;
342
343 if (is_log)
344 align = sizeof(struct d40_log_lli);
345 else
346 align = sizeof(struct d40_phy_lli);
347
348 if (lli_len == 1) {
349 base = d40d->lli_pool.pre_alloc_lli;
350 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
351 d40d->lli_pool.base = NULL;
352 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100353 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200354
355 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
356 d40d->lli_pool.base = base;
357
358 if (d40d->lli_pool.base == NULL)
359 return -ENOMEM;
360 }
361
362 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100363 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100364 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100365
366 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200367 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100368 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100369 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100370
371 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
372 d40d->lli_phy.src,
373 d40d->lli_pool.size,
374 DMA_TO_DEVICE);
375
376 if (dma_mapping_error(d40c->base->dev,
377 d40d->lli_pool.dma_addr)) {
378 kfree(d40d->lli_pool.base);
379 d40d->lli_pool.base = NULL;
380 d40d->lli_pool.dma_addr = 0;
381 return -ENOMEM;
382 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200383 }
384
385 return 0;
386}
387
Rabin Vincentb00f9382011-01-25 11:18:15 +0100388static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200389{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100390 if (d40d->lli_pool.dma_addr)
391 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
392 d40d->lli_pool.size, DMA_TO_DEVICE);
393
Linus Walleij8d318a52010-03-30 15:33:42 +0200394 kfree(d40d->lli_pool.base);
395 d40d->lli_pool.base = NULL;
396 d40d->lli_pool.size = 0;
397 d40d->lli_log.src = NULL;
398 d40d->lli_log.dst = NULL;
399 d40d->lli_phy.src = NULL;
400 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200401}
402
Jonas Aaberg698e4732010-08-09 12:08:56 +0000403static int d40_lcla_alloc_one(struct d40_chan *d40c,
404 struct d40_desc *d40d)
405{
406 unsigned long flags;
407 int i;
408 int ret = -EINVAL;
409 int p;
410
411 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
412
413 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
414
415 /*
416 * Allocate both src and dst at the same time, therefore the half
417 * start on 1 since 0 can't be used since zero is used as end marker.
418 */
419 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
420 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
421 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
422 d40d->lcla_alloc++;
423 ret = i;
424 break;
425 }
426 }
427
428 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
429
430 return ret;
431}
432
433static int d40_lcla_free_all(struct d40_chan *d40c,
434 struct d40_desc *d40d)
435{
436 unsigned long flags;
437 int i;
438 int ret = -EINVAL;
439
Rabin Vincent724a8572011-01-25 11:18:08 +0100440 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000441 return 0;
442
443 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
444
445 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
446 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
447 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
448 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
449 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
450 d40d->lcla_alloc--;
451 if (d40d->lcla_alloc == 0) {
452 ret = 0;
453 break;
454 }
455 }
456 }
457
458 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
459
460 return ret;
461
462}
463
Linus Walleij8d318a52010-03-30 15:33:42 +0200464static void d40_desc_remove(struct d40_desc *d40d)
465{
466 list_del(&d40d->node);
467}
468
469static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
470{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000471 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200472
473 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000474 struct d40_desc *d;
475 struct d40_desc *_d;
476
Linus Walleij8d318a52010-03-30 15:33:42 +0200477 list_for_each_entry_safe(d, _d, &d40c->client, node)
478 if (async_tx_test_ack(&d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +0100479 d40_pool_lli_free(d40c, d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200480 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000481 desc = d;
482 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000483 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200484 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200485 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000486
487 if (!desc)
488 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
489
490 if (desc)
491 INIT_LIST_HEAD(&desc->node);
492
493 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200494}
495
496static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
497{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000498
Rabin Vincentb00f9382011-01-25 11:18:15 +0100499 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000500 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000501 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200502}
503
504static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
505{
506 list_add_tail(&desc->node, &d40c->active);
507}
508
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100509static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
510{
511 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
512 struct d40_phy_lli *lli_src = desc->lli_phy.src;
513 void __iomem *base = chan_base(chan);
514
515 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
516 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
517 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
518 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
519
520 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
521 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
522 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
523 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
524}
525
Rabin Vincente65889c2011-01-25 11:18:31 +0100526static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
527{
528 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
529 struct d40_log_lli_bidir *lli = &desc->lli_log;
530 int lli_current = desc->lli_current;
531 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100532 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100533 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100534 int first_lcla = 0;
535 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100536
Rabin Vincent0c842b52011-01-25 11:18:35 +0100537 /*
538 * We may have partially running cyclic transfers, in case we did't get
539 * enough LCLA entries.
540 */
541 linkback = cyclic && lli_current == 0;
542
543 /*
544 * For linkback, we need one LCLA even with only one link, because we
545 * can't link back to the one in LCPA space
546 */
547 if (linkback || (lli_len - lli_current > 1)) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100548 curr_lcla = d40_lcla_alloc_one(chan, desc);
Rabin Vincent0c842b52011-01-25 11:18:35 +0100549 first_lcla = curr_lcla;
550 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100551
Rabin Vincent0c842b52011-01-25 11:18:35 +0100552 /*
553 * For linkback, we normally load the LCPA in the loop since we need to
554 * link it to the second LCLA and not the first. However, if we
555 * couldn't even get a first LCLA, then we have to run in LCPA and
556 * reload manually.
557 */
558 if (!linkback || curr_lcla == -EINVAL) {
559 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100560
Rabin Vincent0c842b52011-01-25 11:18:35 +0100561 if (curr_lcla == -EINVAL)
562 flags |= LLI_TERM_INT;
563
564 d40_log_lli_lcpa_write(chan->lcpa,
565 &lli->dst[lli_current],
566 &lli->src[lli_current],
567 curr_lcla,
568 flags);
569 lli_current++;
570 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100571
572 if (curr_lcla < 0)
573 goto out;
574
Rabin Vincente65889c2011-01-25 11:18:31 +0100575 for (; lli_current < lli_len; lli_current++) {
576 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
577 8 * curr_lcla * 2;
578 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100579 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100580 int next_lcla;
581
582 if (lli_current + 1 < lli_len)
583 next_lcla = d40_lcla_alloc_one(chan, desc);
584 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100585 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100586
Rabin Vincent0c842b52011-01-25 11:18:35 +0100587 if (cyclic || next_lcla == -EINVAL)
588 flags |= LLI_TERM_INT;
589
590 if (linkback && curr_lcla == first_lcla) {
591 /* First link goes in both LCPA and LCLA */
592 d40_log_lli_lcpa_write(chan->lcpa,
593 &lli->dst[lli_current],
594 &lli->src[lli_current],
595 next_lcla, flags);
596 }
597
598 /*
599 * One unused LCLA in the cyclic case if the very first
600 * next_lcla fails...
601 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100602 d40_log_lli_lcla_write(lcla,
603 &lli->dst[lli_current],
604 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100605 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100606
607 dma_sync_single_range_for_device(chan->base->dev,
608 pool->dma_addr, lcla_offset,
609 2 * sizeof(struct d40_log_lli),
610 DMA_TO_DEVICE);
611
612 curr_lcla = next_lcla;
613
Rabin Vincent0c842b52011-01-25 11:18:35 +0100614 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100615 lli_current++;
616 break;
617 }
618 }
619
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100620out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100621 desc->lli_current = lli_current;
622}
623
Jonas Aaberg698e4732010-08-09 12:08:56 +0000624static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
625{
Rabin Vincent724a8572011-01-25 11:18:08 +0100626 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100627 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000628 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100629 } else
630 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000631}
632
Linus Walleij8d318a52010-03-30 15:33:42 +0200633static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
634{
635 struct d40_desc *d;
636
637 if (list_empty(&d40c->active))
638 return NULL;
639
640 d = list_first_entry(&d40c->active,
641 struct d40_desc,
642 node);
643 return d;
644}
645
646static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
647{
Per Forlina8f30672011-06-26 23:29:52 +0200648 list_add_tail(&desc->node, &d40c->pending_queue);
649}
650
651static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
652{
653 struct d40_desc *d;
654
655 if (list_empty(&d40c->pending_queue))
656 return NULL;
657
658 d = list_first_entry(&d40c->pending_queue,
659 struct d40_desc,
660 node);
661 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200662}
663
664static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
665{
666 struct d40_desc *d;
667
668 if (list_empty(&d40c->queue))
669 return NULL;
670
671 d = list_first_entry(&d40c->queue,
672 struct d40_desc,
673 node);
674 return d;
675}
676
Per Forlind49278e2010-12-20 18:31:38 +0100677static int d40_psize_2_burst_size(bool is_log, int psize)
678{
679 if (is_log) {
680 if (psize == STEDMA40_PSIZE_LOG_1)
681 return 1;
682 } else {
683 if (psize == STEDMA40_PSIZE_PHY_1)
684 return 1;
685 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200686
Per Forlind49278e2010-12-20 18:31:38 +0100687 return 2 << psize;
688}
689
690/*
691 * The dma only supports transmitting packages up to
692 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
693 * dma elements required to send the entire sg list
694 */
695static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
696{
697 int dmalen;
698 u32 max_w = max(data_width1, data_width2);
699 u32 min_w = min(data_width1, data_width2);
700 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
701
702 if (seg_max > STEDMA40_MAX_SEG_SIZE)
703 seg_max -= (1 << max_w);
704
705 if (!IS_ALIGNED(size, 1 << max_w))
706 return -EINVAL;
707
708 if (size <= seg_max)
709 dmalen = 1;
710 else {
711 dmalen = size / seg_max;
712 if (dmalen * seg_max < size)
713 dmalen++;
714 }
715 return dmalen;
716}
717
718static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
719 u32 data_width1, u32 data_width2)
720{
721 struct scatterlist *sg;
722 int i;
723 int len = 0;
724 int ret;
725
726 for_each_sg(sgl, sg, sg_len, i) {
727 ret = d40_size_2_dmalen(sg_dma_len(sg),
728 data_width1, data_width2);
729 if (ret < 0)
730 return ret;
731 len += ret;
732 }
733 return len;
734}
735
736/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200737
738static int d40_channel_execute_command(struct d40_chan *d40c,
739 enum d40_command command)
740{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000741 u32 status;
742 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200743 void __iomem *active_reg;
744 int ret = 0;
745 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000746 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200747
748 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
749
750 if (d40c->phy_chan->num % 2 == 0)
751 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
752 else
753 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
754
755 if (command == D40_DMA_SUSPEND_REQ) {
756 status = (readl(active_reg) &
757 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
758 D40_CHAN_POS(d40c->phy_chan->num);
759
760 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
761 goto done;
762 }
763
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000764 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
765 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
766 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200767
768 if (command == D40_DMA_SUSPEND_REQ) {
769
770 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
771 status = (readl(active_reg) &
772 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
773 D40_CHAN_POS(d40c->phy_chan->num);
774
775 cpu_relax();
776 /*
777 * Reduce the number of bus accesses while
778 * waiting for the DMA to suspend.
779 */
780 udelay(3);
781
782 if (status == D40_DMA_STOP ||
783 status == D40_DMA_SUSPENDED)
784 break;
785 }
786
787 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100788 chan_err(d40c,
789 "unable to suspend the chl %d (log: %d) status %x\n",
790 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200791 status);
792 dump_stack();
793 ret = -EBUSY;
794 }
795
796 }
797done:
798 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
799 return ret;
800}
801
802static void d40_term_all(struct d40_chan *d40c)
803{
804 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200805
806 /* Release active descriptors */
807 while ((d40d = d40_first_active_get(d40c))) {
808 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200809 d40_desc_free(d40c, d40d);
810 }
811
812 /* Release queued descriptors waiting for transfer */
813 while ((d40d = d40_first_queued(d40c))) {
814 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200815 d40_desc_free(d40c, d40d);
816 }
817
Per Forlina8f30672011-06-26 23:29:52 +0200818 /* Release pending descriptors */
819 while ((d40d = d40_first_pending(d40c))) {
820 d40_desc_remove(d40d);
821 d40_desc_free(d40c, d40d);
822 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200823
824 d40c->pending_tx = 0;
825 d40c->busy = false;
826}
827
Rabin Vincent262d2912011-01-25 11:18:05 +0100828static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
829 u32 event, int reg)
830{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100831 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100832 int tries;
833
834 if (!enable) {
835 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
836 | ~D40_EVENTLINE_MASK(event), addr);
837 return;
838 }
839
840 /*
841 * The hardware sometimes doesn't register the enable when src and dst
842 * event lines are active on the same logical channel. Retry to ensure
843 * it does. Usually only one retry is sufficient.
844 */
845 tries = 100;
846 while (--tries) {
847 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
848 | ~D40_EVENTLINE_MASK(event), addr);
849
850 if (readl(addr) & D40_EVENTLINE_MASK(event))
851 break;
852 }
853
854 if (tries != 99)
855 dev_dbg(chan2dev(d40c),
856 "[%s] workaround enable S%cLNK (%d tries)\n",
857 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
858 100 - tries);
859
860 WARN_ON(!tries);
861}
862
Linus Walleij8d318a52010-03-30 15:33:42 +0200863static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
864{
Linus Walleij8d318a52010-03-30 15:33:42 +0200865 unsigned long flags;
866
Linus Walleij8d318a52010-03-30 15:33:42 +0200867 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
868
869 /* Enable event line connected to device (or memcpy) */
870 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
871 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
872 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
873
Rabin Vincent262d2912011-01-25 11:18:05 +0100874 __d40_config_set_event(d40c, do_enable, event,
875 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200876 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100877
Linus Walleij8d318a52010-03-30 15:33:42 +0200878 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
879 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
880
Rabin Vincent262d2912011-01-25 11:18:05 +0100881 __d40_config_set_event(d40c, do_enable, event,
882 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200883 }
884
885 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
886}
887
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200888static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200889{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100890 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000891 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200892
Rabin Vincent8ca84682011-01-25 11:18:07 +0100893 val = readl(chanbase + D40_CHAN_REG_SSLNK);
894 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200895
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200896 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200897}
898
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000899static u32 d40_get_prmo(struct d40_chan *d40c)
900{
901 static const unsigned int phy_map[] = {
902 [STEDMA40_PCHAN_BASIC_MODE]
903 = D40_DREG_PRMO_PCHAN_BASIC,
904 [STEDMA40_PCHAN_MODULO_MODE]
905 = D40_DREG_PRMO_PCHAN_MODULO,
906 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
907 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
908 };
909 static const unsigned int log_map[] = {
910 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
911 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
912 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
913 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
914 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
915 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
916 };
917
Rabin Vincent724a8572011-01-25 11:18:08 +0100918 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000919 return phy_map[d40c->dma_cfg.mode_opt];
920 else
921 return log_map[d40c->dma_cfg.mode_opt];
922}
923
Jonas Aabergb55912c2010-08-09 12:08:02 +0000924static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200925{
926 u32 addr_base;
927 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200928
929 /* Odd addresses are even addresses + 4 */
930 addr_base = (d40c->phy_chan->num % 2) * 4;
931 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100932 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200933 D40_CHAN_POS(d40c->phy_chan->num);
934 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
935
936 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000937 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200938
939 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
940
Rabin Vincent724a8572011-01-25 11:18:08 +0100941 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100942 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
943 & D40_SREG_ELEM_LOG_LIDX_MASK;
944 void __iomem *chanbase = chan_base(d40c);
945
Linus Walleij8d318a52010-03-30 15:33:42 +0200946 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100947 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
948 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200949
Jonas Aabergb55912c2010-08-09 12:08:02 +0000950 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100951 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
952 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200953 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200954}
955
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000956static u32 d40_residue(struct d40_chan *d40c)
957{
958 u32 num_elt;
959
Rabin Vincent724a8572011-01-25 11:18:08 +0100960 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000961 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
962 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100963 else {
964 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
965 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
966 >> D40_SREG_ELEM_PHY_ECNT_POS;
967 }
968
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000969 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
970}
971
972static bool d40_tx_is_linked(struct d40_chan *d40c)
973{
974 bool is_link;
975
Rabin Vincent724a8572011-01-25 11:18:08 +0100976 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000977 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
978 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100979 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
980 & D40_SREG_LNK_PHYS_LNK_MASK;
981
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000982 return is_link;
983}
984
Rabin Vincent86eb5fb2011-01-25 11:18:34 +0100985static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000986{
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000987 int res = 0;
988 unsigned long flags;
989
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000990 if (!d40c->busy)
991 return 0;
992
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000993 spin_lock_irqsave(&d40c->lock, flags);
994
995 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
996 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +0100997 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000998 d40_config_set_event(d40c, false);
999 /* Resume the other logical channels if any */
1000 if (d40_chan_has_events(d40c))
1001 res = d40_channel_execute_command(d40c,
1002 D40_DMA_RUN);
1003 }
1004 }
1005
1006 spin_unlock_irqrestore(&d40c->lock, flags);
1007 return res;
1008}
1009
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001010static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001011{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001012 int res = 0;
1013 unsigned long flags;
1014
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001015 if (!d40c->busy)
1016 return 0;
1017
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001018 spin_lock_irqsave(&d40c->lock, flags);
1019
1020 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +01001021 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001022 res = d40_channel_execute_command(d40c,
1023 D40_DMA_SUSPEND_REQ);
1024 goto no_suspend;
1025 }
1026
1027 /* If bytes left to transfer or linked tx resume job */
1028 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
1029
Rabin Vincent724a8572011-01-25 11:18:08 +01001030 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001031 d40_config_set_event(d40c, true);
1032
1033 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1034 }
1035
1036no_suspend:
1037 spin_unlock_irqrestore(&d40c->lock, flags);
1038 return res;
1039}
1040
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001041static int d40_terminate_all(struct d40_chan *chan)
1042{
1043 unsigned long flags;
1044 int ret = 0;
1045
1046 ret = d40_pause(chan);
1047 if (!ret && chan_is_physical(chan))
1048 ret = d40_channel_execute_command(chan, D40_DMA_STOP);
1049
1050 spin_lock_irqsave(&chan->lock, flags);
1051 d40_term_all(chan);
1052 spin_unlock_irqrestore(&chan->lock, flags);
1053
1054 return ret;
1055}
1056
Linus Walleij8d318a52010-03-30 15:33:42 +02001057static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1058{
1059 struct d40_chan *d40c = container_of(tx->chan,
1060 struct d40_chan,
1061 chan);
1062 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1063 unsigned long flags;
1064
1065 spin_lock_irqsave(&d40c->lock, flags);
1066
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001067 d40c->chan.cookie++;
1068
1069 if (d40c->chan.cookie < 0)
1070 d40c->chan.cookie = 1;
1071
1072 d40d->txd.cookie = d40c->chan.cookie;
1073
Linus Walleij8d318a52010-03-30 15:33:42 +02001074 d40_desc_queue(d40c, d40d);
1075
1076 spin_unlock_irqrestore(&d40c->lock, flags);
1077
1078 return tx->cookie;
1079}
1080
1081static int d40_start(struct d40_chan *d40c)
1082{
Linus Walleijf4185592010-06-22 18:06:42 -07001083 if (d40c->base->rev == 0) {
1084 int err;
1085
Rabin Vincent724a8572011-01-25 11:18:08 +01001086 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -07001087 err = d40_channel_execute_command(d40c,
1088 D40_DMA_SUSPEND_REQ);
1089 if (err)
1090 return err;
1091 }
1092 }
1093
Rabin Vincent724a8572011-01-25 11:18:08 +01001094 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001095 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001096
Jonas Aaberg0c322692010-06-20 21:25:46 +00001097 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001098}
1099
1100static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1101{
1102 struct d40_desc *d40d;
1103 int err;
1104
1105 /* Start queued jobs, if any */
1106 d40d = d40_first_queued(d40c);
1107
1108 if (d40d != NULL) {
1109 d40c->busy = true;
1110
1111 /* Remove from queue */
1112 d40_desc_remove(d40d);
1113
1114 /* Add to active queue */
1115 d40_desc_submit(d40c, d40d);
1116
Rabin Vincent7d83a852011-01-25 11:18:06 +01001117 /* Initiate DMA job */
1118 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001119
Rabin Vincent7d83a852011-01-25 11:18:06 +01001120 /* Start dma job */
1121 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001122
Rabin Vincent7d83a852011-01-25 11:18:06 +01001123 if (err)
1124 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001125 }
1126
1127 return d40d;
1128}
1129
1130/* called from interrupt context */
1131static void dma_tc_handle(struct d40_chan *d40c)
1132{
1133 struct d40_desc *d40d;
1134
Linus Walleij8d318a52010-03-30 15:33:42 +02001135 /* Get first active entry from list */
1136 d40d = d40_first_active_get(d40c);
1137
1138 if (d40d == NULL)
1139 return;
1140
Rabin Vincent0c842b52011-01-25 11:18:35 +01001141 if (d40d->cyclic) {
1142 /*
1143 * If this was a paritially loaded list, we need to reloaded
1144 * it, and only when the list is completed. We need to check
1145 * for done because the interrupt will hit for every link, and
1146 * not just the last one.
1147 */
1148 if (d40d->lli_current < d40d->lli_len
1149 && !d40_tx_is_linked(d40c)
1150 && !d40_residue(d40c)) {
1151 d40_lcla_free_all(d40c, d40d);
1152 d40_desc_load(d40c, d40d);
1153 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001154
Rabin Vincent0c842b52011-01-25 11:18:35 +01001155 if (d40d->lli_current == d40d->lli_len)
1156 d40d->lli_current = 0;
1157 }
1158 } else {
1159 d40_lcla_free_all(d40c, d40d);
1160
1161 if (d40d->lli_current < d40d->lli_len) {
1162 d40_desc_load(d40c, d40d);
1163 /* Start dma job */
1164 (void) d40_start(d40c);
1165 return;
1166 }
1167
1168 if (d40_queue_start(d40c) == NULL)
1169 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001170 }
1171
Linus Walleij8d318a52010-03-30 15:33:42 +02001172 d40c->pending_tx++;
1173 tasklet_schedule(&d40c->tasklet);
1174
1175}
1176
1177static void dma_tasklet(unsigned long data)
1178{
1179 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001180 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001181 unsigned long flags;
1182 dma_async_tx_callback callback;
1183 void *callback_param;
1184
1185 spin_lock_irqsave(&d40c->lock, flags);
1186
1187 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001188 d40d = d40_first_active_get(d40c);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001189 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001190 goto err;
1191
Rabin Vincent0c842b52011-01-25 11:18:35 +01001192 if (!d40d->cyclic)
1193 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001194
1195 /*
1196 * If terminating a channel pending_tx is set to zero.
1197 * This prevents any finished active jobs to return to the client.
1198 */
1199 if (d40c->pending_tx == 0) {
1200 spin_unlock_irqrestore(&d40c->lock, flags);
1201 return;
1202 }
1203
1204 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001205 callback = d40d->txd.callback;
1206 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001207
Rabin Vincent0c842b52011-01-25 11:18:35 +01001208 if (!d40d->cyclic) {
1209 if (async_tx_test_ack(&d40d->txd)) {
1210 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001211 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001212 d40_desc_free(d40c, d40d);
1213 } else {
1214 if (!d40d->is_in_client_list) {
1215 d40_desc_remove(d40d);
1216 d40_lcla_free_all(d40c, d40d);
1217 list_add_tail(&d40d->node, &d40c->client);
1218 d40d->is_in_client_list = true;
1219 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001220 }
1221 }
1222
1223 d40c->pending_tx--;
1224
1225 if (d40c->pending_tx)
1226 tasklet_schedule(&d40c->tasklet);
1227
1228 spin_unlock_irqrestore(&d40c->lock, flags);
1229
Jonas Aaberg767a9672010-08-09 12:08:34 +00001230 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001231 callback(callback_param);
1232
1233 return;
1234
1235 err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001236 /* Rescue manoeuvre if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001237 if (d40c->pending_tx > 0)
1238 d40c->pending_tx--;
1239 spin_unlock_irqrestore(&d40c->lock, flags);
1240}
1241
1242static irqreturn_t d40_handle_interrupt(int irq, void *data)
1243{
1244 static const struct d40_interrupt_lookup il[] = {
1245 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1246 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1247 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1248 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1249 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1250 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1251 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1252 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1253 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1254 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1255 };
1256
1257 int i;
1258 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001259 u32 idx;
1260 u32 row;
1261 long chan = -1;
1262 struct d40_chan *d40c;
1263 unsigned long flags;
1264 struct d40_base *base = data;
1265
1266 spin_lock_irqsave(&base->interrupt_lock, flags);
1267
1268 /* Read interrupt status of both logical and physical channels */
1269 for (i = 0; i < ARRAY_SIZE(il); i++)
1270 regs[i] = readl(base->virtbase + il[i].src);
1271
1272 for (;;) {
1273
1274 chan = find_next_bit((unsigned long *)regs,
1275 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1276
1277 /* No more set bits found? */
1278 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1279 break;
1280
1281 row = chan / BITS_PER_LONG;
1282 idx = chan & (BITS_PER_LONG - 1);
1283
1284 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001285 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001286
1287 if (il[row].offset == D40_PHY_CHAN)
1288 d40c = base->lookup_phy_chans[idx];
1289 else
1290 d40c = base->lookup_log_chans[il[row].offset + idx];
1291 spin_lock(&d40c->lock);
1292
1293 if (!il[row].is_error)
1294 dma_tc_handle(d40c);
1295 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001296 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1297 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001298
1299 spin_unlock(&d40c->lock);
1300 }
1301
1302 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1303
1304 return IRQ_HANDLED;
1305}
1306
Linus Walleij8d318a52010-03-30 15:33:42 +02001307static int d40_validate_conf(struct d40_chan *d40c,
1308 struct stedma40_chan_cfg *conf)
1309{
1310 int res = 0;
1311 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1312 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001313 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001314
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001315 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001316 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001317 res = -EINVAL;
1318 }
1319
1320 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1321 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1322 d40c->runtime_addr == 0) {
1323
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001324 chan_err(d40c, "Invalid TX channel address (%d)\n",
1325 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001326 res = -EINVAL;
1327 }
1328
1329 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1330 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1331 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001332 chan_err(d40c, "Invalid RX channel address (%d)\n",
1333 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001334 res = -EINVAL;
1335 }
1336
1337 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001338 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001339 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001340 res = -EINVAL;
1341 }
1342
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001343 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001344 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001345 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001346 res = -EINVAL;
1347 }
1348
1349 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1350 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001351 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001352 res = -EINVAL;
1353 }
1354
1355 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1356 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001357 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001358 res = -EINVAL;
1359 }
1360
1361 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1362 /*
1363 * DMAC HW supports it. Will be added to this driver,
1364 * in case any dma client requires it.
1365 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001366 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001367 res = -EINVAL;
1368 }
1369
Per Forlind49278e2010-12-20 18:31:38 +01001370 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1371 (1 << conf->src_info.data_width) !=
1372 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1373 (1 << conf->dst_info.data_width)) {
1374 /*
1375 * The DMAC hardware only supports
1376 * src (burst x width) == dst (burst x width)
1377 */
1378
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001379 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001380 res = -EINVAL;
1381 }
1382
Linus Walleij8d318a52010-03-30 15:33:42 +02001383 return res;
1384}
1385
1386static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001387 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001388{
1389 unsigned long flags;
1390 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001391 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001392 /* Physical interrupts are masked per physical full channel */
1393 if (phy->allocated_src == D40_ALLOC_FREE &&
1394 phy->allocated_dst == D40_ALLOC_FREE) {
1395 phy->allocated_dst = D40_ALLOC_PHY;
1396 phy->allocated_src = D40_ALLOC_PHY;
1397 goto found;
1398 } else
1399 goto not_found;
1400 }
1401
1402 /* Logical channel */
1403 if (is_src) {
1404 if (phy->allocated_src == D40_ALLOC_PHY)
1405 goto not_found;
1406
1407 if (phy->allocated_src == D40_ALLOC_FREE)
1408 phy->allocated_src = D40_ALLOC_LOG_FREE;
1409
1410 if (!(phy->allocated_src & (1 << log_event_line))) {
1411 phy->allocated_src |= 1 << log_event_line;
1412 goto found;
1413 } else
1414 goto not_found;
1415 } else {
1416 if (phy->allocated_dst == D40_ALLOC_PHY)
1417 goto not_found;
1418
1419 if (phy->allocated_dst == D40_ALLOC_FREE)
1420 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1421
1422 if (!(phy->allocated_dst & (1 << log_event_line))) {
1423 phy->allocated_dst |= 1 << log_event_line;
1424 goto found;
1425 } else
1426 goto not_found;
1427 }
1428
1429not_found:
1430 spin_unlock_irqrestore(&phy->lock, flags);
1431 return false;
1432found:
1433 spin_unlock_irqrestore(&phy->lock, flags);
1434 return true;
1435}
1436
1437static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1438 int log_event_line)
1439{
1440 unsigned long flags;
1441 bool is_free = false;
1442
1443 spin_lock_irqsave(&phy->lock, flags);
1444 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001445 phy->allocated_dst = D40_ALLOC_FREE;
1446 phy->allocated_src = D40_ALLOC_FREE;
1447 is_free = true;
1448 goto out;
1449 }
1450
1451 /* Logical channel */
1452 if (is_src) {
1453 phy->allocated_src &= ~(1 << log_event_line);
1454 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1455 phy->allocated_src = D40_ALLOC_FREE;
1456 } else {
1457 phy->allocated_dst &= ~(1 << log_event_line);
1458 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1459 phy->allocated_dst = D40_ALLOC_FREE;
1460 }
1461
1462 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1463 D40_ALLOC_FREE);
1464
1465out:
1466 spin_unlock_irqrestore(&phy->lock, flags);
1467
1468 return is_free;
1469}
1470
1471static int d40_allocate_channel(struct d40_chan *d40c)
1472{
1473 int dev_type;
1474 int event_group;
1475 int event_line;
1476 struct d40_phy_res *phys;
1477 int i;
1478 int j;
1479 int log_num;
1480 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001481 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001482
1483 phys = d40c->base->phy_res;
1484
1485 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1486 dev_type = d40c->dma_cfg.src_dev_type;
1487 log_num = 2 * dev_type;
1488 is_src = true;
1489 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1490 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1491 /* dst event lines are used for logical memcpy */
1492 dev_type = d40c->dma_cfg.dst_dev_type;
1493 log_num = 2 * dev_type + 1;
1494 is_src = false;
1495 } else
1496 return -EINVAL;
1497
1498 event_group = D40_TYPE_TO_GROUP(dev_type);
1499 event_line = D40_TYPE_TO_EVENT(dev_type);
1500
1501 if (!is_log) {
1502 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1503 /* Find physical half channel */
1504 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1505
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001506 if (d40_alloc_mask_set(&phys[i], is_src,
1507 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001508 goto found_phy;
1509 }
1510 } else
1511 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1512 int phy_num = j + event_group * 2;
1513 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001514 if (d40_alloc_mask_set(&phys[i],
1515 is_src,
1516 0,
1517 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001518 goto found_phy;
1519 }
1520 }
1521 return -EINVAL;
1522found_phy:
1523 d40c->phy_chan = &phys[i];
1524 d40c->log_num = D40_PHY_CHAN;
1525 goto out;
1526 }
1527 if (dev_type == -1)
1528 return -EINVAL;
1529
1530 /* Find logical channel */
1531 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1532 int phy_num = j + event_group * 2;
1533 /*
1534 * Spread logical channels across all available physical rather
1535 * than pack every logical channel at the first available phy
1536 * channels.
1537 */
1538 if (is_src) {
1539 for (i = phy_num; i < phy_num + 2; i++) {
1540 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001541 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001542 goto found_log;
1543 }
1544 } else {
1545 for (i = phy_num + 1; i >= phy_num; i--) {
1546 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001547 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001548 goto found_log;
1549 }
1550 }
1551 }
1552 return -EINVAL;
1553
1554found_log:
1555 d40c->phy_chan = &phys[i];
1556 d40c->log_num = log_num;
1557out:
1558
1559 if (is_log)
1560 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1561 else
1562 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1563
1564 return 0;
1565
1566}
1567
Linus Walleij8d318a52010-03-30 15:33:42 +02001568static int d40_config_memcpy(struct d40_chan *d40c)
1569{
1570 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1571
1572 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1573 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1574 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1575 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1576 memcpy[d40c->chan.chan_id];
1577
1578 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1579 dma_has_cap(DMA_SLAVE, cap)) {
1580 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1581 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001582 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001583 return -EINVAL;
1584 }
1585
1586 return 0;
1587}
1588
1589
1590static int d40_free_dma(struct d40_chan *d40c)
1591{
1592
1593 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001594 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001595 struct d40_phy_res *phy = d40c->phy_chan;
1596 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001597 struct d40_desc *d;
1598 struct d40_desc *_d;
1599
Linus Walleij8d318a52010-03-30 15:33:42 +02001600
1601 /* Terminate all queued and active transfers */
1602 d40_term_all(d40c);
1603
Per Fridena8be8622010-06-20 21:24:59 +00001604 /* Release client owned descriptors */
1605 if (!list_empty(&d40c->client))
1606 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001607 d40_pool_lli_free(d40c, d);
Per Fridena8be8622010-06-20 21:24:59 +00001608 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001609 d40_desc_free(d40c, d);
1610 }
1611
Linus Walleij8d318a52010-03-30 15:33:42 +02001612 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001613 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001614 return -EINVAL;
1615 }
1616
1617 if (phy->allocated_src == D40_ALLOC_FREE &&
1618 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001619 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001620 return -EINVAL;
1621 }
1622
Linus Walleij8d318a52010-03-30 15:33:42 +02001623 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1624 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1625 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001626 is_src = false;
1627 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1628 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001629 is_src = true;
1630 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001631 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001632 return -EINVAL;
1633 }
1634
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001635 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1636 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001637 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001638 return res;
1639 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001640
Rabin Vincent724a8572011-01-25 11:18:08 +01001641 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001642 /* Release logical channel, deactivate the event line */
1643
1644 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001645 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1646
1647 /*
1648 * Check if there are more logical allocation
1649 * on this phy channel.
1650 */
1651 if (!d40_alloc_mask_free(phy, is_src, event)) {
1652 /* Resume the other logical channels if any */
1653 if (d40_chan_has_events(d40c)) {
1654 res = d40_channel_execute_command(d40c,
1655 D40_DMA_RUN);
1656 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001657 chan_err(d40c,
1658 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001659 return res;
1660 }
1661 }
1662 return 0;
1663 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001664 } else {
1665 (void) d40_alloc_mask_free(phy, is_src, 0);
1666 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001667
1668 /* Release physical channel */
1669 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1670 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001671 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001672 return res;
1673 }
1674 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001675 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001676 d40c->base->lookup_phy_chans[phy->num] = NULL;
1677
1678 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001679}
1680
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001681static bool d40_is_paused(struct d40_chan *d40c)
1682{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001683 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001684 bool is_paused = false;
1685 unsigned long flags;
1686 void __iomem *active_reg;
1687 u32 status;
1688 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001689
1690 spin_lock_irqsave(&d40c->lock, flags);
1691
Rabin Vincent724a8572011-01-25 11:18:08 +01001692 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001693 if (d40c->phy_chan->num % 2 == 0)
1694 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1695 else
1696 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1697
1698 status = (readl(active_reg) &
1699 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1700 D40_CHAN_POS(d40c->phy_chan->num);
1701 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1702 is_paused = true;
1703
1704 goto _exit;
1705 }
1706
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001707 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001708 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001709 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001710 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001711 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001712 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001713 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001714 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001715 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001716 goto _exit;
1717 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001718
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001719 status = (status & D40_EVENTLINE_MASK(event)) >>
1720 D40_EVENTLINE_POS(event);
1721
1722 if (status != D40_DMA_RUN)
1723 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001724_exit:
1725 spin_unlock_irqrestore(&d40c->lock, flags);
1726 return is_paused;
1727
1728}
1729
1730
Linus Walleij8d318a52010-03-30 15:33:42 +02001731static u32 stedma40_residue(struct dma_chan *chan)
1732{
1733 struct d40_chan *d40c =
1734 container_of(chan, struct d40_chan, chan);
1735 u32 bytes_left;
1736 unsigned long flags;
1737
1738 spin_lock_irqsave(&d40c->lock, flags);
1739 bytes_left = d40_residue(d40c);
1740 spin_unlock_irqrestore(&d40c->lock, flags);
1741
1742 return bytes_left;
1743}
1744
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001745static int
1746d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1747 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001748 unsigned int sg_len, dma_addr_t src_dev_addr,
1749 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001750{
1751 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1752 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1753 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001754 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001755
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001756 ret = d40_log_sg_to_lli(sg_src, sg_len,
1757 src_dev_addr,
1758 desc->lli_log.src,
1759 chan->log_def.lcsp1,
1760 src_info->data_width,
1761 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001762
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001763 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1764 dst_dev_addr,
1765 desc->lli_log.dst,
1766 chan->log_def.lcsp3,
1767 dst_info->data_width,
1768 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001769
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001770 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001771}
1772
1773static int
1774d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1775 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001776 unsigned int sg_len, dma_addr_t src_dev_addr,
1777 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001778{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001779 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1780 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1781 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01001782 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001783 int ret;
1784
Rabin Vincent0c842b52011-01-25 11:18:35 +01001785 if (desc->cyclic)
1786 flags |= LLI_CYCLIC | LLI_TERM_INT;
1787
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001788 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1789 desc->lli_phy.src,
1790 virt_to_phys(desc->lli_phy.src),
1791 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001792 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001793
1794 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1795 desc->lli_phy.dst,
1796 virt_to_phys(desc->lli_phy.dst),
1797 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001798 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001799
1800 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1801 desc->lli_pool.size, DMA_TO_DEVICE);
1802
1803 return ret < 0 ? ret : 0;
1804}
1805
1806
Rabin Vincent5f811582011-01-25 11:18:18 +01001807static struct d40_desc *
1808d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1809 unsigned int sg_len, unsigned long dma_flags)
1810{
1811 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1812 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001813 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01001814
1815 desc = d40_desc_get(chan);
1816 if (!desc)
1817 return NULL;
1818
1819 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1820 cfg->dst_info.data_width);
1821 if (desc->lli_len < 0) {
1822 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01001823 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01001824 }
1825
Rabin Vincentdbd88782011-01-25 11:18:19 +01001826 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1827 if (ret < 0) {
1828 chan_err(chan, "Could not allocate lli\n");
1829 goto err;
1830 }
1831
1832
Rabin Vincent5f811582011-01-25 11:18:18 +01001833 desc->lli_current = 0;
1834 desc->txd.flags = dma_flags;
1835 desc->txd.tx_submit = d40_tx_submit;
1836
1837 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1838
1839 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001840
1841err:
1842 d40_desc_free(chan, desc);
1843 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01001844}
1845
Rabin Vincentcade1d32011-01-25 11:18:23 +01001846static dma_addr_t
1847d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02001848{
Rabin Vincentcade1d32011-01-25 11:18:23 +01001849 struct stedma40_platform_data *plat = chan->base->plat_data;
1850 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02001851 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001852
Rabin Vincentcade1d32011-01-25 11:18:23 +01001853 if (chan->runtime_addr)
1854 return chan->runtime_addr;
1855
1856 if (direction == DMA_FROM_DEVICE)
1857 addr = plat->dev_rx[cfg->src_dev_type];
1858 else if (direction == DMA_TO_DEVICE)
1859 addr = plat->dev_tx[cfg->dst_dev_type];
1860
1861 return addr;
1862}
1863
1864static struct dma_async_tx_descriptor *
1865d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
1866 struct scatterlist *sg_dst, unsigned int sg_len,
1867 enum dma_data_direction direction, unsigned long dma_flags)
1868{
1869 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01001870 dma_addr_t src_dev_addr = 0;
1871 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01001872 struct d40_desc *desc;
1873 unsigned long flags;
1874 int ret;
1875
1876 if (!chan->phy_chan) {
1877 chan_err(chan, "Cannot prepare unallocated channel\n");
1878 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001879 }
1880
Rabin Vincent0c842b52011-01-25 11:18:35 +01001881
Rabin Vincentcade1d32011-01-25 11:18:23 +01001882 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001883
Rabin Vincentcade1d32011-01-25 11:18:23 +01001884 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
1885 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001886 goto err;
1887
Rabin Vincent0c842b52011-01-25 11:18:35 +01001888 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
1889 desc->cyclic = true;
1890
Rabin Vincent822c5672011-01-25 11:18:28 +01001891 if (direction != DMA_NONE) {
1892 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
1893
1894 if (direction == DMA_FROM_DEVICE)
1895 src_dev_addr = dev_addr;
1896 else if (direction == DMA_TO_DEVICE)
1897 dst_dev_addr = dev_addr;
1898 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01001899
1900 if (chan_is_logical(chan))
1901 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001902 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001903 else
1904 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001905 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001906
1907 if (ret) {
1908 chan_err(chan, "Failed to prepare %s sg job: %d\n",
1909 chan_is_logical(chan) ? "log" : "phy", ret);
1910 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001911 }
1912
Rabin Vincentcade1d32011-01-25 11:18:23 +01001913 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001914
Rabin Vincentcade1d32011-01-25 11:18:23 +01001915 return &desc->txd;
1916
Linus Walleij8d318a52010-03-30 15:33:42 +02001917err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01001918 if (desc)
1919 d40_desc_free(chan, desc);
1920 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001921 return NULL;
1922}
Linus Walleij8d318a52010-03-30 15:33:42 +02001923
1924bool stedma40_filter(struct dma_chan *chan, void *data)
1925{
1926 struct stedma40_chan_cfg *info = data;
1927 struct d40_chan *d40c =
1928 container_of(chan, struct d40_chan, chan);
1929 int err;
1930
1931 if (data) {
1932 err = d40_validate_conf(d40c, info);
1933 if (!err)
1934 d40c->dma_cfg = *info;
1935 } else
1936 err = d40_config_memcpy(d40c);
1937
Rabin Vincentce2ca122010-10-12 13:00:49 +00001938 if (!err)
1939 d40c->configured = true;
1940
Linus Walleij8d318a52010-03-30 15:33:42 +02001941 return err == 0;
1942}
1943EXPORT_SYMBOL(stedma40_filter);
1944
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001945static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1946{
1947 bool realtime = d40c->dma_cfg.realtime;
1948 bool highprio = d40c->dma_cfg.high_priority;
1949 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1950 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1951 u32 event = D40_TYPE_TO_EVENT(dev_type);
1952 u32 group = D40_TYPE_TO_GROUP(dev_type);
1953 u32 bit = 1 << event;
1954
1955 /* Destination event lines are stored in the upper halfword */
1956 if (!src)
1957 bit <<= 16;
1958
1959 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1960 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1961}
1962
1963static void d40_set_prio_realtime(struct d40_chan *d40c)
1964{
1965 if (d40c->base->rev < 3)
1966 return;
1967
1968 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1969 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1970 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1971
1972 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1973 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1974 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1975}
1976
Linus Walleij8d318a52010-03-30 15:33:42 +02001977/* DMA ENGINE functions */
1978static int d40_alloc_chan_resources(struct dma_chan *chan)
1979{
1980 int err;
1981 unsigned long flags;
1982 struct d40_chan *d40c =
1983 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001984 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001985 spin_lock_irqsave(&d40c->lock, flags);
1986
1987 d40c->completed = chan->cookie = 1;
1988
Rabin Vincentce2ca122010-10-12 13:00:49 +00001989 /* If no dma configuration is set use default configuration (memcpy) */
1990 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001991 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001992 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001993 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001994 goto fail;
1995 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001996 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001997 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001998
1999 err = d40_allocate_channel(d40c);
2000 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002001 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002002 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002003 }
2004
Linus Walleijef1872e2010-06-20 21:24:52 +00002005 /* Fill in basic CFG register values */
2006 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002007 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002008
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002009 d40_set_prio_realtime(d40c);
2010
Rabin Vincent724a8572011-01-25 11:18:08 +01002011 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002012 d40_log_cfg(&d40c->dma_cfg,
2013 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2014
2015 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2016 d40c->lcpa = d40c->base->lcpa_base +
2017 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2018 else
2019 d40c->lcpa = d40c->base->lcpa_base +
2020 d40c->dma_cfg.dst_dev_type *
2021 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2022 }
2023
2024 /*
2025 * Only write channel configuration to the DMA if the physical
2026 * resource is free. In case of multiple logical channels
2027 * on the same physical resource, only the first write is necessary.
2028 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002029 if (is_free_phy)
2030 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002031fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02002032 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002033 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002034}
2035
2036static void d40_free_chan_resources(struct dma_chan *chan)
2037{
2038 struct d40_chan *d40c =
2039 container_of(chan, struct d40_chan, chan);
2040 int err;
2041 unsigned long flags;
2042
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002043 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002044 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002045 return;
2046 }
2047
2048
Linus Walleij8d318a52010-03-30 15:33:42 +02002049 spin_lock_irqsave(&d40c->lock, flags);
2050
2051 err = d40_free_dma(d40c);
2052
2053 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002054 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002055 spin_unlock_irqrestore(&d40c->lock, flags);
2056}
2057
2058static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2059 dma_addr_t dst,
2060 dma_addr_t src,
2061 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002062 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002063{
Rabin Vincent95944c62011-01-25 11:18:17 +01002064 struct scatterlist dst_sg;
2065 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002066
Rabin Vincent95944c62011-01-25 11:18:17 +01002067 sg_init_table(&dst_sg, 1);
2068 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002069
Rabin Vincent95944c62011-01-25 11:18:17 +01002070 sg_dma_address(&dst_sg) = dst;
2071 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002072
Rabin Vincent95944c62011-01-25 11:18:17 +01002073 sg_dma_len(&dst_sg) = size;
2074 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002075
Rabin Vincentcade1d32011-01-25 11:18:23 +01002076 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002077}
2078
Ira Snyder0d688662010-09-30 11:46:47 +00002079static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002080d40_prep_memcpy_sg(struct dma_chan *chan,
2081 struct scatterlist *dst_sg, unsigned int dst_nents,
2082 struct scatterlist *src_sg, unsigned int src_nents,
2083 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002084{
2085 if (dst_nents != src_nents)
2086 return NULL;
2087
Rabin Vincentcade1d32011-01-25 11:18:23 +01002088 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002089}
2090
Linus Walleij8d318a52010-03-30 15:33:42 +02002091static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2092 struct scatterlist *sgl,
2093 unsigned int sg_len,
2094 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002095 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002096{
Rabin Vincent00ac0342011-01-25 11:18:20 +01002097 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
2098 return NULL;
2099
Rabin Vincentcade1d32011-01-25 11:18:23 +01002100 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002101}
2102
Rabin Vincent0c842b52011-01-25 11:18:35 +01002103static struct dma_async_tx_descriptor *
2104dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2105 size_t buf_len, size_t period_len,
2106 enum dma_data_direction direction)
2107{
2108 unsigned int periods = buf_len / period_len;
2109 struct dma_async_tx_descriptor *txd;
2110 struct scatterlist *sg;
2111 int i;
2112
2113 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_KERNEL);
2114 for (i = 0; i < periods; i++) {
2115 sg_dma_address(&sg[i]) = dma_addr;
2116 sg_dma_len(&sg[i]) = period_len;
2117 dma_addr += period_len;
2118 }
2119
2120 sg[periods].offset = 0;
2121 sg[periods].length = 0;
2122 sg[periods].page_link =
2123 ((unsigned long)sg | 0x01) & ~0x02;
2124
2125 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2126 DMA_PREP_INTERRUPT);
2127
2128 kfree(sg);
2129
2130 return txd;
2131}
2132
Linus Walleij8d318a52010-03-30 15:33:42 +02002133static 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
Per Forlina8f30672011-06-26 23:29:52 +02002173 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2174
2175 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002176 if (!d40c->busy)
2177 (void) d40_queue_start(d40c);
2178
2179 spin_unlock_irqrestore(&d40c->lock, flags);
2180}
2181
Linus Walleij95e14002010-08-04 13:37:45 +02002182/* Runtime reconfiguration extension */
2183static void d40_set_runtime_config(struct dma_chan *chan,
2184 struct dma_slave_config *config)
2185{
2186 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2187 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2188 enum dma_slave_buswidth config_addr_width;
2189 dma_addr_t config_addr;
2190 u32 config_maxburst;
2191 enum stedma40_periph_data_width addr_width;
2192 int psize;
2193
2194 if (config->direction == DMA_FROM_DEVICE) {
2195 dma_addr_t dev_addr_rx =
2196 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2197
2198 config_addr = config->src_addr;
2199 if (dev_addr_rx)
2200 dev_dbg(d40c->base->dev,
2201 "channel has a pre-wired RX address %08x "
2202 "overriding with %08x\n",
2203 dev_addr_rx, config_addr);
2204 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2205 dev_dbg(d40c->base->dev,
2206 "channel was not configured for peripheral "
2207 "to memory transfer (%d) overriding\n",
2208 cfg->dir);
2209 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2210
2211 config_addr_width = config->src_addr_width;
2212 config_maxburst = config->src_maxburst;
2213
2214 } else if (config->direction == DMA_TO_DEVICE) {
2215 dma_addr_t dev_addr_tx =
2216 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2217
2218 config_addr = config->dst_addr;
2219 if (dev_addr_tx)
2220 dev_dbg(d40c->base->dev,
2221 "channel has a pre-wired TX address %08x "
2222 "overriding with %08x\n",
2223 dev_addr_tx, config_addr);
2224 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2225 dev_dbg(d40c->base->dev,
2226 "channel was not configured for memory "
2227 "to peripheral transfer (%d) overriding\n",
2228 cfg->dir);
2229 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2230
2231 config_addr_width = config->dst_addr_width;
2232 config_maxburst = config->dst_maxburst;
2233
2234 } else {
2235 dev_err(d40c->base->dev,
2236 "unrecognized channel direction %d\n",
2237 config->direction);
2238 return;
2239 }
2240
2241 switch (config_addr_width) {
2242 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2243 addr_width = STEDMA40_BYTE_WIDTH;
2244 break;
2245 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2246 addr_width = STEDMA40_HALFWORD_WIDTH;
2247 break;
2248 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2249 addr_width = STEDMA40_WORD_WIDTH;
2250 break;
2251 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2252 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2253 break;
2254 default:
2255 dev_err(d40c->base->dev,
2256 "illegal peripheral address width "
2257 "requested (%d)\n",
2258 config->src_addr_width);
2259 return;
2260 }
2261
Rabin Vincent724a8572011-01-25 11:18:08 +01002262 if (chan_is_logical(d40c)) {
Per Forlina59670a2010-10-06 09:05:27 +00002263 if (config_maxburst >= 16)
2264 psize = STEDMA40_PSIZE_LOG_16;
2265 else if (config_maxburst >= 8)
2266 psize = STEDMA40_PSIZE_LOG_8;
2267 else if (config_maxburst >= 4)
2268 psize = STEDMA40_PSIZE_LOG_4;
2269 else
2270 psize = STEDMA40_PSIZE_LOG_1;
2271 } else {
2272 if (config_maxburst >= 16)
2273 psize = STEDMA40_PSIZE_PHY_16;
2274 else if (config_maxburst >= 8)
2275 psize = STEDMA40_PSIZE_PHY_8;
2276 else if (config_maxburst >= 4)
2277 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002278 else if (config_maxburst >= 2)
2279 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002280 else
2281 psize = STEDMA40_PSIZE_PHY_1;
2282 }
Linus Walleij95e14002010-08-04 13:37:45 +02002283
2284 /* Set up all the endpoint configs */
2285 cfg->src_info.data_width = addr_width;
2286 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002287 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002288 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2289 cfg->dst_info.data_width = addr_width;
2290 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002291 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002292 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2293
Per Forlina59670a2010-10-06 09:05:27 +00002294 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002295 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002296 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2297 else
2298 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2299 &d40c->dst_def_cfg, false);
2300
Linus Walleij95e14002010-08-04 13:37:45 +02002301 /* These settings will take precedence later */
2302 d40c->runtime_addr = config_addr;
2303 d40c->runtime_direction = config->direction;
2304 dev_dbg(d40c->base->dev,
2305 "configured channel %s for %s, data width %d, "
2306 "maxburst %d bytes, LE, no flow control\n",
2307 dma_chan_name(chan),
2308 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2309 config_addr_width,
2310 config_maxburst);
2311}
2312
Linus Walleij05827632010-05-17 16:30:42 -07002313static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2314 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002315{
Linus Walleij8d318a52010-03-30 15:33:42 +02002316 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2317
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002318 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002319 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002320 return -EINVAL;
2321 }
2322
Linus Walleij8d318a52010-03-30 15:33:42 +02002323 switch (cmd) {
2324 case DMA_TERMINATE_ALL:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002325 return d40_terminate_all(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002326 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002327 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002328 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002329 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002330 case DMA_SLAVE_CONFIG:
2331 d40_set_runtime_config(chan,
2332 (struct dma_slave_config *) arg);
2333 return 0;
2334 default:
2335 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002336 }
2337
2338 /* Other commands are unimplemented */
2339 return -ENXIO;
2340}
2341
2342/* Initialization functions */
2343
2344static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2345 struct d40_chan *chans, int offset,
2346 int num_chans)
2347{
2348 int i = 0;
2349 struct d40_chan *d40c;
2350
2351 INIT_LIST_HEAD(&dma->channels);
2352
2353 for (i = offset; i < offset + num_chans; i++) {
2354 d40c = &chans[i];
2355 d40c->base = base;
2356 d40c->chan.device = dma;
2357
Linus Walleij8d318a52010-03-30 15:33:42 +02002358 spin_lock_init(&d40c->lock);
2359
2360 d40c->log_num = D40_PHY_CHAN;
2361
Linus Walleij8d318a52010-03-30 15:33:42 +02002362 INIT_LIST_HEAD(&d40c->active);
2363 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002364 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002365 INIT_LIST_HEAD(&d40c->client);
2366
Linus Walleij8d318a52010-03-30 15:33:42 +02002367 tasklet_init(&d40c->tasklet, dma_tasklet,
2368 (unsigned long) d40c);
2369
2370 list_add_tail(&d40c->chan.device_node,
2371 &dma->channels);
2372 }
2373}
2374
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002375static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2376{
2377 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2378 dev->device_prep_slave_sg = d40_prep_slave_sg;
2379
2380 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2381 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2382
2383 /*
2384 * This controller can only access address at even
2385 * 32bit boundaries, i.e. 2^2
2386 */
2387 dev->copy_align = 2;
2388 }
2389
2390 if (dma_has_cap(DMA_SG, dev->cap_mask))
2391 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2392
Rabin Vincent0c842b52011-01-25 11:18:35 +01002393 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2394 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2395
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002396 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2397 dev->device_free_chan_resources = d40_free_chan_resources;
2398 dev->device_issue_pending = d40_issue_pending;
2399 dev->device_tx_status = d40_tx_status;
2400 dev->device_control = d40_control;
2401 dev->dev = base->dev;
2402}
2403
Linus Walleij8d318a52010-03-30 15:33:42 +02002404static int __init d40_dmaengine_init(struct d40_base *base,
2405 int num_reserved_chans)
2406{
2407 int err ;
2408
2409 d40_chan_init(base, &base->dma_slave, base->log_chans,
2410 0, base->num_log_chans);
2411
2412 dma_cap_zero(base->dma_slave.cap_mask);
2413 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002414 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002415
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002416 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002417
2418 err = dma_async_device_register(&base->dma_slave);
2419
2420 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002421 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002422 goto failure1;
2423 }
2424
2425 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2426 base->num_log_chans, base->plat_data->memcpy_len);
2427
2428 dma_cap_zero(base->dma_memcpy.cap_mask);
2429 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002430 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002431
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002432 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002433
2434 err = dma_async_device_register(&base->dma_memcpy);
2435
2436 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002437 d40_err(base->dev,
2438 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002439 goto failure2;
2440 }
2441
2442 d40_chan_init(base, &base->dma_both, base->phy_chans,
2443 0, num_reserved_chans);
2444
2445 dma_cap_zero(base->dma_both.cap_mask);
2446 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2447 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002448 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002449 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002450
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002451 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002452 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{
Rabin Vincent026cbc42011-01-25 11:18:14 +01002774 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00002775 unsigned long *page_list;
2776 int i, j;
2777 int ret = 0;
2778
2779 /*
2780 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2781 * To full fill this hardware requirement without wasting 256 kb
2782 * we allocate pages until we get an aligned one.
2783 */
2784 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2785 GFP_KERNEL);
2786
2787 if (!page_list) {
2788 ret = -ENOMEM;
2789 goto failure;
2790 }
2791
2792 /* Calculating how many pages that are required */
2793 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2794
2795 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2796 page_list[i] = __get_free_pages(GFP_KERNEL,
2797 base->lcla_pool.pages);
2798 if (!page_list[i]) {
2799
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002800 d40_err(base->dev, "Failed to allocate %d pages.\n",
2801 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002802
2803 for (j = 0; j < i; j++)
2804 free_pages(page_list[j], base->lcla_pool.pages);
2805 goto failure;
2806 }
2807
2808 if ((virt_to_phys((void *)page_list[i]) &
2809 (LCLA_ALIGNMENT - 1)) == 0)
2810 break;
2811 }
2812
2813 for (j = 0; j < i; j++)
2814 free_pages(page_list[j], base->lcla_pool.pages);
2815
2816 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2817 base->lcla_pool.base = (void *)page_list[i];
2818 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002819 /*
2820 * After many attempts and no succees with finding the correct
2821 * alignment, try with allocating a big buffer.
2822 */
Linus Walleij508849a2010-06-20 21:26:07 +00002823 dev_warn(base->dev,
2824 "[%s] Failed to get %d pages @ 18 bit align.\n",
2825 __func__, base->lcla_pool.pages);
2826 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2827 base->num_phy_chans +
2828 LCLA_ALIGNMENT,
2829 GFP_KERNEL);
2830 if (!base->lcla_pool.base_unaligned) {
2831 ret = -ENOMEM;
2832 goto failure;
2833 }
2834
2835 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2836 LCLA_ALIGNMENT);
2837 }
2838
Rabin Vincent026cbc42011-01-25 11:18:14 +01002839 pool->dma_addr = dma_map_single(base->dev, pool->base,
2840 SZ_1K * base->num_phy_chans,
2841 DMA_TO_DEVICE);
2842 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2843 pool->dma_addr = 0;
2844 ret = -ENOMEM;
2845 goto failure;
2846 }
2847
Linus Walleij508849a2010-06-20 21:26:07 +00002848 writel(virt_to_phys(base->lcla_pool.base),
2849 base->virtbase + D40_DREG_LCLA);
2850failure:
2851 kfree(page_list);
2852 return ret;
2853}
2854
Linus Walleij8d318a52010-03-30 15:33:42 +02002855static int __init d40_probe(struct platform_device *pdev)
2856{
2857 int err;
2858 int ret = -ENOENT;
2859 struct d40_base *base;
2860 struct resource *res = NULL;
2861 int num_reserved_chans;
2862 u32 val;
2863
2864 base = d40_hw_detect_init(pdev);
2865
2866 if (!base)
2867 goto failure;
2868
2869 num_reserved_chans = d40_phy_res_init(base);
2870
2871 platform_set_drvdata(pdev, base);
2872
2873 spin_lock_init(&base->interrupt_lock);
2874 spin_lock_init(&base->execmd_lock);
2875
2876 /* Get IO for logical channel parameter address */
2877 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2878 if (!res) {
2879 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002880 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002881 goto failure;
2882 }
2883 base->lcpa_size = resource_size(res);
2884 base->phy_lcpa = res->start;
2885
2886 if (request_mem_region(res->start, resource_size(res),
2887 D40_NAME " I/O lcpa") == NULL) {
2888 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002889 d40_err(&pdev->dev,
2890 "Failed to request LCPA region 0x%x-0x%x\n",
2891 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002892 goto failure;
2893 }
2894
2895 /* We make use of ESRAM memory for this. */
2896 val = readl(base->virtbase + D40_DREG_LCPA);
2897 if (res->start != val && val != 0) {
2898 dev_warn(&pdev->dev,
2899 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2900 __func__, val, res->start);
2901 } else
2902 writel(res->start, base->virtbase + D40_DREG_LCPA);
2903
2904 base->lcpa_base = ioremap(res->start, resource_size(res));
2905 if (!base->lcpa_base) {
2906 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002907 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002908 goto failure;
2909 }
Linus Walleij508849a2010-06-20 21:26:07 +00002910
2911 ret = d40_lcla_allocate(base);
2912 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002913 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002914 goto failure;
2915 }
2916
Linus Walleij8d318a52010-03-30 15:33:42 +02002917 spin_lock_init(&base->lcla_pool.lock);
2918
Linus Walleij8d318a52010-03-30 15:33:42 +02002919 base->irq = platform_get_irq(pdev, 0);
2920
2921 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002922 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002923 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002924 goto failure;
2925 }
2926
2927 err = d40_dmaengine_init(base, num_reserved_chans);
2928 if (err)
2929 goto failure;
2930
2931 d40_hw_init(base);
2932
2933 dev_info(base->dev, "initialized\n");
2934 return 0;
2935
2936failure:
2937 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002938 if (base->desc_slab)
2939 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002940 if (base->virtbase)
2941 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01002942
2943 if (base->lcla_pool.dma_addr)
2944 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2945 SZ_1K * base->num_phy_chans,
2946 DMA_TO_DEVICE);
2947
Linus Walleij508849a2010-06-20 21:26:07 +00002948 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2949 free_pages((unsigned long)base->lcla_pool.base,
2950 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002951
2952 kfree(base->lcla_pool.base_unaligned);
2953
Linus Walleij8d318a52010-03-30 15:33:42 +02002954 if (base->phy_lcpa)
2955 release_mem_region(base->phy_lcpa,
2956 base->lcpa_size);
2957 if (base->phy_start)
2958 release_mem_region(base->phy_start,
2959 base->phy_size);
2960 if (base->clk) {
2961 clk_disable(base->clk);
2962 clk_put(base->clk);
2963 }
2964
2965 kfree(base->lcla_pool.alloc_map);
2966 kfree(base->lookup_log_chans);
2967 kfree(base->lookup_phy_chans);
2968 kfree(base->phy_res);
2969 kfree(base);
2970 }
2971
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002972 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002973 return ret;
2974}
2975
2976static struct platform_driver d40_driver = {
2977 .driver = {
2978 .owner = THIS_MODULE,
2979 .name = D40_NAME,
2980 },
2981};
2982
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002983static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002984{
2985 return platform_driver_probe(&d40_driver, d40_probe);
2986}
Linus Walleija0eb2212011-05-18 14:18:57 +02002987subsys_initcall(stedma40_init);