blob: 8c6abc23db80115f1fb2efc502db58e12d3a3cc4 [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
Jonas Aaberg698e4732010-08-09 12:08:56 +0000507static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
508{
509 int curr_lcla = -EINVAL, next_lcla;
510
Rabin Vincent724a8572011-01-25 11:18:08 +0100511 if (chan_is_physical(d40c)) {
Jonas Aaberg698e4732010-08-09 12:08:56 +0000512 d40_phy_lli_write(d40c->base->virtbase,
513 d40c->phy_chan->num,
514 d40d->lli_phy.dst,
515 d40d->lli_phy.src);
516 d40d->lli_current = d40d->lli_len;
517 } else {
518
519 if ((d40d->lli_len - d40d->lli_current) > 1)
520 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
521
522 d40_log_lli_lcpa_write(d40c->lcpa,
523 &d40d->lli_log.dst[d40d->lli_current],
524 &d40d->lli_log.src[d40d->lli_current],
525 curr_lcla);
526
527 d40d->lli_current++;
528 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
Rabin Vincent026cbc42011-01-25 11:18:14 +0100529 unsigned int lcla_offset = d40c->phy_chan->num * 1024 +
530 8 * curr_lcla * 2;
531 struct d40_lcla_pool *pool = &d40c->base->lcla_pool;
532 struct d40_log_lli *lcla = pool->base + lcla_offset;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000533
534 if (d40d->lli_current + 1 < d40d->lli_len)
535 next_lcla = d40_lcla_alloc_one(d40c, d40d);
536 else
537 next_lcla = -EINVAL;
538
Jonas Aaberg698e4732010-08-09 12:08:56 +0000539 d40_log_lli_lcla_write(lcla,
540 &d40d->lli_log.dst[d40d->lli_current],
541 &d40d->lli_log.src[d40d->lli_current],
542 next_lcla);
543
Rabin Vincent026cbc42011-01-25 11:18:14 +0100544 dma_sync_single_range_for_device(d40c->base->dev,
545 pool->dma_addr, lcla_offset,
546 2 * sizeof(struct d40_log_lli),
547 DMA_TO_DEVICE);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000548
549 curr_lcla = next_lcla;
550
551 if (curr_lcla == -EINVAL) {
552 d40d->lli_current++;
553 break;
554 }
555
556 }
557 }
558}
559
Linus Walleij8d318a52010-03-30 15:33:42 +0200560static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
561{
562 struct d40_desc *d;
563
564 if (list_empty(&d40c->active))
565 return NULL;
566
567 d = list_first_entry(&d40c->active,
568 struct d40_desc,
569 node);
570 return d;
571}
572
573static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
574{
575 list_add_tail(&desc->node, &d40c->queue);
576}
577
578static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
579{
580 struct d40_desc *d;
581
582 if (list_empty(&d40c->queue))
583 return NULL;
584
585 d = list_first_entry(&d40c->queue,
586 struct d40_desc,
587 node);
588 return d;
589}
590
Per Forlind49278e2010-12-20 18:31:38 +0100591static int d40_psize_2_burst_size(bool is_log, int psize)
592{
593 if (is_log) {
594 if (psize == STEDMA40_PSIZE_LOG_1)
595 return 1;
596 } else {
597 if (psize == STEDMA40_PSIZE_PHY_1)
598 return 1;
599 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200600
Per Forlind49278e2010-12-20 18:31:38 +0100601 return 2 << psize;
602}
603
604/*
605 * The dma only supports transmitting packages up to
606 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
607 * dma elements required to send the entire sg list
608 */
609static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
610{
611 int dmalen;
612 u32 max_w = max(data_width1, data_width2);
613 u32 min_w = min(data_width1, data_width2);
614 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
615
616 if (seg_max > STEDMA40_MAX_SEG_SIZE)
617 seg_max -= (1 << max_w);
618
619 if (!IS_ALIGNED(size, 1 << max_w))
620 return -EINVAL;
621
622 if (size <= seg_max)
623 dmalen = 1;
624 else {
625 dmalen = size / seg_max;
626 if (dmalen * seg_max < size)
627 dmalen++;
628 }
629 return dmalen;
630}
631
632static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
633 u32 data_width1, u32 data_width2)
634{
635 struct scatterlist *sg;
636 int i;
637 int len = 0;
638 int ret;
639
640 for_each_sg(sgl, sg, sg_len, i) {
641 ret = d40_size_2_dmalen(sg_dma_len(sg),
642 data_width1, data_width2);
643 if (ret < 0)
644 return ret;
645 len += ret;
646 }
647 return len;
648}
649
650/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200651
652static int d40_channel_execute_command(struct d40_chan *d40c,
653 enum d40_command command)
654{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000655 u32 status;
656 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200657 void __iomem *active_reg;
658 int ret = 0;
659 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000660 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200661
662 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
663
664 if (d40c->phy_chan->num % 2 == 0)
665 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
666 else
667 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
668
669 if (command == D40_DMA_SUSPEND_REQ) {
670 status = (readl(active_reg) &
671 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
672 D40_CHAN_POS(d40c->phy_chan->num);
673
674 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
675 goto done;
676 }
677
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000678 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
679 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
680 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200681
682 if (command == D40_DMA_SUSPEND_REQ) {
683
684 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
685 status = (readl(active_reg) &
686 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
687 D40_CHAN_POS(d40c->phy_chan->num);
688
689 cpu_relax();
690 /*
691 * Reduce the number of bus accesses while
692 * waiting for the DMA to suspend.
693 */
694 udelay(3);
695
696 if (status == D40_DMA_STOP ||
697 status == D40_DMA_SUSPENDED)
698 break;
699 }
700
701 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100702 chan_err(d40c,
703 "unable to suspend the chl %d (log: %d) status %x\n",
704 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200705 status);
706 dump_stack();
707 ret = -EBUSY;
708 }
709
710 }
711done:
712 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
713 return ret;
714}
715
716static void d40_term_all(struct d40_chan *d40c)
717{
718 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200719
720 /* Release active descriptors */
721 while ((d40d = d40_first_active_get(d40c))) {
722 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200723 d40_desc_free(d40c, d40d);
724 }
725
726 /* Release queued descriptors waiting for transfer */
727 while ((d40d = d40_first_queued(d40c))) {
728 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200729 d40_desc_free(d40c, d40d);
730 }
731
Linus Walleij8d318a52010-03-30 15:33:42 +0200732
733 d40c->pending_tx = 0;
734 d40c->busy = false;
735}
736
Rabin Vincent262d2912011-01-25 11:18:05 +0100737static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
738 u32 event, int reg)
739{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100740 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100741 int tries;
742
743 if (!enable) {
744 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
745 | ~D40_EVENTLINE_MASK(event), addr);
746 return;
747 }
748
749 /*
750 * The hardware sometimes doesn't register the enable when src and dst
751 * event lines are active on the same logical channel. Retry to ensure
752 * it does. Usually only one retry is sufficient.
753 */
754 tries = 100;
755 while (--tries) {
756 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
757 | ~D40_EVENTLINE_MASK(event), addr);
758
759 if (readl(addr) & D40_EVENTLINE_MASK(event))
760 break;
761 }
762
763 if (tries != 99)
764 dev_dbg(chan2dev(d40c),
765 "[%s] workaround enable S%cLNK (%d tries)\n",
766 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
767 100 - tries);
768
769 WARN_ON(!tries);
770}
771
Linus Walleij8d318a52010-03-30 15:33:42 +0200772static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
773{
Linus Walleij8d318a52010-03-30 15:33:42 +0200774 unsigned long flags;
775
Linus Walleij8d318a52010-03-30 15:33:42 +0200776 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
777
778 /* Enable event line connected to device (or memcpy) */
779 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
780 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
781 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
782
Rabin Vincent262d2912011-01-25 11:18:05 +0100783 __d40_config_set_event(d40c, do_enable, event,
784 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200785 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100786
Linus Walleij8d318a52010-03-30 15:33:42 +0200787 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
788 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
789
Rabin Vincent262d2912011-01-25 11:18:05 +0100790 __d40_config_set_event(d40c, do_enable, event,
791 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200792 }
793
794 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
795}
796
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200797static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200798{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100799 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000800 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200801
Rabin Vincent8ca84682011-01-25 11:18:07 +0100802 val = readl(chanbase + D40_CHAN_REG_SSLNK);
803 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200804
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200805 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200806}
807
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000808static u32 d40_get_prmo(struct d40_chan *d40c)
809{
810 static const unsigned int phy_map[] = {
811 [STEDMA40_PCHAN_BASIC_MODE]
812 = D40_DREG_PRMO_PCHAN_BASIC,
813 [STEDMA40_PCHAN_MODULO_MODE]
814 = D40_DREG_PRMO_PCHAN_MODULO,
815 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
816 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
817 };
818 static const unsigned int log_map[] = {
819 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
820 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
821 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
822 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
823 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
824 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
825 };
826
Rabin Vincent724a8572011-01-25 11:18:08 +0100827 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000828 return phy_map[d40c->dma_cfg.mode_opt];
829 else
830 return log_map[d40c->dma_cfg.mode_opt];
831}
832
Jonas Aabergb55912c2010-08-09 12:08:02 +0000833static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200834{
835 u32 addr_base;
836 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200837
838 /* Odd addresses are even addresses + 4 */
839 addr_base = (d40c->phy_chan->num % 2) * 4;
840 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100841 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200842 D40_CHAN_POS(d40c->phy_chan->num);
843 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
844
845 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000846 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200847
848 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
849
Rabin Vincent724a8572011-01-25 11:18:08 +0100850 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100851 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
852 & D40_SREG_ELEM_LOG_LIDX_MASK;
853 void __iomem *chanbase = chan_base(d40c);
854
Linus Walleij8d318a52010-03-30 15:33:42 +0200855 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100856 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
857 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200858
Jonas Aabergb55912c2010-08-09 12:08:02 +0000859 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100860 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
861 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200862 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200863}
864
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000865static u32 d40_residue(struct d40_chan *d40c)
866{
867 u32 num_elt;
868
Rabin Vincent724a8572011-01-25 11:18:08 +0100869 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000870 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
871 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100872 else {
873 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
874 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
875 >> D40_SREG_ELEM_PHY_ECNT_POS;
876 }
877
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000878 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
879}
880
881static bool d40_tx_is_linked(struct d40_chan *d40c)
882{
883 bool is_link;
884
Rabin Vincent724a8572011-01-25 11:18:08 +0100885 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000886 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
887 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100888 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
889 & D40_SREG_LNK_PHYS_LNK_MASK;
890
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000891 return is_link;
892}
893
894static int d40_pause(struct dma_chan *chan)
895{
896 struct d40_chan *d40c =
897 container_of(chan, struct d40_chan, chan);
898 int res = 0;
899 unsigned long flags;
900
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000901 if (!d40c->busy)
902 return 0;
903
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000904 spin_lock_irqsave(&d40c->lock, flags);
905
906 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
907 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +0100908 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000909 d40_config_set_event(d40c, false);
910 /* Resume the other logical channels if any */
911 if (d40_chan_has_events(d40c))
912 res = d40_channel_execute_command(d40c,
913 D40_DMA_RUN);
914 }
915 }
916
917 spin_unlock_irqrestore(&d40c->lock, flags);
918 return res;
919}
920
921static int d40_resume(struct dma_chan *chan)
922{
923 struct d40_chan *d40c =
924 container_of(chan, struct d40_chan, chan);
925 int res = 0;
926 unsigned long flags;
927
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000928 if (!d40c->busy)
929 return 0;
930
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000931 spin_lock_irqsave(&d40c->lock, flags);
932
933 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +0100934 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000935 res = d40_channel_execute_command(d40c,
936 D40_DMA_SUSPEND_REQ);
937 goto no_suspend;
938 }
939
940 /* If bytes left to transfer or linked tx resume job */
941 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
942
Rabin Vincent724a8572011-01-25 11:18:08 +0100943 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000944 d40_config_set_event(d40c, true);
945
946 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
947 }
948
949no_suspend:
950 spin_unlock_irqrestore(&d40c->lock, flags);
951 return res;
952}
953
Linus Walleij8d318a52010-03-30 15:33:42 +0200954static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
955{
956 struct d40_chan *d40c = container_of(tx->chan,
957 struct d40_chan,
958 chan);
959 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
960 unsigned long flags;
961
962 spin_lock_irqsave(&d40c->lock, flags);
963
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000964 d40c->chan.cookie++;
965
966 if (d40c->chan.cookie < 0)
967 d40c->chan.cookie = 1;
968
969 d40d->txd.cookie = d40c->chan.cookie;
970
Linus Walleij8d318a52010-03-30 15:33:42 +0200971 d40_desc_queue(d40c, d40d);
972
973 spin_unlock_irqrestore(&d40c->lock, flags);
974
975 return tx->cookie;
976}
977
978static int d40_start(struct d40_chan *d40c)
979{
Linus Walleijf4185592010-06-22 18:06:42 -0700980 if (d40c->base->rev == 0) {
981 int err;
982
Rabin Vincent724a8572011-01-25 11:18:08 +0100983 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -0700984 err = d40_channel_execute_command(d40c,
985 D40_DMA_SUSPEND_REQ);
986 if (err)
987 return err;
988 }
989 }
990
Rabin Vincent724a8572011-01-25 11:18:08 +0100991 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +0200992 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +0200993
Jonas Aaberg0c322692010-06-20 21:25:46 +0000994 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +0200995}
996
997static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
998{
999 struct d40_desc *d40d;
1000 int err;
1001
1002 /* Start queued jobs, if any */
1003 d40d = d40_first_queued(d40c);
1004
1005 if (d40d != NULL) {
1006 d40c->busy = true;
1007
1008 /* Remove from queue */
1009 d40_desc_remove(d40d);
1010
1011 /* Add to active queue */
1012 d40_desc_submit(d40c, d40d);
1013
Rabin Vincent7d83a852011-01-25 11:18:06 +01001014 /* Initiate DMA job */
1015 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001016
Rabin Vincent7d83a852011-01-25 11:18:06 +01001017 /* Start dma job */
1018 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001019
Rabin Vincent7d83a852011-01-25 11:18:06 +01001020 if (err)
1021 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001022 }
1023
1024 return d40d;
1025}
1026
1027/* called from interrupt context */
1028static void dma_tc_handle(struct d40_chan *d40c)
1029{
1030 struct d40_desc *d40d;
1031
Linus Walleij8d318a52010-03-30 15:33:42 +02001032 /* Get first active entry from list */
1033 d40d = d40_first_active_get(d40c);
1034
1035 if (d40d == NULL)
1036 return;
1037
Jonas Aaberg698e4732010-08-09 12:08:56 +00001038 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001039
Jonas Aaberg698e4732010-08-09 12:08:56 +00001040 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001041 d40_desc_load(d40c, d40d);
1042 /* Start dma job */
1043 (void) d40_start(d40c);
1044 return;
1045 }
1046
1047 if (d40_queue_start(d40c) == NULL)
1048 d40c->busy = false;
1049
1050 d40c->pending_tx++;
1051 tasklet_schedule(&d40c->tasklet);
1052
1053}
1054
1055static void dma_tasklet(unsigned long data)
1056{
1057 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001058 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001059 unsigned long flags;
1060 dma_async_tx_callback callback;
1061 void *callback_param;
1062
1063 spin_lock_irqsave(&d40c->lock, flags);
1064
1065 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001066 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001067
Jonas Aaberg767a9672010-08-09 12:08:34 +00001068 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001069 goto err;
1070
Jonas Aaberg767a9672010-08-09 12:08:34 +00001071 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001072
1073 /*
1074 * If terminating a channel pending_tx is set to zero.
1075 * This prevents any finished active jobs to return to the client.
1076 */
1077 if (d40c->pending_tx == 0) {
1078 spin_unlock_irqrestore(&d40c->lock, flags);
1079 return;
1080 }
1081
1082 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001083 callback = d40d->txd.callback;
1084 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001085
Jonas Aaberg767a9672010-08-09 12:08:34 +00001086 if (async_tx_test_ack(&d40d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001087 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001088 d40_desc_remove(d40d);
1089 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001090 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001091 if (!d40d->is_in_client_list) {
1092 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001093 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001094 list_add_tail(&d40d->node, &d40c->client);
1095 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001096 }
1097 }
1098
1099 d40c->pending_tx--;
1100
1101 if (d40c->pending_tx)
1102 tasklet_schedule(&d40c->tasklet);
1103
1104 spin_unlock_irqrestore(&d40c->lock, flags);
1105
Jonas Aaberg767a9672010-08-09 12:08:34 +00001106 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001107 callback(callback_param);
1108
1109 return;
1110
1111 err:
1112 /* Rescue manouver if receiving double interrupts */
1113 if (d40c->pending_tx > 0)
1114 d40c->pending_tx--;
1115 spin_unlock_irqrestore(&d40c->lock, flags);
1116}
1117
1118static irqreturn_t d40_handle_interrupt(int irq, void *data)
1119{
1120 static const struct d40_interrupt_lookup il[] = {
1121 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1122 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1123 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1124 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1125 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1126 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1127 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1128 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1129 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1130 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1131 };
1132
1133 int i;
1134 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001135 u32 idx;
1136 u32 row;
1137 long chan = -1;
1138 struct d40_chan *d40c;
1139 unsigned long flags;
1140 struct d40_base *base = data;
1141
1142 spin_lock_irqsave(&base->interrupt_lock, flags);
1143
1144 /* Read interrupt status of both logical and physical channels */
1145 for (i = 0; i < ARRAY_SIZE(il); i++)
1146 regs[i] = readl(base->virtbase + il[i].src);
1147
1148 for (;;) {
1149
1150 chan = find_next_bit((unsigned long *)regs,
1151 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1152
1153 /* No more set bits found? */
1154 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1155 break;
1156
1157 row = chan / BITS_PER_LONG;
1158 idx = chan & (BITS_PER_LONG - 1);
1159
1160 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001161 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001162
1163 if (il[row].offset == D40_PHY_CHAN)
1164 d40c = base->lookup_phy_chans[idx];
1165 else
1166 d40c = base->lookup_log_chans[il[row].offset + idx];
1167 spin_lock(&d40c->lock);
1168
1169 if (!il[row].is_error)
1170 dma_tc_handle(d40c);
1171 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001172 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1173 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001174
1175 spin_unlock(&d40c->lock);
1176 }
1177
1178 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1179
1180 return IRQ_HANDLED;
1181}
1182
Linus Walleij8d318a52010-03-30 15:33:42 +02001183static int d40_validate_conf(struct d40_chan *d40c,
1184 struct stedma40_chan_cfg *conf)
1185{
1186 int res = 0;
1187 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1188 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001189 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001190
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001191 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001192 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001193 res = -EINVAL;
1194 }
1195
1196 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1197 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1198 d40c->runtime_addr == 0) {
1199
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001200 chan_err(d40c, "Invalid TX channel address (%d)\n",
1201 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001202 res = -EINVAL;
1203 }
1204
1205 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1206 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1207 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001208 chan_err(d40c, "Invalid RX channel address (%d)\n",
1209 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001210 res = -EINVAL;
1211 }
1212
1213 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001214 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001215 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001216 res = -EINVAL;
1217 }
1218
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001219 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001220 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001221 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001222 res = -EINVAL;
1223 }
1224
1225 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1226 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001227 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001228 res = -EINVAL;
1229 }
1230
1231 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1232 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001233 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001234 res = -EINVAL;
1235 }
1236
1237 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1238 /*
1239 * DMAC HW supports it. Will be added to this driver,
1240 * in case any dma client requires it.
1241 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001242 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001243 res = -EINVAL;
1244 }
1245
Per Forlind49278e2010-12-20 18:31:38 +01001246 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1247 (1 << conf->src_info.data_width) !=
1248 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1249 (1 << conf->dst_info.data_width)) {
1250 /*
1251 * The DMAC hardware only supports
1252 * src (burst x width) == dst (burst x width)
1253 */
1254
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001255 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001256 res = -EINVAL;
1257 }
1258
Linus Walleij8d318a52010-03-30 15:33:42 +02001259 return res;
1260}
1261
1262static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001263 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001264{
1265 unsigned long flags;
1266 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001267 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001268 /* Physical interrupts are masked per physical full channel */
1269 if (phy->allocated_src == D40_ALLOC_FREE &&
1270 phy->allocated_dst == D40_ALLOC_FREE) {
1271 phy->allocated_dst = D40_ALLOC_PHY;
1272 phy->allocated_src = D40_ALLOC_PHY;
1273 goto found;
1274 } else
1275 goto not_found;
1276 }
1277
1278 /* Logical channel */
1279 if (is_src) {
1280 if (phy->allocated_src == D40_ALLOC_PHY)
1281 goto not_found;
1282
1283 if (phy->allocated_src == D40_ALLOC_FREE)
1284 phy->allocated_src = D40_ALLOC_LOG_FREE;
1285
1286 if (!(phy->allocated_src & (1 << log_event_line))) {
1287 phy->allocated_src |= 1 << log_event_line;
1288 goto found;
1289 } else
1290 goto not_found;
1291 } else {
1292 if (phy->allocated_dst == D40_ALLOC_PHY)
1293 goto not_found;
1294
1295 if (phy->allocated_dst == D40_ALLOC_FREE)
1296 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1297
1298 if (!(phy->allocated_dst & (1 << log_event_line))) {
1299 phy->allocated_dst |= 1 << log_event_line;
1300 goto found;
1301 } else
1302 goto not_found;
1303 }
1304
1305not_found:
1306 spin_unlock_irqrestore(&phy->lock, flags);
1307 return false;
1308found:
1309 spin_unlock_irqrestore(&phy->lock, flags);
1310 return true;
1311}
1312
1313static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1314 int log_event_line)
1315{
1316 unsigned long flags;
1317 bool is_free = false;
1318
1319 spin_lock_irqsave(&phy->lock, flags);
1320 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001321 phy->allocated_dst = D40_ALLOC_FREE;
1322 phy->allocated_src = D40_ALLOC_FREE;
1323 is_free = true;
1324 goto out;
1325 }
1326
1327 /* Logical channel */
1328 if (is_src) {
1329 phy->allocated_src &= ~(1 << log_event_line);
1330 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1331 phy->allocated_src = D40_ALLOC_FREE;
1332 } else {
1333 phy->allocated_dst &= ~(1 << log_event_line);
1334 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1335 phy->allocated_dst = D40_ALLOC_FREE;
1336 }
1337
1338 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1339 D40_ALLOC_FREE);
1340
1341out:
1342 spin_unlock_irqrestore(&phy->lock, flags);
1343
1344 return is_free;
1345}
1346
1347static int d40_allocate_channel(struct d40_chan *d40c)
1348{
1349 int dev_type;
1350 int event_group;
1351 int event_line;
1352 struct d40_phy_res *phys;
1353 int i;
1354 int j;
1355 int log_num;
1356 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001357 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001358
1359 phys = d40c->base->phy_res;
1360
1361 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1362 dev_type = d40c->dma_cfg.src_dev_type;
1363 log_num = 2 * dev_type;
1364 is_src = true;
1365 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1366 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1367 /* dst event lines are used for logical memcpy */
1368 dev_type = d40c->dma_cfg.dst_dev_type;
1369 log_num = 2 * dev_type + 1;
1370 is_src = false;
1371 } else
1372 return -EINVAL;
1373
1374 event_group = D40_TYPE_TO_GROUP(dev_type);
1375 event_line = D40_TYPE_TO_EVENT(dev_type);
1376
1377 if (!is_log) {
1378 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1379 /* Find physical half channel */
1380 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1381
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001382 if (d40_alloc_mask_set(&phys[i], is_src,
1383 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001384 goto found_phy;
1385 }
1386 } else
1387 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1388 int phy_num = j + event_group * 2;
1389 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001390 if (d40_alloc_mask_set(&phys[i],
1391 is_src,
1392 0,
1393 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001394 goto found_phy;
1395 }
1396 }
1397 return -EINVAL;
1398found_phy:
1399 d40c->phy_chan = &phys[i];
1400 d40c->log_num = D40_PHY_CHAN;
1401 goto out;
1402 }
1403 if (dev_type == -1)
1404 return -EINVAL;
1405
1406 /* Find logical channel */
1407 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1408 int phy_num = j + event_group * 2;
1409 /*
1410 * Spread logical channels across all available physical rather
1411 * than pack every logical channel at the first available phy
1412 * channels.
1413 */
1414 if (is_src) {
1415 for (i = phy_num; i < phy_num + 2; i++) {
1416 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001417 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001418 goto found_log;
1419 }
1420 } else {
1421 for (i = phy_num + 1; i >= phy_num; i--) {
1422 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001423 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001424 goto found_log;
1425 }
1426 }
1427 }
1428 return -EINVAL;
1429
1430found_log:
1431 d40c->phy_chan = &phys[i];
1432 d40c->log_num = log_num;
1433out:
1434
1435 if (is_log)
1436 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1437 else
1438 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1439
1440 return 0;
1441
1442}
1443
Linus Walleij8d318a52010-03-30 15:33:42 +02001444static int d40_config_memcpy(struct d40_chan *d40c)
1445{
1446 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1447
1448 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1449 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1450 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1451 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1452 memcpy[d40c->chan.chan_id];
1453
1454 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1455 dma_has_cap(DMA_SLAVE, cap)) {
1456 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1457 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001458 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001459 return -EINVAL;
1460 }
1461
1462 return 0;
1463}
1464
1465
1466static int d40_free_dma(struct d40_chan *d40c)
1467{
1468
1469 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001470 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001471 struct d40_phy_res *phy = d40c->phy_chan;
1472 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001473 struct d40_desc *d;
1474 struct d40_desc *_d;
1475
Linus Walleij8d318a52010-03-30 15:33:42 +02001476
1477 /* Terminate all queued and active transfers */
1478 d40_term_all(d40c);
1479
Per Fridena8be8622010-06-20 21:24:59 +00001480 /* Release client owned descriptors */
1481 if (!list_empty(&d40c->client))
1482 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001483 d40_pool_lli_free(d40c, d);
Per Fridena8be8622010-06-20 21:24:59 +00001484 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001485 d40_desc_free(d40c, d);
1486 }
1487
Linus Walleij8d318a52010-03-30 15:33:42 +02001488 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001489 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001490 return -EINVAL;
1491 }
1492
1493 if (phy->allocated_src == D40_ALLOC_FREE &&
1494 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001495 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001496 return -EINVAL;
1497 }
1498
Linus Walleij8d318a52010-03-30 15:33:42 +02001499 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1500 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1501 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001502 is_src = false;
1503 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1504 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001505 is_src = true;
1506 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001507 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001508 return -EINVAL;
1509 }
1510
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001511 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1512 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001513 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001514 return res;
1515 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001516
Rabin Vincent724a8572011-01-25 11:18:08 +01001517 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001518 /* Release logical channel, deactivate the event line */
1519
1520 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001521 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1522
1523 /*
1524 * Check if there are more logical allocation
1525 * on this phy channel.
1526 */
1527 if (!d40_alloc_mask_free(phy, is_src, event)) {
1528 /* Resume the other logical channels if any */
1529 if (d40_chan_has_events(d40c)) {
1530 res = d40_channel_execute_command(d40c,
1531 D40_DMA_RUN);
1532 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001533 chan_err(d40c,
1534 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 return res;
1536 }
1537 }
1538 return 0;
1539 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001540 } else {
1541 (void) d40_alloc_mask_free(phy, is_src, 0);
1542 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001543
1544 /* Release physical channel */
1545 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1546 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001547 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001548 return res;
1549 }
1550 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001551 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001552 d40c->base->lookup_phy_chans[phy->num] = NULL;
1553
1554 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001555}
1556
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001557static bool d40_is_paused(struct d40_chan *d40c)
1558{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001559 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001560 bool is_paused = false;
1561 unsigned long flags;
1562 void __iomem *active_reg;
1563 u32 status;
1564 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001565
1566 spin_lock_irqsave(&d40c->lock, flags);
1567
Rabin Vincent724a8572011-01-25 11:18:08 +01001568 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001569 if (d40c->phy_chan->num % 2 == 0)
1570 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1571 else
1572 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1573
1574 status = (readl(active_reg) &
1575 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1576 D40_CHAN_POS(d40c->phy_chan->num);
1577 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1578 is_paused = true;
1579
1580 goto _exit;
1581 }
1582
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001583 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001584 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001585 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001586 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001587 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001588 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001589 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001590 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001591 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001592 goto _exit;
1593 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001594
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001595 status = (status & D40_EVENTLINE_MASK(event)) >>
1596 D40_EVENTLINE_POS(event);
1597
1598 if (status != D40_DMA_RUN)
1599 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001600_exit:
1601 spin_unlock_irqrestore(&d40c->lock, flags);
1602 return is_paused;
1603
1604}
1605
1606
Linus Walleij8d318a52010-03-30 15:33:42 +02001607static u32 stedma40_residue(struct dma_chan *chan)
1608{
1609 struct d40_chan *d40c =
1610 container_of(chan, struct d40_chan, chan);
1611 u32 bytes_left;
1612 unsigned long flags;
1613
1614 spin_lock_irqsave(&d40c->lock, flags);
1615 bytes_left = d40_residue(d40c);
1616 spin_unlock_irqrestore(&d40c->lock, flags);
1617
1618 return bytes_left;
1619}
1620
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001621static int
1622d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1623 struct scatterlist *sg_src, struct scatterlist *sg_dst,
1624 unsigned int sg_len, enum dma_data_direction direction,
1625 dma_addr_t dev_addr)
1626{
1627 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1628 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1629 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
1630
1631 if (direction == DMA_NONE) {
1632 /* memcpy */
1633 (void) d40_log_sg_to_lli(sg_src, sg_len,
1634 desc->lli_log.src,
1635 chan->log_def.lcsp1,
1636 src_info->data_width,
1637 dst_info->data_width);
1638
1639 (void) d40_log_sg_to_lli(sg_dst, sg_len,
1640 desc->lli_log.dst,
1641 chan->log_def.lcsp3,
1642 dst_info->data_width,
1643 src_info->data_width);
1644 } else {
1645 unsigned int total_size;
1646
1647 total_size = d40_log_sg_to_dev(sg_src, sg_len,
1648 &desc->lli_log,
1649 &chan->log_def,
1650 src_info->data_width,
1651 dst_info->data_width,
1652 direction, dev_addr);
1653 if (total_size < 0)
1654 return -EINVAL;
1655 }
1656
1657 return 0;
1658}
1659
1660static int
1661d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1662 struct scatterlist *sg_src, struct scatterlist *sg_dst,
1663 unsigned int sg_len, enum dma_data_direction direction,
1664 dma_addr_t dev_addr)
1665{
1666 dma_addr_t src_dev_addr = direction == DMA_FROM_DEVICE ? dev_addr : 0;
1667 dma_addr_t dst_dev_addr = direction == DMA_TO_DEVICE ? dev_addr : 0;
1668 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1669 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1670 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
1671 int ret;
1672
1673 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1674 desc->lli_phy.src,
1675 virt_to_phys(desc->lli_phy.src),
1676 chan->src_def_cfg,
1677 src_info->data_width,
1678 dst_info->data_width,
1679 src_info->psize);
1680
1681 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1682 desc->lli_phy.dst,
1683 virt_to_phys(desc->lli_phy.dst),
1684 chan->dst_def_cfg,
1685 dst_info->data_width,
1686 src_info->data_width,
1687 dst_info->psize);
1688
1689 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1690 desc->lli_pool.size, DMA_TO_DEVICE);
1691
1692 return ret < 0 ? ret : 0;
1693}
1694
1695
Rabin Vincent5f811582011-01-25 11:18:18 +01001696static struct d40_desc *
1697d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1698 unsigned int sg_len, unsigned long dma_flags)
1699{
1700 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1701 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001702 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01001703
1704 desc = d40_desc_get(chan);
1705 if (!desc)
1706 return NULL;
1707
1708 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1709 cfg->dst_info.data_width);
1710 if (desc->lli_len < 0) {
1711 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01001712 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01001713 }
1714
Rabin Vincentdbd88782011-01-25 11:18:19 +01001715 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1716 if (ret < 0) {
1717 chan_err(chan, "Could not allocate lli\n");
1718 goto err;
1719 }
1720
1721
Rabin Vincent5f811582011-01-25 11:18:18 +01001722 desc->lli_current = 0;
1723 desc->txd.flags = dma_flags;
1724 desc->txd.tx_submit = d40_tx_submit;
1725
1726 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1727
1728 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001729
1730err:
1731 d40_desc_free(chan, desc);
1732 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01001733}
1734
Linus Walleij8d318a52010-03-30 15:33:42 +02001735struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
1736 struct scatterlist *sgl_dst,
1737 struct scatterlist *sgl_src,
1738 unsigned int sgl_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001739 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001740{
Linus Walleij8d318a52010-03-30 15:33:42 +02001741 struct d40_desc *d40d;
1742 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1743 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001744 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001745
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001746 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001747 chan_err(d40c, "Unallocated channel.\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001748 return ERR_PTR(-EINVAL);
1749 }
1750
Jonas Aaberg2a614342010-06-20 21:25:24 +00001751 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001752
Rabin Vincent5f811582011-01-25 11:18:18 +01001753 d40d = d40_prep_desc(d40c, sgl_dst, sgl_len, dma_flags);
1754 if (!d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +02001755 goto err;
1756
Rabin Vincent724a8572011-01-25 11:18:08 +01001757 if (chan_is_logical(d40c)) {
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001758 d40_prep_sg_log(d40c, d40d, sgl_src, sgl_dst,
1759 sgl_len, DMA_NONE, 0);
Linus Walleij8d318a52010-03-30 15:33:42 +02001760 } else {
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001761 d40_prep_sg_phy(d40c, d40d, sgl_src, sgl_dst,
1762 sgl_len, DMA_NONE, 0);
Linus Walleij8d318a52010-03-30 15:33:42 +02001763 }
1764
Jonas Aaberg2a614342010-06-20 21:25:24 +00001765 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001766
1767 return &d40d->txd;
1768err:
Rabin Vincent819504f2010-10-06 08:20:38 +00001769 if (d40d)
1770 d40_desc_free(d40c, d40d);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001771 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001772 return NULL;
1773}
1774EXPORT_SYMBOL(stedma40_memcpy_sg);
1775
1776bool stedma40_filter(struct dma_chan *chan, void *data)
1777{
1778 struct stedma40_chan_cfg *info = data;
1779 struct d40_chan *d40c =
1780 container_of(chan, struct d40_chan, chan);
1781 int err;
1782
1783 if (data) {
1784 err = d40_validate_conf(d40c, info);
1785 if (!err)
1786 d40c->dma_cfg = *info;
1787 } else
1788 err = d40_config_memcpy(d40c);
1789
Rabin Vincentce2ca122010-10-12 13:00:49 +00001790 if (!err)
1791 d40c->configured = true;
1792
Linus Walleij8d318a52010-03-30 15:33:42 +02001793 return err == 0;
1794}
1795EXPORT_SYMBOL(stedma40_filter);
1796
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001797static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1798{
1799 bool realtime = d40c->dma_cfg.realtime;
1800 bool highprio = d40c->dma_cfg.high_priority;
1801 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1802 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1803 u32 event = D40_TYPE_TO_EVENT(dev_type);
1804 u32 group = D40_TYPE_TO_GROUP(dev_type);
1805 u32 bit = 1 << event;
1806
1807 /* Destination event lines are stored in the upper halfword */
1808 if (!src)
1809 bit <<= 16;
1810
1811 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1812 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1813}
1814
1815static void d40_set_prio_realtime(struct d40_chan *d40c)
1816{
1817 if (d40c->base->rev < 3)
1818 return;
1819
1820 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1821 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1822 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1823
1824 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1825 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1826 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1827}
1828
Linus Walleij8d318a52010-03-30 15:33:42 +02001829/* DMA ENGINE functions */
1830static int d40_alloc_chan_resources(struct dma_chan *chan)
1831{
1832 int err;
1833 unsigned long flags;
1834 struct d40_chan *d40c =
1835 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001836 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001837 spin_lock_irqsave(&d40c->lock, flags);
1838
1839 d40c->completed = chan->cookie = 1;
1840
Rabin Vincentce2ca122010-10-12 13:00:49 +00001841 /* If no dma configuration is set use default configuration (memcpy) */
1842 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001843 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001844 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001845 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001846 goto fail;
1847 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001848 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001849 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001850
1851 err = d40_allocate_channel(d40c);
1852 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001853 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001854 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001855 }
1856
Linus Walleijef1872e2010-06-20 21:24:52 +00001857 /* Fill in basic CFG register values */
1858 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01001859 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00001860
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001861 d40_set_prio_realtime(d40c);
1862
Rabin Vincent724a8572011-01-25 11:18:08 +01001863 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00001864 d40_log_cfg(&d40c->dma_cfg,
1865 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1866
1867 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1868 d40c->lcpa = d40c->base->lcpa_base +
1869 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1870 else
1871 d40c->lcpa = d40c->base->lcpa_base +
1872 d40c->dma_cfg.dst_dev_type *
1873 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1874 }
1875
1876 /*
1877 * Only write channel configuration to the DMA if the physical
1878 * resource is free. In case of multiple logical channels
1879 * on the same physical resource, only the first write is necessary.
1880 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001881 if (is_free_phy)
1882 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001883fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001884 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001885 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001886}
1887
1888static void d40_free_chan_resources(struct dma_chan *chan)
1889{
1890 struct d40_chan *d40c =
1891 container_of(chan, struct d40_chan, chan);
1892 int err;
1893 unsigned long flags;
1894
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001895 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001896 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001897 return;
1898 }
1899
1900
Linus Walleij8d318a52010-03-30 15:33:42 +02001901 spin_lock_irqsave(&d40c->lock, flags);
1902
1903 err = d40_free_dma(d40c);
1904
1905 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001906 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001907 spin_unlock_irqrestore(&d40c->lock, flags);
1908}
1909
1910static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1911 dma_addr_t dst,
1912 dma_addr_t src,
1913 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001914 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001915{
Rabin Vincent95944c62011-01-25 11:18:17 +01001916 struct scatterlist dst_sg;
1917 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02001918
Rabin Vincent95944c62011-01-25 11:18:17 +01001919 sg_init_table(&dst_sg, 1);
1920 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001921
Rabin Vincent95944c62011-01-25 11:18:17 +01001922 sg_dma_address(&dst_sg) = dst;
1923 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02001924
Rabin Vincent95944c62011-01-25 11:18:17 +01001925 sg_dma_len(&dst_sg) = size;
1926 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001927
Rabin Vincent95944c62011-01-25 11:18:17 +01001928 return stedma40_memcpy_sg(chan, &dst_sg, &src_sg, 1, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001929}
1930
Ira Snyder0d688662010-09-30 11:46:47 +00001931static struct dma_async_tx_descriptor *
1932d40_prep_sg(struct dma_chan *chan,
1933 struct scatterlist *dst_sg, unsigned int dst_nents,
1934 struct scatterlist *src_sg, unsigned int src_nents,
1935 unsigned long dma_flags)
1936{
1937 if (dst_nents != src_nents)
1938 return NULL;
1939
1940 return stedma40_memcpy_sg(chan, dst_sg, src_sg, dst_nents, dma_flags);
1941}
1942
Rabin Vincent00ac0342011-01-25 11:18:20 +01001943static dma_addr_t
1944d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
1945{
1946 struct stedma40_platform_data *plat = chan->base->plat_data;
1947 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1948 dma_addr_t addr;
1949
1950 if (chan->runtime_addr)
1951 return chan->runtime_addr;
1952
1953 if (direction == DMA_FROM_DEVICE)
1954 addr = plat->dev_rx[cfg->src_dev_type];
1955 else if (direction == DMA_TO_DEVICE)
1956 addr = plat->dev_tx[cfg->dst_dev_type];
1957
1958 return addr;
1959}
1960
Linus Walleij8d318a52010-03-30 15:33:42 +02001961static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
1962 struct scatterlist *sgl,
1963 unsigned int sg_len,
1964 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001965 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001966{
1967 struct d40_desc *d40d;
1968 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1969 chan);
Rabin Vincent00ac0342011-01-25 11:18:20 +01001970 dma_addr_t dev_addr;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001971 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001972 int err;
1973
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001974 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001975 chan_err(d40c, "Cannot prepare unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001976 return ERR_PTR(-EINVAL);
1977 }
1978
Rabin Vincent00ac0342011-01-25 11:18:20 +01001979 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
1980 return NULL;
1981
Jonas Aaberg2a614342010-06-20 21:25:24 +00001982 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001983
Rabin Vincent5f811582011-01-25 11:18:18 +01001984 d40d = d40_prep_desc(d40c, sgl, sg_len, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001985 if (d40d == NULL)
Rabin Vincent819504f2010-10-06 08:20:38 +00001986 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001987
Rabin Vincent00ac0342011-01-25 11:18:20 +01001988 dev_addr = d40_get_dev_addr(d40c, direction);
1989
Rabin Vincent724a8572011-01-25 11:18:08 +01001990 if (chan_is_logical(d40c))
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001991 err = d40_prep_sg_log(d40c, d40d, sgl, NULL,
1992 sg_len, direction, dev_addr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001993 else
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001994 err = d40_prep_sg_phy(d40c, d40d, sgl, NULL,
1995 sg_len, direction, dev_addr);
1996
Linus Walleij8d318a52010-03-30 15:33:42 +02001997 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001998 chan_err(d40c, "Failed to prepare %s slave sg job: %d\n",
Rabin Vincent724a8572011-01-25 11:18:08 +01001999 chan_is_logical(d40c) ? "log" : "phy", err);
Rabin Vincent819504f2010-10-06 08:20:38 +00002000 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002001 }
2002
Rabin Vincent819504f2010-10-06 08:20:38 +00002003 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002004 return &d40d->txd;
Rabin Vincent819504f2010-10-06 08:20:38 +00002005
2006err:
2007 if (d40d)
2008 d40_desc_free(d40c, d40d);
2009 spin_unlock_irqrestore(&d40c->lock, flags);
2010 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02002011}
2012
2013static enum dma_status d40_tx_status(struct dma_chan *chan,
2014 dma_cookie_t cookie,
2015 struct dma_tx_state *txstate)
2016{
2017 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2018 dma_cookie_t last_used;
2019 dma_cookie_t last_complete;
2020 int ret;
2021
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002022 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002023 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002024 return -EINVAL;
2025 }
2026
Linus Walleij8d318a52010-03-30 15:33:42 +02002027 last_complete = d40c->completed;
2028 last_used = chan->cookie;
2029
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002030 if (d40_is_paused(d40c))
2031 ret = DMA_PAUSED;
2032 else
2033 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002034
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002035 dma_set_tx_state(txstate, last_complete, last_used,
2036 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002037
2038 return ret;
2039}
2040
2041static void d40_issue_pending(struct dma_chan *chan)
2042{
2043 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2044 unsigned long flags;
2045
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002046 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002047 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002048 return;
2049 }
2050
Linus Walleij8d318a52010-03-30 15:33:42 +02002051 spin_lock_irqsave(&d40c->lock, flags);
2052
2053 /* Busy means that pending jobs are already being processed */
2054 if (!d40c->busy)
2055 (void) d40_queue_start(d40c);
2056
2057 spin_unlock_irqrestore(&d40c->lock, flags);
2058}
2059
Linus Walleij95e14002010-08-04 13:37:45 +02002060/* Runtime reconfiguration extension */
2061static void d40_set_runtime_config(struct dma_chan *chan,
2062 struct dma_slave_config *config)
2063{
2064 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2065 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2066 enum dma_slave_buswidth config_addr_width;
2067 dma_addr_t config_addr;
2068 u32 config_maxburst;
2069 enum stedma40_periph_data_width addr_width;
2070 int psize;
2071
2072 if (config->direction == DMA_FROM_DEVICE) {
2073 dma_addr_t dev_addr_rx =
2074 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2075
2076 config_addr = config->src_addr;
2077 if (dev_addr_rx)
2078 dev_dbg(d40c->base->dev,
2079 "channel has a pre-wired RX address %08x "
2080 "overriding with %08x\n",
2081 dev_addr_rx, config_addr);
2082 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2083 dev_dbg(d40c->base->dev,
2084 "channel was not configured for peripheral "
2085 "to memory transfer (%d) overriding\n",
2086 cfg->dir);
2087 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2088
2089 config_addr_width = config->src_addr_width;
2090 config_maxburst = config->src_maxburst;
2091
2092 } else if (config->direction == DMA_TO_DEVICE) {
2093 dma_addr_t dev_addr_tx =
2094 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2095
2096 config_addr = config->dst_addr;
2097 if (dev_addr_tx)
2098 dev_dbg(d40c->base->dev,
2099 "channel has a pre-wired TX address %08x "
2100 "overriding with %08x\n",
2101 dev_addr_tx, config_addr);
2102 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2103 dev_dbg(d40c->base->dev,
2104 "channel was not configured for memory "
2105 "to peripheral transfer (%d) overriding\n",
2106 cfg->dir);
2107 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2108
2109 config_addr_width = config->dst_addr_width;
2110 config_maxburst = config->dst_maxburst;
2111
2112 } else {
2113 dev_err(d40c->base->dev,
2114 "unrecognized channel direction %d\n",
2115 config->direction);
2116 return;
2117 }
2118
2119 switch (config_addr_width) {
2120 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2121 addr_width = STEDMA40_BYTE_WIDTH;
2122 break;
2123 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2124 addr_width = STEDMA40_HALFWORD_WIDTH;
2125 break;
2126 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2127 addr_width = STEDMA40_WORD_WIDTH;
2128 break;
2129 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2130 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2131 break;
2132 default:
2133 dev_err(d40c->base->dev,
2134 "illegal peripheral address width "
2135 "requested (%d)\n",
2136 config->src_addr_width);
2137 return;
2138 }
2139
Rabin Vincent724a8572011-01-25 11:18:08 +01002140 if (chan_is_logical(d40c)) {
Per Forlina59670a2010-10-06 09:05:27 +00002141 if (config_maxburst >= 16)
2142 psize = STEDMA40_PSIZE_LOG_16;
2143 else if (config_maxburst >= 8)
2144 psize = STEDMA40_PSIZE_LOG_8;
2145 else if (config_maxburst >= 4)
2146 psize = STEDMA40_PSIZE_LOG_4;
2147 else
2148 psize = STEDMA40_PSIZE_LOG_1;
2149 } else {
2150 if (config_maxburst >= 16)
2151 psize = STEDMA40_PSIZE_PHY_16;
2152 else if (config_maxburst >= 8)
2153 psize = STEDMA40_PSIZE_PHY_8;
2154 else if (config_maxburst >= 4)
2155 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002156 else if (config_maxburst >= 2)
2157 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002158 else
2159 psize = STEDMA40_PSIZE_PHY_1;
2160 }
Linus Walleij95e14002010-08-04 13:37:45 +02002161
2162 /* Set up all the endpoint configs */
2163 cfg->src_info.data_width = addr_width;
2164 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002165 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002166 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2167 cfg->dst_info.data_width = addr_width;
2168 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002169 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002170 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2171
Per Forlina59670a2010-10-06 09:05:27 +00002172 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002173 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002174 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2175 else
2176 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2177 &d40c->dst_def_cfg, false);
2178
Linus Walleij95e14002010-08-04 13:37:45 +02002179 /* These settings will take precedence later */
2180 d40c->runtime_addr = config_addr;
2181 d40c->runtime_direction = config->direction;
2182 dev_dbg(d40c->base->dev,
2183 "configured channel %s for %s, data width %d, "
2184 "maxburst %d bytes, LE, no flow control\n",
2185 dma_chan_name(chan),
2186 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2187 config_addr_width,
2188 config_maxburst);
2189}
2190
Linus Walleij05827632010-05-17 16:30:42 -07002191static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2192 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002193{
2194 unsigned long flags;
2195 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2196
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002197 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002198 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002199 return -EINVAL;
2200 }
2201
Linus Walleij8d318a52010-03-30 15:33:42 +02002202 switch (cmd) {
2203 case DMA_TERMINATE_ALL:
2204 spin_lock_irqsave(&d40c->lock, flags);
2205 d40_term_all(d40c);
2206 spin_unlock_irqrestore(&d40c->lock, flags);
2207 return 0;
2208 case DMA_PAUSE:
2209 return d40_pause(chan);
2210 case DMA_RESUME:
2211 return d40_resume(chan);
Linus Walleij95e14002010-08-04 13:37:45 +02002212 case DMA_SLAVE_CONFIG:
2213 d40_set_runtime_config(chan,
2214 (struct dma_slave_config *) arg);
2215 return 0;
2216 default:
2217 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002218 }
2219
2220 /* Other commands are unimplemented */
2221 return -ENXIO;
2222}
2223
2224/* Initialization functions */
2225
2226static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2227 struct d40_chan *chans, int offset,
2228 int num_chans)
2229{
2230 int i = 0;
2231 struct d40_chan *d40c;
2232
2233 INIT_LIST_HEAD(&dma->channels);
2234
2235 for (i = offset; i < offset + num_chans; i++) {
2236 d40c = &chans[i];
2237 d40c->base = base;
2238 d40c->chan.device = dma;
2239
Linus Walleij8d318a52010-03-30 15:33:42 +02002240 spin_lock_init(&d40c->lock);
2241
2242 d40c->log_num = D40_PHY_CHAN;
2243
Linus Walleij8d318a52010-03-30 15:33:42 +02002244 INIT_LIST_HEAD(&d40c->active);
2245 INIT_LIST_HEAD(&d40c->queue);
2246 INIT_LIST_HEAD(&d40c->client);
2247
Linus Walleij8d318a52010-03-30 15:33:42 +02002248 tasklet_init(&d40c->tasklet, dma_tasklet,
2249 (unsigned long) d40c);
2250
2251 list_add_tail(&d40c->chan.device_node,
2252 &dma->channels);
2253 }
2254}
2255
2256static int __init d40_dmaengine_init(struct d40_base *base,
2257 int num_reserved_chans)
2258{
2259 int err ;
2260
2261 d40_chan_init(base, &base->dma_slave, base->log_chans,
2262 0, base->num_log_chans);
2263
2264 dma_cap_zero(base->dma_slave.cap_mask);
2265 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2266
2267 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2268 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2269 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002270 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002271 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2272 base->dma_slave.device_tx_status = d40_tx_status;
2273 base->dma_slave.device_issue_pending = d40_issue_pending;
2274 base->dma_slave.device_control = d40_control;
2275 base->dma_slave.dev = base->dev;
2276
2277 err = dma_async_device_register(&base->dma_slave);
2278
2279 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002280 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002281 goto failure1;
2282 }
2283
2284 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2285 base->num_log_chans, base->plat_data->memcpy_len);
2286
2287 dma_cap_zero(base->dma_memcpy.cap_mask);
2288 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002289 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002290
2291 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2292 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2293 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002294 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002295 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2296 base->dma_memcpy.device_tx_status = d40_tx_status;
2297 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2298 base->dma_memcpy.device_control = d40_control;
2299 base->dma_memcpy.dev = base->dev;
2300 /*
2301 * This controller can only access address at even
2302 * 32bit boundaries, i.e. 2^2
2303 */
2304 base->dma_memcpy.copy_align = 2;
2305
2306 err = dma_async_device_register(&base->dma_memcpy);
2307
2308 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002309 d40_err(base->dev,
2310 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002311 goto failure2;
2312 }
2313
2314 d40_chan_init(base, &base->dma_both, base->phy_chans,
2315 0, num_reserved_chans);
2316
2317 dma_cap_zero(base->dma_both.cap_mask);
2318 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2319 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002320 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002321
2322 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2323 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2324 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002325 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002326 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2327 base->dma_both.device_tx_status = d40_tx_status;
2328 base->dma_both.device_issue_pending = d40_issue_pending;
2329 base->dma_both.device_control = d40_control;
2330 base->dma_both.dev = base->dev;
2331 base->dma_both.copy_align = 2;
2332 err = dma_async_device_register(&base->dma_both);
2333
2334 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002335 d40_err(base->dev,
2336 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002337 goto failure3;
2338 }
2339 return 0;
2340failure3:
2341 dma_async_device_unregister(&base->dma_memcpy);
2342failure2:
2343 dma_async_device_unregister(&base->dma_slave);
2344failure1:
2345 return err;
2346}
2347
2348/* Initialization functions. */
2349
2350static int __init d40_phy_res_init(struct d40_base *base)
2351{
2352 int i;
2353 int num_phy_chans_avail = 0;
2354 u32 val[2];
2355 int odd_even_bit = -2;
2356
2357 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2358 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2359
2360 for (i = 0; i < base->num_phy_chans; i++) {
2361 base->phy_res[i].num = i;
2362 odd_even_bit += 2 * ((i % 2) == 0);
2363 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2364 /* Mark security only channels as occupied */
2365 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2366 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2367 } else {
2368 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2369 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2370 num_phy_chans_avail++;
2371 }
2372 spin_lock_init(&base->phy_res[i].lock);
2373 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002374
2375 /* Mark disabled channels as occupied */
2376 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002377 int chan = base->plat_data->disabled_channels[i];
2378
2379 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2380 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2381 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002382 }
2383
Linus Walleij8d318a52010-03-30 15:33:42 +02002384 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2385 num_phy_chans_avail, base->num_phy_chans);
2386
2387 /* Verify settings extended vs standard */
2388 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2389
2390 for (i = 0; i < base->num_phy_chans; i++) {
2391
2392 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2393 (val[0] & 0x3) != 1)
2394 dev_info(base->dev,
2395 "[%s] INFO: channel %d is misconfigured (%d)\n",
2396 __func__, i, val[0] & 0x3);
2397
2398 val[0] = val[0] >> 2;
2399 }
2400
2401 return num_phy_chans_avail;
2402}
2403
2404static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2405{
2406 static const struct d40_reg_val dma_id_regs[] = {
2407 /* Peripheral Id */
2408 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2409 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2410 /*
2411 * D40_DREG_PERIPHID2 Depends on HW revision:
Rabin Vincent4d594902011-01-25 11:18:10 +01002412 * DB8500ed has 0x0008,
Linus Walleij8d318a52010-03-30 15:33:42 +02002413 * ? has 0x0018,
Rabin Vincent4d594902011-01-25 11:18:10 +01002414 * DB8500v1 has 0x0028
2415 * DB8500v2 has 0x0038
Linus Walleij8d318a52010-03-30 15:33:42 +02002416 */
2417 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2418
2419 /* PCell Id */
2420 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2421 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2422 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2423 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2424 };
2425 struct stedma40_platform_data *plat_data;
2426 struct clk *clk = NULL;
2427 void __iomem *virtbase = NULL;
2428 struct resource *res = NULL;
2429 struct d40_base *base = NULL;
2430 int num_log_chans = 0;
2431 int num_phy_chans;
2432 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002433 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002434 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002435
2436 clk = clk_get(&pdev->dev, NULL);
2437
2438 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002439 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002440 goto failure;
2441 }
2442
2443 clk_enable(clk);
2444
2445 /* Get IO for DMAC base address */
2446 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2447 if (!res)
2448 goto failure;
2449
2450 if (request_mem_region(res->start, resource_size(res),
2451 D40_NAME " I/O base") == NULL)
2452 goto failure;
2453
2454 virtbase = ioremap(res->start, resource_size(res));
2455 if (!virtbase)
2456 goto failure;
2457
2458 /* HW version check */
2459 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2460 if (dma_id_regs[i].val !=
2461 readl(virtbase + dma_id_regs[i].reg)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002462 d40_err(&pdev->dev,
2463 "Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002464 dma_id_regs[i].val,
2465 dma_id_regs[i].reg,
2466 readl(virtbase + dma_id_regs[i].reg));
2467 goto failure;
2468 }
2469 }
2470
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002471 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002472 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002473
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002474 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2475 D40_HW_DESIGNER) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002476 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2477 val & D40_DREG_PERIPHID2_DESIGNER_MASK,
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002478 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002479 goto failure;
2480 }
2481
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002482 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2483 D40_DREG_PERIPHID2_REV_POS;
2484
Linus Walleij8d318a52010-03-30 15:33:42 +02002485 /* The number of physical channels on this HW */
2486 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2487
2488 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002489 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002490
2491 plat_data = pdev->dev.platform_data;
2492
2493 /* Count the number of logical channels in use */
2494 for (i = 0; i < plat_data->dev_len; i++)
2495 if (plat_data->dev_rx[i] != 0)
2496 num_log_chans++;
2497
2498 for (i = 0; i < plat_data->dev_len; i++)
2499 if (plat_data->dev_tx[i] != 0)
2500 num_log_chans++;
2501
2502 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2503 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2504 sizeof(struct d40_chan), GFP_KERNEL);
2505
2506 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002507 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002508 goto failure;
2509 }
2510
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002511 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002512 base->clk = clk;
2513 base->num_phy_chans = num_phy_chans;
2514 base->num_log_chans = num_log_chans;
2515 base->phy_start = res->start;
2516 base->phy_size = resource_size(res);
2517 base->virtbase = virtbase;
2518 base->plat_data = plat_data;
2519 base->dev = &pdev->dev;
2520 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2521 base->log_chans = &base->phy_chans[num_phy_chans];
2522
2523 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2524 GFP_KERNEL);
2525 if (!base->phy_res)
2526 goto failure;
2527
2528 base->lookup_phy_chans = kzalloc(num_phy_chans *
2529 sizeof(struct d40_chan *),
2530 GFP_KERNEL);
2531 if (!base->lookup_phy_chans)
2532 goto failure;
2533
2534 if (num_log_chans + plat_data->memcpy_len) {
2535 /*
2536 * The max number of logical channels are event lines for all
2537 * src devices and dst devices
2538 */
2539 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2540 sizeof(struct d40_chan *),
2541 GFP_KERNEL);
2542 if (!base->lookup_log_chans)
2543 goto failure;
2544 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002545
2546 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2547 sizeof(struct d40_desc *) *
2548 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002549 GFP_KERNEL);
2550 if (!base->lcla_pool.alloc_map)
2551 goto failure;
2552
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002553 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2554 0, SLAB_HWCACHE_ALIGN,
2555 NULL);
2556 if (base->desc_slab == NULL)
2557 goto failure;
2558
Linus Walleij8d318a52010-03-30 15:33:42 +02002559 return base;
2560
2561failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002562 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002563 clk_disable(clk);
2564 clk_put(clk);
2565 }
2566 if (virtbase)
2567 iounmap(virtbase);
2568 if (res)
2569 release_mem_region(res->start,
2570 resource_size(res));
2571 if (virtbase)
2572 iounmap(virtbase);
2573
2574 if (base) {
2575 kfree(base->lcla_pool.alloc_map);
2576 kfree(base->lookup_log_chans);
2577 kfree(base->lookup_phy_chans);
2578 kfree(base->phy_res);
2579 kfree(base);
2580 }
2581
2582 return NULL;
2583}
2584
2585static void __init d40_hw_init(struct d40_base *base)
2586{
2587
2588 static const struct d40_reg_val dma_init_reg[] = {
2589 /* Clock every part of the DMA block from start */
2590 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2591
2592 /* Interrupts on all logical channels */
2593 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2594 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2595 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2596 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2597 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2598 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2599 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2600 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2601 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2602 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2603 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2604 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2605 };
2606 int i;
2607 u32 prmseo[2] = {0, 0};
2608 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2609 u32 pcmis = 0;
2610 u32 pcicr = 0;
2611
2612 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2613 writel(dma_init_reg[i].val,
2614 base->virtbase + dma_init_reg[i].reg);
2615
2616 /* Configure all our dma channels to default settings */
2617 for (i = 0; i < base->num_phy_chans; i++) {
2618
2619 activeo[i % 2] = activeo[i % 2] << 2;
2620
2621 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2622 == D40_ALLOC_PHY) {
2623 activeo[i % 2] |= 3;
2624 continue;
2625 }
2626
2627 /* Enable interrupt # */
2628 pcmis = (pcmis << 1) | 1;
2629
2630 /* Clear interrupt # */
2631 pcicr = (pcicr << 1) | 1;
2632
2633 /* Set channel to physical mode */
2634 prmseo[i % 2] = prmseo[i % 2] << 2;
2635 prmseo[i % 2] |= 1;
2636
2637 }
2638
2639 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2640 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2641 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2642 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2643
2644 /* Write which interrupt to enable */
2645 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2646
2647 /* Write which interrupt to clear */
2648 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2649
2650}
2651
Linus Walleij508849a2010-06-20 21:26:07 +00002652static int __init d40_lcla_allocate(struct d40_base *base)
2653{
Rabin Vincent026cbc42011-01-25 11:18:14 +01002654 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00002655 unsigned long *page_list;
2656 int i, j;
2657 int ret = 0;
2658
2659 /*
2660 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2661 * To full fill this hardware requirement without wasting 256 kb
2662 * we allocate pages until we get an aligned one.
2663 */
2664 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2665 GFP_KERNEL);
2666
2667 if (!page_list) {
2668 ret = -ENOMEM;
2669 goto failure;
2670 }
2671
2672 /* Calculating how many pages that are required */
2673 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2674
2675 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2676 page_list[i] = __get_free_pages(GFP_KERNEL,
2677 base->lcla_pool.pages);
2678 if (!page_list[i]) {
2679
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002680 d40_err(base->dev, "Failed to allocate %d pages.\n",
2681 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002682
2683 for (j = 0; j < i; j++)
2684 free_pages(page_list[j], base->lcla_pool.pages);
2685 goto failure;
2686 }
2687
2688 if ((virt_to_phys((void *)page_list[i]) &
2689 (LCLA_ALIGNMENT - 1)) == 0)
2690 break;
2691 }
2692
2693 for (j = 0; j < i; j++)
2694 free_pages(page_list[j], base->lcla_pool.pages);
2695
2696 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2697 base->lcla_pool.base = (void *)page_list[i];
2698 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002699 /*
2700 * After many attempts and no succees with finding the correct
2701 * alignment, try with allocating a big buffer.
2702 */
Linus Walleij508849a2010-06-20 21:26:07 +00002703 dev_warn(base->dev,
2704 "[%s] Failed to get %d pages @ 18 bit align.\n",
2705 __func__, base->lcla_pool.pages);
2706 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2707 base->num_phy_chans +
2708 LCLA_ALIGNMENT,
2709 GFP_KERNEL);
2710 if (!base->lcla_pool.base_unaligned) {
2711 ret = -ENOMEM;
2712 goto failure;
2713 }
2714
2715 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2716 LCLA_ALIGNMENT);
2717 }
2718
Rabin Vincent026cbc42011-01-25 11:18:14 +01002719 pool->dma_addr = dma_map_single(base->dev, pool->base,
2720 SZ_1K * base->num_phy_chans,
2721 DMA_TO_DEVICE);
2722 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2723 pool->dma_addr = 0;
2724 ret = -ENOMEM;
2725 goto failure;
2726 }
2727
Linus Walleij508849a2010-06-20 21:26:07 +00002728 writel(virt_to_phys(base->lcla_pool.base),
2729 base->virtbase + D40_DREG_LCLA);
2730failure:
2731 kfree(page_list);
2732 return ret;
2733}
2734
Linus Walleij8d318a52010-03-30 15:33:42 +02002735static int __init d40_probe(struct platform_device *pdev)
2736{
2737 int err;
2738 int ret = -ENOENT;
2739 struct d40_base *base;
2740 struct resource *res = NULL;
2741 int num_reserved_chans;
2742 u32 val;
2743
2744 base = d40_hw_detect_init(pdev);
2745
2746 if (!base)
2747 goto failure;
2748
2749 num_reserved_chans = d40_phy_res_init(base);
2750
2751 platform_set_drvdata(pdev, base);
2752
2753 spin_lock_init(&base->interrupt_lock);
2754 spin_lock_init(&base->execmd_lock);
2755
2756 /* Get IO for logical channel parameter address */
2757 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2758 if (!res) {
2759 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002760 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002761 goto failure;
2762 }
2763 base->lcpa_size = resource_size(res);
2764 base->phy_lcpa = res->start;
2765
2766 if (request_mem_region(res->start, resource_size(res),
2767 D40_NAME " I/O lcpa") == NULL) {
2768 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002769 d40_err(&pdev->dev,
2770 "Failed to request LCPA region 0x%x-0x%x\n",
2771 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002772 goto failure;
2773 }
2774
2775 /* We make use of ESRAM memory for this. */
2776 val = readl(base->virtbase + D40_DREG_LCPA);
2777 if (res->start != val && val != 0) {
2778 dev_warn(&pdev->dev,
2779 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2780 __func__, val, res->start);
2781 } else
2782 writel(res->start, base->virtbase + D40_DREG_LCPA);
2783
2784 base->lcpa_base = ioremap(res->start, resource_size(res));
2785 if (!base->lcpa_base) {
2786 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002787 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002788 goto failure;
2789 }
Linus Walleij508849a2010-06-20 21:26:07 +00002790
2791 ret = d40_lcla_allocate(base);
2792 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002793 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002794 goto failure;
2795 }
2796
Linus Walleij8d318a52010-03-30 15:33:42 +02002797 spin_lock_init(&base->lcla_pool.lock);
2798
Linus Walleij8d318a52010-03-30 15:33:42 +02002799 base->irq = platform_get_irq(pdev, 0);
2800
2801 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002802 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002803 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002804 goto failure;
2805 }
2806
2807 err = d40_dmaengine_init(base, num_reserved_chans);
2808 if (err)
2809 goto failure;
2810
2811 d40_hw_init(base);
2812
2813 dev_info(base->dev, "initialized\n");
2814 return 0;
2815
2816failure:
2817 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002818 if (base->desc_slab)
2819 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002820 if (base->virtbase)
2821 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01002822
2823 if (base->lcla_pool.dma_addr)
2824 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2825 SZ_1K * base->num_phy_chans,
2826 DMA_TO_DEVICE);
2827
Linus Walleij508849a2010-06-20 21:26:07 +00002828 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2829 free_pages((unsigned long)base->lcla_pool.base,
2830 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002831
2832 kfree(base->lcla_pool.base_unaligned);
2833
Linus Walleij8d318a52010-03-30 15:33:42 +02002834 if (base->phy_lcpa)
2835 release_mem_region(base->phy_lcpa,
2836 base->lcpa_size);
2837 if (base->phy_start)
2838 release_mem_region(base->phy_start,
2839 base->phy_size);
2840 if (base->clk) {
2841 clk_disable(base->clk);
2842 clk_put(base->clk);
2843 }
2844
2845 kfree(base->lcla_pool.alloc_map);
2846 kfree(base->lookup_log_chans);
2847 kfree(base->lookup_phy_chans);
2848 kfree(base->phy_res);
2849 kfree(base);
2850 }
2851
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002852 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002853 return ret;
2854}
2855
2856static struct platform_driver d40_driver = {
2857 .driver = {
2858 .owner = THIS_MODULE,
2859 .name = D40_NAME,
2860 },
2861};
2862
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002863static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002864{
2865 return platform_driver_probe(&d40_driver, d40_probe);
2866}
2867arch_initcall(stedma40_init);