blob: 3d4cea3cff35fa3efa61756383bbf120c680c91c [file] [log] [blame]
Linus Walleij8d318a52010-03-30 15:33:42 +02001/*
Per Forlind49278e2010-12-20 18:31:38 +01002 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
Per Forlin661385f2010-10-06 09:05:28 +00004 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
Jonas Aaberg767a9672010-08-09 12:08:34 +00005 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
Linus Walleij8d318a52010-03-30 15:33:42 +02006 * License terms: GNU General Public License (GPL) version 2
Linus Walleij8d318a52010-03-30 15:33:42 +02007 */
8
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/dmaengine.h>
12#include <linux/platform_device.h>
13#include <linux/clk.h>
14#include <linux/delay.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000015#include <linux/err.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020016
17#include <plat/ste_dma40.h>
18
19#include "ste_dma40_ll.h"
20
21#define D40_NAME "dma40"
22
23#define D40_PHY_CHAN -1
24
25/* For masking out/in 2 bit channel positions */
26#define D40_CHAN_POS(chan) (2 * (chan / 2))
27#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
28
29/* Maximum iterations taken before giving up suspending a channel */
30#define D40_SUSPEND_MAX_IT 500
31
Linus Walleij508849a2010-06-20 21:26:07 +000032/* Hardware requirement on LCLA alignment */
33#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000034
35/* Max number of links per event group */
36#define D40_LCLA_LINK_PER_EVENT_GRP 128
37#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
38
Linus Walleij508849a2010-06-20 21:26:07 +000039/* Attempts before giving up to trying to get pages that are aligned */
40#define MAX_LCLA_ALLOC_ATTEMPTS 256
41
42/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020043#define D40_ALLOC_FREE (1 << 31)
44#define D40_ALLOC_PHY (1 << 30)
45#define D40_ALLOC_LOG_FREE 0
46
Linus Walleij8d318a52010-03-30 15:33:42 +020047/* Hardware designer of the block */
Jonas Aaberg3ae02672010-08-09 12:08:18 +000048#define D40_HW_DESIGNER 0x8
Linus Walleij8d318a52010-03-30 15:33:42 +020049
50/**
51 * enum 40_command - The different commands and/or statuses.
52 *
53 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
54 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
55 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
56 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
57 */
58enum d40_command {
59 D40_DMA_STOP = 0,
60 D40_DMA_RUN = 1,
61 D40_DMA_SUSPEND_REQ = 2,
62 D40_DMA_SUSPENDED = 3
63};
64
65/**
66 * struct d40_lli_pool - Structure for keeping LLIs in memory
67 *
68 * @base: Pointer to memory area when the pre_alloc_lli's are not large
69 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
70 * pre_alloc_lli is used.
71 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
72 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
73 * one buffer to one buffer.
74 */
75struct d40_lli_pool {
76 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000077 int size;
Linus Walleij8d318a52010-03-30 15:33:42 +020078 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000079 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020080};
81
82/**
83 * struct d40_desc - A descriptor is one DMA job.
84 *
85 * @lli_phy: LLI settings for physical channel. Both src and dst=
86 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
87 * lli_len equals one.
88 * @lli_log: Same as above but for logical channels.
89 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000090 * @lli_len: Number of llis of current descriptor.
Jonas Aaberg698e4732010-08-09 12:08:56 +000091 * @lli_current: Number of transfered llis.
92 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020093 * @txd: DMA engine struct. Used for among other things for communication
94 * during a transfer.
95 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020096 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000097 * the previous one.
Linus Walleij8d318a52010-03-30 15:33:42 +020098 *
99 * This descriptor is used for both logical and physical transfers.
100 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200101struct d40_desc {
102 /* LLI physical */
103 struct d40_phy_lli_bidir lli_phy;
104 /* LLI logical */
105 struct d40_log_lli_bidir lli_log;
106
107 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000108 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000109 int lli_current;
110 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200111
112 struct dma_async_tx_descriptor txd;
113 struct list_head node;
114
Linus Walleij8d318a52010-03-30 15:33:42 +0200115 bool is_in_client_list;
116};
117
118/**
119 * struct d40_lcla_pool - LCLA pool settings and data.
120 *
Linus Walleij508849a2010-06-20 21:26:07 +0000121 * @base: The virtual address of LCLA. 18 bit aligned.
122 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
123 * This pointer is only there for clean-up on error.
124 * @pages: The number of pages needed for all physical channels.
125 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200126 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000127 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200128 */
129struct d40_lcla_pool {
130 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000131 void *base_unaligned;
132 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200133 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000134 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200135};
136
137/**
138 * struct d40_phy_res - struct for handling eventlines mapped to physical
139 * channels.
140 *
141 * @lock: A lock protection this entity.
142 * @num: The physical channel number of this entity.
143 * @allocated_src: Bit mapped to show which src event line's are mapped to
144 * this physical channel. Can also be free or physically allocated.
145 * @allocated_dst: Same as for src but is dst.
146 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000147 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200148 */
149struct d40_phy_res {
150 spinlock_t lock;
151 int num;
152 u32 allocated_src;
153 u32 allocated_dst;
154};
155
156struct d40_base;
157
158/**
159 * struct d40_chan - Struct that describes a channel.
160 *
161 * @lock: A spinlock to protect this struct.
162 * @log_num: The logical number, if any of this channel.
163 * @completed: Starts with 1, after first interrupt it is set to dma engine's
164 * current cookie.
165 * @pending_tx: The number of pending transfers. Used between interrupt handler
166 * and tasklet.
167 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000168 * @phy_chan: Pointer to physical channel which this instance runs on. If this
169 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200170 * @chan: DMA engine handle.
171 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
172 * transfer and call client callback.
173 * @client: Cliented owned descriptor list.
174 * @active: Active descriptor.
175 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200176 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000177 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200178 * @base: Pointer to the device instance struct.
179 * @src_def_cfg: Default cfg register setting for src.
180 * @dst_def_cfg: Default cfg register setting for dst.
181 * @log_def: Default logical channel settings.
182 * @lcla: Space for one dst src pair for logical channel transfers.
183 * @lcpa: Pointer to dst and src lcpa settings.
184 *
185 * This struct can either "be" a logical or a physical channel.
186 */
187struct d40_chan {
188 spinlock_t lock;
189 int log_num;
190 /* ID of the most recent completed transfer */
191 int completed;
192 int pending_tx;
193 bool busy;
194 struct d40_phy_res *phy_chan;
195 struct dma_chan chan;
196 struct tasklet_struct tasklet;
197 struct list_head client;
198 struct list_head active;
199 struct list_head queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200200 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000201 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200202 struct d40_base *base;
203 /* Default register configurations */
204 u32 src_def_cfg;
205 u32 dst_def_cfg;
206 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200207 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200208 /* Runtime reconfiguration */
209 dma_addr_t runtime_addr;
210 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200211};
212
213/**
214 * struct d40_base - The big global struct, one for each probe'd instance.
215 *
216 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
217 * @execmd_lock: Lock for execute command usage since several channels share
218 * the same physical register.
219 * @dev: The device structure.
220 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700221 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200222 * @clk: Pointer to the DMA clock structure.
223 * @phy_start: Physical memory start of the DMA registers.
224 * @phy_size: Size of the DMA register map.
225 * @irq: The IRQ number.
226 * @num_phy_chans: The number of physical channels. Read from HW. This
227 * is the number of available channels for this driver, not counting "Secure
228 * mode" allocated physical channels.
229 * @num_log_chans: The number of logical channels. Calculated from
230 * num_phy_chans.
231 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
232 * @dma_slave: dma_device channels that can do only do slave transfers.
233 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200234 * @log_chans: Room for all possible logical channels in system.
235 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
236 * to log_chans entries.
237 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
238 * to phy_chans entries.
239 * @plat_data: Pointer to provided platform_data which is the driver
240 * configuration.
241 * @phy_res: Vector containing all physical channels.
242 * @lcla_pool: lcla pool settings and data.
243 * @lcpa_base: The virtual mapped address of LCPA.
244 * @phy_lcpa: The physical address of the LCPA.
245 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000246 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200247 */
248struct d40_base {
249 spinlock_t interrupt_lock;
250 spinlock_t execmd_lock;
251 struct device *dev;
252 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700253 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200254 struct clk *clk;
255 phys_addr_t phy_start;
256 resource_size_t phy_size;
257 int irq;
258 int num_phy_chans;
259 int num_log_chans;
260 struct dma_device dma_both;
261 struct dma_device dma_slave;
262 struct dma_device dma_memcpy;
263 struct d40_chan *phy_chans;
264 struct d40_chan *log_chans;
265 struct d40_chan **lookup_log_chans;
266 struct d40_chan **lookup_phy_chans;
267 struct stedma40_platform_data *plat_data;
268 /* Physical half channels */
269 struct d40_phy_res *phy_res;
270 struct d40_lcla_pool lcla_pool;
271 void *lcpa_base;
272 dma_addr_t phy_lcpa;
273 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000274 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200275};
276
277/**
278 * struct d40_interrupt_lookup - lookup table for interrupt handler
279 *
280 * @src: Interrupt mask register.
281 * @clr: Interrupt clear register.
282 * @is_error: true if this is an error interrupt.
283 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
284 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
285 */
286struct d40_interrupt_lookup {
287 u32 src;
288 u32 clr;
289 bool is_error;
290 int offset;
291};
292
293/**
294 * struct d40_reg_val - simple lookup struct
295 *
296 * @reg: The register.
297 * @val: The value that belongs to the register in reg.
298 */
299struct d40_reg_val {
300 unsigned int reg;
301 unsigned int val;
302};
303
Rabin Vincent262d2912011-01-25 11:18:05 +0100304static struct device *chan2dev(struct d40_chan *d40c)
305{
306 return &d40c->chan.dev->device;
307}
308
Rabin Vincent8ca84682011-01-25 11:18:07 +0100309static void __iomem *chan_base(struct d40_chan *chan)
310{
311 return chan->base->virtbase + D40_DREG_PCBASE +
312 chan->phy_chan->num * D40_DREG_PCDELTA;
313}
314
Linus Walleij8d318a52010-03-30 15:33:42 +0200315static int d40_pool_lli_alloc(struct d40_desc *d40d,
316 int lli_len, bool is_log)
317{
318 u32 align;
319 void *base;
320
321 if (is_log)
322 align = sizeof(struct d40_log_lli);
323 else
324 align = sizeof(struct d40_phy_lli);
325
326 if (lli_len == 1) {
327 base = d40d->lli_pool.pre_alloc_lli;
328 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
329 d40d->lli_pool.base = NULL;
330 } else {
331 d40d->lli_pool.size = ALIGN(lli_len * 2 * align, align);
332
333 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
334 d40d->lli_pool.base = base;
335
336 if (d40d->lli_pool.base == NULL)
337 return -ENOMEM;
338 }
339
340 if (is_log) {
341 d40d->lli_log.src = PTR_ALIGN((struct d40_log_lli *) base,
342 align);
343 d40d->lli_log.dst = PTR_ALIGN(d40d->lli_log.src + lli_len,
344 align);
345 } else {
346 d40d->lli_phy.src = PTR_ALIGN((struct d40_phy_lli *)base,
347 align);
348 d40d->lli_phy.dst = PTR_ALIGN(d40d->lli_phy.src + lli_len,
349 align);
Linus Walleij8d318a52010-03-30 15:33:42 +0200350 }
351
352 return 0;
353}
354
355static void d40_pool_lli_free(struct d40_desc *d40d)
356{
357 kfree(d40d->lli_pool.base);
358 d40d->lli_pool.base = NULL;
359 d40d->lli_pool.size = 0;
360 d40d->lli_log.src = NULL;
361 d40d->lli_log.dst = NULL;
362 d40d->lli_phy.src = NULL;
363 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200364}
365
Jonas Aaberg698e4732010-08-09 12:08:56 +0000366static int d40_lcla_alloc_one(struct d40_chan *d40c,
367 struct d40_desc *d40d)
368{
369 unsigned long flags;
370 int i;
371 int ret = -EINVAL;
372 int p;
373
374 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
375
376 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
377
378 /*
379 * Allocate both src and dst at the same time, therefore the half
380 * start on 1 since 0 can't be used since zero is used as end marker.
381 */
382 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
383 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
384 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
385 d40d->lcla_alloc++;
386 ret = i;
387 break;
388 }
389 }
390
391 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
392
393 return ret;
394}
395
396static int d40_lcla_free_all(struct d40_chan *d40c,
397 struct d40_desc *d40d)
398{
399 unsigned long flags;
400 int i;
401 int ret = -EINVAL;
402
403 if (d40c->log_num == D40_PHY_CHAN)
404 return 0;
405
406 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
407
408 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
409 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
410 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
411 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
412 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
413 d40d->lcla_alloc--;
414 if (d40d->lcla_alloc == 0) {
415 ret = 0;
416 break;
417 }
418 }
419 }
420
421 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
422
423 return ret;
424
425}
426
Linus Walleij8d318a52010-03-30 15:33:42 +0200427static void d40_desc_remove(struct d40_desc *d40d)
428{
429 list_del(&d40d->node);
430}
431
432static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
433{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000434 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200435
436 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000437 struct d40_desc *d;
438 struct d40_desc *_d;
439
Linus Walleij8d318a52010-03-30 15:33:42 +0200440 list_for_each_entry_safe(d, _d, &d40c->client, node)
441 if (async_tx_test_ack(&d->txd)) {
442 d40_pool_lli_free(d);
443 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000444 desc = d;
445 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000446 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200447 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200448 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000449
450 if (!desc)
451 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
452
453 if (desc)
454 INIT_LIST_HEAD(&desc->node);
455
456 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200457}
458
459static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
460{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000461
462 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000463 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200464}
465
466static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
467{
468 list_add_tail(&desc->node, &d40c->active);
469}
470
Jonas Aaberg698e4732010-08-09 12:08:56 +0000471static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
472{
473 int curr_lcla = -EINVAL, next_lcla;
474
475 if (d40c->log_num == D40_PHY_CHAN) {
476 d40_phy_lli_write(d40c->base->virtbase,
477 d40c->phy_chan->num,
478 d40d->lli_phy.dst,
479 d40d->lli_phy.src);
480 d40d->lli_current = d40d->lli_len;
481 } else {
482
483 if ((d40d->lli_len - d40d->lli_current) > 1)
484 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
485
486 d40_log_lli_lcpa_write(d40c->lcpa,
487 &d40d->lli_log.dst[d40d->lli_current],
488 &d40d->lli_log.src[d40d->lli_current],
489 curr_lcla);
490
491 d40d->lli_current++;
492 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
493 struct d40_log_lli *lcla;
494
495 if (d40d->lli_current + 1 < d40d->lli_len)
496 next_lcla = d40_lcla_alloc_one(d40c, d40d);
497 else
498 next_lcla = -EINVAL;
499
500 lcla = d40c->base->lcla_pool.base +
501 d40c->phy_chan->num * 1024 +
502 8 * curr_lcla * 2;
503
504 d40_log_lli_lcla_write(lcla,
505 &d40d->lli_log.dst[d40d->lli_current],
506 &d40d->lli_log.src[d40d->lli_current],
507 next_lcla);
508
509 (void) dma_map_single(d40c->base->dev, lcla,
510 2 * sizeof(struct d40_log_lli),
511 DMA_TO_DEVICE);
512
513 curr_lcla = next_lcla;
514
515 if (curr_lcla == -EINVAL) {
516 d40d->lli_current++;
517 break;
518 }
519
520 }
521 }
522}
523
Linus Walleij8d318a52010-03-30 15:33:42 +0200524static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
525{
526 struct d40_desc *d;
527
528 if (list_empty(&d40c->active))
529 return NULL;
530
531 d = list_first_entry(&d40c->active,
532 struct d40_desc,
533 node);
534 return d;
535}
536
537static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
538{
539 list_add_tail(&desc->node, &d40c->queue);
540}
541
542static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
543{
544 struct d40_desc *d;
545
546 if (list_empty(&d40c->queue))
547 return NULL;
548
549 d = list_first_entry(&d40c->queue,
550 struct d40_desc,
551 node);
552 return d;
553}
554
Per Forlind49278e2010-12-20 18:31:38 +0100555static int d40_psize_2_burst_size(bool is_log, int psize)
556{
557 if (is_log) {
558 if (psize == STEDMA40_PSIZE_LOG_1)
559 return 1;
560 } else {
561 if (psize == STEDMA40_PSIZE_PHY_1)
562 return 1;
563 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200564
Per Forlind49278e2010-12-20 18:31:38 +0100565 return 2 << psize;
566}
567
568/*
569 * The dma only supports transmitting packages up to
570 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
571 * dma elements required to send the entire sg list
572 */
573static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
574{
575 int dmalen;
576 u32 max_w = max(data_width1, data_width2);
577 u32 min_w = min(data_width1, data_width2);
578 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
579
580 if (seg_max > STEDMA40_MAX_SEG_SIZE)
581 seg_max -= (1 << max_w);
582
583 if (!IS_ALIGNED(size, 1 << max_w))
584 return -EINVAL;
585
586 if (size <= seg_max)
587 dmalen = 1;
588 else {
589 dmalen = size / seg_max;
590 if (dmalen * seg_max < size)
591 dmalen++;
592 }
593 return dmalen;
594}
595
596static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
597 u32 data_width1, u32 data_width2)
598{
599 struct scatterlist *sg;
600 int i;
601 int len = 0;
602 int ret;
603
604 for_each_sg(sgl, sg, sg_len, i) {
605 ret = d40_size_2_dmalen(sg_dma_len(sg),
606 data_width1, data_width2);
607 if (ret < 0)
608 return ret;
609 len += ret;
610 }
611 return len;
612}
613
614/* Support functions for logical channels */
Linus Walleij8d318a52010-03-30 15:33:42 +0200615
616static int d40_channel_execute_command(struct d40_chan *d40c,
617 enum d40_command command)
618{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000619 u32 status;
620 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200621 void __iomem *active_reg;
622 int ret = 0;
623 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000624 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200625
626 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
627
628 if (d40c->phy_chan->num % 2 == 0)
629 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
630 else
631 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
632
633 if (command == D40_DMA_SUSPEND_REQ) {
634 status = (readl(active_reg) &
635 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
636 D40_CHAN_POS(d40c->phy_chan->num);
637
638 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
639 goto done;
640 }
641
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000642 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
643 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
644 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200645
646 if (command == D40_DMA_SUSPEND_REQ) {
647
648 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
649 status = (readl(active_reg) &
650 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
651 D40_CHAN_POS(d40c->phy_chan->num);
652
653 cpu_relax();
654 /*
655 * Reduce the number of bus accesses while
656 * waiting for the DMA to suspend.
657 */
658 udelay(3);
659
660 if (status == D40_DMA_STOP ||
661 status == D40_DMA_SUSPENDED)
662 break;
663 }
664
665 if (i == D40_SUSPEND_MAX_IT) {
666 dev_err(&d40c->chan.dev->device,
667 "[%s]: unable to suspend the chl %d (log: %d) status %x\n",
668 __func__, d40c->phy_chan->num, d40c->log_num,
669 status);
670 dump_stack();
671 ret = -EBUSY;
672 }
673
674 }
675done:
676 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
677 return ret;
678}
679
680static void d40_term_all(struct d40_chan *d40c)
681{
682 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200683
684 /* Release active descriptors */
685 while ((d40d = d40_first_active_get(d40c))) {
686 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200687 d40_desc_free(d40c, d40d);
688 }
689
690 /* Release queued descriptors waiting for transfer */
691 while ((d40d = d40_first_queued(d40c))) {
692 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200693 d40_desc_free(d40c, d40d);
694 }
695
Linus Walleij8d318a52010-03-30 15:33:42 +0200696
697 d40c->pending_tx = 0;
698 d40c->busy = false;
699}
700
Rabin Vincent262d2912011-01-25 11:18:05 +0100701static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
702 u32 event, int reg)
703{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100704 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100705 int tries;
706
707 if (!enable) {
708 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
709 | ~D40_EVENTLINE_MASK(event), addr);
710 return;
711 }
712
713 /*
714 * The hardware sometimes doesn't register the enable when src and dst
715 * event lines are active on the same logical channel. Retry to ensure
716 * it does. Usually only one retry is sufficient.
717 */
718 tries = 100;
719 while (--tries) {
720 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
721 | ~D40_EVENTLINE_MASK(event), addr);
722
723 if (readl(addr) & D40_EVENTLINE_MASK(event))
724 break;
725 }
726
727 if (tries != 99)
728 dev_dbg(chan2dev(d40c),
729 "[%s] workaround enable S%cLNK (%d tries)\n",
730 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
731 100 - tries);
732
733 WARN_ON(!tries);
734}
735
Linus Walleij8d318a52010-03-30 15:33:42 +0200736static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
737{
Linus Walleij8d318a52010-03-30 15:33:42 +0200738 unsigned long flags;
739
Linus Walleij8d318a52010-03-30 15:33:42 +0200740 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
741
742 /* Enable event line connected to device (or memcpy) */
743 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
744 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
745 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
746
Rabin Vincent262d2912011-01-25 11:18:05 +0100747 __d40_config_set_event(d40c, do_enable, event,
748 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200749 }
Rabin Vincent262d2912011-01-25 11:18:05 +0100750
Linus Walleij8d318a52010-03-30 15:33:42 +0200751 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
752 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
753
Rabin Vincent262d2912011-01-25 11:18:05 +0100754 __d40_config_set_event(d40c, do_enable, event,
755 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200756 }
757
758 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
759}
760
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200761static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200762{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100763 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000764 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200765
Rabin Vincent8ca84682011-01-25 11:18:07 +0100766 val = readl(chanbase + D40_CHAN_REG_SSLNK);
767 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200768
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200769 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200770}
771
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000772static u32 d40_get_prmo(struct d40_chan *d40c)
773{
774 static const unsigned int phy_map[] = {
775 [STEDMA40_PCHAN_BASIC_MODE]
776 = D40_DREG_PRMO_PCHAN_BASIC,
777 [STEDMA40_PCHAN_MODULO_MODE]
778 = D40_DREG_PRMO_PCHAN_MODULO,
779 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
780 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
781 };
782 static const unsigned int log_map[] = {
783 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
784 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
785 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
786 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
787 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
788 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
789 };
790
791 if (d40c->log_num == D40_PHY_CHAN)
792 return phy_map[d40c->dma_cfg.mode_opt];
793 else
794 return log_map[d40c->dma_cfg.mode_opt];
795}
796
Jonas Aabergb55912c2010-08-09 12:08:02 +0000797static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200798{
799 u32 addr_base;
800 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200801
802 /* Odd addresses are even addresses + 4 */
803 addr_base = (d40c->phy_chan->num % 2) * 4;
804 /* Setup channel mode to logical or physical */
805 var = ((u32)(d40c->log_num != D40_PHY_CHAN) + 1) <<
806 D40_CHAN_POS(d40c->phy_chan->num);
807 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
808
809 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +0000810 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +0200811
812 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
813
814 if (d40c->log_num != D40_PHY_CHAN) {
Rabin Vincent8ca84682011-01-25 11:18:07 +0100815 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
816 & D40_SREG_ELEM_LOG_LIDX_MASK;
817 void __iomem *chanbase = chan_base(d40c);
818
Linus Walleij8d318a52010-03-30 15:33:42 +0200819 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100820 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
821 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +0200822
Jonas Aabergb55912c2010-08-09 12:08:02 +0000823 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +0100824 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
825 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +0200826 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200827}
828
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000829static u32 d40_residue(struct d40_chan *d40c)
830{
831 u32 num_elt;
832
833 if (d40c->log_num != D40_PHY_CHAN)
834 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
835 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +0100836 else {
837 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
838 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
839 >> D40_SREG_ELEM_PHY_ECNT_POS;
840 }
841
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000842 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
843}
844
845static bool d40_tx_is_linked(struct d40_chan *d40c)
846{
847 bool is_link;
848
849 if (d40c->log_num != D40_PHY_CHAN)
850 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
851 else
Rabin Vincent8ca84682011-01-25 11:18:07 +0100852 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
853 & D40_SREG_LNK_PHYS_LNK_MASK;
854
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000855 return is_link;
856}
857
858static int d40_pause(struct dma_chan *chan)
859{
860 struct d40_chan *d40c =
861 container_of(chan, struct d40_chan, chan);
862 int res = 0;
863 unsigned long flags;
864
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000865 if (!d40c->busy)
866 return 0;
867
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000868 spin_lock_irqsave(&d40c->lock, flags);
869
870 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
871 if (res == 0) {
872 if (d40c->log_num != D40_PHY_CHAN) {
873 d40_config_set_event(d40c, false);
874 /* Resume the other logical channels if any */
875 if (d40_chan_has_events(d40c))
876 res = d40_channel_execute_command(d40c,
877 D40_DMA_RUN);
878 }
879 }
880
881 spin_unlock_irqrestore(&d40c->lock, flags);
882 return res;
883}
884
885static int d40_resume(struct dma_chan *chan)
886{
887 struct d40_chan *d40c =
888 container_of(chan, struct d40_chan, chan);
889 int res = 0;
890 unsigned long flags;
891
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000892 if (!d40c->busy)
893 return 0;
894
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000895 spin_lock_irqsave(&d40c->lock, flags);
896
897 if (d40c->base->rev == 0)
898 if (d40c->log_num != D40_PHY_CHAN) {
899 res = d40_channel_execute_command(d40c,
900 D40_DMA_SUSPEND_REQ);
901 goto no_suspend;
902 }
903
904 /* If bytes left to transfer or linked tx resume job */
905 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
906
907 if (d40c->log_num != D40_PHY_CHAN)
908 d40_config_set_event(d40c, true);
909
910 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
911 }
912
913no_suspend:
914 spin_unlock_irqrestore(&d40c->lock, flags);
915 return res;
916}
917
Linus Walleij8d318a52010-03-30 15:33:42 +0200918static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
919{
920 struct d40_chan *d40c = container_of(tx->chan,
921 struct d40_chan,
922 chan);
923 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
924 unsigned long flags;
925
926 spin_lock_irqsave(&d40c->lock, flags);
927
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000928 d40c->chan.cookie++;
929
930 if (d40c->chan.cookie < 0)
931 d40c->chan.cookie = 1;
932
933 d40d->txd.cookie = d40c->chan.cookie;
934
Linus Walleij8d318a52010-03-30 15:33:42 +0200935 d40_desc_queue(d40c, d40d);
936
937 spin_unlock_irqrestore(&d40c->lock, flags);
938
939 return tx->cookie;
940}
941
942static int d40_start(struct d40_chan *d40c)
943{
Linus Walleijf4185592010-06-22 18:06:42 -0700944 if (d40c->base->rev == 0) {
945 int err;
946
947 if (d40c->log_num != D40_PHY_CHAN) {
948 err = d40_channel_execute_command(d40c,
949 D40_DMA_SUSPEND_REQ);
950 if (err)
951 return err;
952 }
953 }
954
Jonas Aaberg0c322692010-06-20 21:25:46 +0000955 if (d40c->log_num != D40_PHY_CHAN)
Linus Walleij8d318a52010-03-30 15:33:42 +0200956 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +0200957
Jonas Aaberg0c322692010-06-20 21:25:46 +0000958 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +0200959}
960
961static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
962{
963 struct d40_desc *d40d;
964 int err;
965
966 /* Start queued jobs, if any */
967 d40d = d40_first_queued(d40c);
968
969 if (d40d != NULL) {
970 d40c->busy = true;
971
972 /* Remove from queue */
973 d40_desc_remove(d40d);
974
975 /* Add to active queue */
976 d40_desc_submit(d40c, d40d);
977
Rabin Vincent7d83a852011-01-25 11:18:06 +0100978 /* Initiate DMA job */
979 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000980
Rabin Vincent7d83a852011-01-25 11:18:06 +0100981 /* Start dma job */
982 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200983
Rabin Vincent7d83a852011-01-25 11:18:06 +0100984 if (err)
985 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200986 }
987
988 return d40d;
989}
990
991/* called from interrupt context */
992static void dma_tc_handle(struct d40_chan *d40c)
993{
994 struct d40_desc *d40d;
995
Linus Walleij8d318a52010-03-30 15:33:42 +0200996 /* Get first active entry from list */
997 d40d = d40_first_active_get(d40c);
998
999 if (d40d == NULL)
1000 return;
1001
Jonas Aaberg698e4732010-08-09 12:08:56 +00001002 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001003
Jonas Aaberg698e4732010-08-09 12:08:56 +00001004 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001005 d40_desc_load(d40c, d40d);
1006 /* Start dma job */
1007 (void) d40_start(d40c);
1008 return;
1009 }
1010
1011 if (d40_queue_start(d40c) == NULL)
1012 d40c->busy = false;
1013
1014 d40c->pending_tx++;
1015 tasklet_schedule(&d40c->tasklet);
1016
1017}
1018
1019static void dma_tasklet(unsigned long data)
1020{
1021 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001022 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001023 unsigned long flags;
1024 dma_async_tx_callback callback;
1025 void *callback_param;
1026
1027 spin_lock_irqsave(&d40c->lock, flags);
1028
1029 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001030 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001031
Jonas Aaberg767a9672010-08-09 12:08:34 +00001032 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001033 goto err;
1034
Jonas Aaberg767a9672010-08-09 12:08:34 +00001035 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001036
1037 /*
1038 * If terminating a channel pending_tx is set to zero.
1039 * This prevents any finished active jobs to return to the client.
1040 */
1041 if (d40c->pending_tx == 0) {
1042 spin_unlock_irqrestore(&d40c->lock, flags);
1043 return;
1044 }
1045
1046 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001047 callback = d40d->txd.callback;
1048 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001049
Jonas Aaberg767a9672010-08-09 12:08:34 +00001050 if (async_tx_test_ack(&d40d->txd)) {
1051 d40_pool_lli_free(d40d);
1052 d40_desc_remove(d40d);
1053 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001054 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001055 if (!d40d->is_in_client_list) {
1056 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001057 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001058 list_add_tail(&d40d->node, &d40c->client);
1059 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001060 }
1061 }
1062
1063 d40c->pending_tx--;
1064
1065 if (d40c->pending_tx)
1066 tasklet_schedule(&d40c->tasklet);
1067
1068 spin_unlock_irqrestore(&d40c->lock, flags);
1069
Jonas Aaberg767a9672010-08-09 12:08:34 +00001070 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001071 callback(callback_param);
1072
1073 return;
1074
1075 err:
1076 /* Rescue manouver if receiving double interrupts */
1077 if (d40c->pending_tx > 0)
1078 d40c->pending_tx--;
1079 spin_unlock_irqrestore(&d40c->lock, flags);
1080}
1081
1082static irqreturn_t d40_handle_interrupt(int irq, void *data)
1083{
1084 static const struct d40_interrupt_lookup il[] = {
1085 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1086 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1087 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1088 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1089 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1090 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1091 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1092 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1093 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1094 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1095 };
1096
1097 int i;
1098 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001099 u32 idx;
1100 u32 row;
1101 long chan = -1;
1102 struct d40_chan *d40c;
1103 unsigned long flags;
1104 struct d40_base *base = data;
1105
1106 spin_lock_irqsave(&base->interrupt_lock, flags);
1107
1108 /* Read interrupt status of both logical and physical channels */
1109 for (i = 0; i < ARRAY_SIZE(il); i++)
1110 regs[i] = readl(base->virtbase + il[i].src);
1111
1112 for (;;) {
1113
1114 chan = find_next_bit((unsigned long *)regs,
1115 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1116
1117 /* No more set bits found? */
1118 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1119 break;
1120
1121 row = chan / BITS_PER_LONG;
1122 idx = chan & (BITS_PER_LONG - 1);
1123
1124 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001125 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001126
1127 if (il[row].offset == D40_PHY_CHAN)
1128 d40c = base->lookup_phy_chans[idx];
1129 else
1130 d40c = base->lookup_log_chans[il[row].offset + idx];
1131 spin_lock(&d40c->lock);
1132
1133 if (!il[row].is_error)
1134 dma_tc_handle(d40c);
1135 else
Linus Walleij508849a2010-06-20 21:26:07 +00001136 dev_err(base->dev,
1137 "[%s] IRQ chan: %ld offset %d idx %d\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02001138 __func__, chan, il[row].offset, idx);
1139
1140 spin_unlock(&d40c->lock);
1141 }
1142
1143 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1144
1145 return IRQ_HANDLED;
1146}
1147
Linus Walleij8d318a52010-03-30 15:33:42 +02001148static int d40_validate_conf(struct d40_chan *d40c,
1149 struct stedma40_chan_cfg *conf)
1150{
1151 int res = 0;
1152 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1153 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001154 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001155
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001156 if (!conf->dir) {
1157 dev_err(&d40c->chan.dev->device, "[%s] Invalid direction.\n",
1158 __func__);
1159 res = -EINVAL;
1160 }
1161
1162 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1163 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1164 d40c->runtime_addr == 0) {
1165
1166 dev_err(&d40c->chan.dev->device,
1167 "[%s] Invalid TX channel address (%d)\n",
1168 __func__, conf->dst_dev_type);
1169 res = -EINVAL;
1170 }
1171
1172 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1173 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1174 d40c->runtime_addr == 0) {
1175 dev_err(&d40c->chan.dev->device,
1176 "[%s] Invalid RX channel address (%d)\n",
1177 __func__, conf->src_dev_type);
1178 res = -EINVAL;
1179 }
1180
1181 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001182 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
1183 dev_err(&d40c->chan.dev->device, "[%s] Invalid dst\n",
1184 __func__);
1185 res = -EINVAL;
1186 }
1187
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001188 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001189 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
1190 dev_err(&d40c->chan.dev->device, "[%s] Invalid src\n",
1191 __func__);
1192 res = -EINVAL;
1193 }
1194
1195 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1196 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
1197 dev_err(&d40c->chan.dev->device,
1198 "[%s] No event line\n", __func__);
1199 res = -EINVAL;
1200 }
1201
1202 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1203 (src_event_group != dst_event_group)) {
1204 dev_err(&d40c->chan.dev->device,
1205 "[%s] Invalid event group\n", __func__);
1206 res = -EINVAL;
1207 }
1208
1209 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1210 /*
1211 * DMAC HW supports it. Will be added to this driver,
1212 * in case any dma client requires it.
1213 */
1214 dev_err(&d40c->chan.dev->device,
1215 "[%s] periph to periph not supported\n",
1216 __func__);
1217 res = -EINVAL;
1218 }
1219
Per Forlind49278e2010-12-20 18:31:38 +01001220 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1221 (1 << conf->src_info.data_width) !=
1222 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1223 (1 << conf->dst_info.data_width)) {
1224 /*
1225 * The DMAC hardware only supports
1226 * src (burst x width) == dst (burst x width)
1227 */
1228
1229 dev_err(&d40c->chan.dev->device,
1230 "[%s] src (burst x width) != dst (burst x width)\n",
1231 __func__);
1232 res = -EINVAL;
1233 }
1234
Linus Walleij8d318a52010-03-30 15:33:42 +02001235 return res;
1236}
1237
1238static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001239 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001240{
1241 unsigned long flags;
1242 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001243 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001244 /* Physical interrupts are masked per physical full channel */
1245 if (phy->allocated_src == D40_ALLOC_FREE &&
1246 phy->allocated_dst == D40_ALLOC_FREE) {
1247 phy->allocated_dst = D40_ALLOC_PHY;
1248 phy->allocated_src = D40_ALLOC_PHY;
1249 goto found;
1250 } else
1251 goto not_found;
1252 }
1253
1254 /* Logical channel */
1255 if (is_src) {
1256 if (phy->allocated_src == D40_ALLOC_PHY)
1257 goto not_found;
1258
1259 if (phy->allocated_src == D40_ALLOC_FREE)
1260 phy->allocated_src = D40_ALLOC_LOG_FREE;
1261
1262 if (!(phy->allocated_src & (1 << log_event_line))) {
1263 phy->allocated_src |= 1 << log_event_line;
1264 goto found;
1265 } else
1266 goto not_found;
1267 } else {
1268 if (phy->allocated_dst == D40_ALLOC_PHY)
1269 goto not_found;
1270
1271 if (phy->allocated_dst == D40_ALLOC_FREE)
1272 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1273
1274 if (!(phy->allocated_dst & (1 << log_event_line))) {
1275 phy->allocated_dst |= 1 << log_event_line;
1276 goto found;
1277 } else
1278 goto not_found;
1279 }
1280
1281not_found:
1282 spin_unlock_irqrestore(&phy->lock, flags);
1283 return false;
1284found:
1285 spin_unlock_irqrestore(&phy->lock, flags);
1286 return true;
1287}
1288
1289static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1290 int log_event_line)
1291{
1292 unsigned long flags;
1293 bool is_free = false;
1294
1295 spin_lock_irqsave(&phy->lock, flags);
1296 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001297 phy->allocated_dst = D40_ALLOC_FREE;
1298 phy->allocated_src = D40_ALLOC_FREE;
1299 is_free = true;
1300 goto out;
1301 }
1302
1303 /* Logical channel */
1304 if (is_src) {
1305 phy->allocated_src &= ~(1 << log_event_line);
1306 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1307 phy->allocated_src = D40_ALLOC_FREE;
1308 } else {
1309 phy->allocated_dst &= ~(1 << log_event_line);
1310 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1311 phy->allocated_dst = D40_ALLOC_FREE;
1312 }
1313
1314 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1315 D40_ALLOC_FREE);
1316
1317out:
1318 spin_unlock_irqrestore(&phy->lock, flags);
1319
1320 return is_free;
1321}
1322
1323static int d40_allocate_channel(struct d40_chan *d40c)
1324{
1325 int dev_type;
1326 int event_group;
1327 int event_line;
1328 struct d40_phy_res *phys;
1329 int i;
1330 int j;
1331 int log_num;
1332 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001333 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001334
1335 phys = d40c->base->phy_res;
1336
1337 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1338 dev_type = d40c->dma_cfg.src_dev_type;
1339 log_num = 2 * dev_type;
1340 is_src = true;
1341 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1342 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1343 /* dst event lines are used for logical memcpy */
1344 dev_type = d40c->dma_cfg.dst_dev_type;
1345 log_num = 2 * dev_type + 1;
1346 is_src = false;
1347 } else
1348 return -EINVAL;
1349
1350 event_group = D40_TYPE_TO_GROUP(dev_type);
1351 event_line = D40_TYPE_TO_EVENT(dev_type);
1352
1353 if (!is_log) {
1354 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1355 /* Find physical half channel */
1356 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1357
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001358 if (d40_alloc_mask_set(&phys[i], is_src,
1359 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001360 goto found_phy;
1361 }
1362 } else
1363 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1364 int phy_num = j + event_group * 2;
1365 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001366 if (d40_alloc_mask_set(&phys[i],
1367 is_src,
1368 0,
1369 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001370 goto found_phy;
1371 }
1372 }
1373 return -EINVAL;
1374found_phy:
1375 d40c->phy_chan = &phys[i];
1376 d40c->log_num = D40_PHY_CHAN;
1377 goto out;
1378 }
1379 if (dev_type == -1)
1380 return -EINVAL;
1381
1382 /* Find logical channel */
1383 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1384 int phy_num = j + event_group * 2;
1385 /*
1386 * Spread logical channels across all available physical rather
1387 * than pack every logical channel at the first available phy
1388 * channels.
1389 */
1390 if (is_src) {
1391 for (i = phy_num; i < phy_num + 2; i++) {
1392 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001393 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001394 goto found_log;
1395 }
1396 } else {
1397 for (i = phy_num + 1; i >= phy_num; i--) {
1398 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001399 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001400 goto found_log;
1401 }
1402 }
1403 }
1404 return -EINVAL;
1405
1406found_log:
1407 d40c->phy_chan = &phys[i];
1408 d40c->log_num = log_num;
1409out:
1410
1411 if (is_log)
1412 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1413 else
1414 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1415
1416 return 0;
1417
1418}
1419
Linus Walleij8d318a52010-03-30 15:33:42 +02001420static int d40_config_memcpy(struct d40_chan *d40c)
1421{
1422 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1423
1424 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1425 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1426 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1427 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1428 memcpy[d40c->chan.chan_id];
1429
1430 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1431 dma_has_cap(DMA_SLAVE, cap)) {
1432 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1433 } else {
1434 dev_err(&d40c->chan.dev->device, "[%s] No memcpy\n",
1435 __func__);
1436 return -EINVAL;
1437 }
1438
1439 return 0;
1440}
1441
1442
1443static int d40_free_dma(struct d40_chan *d40c)
1444{
1445
1446 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001447 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001448 struct d40_phy_res *phy = d40c->phy_chan;
1449 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001450 struct d40_desc *d;
1451 struct d40_desc *_d;
1452
Linus Walleij8d318a52010-03-30 15:33:42 +02001453
1454 /* Terminate all queued and active transfers */
1455 d40_term_all(d40c);
1456
Per Fridena8be8622010-06-20 21:24:59 +00001457 /* Release client owned descriptors */
1458 if (!list_empty(&d40c->client))
1459 list_for_each_entry_safe(d, _d, &d40c->client, node) {
1460 d40_pool_lli_free(d);
1461 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001462 d40_desc_free(d40c, d);
1463 }
1464
Linus Walleij8d318a52010-03-30 15:33:42 +02001465 if (phy == NULL) {
1466 dev_err(&d40c->chan.dev->device, "[%s] phy == null\n",
1467 __func__);
1468 return -EINVAL;
1469 }
1470
1471 if (phy->allocated_src == D40_ALLOC_FREE &&
1472 phy->allocated_dst == D40_ALLOC_FREE) {
1473 dev_err(&d40c->chan.dev->device, "[%s] channel already free\n",
1474 __func__);
1475 return -EINVAL;
1476 }
1477
Linus Walleij8d318a52010-03-30 15:33:42 +02001478 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1479 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1480 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001481 is_src = false;
1482 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1483 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001484 is_src = true;
1485 } else {
1486 dev_err(&d40c->chan.dev->device,
1487 "[%s] Unknown direction\n", __func__);
1488 return -EINVAL;
1489 }
1490
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001491 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1492 if (res) {
1493 dev_err(&d40c->chan.dev->device, "[%s] suspend failed\n",
1494 __func__);
1495 return res;
1496 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001497
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001498 if (d40c->log_num != D40_PHY_CHAN) {
1499 /* Release logical channel, deactivate the event line */
1500
1501 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001502 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1503
1504 /*
1505 * Check if there are more logical allocation
1506 * on this phy channel.
1507 */
1508 if (!d40_alloc_mask_free(phy, is_src, event)) {
1509 /* Resume the other logical channels if any */
1510 if (d40_chan_has_events(d40c)) {
1511 res = d40_channel_execute_command(d40c,
1512 D40_DMA_RUN);
1513 if (res) {
1514 dev_err(&d40c->chan.dev->device,
1515 "[%s] Executing RUN command\n",
1516 __func__);
1517 return res;
1518 }
1519 }
1520 return 0;
1521 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001522 } else {
1523 (void) d40_alloc_mask_free(phy, is_src, 0);
1524 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001525
1526 /* Release physical channel */
1527 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1528 if (res) {
1529 dev_err(&d40c->chan.dev->device,
1530 "[%s] Failed to stop channel\n", __func__);
1531 return res;
1532 }
1533 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001534 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 d40c->base->lookup_phy_chans[phy->num] = NULL;
1536
1537 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001538}
1539
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001540static bool d40_is_paused(struct d40_chan *d40c)
1541{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001542 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001543 bool is_paused = false;
1544 unsigned long flags;
1545 void __iomem *active_reg;
1546 u32 status;
1547 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001548
1549 spin_lock_irqsave(&d40c->lock, flags);
1550
1551 if (d40c->log_num == D40_PHY_CHAN) {
1552 if (d40c->phy_chan->num % 2 == 0)
1553 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1554 else
1555 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1556
1557 status = (readl(active_reg) &
1558 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1559 D40_CHAN_POS(d40c->phy_chan->num);
1560 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1561 is_paused = true;
1562
1563 goto _exit;
1564 }
1565
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001566 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001567 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001568 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001569 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001570 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001571 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001572 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001573 } else {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001574 dev_err(&d40c->chan.dev->device,
1575 "[%s] Unknown direction\n", __func__);
1576 goto _exit;
1577 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001578
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001579 status = (status & D40_EVENTLINE_MASK(event)) >>
1580 D40_EVENTLINE_POS(event);
1581
1582 if (status != D40_DMA_RUN)
1583 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001584_exit:
1585 spin_unlock_irqrestore(&d40c->lock, flags);
1586 return is_paused;
1587
1588}
1589
1590
Linus Walleij8d318a52010-03-30 15:33:42 +02001591static u32 stedma40_residue(struct dma_chan *chan)
1592{
1593 struct d40_chan *d40c =
1594 container_of(chan, struct d40_chan, chan);
1595 u32 bytes_left;
1596 unsigned long flags;
1597
1598 spin_lock_irqsave(&d40c->lock, flags);
1599 bytes_left = d40_residue(d40c);
1600 spin_unlock_irqrestore(&d40c->lock, flags);
1601
1602 return bytes_left;
1603}
1604
Linus Walleij8d318a52010-03-30 15:33:42 +02001605struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
1606 struct scatterlist *sgl_dst,
1607 struct scatterlist *sgl_src,
1608 unsigned int sgl_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001609 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001610{
1611 int res;
1612 struct d40_desc *d40d;
1613 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1614 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001615 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001616
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001617 if (d40c->phy_chan == NULL) {
1618 dev_err(&d40c->chan.dev->device,
1619 "[%s] Unallocated channel.\n", __func__);
1620 return ERR_PTR(-EINVAL);
1621 }
1622
Jonas Aaberg2a614342010-06-20 21:25:24 +00001623 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001624 d40d = d40_desc_get(d40c);
1625
1626 if (d40d == NULL)
1627 goto err;
1628
Per Forlind49278e2010-12-20 18:31:38 +01001629 d40d->lli_len = d40_sg_2_dmalen(sgl_dst, sgl_len,
1630 d40c->dma_cfg.src_info.data_width,
1631 d40c->dma_cfg.dst_info.data_width);
1632 if (d40d->lli_len < 0) {
1633 dev_err(&d40c->chan.dev->device,
1634 "[%s] Unaligned size\n", __func__);
1635 goto err;
1636 }
1637
Jonas Aaberg698e4732010-08-09 12:08:56 +00001638 d40d->lli_current = 0;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001639 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001640
1641 if (d40c->log_num != D40_PHY_CHAN) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001642
Per Forlind49278e2010-12-20 18:31:38 +01001643 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001644 dev_err(&d40c->chan.dev->device,
1645 "[%s] Out of memory\n", __func__);
1646 goto err;
1647 }
1648
Jonas Aaberg698e4732010-08-09 12:08:56 +00001649 (void) d40_log_sg_to_lli(sgl_src,
Linus Walleij8d318a52010-03-30 15:33:42 +02001650 sgl_len,
1651 d40d->lli_log.src,
1652 d40c->log_def.lcsp1,
Per Forlind49278e2010-12-20 18:31:38 +01001653 d40c->dma_cfg.src_info.data_width,
1654 d40c->dma_cfg.dst_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001655
Jonas Aaberg698e4732010-08-09 12:08:56 +00001656 (void) d40_log_sg_to_lli(sgl_dst,
Linus Walleij8d318a52010-03-30 15:33:42 +02001657 sgl_len,
1658 d40d->lli_log.dst,
1659 d40c->log_def.lcsp3,
Per Forlind49278e2010-12-20 18:31:38 +01001660 d40c->dma_cfg.dst_info.data_width,
1661 d40c->dma_cfg.src_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001662 } else {
Per Forlind49278e2010-12-20 18:31:38 +01001663 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001664 dev_err(&d40c->chan.dev->device,
1665 "[%s] Out of memory\n", __func__);
1666 goto err;
1667 }
1668
1669 res = d40_phy_sg_to_lli(sgl_src,
1670 sgl_len,
1671 0,
1672 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001673 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02001674 d40c->src_def_cfg,
1675 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001676 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001677 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001678
1679 if (res < 0)
1680 goto err;
1681
1682 res = d40_phy_sg_to_lli(sgl_dst,
1683 sgl_len,
1684 0,
1685 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001686 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02001687 d40c->dst_def_cfg,
1688 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001689 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001690 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001691
1692 if (res < 0)
1693 goto err;
1694
1695 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1696 d40d->lli_pool.size, DMA_TO_DEVICE);
1697 }
1698
1699 dma_async_tx_descriptor_init(&d40d->txd, chan);
1700
1701 d40d->txd.tx_submit = d40_tx_submit;
1702
Jonas Aaberg2a614342010-06-20 21:25:24 +00001703 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001704
1705 return &d40d->txd;
1706err:
Rabin Vincent819504f2010-10-06 08:20:38 +00001707 if (d40d)
1708 d40_desc_free(d40c, d40d);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001709 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001710 return NULL;
1711}
1712EXPORT_SYMBOL(stedma40_memcpy_sg);
1713
1714bool stedma40_filter(struct dma_chan *chan, void *data)
1715{
1716 struct stedma40_chan_cfg *info = data;
1717 struct d40_chan *d40c =
1718 container_of(chan, struct d40_chan, chan);
1719 int err;
1720
1721 if (data) {
1722 err = d40_validate_conf(d40c, info);
1723 if (!err)
1724 d40c->dma_cfg = *info;
1725 } else
1726 err = d40_config_memcpy(d40c);
1727
Rabin Vincentce2ca122010-10-12 13:00:49 +00001728 if (!err)
1729 d40c->configured = true;
1730
Linus Walleij8d318a52010-03-30 15:33:42 +02001731 return err == 0;
1732}
1733EXPORT_SYMBOL(stedma40_filter);
1734
1735/* DMA ENGINE functions */
1736static int d40_alloc_chan_resources(struct dma_chan *chan)
1737{
1738 int err;
1739 unsigned long flags;
1740 struct d40_chan *d40c =
1741 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001742 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001743 spin_lock_irqsave(&d40c->lock, flags);
1744
1745 d40c->completed = chan->cookie = 1;
1746
Rabin Vincentce2ca122010-10-12 13:00:49 +00001747 /* If no dma configuration is set use default configuration (memcpy) */
1748 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001749 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001750 if (err) {
1751 dev_err(&d40c->chan.dev->device,
1752 "[%s] Failed to configure memcpy channel\n",
1753 __func__);
1754 goto fail;
1755 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001756 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001757 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001758
1759 err = d40_allocate_channel(d40c);
1760 if (err) {
1761 dev_err(&d40c->chan.dev->device,
1762 "[%s] Failed to allocate channel\n", __func__);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001763 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001764 }
1765
Linus Walleijef1872e2010-06-20 21:24:52 +00001766 /* Fill in basic CFG register values */
1767 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
1768 &d40c->dst_def_cfg, d40c->log_num != D40_PHY_CHAN);
1769
1770 if (d40c->log_num != D40_PHY_CHAN) {
1771 d40_log_cfg(&d40c->dma_cfg,
1772 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1773
1774 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1775 d40c->lcpa = d40c->base->lcpa_base +
1776 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1777 else
1778 d40c->lcpa = d40c->base->lcpa_base +
1779 d40c->dma_cfg.dst_dev_type *
1780 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1781 }
1782
1783 /*
1784 * Only write channel configuration to the DMA if the physical
1785 * resource is free. In case of multiple logical channels
1786 * on the same physical resource, only the first write is necessary.
1787 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001788 if (is_free_phy)
1789 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001790fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001791 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001792 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001793}
1794
1795static void d40_free_chan_resources(struct dma_chan *chan)
1796{
1797 struct d40_chan *d40c =
1798 container_of(chan, struct d40_chan, chan);
1799 int err;
1800 unsigned long flags;
1801
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001802 if (d40c->phy_chan == NULL) {
1803 dev_err(&d40c->chan.dev->device,
1804 "[%s] Cannot free unallocated channel\n", __func__);
1805 return;
1806 }
1807
1808
Linus Walleij8d318a52010-03-30 15:33:42 +02001809 spin_lock_irqsave(&d40c->lock, flags);
1810
1811 err = d40_free_dma(d40c);
1812
1813 if (err)
1814 dev_err(&d40c->chan.dev->device,
1815 "[%s] Failed to free channel\n", __func__);
1816 spin_unlock_irqrestore(&d40c->lock, flags);
1817}
1818
1819static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1820 dma_addr_t dst,
1821 dma_addr_t src,
1822 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001823 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001824{
1825 struct d40_desc *d40d;
1826 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1827 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001828 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001829
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001830 if (d40c->phy_chan == NULL) {
1831 dev_err(&d40c->chan.dev->device,
1832 "[%s] Channel is not allocated.\n", __func__);
1833 return ERR_PTR(-EINVAL);
1834 }
1835
Jonas Aaberg2a614342010-06-20 21:25:24 +00001836 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001837 d40d = d40_desc_get(d40c);
1838
1839 if (d40d == NULL) {
1840 dev_err(&d40c->chan.dev->device,
1841 "[%s] Descriptor is NULL\n", __func__);
1842 goto err;
1843 }
1844
Jonas Aaberg2a614342010-06-20 21:25:24 +00001845 d40d->txd.flags = dma_flags;
Per Forlind49278e2010-12-20 18:31:38 +01001846 d40d->lli_len = d40_size_2_dmalen(size,
1847 d40c->dma_cfg.src_info.data_width,
1848 d40c->dma_cfg.dst_info.data_width);
1849 if (d40d->lli_len < 0) {
1850 dev_err(&d40c->chan.dev->device,
1851 "[%s] Unaligned size\n", __func__);
1852 goto err;
1853 }
1854
Linus Walleij8d318a52010-03-30 15:33:42 +02001855
1856 dma_async_tx_descriptor_init(&d40d->txd, chan);
1857
1858 d40d->txd.tx_submit = d40_tx_submit;
1859
1860 if (d40c->log_num != D40_PHY_CHAN) {
1861
Per Forlind49278e2010-12-20 18:31:38 +01001862 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001863 dev_err(&d40c->chan.dev->device,
1864 "[%s] Out of memory\n", __func__);
1865 goto err;
1866 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00001867 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001868
Per Forlind49278e2010-12-20 18:31:38 +01001869 if (d40_log_buf_to_lli(d40d->lli_log.src,
1870 src,
1871 size,
1872 d40c->log_def.lcsp1,
1873 d40c->dma_cfg.src_info.data_width,
1874 d40c->dma_cfg.dst_info.data_width,
1875 true) == NULL)
1876 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001877
Per Forlind49278e2010-12-20 18:31:38 +01001878 if (d40_log_buf_to_lli(d40d->lli_log.dst,
1879 dst,
1880 size,
1881 d40c->log_def.lcsp3,
1882 d40c->dma_cfg.dst_info.data_width,
1883 d40c->dma_cfg.src_info.data_width,
1884 true) == NULL)
1885 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001886
1887 } else {
1888
Per Forlind49278e2010-12-20 18:31:38 +01001889 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001890 dev_err(&d40c->chan.dev->device,
1891 "[%s] Out of memory\n", __func__);
1892 goto err;
1893 }
1894
Per Forlind49278e2010-12-20 18:31:38 +01001895 if (d40_phy_buf_to_lli(d40d->lli_phy.src,
Linus Walleij8d318a52010-03-30 15:33:42 +02001896 src,
1897 size,
1898 d40c->dma_cfg.src_info.psize,
1899 0,
1900 d40c->src_def_cfg,
1901 true,
1902 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001903 d40c->dma_cfg.dst_info.data_width,
1904 false) == NULL)
1905 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001906
Per Forlind49278e2010-12-20 18:31:38 +01001907 if (d40_phy_buf_to_lli(d40d->lli_phy.dst,
Linus Walleij8d318a52010-03-30 15:33:42 +02001908 dst,
1909 size,
1910 d40c->dma_cfg.dst_info.psize,
1911 0,
1912 d40c->dst_def_cfg,
1913 true,
1914 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01001915 d40c->dma_cfg.src_info.data_width,
1916 false) == NULL)
1917 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001918
1919 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1920 d40d->lli_pool.size, DMA_TO_DEVICE);
1921 }
1922
Jonas Aaberg2a614342010-06-20 21:25:24 +00001923 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001924 return &d40d->txd;
1925
Linus Walleij8d318a52010-03-30 15:33:42 +02001926err:
Rabin Vincent819504f2010-10-06 08:20:38 +00001927 if (d40d)
1928 d40_desc_free(d40c, d40d);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001929 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001930 return NULL;
1931}
1932
Ira Snyder0d688662010-09-30 11:46:47 +00001933static struct dma_async_tx_descriptor *
1934d40_prep_sg(struct dma_chan *chan,
1935 struct scatterlist *dst_sg, unsigned int dst_nents,
1936 struct scatterlist *src_sg, unsigned int src_nents,
1937 unsigned long dma_flags)
1938{
1939 if (dst_nents != src_nents)
1940 return NULL;
1941
1942 return stedma40_memcpy_sg(chan, dst_sg, src_sg, dst_nents, dma_flags);
1943}
1944
Linus Walleij8d318a52010-03-30 15:33:42 +02001945static int d40_prep_slave_sg_log(struct d40_desc *d40d,
1946 struct d40_chan *d40c,
1947 struct scatterlist *sgl,
1948 unsigned int sg_len,
1949 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001950 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001951{
1952 dma_addr_t dev_addr = 0;
1953 int total_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001954
Per Forlind49278e2010-12-20 18:31:38 +01001955 d40d->lli_len = d40_sg_2_dmalen(sgl, sg_len,
1956 d40c->dma_cfg.src_info.data_width,
1957 d40c->dma_cfg.dst_info.data_width);
1958 if (d40d->lli_len < 0) {
1959 dev_err(&d40c->chan.dev->device,
1960 "[%s] Unaligned size\n", __func__);
1961 return -EINVAL;
1962 }
1963
1964 if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001965 dev_err(&d40c->chan.dev->device,
1966 "[%s] Out of memory\n", __func__);
1967 return -ENOMEM;
1968 }
1969
Jonas Aaberg698e4732010-08-09 12:08:56 +00001970 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001971
Jonas Aaberg2a614342010-06-20 21:25:24 +00001972 if (direction == DMA_FROM_DEVICE)
Linus Walleij95e14002010-08-04 13:37:45 +02001973 if (d40c->runtime_addr)
1974 dev_addr = d40c->runtime_addr;
1975 else
1976 dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Jonas Aaberg2a614342010-06-20 21:25:24 +00001977 else if (direction == DMA_TO_DEVICE)
Linus Walleij95e14002010-08-04 13:37:45 +02001978 if (d40c->runtime_addr)
1979 dev_addr = d40c->runtime_addr;
1980 else
1981 dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
1982
Jonas Aaberg2a614342010-06-20 21:25:24 +00001983 else
Linus Walleij8d318a52010-03-30 15:33:42 +02001984 return -EINVAL;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001985
Jonas Aaberg698e4732010-08-09 12:08:56 +00001986 total_size = d40_log_sg_to_dev(sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001987 &d40d->lli_log,
1988 &d40c->log_def,
1989 d40c->dma_cfg.src_info.data_width,
1990 d40c->dma_cfg.dst_info.data_width,
1991 direction,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001992 dev_addr);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001993
Linus Walleij8d318a52010-03-30 15:33:42 +02001994 if (total_size < 0)
1995 return -EINVAL;
1996
1997 return 0;
1998}
1999
2000static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
2001 struct d40_chan *d40c,
2002 struct scatterlist *sgl,
2003 unsigned int sgl_len,
2004 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002005 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002006{
2007 dma_addr_t src_dev_addr;
2008 dma_addr_t dst_dev_addr;
2009 int res;
2010
Per Forlind49278e2010-12-20 18:31:38 +01002011 d40d->lli_len = d40_sg_2_dmalen(sgl, sgl_len,
2012 d40c->dma_cfg.src_info.data_width,
2013 d40c->dma_cfg.dst_info.data_width);
2014 if (d40d->lli_len < 0) {
2015 dev_err(&d40c->chan.dev->device,
2016 "[%s] Unaligned size\n", __func__);
2017 return -EINVAL;
2018 }
2019
2020 if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002021 dev_err(&d40c->chan.dev->device,
2022 "[%s] Out of memory\n", __func__);
2023 return -ENOMEM;
2024 }
2025
Jonas Aaberg698e4732010-08-09 12:08:56 +00002026 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002027
2028 if (direction == DMA_FROM_DEVICE) {
2029 dst_dev_addr = 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002030 if (d40c->runtime_addr)
2031 src_dev_addr = d40c->runtime_addr;
2032 else
2033 src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002034 } else if (direction == DMA_TO_DEVICE) {
Linus Walleij95e14002010-08-04 13:37:45 +02002035 if (d40c->runtime_addr)
2036 dst_dev_addr = d40c->runtime_addr;
2037 else
2038 dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002039 src_dev_addr = 0;
2040 } else
2041 return -EINVAL;
2042
2043 res = d40_phy_sg_to_lli(sgl,
2044 sgl_len,
2045 src_dev_addr,
2046 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002047 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02002048 d40c->src_def_cfg,
2049 d40c->dma_cfg.src_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01002050 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002051 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002052 if (res < 0)
2053 return res;
2054
2055 res = d40_phy_sg_to_lli(sgl,
2056 sgl_len,
2057 dst_dev_addr,
2058 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002059 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02002060 d40c->dst_def_cfg,
2061 d40c->dma_cfg.dst_info.data_width,
Per Forlind49278e2010-12-20 18:31:38 +01002062 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002063 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002064 if (res < 0)
2065 return res;
2066
2067 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
2068 d40d->lli_pool.size, DMA_TO_DEVICE);
2069 return 0;
2070}
2071
2072static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2073 struct scatterlist *sgl,
2074 unsigned int sg_len,
2075 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002076 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002077{
2078 struct d40_desc *d40d;
2079 struct d40_chan *d40c = container_of(chan, struct d40_chan,
2080 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00002081 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002082 int err;
2083
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002084 if (d40c->phy_chan == NULL) {
2085 dev_err(&d40c->chan.dev->device,
2086 "[%s] Cannot prepare unallocated channel\n", __func__);
2087 return ERR_PTR(-EINVAL);
2088 }
2089
Jonas Aaberg2a614342010-06-20 21:25:24 +00002090 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002091 d40d = d40_desc_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002092
2093 if (d40d == NULL)
Rabin Vincent819504f2010-10-06 08:20:38 +00002094 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002095
Linus Walleij8d318a52010-03-30 15:33:42 +02002096 if (d40c->log_num != D40_PHY_CHAN)
2097 err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002098 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002099 else
2100 err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002101 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002102 if (err) {
2103 dev_err(&d40c->chan.dev->device,
2104 "[%s] Failed to prepare %s slave sg job: %d\n",
2105 __func__,
2106 d40c->log_num != D40_PHY_CHAN ? "log" : "phy", err);
Rabin Vincent819504f2010-10-06 08:20:38 +00002107 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002108 }
2109
Jonas Aaberg2a614342010-06-20 21:25:24 +00002110 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002111
2112 dma_async_tx_descriptor_init(&d40d->txd, chan);
2113
2114 d40d->txd.tx_submit = d40_tx_submit;
2115
Rabin Vincent819504f2010-10-06 08:20:38 +00002116 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002117 return &d40d->txd;
Rabin Vincent819504f2010-10-06 08:20:38 +00002118
2119err:
2120 if (d40d)
2121 d40_desc_free(d40c, d40d);
2122 spin_unlock_irqrestore(&d40c->lock, flags);
2123 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02002124}
2125
2126static enum dma_status d40_tx_status(struct dma_chan *chan,
2127 dma_cookie_t cookie,
2128 struct dma_tx_state *txstate)
2129{
2130 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2131 dma_cookie_t last_used;
2132 dma_cookie_t last_complete;
2133 int ret;
2134
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002135 if (d40c->phy_chan == NULL) {
2136 dev_err(&d40c->chan.dev->device,
2137 "[%s] Cannot read status of unallocated channel\n",
2138 __func__);
2139 return -EINVAL;
2140 }
2141
Linus Walleij8d318a52010-03-30 15:33:42 +02002142 last_complete = d40c->completed;
2143 last_used = chan->cookie;
2144
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002145 if (d40_is_paused(d40c))
2146 ret = DMA_PAUSED;
2147 else
2148 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002149
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002150 dma_set_tx_state(txstate, last_complete, last_used,
2151 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002152
2153 return ret;
2154}
2155
2156static void d40_issue_pending(struct dma_chan *chan)
2157{
2158 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2159 unsigned long flags;
2160
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002161 if (d40c->phy_chan == NULL) {
2162 dev_err(&d40c->chan.dev->device,
2163 "[%s] Channel is not allocated!\n", __func__);
2164 return;
2165 }
2166
Linus Walleij8d318a52010-03-30 15:33:42 +02002167 spin_lock_irqsave(&d40c->lock, flags);
2168
2169 /* Busy means that pending jobs are already being processed */
2170 if (!d40c->busy)
2171 (void) d40_queue_start(d40c);
2172
2173 spin_unlock_irqrestore(&d40c->lock, flags);
2174}
2175
Linus Walleij95e14002010-08-04 13:37:45 +02002176/* Runtime reconfiguration extension */
2177static void d40_set_runtime_config(struct dma_chan *chan,
2178 struct dma_slave_config *config)
2179{
2180 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2181 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2182 enum dma_slave_buswidth config_addr_width;
2183 dma_addr_t config_addr;
2184 u32 config_maxburst;
2185 enum stedma40_periph_data_width addr_width;
2186 int psize;
2187
2188 if (config->direction == DMA_FROM_DEVICE) {
2189 dma_addr_t dev_addr_rx =
2190 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2191
2192 config_addr = config->src_addr;
2193 if (dev_addr_rx)
2194 dev_dbg(d40c->base->dev,
2195 "channel has a pre-wired RX address %08x "
2196 "overriding with %08x\n",
2197 dev_addr_rx, config_addr);
2198 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2199 dev_dbg(d40c->base->dev,
2200 "channel was not configured for peripheral "
2201 "to memory transfer (%d) overriding\n",
2202 cfg->dir);
2203 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2204
2205 config_addr_width = config->src_addr_width;
2206 config_maxburst = config->src_maxburst;
2207
2208 } else if (config->direction == DMA_TO_DEVICE) {
2209 dma_addr_t dev_addr_tx =
2210 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2211
2212 config_addr = config->dst_addr;
2213 if (dev_addr_tx)
2214 dev_dbg(d40c->base->dev,
2215 "channel has a pre-wired TX address %08x "
2216 "overriding with %08x\n",
2217 dev_addr_tx, config_addr);
2218 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2219 dev_dbg(d40c->base->dev,
2220 "channel was not configured for memory "
2221 "to peripheral transfer (%d) overriding\n",
2222 cfg->dir);
2223 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2224
2225 config_addr_width = config->dst_addr_width;
2226 config_maxburst = config->dst_maxburst;
2227
2228 } else {
2229 dev_err(d40c->base->dev,
2230 "unrecognized channel direction %d\n",
2231 config->direction);
2232 return;
2233 }
2234
2235 switch (config_addr_width) {
2236 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2237 addr_width = STEDMA40_BYTE_WIDTH;
2238 break;
2239 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2240 addr_width = STEDMA40_HALFWORD_WIDTH;
2241 break;
2242 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2243 addr_width = STEDMA40_WORD_WIDTH;
2244 break;
2245 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2246 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2247 break;
2248 default:
2249 dev_err(d40c->base->dev,
2250 "illegal peripheral address width "
2251 "requested (%d)\n",
2252 config->src_addr_width);
2253 return;
2254 }
2255
Per Forlina59670a2010-10-06 09:05:27 +00002256 if (d40c->log_num != D40_PHY_CHAN) {
2257 if (config_maxburst >= 16)
2258 psize = STEDMA40_PSIZE_LOG_16;
2259 else if (config_maxburst >= 8)
2260 psize = STEDMA40_PSIZE_LOG_8;
2261 else if (config_maxburst >= 4)
2262 psize = STEDMA40_PSIZE_LOG_4;
2263 else
2264 psize = STEDMA40_PSIZE_LOG_1;
2265 } else {
2266 if (config_maxburst >= 16)
2267 psize = STEDMA40_PSIZE_PHY_16;
2268 else if (config_maxburst >= 8)
2269 psize = STEDMA40_PSIZE_PHY_8;
2270 else if (config_maxburst >= 4)
2271 psize = STEDMA40_PSIZE_PHY_4;
Per Forlind49278e2010-12-20 18:31:38 +01002272 else if (config_maxburst >= 2)
2273 psize = STEDMA40_PSIZE_PHY_2;
Per Forlina59670a2010-10-06 09:05:27 +00002274 else
2275 psize = STEDMA40_PSIZE_PHY_1;
2276 }
Linus Walleij95e14002010-08-04 13:37:45 +02002277
2278 /* Set up all the endpoint configs */
2279 cfg->src_info.data_width = addr_width;
2280 cfg->src_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002281 cfg->src_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002282 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2283 cfg->dst_info.data_width = addr_width;
2284 cfg->dst_info.psize = psize;
Rabin Vincent51f5d742010-10-12 13:00:54 +00002285 cfg->dst_info.big_endian = false;
Linus Walleij95e14002010-08-04 13:37:45 +02002286 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2287
Per Forlina59670a2010-10-06 09:05:27 +00002288 /* Fill in register values */
2289 if (d40c->log_num != D40_PHY_CHAN)
2290 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2291 else
2292 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2293 &d40c->dst_def_cfg, false);
2294
Linus Walleij95e14002010-08-04 13:37:45 +02002295 /* These settings will take precedence later */
2296 d40c->runtime_addr = config_addr;
2297 d40c->runtime_direction = config->direction;
2298 dev_dbg(d40c->base->dev,
2299 "configured channel %s for %s, data width %d, "
2300 "maxburst %d bytes, LE, no flow control\n",
2301 dma_chan_name(chan),
2302 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2303 config_addr_width,
2304 config_maxburst);
2305}
2306
Linus Walleij05827632010-05-17 16:30:42 -07002307static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2308 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002309{
2310 unsigned long flags;
2311 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2312
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002313 if (d40c->phy_chan == NULL) {
2314 dev_err(&d40c->chan.dev->device,
2315 "[%s] Channel is not allocated!\n", __func__);
2316 return -EINVAL;
2317 }
2318
Linus Walleij8d318a52010-03-30 15:33:42 +02002319 switch (cmd) {
2320 case DMA_TERMINATE_ALL:
2321 spin_lock_irqsave(&d40c->lock, flags);
2322 d40_term_all(d40c);
2323 spin_unlock_irqrestore(&d40c->lock, flags);
2324 return 0;
2325 case DMA_PAUSE:
2326 return d40_pause(chan);
2327 case DMA_RESUME:
2328 return d40_resume(chan);
Linus Walleij95e14002010-08-04 13:37:45 +02002329 case DMA_SLAVE_CONFIG:
2330 d40_set_runtime_config(chan,
2331 (struct dma_slave_config *) arg);
2332 return 0;
2333 default:
2334 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002335 }
2336
2337 /* Other commands are unimplemented */
2338 return -ENXIO;
2339}
2340
2341/* Initialization functions */
2342
2343static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2344 struct d40_chan *chans, int offset,
2345 int num_chans)
2346{
2347 int i = 0;
2348 struct d40_chan *d40c;
2349
2350 INIT_LIST_HEAD(&dma->channels);
2351
2352 for (i = offset; i < offset + num_chans; i++) {
2353 d40c = &chans[i];
2354 d40c->base = base;
2355 d40c->chan.device = dma;
2356
Linus Walleij8d318a52010-03-30 15:33:42 +02002357 spin_lock_init(&d40c->lock);
2358
2359 d40c->log_num = D40_PHY_CHAN;
2360
Linus Walleij8d318a52010-03-30 15:33:42 +02002361 INIT_LIST_HEAD(&d40c->active);
2362 INIT_LIST_HEAD(&d40c->queue);
2363 INIT_LIST_HEAD(&d40c->client);
2364
Linus Walleij8d318a52010-03-30 15:33:42 +02002365 tasklet_init(&d40c->tasklet, dma_tasklet,
2366 (unsigned long) d40c);
2367
2368 list_add_tail(&d40c->chan.device_node,
2369 &dma->channels);
2370 }
2371}
2372
2373static int __init d40_dmaengine_init(struct d40_base *base,
2374 int num_reserved_chans)
2375{
2376 int err ;
2377
2378 d40_chan_init(base, &base->dma_slave, base->log_chans,
2379 0, base->num_log_chans);
2380
2381 dma_cap_zero(base->dma_slave.cap_mask);
2382 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2383
2384 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2385 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2386 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002387 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002388 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2389 base->dma_slave.device_tx_status = d40_tx_status;
2390 base->dma_slave.device_issue_pending = d40_issue_pending;
2391 base->dma_slave.device_control = d40_control;
2392 base->dma_slave.dev = base->dev;
2393
2394 err = dma_async_device_register(&base->dma_slave);
2395
2396 if (err) {
2397 dev_err(base->dev,
2398 "[%s] Failed to register slave channels\n",
2399 __func__);
2400 goto failure1;
2401 }
2402
2403 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2404 base->num_log_chans, base->plat_data->memcpy_len);
2405
2406 dma_cap_zero(base->dma_memcpy.cap_mask);
2407 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002408 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002409
2410 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2411 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2412 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002413 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002414 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2415 base->dma_memcpy.device_tx_status = d40_tx_status;
2416 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2417 base->dma_memcpy.device_control = d40_control;
2418 base->dma_memcpy.dev = base->dev;
2419 /*
2420 * This controller can only access address at even
2421 * 32bit boundaries, i.e. 2^2
2422 */
2423 base->dma_memcpy.copy_align = 2;
2424
2425 err = dma_async_device_register(&base->dma_memcpy);
2426
2427 if (err) {
2428 dev_err(base->dev,
2429 "[%s] Failed to regsiter memcpy only channels\n",
2430 __func__);
2431 goto failure2;
2432 }
2433
2434 d40_chan_init(base, &base->dma_both, base->phy_chans,
2435 0, num_reserved_chans);
2436
2437 dma_cap_zero(base->dma_both.cap_mask);
2438 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2439 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Ira Snyder0d688662010-09-30 11:46:47 +00002440 dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002441
2442 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2443 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2444 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
Ira Snyder0d688662010-09-30 11:46:47 +00002445 base->dma_slave.device_prep_dma_sg = d40_prep_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002446 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2447 base->dma_both.device_tx_status = d40_tx_status;
2448 base->dma_both.device_issue_pending = d40_issue_pending;
2449 base->dma_both.device_control = d40_control;
2450 base->dma_both.dev = base->dev;
2451 base->dma_both.copy_align = 2;
2452 err = dma_async_device_register(&base->dma_both);
2453
2454 if (err) {
2455 dev_err(base->dev,
2456 "[%s] Failed to register logical and physical capable channels\n",
2457 __func__);
2458 goto failure3;
2459 }
2460 return 0;
2461failure3:
2462 dma_async_device_unregister(&base->dma_memcpy);
2463failure2:
2464 dma_async_device_unregister(&base->dma_slave);
2465failure1:
2466 return err;
2467}
2468
2469/* Initialization functions. */
2470
2471static int __init d40_phy_res_init(struct d40_base *base)
2472{
2473 int i;
2474 int num_phy_chans_avail = 0;
2475 u32 val[2];
2476 int odd_even_bit = -2;
2477
2478 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2479 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2480
2481 for (i = 0; i < base->num_phy_chans; i++) {
2482 base->phy_res[i].num = i;
2483 odd_even_bit += 2 * ((i % 2) == 0);
2484 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2485 /* Mark security only channels as occupied */
2486 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2487 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2488 } else {
2489 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2490 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2491 num_phy_chans_avail++;
2492 }
2493 spin_lock_init(&base->phy_res[i].lock);
2494 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002495
2496 /* Mark disabled channels as occupied */
2497 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002498 int chan = base->plat_data->disabled_channels[i];
2499
2500 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2501 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2502 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002503 }
2504
Linus Walleij8d318a52010-03-30 15:33:42 +02002505 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2506 num_phy_chans_avail, base->num_phy_chans);
2507
2508 /* Verify settings extended vs standard */
2509 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2510
2511 for (i = 0; i < base->num_phy_chans; i++) {
2512
2513 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2514 (val[0] & 0x3) != 1)
2515 dev_info(base->dev,
2516 "[%s] INFO: channel %d is misconfigured (%d)\n",
2517 __func__, i, val[0] & 0x3);
2518
2519 val[0] = val[0] >> 2;
2520 }
2521
2522 return num_phy_chans_avail;
2523}
2524
2525static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2526{
2527 static const struct d40_reg_val dma_id_regs[] = {
2528 /* Peripheral Id */
2529 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2530 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2531 /*
2532 * D40_DREG_PERIPHID2 Depends on HW revision:
2533 * MOP500/HREF ED has 0x0008,
2534 * ? has 0x0018,
2535 * HREF V1 has 0x0028
2536 */
2537 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2538
2539 /* PCell Id */
2540 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2541 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2542 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2543 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2544 };
2545 struct stedma40_platform_data *plat_data;
2546 struct clk *clk = NULL;
2547 void __iomem *virtbase = NULL;
2548 struct resource *res = NULL;
2549 struct d40_base *base = NULL;
2550 int num_log_chans = 0;
2551 int num_phy_chans;
2552 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002553 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002554 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002555
2556 clk = clk_get(&pdev->dev, NULL);
2557
2558 if (IS_ERR(clk)) {
2559 dev_err(&pdev->dev, "[%s] No matching clock found\n",
2560 __func__);
2561 goto failure;
2562 }
2563
2564 clk_enable(clk);
2565
2566 /* Get IO for DMAC base address */
2567 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2568 if (!res)
2569 goto failure;
2570
2571 if (request_mem_region(res->start, resource_size(res),
2572 D40_NAME " I/O base") == NULL)
2573 goto failure;
2574
2575 virtbase = ioremap(res->start, resource_size(res));
2576 if (!virtbase)
2577 goto failure;
2578
2579 /* HW version check */
2580 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2581 if (dma_id_regs[i].val !=
2582 readl(virtbase + dma_id_regs[i].reg)) {
2583 dev_err(&pdev->dev,
2584 "[%s] Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
2585 __func__,
2586 dma_id_regs[i].val,
2587 dma_id_regs[i].reg,
2588 readl(virtbase + dma_id_regs[i].reg));
2589 goto failure;
2590 }
2591 }
2592
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002593 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002594 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002595
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002596 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2597 D40_HW_DESIGNER) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002598 dev_err(&pdev->dev,
2599 "[%s] Unknown designer! Got %x wanted %x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002600 __func__, val & D40_DREG_PERIPHID2_DESIGNER_MASK,
2601 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002602 goto failure;
2603 }
2604
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002605 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2606 D40_DREG_PERIPHID2_REV_POS;
2607
Linus Walleij8d318a52010-03-30 15:33:42 +02002608 /* The number of physical channels on this HW */
2609 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2610
2611 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002612 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002613
2614 plat_data = pdev->dev.platform_data;
2615
2616 /* Count the number of logical channels in use */
2617 for (i = 0; i < plat_data->dev_len; i++)
2618 if (plat_data->dev_rx[i] != 0)
2619 num_log_chans++;
2620
2621 for (i = 0; i < plat_data->dev_len; i++)
2622 if (plat_data->dev_tx[i] != 0)
2623 num_log_chans++;
2624
2625 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2626 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2627 sizeof(struct d40_chan), GFP_KERNEL);
2628
2629 if (base == NULL) {
2630 dev_err(&pdev->dev, "[%s] Out of memory\n", __func__);
2631 goto failure;
2632 }
2633
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002634 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002635 base->clk = clk;
2636 base->num_phy_chans = num_phy_chans;
2637 base->num_log_chans = num_log_chans;
2638 base->phy_start = res->start;
2639 base->phy_size = resource_size(res);
2640 base->virtbase = virtbase;
2641 base->plat_data = plat_data;
2642 base->dev = &pdev->dev;
2643 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2644 base->log_chans = &base->phy_chans[num_phy_chans];
2645
2646 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2647 GFP_KERNEL);
2648 if (!base->phy_res)
2649 goto failure;
2650
2651 base->lookup_phy_chans = kzalloc(num_phy_chans *
2652 sizeof(struct d40_chan *),
2653 GFP_KERNEL);
2654 if (!base->lookup_phy_chans)
2655 goto failure;
2656
2657 if (num_log_chans + plat_data->memcpy_len) {
2658 /*
2659 * The max number of logical channels are event lines for all
2660 * src devices and dst devices
2661 */
2662 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2663 sizeof(struct d40_chan *),
2664 GFP_KERNEL);
2665 if (!base->lookup_log_chans)
2666 goto failure;
2667 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002668
2669 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2670 sizeof(struct d40_desc *) *
2671 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002672 GFP_KERNEL);
2673 if (!base->lcla_pool.alloc_map)
2674 goto failure;
2675
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002676 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2677 0, SLAB_HWCACHE_ALIGN,
2678 NULL);
2679 if (base->desc_slab == NULL)
2680 goto failure;
2681
Linus Walleij8d318a52010-03-30 15:33:42 +02002682 return base;
2683
2684failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002685 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002686 clk_disable(clk);
2687 clk_put(clk);
2688 }
2689 if (virtbase)
2690 iounmap(virtbase);
2691 if (res)
2692 release_mem_region(res->start,
2693 resource_size(res));
2694 if (virtbase)
2695 iounmap(virtbase);
2696
2697 if (base) {
2698 kfree(base->lcla_pool.alloc_map);
2699 kfree(base->lookup_log_chans);
2700 kfree(base->lookup_phy_chans);
2701 kfree(base->phy_res);
2702 kfree(base);
2703 }
2704
2705 return NULL;
2706}
2707
2708static void __init d40_hw_init(struct d40_base *base)
2709{
2710
2711 static const struct d40_reg_val dma_init_reg[] = {
2712 /* Clock every part of the DMA block from start */
2713 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2714
2715 /* Interrupts on all logical channels */
2716 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2717 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2718 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2719 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2720 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2721 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2722 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2723 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2724 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2725 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2726 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2727 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2728 };
2729 int i;
2730 u32 prmseo[2] = {0, 0};
2731 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2732 u32 pcmis = 0;
2733 u32 pcicr = 0;
2734
2735 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2736 writel(dma_init_reg[i].val,
2737 base->virtbase + dma_init_reg[i].reg);
2738
2739 /* Configure all our dma channels to default settings */
2740 for (i = 0; i < base->num_phy_chans; i++) {
2741
2742 activeo[i % 2] = activeo[i % 2] << 2;
2743
2744 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2745 == D40_ALLOC_PHY) {
2746 activeo[i % 2] |= 3;
2747 continue;
2748 }
2749
2750 /* Enable interrupt # */
2751 pcmis = (pcmis << 1) | 1;
2752
2753 /* Clear interrupt # */
2754 pcicr = (pcicr << 1) | 1;
2755
2756 /* Set channel to physical mode */
2757 prmseo[i % 2] = prmseo[i % 2] << 2;
2758 prmseo[i % 2] |= 1;
2759
2760 }
2761
2762 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2763 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2764 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2765 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2766
2767 /* Write which interrupt to enable */
2768 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2769
2770 /* Write which interrupt to clear */
2771 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2772
2773}
2774
Linus Walleij508849a2010-06-20 21:26:07 +00002775static int __init d40_lcla_allocate(struct d40_base *base)
2776{
2777 unsigned long *page_list;
2778 int i, j;
2779 int ret = 0;
2780
2781 /*
2782 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2783 * To full fill this hardware requirement without wasting 256 kb
2784 * we allocate pages until we get an aligned one.
2785 */
2786 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2787 GFP_KERNEL);
2788
2789 if (!page_list) {
2790 ret = -ENOMEM;
2791 goto failure;
2792 }
2793
2794 /* Calculating how many pages that are required */
2795 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2796
2797 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2798 page_list[i] = __get_free_pages(GFP_KERNEL,
2799 base->lcla_pool.pages);
2800 if (!page_list[i]) {
2801
2802 dev_err(base->dev,
2803 "[%s] Failed to allocate %d pages.\n",
2804 __func__, base->lcla_pool.pages);
2805
2806 for (j = 0; j < i; j++)
2807 free_pages(page_list[j], base->lcla_pool.pages);
2808 goto failure;
2809 }
2810
2811 if ((virt_to_phys((void *)page_list[i]) &
2812 (LCLA_ALIGNMENT - 1)) == 0)
2813 break;
2814 }
2815
2816 for (j = 0; j < i; j++)
2817 free_pages(page_list[j], base->lcla_pool.pages);
2818
2819 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2820 base->lcla_pool.base = (void *)page_list[i];
2821 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002822 /*
2823 * After many attempts and no succees with finding the correct
2824 * alignment, try with allocating a big buffer.
2825 */
Linus Walleij508849a2010-06-20 21:26:07 +00002826 dev_warn(base->dev,
2827 "[%s] Failed to get %d pages @ 18 bit align.\n",
2828 __func__, base->lcla_pool.pages);
2829 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2830 base->num_phy_chans +
2831 LCLA_ALIGNMENT,
2832 GFP_KERNEL);
2833 if (!base->lcla_pool.base_unaligned) {
2834 ret = -ENOMEM;
2835 goto failure;
2836 }
2837
2838 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2839 LCLA_ALIGNMENT);
2840 }
2841
2842 writel(virt_to_phys(base->lcla_pool.base),
2843 base->virtbase + D40_DREG_LCLA);
2844failure:
2845 kfree(page_list);
2846 return ret;
2847}
2848
Linus Walleij8d318a52010-03-30 15:33:42 +02002849static int __init d40_probe(struct platform_device *pdev)
2850{
2851 int err;
2852 int ret = -ENOENT;
2853 struct d40_base *base;
2854 struct resource *res = NULL;
2855 int num_reserved_chans;
2856 u32 val;
2857
2858 base = d40_hw_detect_init(pdev);
2859
2860 if (!base)
2861 goto failure;
2862
2863 num_reserved_chans = d40_phy_res_init(base);
2864
2865 platform_set_drvdata(pdev, base);
2866
2867 spin_lock_init(&base->interrupt_lock);
2868 spin_lock_init(&base->execmd_lock);
2869
2870 /* Get IO for logical channel parameter address */
2871 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2872 if (!res) {
2873 ret = -ENOENT;
2874 dev_err(&pdev->dev,
2875 "[%s] No \"lcpa\" memory resource\n",
2876 __func__);
2877 goto failure;
2878 }
2879 base->lcpa_size = resource_size(res);
2880 base->phy_lcpa = res->start;
2881
2882 if (request_mem_region(res->start, resource_size(res),
2883 D40_NAME " I/O lcpa") == NULL) {
2884 ret = -EBUSY;
2885 dev_err(&pdev->dev,
2886 "[%s] Failed to request LCPA region 0x%x-0x%x\n",
2887 __func__, res->start, res->end);
2888 goto failure;
2889 }
2890
2891 /* We make use of ESRAM memory for this. */
2892 val = readl(base->virtbase + D40_DREG_LCPA);
2893 if (res->start != val && val != 0) {
2894 dev_warn(&pdev->dev,
2895 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2896 __func__, val, res->start);
2897 } else
2898 writel(res->start, base->virtbase + D40_DREG_LCPA);
2899
2900 base->lcpa_base = ioremap(res->start, resource_size(res));
2901 if (!base->lcpa_base) {
2902 ret = -ENOMEM;
2903 dev_err(&pdev->dev,
2904 "[%s] Failed to ioremap LCPA region\n",
2905 __func__);
2906 goto failure;
2907 }
Linus Walleij508849a2010-06-20 21:26:07 +00002908
2909 ret = d40_lcla_allocate(base);
2910 if (ret) {
2911 dev_err(&pdev->dev, "[%s] Failed to allocate LCLA area\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002912 __func__);
2913 goto failure;
2914 }
2915
Linus Walleij8d318a52010-03-30 15:33:42 +02002916 spin_lock_init(&base->lcla_pool.lock);
2917
Linus Walleij8d318a52010-03-30 15:33:42 +02002918 base->irq = platform_get_irq(pdev, 0);
2919
2920 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
2921
2922 if (ret) {
2923 dev_err(&pdev->dev, "[%s] No IRQ defined\n", __func__);
2924 goto failure;
2925 }
2926
2927 err = d40_dmaengine_init(base, num_reserved_chans);
2928 if (err)
2929 goto failure;
2930
2931 d40_hw_init(base);
2932
2933 dev_info(base->dev, "initialized\n");
2934 return 0;
2935
2936failure:
2937 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002938 if (base->desc_slab)
2939 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002940 if (base->virtbase)
2941 iounmap(base->virtbase);
Linus Walleij508849a2010-06-20 21:26:07 +00002942 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2943 free_pages((unsigned long)base->lcla_pool.base,
2944 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002945
2946 kfree(base->lcla_pool.base_unaligned);
2947
Linus Walleij8d318a52010-03-30 15:33:42 +02002948 if (base->phy_lcpa)
2949 release_mem_region(base->phy_lcpa,
2950 base->lcpa_size);
2951 if (base->phy_start)
2952 release_mem_region(base->phy_start,
2953 base->phy_size);
2954 if (base->clk) {
2955 clk_disable(base->clk);
2956 clk_put(base->clk);
2957 }
2958
2959 kfree(base->lcla_pool.alloc_map);
2960 kfree(base->lookup_log_chans);
2961 kfree(base->lookup_phy_chans);
2962 kfree(base->phy_res);
2963 kfree(base);
2964 }
2965
2966 dev_err(&pdev->dev, "[%s] probe failed\n", __func__);
2967 return ret;
2968}
2969
2970static struct platform_driver d40_driver = {
2971 .driver = {
2972 .owner = THIS_MODULE,
2973 .name = D40_NAME,
2974 },
2975};
2976
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01002977static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02002978{
2979 return platform_driver_probe(&d40_driver, d40_probe);
2980}
2981arch_initcall(stedma40_init);