blob: 554e2942667cfe4deb3c0bf46571130ca8cd065b [file] [log] [blame]
Linus Walleij8d318a52010-03-30 15:33:42 +02001/*
Jonas Aaberg767a9672010-08-09 12:08:34 +00002 * Copyright (C) ST-Ericsson SA 2007-2010
3 * Author: Per Friden <per.friden@stericsson.com> for ST-Ericsson
4 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
Linus Walleij8d318a52010-03-30 15:33:42 +02005 * License terms: GNU General Public License (GPL) version 2
Linus Walleij8d318a52010-03-30 15:33:42 +02006 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/dmaengine.h>
11#include <linux/platform_device.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000014#include <linux/err.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020015
16#include <plat/ste_dma40.h>
17
18#include "ste_dma40_ll.h"
19
20#define D40_NAME "dma40"
21
22#define D40_PHY_CHAN -1
23
24/* For masking out/in 2 bit channel positions */
25#define D40_CHAN_POS(chan) (2 * (chan / 2))
26#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
27
28/* Maximum iterations taken before giving up suspending a channel */
29#define D40_SUSPEND_MAX_IT 500
30
Linus Walleij508849a2010-06-20 21:26:07 +000031/* Hardware requirement on LCLA alignment */
32#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000033
34/* Max number of links per event group */
35#define D40_LCLA_LINK_PER_EVENT_GRP 128
36#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
37
Linus Walleij508849a2010-06-20 21:26:07 +000038/* Attempts before giving up to trying to get pages that are aligned */
39#define MAX_LCLA_ALLOC_ATTEMPTS 256
40
41/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020042#define D40_ALLOC_FREE (1 << 31)
43#define D40_ALLOC_PHY (1 << 30)
44#define D40_ALLOC_LOG_FREE 0
45
Linus Walleij8d318a52010-03-30 15:33:42 +020046/* Hardware designer of the block */
Jonas Aaberg3ae02672010-08-09 12:08:18 +000047#define D40_HW_DESIGNER 0x8
Linus Walleij8d318a52010-03-30 15:33:42 +020048
49/**
50 * enum 40_command - The different commands and/or statuses.
51 *
52 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
53 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
54 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
55 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
56 */
57enum d40_command {
58 D40_DMA_STOP = 0,
59 D40_DMA_RUN = 1,
60 D40_DMA_SUSPEND_REQ = 2,
61 D40_DMA_SUSPENDED = 3
62};
63
64/**
65 * struct d40_lli_pool - Structure for keeping LLIs in memory
66 *
67 * @base: Pointer to memory area when the pre_alloc_lli's are not large
68 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
69 * pre_alloc_lli is used.
70 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
71 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
72 * one buffer to one buffer.
73 */
74struct d40_lli_pool {
75 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +000076 int size;
Linus Walleij8d318a52010-03-30 15:33:42 +020077 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +000078 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +020079};
80
81/**
82 * struct d40_desc - A descriptor is one DMA job.
83 *
84 * @lli_phy: LLI settings for physical channel. Both src and dst=
85 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
86 * lli_len equals one.
87 * @lli_log: Same as above but for logical channels.
88 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +000089 * @lli_len: Number of llis of current descriptor.
Jonas Aaberg698e4732010-08-09 12:08:56 +000090 * @lli_current: Number of transfered llis.
91 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +020092 * @txd: DMA engine struct. Used for among other things for communication
93 * during a transfer.
94 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +020095 * @is_in_client_list: true if the client owns this descriptor.
Jonas Aabergaa182ae2010-08-09 12:08:26 +000096 * @is_hw_linked: true if this job will automatically be continued for
97 * 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;
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000116 bool is_hw_linked;
Linus Walleij8d318a52010-03-30 15:33:42 +0200117};
118
119/**
120 * struct d40_lcla_pool - LCLA pool settings and data.
121 *
Linus Walleij508849a2010-06-20 21:26:07 +0000122 * @base: The virtual address of LCLA. 18 bit aligned.
123 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
124 * This pointer is only there for clean-up on error.
125 * @pages: The number of pages needed for all physical channels.
126 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200127 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000128 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200129 */
130struct d40_lcla_pool {
131 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000132 void *base_unaligned;
133 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200134 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000135 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200136};
137
138/**
139 * struct d40_phy_res - struct for handling eventlines mapped to physical
140 * channels.
141 *
142 * @lock: A lock protection this entity.
143 * @num: The physical channel number of this entity.
144 * @allocated_src: Bit mapped to show which src event line's are mapped to
145 * this physical channel. Can also be free or physically allocated.
146 * @allocated_dst: Same as for src but is dst.
147 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000148 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200149 */
150struct d40_phy_res {
151 spinlock_t lock;
152 int num;
153 u32 allocated_src;
154 u32 allocated_dst;
155};
156
157struct d40_base;
158
159/**
160 * struct d40_chan - Struct that describes a channel.
161 *
162 * @lock: A spinlock to protect this struct.
163 * @log_num: The logical number, if any of this channel.
164 * @completed: Starts with 1, after first interrupt it is set to dma engine's
165 * current cookie.
166 * @pending_tx: The number of pending transfers. Used between interrupt handler
167 * and tasklet.
168 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000169 * @phy_chan: Pointer to physical channel which this instance runs on. If this
170 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200171 * @chan: DMA engine handle.
172 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
173 * transfer and call client callback.
174 * @client: Cliented owned descriptor list.
175 * @active: Active descriptor.
176 * @queue: Queued jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200177 * @dma_cfg: The client configuration of this dma channel.
178 * @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;
201 struct d40_base *base;
202 /* Default register configurations */
203 u32 src_def_cfg;
204 u32 dst_def_cfg;
205 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200206 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200207 /* Runtime reconfiguration */
208 dma_addr_t runtime_addr;
209 enum dma_data_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200210};
211
212/**
213 * struct d40_base - The big global struct, one for each probe'd instance.
214 *
215 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
216 * @execmd_lock: Lock for execute command usage since several channels share
217 * the same physical register.
218 * @dev: The device structure.
219 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700220 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200221 * @clk: Pointer to the DMA clock structure.
222 * @phy_start: Physical memory start of the DMA registers.
223 * @phy_size: Size of the DMA register map.
224 * @irq: The IRQ number.
225 * @num_phy_chans: The number of physical channels. Read from HW. This
226 * is the number of available channels for this driver, not counting "Secure
227 * mode" allocated physical channels.
228 * @num_log_chans: The number of logical channels. Calculated from
229 * num_phy_chans.
230 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
231 * @dma_slave: dma_device channels that can do only do slave transfers.
232 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Linus Walleij8d318a52010-03-30 15:33:42 +0200233 * @log_chans: Room for all possible logical channels in system.
234 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
235 * to log_chans entries.
236 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
237 * to phy_chans entries.
238 * @plat_data: Pointer to provided platform_data which is the driver
239 * configuration.
240 * @phy_res: Vector containing all physical channels.
241 * @lcla_pool: lcla pool settings and data.
242 * @lcpa_base: The virtual mapped address of LCPA.
243 * @phy_lcpa: The physical address of the LCPA.
244 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000245 * @desc_slab: cache for descriptors.
Linus Walleij8d318a52010-03-30 15:33:42 +0200246 */
247struct d40_base {
248 spinlock_t interrupt_lock;
249 spinlock_t execmd_lock;
250 struct device *dev;
251 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700252 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200253 struct clk *clk;
254 phys_addr_t phy_start;
255 resource_size_t phy_size;
256 int irq;
257 int num_phy_chans;
258 int num_log_chans;
259 struct dma_device dma_both;
260 struct dma_device dma_slave;
261 struct dma_device dma_memcpy;
262 struct d40_chan *phy_chans;
263 struct d40_chan *log_chans;
264 struct d40_chan **lookup_log_chans;
265 struct d40_chan **lookup_phy_chans;
266 struct stedma40_platform_data *plat_data;
267 /* Physical half channels */
268 struct d40_phy_res *phy_res;
269 struct d40_lcla_pool lcla_pool;
270 void *lcpa_base;
271 dma_addr_t phy_lcpa;
272 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000273 struct kmem_cache *desc_slab;
Linus Walleij8d318a52010-03-30 15:33:42 +0200274};
275
276/**
277 * struct d40_interrupt_lookup - lookup table for interrupt handler
278 *
279 * @src: Interrupt mask register.
280 * @clr: Interrupt clear register.
281 * @is_error: true if this is an error interrupt.
282 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
283 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
284 */
285struct d40_interrupt_lookup {
286 u32 src;
287 u32 clr;
288 bool is_error;
289 int offset;
290};
291
292/**
293 * struct d40_reg_val - simple lookup struct
294 *
295 * @reg: The register.
296 * @val: The value that belongs to the register in reg.
297 */
298struct d40_reg_val {
299 unsigned int reg;
300 unsigned int val;
301};
302
303static int d40_pool_lli_alloc(struct d40_desc *d40d,
304 int lli_len, bool is_log)
305{
306 u32 align;
307 void *base;
308
309 if (is_log)
310 align = sizeof(struct d40_log_lli);
311 else
312 align = sizeof(struct d40_phy_lli);
313
314 if (lli_len == 1) {
315 base = d40d->lli_pool.pre_alloc_lli;
316 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
317 d40d->lli_pool.base = NULL;
318 } else {
319 d40d->lli_pool.size = ALIGN(lli_len * 2 * align, align);
320
321 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
322 d40d->lli_pool.base = base;
323
324 if (d40d->lli_pool.base == NULL)
325 return -ENOMEM;
326 }
327
328 if (is_log) {
329 d40d->lli_log.src = PTR_ALIGN((struct d40_log_lli *) base,
330 align);
331 d40d->lli_log.dst = PTR_ALIGN(d40d->lli_log.src + lli_len,
332 align);
333 } else {
334 d40d->lli_phy.src = PTR_ALIGN((struct d40_phy_lli *)base,
335 align);
336 d40d->lli_phy.dst = PTR_ALIGN(d40d->lli_phy.src + lli_len,
337 align);
Linus Walleij8d318a52010-03-30 15:33:42 +0200338 }
339
340 return 0;
341}
342
343static void d40_pool_lli_free(struct d40_desc *d40d)
344{
345 kfree(d40d->lli_pool.base);
346 d40d->lli_pool.base = NULL;
347 d40d->lli_pool.size = 0;
348 d40d->lli_log.src = NULL;
349 d40d->lli_log.dst = NULL;
350 d40d->lli_phy.src = NULL;
351 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200352}
353
Jonas Aaberg698e4732010-08-09 12:08:56 +0000354static int d40_lcla_alloc_one(struct d40_chan *d40c,
355 struct d40_desc *d40d)
356{
357 unsigned long flags;
358 int i;
359 int ret = -EINVAL;
360 int p;
361
362 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
363
364 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
365
366 /*
367 * Allocate both src and dst at the same time, therefore the half
368 * start on 1 since 0 can't be used since zero is used as end marker.
369 */
370 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
371 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
372 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
373 d40d->lcla_alloc++;
374 ret = i;
375 break;
376 }
377 }
378
379 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
380
381 return ret;
382}
383
384static int d40_lcla_free_all(struct d40_chan *d40c,
385 struct d40_desc *d40d)
386{
387 unsigned long flags;
388 int i;
389 int ret = -EINVAL;
390
391 if (d40c->log_num == D40_PHY_CHAN)
392 return 0;
393
394 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
395
396 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
397 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
398 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
399 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
400 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
401 d40d->lcla_alloc--;
402 if (d40d->lcla_alloc == 0) {
403 ret = 0;
404 break;
405 }
406 }
407 }
408
409 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
410
411 return ret;
412
413}
414
Linus Walleij8d318a52010-03-30 15:33:42 +0200415static void d40_desc_remove(struct d40_desc *d40d)
416{
417 list_del(&d40d->node);
418}
419
420static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
421{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000422 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200423
424 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000425 struct d40_desc *d;
426 struct d40_desc *_d;
427
Linus Walleij8d318a52010-03-30 15:33:42 +0200428 list_for_each_entry_safe(d, _d, &d40c->client, node)
429 if (async_tx_test_ack(&d->txd)) {
430 d40_pool_lli_free(d);
431 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000432 desc = d;
433 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000434 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200435 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200436 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000437
438 if (!desc)
439 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
440
441 if (desc)
442 INIT_LIST_HEAD(&desc->node);
443
444 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200445}
446
447static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
448{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000449
450 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000451 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200452}
453
454static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
455{
456 list_add_tail(&desc->node, &d40c->active);
457}
458
Jonas Aaberg698e4732010-08-09 12:08:56 +0000459static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
460{
461 int curr_lcla = -EINVAL, next_lcla;
462
463 if (d40c->log_num == D40_PHY_CHAN) {
464 d40_phy_lli_write(d40c->base->virtbase,
465 d40c->phy_chan->num,
466 d40d->lli_phy.dst,
467 d40d->lli_phy.src);
468 d40d->lli_current = d40d->lli_len;
469 } else {
470
471 if ((d40d->lli_len - d40d->lli_current) > 1)
472 curr_lcla = d40_lcla_alloc_one(d40c, d40d);
473
474 d40_log_lli_lcpa_write(d40c->lcpa,
475 &d40d->lli_log.dst[d40d->lli_current],
476 &d40d->lli_log.src[d40d->lli_current],
477 curr_lcla);
478
479 d40d->lli_current++;
480 for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
481 struct d40_log_lli *lcla;
482
483 if (d40d->lli_current + 1 < d40d->lli_len)
484 next_lcla = d40_lcla_alloc_one(d40c, d40d);
485 else
486 next_lcla = -EINVAL;
487
488 lcla = d40c->base->lcla_pool.base +
489 d40c->phy_chan->num * 1024 +
490 8 * curr_lcla * 2;
491
492 d40_log_lli_lcla_write(lcla,
493 &d40d->lli_log.dst[d40d->lli_current],
494 &d40d->lli_log.src[d40d->lli_current],
495 next_lcla);
496
497 (void) dma_map_single(d40c->base->dev, lcla,
498 2 * sizeof(struct d40_log_lli),
499 DMA_TO_DEVICE);
500
501 curr_lcla = next_lcla;
502
503 if (curr_lcla == -EINVAL) {
504 d40d->lli_current++;
505 break;
506 }
507
508 }
509 }
510}
511
Linus Walleij8d318a52010-03-30 15:33:42 +0200512static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
513{
514 struct d40_desc *d;
515
516 if (list_empty(&d40c->active))
517 return NULL;
518
519 d = list_first_entry(&d40c->active,
520 struct d40_desc,
521 node);
522 return d;
523}
524
525static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
526{
527 list_add_tail(&desc->node, &d40c->queue);
528}
529
530static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
531{
532 struct d40_desc *d;
533
534 if (list_empty(&d40c->queue))
535 return NULL;
536
537 d = list_first_entry(&d40c->queue,
538 struct d40_desc,
539 node);
540 return d;
541}
542
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000543static struct d40_desc *d40_last_queued(struct d40_chan *d40c)
544{
545 struct d40_desc *d;
546
547 if (list_empty(&d40c->queue))
548 return NULL;
549 list_for_each_entry(d, &d40c->queue, node)
550 if (list_is_last(&d->node, &d40c->queue))
551 break;
552 return d;
553}
554
Linus Walleij8d318a52010-03-30 15:33:42 +0200555/* Support functions for logical channels */
556
Linus Walleij8d318a52010-03-30 15:33:42 +0200557
558static int d40_channel_execute_command(struct d40_chan *d40c,
559 enum d40_command command)
560{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000561 u32 status;
562 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200563 void __iomem *active_reg;
564 int ret = 0;
565 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000566 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200567
568 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
569
570 if (d40c->phy_chan->num % 2 == 0)
571 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
572 else
573 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
574
575 if (command == D40_DMA_SUSPEND_REQ) {
576 status = (readl(active_reg) &
577 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
578 D40_CHAN_POS(d40c->phy_chan->num);
579
580 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
581 goto done;
582 }
583
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000584 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
585 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
586 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200587
588 if (command == D40_DMA_SUSPEND_REQ) {
589
590 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
591 status = (readl(active_reg) &
592 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
593 D40_CHAN_POS(d40c->phy_chan->num);
594
595 cpu_relax();
596 /*
597 * Reduce the number of bus accesses while
598 * waiting for the DMA to suspend.
599 */
600 udelay(3);
601
602 if (status == D40_DMA_STOP ||
603 status == D40_DMA_SUSPENDED)
604 break;
605 }
606
607 if (i == D40_SUSPEND_MAX_IT) {
608 dev_err(&d40c->chan.dev->device,
609 "[%s]: unable to suspend the chl %d (log: %d) status %x\n",
610 __func__, d40c->phy_chan->num, d40c->log_num,
611 status);
612 dump_stack();
613 ret = -EBUSY;
614 }
615
616 }
617done:
618 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
619 return ret;
620}
621
622static void d40_term_all(struct d40_chan *d40c)
623{
624 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200625
626 /* Release active descriptors */
627 while ((d40d = d40_first_active_get(d40c))) {
628 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200629 d40_desc_free(d40c, d40d);
630 }
631
632 /* Release queued descriptors waiting for transfer */
633 while ((d40d = d40_first_queued(d40c))) {
634 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200635 d40_desc_free(d40c, d40d);
636 }
637
Linus Walleij8d318a52010-03-30 15:33:42 +0200638
639 d40c->pending_tx = 0;
640 d40c->busy = false;
641}
642
643static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
644{
645 u32 val;
646 unsigned long flags;
647
Jonas Aaberg0c322692010-06-20 21:25:46 +0000648 /* Notice, that disable requires the physical channel to be stopped */
Linus Walleij8d318a52010-03-30 15:33:42 +0200649 if (do_enable)
650 val = D40_ACTIVATE_EVENTLINE;
651 else
652 val = D40_DEACTIVATE_EVENTLINE;
653
654 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
655
656 /* Enable event line connected to device (or memcpy) */
657 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
658 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
659 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
660
661 writel((val << D40_EVENTLINE_POS(event)) |
662 ~D40_EVENTLINE_MASK(event),
663 d40c->base->virtbase + D40_DREG_PCBASE +
664 d40c->phy_chan->num * D40_DREG_PCDELTA +
665 D40_CHAN_REG_SSLNK);
666 }
667 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
668 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
669
670 writel((val << D40_EVENTLINE_POS(event)) |
671 ~D40_EVENTLINE_MASK(event),
672 d40c->base->virtbase + D40_DREG_PCBASE +
673 d40c->phy_chan->num * D40_DREG_PCDELTA +
674 D40_CHAN_REG_SDLNK);
675 }
676
677 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
678}
679
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200680static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200681{
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000682 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200683
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000684 val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
685 d40c->phy_chan->num * D40_DREG_PCDELTA +
686 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +0200687
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +0000688 val |= readl(d40c->base->virtbase + D40_DREG_PCBASE +
689 d40c->phy_chan->num * D40_DREG_PCDELTA +
690 D40_CHAN_REG_SDLNK);
Jonas Aaberga5ebca42010-05-18 00:41:09 +0200691 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +0200692}
693
Jonas Aabergb55912c2010-08-09 12:08:02 +0000694static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +0200695{
696 u32 addr_base;
697 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +0200698
699 /* Odd addresses are even addresses + 4 */
700 addr_base = (d40c->phy_chan->num % 2) * 4;
701 /* Setup channel mode to logical or physical */
702 var = ((u32)(d40c->log_num != D40_PHY_CHAN) + 1) <<
703 D40_CHAN_POS(d40c->phy_chan->num);
704 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
705
706 /* Setup operational mode option register */
707 var = ((d40c->dma_cfg.channel_type >> STEDMA40_INFO_CH_MODE_OPT_POS) &
708 0x3) << D40_CHAN_POS(d40c->phy_chan->num);
709
710 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
711
712 if (d40c->log_num != D40_PHY_CHAN) {
713 /* Set default config for CFG reg */
714 writel(d40c->src_def_cfg,
715 d40c->base->virtbase + D40_DREG_PCBASE +
716 d40c->phy_chan->num * D40_DREG_PCDELTA +
717 D40_CHAN_REG_SSCFG);
718 writel(d40c->dst_def_cfg,
719 d40c->base->virtbase + D40_DREG_PCBASE +
720 d40c->phy_chan->num * D40_DREG_PCDELTA +
721 D40_CHAN_REG_SDCFG);
722
Jonas Aabergb55912c2010-08-09 12:08:02 +0000723 /* Set LIDX for lcla */
724 writel((d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) &
725 D40_SREG_ELEM_LOG_LIDX_MASK,
726 d40c->base->virtbase + D40_DREG_PCBASE +
727 d40c->phy_chan->num * D40_DREG_PCDELTA +
728 D40_CHAN_REG_SDELT);
729
730 writel((d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) &
731 D40_SREG_ELEM_LOG_LIDX_MASK,
732 d40c->base->virtbase + D40_DREG_PCBASE +
733 d40c->phy_chan->num * D40_DREG_PCDELTA +
734 D40_CHAN_REG_SSELT);
735
Linus Walleij8d318a52010-03-30 15:33:42 +0200736 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200737}
738
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000739static u32 d40_residue(struct d40_chan *d40c)
740{
741 u32 num_elt;
742
743 if (d40c->log_num != D40_PHY_CHAN)
744 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
745 >> D40_MEM_LCSP2_ECNT_POS;
746 else
747 num_elt = (readl(d40c->base->virtbase + D40_DREG_PCBASE +
748 d40c->phy_chan->num * D40_DREG_PCDELTA +
749 D40_CHAN_REG_SDELT) &
750 D40_SREG_ELEM_PHY_ECNT_MASK) >>
751 D40_SREG_ELEM_PHY_ECNT_POS;
752 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
753}
754
755static bool d40_tx_is_linked(struct d40_chan *d40c)
756{
757 bool is_link;
758
759 if (d40c->log_num != D40_PHY_CHAN)
760 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
761 else
762 is_link = readl(d40c->base->virtbase + D40_DREG_PCBASE +
763 d40c->phy_chan->num * D40_DREG_PCDELTA +
764 D40_CHAN_REG_SDLNK) &
765 D40_SREG_LNK_PHYS_LNK_MASK;
766 return is_link;
767}
768
769static int d40_pause(struct dma_chan *chan)
770{
771 struct d40_chan *d40c =
772 container_of(chan, struct d40_chan, chan);
773 int res = 0;
774 unsigned long flags;
775
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000776 if (!d40c->busy)
777 return 0;
778
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000779 spin_lock_irqsave(&d40c->lock, flags);
780
781 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
782 if (res == 0) {
783 if (d40c->log_num != D40_PHY_CHAN) {
784 d40_config_set_event(d40c, false);
785 /* Resume the other logical channels if any */
786 if (d40_chan_has_events(d40c))
787 res = d40_channel_execute_command(d40c,
788 D40_DMA_RUN);
789 }
790 }
791
792 spin_unlock_irqrestore(&d40c->lock, flags);
793 return res;
794}
795
796static int d40_resume(struct dma_chan *chan)
797{
798 struct d40_chan *d40c =
799 container_of(chan, struct d40_chan, chan);
800 int res = 0;
801 unsigned long flags;
802
Jonas Aaberg3ac012a2010-08-09 12:09:12 +0000803 if (!d40c->busy)
804 return 0;
805
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000806 spin_lock_irqsave(&d40c->lock, flags);
807
808 if (d40c->base->rev == 0)
809 if (d40c->log_num != D40_PHY_CHAN) {
810 res = d40_channel_execute_command(d40c,
811 D40_DMA_SUSPEND_REQ);
812 goto no_suspend;
813 }
814
815 /* If bytes left to transfer or linked tx resume job */
816 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
817
818 if (d40c->log_num != D40_PHY_CHAN)
819 d40_config_set_event(d40c, true);
820
821 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
822 }
823
824no_suspend:
825 spin_unlock_irqrestore(&d40c->lock, flags);
826 return res;
827}
828
829static void d40_tx_submit_log(struct d40_chan *d40c, struct d40_desc *d40d)
830{
831 /* TODO: Write */
832}
833
834static void d40_tx_submit_phy(struct d40_chan *d40c, struct d40_desc *d40d)
835{
836 struct d40_desc *d40d_prev = NULL;
837 int i;
838 u32 val;
839
840 if (!list_empty(&d40c->queue))
841 d40d_prev = d40_last_queued(d40c);
842 else if (!list_empty(&d40c->active))
843 d40d_prev = d40_first_active_get(d40c);
844
845 if (!d40d_prev)
846 return;
847
848 /* Here we try to join this job with previous jobs */
849 val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
850 d40c->phy_chan->num * D40_DREG_PCDELTA +
851 D40_CHAN_REG_SSLNK);
852
853 /* Figure out which link we're currently transmitting */
854 for (i = 0; i < d40d_prev->lli_len; i++)
855 if (val == d40d_prev->lli_phy.src[i].reg_lnk)
856 break;
857
858 val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
859 d40c->phy_chan->num * D40_DREG_PCDELTA +
860 D40_CHAN_REG_SSELT) >> D40_SREG_ELEM_LOG_ECNT_POS;
861
862 if (i == (d40d_prev->lli_len - 1) && val > 0) {
863 /* Change the current one */
864 writel(virt_to_phys(d40d->lli_phy.src),
865 d40c->base->virtbase + D40_DREG_PCBASE +
866 d40c->phy_chan->num * D40_DREG_PCDELTA +
867 D40_CHAN_REG_SSLNK);
868 writel(virt_to_phys(d40d->lli_phy.dst),
869 d40c->base->virtbase + D40_DREG_PCBASE +
870 d40c->phy_chan->num * D40_DREG_PCDELTA +
871 D40_CHAN_REG_SDLNK);
872
873 d40d->is_hw_linked = true;
874
875 } else if (i < d40d_prev->lli_len) {
876 (void) dma_unmap_single(d40c->base->dev,
877 virt_to_phys(d40d_prev->lli_phy.src),
878 d40d_prev->lli_pool.size,
879 DMA_TO_DEVICE);
880
881 /* Keep the settings */
882 val = d40d_prev->lli_phy.src[d40d_prev->lli_len - 1].reg_lnk &
883 ~D40_SREG_LNK_PHYS_LNK_MASK;
884 d40d_prev->lli_phy.src[d40d_prev->lli_len - 1].reg_lnk =
885 val | virt_to_phys(d40d->lli_phy.src);
886
887 val = d40d_prev->lli_phy.dst[d40d_prev->lli_len - 1].reg_lnk &
888 ~D40_SREG_LNK_PHYS_LNK_MASK;
889 d40d_prev->lli_phy.dst[d40d_prev->lli_len - 1].reg_lnk =
890 val | virt_to_phys(d40d->lli_phy.dst);
891
892 (void) dma_map_single(d40c->base->dev,
893 d40d_prev->lli_phy.src,
894 d40d_prev->lli_pool.size,
895 DMA_TO_DEVICE);
896 d40d->is_hw_linked = true;
897 }
898}
899
Linus Walleij8d318a52010-03-30 15:33:42 +0200900static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
901{
902 struct d40_chan *d40c = container_of(tx->chan,
903 struct d40_chan,
904 chan);
905 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
906 unsigned long flags;
907
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000908 (void) d40_pause(&d40c->chan);
909
Linus Walleij8d318a52010-03-30 15:33:42 +0200910 spin_lock_irqsave(&d40c->lock, flags);
911
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000912 d40c->chan.cookie++;
913
914 if (d40c->chan.cookie < 0)
915 d40c->chan.cookie = 1;
916
917 d40d->txd.cookie = d40c->chan.cookie;
918
919 if (d40c->log_num == D40_PHY_CHAN)
920 d40_tx_submit_phy(d40c, d40d);
921 else
922 d40_tx_submit_log(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200923
924 d40_desc_queue(d40c, d40d);
925
926 spin_unlock_irqrestore(&d40c->lock, flags);
927
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000928 (void) d40_resume(&d40c->chan);
929
Linus Walleij8d318a52010-03-30 15:33:42 +0200930 return tx->cookie;
931}
932
933static int d40_start(struct d40_chan *d40c)
934{
Linus Walleijf4185592010-06-22 18:06:42 -0700935 if (d40c->base->rev == 0) {
936 int err;
937
938 if (d40c->log_num != D40_PHY_CHAN) {
939 err = d40_channel_execute_command(d40c,
940 D40_DMA_SUSPEND_REQ);
941 if (err)
942 return err;
943 }
944 }
945
Jonas Aaberg0c322692010-06-20 21:25:46 +0000946 if (d40c->log_num != D40_PHY_CHAN)
Linus Walleij8d318a52010-03-30 15:33:42 +0200947 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +0200948
Jonas Aaberg0c322692010-06-20 21:25:46 +0000949 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +0200950}
951
952static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
953{
954 struct d40_desc *d40d;
955 int err;
956
957 /* Start queued jobs, if any */
958 d40d = d40_first_queued(d40c);
959
960 if (d40d != NULL) {
961 d40c->busy = true;
962
963 /* Remove from queue */
964 d40_desc_remove(d40d);
965
966 /* Add to active queue */
967 d40_desc_submit(d40c, d40d);
968
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000969 /*
970 * If this job is already linked in hw,
971 * do not submit it.
972 */
Jonas Aaberg698e4732010-08-09 12:08:56 +0000973
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000974 if (!d40d->is_hw_linked) {
975 /* Initiate DMA job */
976 d40_desc_load(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200977
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000978 /* Start dma job */
979 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200980
Jonas Aabergaa182ae2010-08-09 12:08:26 +0000981 if (err)
982 return NULL;
983 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200984 }
985
986 return d40d;
987}
988
989/* called from interrupt context */
990static void dma_tc_handle(struct d40_chan *d40c)
991{
992 struct d40_desc *d40d;
993
Linus Walleij8d318a52010-03-30 15:33:42 +0200994 /* Get first active entry from list */
995 d40d = d40_first_active_get(d40c);
996
997 if (d40d == NULL)
998 return;
999
Jonas Aaberg698e4732010-08-09 12:08:56 +00001000 d40_lcla_free_all(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001001
Jonas Aaberg698e4732010-08-09 12:08:56 +00001002 if (d40d->lli_current < d40d->lli_len) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001003 d40_desc_load(d40c, d40d);
1004 /* Start dma job */
1005 (void) d40_start(d40c);
1006 return;
1007 }
1008
1009 if (d40_queue_start(d40c) == NULL)
1010 d40c->busy = false;
1011
1012 d40c->pending_tx++;
1013 tasklet_schedule(&d40c->tasklet);
1014
1015}
1016
1017static void dma_tasklet(unsigned long data)
1018{
1019 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001020 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001021 unsigned long flags;
1022 dma_async_tx_callback callback;
1023 void *callback_param;
1024
1025 spin_lock_irqsave(&d40c->lock, flags);
1026
1027 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001028 d40d = d40_first_active_get(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001029
Jonas Aaberg767a9672010-08-09 12:08:34 +00001030 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001031 goto err;
1032
Jonas Aaberg767a9672010-08-09 12:08:34 +00001033 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001034
1035 /*
1036 * If terminating a channel pending_tx is set to zero.
1037 * This prevents any finished active jobs to return to the client.
1038 */
1039 if (d40c->pending_tx == 0) {
1040 spin_unlock_irqrestore(&d40c->lock, flags);
1041 return;
1042 }
1043
1044 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001045 callback = d40d->txd.callback;
1046 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001047
Jonas Aaberg767a9672010-08-09 12:08:34 +00001048 if (async_tx_test_ack(&d40d->txd)) {
1049 d40_pool_lli_free(d40d);
1050 d40_desc_remove(d40d);
1051 d40_desc_free(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001052 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001053 if (!d40d->is_in_client_list) {
1054 d40_desc_remove(d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001055 d40_lcla_free_all(d40c, d40d);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001056 list_add_tail(&d40d->node, &d40c->client);
1057 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001058 }
1059 }
1060
1061 d40c->pending_tx--;
1062
1063 if (d40c->pending_tx)
1064 tasklet_schedule(&d40c->tasklet);
1065
1066 spin_unlock_irqrestore(&d40c->lock, flags);
1067
Jonas Aaberg767a9672010-08-09 12:08:34 +00001068 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001069 callback(callback_param);
1070
1071 return;
1072
1073 err:
1074 /* Rescue manouver if receiving double interrupts */
1075 if (d40c->pending_tx > 0)
1076 d40c->pending_tx--;
1077 spin_unlock_irqrestore(&d40c->lock, flags);
1078}
1079
1080static irqreturn_t d40_handle_interrupt(int irq, void *data)
1081{
1082 static const struct d40_interrupt_lookup il[] = {
1083 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1084 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1085 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1086 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1087 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1088 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1089 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1090 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1091 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1092 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1093 };
1094
1095 int i;
1096 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001097 u32 idx;
1098 u32 row;
1099 long chan = -1;
1100 struct d40_chan *d40c;
1101 unsigned long flags;
1102 struct d40_base *base = data;
1103
1104 spin_lock_irqsave(&base->interrupt_lock, flags);
1105
1106 /* Read interrupt status of both logical and physical channels */
1107 for (i = 0; i < ARRAY_SIZE(il); i++)
1108 regs[i] = readl(base->virtbase + il[i].src);
1109
1110 for (;;) {
1111
1112 chan = find_next_bit((unsigned long *)regs,
1113 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1114
1115 /* No more set bits found? */
1116 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1117 break;
1118
1119 row = chan / BITS_PER_LONG;
1120 idx = chan & (BITS_PER_LONG - 1);
1121
1122 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001123 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001124
1125 if (il[row].offset == D40_PHY_CHAN)
1126 d40c = base->lookup_phy_chans[idx];
1127 else
1128 d40c = base->lookup_log_chans[il[row].offset + idx];
1129 spin_lock(&d40c->lock);
1130
1131 if (!il[row].is_error)
1132 dma_tc_handle(d40c);
1133 else
Linus Walleij508849a2010-06-20 21:26:07 +00001134 dev_err(base->dev,
1135 "[%s] IRQ chan: %ld offset %d idx %d\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02001136 __func__, chan, il[row].offset, idx);
1137
1138 spin_unlock(&d40c->lock);
1139 }
1140
1141 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1142
1143 return IRQ_HANDLED;
1144}
1145
Linus Walleij8d318a52010-03-30 15:33:42 +02001146static int d40_validate_conf(struct d40_chan *d40c,
1147 struct stedma40_chan_cfg *conf)
1148{
1149 int res = 0;
1150 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1151 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
1152 bool is_log = (conf->channel_type & STEDMA40_CHANNEL_IN_OPER_MODE)
1153 == STEDMA40_CHANNEL_IN_LOG_MODE;
1154
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001155 if (!conf->dir) {
1156 dev_err(&d40c->chan.dev->device, "[%s] Invalid direction.\n",
1157 __func__);
1158 res = -EINVAL;
1159 }
1160
1161 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1162 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1163 d40c->runtime_addr == 0) {
1164
1165 dev_err(&d40c->chan.dev->device,
1166 "[%s] Invalid TX channel address (%d)\n",
1167 __func__, conf->dst_dev_type);
1168 res = -EINVAL;
1169 }
1170
1171 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1172 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1173 d40c->runtime_addr == 0) {
1174 dev_err(&d40c->chan.dev->device,
1175 "[%s] Invalid RX channel address (%d)\n",
1176 __func__, conf->src_dev_type);
1177 res = -EINVAL;
1178 }
1179
1180 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001181 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
1182 dev_err(&d40c->chan.dev->device, "[%s] Invalid dst\n",
1183 __func__);
1184 res = -EINVAL;
1185 }
1186
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001187 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001188 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
1189 dev_err(&d40c->chan.dev->device, "[%s] Invalid src\n",
1190 __func__);
1191 res = -EINVAL;
1192 }
1193
1194 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1195 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
1196 dev_err(&d40c->chan.dev->device,
1197 "[%s] No event line\n", __func__);
1198 res = -EINVAL;
1199 }
1200
1201 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1202 (src_event_group != dst_event_group)) {
1203 dev_err(&d40c->chan.dev->device,
1204 "[%s] Invalid event group\n", __func__);
1205 res = -EINVAL;
1206 }
1207
1208 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1209 /*
1210 * DMAC HW supports it. Will be added to this driver,
1211 * in case any dma client requires it.
1212 */
1213 dev_err(&d40c->chan.dev->device,
1214 "[%s] periph to periph not supported\n",
1215 __func__);
1216 res = -EINVAL;
1217 }
1218
1219 return res;
1220}
1221
1222static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001223 int log_event_line, bool is_log)
Linus Walleij8d318a52010-03-30 15:33:42 +02001224{
1225 unsigned long flags;
1226 spin_lock_irqsave(&phy->lock, flags);
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001227 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001228 /* Physical interrupts are masked per physical full channel */
1229 if (phy->allocated_src == D40_ALLOC_FREE &&
1230 phy->allocated_dst == D40_ALLOC_FREE) {
1231 phy->allocated_dst = D40_ALLOC_PHY;
1232 phy->allocated_src = D40_ALLOC_PHY;
1233 goto found;
1234 } else
1235 goto not_found;
1236 }
1237
1238 /* Logical channel */
1239 if (is_src) {
1240 if (phy->allocated_src == D40_ALLOC_PHY)
1241 goto not_found;
1242
1243 if (phy->allocated_src == D40_ALLOC_FREE)
1244 phy->allocated_src = D40_ALLOC_LOG_FREE;
1245
1246 if (!(phy->allocated_src & (1 << log_event_line))) {
1247 phy->allocated_src |= 1 << log_event_line;
1248 goto found;
1249 } else
1250 goto not_found;
1251 } else {
1252 if (phy->allocated_dst == D40_ALLOC_PHY)
1253 goto not_found;
1254
1255 if (phy->allocated_dst == D40_ALLOC_FREE)
1256 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1257
1258 if (!(phy->allocated_dst & (1 << log_event_line))) {
1259 phy->allocated_dst |= 1 << log_event_line;
1260 goto found;
1261 } else
1262 goto not_found;
1263 }
1264
1265not_found:
1266 spin_unlock_irqrestore(&phy->lock, flags);
1267 return false;
1268found:
1269 spin_unlock_irqrestore(&phy->lock, flags);
1270 return true;
1271}
1272
1273static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1274 int log_event_line)
1275{
1276 unsigned long flags;
1277 bool is_free = false;
1278
1279 spin_lock_irqsave(&phy->lock, flags);
1280 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001281 phy->allocated_dst = D40_ALLOC_FREE;
1282 phy->allocated_src = D40_ALLOC_FREE;
1283 is_free = true;
1284 goto out;
1285 }
1286
1287 /* Logical channel */
1288 if (is_src) {
1289 phy->allocated_src &= ~(1 << log_event_line);
1290 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1291 phy->allocated_src = D40_ALLOC_FREE;
1292 } else {
1293 phy->allocated_dst &= ~(1 << log_event_line);
1294 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1295 phy->allocated_dst = D40_ALLOC_FREE;
1296 }
1297
1298 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1299 D40_ALLOC_FREE);
1300
1301out:
1302 spin_unlock_irqrestore(&phy->lock, flags);
1303
1304 return is_free;
1305}
1306
1307static int d40_allocate_channel(struct d40_chan *d40c)
1308{
1309 int dev_type;
1310 int event_group;
1311 int event_line;
1312 struct d40_phy_res *phys;
1313 int i;
1314 int j;
1315 int log_num;
1316 bool is_src;
Linus Walleij508849a2010-06-20 21:26:07 +00001317 bool is_log = (d40c->dma_cfg.channel_type &
1318 STEDMA40_CHANNEL_IN_OPER_MODE)
Linus Walleij8d318a52010-03-30 15:33:42 +02001319 == STEDMA40_CHANNEL_IN_LOG_MODE;
1320
1321
1322 phys = d40c->base->phy_res;
1323
1324 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1325 dev_type = d40c->dma_cfg.src_dev_type;
1326 log_num = 2 * dev_type;
1327 is_src = true;
1328 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1329 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1330 /* dst event lines are used for logical memcpy */
1331 dev_type = d40c->dma_cfg.dst_dev_type;
1332 log_num = 2 * dev_type + 1;
1333 is_src = false;
1334 } else
1335 return -EINVAL;
1336
1337 event_group = D40_TYPE_TO_GROUP(dev_type);
1338 event_line = D40_TYPE_TO_EVENT(dev_type);
1339
1340 if (!is_log) {
1341 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1342 /* Find physical half channel */
1343 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1344
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001345 if (d40_alloc_mask_set(&phys[i], is_src,
1346 0, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001347 goto found_phy;
1348 }
1349 } else
1350 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1351 int phy_num = j + event_group * 2;
1352 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001353 if (d40_alloc_mask_set(&phys[i],
1354 is_src,
1355 0,
1356 is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001357 goto found_phy;
1358 }
1359 }
1360 return -EINVAL;
1361found_phy:
1362 d40c->phy_chan = &phys[i];
1363 d40c->log_num = D40_PHY_CHAN;
1364 goto out;
1365 }
1366 if (dev_type == -1)
1367 return -EINVAL;
1368
1369 /* Find logical channel */
1370 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1371 int phy_num = j + event_group * 2;
1372 /*
1373 * Spread logical channels across all available physical rather
1374 * than pack every logical channel at the first available phy
1375 * channels.
1376 */
1377 if (is_src) {
1378 for (i = phy_num; i < phy_num + 2; i++) {
1379 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001380 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001381 goto found_log;
1382 }
1383 } else {
1384 for (i = phy_num + 1; i >= phy_num; i--) {
1385 if (d40_alloc_mask_set(&phys[i], is_src,
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001386 event_line, is_log))
Linus Walleij8d318a52010-03-30 15:33:42 +02001387 goto found_log;
1388 }
1389 }
1390 }
1391 return -EINVAL;
1392
1393found_log:
1394 d40c->phy_chan = &phys[i];
1395 d40c->log_num = log_num;
1396out:
1397
1398 if (is_log)
1399 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1400 else
1401 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1402
1403 return 0;
1404
1405}
1406
Linus Walleij8d318a52010-03-30 15:33:42 +02001407static int d40_config_memcpy(struct d40_chan *d40c)
1408{
1409 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1410
1411 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1412 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1413 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1414 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1415 memcpy[d40c->chan.chan_id];
1416
1417 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1418 dma_has_cap(DMA_SLAVE, cap)) {
1419 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1420 } else {
1421 dev_err(&d40c->chan.dev->device, "[%s] No memcpy\n",
1422 __func__);
1423 return -EINVAL;
1424 }
1425
1426 return 0;
1427}
1428
1429
1430static int d40_free_dma(struct d40_chan *d40c)
1431{
1432
1433 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001434 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001435 struct d40_phy_res *phy = d40c->phy_chan;
1436 bool is_src;
Per Fridena8be8622010-06-20 21:24:59 +00001437 struct d40_desc *d;
1438 struct d40_desc *_d;
1439
Linus Walleij8d318a52010-03-30 15:33:42 +02001440
1441 /* Terminate all queued and active transfers */
1442 d40_term_all(d40c);
1443
Per Fridena8be8622010-06-20 21:24:59 +00001444 /* Release client owned descriptors */
1445 if (!list_empty(&d40c->client))
1446 list_for_each_entry_safe(d, _d, &d40c->client, node) {
1447 d40_pool_lli_free(d);
1448 d40_desc_remove(d);
Per Fridena8be8622010-06-20 21:24:59 +00001449 d40_desc_free(d40c, d);
1450 }
1451
Linus Walleij8d318a52010-03-30 15:33:42 +02001452 if (phy == NULL) {
1453 dev_err(&d40c->chan.dev->device, "[%s] phy == null\n",
1454 __func__);
1455 return -EINVAL;
1456 }
1457
1458 if (phy->allocated_src == D40_ALLOC_FREE &&
1459 phy->allocated_dst == D40_ALLOC_FREE) {
1460 dev_err(&d40c->chan.dev->device, "[%s] channel already free\n",
1461 __func__);
1462 return -EINVAL;
1463 }
1464
Linus Walleij8d318a52010-03-30 15:33:42 +02001465 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1466 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1467 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001468 is_src = false;
1469 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1470 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001471 is_src = true;
1472 } else {
1473 dev_err(&d40c->chan.dev->device,
1474 "[%s] Unknown direction\n", __func__);
1475 return -EINVAL;
1476 }
1477
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001478 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1479 if (res) {
1480 dev_err(&d40c->chan.dev->device, "[%s] suspend failed\n",
1481 __func__);
1482 return res;
1483 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001484
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001485 if (d40c->log_num != D40_PHY_CHAN) {
1486 /* Release logical channel, deactivate the event line */
1487
1488 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001489 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1490
1491 /*
1492 * Check if there are more logical allocation
1493 * on this phy channel.
1494 */
1495 if (!d40_alloc_mask_free(phy, is_src, event)) {
1496 /* Resume the other logical channels if any */
1497 if (d40_chan_has_events(d40c)) {
1498 res = d40_channel_execute_command(d40c,
1499 D40_DMA_RUN);
1500 if (res) {
1501 dev_err(&d40c->chan.dev->device,
1502 "[%s] Executing RUN command\n",
1503 __func__);
1504 return res;
1505 }
1506 }
1507 return 0;
1508 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001509 } else {
1510 (void) d40_alloc_mask_free(phy, is_src, 0);
1511 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001512
1513 /* Release physical channel */
1514 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1515 if (res) {
1516 dev_err(&d40c->chan.dev->device,
1517 "[%s] Failed to stop channel\n", __func__);
1518 return res;
1519 }
1520 d40c->phy_chan = NULL;
1521 /* Invalidate channel type */
1522 d40c->dma_cfg.channel_type = 0;
1523 d40c->base->lookup_phy_chans[phy->num] = NULL;
1524
1525 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001526}
1527
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001528static bool d40_is_paused(struct d40_chan *d40c)
1529{
1530 bool is_paused = false;
1531 unsigned long flags;
1532 void __iomem *active_reg;
1533 u32 status;
1534 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001535
1536 spin_lock_irqsave(&d40c->lock, flags);
1537
1538 if (d40c->log_num == D40_PHY_CHAN) {
1539 if (d40c->phy_chan->num % 2 == 0)
1540 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1541 else
1542 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1543
1544 status = (readl(active_reg) &
1545 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1546 D40_CHAN_POS(d40c->phy_chan->num);
1547 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1548 is_paused = true;
1549
1550 goto _exit;
1551 }
1552
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001553 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001554 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001555 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001556 status = readl(d40c->base->virtbase + D40_DREG_PCBASE +
1557 d40c->phy_chan->num * D40_DREG_PCDELTA +
1558 D40_CHAN_REG_SDLNK);
1559 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001560 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001561 status = readl(d40c->base->virtbase + D40_DREG_PCBASE +
1562 d40c->phy_chan->num * D40_DREG_PCDELTA +
1563 D40_CHAN_REG_SSLNK);
1564 } else {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001565 dev_err(&d40c->chan.dev->device,
1566 "[%s] Unknown direction\n", __func__);
1567 goto _exit;
1568 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001569
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001570 status = (status & D40_EVENTLINE_MASK(event)) >>
1571 D40_EVENTLINE_POS(event);
1572
1573 if (status != D40_DMA_RUN)
1574 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001575_exit:
1576 spin_unlock_irqrestore(&d40c->lock, flags);
1577 return is_paused;
1578
1579}
1580
1581
Linus Walleij8d318a52010-03-30 15:33:42 +02001582static u32 stedma40_residue(struct dma_chan *chan)
1583{
1584 struct d40_chan *d40c =
1585 container_of(chan, struct d40_chan, chan);
1586 u32 bytes_left;
1587 unsigned long flags;
1588
1589 spin_lock_irqsave(&d40c->lock, flags);
1590 bytes_left = d40_residue(d40c);
1591 spin_unlock_irqrestore(&d40c->lock, flags);
1592
1593 return bytes_left;
1594}
1595
1596/* Public DMA functions in addition to the DMA engine framework */
1597
1598int stedma40_set_psize(struct dma_chan *chan,
1599 int src_psize,
1600 int dst_psize)
1601{
1602 struct d40_chan *d40c =
1603 container_of(chan, struct d40_chan, chan);
1604 unsigned long flags;
1605
1606 spin_lock_irqsave(&d40c->lock, flags);
1607
1608 if (d40c->log_num != D40_PHY_CHAN) {
1609 d40c->log_def.lcsp1 &= ~D40_MEM_LCSP1_SCFG_PSIZE_MASK;
1610 d40c->log_def.lcsp3 &= ~D40_MEM_LCSP1_SCFG_PSIZE_MASK;
Linus Walleij508849a2010-06-20 21:26:07 +00001611 d40c->log_def.lcsp1 |= src_psize <<
1612 D40_MEM_LCSP1_SCFG_PSIZE_POS;
1613 d40c->log_def.lcsp3 |= dst_psize <<
1614 D40_MEM_LCSP1_SCFG_PSIZE_POS;
Linus Walleij8d318a52010-03-30 15:33:42 +02001615 goto out;
1616 }
1617
1618 if (src_psize == STEDMA40_PSIZE_PHY_1)
1619 d40c->src_def_cfg &= ~(1 << D40_SREG_CFG_PHY_PEN_POS);
1620 else {
1621 d40c->src_def_cfg |= 1 << D40_SREG_CFG_PHY_PEN_POS;
1622 d40c->src_def_cfg &= ~(STEDMA40_PSIZE_PHY_16 <<
1623 D40_SREG_CFG_PSIZE_POS);
1624 d40c->src_def_cfg |= src_psize << D40_SREG_CFG_PSIZE_POS;
1625 }
1626
1627 if (dst_psize == STEDMA40_PSIZE_PHY_1)
1628 d40c->dst_def_cfg &= ~(1 << D40_SREG_CFG_PHY_PEN_POS);
1629 else {
1630 d40c->dst_def_cfg |= 1 << D40_SREG_CFG_PHY_PEN_POS;
1631 d40c->dst_def_cfg &= ~(STEDMA40_PSIZE_PHY_16 <<
1632 D40_SREG_CFG_PSIZE_POS);
1633 d40c->dst_def_cfg |= dst_psize << D40_SREG_CFG_PSIZE_POS;
1634 }
1635out:
1636 spin_unlock_irqrestore(&d40c->lock, flags);
1637 return 0;
1638}
1639EXPORT_SYMBOL(stedma40_set_psize);
1640
1641struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
1642 struct scatterlist *sgl_dst,
1643 struct scatterlist *sgl_src,
1644 unsigned int sgl_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001645 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001646{
1647 int res;
1648 struct d40_desc *d40d;
1649 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1650 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001651 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001652
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001653 if (d40c->phy_chan == NULL) {
1654 dev_err(&d40c->chan.dev->device,
1655 "[%s] Unallocated channel.\n", __func__);
1656 return ERR_PTR(-EINVAL);
1657 }
1658
Jonas Aaberg2a614342010-06-20 21:25:24 +00001659 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001660 d40d = d40_desc_get(d40c);
1661
1662 if (d40d == NULL)
1663 goto err;
1664
Linus Walleij8d318a52010-03-30 15:33:42 +02001665 d40d->lli_len = sgl_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +00001666 d40d->lli_current = 0;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001667 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001668
1669 if (d40c->log_num != D40_PHY_CHAN) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001670
1671 if (d40_pool_lli_alloc(d40d, sgl_len, true) < 0) {
1672 dev_err(&d40c->chan.dev->device,
1673 "[%s] Out of memory\n", __func__);
1674 goto err;
1675 }
1676
Jonas Aaberg698e4732010-08-09 12:08:56 +00001677 (void) d40_log_sg_to_lli(sgl_src,
Linus Walleij8d318a52010-03-30 15:33:42 +02001678 sgl_len,
1679 d40d->lli_log.src,
1680 d40c->log_def.lcsp1,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001681 d40c->dma_cfg.src_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001682
Jonas Aaberg698e4732010-08-09 12:08:56 +00001683 (void) d40_log_sg_to_lli(sgl_dst,
Linus Walleij8d318a52010-03-30 15:33:42 +02001684 sgl_len,
1685 d40d->lli_log.dst,
1686 d40c->log_def.lcsp3,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001687 d40c->dma_cfg.dst_info.data_width);
Linus Walleij8d318a52010-03-30 15:33:42 +02001688 } else {
1689 if (d40_pool_lli_alloc(d40d, sgl_len, false) < 0) {
1690 dev_err(&d40c->chan.dev->device,
1691 "[%s] Out of memory\n", __func__);
1692 goto err;
1693 }
1694
1695 res = d40_phy_sg_to_lli(sgl_src,
1696 sgl_len,
1697 0,
1698 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001699 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02001700 d40c->src_def_cfg,
1701 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001702 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001703
1704 if (res < 0)
1705 goto err;
1706
1707 res = d40_phy_sg_to_lli(sgl_dst,
1708 sgl_len,
1709 0,
1710 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001711 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02001712 d40c->dst_def_cfg,
1713 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00001714 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02001715
1716 if (res < 0)
1717 goto err;
1718
1719 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1720 d40d->lli_pool.size, DMA_TO_DEVICE);
1721 }
1722
1723 dma_async_tx_descriptor_init(&d40d->txd, chan);
1724
1725 d40d->txd.tx_submit = d40_tx_submit;
1726
Jonas Aaberg2a614342010-06-20 21:25:24 +00001727 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001728
1729 return &d40d->txd;
1730err:
Jonas Aaberg2a614342010-06-20 21:25:24 +00001731 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001732 return NULL;
1733}
1734EXPORT_SYMBOL(stedma40_memcpy_sg);
1735
1736bool stedma40_filter(struct dma_chan *chan, void *data)
1737{
1738 struct stedma40_chan_cfg *info = data;
1739 struct d40_chan *d40c =
1740 container_of(chan, struct d40_chan, chan);
1741 int err;
1742
1743 if (data) {
1744 err = d40_validate_conf(d40c, info);
1745 if (!err)
1746 d40c->dma_cfg = *info;
1747 } else
1748 err = d40_config_memcpy(d40c);
1749
1750 return err == 0;
1751}
1752EXPORT_SYMBOL(stedma40_filter);
1753
1754/* DMA ENGINE functions */
1755static int d40_alloc_chan_resources(struct dma_chan *chan)
1756{
1757 int err;
1758 unsigned long flags;
1759 struct d40_chan *d40c =
1760 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00001761 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02001762 spin_lock_irqsave(&d40c->lock, flags);
1763
1764 d40c->completed = chan->cookie = 1;
1765
1766 /*
1767 * If no dma configuration is set (channel_type == 0)
Linus Walleijef1872e2010-06-20 21:24:52 +00001768 * use default configuration (memcpy)
Linus Walleij8d318a52010-03-30 15:33:42 +02001769 */
1770 if (d40c->dma_cfg.channel_type == 0) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001771
Linus Walleij8d318a52010-03-30 15:33:42 +02001772 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001773 if (err) {
1774 dev_err(&d40c->chan.dev->device,
1775 "[%s] Failed to configure memcpy channel\n",
1776 __func__);
1777 goto fail;
1778 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001779 }
Linus Walleijef1872e2010-06-20 21:24:52 +00001780 is_free_phy = (d40c->phy_chan == NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02001781
1782 err = d40_allocate_channel(d40c);
1783 if (err) {
1784 dev_err(&d40c->chan.dev->device,
1785 "[%s] Failed to allocate channel\n", __func__);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001786 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02001787 }
1788
Linus Walleijef1872e2010-06-20 21:24:52 +00001789 /* Fill in basic CFG register values */
1790 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
1791 &d40c->dst_def_cfg, d40c->log_num != D40_PHY_CHAN);
1792
1793 if (d40c->log_num != D40_PHY_CHAN) {
1794 d40_log_cfg(&d40c->dma_cfg,
1795 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
1796
1797 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
1798 d40c->lcpa = d40c->base->lcpa_base +
1799 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
1800 else
1801 d40c->lcpa = d40c->base->lcpa_base +
1802 d40c->dma_cfg.dst_dev_type *
1803 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
1804 }
1805
1806 /*
1807 * Only write channel configuration to the DMA if the physical
1808 * resource is free. In case of multiple logical channels
1809 * on the same physical resource, only the first write is necessary.
1810 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00001811 if (is_free_phy)
1812 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001813fail:
Linus Walleij8d318a52010-03-30 15:33:42 +02001814 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00001815 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02001816}
1817
1818static void d40_free_chan_resources(struct dma_chan *chan)
1819{
1820 struct d40_chan *d40c =
1821 container_of(chan, struct d40_chan, chan);
1822 int err;
1823 unsigned long flags;
1824
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001825 if (d40c->phy_chan == NULL) {
1826 dev_err(&d40c->chan.dev->device,
1827 "[%s] Cannot free unallocated channel\n", __func__);
1828 return;
1829 }
1830
1831
Linus Walleij8d318a52010-03-30 15:33:42 +02001832 spin_lock_irqsave(&d40c->lock, flags);
1833
1834 err = d40_free_dma(d40c);
1835
1836 if (err)
1837 dev_err(&d40c->chan.dev->device,
1838 "[%s] Failed to free channel\n", __func__);
1839 spin_unlock_irqrestore(&d40c->lock, flags);
1840}
1841
1842static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
1843 dma_addr_t dst,
1844 dma_addr_t src,
1845 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001846 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001847{
1848 struct d40_desc *d40d;
1849 struct d40_chan *d40c = container_of(chan, struct d40_chan,
1850 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001851 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001852 int err = 0;
1853
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00001854 if (d40c->phy_chan == NULL) {
1855 dev_err(&d40c->chan.dev->device,
1856 "[%s] Channel is not allocated.\n", __func__);
1857 return ERR_PTR(-EINVAL);
1858 }
1859
Jonas Aaberg2a614342010-06-20 21:25:24 +00001860 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001861 d40d = d40_desc_get(d40c);
1862
1863 if (d40d == NULL) {
1864 dev_err(&d40c->chan.dev->device,
1865 "[%s] Descriptor is NULL\n", __func__);
1866 goto err;
1867 }
1868
Jonas Aaberg2a614342010-06-20 21:25:24 +00001869 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02001870
1871 dma_async_tx_descriptor_init(&d40d->txd, chan);
1872
1873 d40d->txd.tx_submit = d40_tx_submit;
1874
1875 if (d40c->log_num != D40_PHY_CHAN) {
1876
1877 if (d40_pool_lli_alloc(d40d, 1, true) < 0) {
1878 dev_err(&d40c->chan.dev->device,
1879 "[%s] Out of memory\n", __func__);
1880 goto err;
1881 }
1882 d40d->lli_len = 1;
Jonas Aaberg698e4732010-08-09 12:08:56 +00001883 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001884
1885 d40_log_fill_lli(d40d->lli_log.src,
1886 src,
1887 size,
Linus Walleij8d318a52010-03-30 15:33:42 +02001888 d40c->log_def.lcsp1,
1889 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001890 true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001891
1892 d40_log_fill_lli(d40d->lli_log.dst,
1893 dst,
1894 size,
Linus Walleij8d318a52010-03-30 15:33:42 +02001895 d40c->log_def.lcsp3,
1896 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001897 true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001898
1899 } else {
1900
1901 if (d40_pool_lli_alloc(d40d, 1, false) < 0) {
1902 dev_err(&d40c->chan.dev->device,
1903 "[%s] Out of memory\n", __func__);
1904 goto err;
1905 }
1906
1907 err = d40_phy_fill_lli(d40d->lli_phy.src,
1908 src,
1909 size,
1910 d40c->dma_cfg.src_info.psize,
1911 0,
1912 d40c->src_def_cfg,
1913 true,
1914 d40c->dma_cfg.src_info.data_width,
1915 false);
1916 if (err)
1917 goto err_fill_lli;
1918
1919 err = d40_phy_fill_lli(d40d->lli_phy.dst,
1920 dst,
1921 size,
1922 d40c->dma_cfg.dst_info.psize,
1923 0,
1924 d40c->dst_def_cfg,
1925 true,
1926 d40c->dma_cfg.dst_info.data_width,
1927 false);
1928
1929 if (err)
1930 goto err_fill_lli;
1931
1932 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
1933 d40d->lli_pool.size, DMA_TO_DEVICE);
1934 }
1935
Jonas Aaberg2a614342010-06-20 21:25:24 +00001936 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001937 return &d40d->txd;
1938
1939err_fill_lli:
1940 dev_err(&d40c->chan.dev->device,
1941 "[%s] Failed filling in PHY LLI\n", __func__);
1942 d40_pool_lli_free(d40d);
1943err:
Jonas Aaberg2a614342010-06-20 21:25:24 +00001944 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02001945 return NULL;
1946}
1947
1948static int d40_prep_slave_sg_log(struct d40_desc *d40d,
1949 struct d40_chan *d40c,
1950 struct scatterlist *sgl,
1951 unsigned int sg_len,
1952 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001953 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02001954{
1955 dma_addr_t dev_addr = 0;
1956 int total_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001957
1958 if (d40_pool_lli_alloc(d40d, sg_len, true) < 0) {
1959 dev_err(&d40c->chan.dev->device,
1960 "[%s] Out of memory\n", __func__);
1961 return -ENOMEM;
1962 }
1963
1964 d40d->lli_len = sg_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +00001965 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001966
Jonas Aaberg2a614342010-06-20 21:25:24 +00001967 if (direction == DMA_FROM_DEVICE)
Linus Walleij95e14002010-08-04 13:37:45 +02001968 if (d40c->runtime_addr)
1969 dev_addr = d40c->runtime_addr;
1970 else
1971 dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Jonas Aaberg2a614342010-06-20 21:25:24 +00001972 else if (direction == DMA_TO_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_tx[d40c->dma_cfg.dst_dev_type];
1977
Jonas Aaberg2a614342010-06-20 21:25:24 +00001978 else
Linus Walleij8d318a52010-03-30 15:33:42 +02001979 return -EINVAL;
Jonas Aaberg2a614342010-06-20 21:25:24 +00001980
Jonas Aaberg698e4732010-08-09 12:08:56 +00001981 total_size = d40_log_sg_to_dev(sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00001982 &d40d->lli_log,
1983 &d40c->log_def,
1984 d40c->dma_cfg.src_info.data_width,
1985 d40c->dma_cfg.dst_info.data_width,
1986 direction,
Jonas Aaberg698e4732010-08-09 12:08:56 +00001987 dev_addr);
Jonas Aaberg2a614342010-06-20 21:25:24 +00001988
Linus Walleij8d318a52010-03-30 15:33:42 +02001989 if (total_size < 0)
1990 return -EINVAL;
1991
1992 return 0;
1993}
1994
1995static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
1996 struct d40_chan *d40c,
1997 struct scatterlist *sgl,
1998 unsigned int sgl_len,
1999 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002000 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002001{
2002 dma_addr_t src_dev_addr;
2003 dma_addr_t dst_dev_addr;
2004 int res;
2005
2006 if (d40_pool_lli_alloc(d40d, sgl_len, false) < 0) {
2007 dev_err(&d40c->chan.dev->device,
2008 "[%s] Out of memory\n", __func__);
2009 return -ENOMEM;
2010 }
2011
2012 d40d->lli_len = sgl_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +00002013 d40d->lli_current = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002014
2015 if (direction == DMA_FROM_DEVICE) {
2016 dst_dev_addr = 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002017 if (d40c->runtime_addr)
2018 src_dev_addr = d40c->runtime_addr;
2019 else
2020 src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002021 } else if (direction == DMA_TO_DEVICE) {
Linus Walleij95e14002010-08-04 13:37:45 +02002022 if (d40c->runtime_addr)
2023 dst_dev_addr = d40c->runtime_addr;
2024 else
2025 dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
Linus Walleij8d318a52010-03-30 15:33:42 +02002026 src_dev_addr = 0;
2027 } else
2028 return -EINVAL;
2029
2030 res = d40_phy_sg_to_lli(sgl,
2031 sgl_len,
2032 src_dev_addr,
2033 d40d->lli_phy.src,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002034 virt_to_phys(d40d->lli_phy.src),
Linus Walleij8d318a52010-03-30 15:33:42 +02002035 d40c->src_def_cfg,
2036 d40c->dma_cfg.src_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002037 d40c->dma_cfg.src_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002038 if (res < 0)
2039 return res;
2040
2041 res = d40_phy_sg_to_lli(sgl,
2042 sgl_len,
2043 dst_dev_addr,
2044 d40d->lli_phy.dst,
Jonas Aabergaa182ae2010-08-09 12:08:26 +00002045 virt_to_phys(d40d->lli_phy.dst),
Linus Walleij8d318a52010-03-30 15:33:42 +02002046 d40c->dst_def_cfg,
2047 d40c->dma_cfg.dst_info.data_width,
Jonas Aaberg0246e772010-08-09 12:08:10 +00002048 d40c->dma_cfg.dst_info.psize);
Linus Walleij8d318a52010-03-30 15:33:42 +02002049 if (res < 0)
2050 return res;
2051
2052 (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
2053 d40d->lli_pool.size, DMA_TO_DEVICE);
2054 return 0;
2055}
2056
2057static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2058 struct scatterlist *sgl,
2059 unsigned int sg_len,
2060 enum dma_data_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002061 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002062{
2063 struct d40_desc *d40d;
2064 struct d40_chan *d40c = container_of(chan, struct d40_chan,
2065 chan);
Jonas Aaberg2a614342010-06-20 21:25:24 +00002066 unsigned long flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002067 int err;
2068
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002069 if (d40c->phy_chan == NULL) {
2070 dev_err(&d40c->chan.dev->device,
2071 "[%s] Cannot prepare unallocated channel\n", __func__);
2072 return ERR_PTR(-EINVAL);
2073 }
2074
Linus Walleij8d318a52010-03-30 15:33:42 +02002075 if (d40c->dma_cfg.pre_transfer)
2076 d40c->dma_cfg.pre_transfer(chan,
2077 d40c->dma_cfg.pre_transfer_data,
2078 sg_dma_len(sgl));
2079
Jonas Aaberg2a614342010-06-20 21:25:24 +00002080 spin_lock_irqsave(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002081 d40d = d40_desc_get(d40c);
Jonas Aaberg2a614342010-06-20 21:25:24 +00002082 spin_unlock_irqrestore(&d40c->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002083
2084 if (d40d == NULL)
2085 return NULL;
2086
Linus Walleij8d318a52010-03-30 15:33:42 +02002087 if (d40c->log_num != D40_PHY_CHAN)
2088 err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002089 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002090 else
2091 err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002092 direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002093 if (err) {
2094 dev_err(&d40c->chan.dev->device,
2095 "[%s] Failed to prepare %s slave sg job: %d\n",
2096 __func__,
2097 d40c->log_num != D40_PHY_CHAN ? "log" : "phy", err);
2098 return NULL;
2099 }
2100
Jonas Aaberg2a614342010-06-20 21:25:24 +00002101 d40d->txd.flags = dma_flags;
Linus Walleij8d318a52010-03-30 15:33:42 +02002102
2103 dma_async_tx_descriptor_init(&d40d->txd, chan);
2104
2105 d40d->txd.tx_submit = d40_tx_submit;
2106
2107 return &d40d->txd;
2108}
2109
2110static enum dma_status d40_tx_status(struct dma_chan *chan,
2111 dma_cookie_t cookie,
2112 struct dma_tx_state *txstate)
2113{
2114 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2115 dma_cookie_t last_used;
2116 dma_cookie_t last_complete;
2117 int ret;
2118
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002119 if (d40c->phy_chan == NULL) {
2120 dev_err(&d40c->chan.dev->device,
2121 "[%s] Cannot read status of unallocated channel\n",
2122 __func__);
2123 return -EINVAL;
2124 }
2125
Linus Walleij8d318a52010-03-30 15:33:42 +02002126 last_complete = d40c->completed;
2127 last_used = chan->cookie;
2128
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002129 if (d40_is_paused(d40c))
2130 ret = DMA_PAUSED;
2131 else
2132 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002133
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002134 dma_set_tx_state(txstate, last_complete, last_used,
2135 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002136
2137 return ret;
2138}
2139
2140static void d40_issue_pending(struct dma_chan *chan)
2141{
2142 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2143 unsigned long flags;
2144
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002145 if (d40c->phy_chan == NULL) {
2146 dev_err(&d40c->chan.dev->device,
2147 "[%s] Channel is not allocated!\n", __func__);
2148 return;
2149 }
2150
Linus Walleij8d318a52010-03-30 15:33:42 +02002151 spin_lock_irqsave(&d40c->lock, flags);
2152
2153 /* Busy means that pending jobs are already being processed */
2154 if (!d40c->busy)
2155 (void) d40_queue_start(d40c);
2156
2157 spin_unlock_irqrestore(&d40c->lock, flags);
2158}
2159
Linus Walleij95e14002010-08-04 13:37:45 +02002160/* Runtime reconfiguration extension */
2161static void d40_set_runtime_config(struct dma_chan *chan,
2162 struct dma_slave_config *config)
2163{
2164 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2165 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2166 enum dma_slave_buswidth config_addr_width;
2167 dma_addr_t config_addr;
2168 u32 config_maxburst;
2169 enum stedma40_periph_data_width addr_width;
2170 int psize;
2171
2172 if (config->direction == DMA_FROM_DEVICE) {
2173 dma_addr_t dev_addr_rx =
2174 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2175
2176 config_addr = config->src_addr;
2177 if (dev_addr_rx)
2178 dev_dbg(d40c->base->dev,
2179 "channel has a pre-wired RX address %08x "
2180 "overriding with %08x\n",
2181 dev_addr_rx, config_addr);
2182 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2183 dev_dbg(d40c->base->dev,
2184 "channel was not configured for peripheral "
2185 "to memory transfer (%d) overriding\n",
2186 cfg->dir);
2187 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2188
2189 config_addr_width = config->src_addr_width;
2190 config_maxburst = config->src_maxburst;
2191
2192 } else if (config->direction == DMA_TO_DEVICE) {
2193 dma_addr_t dev_addr_tx =
2194 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2195
2196 config_addr = config->dst_addr;
2197 if (dev_addr_tx)
2198 dev_dbg(d40c->base->dev,
2199 "channel has a pre-wired TX address %08x "
2200 "overriding with %08x\n",
2201 dev_addr_tx, config_addr);
2202 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2203 dev_dbg(d40c->base->dev,
2204 "channel was not configured for memory "
2205 "to peripheral transfer (%d) overriding\n",
2206 cfg->dir);
2207 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2208
2209 config_addr_width = config->dst_addr_width;
2210 config_maxburst = config->dst_maxburst;
2211
2212 } else {
2213 dev_err(d40c->base->dev,
2214 "unrecognized channel direction %d\n",
2215 config->direction);
2216 return;
2217 }
2218
2219 switch (config_addr_width) {
2220 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2221 addr_width = STEDMA40_BYTE_WIDTH;
2222 break;
2223 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2224 addr_width = STEDMA40_HALFWORD_WIDTH;
2225 break;
2226 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2227 addr_width = STEDMA40_WORD_WIDTH;
2228 break;
2229 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2230 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2231 break;
2232 default:
2233 dev_err(d40c->base->dev,
2234 "illegal peripheral address width "
2235 "requested (%d)\n",
2236 config->src_addr_width);
2237 return;
2238 }
2239
2240 if (config_maxburst >= 16)
2241 psize = STEDMA40_PSIZE_LOG_16;
2242 else if (config_maxburst >= 8)
2243 psize = STEDMA40_PSIZE_LOG_8;
2244 else if (config_maxburst >= 4)
2245 psize = STEDMA40_PSIZE_LOG_4;
2246 else
2247 psize = STEDMA40_PSIZE_LOG_1;
2248
2249 /* Set up all the endpoint configs */
2250 cfg->src_info.data_width = addr_width;
2251 cfg->src_info.psize = psize;
2252 cfg->src_info.endianess = STEDMA40_LITTLE_ENDIAN;
2253 cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2254 cfg->dst_info.data_width = addr_width;
2255 cfg->dst_info.psize = psize;
2256 cfg->dst_info.endianess = STEDMA40_LITTLE_ENDIAN;
2257 cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2258
2259 /* These settings will take precedence later */
2260 d40c->runtime_addr = config_addr;
2261 d40c->runtime_direction = config->direction;
2262 dev_dbg(d40c->base->dev,
2263 "configured channel %s for %s, data width %d, "
2264 "maxburst %d bytes, LE, no flow control\n",
2265 dma_chan_name(chan),
2266 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
2267 config_addr_width,
2268 config_maxburst);
2269}
2270
Linus Walleij05827632010-05-17 16:30:42 -07002271static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2272 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002273{
2274 unsigned long flags;
2275 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2276
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002277 if (d40c->phy_chan == NULL) {
2278 dev_err(&d40c->chan.dev->device,
2279 "[%s] Channel is not allocated!\n", __func__);
2280 return -EINVAL;
2281 }
2282
Linus Walleij8d318a52010-03-30 15:33:42 +02002283 switch (cmd) {
2284 case DMA_TERMINATE_ALL:
2285 spin_lock_irqsave(&d40c->lock, flags);
2286 d40_term_all(d40c);
2287 spin_unlock_irqrestore(&d40c->lock, flags);
2288 return 0;
2289 case DMA_PAUSE:
2290 return d40_pause(chan);
2291 case DMA_RESUME:
2292 return d40_resume(chan);
Linus Walleij95e14002010-08-04 13:37:45 +02002293 case DMA_SLAVE_CONFIG:
2294 d40_set_runtime_config(chan,
2295 (struct dma_slave_config *) arg);
2296 return 0;
2297 default:
2298 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002299 }
2300
2301 /* Other commands are unimplemented */
2302 return -ENXIO;
2303}
2304
2305/* Initialization functions */
2306
2307static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2308 struct d40_chan *chans, int offset,
2309 int num_chans)
2310{
2311 int i = 0;
2312 struct d40_chan *d40c;
2313
2314 INIT_LIST_HEAD(&dma->channels);
2315
2316 for (i = offset; i < offset + num_chans; i++) {
2317 d40c = &chans[i];
2318 d40c->base = base;
2319 d40c->chan.device = dma;
2320
Linus Walleij8d318a52010-03-30 15:33:42 +02002321 spin_lock_init(&d40c->lock);
2322
2323 d40c->log_num = D40_PHY_CHAN;
2324
Linus Walleij8d318a52010-03-30 15:33:42 +02002325 INIT_LIST_HEAD(&d40c->active);
2326 INIT_LIST_HEAD(&d40c->queue);
2327 INIT_LIST_HEAD(&d40c->client);
2328
Linus Walleij8d318a52010-03-30 15:33:42 +02002329 tasklet_init(&d40c->tasklet, dma_tasklet,
2330 (unsigned long) d40c);
2331
2332 list_add_tail(&d40c->chan.device_node,
2333 &dma->channels);
2334 }
2335}
2336
2337static int __init d40_dmaengine_init(struct d40_base *base,
2338 int num_reserved_chans)
2339{
2340 int err ;
2341
2342 d40_chan_init(base, &base->dma_slave, base->log_chans,
2343 0, base->num_log_chans);
2344
2345 dma_cap_zero(base->dma_slave.cap_mask);
2346 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
2347
2348 base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
2349 base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
2350 base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
2351 base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
2352 base->dma_slave.device_tx_status = d40_tx_status;
2353 base->dma_slave.device_issue_pending = d40_issue_pending;
2354 base->dma_slave.device_control = d40_control;
2355 base->dma_slave.dev = base->dev;
2356
2357 err = dma_async_device_register(&base->dma_slave);
2358
2359 if (err) {
2360 dev_err(base->dev,
2361 "[%s] Failed to register slave channels\n",
2362 __func__);
2363 goto failure1;
2364 }
2365
2366 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2367 base->num_log_chans, base->plat_data->memcpy_len);
2368
2369 dma_cap_zero(base->dma_memcpy.cap_mask);
2370 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
2371
2372 base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
2373 base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
2374 base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
2375 base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
2376 base->dma_memcpy.device_tx_status = d40_tx_status;
2377 base->dma_memcpy.device_issue_pending = d40_issue_pending;
2378 base->dma_memcpy.device_control = d40_control;
2379 base->dma_memcpy.dev = base->dev;
2380 /*
2381 * This controller can only access address at even
2382 * 32bit boundaries, i.e. 2^2
2383 */
2384 base->dma_memcpy.copy_align = 2;
2385
2386 err = dma_async_device_register(&base->dma_memcpy);
2387
2388 if (err) {
2389 dev_err(base->dev,
2390 "[%s] Failed to regsiter memcpy only channels\n",
2391 __func__);
2392 goto failure2;
2393 }
2394
2395 d40_chan_init(base, &base->dma_both, base->phy_chans,
2396 0, num_reserved_chans);
2397
2398 dma_cap_zero(base->dma_both.cap_mask);
2399 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2400 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
2401
2402 base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
2403 base->dma_both.device_free_chan_resources = d40_free_chan_resources;
2404 base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
2405 base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
2406 base->dma_both.device_tx_status = d40_tx_status;
2407 base->dma_both.device_issue_pending = d40_issue_pending;
2408 base->dma_both.device_control = d40_control;
2409 base->dma_both.dev = base->dev;
2410 base->dma_both.copy_align = 2;
2411 err = dma_async_device_register(&base->dma_both);
2412
2413 if (err) {
2414 dev_err(base->dev,
2415 "[%s] Failed to register logical and physical capable channels\n",
2416 __func__);
2417 goto failure3;
2418 }
2419 return 0;
2420failure3:
2421 dma_async_device_unregister(&base->dma_memcpy);
2422failure2:
2423 dma_async_device_unregister(&base->dma_slave);
2424failure1:
2425 return err;
2426}
2427
2428/* Initialization functions. */
2429
2430static int __init d40_phy_res_init(struct d40_base *base)
2431{
2432 int i;
2433 int num_phy_chans_avail = 0;
2434 u32 val[2];
2435 int odd_even_bit = -2;
2436
2437 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2438 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2439
2440 for (i = 0; i < base->num_phy_chans; i++) {
2441 base->phy_res[i].num = i;
2442 odd_even_bit += 2 * ((i % 2) == 0);
2443 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2444 /* Mark security only channels as occupied */
2445 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2446 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
2447 } else {
2448 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2449 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
2450 num_phy_chans_avail++;
2451 }
2452 spin_lock_init(&base->phy_res[i].lock);
2453 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002454
2455 /* Mark disabled channels as occupied */
2456 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002457 int chan = base->plat_data->disabled_channels[i];
2458
2459 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2460 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
2461 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002462 }
2463
Linus Walleij8d318a52010-03-30 15:33:42 +02002464 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2465 num_phy_chans_avail, base->num_phy_chans);
2466
2467 /* Verify settings extended vs standard */
2468 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2469
2470 for (i = 0; i < base->num_phy_chans; i++) {
2471
2472 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2473 (val[0] & 0x3) != 1)
2474 dev_info(base->dev,
2475 "[%s] INFO: channel %d is misconfigured (%d)\n",
2476 __func__, i, val[0] & 0x3);
2477
2478 val[0] = val[0] >> 2;
2479 }
2480
2481 return num_phy_chans_avail;
2482}
2483
2484static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2485{
2486 static const struct d40_reg_val dma_id_regs[] = {
2487 /* Peripheral Id */
2488 { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
2489 { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
2490 /*
2491 * D40_DREG_PERIPHID2 Depends on HW revision:
2492 * MOP500/HREF ED has 0x0008,
2493 * ? has 0x0018,
2494 * HREF V1 has 0x0028
2495 */
2496 { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
2497
2498 /* PCell Id */
2499 { .reg = D40_DREG_CELLID0, .val = 0x000d},
2500 { .reg = D40_DREG_CELLID1, .val = 0x00f0},
2501 { .reg = D40_DREG_CELLID2, .val = 0x0005},
2502 { .reg = D40_DREG_CELLID3, .val = 0x00b1}
2503 };
2504 struct stedma40_platform_data *plat_data;
2505 struct clk *clk = NULL;
2506 void __iomem *virtbase = NULL;
2507 struct resource *res = NULL;
2508 struct d40_base *base = NULL;
2509 int num_log_chans = 0;
2510 int num_phy_chans;
2511 int i;
Linus Walleijf4185592010-06-22 18:06:42 -07002512 u32 val;
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002513 u32 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002514
2515 clk = clk_get(&pdev->dev, NULL);
2516
2517 if (IS_ERR(clk)) {
2518 dev_err(&pdev->dev, "[%s] No matching clock found\n",
2519 __func__);
2520 goto failure;
2521 }
2522
2523 clk_enable(clk);
2524
2525 /* Get IO for DMAC base address */
2526 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2527 if (!res)
2528 goto failure;
2529
2530 if (request_mem_region(res->start, resource_size(res),
2531 D40_NAME " I/O base") == NULL)
2532 goto failure;
2533
2534 virtbase = ioremap(res->start, resource_size(res));
2535 if (!virtbase)
2536 goto failure;
2537
2538 /* HW version check */
2539 for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
2540 if (dma_id_regs[i].val !=
2541 readl(virtbase + dma_id_regs[i].reg)) {
2542 dev_err(&pdev->dev,
2543 "[%s] Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
2544 __func__,
2545 dma_id_regs[i].val,
2546 dma_id_regs[i].reg,
2547 readl(virtbase + dma_id_regs[i].reg));
2548 goto failure;
2549 }
2550 }
2551
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002552 /* Get silicon revision and designer */
Linus Walleijf4185592010-06-22 18:06:42 -07002553 val = readl(virtbase + D40_DREG_PERIPHID2);
Linus Walleij8d318a52010-03-30 15:33:42 +02002554
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002555 if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
2556 D40_HW_DESIGNER) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002557 dev_err(&pdev->dev,
2558 "[%s] Unknown designer! Got %x wanted %x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002559 __func__, val & D40_DREG_PERIPHID2_DESIGNER_MASK,
2560 D40_HW_DESIGNER);
Linus Walleij8d318a52010-03-30 15:33:42 +02002561 goto failure;
2562 }
2563
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002564 rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
2565 D40_DREG_PERIPHID2_REV_POS;
2566
Linus Walleij8d318a52010-03-30 15:33:42 +02002567 /* The number of physical channels on this HW */
2568 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2569
2570 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002571 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002572
2573 plat_data = pdev->dev.platform_data;
2574
2575 /* Count the number of logical channels in use */
2576 for (i = 0; i < plat_data->dev_len; i++)
2577 if (plat_data->dev_rx[i] != 0)
2578 num_log_chans++;
2579
2580 for (i = 0; i < plat_data->dev_len; i++)
2581 if (plat_data->dev_tx[i] != 0)
2582 num_log_chans++;
2583
2584 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2585 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2586 sizeof(struct d40_chan), GFP_KERNEL);
2587
2588 if (base == NULL) {
2589 dev_err(&pdev->dev, "[%s] Out of memory\n", __func__);
2590 goto failure;
2591 }
2592
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002593 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002594 base->clk = clk;
2595 base->num_phy_chans = num_phy_chans;
2596 base->num_log_chans = num_log_chans;
2597 base->phy_start = res->start;
2598 base->phy_size = resource_size(res);
2599 base->virtbase = virtbase;
2600 base->plat_data = plat_data;
2601 base->dev = &pdev->dev;
2602 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2603 base->log_chans = &base->phy_chans[num_phy_chans];
2604
2605 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2606 GFP_KERNEL);
2607 if (!base->phy_res)
2608 goto failure;
2609
2610 base->lookup_phy_chans = kzalloc(num_phy_chans *
2611 sizeof(struct d40_chan *),
2612 GFP_KERNEL);
2613 if (!base->lookup_phy_chans)
2614 goto failure;
2615
2616 if (num_log_chans + plat_data->memcpy_len) {
2617 /*
2618 * The max number of logical channels are event lines for all
2619 * src devices and dst devices
2620 */
2621 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2622 sizeof(struct d40_chan *),
2623 GFP_KERNEL);
2624 if (!base->lookup_log_chans)
2625 goto failure;
2626 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002627
2628 base->lcla_pool.alloc_map = kzalloc(num_phy_chans *
2629 sizeof(struct d40_desc *) *
2630 D40_LCLA_LINK_PER_EVENT_GRP,
Linus Walleij8d318a52010-03-30 15:33:42 +02002631 GFP_KERNEL);
2632 if (!base->lcla_pool.alloc_map)
2633 goto failure;
2634
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002635 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2636 0, SLAB_HWCACHE_ALIGN,
2637 NULL);
2638 if (base->desc_slab == NULL)
2639 goto failure;
2640
Linus Walleij8d318a52010-03-30 15:33:42 +02002641 return base;
2642
2643failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002644 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002645 clk_disable(clk);
2646 clk_put(clk);
2647 }
2648 if (virtbase)
2649 iounmap(virtbase);
2650 if (res)
2651 release_mem_region(res->start,
2652 resource_size(res));
2653 if (virtbase)
2654 iounmap(virtbase);
2655
2656 if (base) {
2657 kfree(base->lcla_pool.alloc_map);
2658 kfree(base->lookup_log_chans);
2659 kfree(base->lookup_phy_chans);
2660 kfree(base->phy_res);
2661 kfree(base);
2662 }
2663
2664 return NULL;
2665}
2666
2667static void __init d40_hw_init(struct d40_base *base)
2668{
2669
2670 static const struct d40_reg_val dma_init_reg[] = {
2671 /* Clock every part of the DMA block from start */
2672 { .reg = D40_DREG_GCC, .val = 0x0000ff01},
2673
2674 /* Interrupts on all logical channels */
2675 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2676 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2677 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2678 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2679 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2680 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2681 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2682 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2683 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2684 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2685 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
2686 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
2687 };
2688 int i;
2689 u32 prmseo[2] = {0, 0};
2690 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
2691 u32 pcmis = 0;
2692 u32 pcicr = 0;
2693
2694 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
2695 writel(dma_init_reg[i].val,
2696 base->virtbase + dma_init_reg[i].reg);
2697
2698 /* Configure all our dma channels to default settings */
2699 for (i = 0; i < base->num_phy_chans; i++) {
2700
2701 activeo[i % 2] = activeo[i % 2] << 2;
2702
2703 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
2704 == D40_ALLOC_PHY) {
2705 activeo[i % 2] |= 3;
2706 continue;
2707 }
2708
2709 /* Enable interrupt # */
2710 pcmis = (pcmis << 1) | 1;
2711
2712 /* Clear interrupt # */
2713 pcicr = (pcicr << 1) | 1;
2714
2715 /* Set channel to physical mode */
2716 prmseo[i % 2] = prmseo[i % 2] << 2;
2717 prmseo[i % 2] |= 1;
2718
2719 }
2720
2721 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
2722 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
2723 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
2724 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
2725
2726 /* Write which interrupt to enable */
2727 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
2728
2729 /* Write which interrupt to clear */
2730 writel(pcicr, base->virtbase + D40_DREG_PCICR);
2731
2732}
2733
Linus Walleij508849a2010-06-20 21:26:07 +00002734static int __init d40_lcla_allocate(struct d40_base *base)
2735{
2736 unsigned long *page_list;
2737 int i, j;
2738 int ret = 0;
2739
2740 /*
2741 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
2742 * To full fill this hardware requirement without wasting 256 kb
2743 * we allocate pages until we get an aligned one.
2744 */
2745 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
2746 GFP_KERNEL);
2747
2748 if (!page_list) {
2749 ret = -ENOMEM;
2750 goto failure;
2751 }
2752
2753 /* Calculating how many pages that are required */
2754 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
2755
2756 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
2757 page_list[i] = __get_free_pages(GFP_KERNEL,
2758 base->lcla_pool.pages);
2759 if (!page_list[i]) {
2760
2761 dev_err(base->dev,
2762 "[%s] Failed to allocate %d pages.\n",
2763 __func__, base->lcla_pool.pages);
2764
2765 for (j = 0; j < i; j++)
2766 free_pages(page_list[j], base->lcla_pool.pages);
2767 goto failure;
2768 }
2769
2770 if ((virt_to_phys((void *)page_list[i]) &
2771 (LCLA_ALIGNMENT - 1)) == 0)
2772 break;
2773 }
2774
2775 for (j = 0; j < i; j++)
2776 free_pages(page_list[j], base->lcla_pool.pages);
2777
2778 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
2779 base->lcla_pool.base = (void *)page_list[i];
2780 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00002781 /*
2782 * After many attempts and no succees with finding the correct
2783 * alignment, try with allocating a big buffer.
2784 */
Linus Walleij508849a2010-06-20 21:26:07 +00002785 dev_warn(base->dev,
2786 "[%s] Failed to get %d pages @ 18 bit align.\n",
2787 __func__, base->lcla_pool.pages);
2788 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
2789 base->num_phy_chans +
2790 LCLA_ALIGNMENT,
2791 GFP_KERNEL);
2792 if (!base->lcla_pool.base_unaligned) {
2793 ret = -ENOMEM;
2794 goto failure;
2795 }
2796
2797 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
2798 LCLA_ALIGNMENT);
2799 }
2800
2801 writel(virt_to_phys(base->lcla_pool.base),
2802 base->virtbase + D40_DREG_LCLA);
2803failure:
2804 kfree(page_list);
2805 return ret;
2806}
2807
Linus Walleij8d318a52010-03-30 15:33:42 +02002808static int __init d40_probe(struct platform_device *pdev)
2809{
2810 int err;
2811 int ret = -ENOENT;
2812 struct d40_base *base;
2813 struct resource *res = NULL;
2814 int num_reserved_chans;
2815 u32 val;
2816
2817 base = d40_hw_detect_init(pdev);
2818
2819 if (!base)
2820 goto failure;
2821
2822 num_reserved_chans = d40_phy_res_init(base);
2823
2824 platform_set_drvdata(pdev, base);
2825
2826 spin_lock_init(&base->interrupt_lock);
2827 spin_lock_init(&base->execmd_lock);
2828
2829 /* Get IO for logical channel parameter address */
2830 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
2831 if (!res) {
2832 ret = -ENOENT;
2833 dev_err(&pdev->dev,
2834 "[%s] No \"lcpa\" memory resource\n",
2835 __func__);
2836 goto failure;
2837 }
2838 base->lcpa_size = resource_size(res);
2839 base->phy_lcpa = res->start;
2840
2841 if (request_mem_region(res->start, resource_size(res),
2842 D40_NAME " I/O lcpa") == NULL) {
2843 ret = -EBUSY;
2844 dev_err(&pdev->dev,
2845 "[%s] Failed to request LCPA region 0x%x-0x%x\n",
2846 __func__, res->start, res->end);
2847 goto failure;
2848 }
2849
2850 /* We make use of ESRAM memory for this. */
2851 val = readl(base->virtbase + D40_DREG_LCPA);
2852 if (res->start != val && val != 0) {
2853 dev_warn(&pdev->dev,
2854 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
2855 __func__, val, res->start);
2856 } else
2857 writel(res->start, base->virtbase + D40_DREG_LCPA);
2858
2859 base->lcpa_base = ioremap(res->start, resource_size(res));
2860 if (!base->lcpa_base) {
2861 ret = -ENOMEM;
2862 dev_err(&pdev->dev,
2863 "[%s] Failed to ioremap LCPA region\n",
2864 __func__);
2865 goto failure;
2866 }
Linus Walleij508849a2010-06-20 21:26:07 +00002867
2868 ret = d40_lcla_allocate(base);
2869 if (ret) {
2870 dev_err(&pdev->dev, "[%s] Failed to allocate LCLA area\n",
Linus Walleij8d318a52010-03-30 15:33:42 +02002871 __func__);
2872 goto failure;
2873 }
2874
Linus Walleij8d318a52010-03-30 15:33:42 +02002875 spin_lock_init(&base->lcla_pool.lock);
2876
Linus Walleij8d318a52010-03-30 15:33:42 +02002877 base->irq = platform_get_irq(pdev, 0);
2878
2879 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
2880
2881 if (ret) {
2882 dev_err(&pdev->dev, "[%s] No IRQ defined\n", __func__);
2883 goto failure;
2884 }
2885
2886 err = d40_dmaengine_init(base, num_reserved_chans);
2887 if (err)
2888 goto failure;
2889
2890 d40_hw_init(base);
2891
2892 dev_info(base->dev, "initialized\n");
2893 return 0;
2894
2895failure:
2896 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002897 if (base->desc_slab)
2898 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02002899 if (base->virtbase)
2900 iounmap(base->virtbase);
Linus Walleij508849a2010-06-20 21:26:07 +00002901 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
2902 free_pages((unsigned long)base->lcla_pool.base,
2903 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00002904
2905 kfree(base->lcla_pool.base_unaligned);
2906
Linus Walleij8d318a52010-03-30 15:33:42 +02002907 if (base->phy_lcpa)
2908 release_mem_region(base->phy_lcpa,
2909 base->lcpa_size);
2910 if (base->phy_start)
2911 release_mem_region(base->phy_start,
2912 base->phy_size);
2913 if (base->clk) {
2914 clk_disable(base->clk);
2915 clk_put(base->clk);
2916 }
2917
2918 kfree(base->lcla_pool.alloc_map);
2919 kfree(base->lookup_log_chans);
2920 kfree(base->lookup_phy_chans);
2921 kfree(base->phy_res);
2922 kfree(base);
2923 }
2924
2925 dev_err(&pdev->dev, "[%s] probe failed\n", __func__);
2926 return ret;
2927}
2928
2929static struct platform_driver d40_driver = {
2930 .driver = {
2931 .owner = THIS_MODULE,
2932 .name = D40_NAME,
2933 },
2934};
2935
2936int __init stedma40_init(void)
2937{
2938 return platform_driver_probe(&d40_driver, d40_probe);
2939}
2940arch_initcall(stedma40_init);