blob: 8fd0bb94e777ba61178134a92cbab6f0d825a9be [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.
Jonas Aaberg698e4732010-08-09 12:08:56 +000093 * @lli_current: Number of transfered llis.
94 * @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;
118};
119
120/**
121 * struct d40_lcla_pool - LCLA pool settings and data.
122 *
Linus Walleij508849a2010-06-20 21:26:07 +0000123 * @base: The virtual address of LCLA. 18 bit aligned.
124 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
125 * This pointer is only there for clean-up on error.
126 * @pages: The number of pages needed for all physical channels.
127 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200128 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000129 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200130 */
131struct d40_lcla_pool {
132 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100133 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000134 void *base_unaligned;
135 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200136 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000137 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200138};
139
140/**
141 * struct d40_phy_res - struct for handling eventlines mapped to physical
142 * channels.
143 *
144 * @lock: A lock protection this entity.
145 * @num: The physical channel number of this entity.
146 * @allocated_src: Bit mapped to show which src event line's are mapped to
147 * this physical channel. Can also be free or physically allocated.
148 * @allocated_dst: Same as for src but is dst.
149 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000150 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200151 */
152struct d40_phy_res {
153 spinlock_t lock;
154 int num;
155 u32 allocated_src;
156 u32 allocated_dst;
157};
158
159struct d40_base;
160
161/**
162 * struct d40_chan - Struct that describes a channel.
163 *
164 * @lock: A spinlock to protect this struct.
165 * @log_num: The logical number, if any of this channel.
166 * @completed: Starts with 1, after first interrupt it is set to dma engine's
167 * current cookie.
168 * @pending_tx: The number of pending transfers. Used between interrupt handler
169 * and tasklet.
170 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000171 * @phy_chan: Pointer to physical channel which this instance runs on. If this
172 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200173 * @chan: DMA engine handle.
174 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
175 * transfer and call client callback.
176 * @client: Cliented owned descriptor list.
177 * @active: Active descriptor.
178 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200179 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000180 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200181 * @base: Pointer to the device instance struct.
182 * @src_def_cfg: Default cfg register setting for src.
183 * @dst_def_cfg: Default cfg register setting for dst.
184 * @log_def: Default logical channel settings.
185 * @lcla: Space for one dst src pair for logical channel transfers.
186 * @lcpa: Pointer to dst and src lcpa settings.
187 *
188 * This struct can either "be" a logical or a physical channel.
189 */
190struct d40_chan {
191 spinlock_t lock;
192 int log_num;
193 /* ID of the most recent completed transfer */
194 int completed;
195 int pending_tx;
196 bool busy;
197 struct d40_phy_res *phy_chan;
198 struct dma_chan chan;
199 struct tasklet_struct tasklet;
200 struct list_head client;
201 struct list_head active;
202 struct list_head queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200203 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000204 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200205 struct d40_base *base;
206 /* Default register configurations */
207 u32 src_def_cfg;
208 u32 dst_def_cfg;
209 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200210 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200211 /* Runtime reconfiguration */
212 dma_addr_t runtime_addr;
213 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200214};
215
216/**
217 * struct d40_base - The big global struct, one for each probe'd instance.
218 *
219 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
220 * @execmd_lock: Lock for execute command usage since several channels share
221 * the same physical register.
222 * @dev: The device structure.
223 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700224 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200225 * @clk: Pointer to the DMA clock structure.
226 * @phy_start: Physical memory start of the DMA registers.
227 * @phy_size: Size of the DMA register map.
228 * @irq: The IRQ number.
229 * @num_phy_chans: The number of physical channels. Read from HW. This
230 * is the number of available channels for this driver, not counting "Secure
231 * mode" allocated physical channels.
232 * @num_log_chans: The number of logical channels. Calculated from
233 * num_phy_chans.
234 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
235 * @dma_slave: dma_device channels that can do only do slave transfers.
236 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200237 * @log_chans: Room for all possible logical channels in system.
238 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
239 * to log_chans entries.
240 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
241 * to phy_chans entries.
242 * @plat_data: Pointer to provided platform_data which is the driver
243 * configuration.
244 * @phy_res: Vector containing all physical channels.
245 * @lcla_pool: lcla pool settings and data.
246 * @lcpa_base: The virtual mapped address of LCPA.
247 * @phy_lcpa: The physical address of the LCPA.
248 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000249 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200250 */
251struct d40_base {
252 spinlock_t interrupt_lock;
253 spinlock_t execmd_lock;
254 struct device *dev;
255 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700256 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200257 struct clk *clk;
258 phys_addr_t phy_start;
259 resource_size_t phy_size;
260 int irq;
261 int num_phy_chans;
262 int num_log_chans;
263 struct dma_device dma_both;
264 struct dma_device dma_slave;
265 struct dma_device dma_memcpy;
266 struct d40_chan *phy_chans;
267 struct d40_chan *log_chans;
268 struct d40_chan **lookup_log_chans;
269 struct d40_chan **lookup_phy_chans;
270 struct stedma40_platform_data *plat_data;
271 /* Physical half channels */
272 struct d40_phy_res *phy_res;
273 struct d40_lcla_pool lcla_pool;
274 void *lcpa_base;
275 dma_addr_t phy_lcpa;
276 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000277 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200278};
279
280/**
281 * struct d40_interrupt_lookup - lookup table for interrupt handler
282 *
283 * @src: Interrupt mask register.
284 * @clr: Interrupt clear register.
285 * @is_error: true if this is an error interrupt.
286 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
287 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
288 */
289struct d40_interrupt_lookup {
290 u32 src;
291 u32 clr;
292 bool is_error;
293 int offset;
294};
295
296/**
297 * struct d40_reg_val - simple lookup struct
298 *
299 * @reg: The register.
300 * @val: The value that belongs to the register in reg.
301 */
302struct d40_reg_val {
303 unsigned int reg;
304 unsigned int val;
305};
306
Rabin Vincent262d2912011-01-25 11:18:05 +0100307static struct device *chan2dev(struct d40_chan *d40c)
308{
309 return &d40c->chan.dev->device;
310}
311
Rabin Vincent724a8572011-01-25 11:18:08 +0100312static bool chan_is_physical(struct d40_chan *chan)
313{
314 return chan->log_num == D40_PHY_CHAN;
315}
316
317static bool chan_is_logical(struct d40_chan *chan)
318{
319 return !chan_is_physical(chan);
320}
321
Rabin Vincent8ca84682011-01-25 11:18:07 +0100322static void __iomem *chan_base(struct d40_chan *chan)
323{
324 return chan->base->virtbase + D40_DREG_PCBASE +
325 chan->phy_chan->num * D40_DREG_PCDELTA;
326}
327
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100328#define d40_err(dev, format, arg...) \
329 dev_err(dev, "[%s] " format, __func__, ## arg)
330
331#define chan_err(d40c, format, arg...) \
332 d40_err(chan2dev(d40c), format, ## arg)
333
Rabin Vincentb00f9382011-01-25 11:18:15 +0100334static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100335 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200336{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100337 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200338 u32 align;
339 void *base;
340
341 if (is_log)
342 align = sizeof(struct d40_log_lli);
343 else
344 align = sizeof(struct d40_phy_lli);
345
346 if (lli_len == 1) {
347 base = d40d->lli_pool.pre_alloc_lli;
348 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
349 d40d->lli_pool.base = NULL;
350 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100351 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200352
353 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
354 d40d->lli_pool.base = base;
355
356 if (d40d->lli_pool.base == NULL)
357 return -ENOMEM;
358 }
359
360 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100361 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100362 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100363
364 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200365 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100366 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100367 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100368
369 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
370 d40d->lli_phy.src,
371 d40d->lli_pool.size,
372 DMA_TO_DEVICE);
373
374 if (dma_mapping_error(d40c->base->dev,
375 d40d->lli_pool.dma_addr)) {
376 kfree(d40d->lli_pool.base);
377 d40d->lli_pool.base = NULL;
378 d40d->lli_pool.dma_addr = 0;
379 return -ENOMEM;
380 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200381 }
382
383 return 0;
384}
385
Rabin Vincentb00f9382011-01-25 11:18:15 +0100386static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200387{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100388 if (d40d->lli_pool.dma_addr)
389 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
390 d40d->lli_pool.size, DMA_TO_DEVICE);
391
Linus Walleij8d318a52010-03-30 15:33:42 +0200392 kfree(d40d->lli_pool.base);
393 d40d->lli_pool.base = NULL;
394 d40d->lli_pool.size = 0;
395 d40d->lli_log.src = NULL;
396 d40d->lli_log.dst = NULL;
397 d40d->lli_phy.src = NULL;
398 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200399}
400
Jonas Aaberg698e4732010-08-09 12:08:56 +0000401static int d40_lcla_alloc_one(struct d40_chan *d40c,
402 struct d40_desc *d40d)
403{
404 unsigned long flags;
405 int i;
406 int ret = -EINVAL;
407 int p;
408
409 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
410
411 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
412
413 /*
414 * Allocate both src and dst at the same time, therefore the half
415 * start on 1 since 0 can't be used since zero is used as end marker.
416 */
417 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
418 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
419 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
420 d40d->lcla_alloc++;
421 ret = i;
422 break;
423 }
424 }
425
426 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
427
428 return ret;
429}
430
431static int d40_lcla_free_all(struct d40_chan *d40c,
432 struct d40_desc *d40d)
433{
434 unsigned long flags;
435 int i;
436 int ret = -EINVAL;
437
Rabin Vincent724a8572011-01-25 11:18:08 +0100438 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000439 return 0;
440
441 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
442
443 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
444 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
445 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
446 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
447 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
448 d40d->lcla_alloc--;
449 if (d40d->lcla_alloc == 0) {
450 ret = 0;
451 break;
452 }
453 }
454 }
455
456 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
457
458 return ret;
459
460}
461
Linus Walleij8d318a52010-03-30 15:33:42 +0200462static void d40_desc_remove(struct d40_desc *d40d)
463{
464 list_del(&d40d->node);
465}
466
467static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
468{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000469 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200470
471 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000472 struct d40_desc *d;
473 struct d40_desc *_d;
474
Linus Walleij8d318a52010-03-30 15:33:42 +0200475 list_for_each_entry_safe(d, _d, &d40c->client, node)
476 if (async_tx_test_ack(&d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +0100477 d40_pool_lli_free(d40c, d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200478 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000479 desc = d;
480 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000481 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200482 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200483 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000484
485 if (!desc)
486 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
487
488 if (desc)
489 INIT_LIST_HEAD(&desc->node);
490
491 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200492}
493
494static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
495{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000496
Rabin Vincentb00f9382011-01-25 11:18:15 +0100497 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000498 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000499 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200500}
501
502static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
503{
504 list_add_tail(&desc->node, &d40c->active);
505}
506
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100507static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
508{
509 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
510 struct d40_phy_lli *lli_src = desc->lli_phy.src;
511 void __iomem *base = chan_base(chan);
512
513 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
514 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
515 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
516 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
517
518 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
519 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
520 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
521 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
522}
523
Rabin Vincente65889c2011-01-25 11:18:31 +0100524static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
525{
526 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
527 struct d40_log_lli_bidir *lli = &desc->lli_log;
528 int lli_current = desc->lli_current;
529 int lli_len = desc->lli_len;
530 int curr_lcla = -EINVAL;
531
532 if (lli_len - lli_current > 1)
533 curr_lcla = d40_lcla_alloc_one(chan, desc);
534
535 d40_log_lli_lcpa_write(chan->lcpa,
536 &lli->dst[lli_current],
537 &lli->src[lli_current],
538 curr_lcla);
539
540 lli_current++;
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100541
542 if (curr_lcla < 0)
543 goto out;
544
Rabin Vincente65889c2011-01-25 11:18:31 +0100545 for (; lli_current < lli_len; lli_current++) {
546 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
547 8 * curr_lcla * 2;
548 struct d40_log_lli *lcla = pool->base + lcla_offset;
549 int next_lcla;
550
551 if (lli_current + 1 < lli_len)
552 next_lcla = d40_lcla_alloc_one(chan, desc);
553 else
554 next_lcla = -EINVAL;
555
556 d40_log_lli_lcla_write(lcla,
557 &lli->dst[lli_current],
558 &lli->src[lli_current],
559 next_lcla);
560
561 dma_sync_single_range_for_device(chan->base->dev,
562 pool->dma_addr, lcla_offset,
563 2 * sizeof(struct d40_log_lli),
564 DMA_TO_DEVICE);
565
566 curr_lcla = next_lcla;
567
568 if (curr_lcla == -EINVAL) {
569 lli_current++;
570 break;
571 }
572 }
573
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100574out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100575 desc->lli_current = lli_current;
576}
577
Jonas Aaberg698e4732010-08-09 12:08:56 +0000578static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
579{
Rabin Vincent724a8572011-01-25 11:18:08 +0100580 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100581 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000582 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100583 } else
584 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000585}
586
Linus Walleij8d318a52010-03-30 15:33:42 +0200587static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
588{
589 struct d40_desc *d;
590
591 if (list_empty(&d40c->active))
592 return NULL;
593
594 d = list_first_entry(&d40c->active,
595 struct d40_desc,
596 node);
597 return d;
598}
599
600static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
601{
602 list_add_tail(&desc->node, &d40c->queue);
603}
604
605static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
606{
607 struct d40_desc *d;
608
609 if (list_empty(&d40c->queue))
610 return NULL;
611
612 d = list_first_entry(&d40c->queue,
613 struct d40_desc,
614 node);
615 return d;
616}
617
Per Forlind49278e2010-12-20 18:31:38 +0100618static int d40_psize_2_burst_size(bool is_log, int psize)
619{
620 if (is_log) {
621 if (psize == STEDMA40_PSIZE_LOG_1)
622 return 1;
623 } else {
624 if (psize == STEDMA40_PSIZE_PHY_1)
625 return 1;
626 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200627
Per Forlind49278e2010-12-20 18:31:38 +0100628 return 2 << psize;
629}
630
631/*
632 * The dma only supports transmitting packages up to
633 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
634 * dma elements required to send the entire sg list
635 */
636static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
637{
638 int dmalen;
639 u32 max_w = max(data_width1, data_width2);
640 u32 min_w = min(data_width1, data_width2);
641 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
642
643 if (seg_max > STEDMA40_MAX_SEG_SIZE)
644 seg_max -= (1 << max_w);
645
646 if (!IS_ALIGNED(size, 1 << max_w))
647 return -EINVAL;
648
649 if (size <= seg_max)
650 dmalen = 1;
651 else {
652 dmalen = size / seg_max;
653 if (dmalen * seg_max < size)
654 dmalen++;
655 }
656 return dmalen;
657}
658
659static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
660 u32 data_width1, u32 data_width2)
661{
662 struct scatterlist *sg;
663 int i;
664 int len = 0;
665 int ret;
666
667 for_each_sg(sgl, sg, sg_len, i) {
668 ret = d40_size_2_dmalen(sg_dma_len(sg),
669 data_width1, data_width2);
670 if (ret < 0)
671 return ret;
672 len += ret;
673 }
674 return len;
675}
676
677/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200678
679static int d40_channel_execute_command(struct d40_chan *d40c,
680 enum d40_command command)
681{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000682 u32 status;
683 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200684 void __iomem *active_reg;
685 int ret = 0;
686 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000687 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200688
689 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
690
691 if (d40c->phy_chan->num % 2 == 0)
692 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
693 else
694 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
695
696 if (command == D40_DMA_SUSPEND_REQ) {
697 status = (readl(active_reg) &
698 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
699 D40_CHAN_POS(d40c->phy_chan->num);
700
701 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
702 goto done;
703 }
704
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000705 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
706 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
707 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200708
709 if (command == D40_DMA_SUSPEND_REQ) {
710
711 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
712 status = (readl(active_reg) &
713 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
714 D40_CHAN_POS(d40c->phy_chan->num);
715
716 cpu_relax();
717 /*
718 * Reduce the number of bus accesses while
719 * waiting for the DMA to suspend.
720 */
721 udelay(3);
722
723 if (status == D40_DMA_STOP ||
724 status == D40_DMA_SUSPENDED)
725 break;
726 }
727
728 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100729 chan_err(d40c,
730 "unable to suspend the chl %d (log: %d) status %x\n",
731 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200732 status);
733 dump_stack();
734 ret = -EBUSY;
735 }
736
737 }
738done:
739 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
740 return ret;
741}
742
743static void d40_term_all(struct d40_chan *d40c)
744{
745 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200746
747 /* Release active descriptors */
748 while ((d40d = d40_first_active_get(d40c))) {
749 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200750 d40_desc_free(d40c, d40d);
751 }
752
753 /* Release queued descriptors waiting for transfer */
754 while ((d40d = d40_first_queued(d40c))) {
755 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200756 d40_desc_free(d40c, d40d);
757 }
758
Linus Walleij8d318a52010-03-30 15:33:42 +0200759
760 d40c->pending_tx = 0;
761 d40c->busy = false;
762}
763
Rabin Vincent262d2912011-01-25 11:18:05 +0100764static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
765 u32 event, int reg)
766{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100767 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100768 int tries;
769
770 if (!enable) {
771 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
772 | ~D40_EVENTLINE_MASK(event), addr);
773 return;
774 }
775
776 /*
777 * The hardware sometimes doesn't register the enable when src and dst
778 * event lines are active on the same logical channel. Retry to ensure
779 * it does. Usually only one retry is sufficient.
780 */
781 tries = 100;
782 while (--tries) {
783 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
784 | ~D40_EVENTLINE_MASK(event), addr);
785
786 if (readl(addr) & D40_EVENTLINE_MASK(event))
787 break;
788 }
789
790 if (tries != 99)
791 dev_dbg(chan2dev(d40c),
792 "[%s] workaround enable S%cLNK (%d tries)\n",
793 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
794 100 - tries);
795
796 WARN_ON(!tries);
797}
798
Linus Walleij8d318a52010-03-30 15:33:42 +0200799static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
800{
Linus Walleij8d318a52010-03-30 15:33:42 +0200801 unsigned long flags;
802
Linus Walleij8d318a52010-03-30 15:33:42 +0200803 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
804
805 /* Enable event line connected to device (or memcpy) */
806 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
807 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
808 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
809
Rabin Vincent262d2912011-01-25 11:18:05 +0100810 __d40_config_set_event(d40c, do_enable, event,
811 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200812 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100813
Linus Walleij8d318a52010-03-30 15:33:42 +0200814 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
815 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
816
Rabin Vincent262d2912011-01-25 11:18:05 +0100817 __d40_config_set_event(d40c, do_enable, event,
818 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200819 }
820
821 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
822}
823
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200824static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200825{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100826 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000827 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200828
Rabin Vincent8ca84682011-01-25 11:18:07 +0100829 val = readl(chanbase + D40_CHAN_REG_SSLNK);
830 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200831
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200832 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200833}
834
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000835static u32 d40_get_prmo(struct d40_chan *d40c)
836{
837 static const unsigned int phy_map[] = {
838 [STEDMA40_PCHAN_BASIC_MODE]
839 = D40_DREG_PRMO_PCHAN_BASIC,
840 [STEDMA40_PCHAN_MODULO_MODE]
841 = D40_DREG_PRMO_PCHAN_MODULO,
842 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
843 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
844 };
845 static const unsigned int log_map[] = {
846 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
847 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
848 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
849 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
850 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
851 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
852 };
853
Rabin Vincent724a8572011-01-25 11:18:08 +0100854 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000855 return phy_map[d40c->dma_cfg.mode_opt];
856 else
857 return log_map[d40c->dma_cfg.mode_opt];
858}
859
Jonas Aabergb55912c2010-08-09 12:08:02 +0000860static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200861{
862 u32 addr_base;
863 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200864
865 /* Odd addresses are even addresses + 4 */
866 addr_base = (d40c->phy_chan->num % 2) * 4;
867 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100868 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200869 D40_CHAN_POS(d40c->phy_chan->num);
870 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
871
872 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000873 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200874
875 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
876
Rabin Vincent724a8572011-01-25 11:18:08 +0100877 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100878 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
879 & D40_SREG_ELEM_LOG_LIDX_MASK;
880 void __iomem *chanbase = chan_base(d40c);
881
Linus Walleij8d318a52010-03-30 15:33:42 +0200882 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100883 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
884 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200885
Jonas Aabergb55912c2010-08-09 12:08:02 +0000886 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100887 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
888 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200889 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200890}
891
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000892static u32 d40_residue(struct d40_chan *d40c)
893{
894 u32 num_elt;
895
Rabin Vincent724a8572011-01-25 11:18:08 +0100896 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000897 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
898 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100899 else {
900 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
901 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
902 >> D40_SREG_ELEM_PHY_ECNT_POS;
903 }
904
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000905 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
906}
907
908static bool d40_tx_is_linked(struct d40_chan *d40c)
909{
910 bool is_link;
911
Rabin Vincent724a8572011-01-25 11:18:08 +0100912 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000913 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
914 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100915 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
916 & D40_SREG_LNK_PHYS_LNK_MASK;
917
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000918 return is_link;
919}
920
Rabin Vincent86eb5fb2011-01-25 11:18:34 +0100921static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000922{
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000923 int res = 0;
924 unsigned long flags;
925
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000926 if (!d40c->busy)
927 return 0;
928
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000929 spin_lock_irqsave(&d40c->lock, flags);
930
931 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
932 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +0100933 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000934 d40_config_set_event(d40c, false);
935 /* Resume the other logical channels if any */
936 if (d40_chan_has_events(d40c))
937 res = d40_channel_execute_command(d40c,
938 D40_DMA_RUN);
939 }
940 }
941
942 spin_unlock_irqrestore(&d40c->lock, flags);
943 return res;
944}
945
Rabin Vincent86eb5fb2011-01-25 11:18:34 +0100946static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000947{
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000948 int res = 0;
949 unsigned long flags;
950
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000951 if (!d40c->busy)
952 return 0;
953
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000954 spin_lock_irqsave(&d40c->lock, flags);
955
956 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +0100957 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000958 res = d40_channel_execute_command(d40c,
959 D40_DMA_SUSPEND_REQ);
960 goto no_suspend;
961 }
962
963 /* If bytes left to transfer or linked tx resume job */
964 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
965
Rabin Vincent724a8572011-01-25 11:18:08 +0100966 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000967 d40_config_set_event(d40c, true);
968
969 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
970 }
971
972no_suspend:
973 spin_unlock_irqrestore(&d40c->lock, flags);
974 return res;
975}
976
Rabin Vincent86eb5fb2011-01-25 11:18:34 +0100977static int d40_terminate_all(struct d40_chan *chan)
978{
979 unsigned long flags;
980 int ret = 0;
981
982 ret = d40_pause(chan);
983 if (!ret && chan_is_physical(chan))
984 ret = d40_channel_execute_command(chan, D40_DMA_STOP);
985
986 spin_lock_irqsave(&chan->lock, flags);
987 d40_term_all(chan);
988 spin_unlock_irqrestore(&chan->lock, flags);
989
990 return ret;
991}
992
Linus Walleij8d318a52010-03-30 15:33:42 +0200993static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
994{
995 struct d40_chan *d40c = container_of(tx->chan,
996 struct d40_chan,
997 chan);
998 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
999 unsigned long flags;
1000
1001 spin_lock_irqsave(&d40c->lock, flags);
1002
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001003 d40c->chan.cookie++;
1004
1005 if (d40c->chan.cookie < 0)
1006 d40c->chan.cookie = 1;
1007
1008 d40d->txd.cookie = d40c->chan.cookie;
1009
Linus Walleij8d318a52010-03-30 15:33:42 +02001010 d40_desc_queue(d40c, d40d);
1011
1012 spin_unlock_irqrestore(&d40c->lock, flags);
1013
1014 return tx->cookie;
1015}
1016
1017static int d40_start(struct d40_chan *d40c)
1018{
Linus Walleijf4185592010-06-22 18:06:42 -07001019 if (d40c->base->rev == 0) {
1020 int err;
1021
Rabin Vincent724a8572011-01-25 11:18:08 +01001022 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -07001023 err = d40_channel_execute_command(d40c,
1024 D40_DMA_SUSPEND_REQ);
1025 if (err)
1026 return err;
1027 }
1028 }
1029
Rabin Vincent724a8572011-01-25 11:18:08 +01001030 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001031 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001032
Jonas Aaberg0c322692010-06-20 21:25:46 +00001033 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001034}
1035
1036static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1037{
1038 struct d40_desc *d40d;
1039 int err;
1040
1041 /* Start queued jobs, if any */
1042 d40d = d40_first_queued(d40c);
1043
1044 if (d40d != NULL) {
1045 d40c->busy = true;
1046
1047 /* Remove from queue */
1048 d40_desc_remove(d40d);
1049
1050 /* Add to active queue */
1051 d40_desc_submit(d40c, d40d);
1052
Rabin Vincent7d83a852011-01-25 11:18:06 +01001053 /* Initiate DMA job */
1054 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001055
Rabin Vincent7d83a852011-01-25 11:18:06 +01001056 /* Start dma job */
1057 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001058
Rabin Vincent7d83a852011-01-25 11:18:06 +01001059 if (err)
1060 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001061 }
1062
1063 return d40d;
1064}
1065
1066/* called from interrupt context */
1067static void dma_tc_handle(struct d40_chan *d40c)
1068{
1069 struct d40_desc *d40d;
1070
Linus Walleij8d318a52010-03-30 15:33:42 +02001071 /* Get first active entry from list */
1072 d40d = d40_first_active_get(d40c);
1073
1074 if (d40d == NULL)
1075 return;
1076
Jonas Aaberg698e4732010-08-09 12:08:56 +00001077 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001078
Jonas Aaberg698e4732010-08-09 12:08:56 +00001079 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001080 d40_desc_load(d40c, d40d);
1081 /* Start dma job */
1082 (void) d40_start(d40c);
1083 return;
1084 }
1085
1086 if (d40_queue_start(d40c) == NULL)
1087 d40c->busy = false;
1088
1089 d40c->pending_tx++;
1090 tasklet_schedule(&d40c->tasklet);
1091
1092}
1093
1094static void dma_tasklet(unsigned long data)
1095{
1096 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001097 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001098 unsigned long flags;
1099 dma_async_tx_callback callback;
1100 void *callback_param;
1101
1102 spin_lock_irqsave(&d40c->lock, flags);
1103
1104 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001105 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001106
Jonas Aaberg767a9672010-08-09 12:08:34 +00001107 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001108 goto err;
1109
Jonas Aaberg767a9672010-08-09 12:08:34 +00001110 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001111
1112 /*
1113 * If terminating a channel pending_tx is set to zero.
1114 * This prevents any finished active jobs to return to the client.
1115 */
1116 if (d40c->pending_tx == 0) {
1117 spin_unlock_irqrestore(&d40c->lock, flags);
1118 return;
1119 }
1120
1121 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001122 callback = d40d->txd.callback;
1123 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001124
Jonas Aaberg767a9672010-08-09 12:08:34 +00001125 if (async_tx_test_ack(&d40d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001126 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001127 d40_desc_remove(d40d);
1128 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001129 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001130 if (!d40d->is_in_client_list) {
1131 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001132 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001133 list_add_tail(&d40d->node, &d40c->client);
1134 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001135 }
1136 }
1137
1138 d40c->pending_tx--;
1139
1140 if (d40c->pending_tx)
1141 tasklet_schedule(&d40c->tasklet);
1142
1143 spin_unlock_irqrestore(&d40c->lock, flags);
1144
Jonas Aaberg767a9672010-08-09 12:08:34 +00001145 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001146 callback(callback_param);
1147
1148 return;
1149
1150 err:
1151 /* Rescue manouver if receiving double interrupts */
1152 if (d40c->pending_tx > 0)
1153 d40c->pending_tx--;
1154 spin_unlock_irqrestore(&d40c->lock, flags);
1155}
1156
1157static irqreturn_t d40_handle_interrupt(int irq, void *data)
1158{
1159 static const struct d40_interrupt_lookup il[] = {
1160 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1161 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1162 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1163 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1164 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1165 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1166 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1167 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1168 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1169 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1170 };
1171
1172 int i;
1173 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001174 u32 idx;
1175 u32 row;
1176 long chan = -1;
1177 struct d40_chan *d40c;
1178 unsigned long flags;
1179 struct d40_base *base = data;
1180
1181 spin_lock_irqsave(&base->interrupt_lock, flags);
1182
1183 /* Read interrupt status of both logical and physical channels */
1184 for (i = 0; i < ARRAY_SIZE(il); i++)
1185 regs[i] = readl(base->virtbase + il[i].src);
1186
1187 for (;;) {
1188
1189 chan = find_next_bit((unsigned long *)regs,
1190 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1191
1192 /* No more set bits found? */
1193 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1194 break;
1195
1196 row = chan / BITS_PER_LONG;
1197 idx = chan & (BITS_PER_LONG - 1);
1198
1199 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001200 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001201
1202 if (il[row].offset == D40_PHY_CHAN)
1203 d40c = base->lookup_phy_chans[idx];
1204 else
1205 d40c = base->lookup_log_chans[il[row].offset + idx];
1206 spin_lock(&d40c->lock);
1207
1208 if (!il[row].is_error)
1209 dma_tc_handle(d40c);
1210 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001211 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1212 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001213
1214 spin_unlock(&d40c->lock);
1215 }
1216
1217 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1218
1219 return IRQ_HANDLED;
1220}
1221
Linus Walleij8d318a52010-03-30 15:33:42 +02001222static int d40_validate_conf(struct d40_chan *d40c,
1223 struct stedma40_chan_cfg *conf)
1224{
1225 int res = 0;
1226 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1227 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001228 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001229
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001230 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001231 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001232 res = -EINVAL;
1233 }
1234
1235 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1236 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1237 d40c->runtime_addr == 0) {
1238
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001239 chan_err(d40c, "Invalid TX channel address (%d)\n",
1240 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001241 res = -EINVAL;
1242 }
1243
1244 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1245 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1246 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001247 chan_err(d40c, "Invalid RX channel address (%d)\n",
1248 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001249 res = -EINVAL;
1250 }
1251
1252 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001253 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001254 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001255 res = -EINVAL;
1256 }
1257
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001258 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001259 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001260 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001261 res = -EINVAL;
1262 }
1263
1264 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1265 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001266 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001267 res = -EINVAL;
1268 }
1269
1270 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1271 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001272 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001273 res = -EINVAL;
1274 }
1275
1276 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1277 /*
1278 * DMAC HW supports it. Will be added to this driver,
1279 * in case any dma client requires it.
1280 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001281 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001282 res = -EINVAL;
1283 }
1284
Per Forlind49278e2010-12-20 18:31:38 +01001285 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1286 (1 << conf->src_info.data_width) !=
1287 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1288 (1 << conf->dst_info.data_width)) {
1289 /*
1290 * The DMAC hardware only supports
1291 * src (burst x width) == dst (burst x width)
1292 */
1293
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001294 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001295 res = -EINVAL;
1296 }
1297
Linus Walleij8d318a52010-03-30 15:33:42 +02001298 return res;
1299}
1300
1301static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001302 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001303{
1304 unsigned long flags;
1305 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001306 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001307 /* Physical interrupts are masked per physical full channel */
1308 if (phy->allocated_src == D40_ALLOC_FREE &&
1309 phy->allocated_dst == D40_ALLOC_FREE) {
1310 phy->allocated_dst = D40_ALLOC_PHY;
1311 phy->allocated_src = D40_ALLOC_PHY;
1312 goto found;
1313 } else
1314 goto not_found;
1315 }
1316
1317 /* Logical channel */
1318 if (is_src) {
1319 if (phy->allocated_src == D40_ALLOC_PHY)
1320 goto not_found;
1321
1322 if (phy->allocated_src == D40_ALLOC_FREE)
1323 phy->allocated_src = D40_ALLOC_LOG_FREE;
1324
1325 if (!(phy->allocated_src & (1 << log_event_line))) {
1326 phy->allocated_src |= 1 << log_event_line;
1327 goto found;
1328 } else
1329 goto not_found;
1330 } else {
1331 if (phy->allocated_dst == D40_ALLOC_PHY)
1332 goto not_found;
1333
1334 if (phy->allocated_dst == D40_ALLOC_FREE)
1335 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1336
1337 if (!(phy->allocated_dst & (1 << log_event_line))) {
1338 phy->allocated_dst |= 1 << log_event_line;
1339 goto found;
1340 } else
1341 goto not_found;
1342 }
1343
1344not_found:
1345 spin_unlock_irqrestore(&phy->lock, flags);
1346 return false;
1347found:
1348 spin_unlock_irqrestore(&phy->lock, flags);
1349 return true;
1350}
1351
1352static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1353 int log_event_line)
1354{
1355 unsigned long flags;
1356 bool is_free = false;
1357
1358 spin_lock_irqsave(&phy->lock, flags);
1359 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001360 phy->allocated_dst = D40_ALLOC_FREE;
1361 phy->allocated_src = D40_ALLOC_FREE;
1362 is_free = true;
1363 goto out;
1364 }
1365
1366 /* Logical channel */
1367 if (is_src) {
1368 phy->allocated_src &= ~(1 << log_event_line);
1369 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1370 phy->allocated_src = D40_ALLOC_FREE;
1371 } else {
1372 phy->allocated_dst &= ~(1 << log_event_line);
1373 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1374 phy->allocated_dst = D40_ALLOC_FREE;
1375 }
1376
1377 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1378 D40_ALLOC_FREE);
1379
1380out:
1381 spin_unlock_irqrestore(&phy->lock, flags);
1382
1383 return is_free;
1384}
1385
1386static int d40_allocate_channel(struct d40_chan *d40c)
1387{
1388 int dev_type;
1389 int event_group;
1390 int event_line;
1391 struct d40_phy_res *phys;
1392 int i;
1393 int j;
1394 int log_num;
1395 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001396 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001397
1398 phys = d40c->base->phy_res;
1399
1400 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1401 dev_type = d40c->dma_cfg.src_dev_type;
1402 log_num = 2 * dev_type;
1403 is_src = true;
1404 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1405 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1406 /* dst event lines are used for logical memcpy */
1407 dev_type = d40c->dma_cfg.dst_dev_type;
1408 log_num = 2 * dev_type + 1;
1409 is_src = false;
1410 } else
1411 return -EINVAL;
1412
1413 event_group = D40_TYPE_TO_GROUP(dev_type);
1414 event_line = D40_TYPE_TO_EVENT(dev_type);
1415
1416 if (!is_log) {
1417 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1418 /* Find physical half channel */
1419 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1420
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001421 if (d40_alloc_mask_set(&phys[i], is_src,
1422 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001423 goto found_phy;
1424 }
1425 } else
1426 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1427 int phy_num = j + event_group * 2;
1428 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001429 if (d40_alloc_mask_set(&phys[i],
1430 is_src,
1431 0,
1432 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001433 goto found_phy;
1434 }
1435 }
1436 return -EINVAL;
1437found_phy:
1438 d40c->phy_chan = &phys[i];
1439 d40c->log_num = D40_PHY_CHAN;
1440 goto out;
1441 }
1442 if (dev_type == -1)
1443 return -EINVAL;
1444
1445 /* Find logical channel */
1446 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1447 int phy_num = j + event_group * 2;
1448 /*
1449 * Spread logical channels across all available physical rather
1450 * than pack every logical channel at the first available phy
1451 * channels.
1452 */
1453 if (is_src) {
1454 for (i = phy_num; i < phy_num + 2; i++) {
1455 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001456 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001457 goto found_log;
1458 }
1459 } else {
1460 for (i = phy_num + 1; i >= phy_num; i--) {
1461 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001462 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001463 goto found_log;
1464 }
1465 }
1466 }
1467 return -EINVAL;
1468
1469found_log:
1470 d40c->phy_chan = &phys[i];
1471 d40c->log_num = log_num;
1472out:
1473
1474 if (is_log)
1475 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1476 else
1477 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1478
1479 return 0;
1480
1481}
1482
Linus Walleij8d318a52010-03-30 15:33:42 +02001483static int d40_config_memcpy(struct d40_chan *d40c)
1484{
1485 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1486
1487 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1488 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1489 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1490 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1491 memcpy[d40c->chan.chan_id];
1492
1493 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1494 dma_has_cap(DMA_SLAVE, cap)) {
1495 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1496 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001497 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001498 return -EINVAL;
1499 }
1500
1501 return 0;
1502}
1503
1504
1505static int d40_free_dma(struct d40_chan *d40c)
1506{
1507
1508 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001509 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001510 struct d40_phy_res *phy = d40c->phy_chan;
1511 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001512 struct d40_desc *d;
1513 struct d40_desc *_d;
1514
Linus Walleij8d318a52010-03-30 15:33:42 +02001515
1516 /* Terminate all queued and active transfers */
1517 d40_term_all(d40c);
1518
Per Fridena8be8622010-06-20 21:24:59 +00001519 /* Release client owned descriptors */
1520 if (!list_empty(&d40c->client))
1521 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001522 d40_pool_lli_free(d40c, d);
Per Fridena8be8622010-06-20 21:24:59 +00001523 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001524 d40_desc_free(d40c, d);
1525 }
1526
Linus Walleij8d318a52010-03-30 15:33:42 +02001527 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001528 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001529 return -EINVAL;
1530 }
1531
1532 if (phy->allocated_src == D40_ALLOC_FREE &&
1533 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001534 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 return -EINVAL;
1536 }
1537
Linus Walleij8d318a52010-03-30 15:33:42 +02001538 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1539 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1540 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001541 is_src = false;
1542 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1543 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001544 is_src = true;
1545 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001546 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001547 return -EINVAL;
1548 }
1549
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001550 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1551 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001552 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001553 return res;
1554 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001555
Rabin Vincent724a8572011-01-25 11:18:08 +01001556 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001557 /* Release logical channel, deactivate the event line */
1558
1559 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001560 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1561
1562 /*
1563 * Check if there are more logical allocation
1564 * on this phy channel.
1565 */
1566 if (!d40_alloc_mask_free(phy, is_src, event)) {
1567 /* Resume the other logical channels if any */
1568 if (d40_chan_has_events(d40c)) {
1569 res = d40_channel_execute_command(d40c,
1570 D40_DMA_RUN);
1571 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001572 chan_err(d40c,
1573 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001574 return res;
1575 }
1576 }
1577 return 0;
1578 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001579 } else {
1580 (void) d40_alloc_mask_free(phy, is_src, 0);
1581 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001582
1583 /* Release physical channel */
1584 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1585 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001586 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001587 return res;
1588 }
1589 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001590 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001591 d40c->base->lookup_phy_chans[phy->num] = NULL;
1592
1593 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001594}
1595
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001596static bool d40_is_paused(struct d40_chan *d40c)
1597{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001598 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001599 bool is_paused = false;
1600 unsigned long flags;
1601 void __iomem *active_reg;
1602 u32 status;
1603 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001604
1605 spin_lock_irqsave(&d40c->lock, flags);
1606
Rabin Vincent724a8572011-01-25 11:18:08 +01001607 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001608 if (d40c->phy_chan->num % 2 == 0)
1609 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1610 else
1611 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1612
1613 status = (readl(active_reg) &
1614 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1615 D40_CHAN_POS(d40c->phy_chan->num);
1616 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1617 is_paused = true;
1618
1619 goto _exit;
1620 }
1621
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001622 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001623 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001624 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001625 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001626 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001627 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001628 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001629 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001630 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001631 goto _exit;
1632 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001633
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001634 status = (status & D40_EVENTLINE_MASK(event)) >>
1635 D40_EVENTLINE_POS(event);
1636
1637 if (status != D40_DMA_RUN)
1638 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001639_exit:
1640 spin_unlock_irqrestore(&d40c->lock, flags);
1641 return is_paused;
1642
1643}
1644
1645
Linus Walleij8d318a52010-03-30 15:33:42 +02001646static u32 stedma40_residue(struct dma_chan *chan)
1647{
1648 struct d40_chan *d40c =
1649 container_of(chan, struct d40_chan, chan);
1650 u32 bytes_left;
1651 unsigned long flags;
1652
1653 spin_lock_irqsave(&d40c->lock, flags);
1654 bytes_left = d40_residue(d40c);
1655 spin_unlock_irqrestore(&d40c->lock, flags);
1656
1657 return bytes_left;
1658}
1659
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001660static int
1661d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1662 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001663 unsigned int sg_len, dma_addr_t src_dev_addr,
1664 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001665{
1666 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1667 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1668 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001669 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001670
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001671 ret = d40_log_sg_to_lli(sg_src, sg_len,
1672 src_dev_addr,
1673 desc->lli_log.src,
1674 chan->log_def.lcsp1,
1675 src_info->data_width,
1676 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001677
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001678 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1679 dst_dev_addr,
1680 desc->lli_log.dst,
1681 chan->log_def.lcsp3,
1682 dst_info->data_width,
1683 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001684
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001685 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001686}
1687
1688static int
1689d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1690 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001691 unsigned int sg_len, dma_addr_t src_dev_addr,
1692 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001693{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001694 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1695 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1696 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
1697 int ret;
1698
1699 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1700 desc->lli_phy.src,
1701 virt_to_phys(desc->lli_phy.src),
1702 chan->src_def_cfg,
Rabin Vincentcc31b6f2011-01-25 11:18:27 +01001703 src_info, dst_info);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001704
1705 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1706 desc->lli_phy.dst,
1707 virt_to_phys(desc->lli_phy.dst),
1708 chan->dst_def_cfg,
Rabin Vincentcc31b6f2011-01-25 11:18:27 +01001709 dst_info, src_info);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001710
1711 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1712 desc->lli_pool.size, DMA_TO_DEVICE);
1713
1714 return ret < 0 ? ret : 0;
1715}
1716
1717
Rabin Vincent5f811582011-01-25 11:18:18 +01001718static struct d40_desc *
1719d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1720 unsigned int sg_len, unsigned long dma_flags)
1721{
1722 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1723 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001724 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01001725
1726 desc = d40_desc_get(chan);
1727 if (!desc)
1728 return NULL;
1729
1730 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1731 cfg->dst_info.data_width);
1732 if (desc->lli_len < 0) {
1733 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01001734 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01001735 }
1736
Rabin Vincentdbd88782011-01-25 11:18:19 +01001737 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1738 if (ret < 0) {
1739 chan_err(chan, "Could not allocate lli\n");
1740 goto err;
1741 }
1742
1743
Rabin Vincent5f811582011-01-25 11:18:18 +01001744 desc->lli_current = 0;
1745 desc->txd.flags = dma_flags;
1746 desc->txd.tx_submit = d40_tx_submit;
1747
1748 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1749
1750 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001751
1752err:
1753 d40_desc_free(chan, desc);
1754 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01001755}
1756
Rabin Vincentcade1d32011-01-25 11:18:23 +01001757static dma_addr_t
1758d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02001759{
Rabin Vincentcade1d32011-01-25 11:18:23 +01001760 struct stedma40_platform_data *plat = chan->base->plat_data;
1761 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1762 dma_addr_t addr;
Linus Walleij8d318a52010-03-30 15:33:42 +02001763
Rabin Vincentcade1d32011-01-25 11:18:23 +01001764 if (chan->runtime_addr)
1765 return chan->runtime_addr;
1766
1767 if (direction == DMA_FROM_DEVICE)
1768 addr = plat->dev_rx[cfg->src_dev_type];
1769 else if (direction == DMA_TO_DEVICE)
1770 addr = plat->dev_tx[cfg->dst_dev_type];
1771
1772 return addr;
1773}
1774
1775static struct dma_async_tx_descriptor *
1776d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
1777 struct scatterlist *sg_dst, unsigned int sg_len,
1778 enum dma_data_direction direction, unsigned long dma_flags)
1779{
1780 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01001781 dma_addr_t src_dev_addr = 0;
1782 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01001783 struct d40_desc *desc;
1784 unsigned long flags;
1785 int ret;
1786
1787 if (!chan->phy_chan) {
1788 chan_err(chan, "Cannot prepare unallocated channel\n");
1789 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001790 }
1791
Rabin Vincentcade1d32011-01-25 11:18:23 +01001792 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001793
Rabin Vincentcade1d32011-01-25 11:18:23 +01001794 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
1795 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001796 goto err;
1797
Rabin Vincent822c5672011-01-25 11:18:28 +01001798 if (direction != DMA_NONE) {
1799 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
1800
1801 if (direction == DMA_FROM_DEVICE)
1802 src_dev_addr = dev_addr;
1803 else if (direction == DMA_TO_DEVICE)
1804 dst_dev_addr = dev_addr;
1805 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01001806
1807 if (chan_is_logical(chan))
1808 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001809 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001810 else
1811 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001812 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01001813
1814 if (ret) {
1815 chan_err(chan, "Failed to prepare %s sg job: %d\n",
1816 chan_is_logical(chan) ? "log" : "phy", ret);
1817 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001818 }
1819
Rabin Vincentcade1d32011-01-25 11:18:23 +01001820 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001821
Rabin Vincentcade1d32011-01-25 11:18:23 +01001822 return &desc->txd;
1823
Linus Walleij8d318a52010-03-30 15:33:42 +02001824err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01001825 if (desc)
1826 d40_desc_free(chan, desc);
1827 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001828 return NULL;
1829}
Linus Walleij8d318a52010-03-30 15:33:42 +02001830
1831bool stedma40_filter(struct dma_chan *chan, void *data)
1832{
1833 struct stedma40_chan_cfg *info = data;
1834 struct d40_chan *d40c =
1835 container_of(chan, struct d40_chan, chan);
1836 int err;
1837
1838 if (data) {
1839 err = d40_validate_conf(d40c, info);
1840 if (!err)
1841 d40c->dma_cfg = *info;
1842 } else
1843 err = d40_config_memcpy(d40c);
1844
Rabin Vincentce2ca122010-10-12 13:00:49 +00001845 if (!err)
1846 d40c->configured = true;
1847
Linus Walleij8d318a52010-03-30 15:33:42 +02001848 return err == 0;
1849}
1850EXPORT_SYMBOL(stedma40_filter);
1851
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001852static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1853{
1854 bool realtime = d40c->dma_cfg.realtime;
1855 bool highprio = d40c->dma_cfg.high_priority;
1856 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1857 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1858 u32 event = D40_TYPE_TO_EVENT(dev_type);
1859 u32 group = D40_TYPE_TO_GROUP(dev_type);
1860 u32 bit = 1 << event;
1861
1862 /* Destination event lines are stored in the upper halfword */
1863 if (!src)
1864 bit <<= 16;
1865
1866 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1867 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1868}
1869
1870static void d40_set_prio_realtime(struct d40_chan *d40c)
1871{
1872 if (d40c->base->rev < 3)
1873 return;
1874
1875 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1876 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1877 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1878
1879 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1880 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1881 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1882}
1883
Linus Walleij8d318a52010-03-30 15:33:42 +02001884/* DMA ENGINE functions */
1885static int d40_alloc_chan_resources(struct dma_chan *chan)
1886{
1887 int err;
1888 unsigned long flags;
1889 struct d40_chan *d40c =
1890 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001891 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001892 spin_lock_irqsave(&d40c->lock, flags);
1893
1894 d40c->completed = chan->cookie = 1;
1895
Rabin Vincentce2ca122010-10-12 13:00:49 +00001896 /* If no dma configuration is set use default configuration (memcpy) */
1897 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001898 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001899 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001900 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001901 goto fail;
1902 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001903 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001904 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001905
1906 err = d40_allocate_channel(d40c);
1907 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001908 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001909 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001910 }
1911
Linus Walleijef1872e2010-06-20 21:24:52 +00001912 /* Fill in basic CFG register values */
1913 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01001914 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00001915
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001916 d40_set_prio_realtime(d40c);
1917
Rabin Vincent724a8572011-01-25 11:18:08 +01001918 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00001919 d40_log_cfg(&d40c->dma_cfg,
1920 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1921
1922 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1923 d40c->lcpa = d40c->base->lcpa_base +
1924 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1925 else
1926 d40c->lcpa = d40c->base->lcpa_base +
1927 d40c->dma_cfg.dst_dev_type *
1928 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1929 }
1930
1931 /*
1932 * Only write channel configuration to the DMA if the physical
1933 * resource is free. In case of multiple logical channels
1934 * on the same physical resource, only the first write is necessary.
1935 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001936 if (is_free_phy)
1937 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001938fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001939 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001940 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001941}
1942
1943static void d40_free_chan_resources(struct dma_chan *chan)
1944{
1945 struct d40_chan *d40c =
1946 container_of(chan, struct d40_chan, chan);
1947 int err;
1948 unsigned long flags;
1949
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001950 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001951 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001952 return;
1953 }
1954
1955
Linus Walleij8d318a52010-03-30 15:33:42 +02001956 spin_lock_irqsave(&d40c->lock, flags);
1957
1958 err = d40_free_dma(d40c);
1959
1960 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001961 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001962 spin_unlock_irqrestore(&d40c->lock, flags);
1963}
1964
1965static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1966 dma_addr_t dst,
1967 dma_addr_t src,
1968 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001969 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001970{
Rabin Vincent95944c62011-01-25 11:18:17 +01001971 struct scatterlist dst_sg;
1972 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02001973
Rabin Vincent95944c62011-01-25 11:18:17 +01001974 sg_init_table(&dst_sg, 1);
1975 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001976
Rabin Vincent95944c62011-01-25 11:18:17 +01001977 sg_dma_address(&dst_sg) = dst;
1978 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02001979
Rabin Vincent95944c62011-01-25 11:18:17 +01001980 sg_dma_len(&dst_sg) = size;
1981 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001982
Rabin Vincentcade1d32011-01-25 11:18:23 +01001983 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001984}
1985
Ira Snyder0d688662010-09-30 11:46:47 +00001986static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01001987d40_prep_memcpy_sg(struct dma_chan *chan,
1988 struct scatterlist *dst_sg, unsigned int dst_nents,
1989 struct scatterlist *src_sg, unsigned int src_nents,
1990 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00001991{
1992 if (dst_nents != src_nents)
1993 return NULL;
1994
Rabin Vincentcade1d32011-01-25 11:18:23 +01001995 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01001996}
1997
Linus Walleij8d318a52010-03-30 15:33:42 +02001998static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
1999 struct scatterlist *sgl,
2000 unsigned int sg_len,
2001 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002002 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002003{
Rabin Vincent00ac0342011-01-25 11:18:20 +01002004 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
2005 return NULL;
2006
Rabin Vincentcade1d32011-01-25 11:18:23 +01002007 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002008}
2009
2010static enum dma_status d40_tx_status(struct dma_chan *chan,
2011 dma_cookie_t cookie,
2012 struct dma_tx_state *txstate)
2013{
2014 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2015 dma_cookie_t last_used;
2016 dma_cookie_t last_complete;
2017 int ret;
2018
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002019 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002020 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002021 return -EINVAL;
2022 }
2023
Linus Walleij8d318a52010-03-30 15:33:42 +02002024 last_complete = d40c->completed;
2025 last_used = chan->cookie;
2026
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002027 if (d40_is_paused(d40c))
2028 ret = DMA_PAUSED;
2029 else
2030 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002031
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002032 dma_set_tx_state(txstate, last_complete, last_used,
2033 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002034
2035 return ret;
2036}
2037
2038static void d40_issue_pending(struct dma_chan *chan)
2039{
2040 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
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, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002045 return;
2046 }
2047
Linus Walleij8d318a52010-03-30 15:33:42 +02002048 spin_lock_irqsave(&d40c->lock, flags);
2049
2050 /* Busy means that pending jobs are already being processed */
2051 if (!d40c->busy)
2052 (void) d40_queue_start(d40c);
2053
2054 spin_unlock_irqrestore(&d40c->lock, flags);
2055}
2056
Linus Walleij95e14002010-08-04 13:37:45 +02002057/* Runtime reconfiguration extension */
2058static void d40_set_runtime_config(struct dma_chan *chan,
2059 struct dma_slave_config *config)
2060{
2061 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2062 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2063 enum dma_slave_buswidth config_addr_width;
2064 dma_addr_t config_addr;
2065 u32 config_maxburst;
2066 enum stedma40_periph_data_width addr_width;
2067 int psize;
2068
2069 if (config->direction == DMA_FROM_DEVICE) {
2070 dma_addr_t dev_addr_rx =
2071 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2072
2073 config_addr = config->src_addr;
2074 if (dev_addr_rx)
2075 dev_dbg(d40c->base->dev,
2076 "channel has a pre-wired RX address %08x "
2077 "overriding with %08x\n",
2078 dev_addr_rx, config_addr);
2079 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2080 dev_dbg(d40c->base->dev,
2081 "channel was not configured for peripheral "
2082 "to memory transfer (%d) overriding\n",
2083 cfg->dir);
2084 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2085
2086 config_addr_width = config->src_addr_width;
2087 config_maxburst = config->src_maxburst;
2088
2089 } else if (config->direction == DMA_TO_DEVICE) {
2090 dma_addr_t dev_addr_tx =
2091 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2092
2093 config_addr = config->dst_addr;
2094 if (dev_addr_tx)
2095 dev_dbg(d40c->base->dev,
2096 "channel has a pre-wired TX address %08x "
2097 "overriding with %08x\n",
2098 dev_addr_tx, config_addr);
2099 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2100 dev_dbg(d40c->base->dev,
2101 "channel was not configured for memory "
2102 "to peripheral transfer (%d) overriding\n",
2103 cfg->dir);
2104 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2105
2106 config_addr_width = config->dst_addr_width;
2107 config_maxburst = config->dst_maxburst;
2108
2109 } else {
2110 dev_err(d40c->base->dev,
2111 "unrecognized channel direction %d\n",
2112 config->direction);
2113 return;
2114 }
2115
2116 switch (config_addr_width) {
2117 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2118 addr_width = STEDMA40_BYTE_WIDTH;
2119 break;
2120 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2121 addr_width = STEDMA40_HALFWORD_WIDTH;
2122 break;
2123 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2124 addr_width = STEDMA40_WORD_WIDTH;
2125 break;
2126 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2127 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2128 break;
2129 default:
2130 dev_err(d40c->base->dev,
2131 "illegal peripheral address width "
2132 "requested (%d)\n",
2133 config->src_addr_width);
2134 return;
2135 }
2136
Rabin Vincent724a8572011-01-25 11:18:08 +01002137 if (chan_is_logical(d40c)) {
Per Forlina59670a2010-10-06 09:05:27 +00002138 if (config_maxburst >= 16)
2139 psize = STEDMA40_PSIZE_LOG_16;
2140 else if (config_maxburst >= 8)
2141 psize = STEDMA40_PSIZE_LOG_8;
2142 else if (config_maxburst >= 4)
2143 psize = STEDMA40_PSIZE_LOG_4;
2144 else
2145 psize = STEDMA40_PSIZE_LOG_1;
2146 } else {
2147 if (config_maxburst >= 16)
2148 psize = STEDMA40_PSIZE_PHY_16;
2149 else if (config_maxburst >= 8)
2150 psize = STEDMA40_PSIZE_PHY_8;
2151 else if (config_maxburst >= 4)
2152 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002153 else if (config_maxburst >= 2)
2154 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002155 else
2156 psize = STEDMA40_PSIZE_PHY_1;
2157 }
Linus Walleij95e14002010-08-04 13:37:45 +02002158
2159 /* Set up all the endpoint configs */
2160 cfg->src_info.data_width = addr_width;
2161 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002162 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002163 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2164 cfg->dst_info.data_width = addr_width;
2165 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002166 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002167 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2168
Per Forlina59670a2010-10-06 09:05:27 +00002169 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002170 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002171 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2172 else
2173 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2174 &d40c->dst_def_cfg, false);
2175
Linus Walleij95e14002010-08-04 13:37:45 +02002176 /* These settings will take precedence later */
2177 d40c->runtime_addr = config_addr;
2178 d40c->runtime_direction = config->direction;
2179 dev_dbg(d40c->base->dev,
2180 "configured channel %s for %s, data width %d, "
2181 "maxburst %d bytes, LE, no flow control\n",
2182 dma_chan_name(chan),
2183 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2184 config_addr_width,
2185 config_maxburst);
2186}
2187
Linus Walleij05827632010-05-17 16:30:42 -07002188static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2189 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002190{
Linus Walleij8d318a52010-03-30 15:33:42 +02002191 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2192
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002193 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002194 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002195 return -EINVAL;
2196 }
2197
Linus Walleij8d318a52010-03-30 15:33:42 +02002198 switch (cmd) {
2199 case DMA_TERMINATE_ALL:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002200 return d40_terminate_all(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002201 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002202 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002203 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002204 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002205 case DMA_SLAVE_CONFIG:
2206 d40_set_runtime_config(chan,
2207 (struct dma_slave_config *) arg);
2208 return 0;
2209 default:
2210 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002211 }
2212
2213 /* Other commands are unimplemented */
2214 return -ENXIO;
2215}
2216
2217/* Initialization functions */
2218
2219static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2220 struct d40_chan *chans, int offset,
2221 int num_chans)
2222{
2223 int i = 0;
2224 struct d40_chan *d40c;
2225
2226 INIT_LIST_HEAD(&dma->channels);
2227
2228 for (i = offset; i < offset + num_chans; i++) {
2229 d40c = &chans[i];
2230 d40c->base = base;
2231 d40c->chan.device = dma;
2232
Linus Walleij8d318a52010-03-30 15:33:42 +02002233 spin_lock_init(&d40c->lock);
2234
2235 d40c->log_num = D40_PHY_CHAN;
2236
Linus Walleij8d318a52010-03-30 15:33:42 +02002237 INIT_LIST_HEAD(&d40c->active);
2238 INIT_LIST_HEAD(&d40c->queue);
2239 INIT_LIST_HEAD(&d40c->client);
2240
Linus Walleij8d318a52010-03-30 15:33:42 +02002241 tasklet_init(&d40c->tasklet, dma_tasklet,
2242 (unsigned long) d40c);
2243
2244 list_add_tail(&d40c->chan.device_node,
2245 &dma->channels);
2246 }
2247}
2248
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002249static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2250{
2251 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2252 dev->device_prep_slave_sg = d40_prep_slave_sg;
2253
2254 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2255 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2256
2257 /*
2258 * This controller can only access address at even
2259 * 32bit boundaries, i.e. 2^2
2260 */
2261 dev->copy_align = 2;
2262 }
2263
2264 if (dma_has_cap(DMA_SG, dev->cap_mask))
2265 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2266
2267 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2268 dev->device_free_chan_resources = d40_free_chan_resources;
2269 dev->device_issue_pending = d40_issue_pending;
2270 dev->device_tx_status = d40_tx_status;
2271 dev->device_control = d40_control;
2272 dev->dev = base->dev;
2273}
2274
Linus Walleij8d318a52010-03-30 15:33:42 +02002275static int __init d40_dmaengine_init(struct d40_base *base,
2276 int num_reserved_chans)
2277{
2278 int err ;
2279
2280 d40_chan_init(base, &base->dma_slave, base->log_chans,
2281 0, base->num_log_chans);
2282
2283 dma_cap_zero(base->dma_slave.cap_mask);
2284 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2285
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002286 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002287
2288 err = dma_async_device_register(&base->dma_slave);
2289
2290 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002291 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002292 goto failure1;
2293 }
2294
2295 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2296 base->num_log_chans, base->plat_data->memcpy_len);
2297
2298 dma_cap_zero(base->dma_memcpy.cap_mask);
2299 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002300 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002301
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002302 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002303
2304 err = dma_async_device_register(&base->dma_memcpy);
2305
2306 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002307 d40_err(base->dev,
2308 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002309 goto failure2;
2310 }
2311
2312 d40_chan_init(base, &base->dma_both, base->phy_chans,
2313 0, num_reserved_chans);
2314
2315 dma_cap_zero(base->dma_both.cap_mask);
2316 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2317 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002318 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002319
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002320 d40_ops_init(base, &base->dma_both);
2321
Linus Walleij8d318a52010-03-30 15:33:42 +02002322 err = dma_async_device_register(&base->dma_both);
2323
2324 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002325 d40_err(base->dev,
2326 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002327 goto failure3;
2328 }
2329 return 0;
2330failure3:
2331 dma_async_device_unregister(&base->dma_memcpy);
2332failure2:
2333 dma_async_device_unregister(&base->dma_slave);
2334failure1:
2335 return err;
2336}
2337
2338/* Initialization functions. */
2339
2340static int __init d40_phy_res_init(struct d40_base *base)
2341{
2342 int i;
2343 int num_phy_chans_avail = 0;
2344 u32 val[2];
2345 int odd_even_bit = -2;
2346
2347 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2348 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2349
2350 for (i = 0; i < base->num_phy_chans; i++) {
2351 base->phy_res[i].num = i;
2352 odd_even_bit += 2 * ((i % 2) == 0);
2353 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2354 /* Mark security only channels as occupied */
2355 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2356 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2357 } else {
2358 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2359 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2360 num_phy_chans_avail++;
2361 }
2362 spin_lock_init(&base->phy_res[i].lock);
2363 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002364
2365 /* Mark disabled channels as occupied */
2366 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002367 int chan = base->plat_data->disabled_channels[i];
2368
2369 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2370 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2371 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002372 }
2373
Linus Walleij8d318a52010-03-30 15:33:42 +02002374 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2375 num_phy_chans_avail, base->num_phy_chans);
2376
2377 /* Verify settings extended vs standard */
2378 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2379
2380 for (i = 0; i < base->num_phy_chans; i++) {
2381
2382 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2383 (val[0] & 0x3) != 1)
2384 dev_info(base->dev,
2385 "[%s] INFO: channel %d is misconfigured (%d)\n",
2386 __func__, i, val[0] & 0x3);
2387
2388 val[0] = val[0] >> 2;
2389 }
2390
2391 return num_phy_chans_avail;
2392}
2393
2394static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2395{
2396 static const struct d40_reg_val dma_id_regs[] = {
2397 /* Peripheral Id */
2398 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2399 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2400 /*
2401 * D40_DREG_PERIPHID2 Depends on HW revision:
Rabin Vincent4d594902011-01-25 11:18:10 +01002402 * DB8500ed has 0x0008,
Linus Walleij8d318a52010-03-30 15:33:42 +02002403 * ? has 0x0018,
Rabin Vincent4d594902011-01-25 11:18:10 +01002404 * DB8500v1 has 0x0028
2405 * DB8500v2 has 0x0038
Linus Walleij8d318a52010-03-30 15:33:42 +02002406 */
2407 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2408
2409 /* PCell Id */
2410 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2411 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2412 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2413 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2414 };
2415 struct stedma40_platform_data *plat_data;
2416 struct clk *clk = NULL;
2417 void __iomem *virtbase = NULL;
2418 struct resource *res = NULL;
2419 struct d40_base *base = NULL;
2420 int num_log_chans = 0;
2421 int num_phy_chans;
2422 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002423 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002424 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002425
2426 clk = clk_get(&pdev->dev, NULL);
2427
2428 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002429 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002430 goto failure;
2431 }
2432
2433 clk_enable(clk);
2434
2435 /* Get IO for DMAC base address */
2436 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2437 if (!res)
2438 goto failure;
2439
2440 if (request_mem_region(res->start, resource_size(res),
2441 D40_NAME " I/O base") == NULL)
2442 goto failure;
2443
2444 virtbase = ioremap(res->start, resource_size(res));
2445 if (!virtbase)
2446 goto failure;
2447
2448 /* HW version check */
2449 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2450 if (dma_id_regs[i].val !=
2451 readl(virtbase + dma_id_regs[i].reg)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002452 d40_err(&pdev->dev,
2453 "Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002454 dma_id_regs[i].val,
2455 dma_id_regs[i].reg,
2456 readl(virtbase + dma_id_regs[i].reg));
2457 goto failure;
2458 }
2459 }
2460
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002461 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002462 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002463
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002464 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2465 D40_HW_DESIGNER) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002466 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2467 val & D40_DREG_PERIPHID2_DESIGNER_MASK,
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002468 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002469 goto failure;
2470 }
2471
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002472 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2473 D40_DREG_PERIPHID2_REV_POS;
2474
Linus Walleij8d318a52010-03-30 15:33:42 +02002475 /* The number of physical channels on this HW */
2476 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2477
2478 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002479 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002480
2481 plat_data = pdev->dev.platform_data;
2482
2483 /* Count the number of logical channels in use */
2484 for (i = 0; i < plat_data->dev_len; i++)
2485 if (plat_data->dev_rx[i] != 0)
2486 num_log_chans++;
2487
2488 for (i = 0; i < plat_data->dev_len; i++)
2489 if (plat_data->dev_tx[i] != 0)
2490 num_log_chans++;
2491
2492 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2493 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2494 sizeof(struct d40_chan), GFP_KERNEL);
2495
2496 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002497 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002498 goto failure;
2499 }
2500
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002501 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002502 base->clk = clk;
2503 base->num_phy_chans = num_phy_chans;
2504 base->num_log_chans = num_log_chans;
2505 base->phy_start = res->start;
2506 base->phy_size = resource_size(res);
2507 base->virtbase = virtbase;
2508 base->plat_data = plat_data;
2509 base->dev = &pdev->dev;
2510 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2511 base->log_chans = &base->phy_chans[num_phy_chans];
2512
2513 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2514 GFP_KERNEL);
2515 if (!base->phy_res)
2516 goto failure;
2517
2518 base->lookup_phy_chans = kzalloc(num_phy_chans *
2519 sizeof(struct d40_chan *),
2520 GFP_KERNEL);
2521 if (!base->lookup_phy_chans)
2522 goto failure;
2523
2524 if (num_log_chans + plat_data->memcpy_len) {
2525 /*
2526 * The max number of logical channels are event lines for all
2527 * src devices and dst devices
2528 */
2529 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2530 sizeof(struct d40_chan *),
2531 GFP_KERNEL);
2532 if (!base->lookup_log_chans)
2533 goto failure;
2534 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002535
2536 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2537 sizeof(struct d40_desc *) *
2538 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002539 GFP_KERNEL);
2540 if (!base->lcla_pool.alloc_map)
2541 goto failure;
2542
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002543 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2544 0, SLAB_HWCACHE_ALIGN,
2545 NULL);
2546 if (base->desc_slab == NULL)
2547 goto failure;
2548
Linus Walleij8d318a52010-03-30 15:33:42 +02002549 return base;
2550
2551failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002552 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002553 clk_disable(clk);
2554 clk_put(clk);
2555 }
2556 if (virtbase)
2557 iounmap(virtbase);
2558 if (res)
2559 release_mem_region(res->start,
2560 resource_size(res));
2561 if (virtbase)
2562 iounmap(virtbase);
2563
2564 if (base) {
2565 kfree(base->lcla_pool.alloc_map);
2566 kfree(base->lookup_log_chans);
2567 kfree(base->lookup_phy_chans);
2568 kfree(base->phy_res);
2569 kfree(base);
2570 }
2571
2572 return NULL;
2573}
2574
2575static void __init d40_hw_init(struct d40_base *base)
2576{
2577
2578 static const struct d40_reg_val dma_init_reg[] = {
2579 /* Clock every part of the DMA block from start */
2580 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2581
2582 /* Interrupts on all logical channels */
2583 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2584 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2585 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2586 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2587 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2588 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2589 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2590 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2591 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2592 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2593 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2594 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2595 };
2596 int i;
2597 u32 prmseo[2] = {0, 0};
2598 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2599 u32 pcmis = 0;
2600 u32 pcicr = 0;
2601
2602 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2603 writel(dma_init_reg[i].val,
2604 base->virtbase + dma_init_reg[i].reg);
2605
2606 /* Configure all our dma channels to default settings */
2607 for (i = 0; i < base->num_phy_chans; i++) {
2608
2609 activeo[i % 2] = activeo[i % 2] << 2;
2610
2611 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2612 == D40_ALLOC_PHY) {
2613 activeo[i % 2] |= 3;
2614 continue;
2615 }
2616
2617 /* Enable interrupt # */
2618 pcmis = (pcmis << 1) | 1;
2619
2620 /* Clear interrupt # */
2621 pcicr = (pcicr << 1) | 1;
2622
2623 /* Set channel to physical mode */
2624 prmseo[i % 2] = prmseo[i % 2] << 2;
2625 prmseo[i % 2] |= 1;
2626
2627 }
2628
2629 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2630 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2631 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2632 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2633
2634 /* Write which interrupt to enable */
2635 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2636
2637 /* Write which interrupt to clear */
2638 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2639
2640}
2641
Linus Walleij508849a2010-06-20 21:26:07 +00002642static int __init d40_lcla_allocate(struct d40_base *base)
2643{
Rabin Vincent026cbc42011-01-25 11:18:14 +01002644 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00002645 unsigned long *page_list;
2646 int i, j;
2647 int ret = 0;
2648
2649 /*
2650 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2651 * To full fill this hardware requirement without wasting 256 kb
2652 * we allocate pages until we get an aligned one.
2653 */
2654 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2655 GFP_KERNEL);
2656
2657 if (!page_list) {
2658 ret = -ENOMEM;
2659 goto failure;
2660 }
2661
2662 /* Calculating how many pages that are required */
2663 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2664
2665 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2666 page_list[i] = __get_free_pages(GFP_KERNEL,
2667 base->lcla_pool.pages);
2668 if (!page_list[i]) {
2669
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002670 d40_err(base->dev, "Failed to allocate %d pages.\n",
2671 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002672
2673 for (j = 0; j < i; j++)
2674 free_pages(page_list[j], base->lcla_pool.pages);
2675 goto failure;
2676 }
2677
2678 if ((virt_to_phys((void *)page_list[i]) &
2679 (LCLA_ALIGNMENT - 1)) == 0)
2680 break;
2681 }
2682
2683 for (j = 0; j < i; j++)
2684 free_pages(page_list[j], base->lcla_pool.pages);
2685
2686 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2687 base->lcla_pool.base = (void *)page_list[i];
2688 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002689 /*
2690 * After many attempts and no succees with finding the correct
2691 * alignment, try with allocating a big buffer.
2692 */
Linus Walleij508849a2010-06-20 21:26:07 +00002693 dev_warn(base->dev,
2694 "[%s] Failed to get %d pages @ 18 bit align.\n",
2695 __func__, base->lcla_pool.pages);
2696 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2697 base->num_phy_chans +
2698 LCLA_ALIGNMENT,
2699 GFP_KERNEL);
2700 if (!base->lcla_pool.base_unaligned) {
2701 ret = -ENOMEM;
2702 goto failure;
2703 }
2704
2705 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2706 LCLA_ALIGNMENT);
2707 }
2708
Rabin Vincent026cbc42011-01-25 11:18:14 +01002709 pool->dma_addr = dma_map_single(base->dev, pool->base,
2710 SZ_1K * base->num_phy_chans,
2711 DMA_TO_DEVICE);
2712 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2713 pool->dma_addr = 0;
2714 ret = -ENOMEM;
2715 goto failure;
2716 }
2717
Linus Walleij508849a2010-06-20 21:26:07 +00002718 writel(virt_to_phys(base->lcla_pool.base),
2719 base->virtbase + D40_DREG_LCLA);
2720failure:
2721 kfree(page_list);
2722 return ret;
2723}
2724
Linus Walleij8d318a52010-03-30 15:33:42 +02002725static int __init d40_probe(struct platform_device *pdev)
2726{
2727 int err;
2728 int ret = -ENOENT;
2729 struct d40_base *base;
2730 struct resource *res = NULL;
2731 int num_reserved_chans;
2732 u32 val;
2733
2734 base = d40_hw_detect_init(pdev);
2735
2736 if (!base)
2737 goto failure;
2738
2739 num_reserved_chans = d40_phy_res_init(base);
2740
2741 platform_set_drvdata(pdev, base);
2742
2743 spin_lock_init(&base->interrupt_lock);
2744 spin_lock_init(&base->execmd_lock);
2745
2746 /* Get IO for logical channel parameter address */
2747 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2748 if (!res) {
2749 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002750 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002751 goto failure;
2752 }
2753 base->lcpa_size = resource_size(res);
2754 base->phy_lcpa = res->start;
2755
2756 if (request_mem_region(res->start, resource_size(res),
2757 D40_NAME " I/O lcpa") == NULL) {
2758 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002759 d40_err(&pdev->dev,
2760 "Failed to request LCPA region 0x%x-0x%x\n",
2761 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002762 goto failure;
2763 }
2764
2765 /* We make use of ESRAM memory for this. */
2766 val = readl(base->virtbase + D40_DREG_LCPA);
2767 if (res->start != val && val != 0) {
2768 dev_warn(&pdev->dev,
2769 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2770 __func__, val, res->start);
2771 } else
2772 writel(res->start, base->virtbase + D40_DREG_LCPA);
2773
2774 base->lcpa_base = ioremap(res->start, resource_size(res));
2775 if (!base->lcpa_base) {
2776 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002777 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002778 goto failure;
2779 }
Linus Walleij508849a2010-06-20 21:26:07 +00002780
2781 ret = d40_lcla_allocate(base);
2782 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002783 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002784 goto failure;
2785 }
2786
Linus Walleij8d318a52010-03-30 15:33:42 +02002787 spin_lock_init(&base->lcla_pool.lock);
2788
Linus Walleij8d318a52010-03-30 15:33:42 +02002789 base->irq = platform_get_irq(pdev, 0);
2790
2791 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002792 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002793 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002794 goto failure;
2795 }
2796
2797 err = d40_dmaengine_init(base, num_reserved_chans);
2798 if (err)
2799 goto failure;
2800
2801 d40_hw_init(base);
2802
2803 dev_info(base->dev, "initialized\n");
2804 return 0;
2805
2806failure:
2807 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002808 if (base->desc_slab)
2809 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002810 if (base->virtbase)
2811 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01002812
2813 if (base->lcla_pool.dma_addr)
2814 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2815 SZ_1K * base->num_phy_chans,
2816 DMA_TO_DEVICE);
2817
Linus Walleij508849a2010-06-20 21:26:07 +00002818 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2819 free_pages((unsigned long)base->lcla_pool.base,
2820 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002821
2822 kfree(base->lcla_pool.base_unaligned);
2823
Linus Walleij8d318a52010-03-30 15:33:42 +02002824 if (base->phy_lcpa)
2825 release_mem_region(base->phy_lcpa,
2826 base->lcpa_size);
2827 if (base->phy_start)
2828 release_mem_region(base->phy_start,
2829 base->phy_size);
2830 if (base->clk) {
2831 clk_disable(base->clk);
2832 clk_put(base->clk);
2833 }
2834
2835 kfree(base->lcla_pool.alloc_map);
2836 kfree(base->lookup_log_chans);
2837 kfree(base->lookup_phy_chans);
2838 kfree(base->phy_res);
2839 kfree(base);
2840 }
2841
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002842 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002843 return ret;
2844}
2845
2846static struct platform_driver d40_driver = {
2847 .driver = {
2848 .owner = THIS_MODULE,
2849 .name = D40_NAME,
2850 },
2851};
2852
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002853static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002854{
2855 return platform_driver_probe(&d40_driver, d40_probe);
2856}
2857arch_initcall(stedma40_init);