blob: c597dba713b00587a8c67d87af30db92755dfe48 [file] [log] [blame]
Linus Walleij8d318a52010-03-30 15:33:42 +02001/*
Per Forlind49278e2010-12-20 18:31:38 +01002 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
Per Forlin661385f2010-10-06 09:05:28 +00004 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
Jonas Aaberg767a9672010-08-09 12:08:34 +00005 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
Linus Walleij8d318a52010-03-30 15:33:42 +02006 * License terms: GNU General Public License (GPL) version 2
Linus Walleij8d318a52010-03-30 15:33:42 +02007 */
8
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/dmaengine.h>
12#include <linux/platform_device.h>
13#include <linux/clk.h>
14#include <linux/delay.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000015#include <linux/err.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020016
17#include <plat/ste_dma40.h>
18
19#include "ste_dma40_ll.h"
20
21#define D40_NAME "dma40"
22
23#define D40_PHY_CHAN -1
24
25/* For masking out/in 2 bit channel positions */
26#define D40_CHAN_POS(chan) (2 * (chan / 2))
27#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
28
29/* Maximum iterations taken before giving up suspending a channel */
30#define D40_SUSPEND_MAX_IT 500
31
Linus Walleij508849a2010-06-20 21:26:07 +000032/* Hardware requirement on LCLA alignment */
33#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000034
35/* Max number of links per event group */
36#define D40_LCLA_LINK_PER_EVENT_GRP 128
37#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
38
Linus Walleij508849a2010-06-20 21:26:07 +000039/* Attempts before giving up to trying to get pages that are aligned */
40#define MAX_LCLA_ALLOC_ATTEMPTS 256
41
42/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020043#define D40_ALLOC_FREE (1 << 31)
44#define D40_ALLOC_PHY (1 << 30)
45#define D40_ALLOC_LOG_FREE 0
46
Linus Walleij8d318a52010-03-30 15:33:42 +020047/* Hardware designer of the block */
Jonas Aaberg3ae02672010-08-09 12:08:18 +000048#define D40_HW_DESIGNER 0x8
Linus Walleij8d318a52010-03-30 15:33:42 +020049
50/**
51 * enum 40_command - The different commands and/or statuses.
52 *
53 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
54 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
55 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
56 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
57 */
58enum d40_command {
59 D40_DMA_STOP = 0,
60 D40_DMA_RUN = 1,
61 D40_DMA_SUSPEND_REQ = 2,
62 D40_DMA_SUSPENDED = 3
63};
64
65/**
66 * struct d40_lli_pool - Structure for keeping LLIs in memory
67 *
68 * @base: Pointer to memory area when the pre_alloc_lli's are not large
69 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
70 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +010071 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +020072 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
73 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
74 * one buffer to one buffer.
75 */
76struct d40_lli_pool {
77 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000078 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +010079 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +020080 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000081 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020082};
83
84/**
85 * struct d40_desc - A descriptor is one DMA job.
86 *
87 * @lli_phy: LLI settings for physical channel. Both src and dst=
88 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
89 * lli_len equals one.
90 * @lli_log: Same as above but for logical channels.
91 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000092 * @lli_len: Number of llis of current descriptor.
Jonas Aaberg698e4732010-08-09 12:08:56 +000093 * @lli_current: Number of transfered llis.
94 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020095 * @txd: DMA engine struct. Used for among other things for communication
96 * during a transfer.
97 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020098 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000099 * the previous one.
Linus Walleij8d318a52010-03-30 15:33:42 +0200100 *
101 * This descriptor is used for both logical and physical transfers.
102 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200103struct d40_desc {
104 /* LLI physical */
105 struct d40_phy_lli_bidir lli_phy;
106 /* LLI logical */
107 struct d40_log_lli_bidir lli_log;
108
109 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000110 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000111 int lli_current;
112 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200113
114 struct dma_async_tx_descriptor txd;
115 struct list_head node;
116
Linus Walleij8d318a52010-03-30 15:33:42 +0200117 bool is_in_client_list;
118};
119
120/**
121 * struct d40_lcla_pool - LCLA pool settings and data.
122 *
Linus Walleij508849a2010-06-20 21:26:07 +0000123 * @base: The virtual address of LCLA. 18 bit aligned.
124 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
125 * This pointer is only there for clean-up on error.
126 * @pages: The number of pages needed for all physical channels.
127 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200128 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000129 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200130 */
131struct d40_lcla_pool {
132 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100133 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000134 void *base_unaligned;
135 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200136 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000137 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200138};
139
140/**
141 * struct d40_phy_res - struct for handling eventlines mapped to physical
142 * channels.
143 *
144 * @lock: A lock protection this entity.
145 * @num: The physical channel number of this entity.
146 * @allocated_src: Bit mapped to show which src event line's are mapped to
147 * this physical channel. Can also be free or physically allocated.
148 * @allocated_dst: Same as for src but is dst.
149 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000150 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200151 */
152struct d40_phy_res {
153 spinlock_t lock;
154 int num;
155 u32 allocated_src;
156 u32 allocated_dst;
157};
158
159struct d40_base;
160
161/**
162 * struct d40_chan - Struct that describes a channel.
163 *
164 * @lock: A spinlock to protect this struct.
165 * @log_num: The logical number, if any of this channel.
166 * @completed: Starts with 1, after first interrupt it is set to dma engine's
167 * current cookie.
168 * @pending_tx: The number of pending transfers. Used between interrupt handler
169 * and tasklet.
170 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000171 * @phy_chan: Pointer to physical channel which this instance runs on. If this
172 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200173 * @chan: DMA engine handle.
174 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
175 * transfer and call client callback.
176 * @client: Cliented owned descriptor list.
177 * @active: Active descriptor.
178 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200179 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000180 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200181 * @base: Pointer to the device instance struct.
182 * @src_def_cfg: Default cfg register setting for src.
183 * @dst_def_cfg: Default cfg register setting for dst.
184 * @log_def: Default logical channel settings.
185 * @lcla: Space for one dst src pair for logical channel transfers.
186 * @lcpa: Pointer to dst and src lcpa settings.
187 *
188 * This struct can either "be" a logical or a physical channel.
189 */
190struct d40_chan {
191 spinlock_t lock;
192 int log_num;
193 /* ID of the most recent completed transfer */
194 int completed;
195 int pending_tx;
196 bool busy;
197 struct d40_phy_res *phy_chan;
198 struct dma_chan chan;
199 struct tasklet_struct tasklet;
200 struct list_head client;
201 struct list_head active;
202 struct list_head queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200203 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000204 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200205 struct d40_base *base;
206 /* Default register configurations */
207 u32 src_def_cfg;
208 u32 dst_def_cfg;
209 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200210 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200211 /* Runtime reconfiguration */
212 dma_addr_t runtime_addr;
213 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200214};
215
216/**
217 * struct d40_base - The big global struct, one for each probe'd instance.
218 *
219 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
220 * @execmd_lock: Lock for execute command usage since several channels share
221 * the same physical register.
222 * @dev: The device structure.
223 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700224 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200225 * @clk: Pointer to the DMA clock structure.
226 * @phy_start: Physical memory start of the DMA registers.
227 * @phy_size: Size of the DMA register map.
228 * @irq: The IRQ number.
229 * @num_phy_chans: The number of physical channels. Read from HW. This
230 * is the number of available channels for this driver, not counting "Secure
231 * mode" allocated physical channels.
232 * @num_log_chans: The number of logical channels. Calculated from
233 * num_phy_chans.
234 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
235 * @dma_slave: dma_device channels that can do only do slave transfers.
236 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200237 * @log_chans: Room for all possible logical channels in system.
238 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
239 * to log_chans entries.
240 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
241 * to phy_chans entries.
242 * @plat_data: Pointer to provided platform_data which is the driver
243 * configuration.
244 * @phy_res: Vector containing all physical channels.
245 * @lcla_pool: lcla pool settings and data.
246 * @lcpa_base: The virtual mapped address of LCPA.
247 * @phy_lcpa: The physical address of the LCPA.
248 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000249 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200250 */
251struct d40_base {
252 spinlock_t interrupt_lock;
253 spinlock_t execmd_lock;
254 struct device *dev;
255 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700256 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200257 struct clk *clk;
258 phys_addr_t phy_start;
259 resource_size_t phy_size;
260 int irq;
261 int num_phy_chans;
262 int num_log_chans;
263 struct dma_device dma_both;
264 struct dma_device dma_slave;
265 struct dma_device dma_memcpy;
266 struct d40_chan *phy_chans;
267 struct d40_chan *log_chans;
268 struct d40_chan **lookup_log_chans;
269 struct d40_chan **lookup_phy_chans;
270 struct stedma40_platform_data *plat_data;
271 /* Physical half channels */
272 struct d40_phy_res *phy_res;
273 struct d40_lcla_pool lcla_pool;
274 void *lcpa_base;
275 dma_addr_t phy_lcpa;
276 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000277 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200278};
279
280/**
281 * struct d40_interrupt_lookup - lookup table for interrupt handler
282 *
283 * @src: Interrupt mask register.
284 * @clr: Interrupt clear register.
285 * @is_error: true if this is an error interrupt.
286 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
287 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
288 */
289struct d40_interrupt_lookup {
290 u32 src;
291 u32 clr;
292 bool is_error;
293 int offset;
294};
295
296/**
297 * struct d40_reg_val - simple lookup struct
298 *
299 * @reg: The register.
300 * @val: The value that belongs to the register in reg.
301 */
302struct d40_reg_val {
303 unsigned int reg;
304 unsigned int val;
305};
306
Rabin Vincent262d2912011-01-25 11:18:05 +0100307static struct device *chan2dev(struct d40_chan *d40c)
308{
309 return &d40c->chan.dev->device;
310}
311
Rabin Vincent724a8572011-01-25 11:18:08 +0100312static bool chan_is_physical(struct d40_chan *chan)
313{
314 return chan->log_num == D40_PHY_CHAN;
315}
316
317static bool chan_is_logical(struct d40_chan *chan)
318{
319 return !chan_is_physical(chan);
320}
321
Rabin Vincent8ca84682011-01-25 11:18:07 +0100322static void __iomem *chan_base(struct d40_chan *chan)
323{
324 return chan->base->virtbase + D40_DREG_PCBASE +
325 chan->phy_chan->num * D40_DREG_PCDELTA;
326}
327
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100328#define d40_err(dev, format, arg...) \
329 dev_err(dev, "[%s] " format, __func__, ## arg)
330
331#define chan_err(d40c, format, arg...) \
332 d40_err(chan2dev(d40c), format, ## arg)
333
Rabin Vincentb00f9382011-01-25 11:18:15 +0100334static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100335 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200336{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100337 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200338 u32 align;
339 void *base;
340
341 if (is_log)
342 align = sizeof(struct d40_log_lli);
343 else
344 align = sizeof(struct d40_phy_lli);
345
346 if (lli_len == 1) {
347 base = d40d->lli_pool.pre_alloc_lli;
348 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
349 d40d->lli_pool.base = NULL;
350 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100351 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200352
353 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
354 d40d->lli_pool.base = base;
355
356 if (d40d->lli_pool.base == NULL)
357 return -ENOMEM;
358 }
359
360 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100361 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100362 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100363
364 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200365 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100366 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100367 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100368
369 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
370 d40d->lli_phy.src,
371 d40d->lli_pool.size,
372 DMA_TO_DEVICE);
373
374 if (dma_mapping_error(d40c->base->dev,
375 d40d->lli_pool.dma_addr)) {
376 kfree(d40d->lli_pool.base);
377 d40d->lli_pool.base = NULL;
378 d40d->lli_pool.dma_addr = 0;
379 return -ENOMEM;
380 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200381 }
382
383 return 0;
384}
385
Rabin Vincentb00f9382011-01-25 11:18:15 +0100386static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200387{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100388 if (d40d->lli_pool.dma_addr)
389 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
390 d40d->lli_pool.size, DMA_TO_DEVICE);
391
Linus Walleij8d318a52010-03-30 15:33:42 +0200392 kfree(d40d->lli_pool.base);
393 d40d->lli_pool.base = NULL;
394 d40d->lli_pool.size = 0;
395 d40d->lli_log.src = NULL;
396 d40d->lli_log.dst = NULL;
397 d40d->lli_phy.src = NULL;
398 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200399}
400
Jonas Aaberg698e4732010-08-09 12:08:56 +0000401static int d40_lcla_alloc_one(struct d40_chan *d40c,
402 struct d40_desc *d40d)
403{
404 unsigned long flags;
405 int i;
406 int ret = -EINVAL;
407 int p;
408
409 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
410
411 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
412
413 /*
414 * Allocate both src and dst at the same time, therefore the half
415 * start on 1 since 0 can't be used since zero is used as end marker.
416 */
417 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
418 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
419 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
420 d40d->lcla_alloc++;
421 ret = i;
422 break;
423 }
424 }
425
426 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
427
428 return ret;
429}
430
431static int d40_lcla_free_all(struct d40_chan *d40c,
432 struct d40_desc *d40d)
433{
434 unsigned long flags;
435 int i;
436 int ret = -EINVAL;
437
Rabin Vincent724a8572011-01-25 11:18:08 +0100438 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000439 return 0;
440
441 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
442
443 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
444 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
445 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
446 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
447 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
448 d40d->lcla_alloc--;
449 if (d40d->lcla_alloc == 0) {
450 ret = 0;
451 break;
452 }
453 }
454 }
455
456 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
457
458 return ret;
459
460}
461
Linus Walleij8d318a52010-03-30 15:33:42 +0200462static void d40_desc_remove(struct d40_desc *d40d)
463{
464 list_del(&d40d->node);
465}
466
467static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
468{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000469 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200470
471 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000472 struct d40_desc *d;
473 struct d40_desc *_d;
474
Linus Walleij8d318a52010-03-30 15:33:42 +0200475 list_for_each_entry_safe(d, _d, &d40c->client, node)
476 if (async_tx_test_ack(&d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +0100477 d40_pool_lli_free(d40c, d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200478 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000479 desc = d;
480 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000481 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200482 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200483 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000484
485 if (!desc)
486 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
487
488 if (desc)
489 INIT_LIST_HEAD(&desc->node);
490
491 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200492}
493
494static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
495{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000496
Rabin Vincentb00f9382011-01-25 11:18:15 +0100497 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000498 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000499 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200500}
501
502static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
503{
504 list_add_tail(&desc->node, &d40c->active);
505}
506
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100507static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
508{
509 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
510 struct d40_phy_lli *lli_src = desc->lli_phy.src;
511 void __iomem *base = chan_base(chan);
512
513 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
514 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
515 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
516 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
517
518 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
519 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
520 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
521 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
522}
523
Jonas Aaberg698e4732010-08-09 12:08:56 +0000524static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
525{
526 int curr_lcla = -EINVAL, next_lcla;
527
Rabin Vincent724a8572011-01-25 11:18:08 +0100528 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100529 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000530 d40d->lli_current = d40d->lli_len;
531 } else {
532
533 if ((d40d->lli_len - d40d->lli_current) > 1)
534 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
535
536 d40_log_lli_lcpa_write(d40c->lcpa,
537 &d40d->lli_log.dst[d40d->lli_current],
538 &d40d->lli_log.src[d40d->lli_current],
539 curr_lcla);
540
541 d40d->lli_current++;
542 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
Rabin Vincent026cbc42011-01-25 11:18:14 +0100543 unsigned int lcla_offset = d40c->phy_chan->num * 1024 +
544 8 * curr_lcla * 2;
545 struct d40_lcla_pool *pool = &d40c->base->lcla_pool;
546 struct d40_log_lli *lcla = pool->base + lcla_offset;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000547
548 if (d40d->lli_current + 1 < d40d->lli_len)
549 next_lcla = d40_lcla_alloc_one(d40c, d40d);
550 else
551 next_lcla = -EINVAL;
552
Jonas Aaberg698e4732010-08-09 12:08:56 +0000553 d40_log_lli_lcla_write(lcla,
554 &d40d->lli_log.dst[d40d->lli_current],
555 &d40d->lli_log.src[d40d->lli_current],
556 next_lcla);
557
Rabin Vincent026cbc42011-01-25 11:18:14 +0100558 dma_sync_single_range_for_device(d40c->base->dev,
559 pool->dma_addr, lcla_offset,
560 2 * sizeof(struct d40_log_lli),
561 DMA_TO_DEVICE);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000562
563 curr_lcla = next_lcla;
564
565 if (curr_lcla == -EINVAL) {
566 d40d->lli_current++;
567 break;
568 }
569
570 }
571 }
572}
573
Linus Walleij8d318a52010-03-30 15:33:42 +0200574static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
575{
576 struct d40_desc *d;
577
578 if (list_empty(&d40c->active))
579 return NULL;
580
581 d = list_first_entry(&d40c->active,
582 struct d40_desc,
583 node);
584 return d;
585}
586
587static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
588{
589 list_add_tail(&desc->node, &d40c->queue);
590}
591
592static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
593{
594 struct d40_desc *d;
595
596 if (list_empty(&d40c->queue))
597 return NULL;
598
599 d = list_first_entry(&d40c->queue,
600 struct d40_desc,
601 node);
602 return d;
603}
604
Per Forlind49278e2010-12-20 18:31:38 +0100605static int d40_psize_2_burst_size(bool is_log, int psize)
606{
607 if (is_log) {
608 if (psize == STEDMA40_PSIZE_LOG_1)
609 return 1;
610 } else {
611 if (psize == STEDMA40_PSIZE_PHY_1)
612 return 1;
613 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200614
Per Forlind49278e2010-12-20 18:31:38 +0100615 return 2 << psize;
616}
617
618/*
619 * The dma only supports transmitting packages up to
620 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
621 * dma elements required to send the entire sg list
622 */
623static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
624{
625 int dmalen;
626 u32 max_w = max(data_width1, data_width2);
627 u32 min_w = min(data_width1, data_width2);
628 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
629
630 if (seg_max > STEDMA40_MAX_SEG_SIZE)
631 seg_max -= (1 << max_w);
632
633 if (!IS_ALIGNED(size, 1 << max_w))
634 return -EINVAL;
635
636 if (size <= seg_max)
637 dmalen = 1;
638 else {
639 dmalen = size / seg_max;
640 if (dmalen * seg_max < size)
641 dmalen++;
642 }
643 return dmalen;
644}
645
646static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
647 u32 data_width1, u32 data_width2)
648{
649 struct scatterlist *sg;
650 int i;
651 int len = 0;
652 int ret;
653
654 for_each_sg(sgl, sg, sg_len, i) {
655 ret = d40_size_2_dmalen(sg_dma_len(sg),
656 data_width1, data_width2);
657 if (ret < 0)
658 return ret;
659 len += ret;
660 }
661 return len;
662}
663
664/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200665
666static int d40_channel_execute_command(struct d40_chan *d40c,
667 enum d40_command command)
668{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000669 u32 status;
670 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200671 void __iomem *active_reg;
672 int ret = 0;
673 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000674 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200675
676 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
677
678 if (d40c->phy_chan->num % 2 == 0)
679 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
680 else
681 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
682
683 if (command == D40_DMA_SUSPEND_REQ) {
684 status = (readl(active_reg) &
685 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
686 D40_CHAN_POS(d40c->phy_chan->num);
687
688 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
689 goto done;
690 }
691
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000692 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
693 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
694 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200695
696 if (command == D40_DMA_SUSPEND_REQ) {
697
698 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
699 status = (readl(active_reg) &
700 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
701 D40_CHAN_POS(d40c->phy_chan->num);
702
703 cpu_relax();
704 /*
705 * Reduce the number of bus accesses while
706 * waiting for the DMA to suspend.
707 */
708 udelay(3);
709
710 if (status == D40_DMA_STOP ||
711 status == D40_DMA_SUSPENDED)
712 break;
713 }
714
715 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100716 chan_err(d40c,
717 "unable to suspend the chl %d (log: %d) status %x\n",
718 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200719 status);
720 dump_stack();
721 ret = -EBUSY;
722 }
723
724 }
725done:
726 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
727 return ret;
728}
729
730static void d40_term_all(struct d40_chan *d40c)
731{
732 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200733
734 /* Release active descriptors */
735 while ((d40d = d40_first_active_get(d40c))) {
736 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200737 d40_desc_free(d40c, d40d);
738 }
739
740 /* Release queued descriptors waiting for transfer */
741 while ((d40d = d40_first_queued(d40c))) {
742 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200743 d40_desc_free(d40c, d40d);
744 }
745
Linus Walleij8d318a52010-03-30 15:33:42 +0200746
747 d40c->pending_tx = 0;
748 d40c->busy = false;
749}
750
Rabin Vincent262d2912011-01-25 11:18:05 +0100751static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
752 u32 event, int reg)
753{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100754 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100755 int tries;
756
757 if (!enable) {
758 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
759 | ~D40_EVENTLINE_MASK(event), addr);
760 return;
761 }
762
763 /*
764 * The hardware sometimes doesn't register the enable when src and dst
765 * event lines are active on the same logical channel. Retry to ensure
766 * it does. Usually only one retry is sufficient.
767 */
768 tries = 100;
769 while (--tries) {
770 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
771 | ~D40_EVENTLINE_MASK(event), addr);
772
773 if (readl(addr) & D40_EVENTLINE_MASK(event))
774 break;
775 }
776
777 if (tries != 99)
778 dev_dbg(chan2dev(d40c),
779 "[%s] workaround enable S%cLNK (%d tries)\n",
780 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
781 100 - tries);
782
783 WARN_ON(!tries);
784}
785
Linus Walleij8d318a52010-03-30 15:33:42 +0200786static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
787{
Linus Walleij8d318a52010-03-30 15:33:42 +0200788 unsigned long flags;
789
Linus Walleij8d318a52010-03-30 15:33:42 +0200790 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
791
792 /* Enable event line connected to device (or memcpy) */
793 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
794 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
795 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
796
Rabin Vincent262d2912011-01-25 11:18:05 +0100797 __d40_config_set_event(d40c, do_enable, event,
798 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200799 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100800
Linus Walleij8d318a52010-03-30 15:33:42 +0200801 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
802 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
803
Rabin Vincent262d2912011-01-25 11:18:05 +0100804 __d40_config_set_event(d40c, do_enable, event,
805 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200806 }
807
808 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
809}
810
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200811static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200812{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100813 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000814 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200815
Rabin Vincent8ca84682011-01-25 11:18:07 +0100816 val = readl(chanbase + D40_CHAN_REG_SSLNK);
817 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200818
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200819 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200820}
821
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000822static u32 d40_get_prmo(struct d40_chan *d40c)
823{
824 static const unsigned int phy_map[] = {
825 [STEDMA40_PCHAN_BASIC_MODE]
826 = D40_DREG_PRMO_PCHAN_BASIC,
827 [STEDMA40_PCHAN_MODULO_MODE]
828 = D40_DREG_PRMO_PCHAN_MODULO,
829 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
830 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
831 };
832 static const unsigned int log_map[] = {
833 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
834 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
835 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
836 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
837 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
838 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
839 };
840
Rabin Vincent724a8572011-01-25 11:18:08 +0100841 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000842 return phy_map[d40c->dma_cfg.mode_opt];
843 else
844 return log_map[d40c->dma_cfg.mode_opt];
845}
846
Jonas Aabergb55912c2010-08-09 12:08:02 +0000847static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200848{
849 u32 addr_base;
850 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200851
852 /* Odd addresses are even addresses + 4 */
853 addr_base = (d40c->phy_chan->num % 2) * 4;
854 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +0100855 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +0200856 D40_CHAN_POS(d40c->phy_chan->num);
857 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
858
859 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000860 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200861
862 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
863
Rabin Vincent724a8572011-01-25 11:18:08 +0100864 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100865 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
866 & D40_SREG_ELEM_LOG_LIDX_MASK;
867 void __iomem *chanbase = chan_base(d40c);
868
Linus Walleij8d318a52010-03-30 15:33:42 +0200869 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100870 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
871 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200872
Jonas Aabergb55912c2010-08-09 12:08:02 +0000873 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100874 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
875 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200876 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200877}
878
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000879static u32 d40_residue(struct d40_chan *d40c)
880{
881 u32 num_elt;
882
Rabin Vincent724a8572011-01-25 11:18:08 +0100883 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000884 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
885 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100886 else {
887 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
888 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
889 >> D40_SREG_ELEM_PHY_ECNT_POS;
890 }
891
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000892 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
893}
894
895static bool d40_tx_is_linked(struct d40_chan *d40c)
896{
897 bool is_link;
898
Rabin Vincent724a8572011-01-25 11:18:08 +0100899 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000900 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
901 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100902 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
903 & D40_SREG_LNK_PHYS_LNK_MASK;
904
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000905 return is_link;
906}
907
908static int d40_pause(struct dma_chan *chan)
909{
910 struct d40_chan *d40c =
911 container_of(chan, struct d40_chan, chan);
912 int res = 0;
913 unsigned long flags;
914
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000915 if (!d40c->busy)
916 return 0;
917
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000918 spin_lock_irqsave(&d40c->lock, flags);
919
920 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
921 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +0100922 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000923 d40_config_set_event(d40c, false);
924 /* Resume the other logical channels if any */
925 if (d40_chan_has_events(d40c))
926 res = d40_channel_execute_command(d40c,
927 D40_DMA_RUN);
928 }
929 }
930
931 spin_unlock_irqrestore(&d40c->lock, flags);
932 return res;
933}
934
935static int d40_resume(struct dma_chan *chan)
936{
937 struct d40_chan *d40c =
938 container_of(chan, struct d40_chan, chan);
939 int res = 0;
940 unsigned long flags;
941
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000942 if (!d40c->busy)
943 return 0;
944
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000945 spin_lock_irqsave(&d40c->lock, flags);
946
947 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +0100948 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000949 res = d40_channel_execute_command(d40c,
950 D40_DMA_SUSPEND_REQ);
951 goto no_suspend;
952 }
953
954 /* If bytes left to transfer or linked tx resume job */
955 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
956
Rabin Vincent724a8572011-01-25 11:18:08 +0100957 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000958 d40_config_set_event(d40c, true);
959
960 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
961 }
962
963no_suspend:
964 spin_unlock_irqrestore(&d40c->lock, flags);
965 return res;
966}
967
Linus Walleij8d318a52010-03-30 15:33:42 +0200968static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
969{
970 struct d40_chan *d40c = container_of(tx->chan,
971 struct d40_chan,
972 chan);
973 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
974 unsigned long flags;
975
976 spin_lock_irqsave(&d40c->lock, flags);
977
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000978 d40c->chan.cookie++;
979
980 if (d40c->chan.cookie < 0)
981 d40c->chan.cookie = 1;
982
983 d40d->txd.cookie = d40c->chan.cookie;
984
Linus Walleij8d318a52010-03-30 15:33:42 +0200985 d40_desc_queue(d40c, d40d);
986
987 spin_unlock_irqrestore(&d40c->lock, flags);
988
989 return tx->cookie;
990}
991
992static int d40_start(struct d40_chan *d40c)
993{
Linus Walleijf4185592010-06-22 18:06:42 -0700994 if (d40c->base->rev == 0) {
995 int err;
996
Rabin Vincent724a8572011-01-25 11:18:08 +0100997 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -0700998 err = d40_channel_execute_command(d40c,
999 D40_DMA_SUSPEND_REQ);
1000 if (err)
1001 return err;
1002 }
1003 }
1004
Rabin Vincent724a8572011-01-25 11:18:08 +01001005 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001006 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001007
Jonas Aaberg0c322692010-06-20 21:25:46 +00001008 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001009}
1010
1011static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1012{
1013 struct d40_desc *d40d;
1014 int err;
1015
1016 /* Start queued jobs, if any */
1017 d40d = d40_first_queued(d40c);
1018
1019 if (d40d != NULL) {
1020 d40c->busy = true;
1021
1022 /* Remove from queue */
1023 d40_desc_remove(d40d);
1024
1025 /* Add to active queue */
1026 d40_desc_submit(d40c, d40d);
1027
Rabin Vincent7d83a852011-01-25 11:18:06 +01001028 /* Initiate DMA job */
1029 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001030
Rabin Vincent7d83a852011-01-25 11:18:06 +01001031 /* Start dma job */
1032 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001033
Rabin Vincent7d83a852011-01-25 11:18:06 +01001034 if (err)
1035 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001036 }
1037
1038 return d40d;
1039}
1040
1041/* called from interrupt context */
1042static void dma_tc_handle(struct d40_chan *d40c)
1043{
1044 struct d40_desc *d40d;
1045
Linus Walleij8d318a52010-03-30 15:33:42 +02001046 /* Get first active entry from list */
1047 d40d = d40_first_active_get(d40c);
1048
1049 if (d40d == NULL)
1050 return;
1051
Jonas Aaberg698e4732010-08-09 12:08:56 +00001052 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001053
Jonas Aaberg698e4732010-08-09 12:08:56 +00001054 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001055 d40_desc_load(d40c, d40d);
1056 /* Start dma job */
1057 (void) d40_start(d40c);
1058 return;
1059 }
1060
1061 if (d40_queue_start(d40c) == NULL)
1062 d40c->busy = false;
1063
1064 d40c->pending_tx++;
1065 tasklet_schedule(&d40c->tasklet);
1066
1067}
1068
1069static void dma_tasklet(unsigned long data)
1070{
1071 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001072 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001073 unsigned long flags;
1074 dma_async_tx_callback callback;
1075 void *callback_param;
1076
1077 spin_lock_irqsave(&d40c->lock, flags);
1078
1079 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001080 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001081
Jonas Aaberg767a9672010-08-09 12:08:34 +00001082 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001083 goto err;
1084
Jonas Aaberg767a9672010-08-09 12:08:34 +00001085 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001086
1087 /*
1088 * If terminating a channel pending_tx is set to zero.
1089 * This prevents any finished active jobs to return to the client.
1090 */
1091 if (d40c->pending_tx == 0) {
1092 spin_unlock_irqrestore(&d40c->lock, flags);
1093 return;
1094 }
1095
1096 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001097 callback = d40d->txd.callback;
1098 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001099
Jonas Aaberg767a9672010-08-09 12:08:34 +00001100 if (async_tx_test_ack(&d40d->txd)) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001101 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001102 d40_desc_remove(d40d);
1103 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001104 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001105 if (!d40d->is_in_client_list) {
1106 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001107 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001108 list_add_tail(&d40d->node, &d40c->client);
1109 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001110 }
1111 }
1112
1113 d40c->pending_tx--;
1114
1115 if (d40c->pending_tx)
1116 tasklet_schedule(&d40c->tasklet);
1117
1118 spin_unlock_irqrestore(&d40c->lock, flags);
1119
Jonas Aaberg767a9672010-08-09 12:08:34 +00001120 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001121 callback(callback_param);
1122
1123 return;
1124
1125 err:
1126 /* Rescue manouver if receiving double interrupts */
1127 if (d40c->pending_tx > 0)
1128 d40c->pending_tx--;
1129 spin_unlock_irqrestore(&d40c->lock, flags);
1130}
1131
1132static irqreturn_t d40_handle_interrupt(int irq, void *data)
1133{
1134 static const struct d40_interrupt_lookup il[] = {
1135 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1136 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1137 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1138 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1139 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1140 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1141 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1142 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1143 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1144 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1145 };
1146
1147 int i;
1148 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001149 u32 idx;
1150 u32 row;
1151 long chan = -1;
1152 struct d40_chan *d40c;
1153 unsigned long flags;
1154 struct d40_base *base = data;
1155
1156 spin_lock_irqsave(&base->interrupt_lock, flags);
1157
1158 /* Read interrupt status of both logical and physical channels */
1159 for (i = 0; i < ARRAY_SIZE(il); i++)
1160 regs[i] = readl(base->virtbase + il[i].src);
1161
1162 for (;;) {
1163
1164 chan = find_next_bit((unsigned long *)regs,
1165 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1166
1167 /* No more set bits found? */
1168 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1169 break;
1170
1171 row = chan / BITS_PER_LONG;
1172 idx = chan & (BITS_PER_LONG - 1);
1173
1174 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001175 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001176
1177 if (il[row].offset == D40_PHY_CHAN)
1178 d40c = base->lookup_phy_chans[idx];
1179 else
1180 d40c = base->lookup_log_chans[il[row].offset + idx];
1181 spin_lock(&d40c->lock);
1182
1183 if (!il[row].is_error)
1184 dma_tc_handle(d40c);
1185 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001186 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1187 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001188
1189 spin_unlock(&d40c->lock);
1190 }
1191
1192 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1193
1194 return IRQ_HANDLED;
1195}
1196
Linus Walleij8d318a52010-03-30 15:33:42 +02001197static int d40_validate_conf(struct d40_chan *d40c,
1198 struct stedma40_chan_cfg *conf)
1199{
1200 int res = 0;
1201 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1202 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001203 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001204
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001205 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001206 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001207 res = -EINVAL;
1208 }
1209
1210 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1211 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1212 d40c->runtime_addr == 0) {
1213
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001214 chan_err(d40c, "Invalid TX channel address (%d)\n",
1215 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001216 res = -EINVAL;
1217 }
1218
1219 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1220 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1221 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001222 chan_err(d40c, "Invalid RX channel address (%d)\n",
1223 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001224 res = -EINVAL;
1225 }
1226
1227 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001228 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001229 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001230 res = -EINVAL;
1231 }
1232
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001233 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001234 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001235 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001236 res = -EINVAL;
1237 }
1238
1239 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1240 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001241 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001242 res = -EINVAL;
1243 }
1244
1245 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1246 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001247 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001248 res = -EINVAL;
1249 }
1250
1251 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1252 /*
1253 * DMAC HW supports it. Will be added to this driver,
1254 * in case any dma client requires it.
1255 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001256 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001257 res = -EINVAL;
1258 }
1259
Per Forlind49278e2010-12-20 18:31:38 +01001260 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1261 (1 << conf->src_info.data_width) !=
1262 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1263 (1 << conf->dst_info.data_width)) {
1264 /*
1265 * The DMAC hardware only supports
1266 * src (burst x width) == dst (burst x width)
1267 */
1268
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001269 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001270 res = -EINVAL;
1271 }
1272
Linus Walleij8d318a52010-03-30 15:33:42 +02001273 return res;
1274}
1275
1276static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001277 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001278{
1279 unsigned long flags;
1280 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001281 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001282 /* Physical interrupts are masked per physical full channel */
1283 if (phy->allocated_src == D40_ALLOC_FREE &&
1284 phy->allocated_dst == D40_ALLOC_FREE) {
1285 phy->allocated_dst = D40_ALLOC_PHY;
1286 phy->allocated_src = D40_ALLOC_PHY;
1287 goto found;
1288 } else
1289 goto not_found;
1290 }
1291
1292 /* Logical channel */
1293 if (is_src) {
1294 if (phy->allocated_src == D40_ALLOC_PHY)
1295 goto not_found;
1296
1297 if (phy->allocated_src == D40_ALLOC_FREE)
1298 phy->allocated_src = D40_ALLOC_LOG_FREE;
1299
1300 if (!(phy->allocated_src & (1 << log_event_line))) {
1301 phy->allocated_src |= 1 << log_event_line;
1302 goto found;
1303 } else
1304 goto not_found;
1305 } else {
1306 if (phy->allocated_dst == D40_ALLOC_PHY)
1307 goto not_found;
1308
1309 if (phy->allocated_dst == D40_ALLOC_FREE)
1310 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1311
1312 if (!(phy->allocated_dst & (1 << log_event_line))) {
1313 phy->allocated_dst |= 1 << log_event_line;
1314 goto found;
1315 } else
1316 goto not_found;
1317 }
1318
1319not_found:
1320 spin_unlock_irqrestore(&phy->lock, flags);
1321 return false;
1322found:
1323 spin_unlock_irqrestore(&phy->lock, flags);
1324 return true;
1325}
1326
1327static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1328 int log_event_line)
1329{
1330 unsigned long flags;
1331 bool is_free = false;
1332
1333 spin_lock_irqsave(&phy->lock, flags);
1334 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001335 phy->allocated_dst = D40_ALLOC_FREE;
1336 phy->allocated_src = D40_ALLOC_FREE;
1337 is_free = true;
1338 goto out;
1339 }
1340
1341 /* Logical channel */
1342 if (is_src) {
1343 phy->allocated_src &= ~(1 << log_event_line);
1344 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1345 phy->allocated_src = D40_ALLOC_FREE;
1346 } else {
1347 phy->allocated_dst &= ~(1 << log_event_line);
1348 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1349 phy->allocated_dst = D40_ALLOC_FREE;
1350 }
1351
1352 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1353 D40_ALLOC_FREE);
1354
1355out:
1356 spin_unlock_irqrestore(&phy->lock, flags);
1357
1358 return is_free;
1359}
1360
1361static int d40_allocate_channel(struct d40_chan *d40c)
1362{
1363 int dev_type;
1364 int event_group;
1365 int event_line;
1366 struct d40_phy_res *phys;
1367 int i;
1368 int j;
1369 int log_num;
1370 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001371 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001372
1373 phys = d40c->base->phy_res;
1374
1375 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1376 dev_type = d40c->dma_cfg.src_dev_type;
1377 log_num = 2 * dev_type;
1378 is_src = true;
1379 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1380 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1381 /* dst event lines are used for logical memcpy */
1382 dev_type = d40c->dma_cfg.dst_dev_type;
1383 log_num = 2 * dev_type + 1;
1384 is_src = false;
1385 } else
1386 return -EINVAL;
1387
1388 event_group = D40_TYPE_TO_GROUP(dev_type);
1389 event_line = D40_TYPE_TO_EVENT(dev_type);
1390
1391 if (!is_log) {
1392 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1393 /* Find physical half channel */
1394 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1395
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001396 if (d40_alloc_mask_set(&phys[i], is_src,
1397 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001398 goto found_phy;
1399 }
1400 } else
1401 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1402 int phy_num = j + event_group * 2;
1403 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001404 if (d40_alloc_mask_set(&phys[i],
1405 is_src,
1406 0,
1407 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001408 goto found_phy;
1409 }
1410 }
1411 return -EINVAL;
1412found_phy:
1413 d40c->phy_chan = &phys[i];
1414 d40c->log_num = D40_PHY_CHAN;
1415 goto out;
1416 }
1417 if (dev_type == -1)
1418 return -EINVAL;
1419
1420 /* Find logical channel */
1421 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1422 int phy_num = j + event_group * 2;
1423 /*
1424 * Spread logical channels across all available physical rather
1425 * than pack every logical channel at the first available phy
1426 * channels.
1427 */
1428 if (is_src) {
1429 for (i = phy_num; i < phy_num + 2; i++) {
1430 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001431 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001432 goto found_log;
1433 }
1434 } else {
1435 for (i = phy_num + 1; i >= phy_num; i--) {
1436 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001437 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001438 goto found_log;
1439 }
1440 }
1441 }
1442 return -EINVAL;
1443
1444found_log:
1445 d40c->phy_chan = &phys[i];
1446 d40c->log_num = log_num;
1447out:
1448
1449 if (is_log)
1450 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1451 else
1452 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1453
1454 return 0;
1455
1456}
1457
Linus Walleij8d318a52010-03-30 15:33:42 +02001458static int d40_config_memcpy(struct d40_chan *d40c)
1459{
1460 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1461
1462 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1463 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1464 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1465 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1466 memcpy[d40c->chan.chan_id];
1467
1468 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1469 dma_has_cap(DMA_SLAVE, cap)) {
1470 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1471 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001472 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001473 return -EINVAL;
1474 }
1475
1476 return 0;
1477}
1478
1479
1480static int d40_free_dma(struct d40_chan *d40c)
1481{
1482
1483 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001484 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001485 struct d40_phy_res *phy = d40c->phy_chan;
1486 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001487 struct d40_desc *d;
1488 struct d40_desc *_d;
1489
Linus Walleij8d318a52010-03-30 15:33:42 +02001490
1491 /* Terminate all queued and active transfers */
1492 d40_term_all(d40c);
1493
Per Fridena8be8622010-06-20 21:24:59 +00001494 /* Release client owned descriptors */
1495 if (!list_empty(&d40c->client))
1496 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Rabin Vincentb00f9382011-01-25 11:18:15 +01001497 d40_pool_lli_free(d40c, d);
Per Fridena8be8622010-06-20 21:24:59 +00001498 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001499 d40_desc_free(d40c, d);
1500 }
1501
Linus Walleij8d318a52010-03-30 15:33:42 +02001502 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001503 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001504 return -EINVAL;
1505 }
1506
1507 if (phy->allocated_src == D40_ALLOC_FREE &&
1508 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001509 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001510 return -EINVAL;
1511 }
1512
Linus Walleij8d318a52010-03-30 15:33:42 +02001513 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1514 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1515 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001516 is_src = false;
1517 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1518 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001519 is_src = true;
1520 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001521 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001522 return -EINVAL;
1523 }
1524
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001525 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1526 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001527 chan_err(d40c, "suspend failed\n");
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001528 return res;
1529 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001530
Rabin Vincent724a8572011-01-25 11:18:08 +01001531 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001532 /* Release logical channel, deactivate the event line */
1533
1534 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1536
1537 /*
1538 * Check if there are more logical allocation
1539 * on this phy channel.
1540 */
1541 if (!d40_alloc_mask_free(phy, is_src, event)) {
1542 /* Resume the other logical channels if any */
1543 if (d40_chan_has_events(d40c)) {
1544 res = d40_channel_execute_command(d40c,
1545 D40_DMA_RUN);
1546 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001547 chan_err(d40c,
1548 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001549 return res;
1550 }
1551 }
1552 return 0;
1553 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001554 } else {
1555 (void) d40_alloc_mask_free(phy, is_src, 0);
1556 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001557
1558 /* Release physical channel */
1559 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1560 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001561 chan_err(d40c, "Failed to stop channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001562 return res;
1563 }
1564 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001565 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001566 d40c->base->lookup_phy_chans[phy->num] = NULL;
1567
1568 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001569}
1570
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001571static bool d40_is_paused(struct d40_chan *d40c)
1572{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001573 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001574 bool is_paused = false;
1575 unsigned long flags;
1576 void __iomem *active_reg;
1577 u32 status;
1578 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001579
1580 spin_lock_irqsave(&d40c->lock, flags);
1581
Rabin Vincent724a8572011-01-25 11:18:08 +01001582 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001583 if (d40c->phy_chan->num % 2 == 0)
1584 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1585 else
1586 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1587
1588 status = (readl(active_reg) &
1589 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1590 D40_CHAN_POS(d40c->phy_chan->num);
1591 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1592 is_paused = true;
1593
1594 goto _exit;
1595 }
1596
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001597 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001598 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001599 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001600 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001601 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001602 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001603 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001604 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001605 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001606 goto _exit;
1607 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001608
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001609 status = (status & D40_EVENTLINE_MASK(event)) >>
1610 D40_EVENTLINE_POS(event);
1611
1612 if (status != D40_DMA_RUN)
1613 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001614_exit:
1615 spin_unlock_irqrestore(&d40c->lock, flags);
1616 return is_paused;
1617
1618}
1619
1620
Linus Walleij8d318a52010-03-30 15:33:42 +02001621static u32 stedma40_residue(struct dma_chan *chan)
1622{
1623 struct d40_chan *d40c =
1624 container_of(chan, struct d40_chan, chan);
1625 u32 bytes_left;
1626 unsigned long flags;
1627
1628 spin_lock_irqsave(&d40c->lock, flags);
1629 bytes_left = d40_residue(d40c);
1630 spin_unlock_irqrestore(&d40c->lock, flags);
1631
1632 return bytes_left;
1633}
1634
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001635static int
1636d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1637 struct scatterlist *sg_src, struct scatterlist *sg_dst,
1638 unsigned int sg_len, enum dma_data_direction direction,
1639 dma_addr_t dev_addr)
1640{
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001641 dma_addr_t src_dev_addr = direction == DMA_FROM_DEVICE ? dev_addr : 0;
1642 dma_addr_t dst_dev_addr = direction == DMA_TO_DEVICE ? dev_addr : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001643 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1644 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1645 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001646 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001647
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001648 ret = d40_log_sg_to_lli(sg_src, sg_len,
1649 src_dev_addr,
1650 desc->lli_log.src,
1651 chan->log_def.lcsp1,
1652 src_info->data_width,
1653 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001654
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001655 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1656 dst_dev_addr,
1657 desc->lli_log.dst,
1658 chan->log_def.lcsp3,
1659 dst_info->data_width,
1660 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001661
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001662 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001663}
1664
1665static int
1666d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1667 struct scatterlist *sg_src, struct scatterlist *sg_dst,
1668 unsigned int sg_len, enum dma_data_direction direction,
1669 dma_addr_t dev_addr)
1670{
1671 dma_addr_t src_dev_addr = direction == DMA_FROM_DEVICE ? dev_addr : 0;
1672 dma_addr_t dst_dev_addr = direction == DMA_TO_DEVICE ? dev_addr : 0;
1673 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1674 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1675 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
1676 int ret;
1677
1678 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1679 desc->lli_phy.src,
1680 virt_to_phys(desc->lli_phy.src),
1681 chan->src_def_cfg,
1682 src_info->data_width,
1683 dst_info->data_width,
1684 src_info->psize);
1685
1686 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1687 desc->lli_phy.dst,
1688 virt_to_phys(desc->lli_phy.dst),
1689 chan->dst_def_cfg,
1690 dst_info->data_width,
1691 src_info->data_width,
1692 dst_info->psize);
1693
1694 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1695 desc->lli_pool.size, DMA_TO_DEVICE);
1696
1697 return ret < 0 ? ret : 0;
1698}
1699
1700
Rabin Vincent5f811582011-01-25 11:18:18 +01001701static struct d40_desc *
1702d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1703 unsigned int sg_len, unsigned long dma_flags)
1704{
1705 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1706 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001707 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01001708
1709 desc = d40_desc_get(chan);
1710 if (!desc)
1711 return NULL;
1712
1713 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
1714 cfg->dst_info.data_width);
1715 if (desc->lli_len < 0) {
1716 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01001717 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01001718 }
1719
Rabin Vincentdbd88782011-01-25 11:18:19 +01001720 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
1721 if (ret < 0) {
1722 chan_err(chan, "Could not allocate lli\n");
1723 goto err;
1724 }
1725
1726
Rabin Vincent5f811582011-01-25 11:18:18 +01001727 desc->lli_current = 0;
1728 desc->txd.flags = dma_flags;
1729 desc->txd.tx_submit = d40_tx_submit;
1730
1731 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
1732
1733 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01001734
1735err:
1736 d40_desc_free(chan, desc);
1737 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01001738}
1739
Rabin Vincentcade1d32011-01-25 11:18:23 +01001740static dma_addr_t
1741d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02001742{
Rabin Vincentcade1d32011-01-25 11:18:23 +01001743 struct stedma40_platform_data *plat = chan->base->plat_data;
1744 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1745 dma_addr_t addr;
Linus Walleij8d318a52010-03-30 15:33:42 +02001746
Rabin Vincentcade1d32011-01-25 11:18:23 +01001747 if (chan->runtime_addr)
1748 return chan->runtime_addr;
1749
1750 if (direction == DMA_FROM_DEVICE)
1751 addr = plat->dev_rx[cfg->src_dev_type];
1752 else if (direction == DMA_TO_DEVICE)
1753 addr = plat->dev_tx[cfg->dst_dev_type];
1754
1755 return addr;
1756}
1757
1758static struct dma_async_tx_descriptor *
1759d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
1760 struct scatterlist *sg_dst, unsigned int sg_len,
1761 enum dma_data_direction direction, unsigned long dma_flags)
1762{
1763 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
1764 dma_addr_t dev_addr = 0;
1765 struct d40_desc *desc;
1766 unsigned long flags;
1767 int ret;
1768
1769 if (!chan->phy_chan) {
1770 chan_err(chan, "Cannot prepare unallocated channel\n");
1771 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001772 }
1773
Rabin Vincentcade1d32011-01-25 11:18:23 +01001774 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001775
Rabin Vincentcade1d32011-01-25 11:18:23 +01001776 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
1777 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001778 goto err;
1779
Rabin Vincentcade1d32011-01-25 11:18:23 +01001780 if (direction != DMA_NONE)
1781 dev_addr = d40_get_dev_addr(chan, direction);
1782
1783 if (chan_is_logical(chan))
1784 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
1785 sg_len, direction, dev_addr);
1786 else
1787 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
1788 sg_len, direction, dev_addr);
1789
1790 if (ret) {
1791 chan_err(chan, "Failed to prepare %s sg job: %d\n",
1792 chan_is_logical(chan) ? "log" : "phy", ret);
1793 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001794 }
1795
Rabin Vincentcade1d32011-01-25 11:18:23 +01001796 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001797
Rabin Vincentcade1d32011-01-25 11:18:23 +01001798 return &desc->txd;
1799
Linus Walleij8d318a52010-03-30 15:33:42 +02001800err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01001801 if (desc)
1802 d40_desc_free(chan, desc);
1803 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001804 return NULL;
1805}
Linus Walleij8d318a52010-03-30 15:33:42 +02001806
1807bool stedma40_filter(struct dma_chan *chan, void *data)
1808{
1809 struct stedma40_chan_cfg *info = data;
1810 struct d40_chan *d40c =
1811 container_of(chan, struct d40_chan, chan);
1812 int err;
1813
1814 if (data) {
1815 err = d40_validate_conf(d40c, info);
1816 if (!err)
1817 d40c->dma_cfg = *info;
1818 } else
1819 err = d40_config_memcpy(d40c);
1820
Rabin Vincentce2ca122010-10-12 13:00:49 +00001821 if (!err)
1822 d40c->configured = true;
1823
Linus Walleij8d318a52010-03-30 15:33:42 +02001824 return err == 0;
1825}
1826EXPORT_SYMBOL(stedma40_filter);
1827
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001828static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
1829{
1830 bool realtime = d40c->dma_cfg.realtime;
1831 bool highprio = d40c->dma_cfg.high_priority;
1832 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
1833 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
1834 u32 event = D40_TYPE_TO_EVENT(dev_type);
1835 u32 group = D40_TYPE_TO_GROUP(dev_type);
1836 u32 bit = 1 << event;
1837
1838 /* Destination event lines are stored in the upper halfword */
1839 if (!src)
1840 bit <<= 16;
1841
1842 writel(bit, d40c->base->virtbase + prioreg + group * 4);
1843 writel(bit, d40c->base->virtbase + rtreg + group * 4);
1844}
1845
1846static void d40_set_prio_realtime(struct d40_chan *d40c)
1847{
1848 if (d40c->base->rev < 3)
1849 return;
1850
1851 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1852 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1853 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
1854
1855 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
1856 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
1857 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
1858}
1859
Linus Walleij8d318a52010-03-30 15:33:42 +02001860/* DMA ENGINE functions */
1861static int d40_alloc_chan_resources(struct dma_chan *chan)
1862{
1863 int err;
1864 unsigned long flags;
1865 struct d40_chan *d40c =
1866 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001867 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001868 spin_lock_irqsave(&d40c->lock, flags);
1869
1870 d40c->completed = chan->cookie = 1;
1871
Rabin Vincentce2ca122010-10-12 13:00:49 +00001872 /* If no dma configuration is set use default configuration (memcpy) */
1873 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001874 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001875 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001876 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001877 goto fail;
1878 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001879 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001880 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001881
1882 err = d40_allocate_channel(d40c);
1883 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001884 chan_err(d40c, "Failed to allocate channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001885 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001886 }
1887
Linus Walleijef1872e2010-06-20 21:24:52 +00001888 /* Fill in basic CFG register values */
1889 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01001890 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00001891
Rabin Vincentac2c0a32011-01-25 11:18:11 +01001892 d40_set_prio_realtime(d40c);
1893
Rabin Vincent724a8572011-01-25 11:18:08 +01001894 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00001895 d40_log_cfg(&d40c->dma_cfg,
1896 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1897
1898 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1899 d40c->lcpa = d40c->base->lcpa_base +
1900 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1901 else
1902 d40c->lcpa = d40c->base->lcpa_base +
1903 d40c->dma_cfg.dst_dev_type *
1904 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1905 }
1906
1907 /*
1908 * Only write channel configuration to the DMA if the physical
1909 * resource is free. In case of multiple logical channels
1910 * on the same physical resource, only the first write is necessary.
1911 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001912 if (is_free_phy)
1913 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001914fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001915 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001916 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001917}
1918
1919static void d40_free_chan_resources(struct dma_chan *chan)
1920{
1921 struct d40_chan *d40c =
1922 container_of(chan, struct d40_chan, chan);
1923 int err;
1924 unsigned long flags;
1925
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001926 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001927 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001928 return;
1929 }
1930
1931
Linus Walleij8d318a52010-03-30 15:33:42 +02001932 spin_lock_irqsave(&d40c->lock, flags);
1933
1934 err = d40_free_dma(d40c);
1935
1936 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001937 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001938 spin_unlock_irqrestore(&d40c->lock, flags);
1939}
1940
1941static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1942 dma_addr_t dst,
1943 dma_addr_t src,
1944 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001945 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001946{
Rabin Vincent95944c62011-01-25 11:18:17 +01001947 struct scatterlist dst_sg;
1948 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02001949
Rabin Vincent95944c62011-01-25 11:18:17 +01001950 sg_init_table(&dst_sg, 1);
1951 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001952
Rabin Vincent95944c62011-01-25 11:18:17 +01001953 sg_dma_address(&dst_sg) = dst;
1954 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02001955
Rabin Vincent95944c62011-01-25 11:18:17 +01001956 sg_dma_len(&dst_sg) = size;
1957 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001958
Rabin Vincentcade1d32011-01-25 11:18:23 +01001959 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001960}
1961
Ira Snyder0d688662010-09-30 11:46:47 +00001962static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01001963d40_prep_memcpy_sg(struct dma_chan *chan,
1964 struct scatterlist *dst_sg, unsigned int dst_nents,
1965 struct scatterlist *src_sg, unsigned int src_nents,
1966 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00001967{
1968 if (dst_nents != src_nents)
1969 return NULL;
1970
Rabin Vincentcade1d32011-01-25 11:18:23 +01001971 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01001972}
1973
Linus Walleij8d318a52010-03-30 15:33:42 +02001974static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
1975 struct scatterlist *sgl,
1976 unsigned int sg_len,
1977 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001978 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001979{
Rabin Vincent00ac0342011-01-25 11:18:20 +01001980 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
1981 return NULL;
1982
Rabin Vincentcade1d32011-01-25 11:18:23 +01001983 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001984}
1985
1986static enum dma_status d40_tx_status(struct dma_chan *chan,
1987 dma_cookie_t cookie,
1988 struct dma_tx_state *txstate)
1989{
1990 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1991 dma_cookie_t last_used;
1992 dma_cookie_t last_complete;
1993 int ret;
1994
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001995 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001996 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001997 return -EINVAL;
1998 }
1999
Linus Walleij8d318a52010-03-30 15:33:42 +02002000 last_complete = d40c->completed;
2001 last_used = chan->cookie;
2002
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002003 if (d40_is_paused(d40c))
2004 ret = DMA_PAUSED;
2005 else
2006 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002007
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002008 dma_set_tx_state(txstate, last_complete, last_used,
2009 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002010
2011 return ret;
2012}
2013
2014static void d40_issue_pending(struct dma_chan *chan)
2015{
2016 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2017 unsigned long flags;
2018
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002019 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002020 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002021 return;
2022 }
2023
Linus Walleij8d318a52010-03-30 15:33:42 +02002024 spin_lock_irqsave(&d40c->lock, flags);
2025
2026 /* Busy means that pending jobs are already being processed */
2027 if (!d40c->busy)
2028 (void) d40_queue_start(d40c);
2029
2030 spin_unlock_irqrestore(&d40c->lock, flags);
2031}
2032
Linus Walleij95e14002010-08-04 13:37:45 +02002033/* Runtime reconfiguration extension */
2034static void d40_set_runtime_config(struct dma_chan *chan,
2035 struct dma_slave_config *config)
2036{
2037 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2038 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2039 enum dma_slave_buswidth config_addr_width;
2040 dma_addr_t config_addr;
2041 u32 config_maxburst;
2042 enum stedma40_periph_data_width addr_width;
2043 int psize;
2044
2045 if (config->direction == DMA_FROM_DEVICE) {
2046 dma_addr_t dev_addr_rx =
2047 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2048
2049 config_addr = config->src_addr;
2050 if (dev_addr_rx)
2051 dev_dbg(d40c->base->dev,
2052 "channel has a pre-wired RX address %08x "
2053 "overriding with %08x\n",
2054 dev_addr_rx, config_addr);
2055 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2056 dev_dbg(d40c->base->dev,
2057 "channel was not configured for peripheral "
2058 "to memory transfer (%d) overriding\n",
2059 cfg->dir);
2060 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2061
2062 config_addr_width = config->src_addr_width;
2063 config_maxburst = config->src_maxburst;
2064
2065 } else if (config->direction == DMA_TO_DEVICE) {
2066 dma_addr_t dev_addr_tx =
2067 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2068
2069 config_addr = config->dst_addr;
2070 if (dev_addr_tx)
2071 dev_dbg(d40c->base->dev,
2072 "channel has a pre-wired TX address %08x "
2073 "overriding with %08x\n",
2074 dev_addr_tx, config_addr);
2075 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2076 dev_dbg(d40c->base->dev,
2077 "channel was not configured for memory "
2078 "to peripheral transfer (%d) overriding\n",
2079 cfg->dir);
2080 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2081
2082 config_addr_width = config->dst_addr_width;
2083 config_maxburst = config->dst_maxburst;
2084
2085 } else {
2086 dev_err(d40c->base->dev,
2087 "unrecognized channel direction %d\n",
2088 config->direction);
2089 return;
2090 }
2091
2092 switch (config_addr_width) {
2093 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2094 addr_width = STEDMA40_BYTE_WIDTH;
2095 break;
2096 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2097 addr_width = STEDMA40_HALFWORD_WIDTH;
2098 break;
2099 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2100 addr_width = STEDMA40_WORD_WIDTH;
2101 break;
2102 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2103 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2104 break;
2105 default:
2106 dev_err(d40c->base->dev,
2107 "illegal peripheral address width "
2108 "requested (%d)\n",
2109 config->src_addr_width);
2110 return;
2111 }
2112
Rabin Vincent724a8572011-01-25 11:18:08 +01002113 if (chan_is_logical(d40c)) {
Per Forlina59670a2010-10-06 09:05:27 +00002114 if (config_maxburst >= 16)
2115 psize = STEDMA40_PSIZE_LOG_16;
2116 else if (config_maxburst >= 8)
2117 psize = STEDMA40_PSIZE_LOG_8;
2118 else if (config_maxburst >= 4)
2119 psize = STEDMA40_PSIZE_LOG_4;
2120 else
2121 psize = STEDMA40_PSIZE_LOG_1;
2122 } else {
2123 if (config_maxburst >= 16)
2124 psize = STEDMA40_PSIZE_PHY_16;
2125 else if (config_maxburst >= 8)
2126 psize = STEDMA40_PSIZE_PHY_8;
2127 else if (config_maxburst >= 4)
2128 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002129 else if (config_maxburst >= 2)
2130 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002131 else
2132 psize = STEDMA40_PSIZE_PHY_1;
2133 }
Linus Walleij95e14002010-08-04 13:37:45 +02002134
2135 /* Set up all the endpoint configs */
2136 cfg->src_info.data_width = addr_width;
2137 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002138 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002139 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2140 cfg->dst_info.data_width = addr_width;
2141 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002142 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002143 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2144
Per Forlina59670a2010-10-06 09:05:27 +00002145 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002146 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002147 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2148 else
2149 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2150 &d40c->dst_def_cfg, false);
2151
Linus Walleij95e14002010-08-04 13:37:45 +02002152 /* These settings will take precedence later */
2153 d40c->runtime_addr = config_addr;
2154 d40c->runtime_direction = config->direction;
2155 dev_dbg(d40c->base->dev,
2156 "configured channel %s for %s, data width %d, "
2157 "maxburst %d bytes, LE, no flow control\n",
2158 dma_chan_name(chan),
2159 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2160 config_addr_width,
2161 config_maxburst);
2162}
2163
Linus Walleij05827632010-05-17 16:30:42 -07002164static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2165 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002166{
2167 unsigned long flags;
2168 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2169
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002170 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002171 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002172 return -EINVAL;
2173 }
2174
Linus Walleij8d318a52010-03-30 15:33:42 +02002175 switch (cmd) {
2176 case DMA_TERMINATE_ALL:
2177 spin_lock_irqsave(&d40c->lock, flags);
2178 d40_term_all(d40c);
2179 spin_unlock_irqrestore(&d40c->lock, flags);
2180 return 0;
2181 case DMA_PAUSE:
2182 return d40_pause(chan);
2183 case DMA_RESUME:
2184 return d40_resume(chan);
Linus Walleij95e14002010-08-04 13:37:45 +02002185 case DMA_SLAVE_CONFIG:
2186 d40_set_runtime_config(chan,
2187 (struct dma_slave_config *) arg);
2188 return 0;
2189 default:
2190 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002191 }
2192
2193 /* Other commands are unimplemented */
2194 return -ENXIO;
2195}
2196
2197/* Initialization functions */
2198
2199static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2200 struct d40_chan *chans, int offset,
2201 int num_chans)
2202{
2203 int i = 0;
2204 struct d40_chan *d40c;
2205
2206 INIT_LIST_HEAD(&dma->channels);
2207
2208 for (i = offset; i < offset + num_chans; i++) {
2209 d40c = &chans[i];
2210 d40c->base = base;
2211 d40c->chan.device = dma;
2212
Linus Walleij8d318a52010-03-30 15:33:42 +02002213 spin_lock_init(&d40c->lock);
2214
2215 d40c->log_num = D40_PHY_CHAN;
2216
Linus Walleij8d318a52010-03-30 15:33:42 +02002217 INIT_LIST_HEAD(&d40c->active);
2218 INIT_LIST_HEAD(&d40c->queue);
2219 INIT_LIST_HEAD(&d40c->client);
2220
Linus Walleij8d318a52010-03-30 15:33:42 +02002221 tasklet_init(&d40c->tasklet, dma_tasklet,
2222 (unsigned long) d40c);
2223
2224 list_add_tail(&d40c->chan.device_node,
2225 &dma->channels);
2226 }
2227}
2228
2229static int __init d40_dmaengine_init(struct d40_base *base,
2230 int num_reserved_chans)
2231{
2232 int err ;
2233
2234 d40_chan_init(base, &base->dma_slave, base->log_chans,
2235 0, base->num_log_chans);
2236
2237 dma_cap_zero(base->dma_slave.cap_mask);
2238 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2239
2240 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2241 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2242 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002243 base->dma_slave.device_prep_dma_sg = d40_prep_memcpy_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002244 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2245 base->dma_slave.device_tx_status = d40_tx_status;
2246 base->dma_slave.device_issue_pending = d40_issue_pending;
2247 base->dma_slave.device_control = d40_control;
2248 base->dma_slave.dev = base->dev;
2249
2250 err = dma_async_device_register(&base->dma_slave);
2251
2252 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002253 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002254 goto failure1;
2255 }
2256
2257 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2258 base->num_log_chans, base->plat_data->memcpy_len);
2259
2260 dma_cap_zero(base->dma_memcpy.cap_mask);
2261 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002262 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002263
2264 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2265 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2266 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002267 base->dma_slave.device_prep_dma_sg = d40_prep_memcpy_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002268 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2269 base->dma_memcpy.device_tx_status = d40_tx_status;
2270 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2271 base->dma_memcpy.device_control = d40_control;
2272 base->dma_memcpy.dev = base->dev;
2273 /*
2274 * This controller can only access address at even
2275 * 32bit boundaries, i.e. 2^2
2276 */
2277 base->dma_memcpy.copy_align = 2;
2278
2279 err = dma_async_device_register(&base->dma_memcpy);
2280
2281 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002282 d40_err(base->dev,
2283 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002284 goto failure2;
2285 }
2286
2287 d40_chan_init(base, &base->dma_both, base->phy_chans,
2288 0, num_reserved_chans);
2289
2290 dma_cap_zero(base->dma_both.cap_mask);
2291 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2292 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002293 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002294
2295 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2296 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2297 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002298 base->dma_slave.device_prep_dma_sg = d40_prep_memcpy_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002299 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2300 base->dma_both.device_tx_status = d40_tx_status;
2301 base->dma_both.device_issue_pending = d40_issue_pending;
2302 base->dma_both.device_control = d40_control;
2303 base->dma_both.dev = base->dev;
2304 base->dma_both.copy_align = 2;
2305 err = dma_async_device_register(&base->dma_both);
2306
2307 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002308 d40_err(base->dev,
2309 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002310 goto failure3;
2311 }
2312 return 0;
2313failure3:
2314 dma_async_device_unregister(&base->dma_memcpy);
2315failure2:
2316 dma_async_device_unregister(&base->dma_slave);
2317failure1:
2318 return err;
2319}
2320
2321/* Initialization functions. */
2322
2323static int __init d40_phy_res_init(struct d40_base *base)
2324{
2325 int i;
2326 int num_phy_chans_avail = 0;
2327 u32 val[2];
2328 int odd_even_bit = -2;
2329
2330 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2331 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2332
2333 for (i = 0; i < base->num_phy_chans; i++) {
2334 base->phy_res[i].num = i;
2335 odd_even_bit += 2 * ((i % 2) == 0);
2336 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2337 /* Mark security only channels as occupied */
2338 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2339 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2340 } else {
2341 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2342 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2343 num_phy_chans_avail++;
2344 }
2345 spin_lock_init(&base->phy_res[i].lock);
2346 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002347
2348 /* Mark disabled channels as occupied */
2349 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002350 int chan = base->plat_data->disabled_channels[i];
2351
2352 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2353 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2354 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002355 }
2356
Linus Walleij8d318a52010-03-30 15:33:42 +02002357 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2358 num_phy_chans_avail, base->num_phy_chans);
2359
2360 /* Verify settings extended vs standard */
2361 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2362
2363 for (i = 0; i < base->num_phy_chans; i++) {
2364
2365 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2366 (val[0] & 0x3) != 1)
2367 dev_info(base->dev,
2368 "[%s] INFO: channel %d is misconfigured (%d)\n",
2369 __func__, i, val[0] & 0x3);
2370
2371 val[0] = val[0] >> 2;
2372 }
2373
2374 return num_phy_chans_avail;
2375}
2376
2377static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2378{
2379 static const struct d40_reg_val dma_id_regs[] = {
2380 /* Peripheral Id */
2381 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2382 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2383 /*
2384 * D40_DREG_PERIPHID2 Depends on HW revision:
Rabin Vincent4d594902011-01-25 11:18:10 +01002385 * DB8500ed has 0x0008,
Linus Walleij8d318a52010-03-30 15:33:42 +02002386 * ? has 0x0018,
Rabin Vincent4d594902011-01-25 11:18:10 +01002387 * DB8500v1 has 0x0028
2388 * DB8500v2 has 0x0038
Linus Walleij8d318a52010-03-30 15:33:42 +02002389 */
2390 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2391
2392 /* PCell Id */
2393 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2394 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2395 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2396 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2397 };
2398 struct stedma40_platform_data *plat_data;
2399 struct clk *clk = NULL;
2400 void __iomem *virtbase = NULL;
2401 struct resource *res = NULL;
2402 struct d40_base *base = NULL;
2403 int num_log_chans = 0;
2404 int num_phy_chans;
2405 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002406 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002407 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002408
2409 clk = clk_get(&pdev->dev, NULL);
2410
2411 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002412 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002413 goto failure;
2414 }
2415
2416 clk_enable(clk);
2417
2418 /* Get IO for DMAC base address */
2419 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2420 if (!res)
2421 goto failure;
2422
2423 if (request_mem_region(res->start, resource_size(res),
2424 D40_NAME " I/O base") == NULL)
2425 goto failure;
2426
2427 virtbase = ioremap(res->start, resource_size(res));
2428 if (!virtbase)
2429 goto failure;
2430
2431 /* HW version check */
2432 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2433 if (dma_id_regs[i].val !=
2434 readl(virtbase + dma_id_regs[i].reg)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002435 d40_err(&pdev->dev,
2436 "Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002437 dma_id_regs[i].val,
2438 dma_id_regs[i].reg,
2439 readl(virtbase + dma_id_regs[i].reg));
2440 goto failure;
2441 }
2442 }
2443
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002444 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002445 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002446
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002447 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2448 D40_HW_DESIGNER) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002449 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2450 val & D40_DREG_PERIPHID2_DESIGNER_MASK,
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002451 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002452 goto failure;
2453 }
2454
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002455 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2456 D40_DREG_PERIPHID2_REV_POS;
2457
Linus Walleij8d318a52010-03-30 15:33:42 +02002458 /* The number of physical channels on this HW */
2459 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2460
2461 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002462 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002463
2464 plat_data = pdev->dev.platform_data;
2465
2466 /* Count the number of logical channels in use */
2467 for (i = 0; i < plat_data->dev_len; i++)
2468 if (plat_data->dev_rx[i] != 0)
2469 num_log_chans++;
2470
2471 for (i = 0; i < plat_data->dev_len; i++)
2472 if (plat_data->dev_tx[i] != 0)
2473 num_log_chans++;
2474
2475 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2476 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2477 sizeof(struct d40_chan), GFP_KERNEL);
2478
2479 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002480 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002481 goto failure;
2482 }
2483
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002484 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002485 base->clk = clk;
2486 base->num_phy_chans = num_phy_chans;
2487 base->num_log_chans = num_log_chans;
2488 base->phy_start = res->start;
2489 base->phy_size = resource_size(res);
2490 base->virtbase = virtbase;
2491 base->plat_data = plat_data;
2492 base->dev = &pdev->dev;
2493 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2494 base->log_chans = &base->phy_chans[num_phy_chans];
2495
2496 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2497 GFP_KERNEL);
2498 if (!base->phy_res)
2499 goto failure;
2500
2501 base->lookup_phy_chans = kzalloc(num_phy_chans *
2502 sizeof(struct d40_chan *),
2503 GFP_KERNEL);
2504 if (!base->lookup_phy_chans)
2505 goto failure;
2506
2507 if (num_log_chans + plat_data->memcpy_len) {
2508 /*
2509 * The max number of logical channels are event lines for all
2510 * src devices and dst devices
2511 */
2512 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2513 sizeof(struct d40_chan *),
2514 GFP_KERNEL);
2515 if (!base->lookup_log_chans)
2516 goto failure;
2517 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002518
2519 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2520 sizeof(struct d40_desc *) *
2521 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002522 GFP_KERNEL);
2523 if (!base->lcla_pool.alloc_map)
2524 goto failure;
2525
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002526 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2527 0, SLAB_HWCACHE_ALIGN,
2528 NULL);
2529 if (base->desc_slab == NULL)
2530 goto failure;
2531
Linus Walleij8d318a52010-03-30 15:33:42 +02002532 return base;
2533
2534failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002535 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002536 clk_disable(clk);
2537 clk_put(clk);
2538 }
2539 if (virtbase)
2540 iounmap(virtbase);
2541 if (res)
2542 release_mem_region(res->start,
2543 resource_size(res));
2544 if (virtbase)
2545 iounmap(virtbase);
2546
2547 if (base) {
2548 kfree(base->lcla_pool.alloc_map);
2549 kfree(base->lookup_log_chans);
2550 kfree(base->lookup_phy_chans);
2551 kfree(base->phy_res);
2552 kfree(base);
2553 }
2554
2555 return NULL;
2556}
2557
2558static void __init d40_hw_init(struct d40_base *base)
2559{
2560
2561 static const struct d40_reg_val dma_init_reg[] = {
2562 /* Clock every part of the DMA block from start */
2563 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2564
2565 /* Interrupts on all logical channels */
2566 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2567 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2568 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2569 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2570 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2571 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2572 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2573 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2574 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2575 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2576 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2577 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2578 };
2579 int i;
2580 u32 prmseo[2] = {0, 0};
2581 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2582 u32 pcmis = 0;
2583 u32 pcicr = 0;
2584
2585 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2586 writel(dma_init_reg[i].val,
2587 base->virtbase + dma_init_reg[i].reg);
2588
2589 /* Configure all our dma channels to default settings */
2590 for (i = 0; i < base->num_phy_chans; i++) {
2591
2592 activeo[i % 2] = activeo[i % 2] << 2;
2593
2594 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2595 == D40_ALLOC_PHY) {
2596 activeo[i % 2] |= 3;
2597 continue;
2598 }
2599
2600 /* Enable interrupt # */
2601 pcmis = (pcmis << 1) | 1;
2602
2603 /* Clear interrupt # */
2604 pcicr = (pcicr << 1) | 1;
2605
2606 /* Set channel to physical mode */
2607 prmseo[i % 2] = prmseo[i % 2] << 2;
2608 prmseo[i % 2] |= 1;
2609
2610 }
2611
2612 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2613 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2614 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2615 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2616
2617 /* Write which interrupt to enable */
2618 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2619
2620 /* Write which interrupt to clear */
2621 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2622
2623}
2624
Linus Walleij508849a2010-06-20 21:26:07 +00002625static int __init d40_lcla_allocate(struct d40_base *base)
2626{
Rabin Vincent026cbc42011-01-25 11:18:14 +01002627 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00002628 unsigned long *page_list;
2629 int i, j;
2630 int ret = 0;
2631
2632 /*
2633 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2634 * To full fill this hardware requirement without wasting 256 kb
2635 * we allocate pages until we get an aligned one.
2636 */
2637 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2638 GFP_KERNEL);
2639
2640 if (!page_list) {
2641 ret = -ENOMEM;
2642 goto failure;
2643 }
2644
2645 /* Calculating how many pages that are required */
2646 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2647
2648 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2649 page_list[i] = __get_free_pages(GFP_KERNEL,
2650 base->lcla_pool.pages);
2651 if (!page_list[i]) {
2652
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002653 d40_err(base->dev, "Failed to allocate %d pages.\n",
2654 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00002655
2656 for (j = 0; j < i; j++)
2657 free_pages(page_list[j], base->lcla_pool.pages);
2658 goto failure;
2659 }
2660
2661 if ((virt_to_phys((void *)page_list[i]) &
2662 (LCLA_ALIGNMENT - 1)) == 0)
2663 break;
2664 }
2665
2666 for (j = 0; j < i; j++)
2667 free_pages(page_list[j], base->lcla_pool.pages);
2668
2669 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2670 base->lcla_pool.base = (void *)page_list[i];
2671 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002672 /*
2673 * After many attempts and no succees with finding the correct
2674 * alignment, try with allocating a big buffer.
2675 */
Linus Walleij508849a2010-06-20 21:26:07 +00002676 dev_warn(base->dev,
2677 "[%s] Failed to get %d pages @ 18 bit align.\n",
2678 __func__, base->lcla_pool.pages);
2679 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2680 base->num_phy_chans +
2681 LCLA_ALIGNMENT,
2682 GFP_KERNEL);
2683 if (!base->lcla_pool.base_unaligned) {
2684 ret = -ENOMEM;
2685 goto failure;
2686 }
2687
2688 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2689 LCLA_ALIGNMENT);
2690 }
2691
Rabin Vincent026cbc42011-01-25 11:18:14 +01002692 pool->dma_addr = dma_map_single(base->dev, pool->base,
2693 SZ_1K * base->num_phy_chans,
2694 DMA_TO_DEVICE);
2695 if (dma_mapping_error(base->dev, pool->dma_addr)) {
2696 pool->dma_addr = 0;
2697 ret = -ENOMEM;
2698 goto failure;
2699 }
2700
Linus Walleij508849a2010-06-20 21:26:07 +00002701 writel(virt_to_phys(base->lcla_pool.base),
2702 base->virtbase + D40_DREG_LCLA);
2703failure:
2704 kfree(page_list);
2705 return ret;
2706}
2707
Linus Walleij8d318a52010-03-30 15:33:42 +02002708static int __init d40_probe(struct platform_device *pdev)
2709{
2710 int err;
2711 int ret = -ENOENT;
2712 struct d40_base *base;
2713 struct resource *res = NULL;
2714 int num_reserved_chans;
2715 u32 val;
2716
2717 base = d40_hw_detect_init(pdev);
2718
2719 if (!base)
2720 goto failure;
2721
2722 num_reserved_chans = d40_phy_res_init(base);
2723
2724 platform_set_drvdata(pdev, base);
2725
2726 spin_lock_init(&base->interrupt_lock);
2727 spin_lock_init(&base->execmd_lock);
2728
2729 /* Get IO for logical channel parameter address */
2730 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2731 if (!res) {
2732 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002733 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002734 goto failure;
2735 }
2736 base->lcpa_size = resource_size(res);
2737 base->phy_lcpa = res->start;
2738
2739 if (request_mem_region(res->start, resource_size(res),
2740 D40_NAME " I/O lcpa") == NULL) {
2741 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002742 d40_err(&pdev->dev,
2743 "Failed to request LCPA region 0x%x-0x%x\n",
2744 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02002745 goto failure;
2746 }
2747
2748 /* We make use of ESRAM memory for this. */
2749 val = readl(base->virtbase + D40_DREG_LCPA);
2750 if (res->start != val && val != 0) {
2751 dev_warn(&pdev->dev,
2752 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2753 __func__, val, res->start);
2754 } else
2755 writel(res->start, base->virtbase + D40_DREG_LCPA);
2756
2757 base->lcpa_base = ioremap(res->start, resource_size(res));
2758 if (!base->lcpa_base) {
2759 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002760 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002761 goto failure;
2762 }
Linus Walleij508849a2010-06-20 21:26:07 +00002763
2764 ret = d40_lcla_allocate(base);
2765 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002766 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002767 goto failure;
2768 }
2769
Linus Walleij8d318a52010-03-30 15:33:42 +02002770 spin_lock_init(&base->lcla_pool.lock);
2771
Linus Walleij8d318a52010-03-30 15:33:42 +02002772 base->irq = platform_get_irq(pdev, 0);
2773
2774 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02002775 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002776 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002777 goto failure;
2778 }
2779
2780 err = d40_dmaengine_init(base, num_reserved_chans);
2781 if (err)
2782 goto failure;
2783
2784 d40_hw_init(base);
2785
2786 dev_info(base->dev, "initialized\n");
2787 return 0;
2788
2789failure:
2790 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002791 if (base->desc_slab)
2792 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002793 if (base->virtbase)
2794 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01002795
2796 if (base->lcla_pool.dma_addr)
2797 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
2798 SZ_1K * base->num_phy_chans,
2799 DMA_TO_DEVICE);
2800
Linus Walleij508849a2010-06-20 21:26:07 +00002801 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2802 free_pages((unsigned long)base->lcla_pool.base,
2803 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002804
2805 kfree(base->lcla_pool.base_unaligned);
2806
Linus Walleij8d318a52010-03-30 15:33:42 +02002807 if (base->phy_lcpa)
2808 release_mem_region(base->phy_lcpa,
2809 base->lcpa_size);
2810 if (base->phy_start)
2811 release_mem_region(base->phy_start,
2812 base->phy_size);
2813 if (base->clk) {
2814 clk_disable(base->clk);
2815 clk_put(base->clk);
2816 }
2817
2818 kfree(base->lcla_pool.alloc_map);
2819 kfree(base->lookup_log_chans);
2820 kfree(base->lookup_phy_chans);
2821 kfree(base->phy_res);
2822 kfree(base);
2823 }
2824
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002825 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002826 return ret;
2827}
2828
2829static struct platform_driver d40_driver = {
2830 .driver = {
2831 .owner = THIS_MODULE,
2832 .name = D40_NAME,
2833 },
2834};
2835
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002836static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002837{
2838 return platform_driver_probe(&d40_driver, d40_probe);
2839}
2840arch_initcall(stedma40_init);