blob: 156b98f661a39c74de8b8a9bdd628a9598d7222b [file] [log] [blame]
Linus Walleij8d318a52010-03-30 15:33:42 +02001/*
Per Forlind49278e2010-12-20 18:31:38 +01002 * Copyright (C) Ericsson AB 2007-2008
3 * Copyright (C) ST-Ericsson SA 2008-2010
Per Forlin661385f2010-10-06 09:05:28 +00004 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
Jonas Aaberg767a9672010-08-09 12:08:34 +00005 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
Linus Walleij8d318a52010-03-30 15:33:42 +02006 * License terms: GNU General Public License (GPL) version 2
Linus Walleij8d318a52010-03-30 15:33:42 +02007 */
8
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +00009#include <linux/dma-mapping.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020010#include <linux/kernel.h>
11#include <linux/slab.h>
Paul Gortmakerf492b212011-07-31 16:17:36 -040012#include <linux/export.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020013#include <linux/dmaengine.h>
14#include <linux/platform_device.h>
15#include <linux/clk.h>
16#include <linux/delay.h>
Narayanan G7fb3e752011-11-17 17:26:41 +053017#include <linux/pm.h>
18#include <linux/pm_runtime.h>
Jonas Aaberg698e4732010-08-09 12:08:56 +000019#include <linux/err.h>
Linus Walleijf4b89762011-06-27 11:33:46 +020020#include <linux/amba/bus.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020021
22#include <plat/ste_dma40.h>
23
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000024#include "dmaengine.h"
Linus Walleij8d318a52010-03-30 15:33:42 +020025#include "ste_dma40_ll.h"
26
27#define D40_NAME "dma40"
28
29#define D40_PHY_CHAN -1
30
31/* For masking out/in 2 bit channel positions */
32#define D40_CHAN_POS(chan) (2 * (chan / 2))
33#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
34
35/* Maximum iterations taken before giving up suspending a channel */
36#define D40_SUSPEND_MAX_IT 500
37
Narayanan G7fb3e752011-11-17 17:26:41 +053038/* Milliseconds */
39#define DMA40_AUTOSUSPEND_DELAY 100
40
Linus Walleij508849a2010-06-20 21:26:07 +000041/* Hardware requirement on LCLA alignment */
42#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000043
44/* Max number of links per event group */
45#define D40_LCLA_LINK_PER_EVENT_GRP 128
46#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
47
Linus Walleij508849a2010-06-20 21:26:07 +000048/* Attempts before giving up to trying to get pages that are aligned */
49#define MAX_LCLA_ALLOC_ATTEMPTS 256
50
51/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020052#define D40_ALLOC_FREE (1 << 31)
53#define D40_ALLOC_PHY (1 << 30)
54#define D40_ALLOC_LOG_FREE 0
55
Linus Walleij8d318a52010-03-30 15:33:42 +020056/**
57 * enum 40_command - The different commands and/or statuses.
58 *
59 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
60 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
61 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
62 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
63 */
64enum d40_command {
65 D40_DMA_STOP = 0,
66 D40_DMA_RUN = 1,
67 D40_DMA_SUSPEND_REQ = 2,
68 D40_DMA_SUSPENDED = 3
69};
70
Narayanan G7fb3e752011-11-17 17:26:41 +053071/*
72 * These are the registers that has to be saved and later restored
73 * when the DMA hw is powered off.
74 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
75 */
76static u32 d40_backup_regs[] = {
77 D40_DREG_LCPA,
78 D40_DREG_LCLA,
79 D40_DREG_PRMSE,
80 D40_DREG_PRMSO,
81 D40_DREG_PRMOE,
82 D40_DREG_PRMOO,
83};
84
85#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
86
87/* TODO: Check if all these registers have to be saved/restored on dma40 v3 */
88static u32 d40_backup_regs_v3[] = {
89 D40_DREG_PSEG1,
90 D40_DREG_PSEG2,
91 D40_DREG_PSEG3,
92 D40_DREG_PSEG4,
93 D40_DREG_PCEG1,
94 D40_DREG_PCEG2,
95 D40_DREG_PCEG3,
96 D40_DREG_PCEG4,
97 D40_DREG_RSEG1,
98 D40_DREG_RSEG2,
99 D40_DREG_RSEG3,
100 D40_DREG_RSEG4,
101 D40_DREG_RCEG1,
102 D40_DREG_RCEG2,
103 D40_DREG_RCEG3,
104 D40_DREG_RCEG4,
105};
106
107#define BACKUP_REGS_SZ_V3 ARRAY_SIZE(d40_backup_regs_v3)
108
109static u32 d40_backup_regs_chan[] = {
110 D40_CHAN_REG_SSCFG,
111 D40_CHAN_REG_SSELT,
112 D40_CHAN_REG_SSPTR,
113 D40_CHAN_REG_SSLNK,
114 D40_CHAN_REG_SDCFG,
115 D40_CHAN_REG_SDELT,
116 D40_CHAN_REG_SDPTR,
117 D40_CHAN_REG_SDLNK,
118};
119
Linus Walleij8d318a52010-03-30 15:33:42 +0200120/**
121 * struct d40_lli_pool - Structure for keeping LLIs in memory
122 *
123 * @base: Pointer to memory area when the pre_alloc_lli's are not large
124 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
125 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100126 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200127 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
128 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
129 * one buffer to one buffer.
130 */
131struct d40_lli_pool {
132 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000133 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100134 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200135 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000136 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200137};
138
139/**
140 * struct d40_desc - A descriptor is one DMA job.
141 *
142 * @lli_phy: LLI settings for physical channel. Both src and dst=
143 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
144 * lli_len equals one.
145 * @lli_log: Same as above but for logical channels.
146 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000147 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300148 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000149 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200150 * @txd: DMA engine struct. Used for among other things for communication
151 * during a transfer.
152 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200153 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530154 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200155 *
156 * This descriptor is used for both logical and physical transfers.
157 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200158struct d40_desc {
159 /* LLI physical */
160 struct d40_phy_lli_bidir lli_phy;
161 /* LLI logical */
162 struct d40_log_lli_bidir lli_log;
163
164 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000165 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000166 int lli_current;
167 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200168
169 struct dma_async_tx_descriptor txd;
170 struct list_head node;
171
Linus Walleij8d318a52010-03-30 15:33:42 +0200172 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100173 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200174};
175
176/**
177 * struct d40_lcla_pool - LCLA pool settings and data.
178 *
Linus Walleij508849a2010-06-20 21:26:07 +0000179 * @base: The virtual address of LCLA. 18 bit aligned.
180 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
181 * This pointer is only there for clean-up on error.
182 * @pages: The number of pages needed for all physical channels.
183 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200184 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000185 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200186 */
187struct d40_lcla_pool {
188 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100189 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000190 void *base_unaligned;
191 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200192 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000193 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200194};
195
196/**
197 * struct d40_phy_res - struct for handling eventlines mapped to physical
198 * channels.
199 *
200 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530201 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200202 * @num: The physical channel number of this entity.
203 * @allocated_src: Bit mapped to show which src event line's are mapped to
204 * this physical channel. Can also be free or physically allocated.
205 * @allocated_dst: Same as for src but is dst.
206 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000207 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200208 */
209struct d40_phy_res {
210 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530211 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200212 int num;
213 u32 allocated_src;
214 u32 allocated_dst;
215};
216
217struct d40_base;
218
219/**
220 * struct d40_chan - Struct that describes a channel.
221 *
222 * @lock: A spinlock to protect this struct.
223 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200224 * @pending_tx: The number of pending transfers. Used between interrupt handler
225 * and tasklet.
226 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000227 * @phy_chan: Pointer to physical channel which this instance runs on. If this
228 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200229 * @chan: DMA engine handle.
230 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
231 * transfer and call client callback.
232 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200233 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200234 * @active: Active descriptor.
235 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200236 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200237 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000238 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200239 * @base: Pointer to the device instance struct.
240 * @src_def_cfg: Default cfg register setting for src.
241 * @dst_def_cfg: Default cfg register setting for dst.
242 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200243 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200244 * @runtime_addr: runtime configured address.
245 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200246 *
247 * This struct can either "be" a logical or a physical channel.
248 */
249struct d40_chan {
250 spinlock_t lock;
251 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200252 int pending_tx;
253 bool busy;
254 struct d40_phy_res *phy_chan;
255 struct dma_chan chan;
256 struct tasklet_struct tasklet;
257 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200258 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200259 struct list_head active;
260 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200261 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200262 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000263 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200264 struct d40_base *base;
265 /* Default register configurations */
266 u32 src_def_cfg;
267 u32 dst_def_cfg;
268 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200269 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200270 /* Runtime reconfiguration */
271 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530272 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200273};
274
275/**
276 * struct d40_base - The big global struct, one for each probe'd instance.
277 *
278 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
279 * @execmd_lock: Lock for execute command usage since several channels share
280 * the same physical register.
281 * @dev: The device structure.
282 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700283 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200284 * @clk: Pointer to the DMA clock structure.
285 * @phy_start: Physical memory start of the DMA registers.
286 * @phy_size: Size of the DMA register map.
287 * @irq: The IRQ number.
288 * @num_phy_chans: The number of physical channels. Read from HW. This
289 * is the number of available channels for this driver, not counting "Secure
290 * mode" allocated physical channels.
291 * @num_log_chans: The number of logical channels. Calculated from
292 * num_phy_chans.
293 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
294 * @dma_slave: dma_device channels that can do only do slave transfers.
295 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530296 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200297 * @log_chans: Room for all possible logical channels in system.
298 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
299 * to log_chans entries.
300 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
301 * to phy_chans entries.
302 * @plat_data: Pointer to provided platform_data which is the driver
303 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530304 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200305 * @phy_res: Vector containing all physical channels.
306 * @lcla_pool: lcla pool settings and data.
307 * @lcpa_base: The virtual mapped address of LCPA.
308 * @phy_lcpa: The physical address of the LCPA.
309 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000310 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530311 * @reg_val_backup: Here the values of some hardware registers are stored
312 * before the DMA is powered off. They are restored when the power is back on.
313 * @reg_val_backup_v3: Backup of registers that only exits on dma40 v3 and
314 * later.
315 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
316 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
317 * @initialized: true if the dma has been initialized
Linus Walleij8d318a52010-03-30 15:33:42 +0200318 */
319struct d40_base {
320 spinlock_t interrupt_lock;
321 spinlock_t execmd_lock;
322 struct device *dev;
323 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700324 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200325 struct clk *clk;
326 phys_addr_t phy_start;
327 resource_size_t phy_size;
328 int irq;
329 int num_phy_chans;
330 int num_log_chans;
331 struct dma_device dma_both;
332 struct dma_device dma_slave;
333 struct dma_device dma_memcpy;
334 struct d40_chan *phy_chans;
335 struct d40_chan *log_chans;
336 struct d40_chan **lookup_log_chans;
337 struct d40_chan **lookup_phy_chans;
338 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530339 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200340 /* Physical half channels */
341 struct d40_phy_res *phy_res;
342 struct d40_lcla_pool lcla_pool;
343 void *lcpa_base;
344 dma_addr_t phy_lcpa;
345 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000346 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530347 u32 reg_val_backup[BACKUP_REGS_SZ];
348 u32 reg_val_backup_v3[BACKUP_REGS_SZ_V3];
349 u32 *reg_val_backup_chan;
350 u16 gcc_pwr_off_mask;
351 bool initialized;
Linus Walleij8d318a52010-03-30 15:33:42 +0200352};
353
354/**
355 * struct d40_interrupt_lookup - lookup table for interrupt handler
356 *
357 * @src: Interrupt mask register.
358 * @clr: Interrupt clear register.
359 * @is_error: true if this is an error interrupt.
360 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
361 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
362 */
363struct d40_interrupt_lookup {
364 u32 src;
365 u32 clr;
366 bool is_error;
367 int offset;
368};
369
370/**
371 * struct d40_reg_val - simple lookup struct
372 *
373 * @reg: The register.
374 * @val: The value that belongs to the register in reg.
375 */
376struct d40_reg_val {
377 unsigned int reg;
378 unsigned int val;
379};
380
Rabin Vincent262d2912011-01-25 11:18:05 +0100381static struct device *chan2dev(struct d40_chan *d40c)
382{
383 return &d40c->chan.dev->device;
384}
385
Rabin Vincent724a8572011-01-25 11:18:08 +0100386static bool chan_is_physical(struct d40_chan *chan)
387{
388 return chan->log_num == D40_PHY_CHAN;
389}
390
391static bool chan_is_logical(struct d40_chan *chan)
392{
393 return !chan_is_physical(chan);
394}
395
Rabin Vincent8ca84682011-01-25 11:18:07 +0100396static void __iomem *chan_base(struct d40_chan *chan)
397{
398 return chan->base->virtbase + D40_DREG_PCBASE +
399 chan->phy_chan->num * D40_DREG_PCDELTA;
400}
401
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100402#define d40_err(dev, format, arg...) \
403 dev_err(dev, "[%s] " format, __func__, ## arg)
404
405#define chan_err(d40c, format, arg...) \
406 d40_err(chan2dev(d40c), format, ## arg)
407
Rabin Vincentb00f9382011-01-25 11:18:15 +0100408static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100409 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200410{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100411 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200412 u32 align;
413 void *base;
414
415 if (is_log)
416 align = sizeof(struct d40_log_lli);
417 else
418 align = sizeof(struct d40_phy_lli);
419
420 if (lli_len == 1) {
421 base = d40d->lli_pool.pre_alloc_lli;
422 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
423 d40d->lli_pool.base = NULL;
424 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100425 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200426
427 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
428 d40d->lli_pool.base = base;
429
430 if (d40d->lli_pool.base == NULL)
431 return -ENOMEM;
432 }
433
434 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100435 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100436 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100437
438 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200439 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100440 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100441 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100442
443 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
444 d40d->lli_phy.src,
445 d40d->lli_pool.size,
446 DMA_TO_DEVICE);
447
448 if (dma_mapping_error(d40c->base->dev,
449 d40d->lli_pool.dma_addr)) {
450 kfree(d40d->lli_pool.base);
451 d40d->lli_pool.base = NULL;
452 d40d->lli_pool.dma_addr = 0;
453 return -ENOMEM;
454 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200455 }
456
457 return 0;
458}
459
Rabin Vincentb00f9382011-01-25 11:18:15 +0100460static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200461{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100462 if (d40d->lli_pool.dma_addr)
463 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
464 d40d->lli_pool.size, DMA_TO_DEVICE);
465
Linus Walleij8d318a52010-03-30 15:33:42 +0200466 kfree(d40d->lli_pool.base);
467 d40d->lli_pool.base = NULL;
468 d40d->lli_pool.size = 0;
469 d40d->lli_log.src = NULL;
470 d40d->lli_log.dst = NULL;
471 d40d->lli_phy.src = NULL;
472 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200473}
474
Jonas Aaberg698e4732010-08-09 12:08:56 +0000475static int d40_lcla_alloc_one(struct d40_chan *d40c,
476 struct d40_desc *d40d)
477{
478 unsigned long flags;
479 int i;
480 int ret = -EINVAL;
481 int p;
482
483 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
484
485 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
486
487 /*
488 * Allocate both src and dst at the same time, therefore the half
489 * start on 1 since 0 can't be used since zero is used as end marker.
490 */
491 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
492 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
493 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
494 d40d->lcla_alloc++;
495 ret = i;
496 break;
497 }
498 }
499
500 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
501
502 return ret;
503}
504
505static int d40_lcla_free_all(struct d40_chan *d40c,
506 struct d40_desc *d40d)
507{
508 unsigned long flags;
509 int i;
510 int ret = -EINVAL;
511
Rabin Vincent724a8572011-01-25 11:18:08 +0100512 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000513 return 0;
514
515 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
516
517 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
518 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
519 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
520 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
521 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
522 d40d->lcla_alloc--;
523 if (d40d->lcla_alloc == 0) {
524 ret = 0;
525 break;
526 }
527 }
528 }
529
530 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
531
532 return ret;
533
534}
535
Linus Walleij8d318a52010-03-30 15:33:42 +0200536static void d40_desc_remove(struct d40_desc *d40d)
537{
538 list_del(&d40d->node);
539}
540
541static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
542{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000543 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200544
545 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000546 struct d40_desc *d;
547 struct d40_desc *_d;
548
Narayanan G7fb3e752011-11-17 17:26:41 +0530549 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200550 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200551 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000552 desc = d;
553 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000554 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200555 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530556 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200557 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000558
559 if (!desc)
560 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
561
562 if (desc)
563 INIT_LIST_HEAD(&desc->node);
564
565 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200566}
567
568static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
569{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000570
Rabin Vincentb00f9382011-01-25 11:18:15 +0100571 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000572 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000573 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200574}
575
576static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
577{
578 list_add_tail(&desc->node, &d40c->active);
579}
580
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100581static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
582{
583 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
584 struct d40_phy_lli *lli_src = desc->lli_phy.src;
585 void __iomem *base = chan_base(chan);
586
587 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
588 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
589 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
590 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
591
592 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
593 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
594 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
595 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
596}
597
Rabin Vincente65889c2011-01-25 11:18:31 +0100598static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
599{
600 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
601 struct d40_log_lli_bidir *lli = &desc->lli_log;
602 int lli_current = desc->lli_current;
603 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100604 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100605 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100606 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530607 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100608 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100609
Rabin Vincent0c842b52011-01-25 11:18:35 +0100610 /*
611 * We may have partially running cyclic transfers, in case we did't get
612 * enough LCLA entries.
613 */
614 linkback = cyclic && lli_current == 0;
615
616 /*
617 * For linkback, we need one LCLA even with only one link, because we
618 * can't link back to the one in LCPA space
619 */
620 if (linkback || (lli_len - lli_current > 1)) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100621 curr_lcla = d40_lcla_alloc_one(chan, desc);
Rabin Vincent0c842b52011-01-25 11:18:35 +0100622 first_lcla = curr_lcla;
623 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100624
Rabin Vincent0c842b52011-01-25 11:18:35 +0100625 /*
626 * For linkback, we normally load the LCPA in the loop since we need to
627 * link it to the second LCLA and not the first. However, if we
628 * couldn't even get a first LCLA, then we have to run in LCPA and
629 * reload manually.
630 */
631 if (!linkback || curr_lcla == -EINVAL) {
632 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100633
Rabin Vincent0c842b52011-01-25 11:18:35 +0100634 if (curr_lcla == -EINVAL)
635 flags |= LLI_TERM_INT;
636
637 d40_log_lli_lcpa_write(chan->lcpa,
638 &lli->dst[lli_current],
639 &lli->src[lli_current],
640 curr_lcla,
641 flags);
642 lli_current++;
643 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100644
645 if (curr_lcla < 0)
646 goto out;
647
Rabin Vincente65889c2011-01-25 11:18:31 +0100648 for (; lli_current < lli_len; lli_current++) {
649 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
650 8 * curr_lcla * 2;
651 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100652 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100653 int next_lcla;
654
655 if (lli_current + 1 < lli_len)
656 next_lcla = d40_lcla_alloc_one(chan, desc);
657 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100658 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100659
Rabin Vincent0c842b52011-01-25 11:18:35 +0100660 if (cyclic || next_lcla == -EINVAL)
661 flags |= LLI_TERM_INT;
662
663 if (linkback && curr_lcla == first_lcla) {
664 /* First link goes in both LCPA and LCLA */
665 d40_log_lli_lcpa_write(chan->lcpa,
666 &lli->dst[lli_current],
667 &lli->src[lli_current],
668 next_lcla, flags);
669 }
670
671 /*
672 * One unused LCLA in the cyclic case if the very first
673 * next_lcla fails...
674 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100675 d40_log_lli_lcla_write(lcla,
676 &lli->dst[lli_current],
677 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100678 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100679
Narayanan G28c7a192011-11-22 13:56:55 +0530680 /*
681 * Cache maintenance is not needed if lcla is
682 * mapped in esram
683 */
684 if (!use_esram_lcla) {
685 dma_sync_single_range_for_device(chan->base->dev,
686 pool->dma_addr, lcla_offset,
687 2 * sizeof(struct d40_log_lli),
688 DMA_TO_DEVICE);
689 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100690 curr_lcla = next_lcla;
691
Rabin Vincent0c842b52011-01-25 11:18:35 +0100692 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100693 lli_current++;
694 break;
695 }
696 }
697
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100698out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100699 desc->lli_current = lli_current;
700}
701
Jonas Aaberg698e4732010-08-09 12:08:56 +0000702static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
703{
Rabin Vincent724a8572011-01-25 11:18:08 +0100704 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100705 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000706 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100707 } else
708 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000709}
710
Linus Walleij8d318a52010-03-30 15:33:42 +0200711static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
712{
713 struct d40_desc *d;
714
715 if (list_empty(&d40c->active))
716 return NULL;
717
718 d = list_first_entry(&d40c->active,
719 struct d40_desc,
720 node);
721 return d;
722}
723
Per Forlin74043682011-08-29 13:33:34 +0200724/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200725static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
726{
Per Forlin74043682011-08-29 13:33:34 +0200727 d40_desc_remove(desc);
728 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200729 list_add_tail(&desc->node, &d40c->pending_queue);
730}
731
732static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
733{
734 struct d40_desc *d;
735
736 if (list_empty(&d40c->pending_queue))
737 return NULL;
738
739 d = list_first_entry(&d40c->pending_queue,
740 struct d40_desc,
741 node);
742 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200743}
744
745static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
746{
747 struct d40_desc *d;
748
749 if (list_empty(&d40c->queue))
750 return NULL;
751
752 d = list_first_entry(&d40c->queue,
753 struct d40_desc,
754 node);
755 return d;
756}
757
Per Forlind49278e2010-12-20 18:31:38 +0100758static int d40_psize_2_burst_size(bool is_log, int psize)
759{
760 if (is_log) {
761 if (psize == STEDMA40_PSIZE_LOG_1)
762 return 1;
763 } else {
764 if (psize == STEDMA40_PSIZE_PHY_1)
765 return 1;
766 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200767
Per Forlind49278e2010-12-20 18:31:38 +0100768 return 2 << psize;
769}
770
771/*
772 * The dma only supports transmitting packages up to
773 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
774 * dma elements required to send the entire sg list
775 */
776static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
777{
778 int dmalen;
779 u32 max_w = max(data_width1, data_width2);
780 u32 min_w = min(data_width1, data_width2);
781 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
782
783 if (seg_max > STEDMA40_MAX_SEG_SIZE)
784 seg_max -= (1 << max_w);
785
786 if (!IS_ALIGNED(size, 1 << max_w))
787 return -EINVAL;
788
789 if (size <= seg_max)
790 dmalen = 1;
791 else {
792 dmalen = size / seg_max;
793 if (dmalen * seg_max < size)
794 dmalen++;
795 }
796 return dmalen;
797}
798
799static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
800 u32 data_width1, u32 data_width2)
801{
802 struct scatterlist *sg;
803 int i;
804 int len = 0;
805 int ret;
806
807 for_each_sg(sgl, sg, sg_len, i) {
808 ret = d40_size_2_dmalen(sg_dma_len(sg),
809 data_width1, data_width2);
810 if (ret < 0)
811 return ret;
812 len += ret;
813 }
814 return len;
815}
816
Narayanan G7fb3e752011-11-17 17:26:41 +0530817
818#ifdef CONFIG_PM
819static void dma40_backup(void __iomem *baseaddr, u32 *backup,
820 u32 *regaddr, int num, bool save)
821{
822 int i;
823
824 for (i = 0; i < num; i++) {
825 void __iomem *addr = baseaddr + regaddr[i];
826
827 if (save)
828 backup[i] = readl_relaxed(addr);
829 else
830 writel_relaxed(backup[i], addr);
831 }
832}
833
834static void d40_save_restore_registers(struct d40_base *base, bool save)
835{
836 int i;
837
838 /* Save/Restore channel specific registers */
839 for (i = 0; i < base->num_phy_chans; i++) {
840 void __iomem *addr;
841 int idx;
842
843 if (base->phy_res[i].reserved)
844 continue;
845
846 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
847 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
848
849 dma40_backup(addr, &base->reg_val_backup_chan[idx],
850 d40_backup_regs_chan,
851 ARRAY_SIZE(d40_backup_regs_chan),
852 save);
853 }
854
855 /* Save/Restore global registers */
856 dma40_backup(base->virtbase, base->reg_val_backup,
857 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
858 save);
859
860 /* Save/Restore registers only existing on dma40 v3 and later */
861 if (base->rev >= 3)
862 dma40_backup(base->virtbase, base->reg_val_backup_v3,
863 d40_backup_regs_v3,
864 ARRAY_SIZE(d40_backup_regs_v3),
865 save);
866}
867#else
868static void d40_save_restore_registers(struct d40_base *base, bool save)
869{
870}
871#endif
Linus Walleij8d318a52010-03-30 15:33:42 +0200872
873static int d40_channel_execute_command(struct d40_chan *d40c,
874 enum d40_command command)
875{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000876 u32 status;
877 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200878 void __iomem *active_reg;
879 int ret = 0;
880 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000881 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200882
883 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
884
885 if (d40c->phy_chan->num % 2 == 0)
886 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
887 else
888 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
889
890 if (command == D40_DMA_SUSPEND_REQ) {
891 status = (readl(active_reg) &
892 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
893 D40_CHAN_POS(d40c->phy_chan->num);
894
895 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
896 goto done;
897 }
898
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000899 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
900 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
901 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200902
903 if (command == D40_DMA_SUSPEND_REQ) {
904
905 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
906 status = (readl(active_reg) &
907 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
908 D40_CHAN_POS(d40c->phy_chan->num);
909
910 cpu_relax();
911 /*
912 * Reduce the number of bus accesses while
913 * waiting for the DMA to suspend.
914 */
915 udelay(3);
916
917 if (status == D40_DMA_STOP ||
918 status == D40_DMA_SUSPENDED)
919 break;
920 }
921
922 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100923 chan_err(d40c,
924 "unable to suspend the chl %d (log: %d) status %x\n",
925 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200926 status);
927 dump_stack();
928 ret = -EBUSY;
929 }
930
931 }
932done:
933 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
934 return ret;
935}
936
937static void d40_term_all(struct d40_chan *d40c)
938{
939 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +0200940 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200941
942 /* Release active descriptors */
943 while ((d40d = d40_first_active_get(d40c))) {
944 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200945 d40_desc_free(d40c, d40d);
946 }
947
948 /* Release queued descriptors waiting for transfer */
949 while ((d40d = d40_first_queued(d40c))) {
950 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200951 d40_desc_free(d40c, d40d);
952 }
953
Per Forlina8f30672011-06-26 23:29:52 +0200954 /* Release pending descriptors */
955 while ((d40d = d40_first_pending(d40c))) {
956 d40_desc_remove(d40d);
957 d40_desc_free(d40c, d40d);
958 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200959
Per Forlin74043682011-08-29 13:33:34 +0200960 /* Release client owned descriptors */
961 if (!list_empty(&d40c->client))
962 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
963 d40_desc_remove(d40d);
964 d40_desc_free(d40c, d40d);
965 }
966
Per Forlin82babbb362011-08-29 13:33:35 +0200967 /* Release descriptors in prepare queue */
968 if (!list_empty(&d40c->prepare_queue))
969 list_for_each_entry_safe(d40d, _d,
970 &d40c->prepare_queue, node) {
971 d40_desc_remove(d40d);
972 d40_desc_free(d40c, d40d);
973 }
Per Forlin74043682011-08-29 13:33:34 +0200974
Linus Walleij8d318a52010-03-30 15:33:42 +0200975 d40c->pending_tx = 0;
976 d40c->busy = false;
977}
978
Rabin Vincent262d2912011-01-25 11:18:05 +0100979static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
980 u32 event, int reg)
981{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100982 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100983 int tries;
984
985 if (!enable) {
986 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
987 | ~D40_EVENTLINE_MASK(event), addr);
988 return;
989 }
990
991 /*
992 * The hardware sometimes doesn't register the enable when src and dst
993 * event lines are active on the same logical channel. Retry to ensure
994 * it does. Usually only one retry is sufficient.
995 */
996 tries = 100;
997 while (--tries) {
998 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
999 | ~D40_EVENTLINE_MASK(event), addr);
1000
1001 if (readl(addr) & D40_EVENTLINE_MASK(event))
1002 break;
1003 }
1004
1005 if (tries != 99)
1006 dev_dbg(chan2dev(d40c),
1007 "[%s] workaround enable S%cLNK (%d tries)\n",
1008 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1009 100 - tries);
1010
1011 WARN_ON(!tries);
1012}
1013
Linus Walleij8d318a52010-03-30 15:33:42 +02001014static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
1015{
Linus Walleij8d318a52010-03-30 15:33:42 +02001016 unsigned long flags;
1017
Linus Walleij8d318a52010-03-30 15:33:42 +02001018 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1019
1020 /* Enable event line connected to device (or memcpy) */
1021 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1022 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1023 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1024
Rabin Vincent262d2912011-01-25 11:18:05 +01001025 __d40_config_set_event(d40c, do_enable, event,
1026 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001027 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001028
Linus Walleij8d318a52010-03-30 15:33:42 +02001029 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1030 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1031
Rabin Vincent262d2912011-01-25 11:18:05 +01001032 __d40_config_set_event(d40c, do_enable, event,
1033 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001034 }
1035
1036 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1037}
1038
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001039static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001040{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001041 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001042 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001043
Rabin Vincent8ca84682011-01-25 11:18:07 +01001044 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1045 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001046
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001047 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001048}
1049
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001050static u32 d40_get_prmo(struct d40_chan *d40c)
1051{
1052 static const unsigned int phy_map[] = {
1053 [STEDMA40_PCHAN_BASIC_MODE]
1054 = D40_DREG_PRMO_PCHAN_BASIC,
1055 [STEDMA40_PCHAN_MODULO_MODE]
1056 = D40_DREG_PRMO_PCHAN_MODULO,
1057 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1058 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1059 };
1060 static const unsigned int log_map[] = {
1061 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1062 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1063 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1064 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1065 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1066 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1067 };
1068
Rabin Vincent724a8572011-01-25 11:18:08 +01001069 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001070 return phy_map[d40c->dma_cfg.mode_opt];
1071 else
1072 return log_map[d40c->dma_cfg.mode_opt];
1073}
1074
Jonas Aabergb55912c2010-08-09 12:08:02 +00001075static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001076{
1077 u32 addr_base;
1078 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001079
1080 /* Odd addresses are even addresses + 4 */
1081 addr_base = (d40c->phy_chan->num % 2) * 4;
1082 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001083 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001084 D40_CHAN_POS(d40c->phy_chan->num);
1085 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1086
1087 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001088 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001089
1090 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1091
Rabin Vincent724a8572011-01-25 11:18:08 +01001092 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001093 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1094 & D40_SREG_ELEM_LOG_LIDX_MASK;
1095 void __iomem *chanbase = chan_base(d40c);
1096
Linus Walleij8d318a52010-03-30 15:33:42 +02001097 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001098 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1099 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001100
Jonas Aabergb55912c2010-08-09 12:08:02 +00001101 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001102 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1103 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301104
1105 /* Clear LNK which will be used by d40_chan_has_events() */
1106 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1107 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001108 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001109}
1110
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001111static u32 d40_residue(struct d40_chan *d40c)
1112{
1113 u32 num_elt;
1114
Rabin Vincent724a8572011-01-25 11:18:08 +01001115 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001116 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1117 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001118 else {
1119 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1120 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1121 >> D40_SREG_ELEM_PHY_ECNT_POS;
1122 }
1123
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001124 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1125}
1126
1127static bool d40_tx_is_linked(struct d40_chan *d40c)
1128{
1129 bool is_link;
1130
Rabin Vincent724a8572011-01-25 11:18:08 +01001131 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001132 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1133 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001134 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1135 & D40_SREG_LNK_PHYS_LNK_MASK;
1136
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001137 return is_link;
1138}
1139
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001140static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001141{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001142 int res = 0;
1143 unsigned long flags;
1144
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001145 if (!d40c->busy)
1146 return 0;
1147
Narayanan G7fb3e752011-11-17 17:26:41 +05301148 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001149 spin_lock_irqsave(&d40c->lock, flags);
1150
1151 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1152 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +01001153 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001154 d40_config_set_event(d40c, false);
1155 /* Resume the other logical channels if any */
1156 if (d40_chan_has_events(d40c))
1157 res = d40_channel_execute_command(d40c,
1158 D40_DMA_RUN);
1159 }
1160 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301161 pm_runtime_mark_last_busy(d40c->base->dev);
1162 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001163 spin_unlock_irqrestore(&d40c->lock, flags);
1164 return res;
1165}
1166
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001167static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001168{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001169 int res = 0;
1170 unsigned long flags;
1171
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001172 if (!d40c->busy)
1173 return 0;
1174
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001175 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301176 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001177 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +01001178 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001179 res = d40_channel_execute_command(d40c,
1180 D40_DMA_SUSPEND_REQ);
1181 goto no_suspend;
1182 }
1183
1184 /* If bytes left to transfer or linked tx resume job */
1185 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
1186
Rabin Vincent724a8572011-01-25 11:18:08 +01001187 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001188 d40_config_set_event(d40c, true);
1189
1190 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1191 }
1192
1193no_suspend:
Narayanan G7fb3e752011-11-17 17:26:41 +05301194 pm_runtime_mark_last_busy(d40c->base->dev);
1195 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001196 spin_unlock_irqrestore(&d40c->lock, flags);
1197 return res;
1198}
1199
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001200static int d40_terminate_all(struct d40_chan *chan)
1201{
1202 unsigned long flags;
1203 int ret = 0;
1204
1205 ret = d40_pause(chan);
1206 if (!ret && chan_is_physical(chan))
1207 ret = d40_channel_execute_command(chan, D40_DMA_STOP);
1208
1209 spin_lock_irqsave(&chan->lock, flags);
1210 d40_term_all(chan);
1211 spin_unlock_irqrestore(&chan->lock, flags);
1212
1213 return ret;
1214}
1215
Linus Walleij8d318a52010-03-30 15:33:42 +02001216static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1217{
1218 struct d40_chan *d40c = container_of(tx->chan,
1219 struct d40_chan,
1220 chan);
1221 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1222 unsigned long flags;
1223
1224 spin_lock_irqsave(&d40c->lock, flags);
1225
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001226 d40c->chan.cookie++;
1227
1228 if (d40c->chan.cookie < 0)
1229 d40c->chan.cookie = 1;
1230
1231 d40d->txd.cookie = d40c->chan.cookie;
1232
Linus Walleij8d318a52010-03-30 15:33:42 +02001233 d40_desc_queue(d40c, d40d);
1234
1235 spin_unlock_irqrestore(&d40c->lock, flags);
1236
1237 return tx->cookie;
1238}
1239
1240static int d40_start(struct d40_chan *d40c)
1241{
Linus Walleijf4185592010-06-22 18:06:42 -07001242 if (d40c->base->rev == 0) {
1243 int err;
1244
Rabin Vincent724a8572011-01-25 11:18:08 +01001245 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -07001246 err = d40_channel_execute_command(d40c,
1247 D40_DMA_SUSPEND_REQ);
1248 if (err)
1249 return err;
1250 }
1251 }
1252
Rabin Vincent724a8572011-01-25 11:18:08 +01001253 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001254 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001255
Jonas Aaberg0c322692010-06-20 21:25:46 +00001256 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001257}
1258
1259static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1260{
1261 struct d40_desc *d40d;
1262 int err;
1263
1264 /* Start queued jobs, if any */
1265 d40d = d40_first_queued(d40c);
1266
1267 if (d40d != NULL) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301268 if (!d40c->busy)
1269 d40c->busy = true;
1270
1271 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001272
1273 /* Remove from queue */
1274 d40_desc_remove(d40d);
1275
1276 /* Add to active queue */
1277 d40_desc_submit(d40c, d40d);
1278
Rabin Vincent7d83a852011-01-25 11:18:06 +01001279 /* Initiate DMA job */
1280 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001281
Rabin Vincent7d83a852011-01-25 11:18:06 +01001282 /* Start dma job */
1283 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001284
Rabin Vincent7d83a852011-01-25 11:18:06 +01001285 if (err)
1286 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001287 }
1288
1289 return d40d;
1290}
1291
1292/* called from interrupt context */
1293static void dma_tc_handle(struct d40_chan *d40c)
1294{
1295 struct d40_desc *d40d;
1296
Linus Walleij8d318a52010-03-30 15:33:42 +02001297 /* Get first active entry from list */
1298 d40d = d40_first_active_get(d40c);
1299
1300 if (d40d == NULL)
1301 return;
1302
Rabin Vincent0c842b52011-01-25 11:18:35 +01001303 if (d40d->cyclic) {
1304 /*
1305 * If this was a paritially loaded list, we need to reloaded
1306 * it, and only when the list is completed. We need to check
1307 * for done because the interrupt will hit for every link, and
1308 * not just the last one.
1309 */
1310 if (d40d->lli_current < d40d->lli_len
1311 && !d40_tx_is_linked(d40c)
1312 && !d40_residue(d40c)) {
1313 d40_lcla_free_all(d40c, d40d);
1314 d40_desc_load(d40c, d40d);
1315 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001316
Rabin Vincent0c842b52011-01-25 11:18:35 +01001317 if (d40d->lli_current == d40d->lli_len)
1318 d40d->lli_current = 0;
1319 }
1320 } else {
1321 d40_lcla_free_all(d40c, d40d);
1322
1323 if (d40d->lli_current < d40d->lli_len) {
1324 d40_desc_load(d40c, d40d);
1325 /* Start dma job */
1326 (void) d40_start(d40c);
1327 return;
1328 }
1329
1330 if (d40_queue_start(d40c) == NULL)
1331 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301332 pm_runtime_mark_last_busy(d40c->base->dev);
1333 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001334 }
1335
Linus Walleij8d318a52010-03-30 15:33:42 +02001336 d40c->pending_tx++;
1337 tasklet_schedule(&d40c->tasklet);
1338
1339}
1340
1341static void dma_tasklet(unsigned long data)
1342{
1343 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001344 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001345 unsigned long flags;
1346 dma_async_tx_callback callback;
1347 void *callback_param;
1348
1349 spin_lock_irqsave(&d40c->lock, flags);
1350
1351 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001352 d40d = d40_first_active_get(d40c);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001353 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001354 goto err;
1355
Rabin Vincent0c842b52011-01-25 11:18:35 +01001356 if (!d40d->cyclic)
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001357 d40c->chan.completed_cookie = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001358
1359 /*
1360 * If terminating a channel pending_tx is set to zero.
1361 * This prevents any finished active jobs to return to the client.
1362 */
1363 if (d40c->pending_tx == 0) {
1364 spin_unlock_irqrestore(&d40c->lock, flags);
1365 return;
1366 }
1367
1368 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001369 callback = d40d->txd.callback;
1370 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001371
Rabin Vincent0c842b52011-01-25 11:18:35 +01001372 if (!d40d->cyclic) {
1373 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001374 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001375 d40_desc_free(d40c, d40d);
1376 } else {
1377 if (!d40d->is_in_client_list) {
1378 d40_desc_remove(d40d);
1379 d40_lcla_free_all(d40c, d40d);
1380 list_add_tail(&d40d->node, &d40c->client);
1381 d40d->is_in_client_list = true;
1382 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001383 }
1384 }
1385
1386 d40c->pending_tx--;
1387
1388 if (d40c->pending_tx)
1389 tasklet_schedule(&d40c->tasklet);
1390
1391 spin_unlock_irqrestore(&d40c->lock, flags);
1392
Jonas Aaberg767a9672010-08-09 12:08:34 +00001393 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001394 callback(callback_param);
1395
1396 return;
1397
1398 err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001399 /* Rescue manoeuvre if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001400 if (d40c->pending_tx > 0)
1401 d40c->pending_tx--;
1402 spin_unlock_irqrestore(&d40c->lock, flags);
1403}
1404
1405static irqreturn_t d40_handle_interrupt(int irq, void *data)
1406{
1407 static const struct d40_interrupt_lookup il[] = {
1408 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1409 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1410 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1411 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1412 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1413 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1414 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1415 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1416 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1417 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1418 };
1419
1420 int i;
1421 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001422 u32 idx;
1423 u32 row;
1424 long chan = -1;
1425 struct d40_chan *d40c;
1426 unsigned long flags;
1427 struct d40_base *base = data;
1428
1429 spin_lock_irqsave(&base->interrupt_lock, flags);
1430
1431 /* Read interrupt status of both logical and physical channels */
1432 for (i = 0; i < ARRAY_SIZE(il); i++)
1433 regs[i] = readl(base->virtbase + il[i].src);
1434
1435 for (;;) {
1436
1437 chan = find_next_bit((unsigned long *)regs,
1438 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1439
1440 /* No more set bits found? */
1441 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1442 break;
1443
1444 row = chan / BITS_PER_LONG;
1445 idx = chan & (BITS_PER_LONG - 1);
1446
1447 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001448 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001449
1450 if (il[row].offset == D40_PHY_CHAN)
1451 d40c = base->lookup_phy_chans[idx];
1452 else
1453 d40c = base->lookup_log_chans[il[row].offset + idx];
1454 spin_lock(&d40c->lock);
1455
1456 if (!il[row].is_error)
1457 dma_tc_handle(d40c);
1458 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001459 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1460 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001461
1462 spin_unlock(&d40c->lock);
1463 }
1464
1465 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1466
1467 return IRQ_HANDLED;
1468}
1469
Linus Walleij8d318a52010-03-30 15:33:42 +02001470static int d40_validate_conf(struct d40_chan *d40c,
1471 struct stedma40_chan_cfg *conf)
1472{
1473 int res = 0;
1474 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1475 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001476 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001477
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001478 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001479 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001480 res = -EINVAL;
1481 }
1482
1483 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1484 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1485 d40c->runtime_addr == 0) {
1486
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001487 chan_err(d40c, "Invalid TX channel address (%d)\n",
1488 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001489 res = -EINVAL;
1490 }
1491
1492 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1493 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1494 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001495 chan_err(d40c, "Invalid RX channel address (%d)\n",
1496 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001497 res = -EINVAL;
1498 }
1499
1500 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001501 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001502 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001503 res = -EINVAL;
1504 }
1505
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001506 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001507 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001508 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001509 res = -EINVAL;
1510 }
1511
1512 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1513 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001514 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001515 res = -EINVAL;
1516 }
1517
1518 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1519 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001520 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001521 res = -EINVAL;
1522 }
1523
1524 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1525 /*
1526 * DMAC HW supports it. Will be added to this driver,
1527 * in case any dma client requires it.
1528 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001529 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001530 res = -EINVAL;
1531 }
1532
Per Forlind49278e2010-12-20 18:31:38 +01001533 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1534 (1 << conf->src_info.data_width) !=
1535 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1536 (1 << conf->dst_info.data_width)) {
1537 /*
1538 * The DMAC hardware only supports
1539 * src (burst x width) == dst (burst x width)
1540 */
1541
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001542 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001543 res = -EINVAL;
1544 }
1545
Linus Walleij8d318a52010-03-30 15:33:42 +02001546 return res;
1547}
1548
Narayanan G5cd326f2011-11-30 19:20:42 +05301549static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1550 bool is_src, int log_event_line, bool is_log,
1551 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001552{
1553 unsigned long flags;
1554 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301555
1556 *first_user = ((phy->allocated_src | phy->allocated_dst)
1557 == D40_ALLOC_FREE);
1558
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001559 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001560 /* Physical interrupts are masked per physical full channel */
1561 if (phy->allocated_src == D40_ALLOC_FREE &&
1562 phy->allocated_dst == D40_ALLOC_FREE) {
1563 phy->allocated_dst = D40_ALLOC_PHY;
1564 phy->allocated_src = D40_ALLOC_PHY;
1565 goto found;
1566 } else
1567 goto not_found;
1568 }
1569
1570 /* Logical channel */
1571 if (is_src) {
1572 if (phy->allocated_src == D40_ALLOC_PHY)
1573 goto not_found;
1574
1575 if (phy->allocated_src == D40_ALLOC_FREE)
1576 phy->allocated_src = D40_ALLOC_LOG_FREE;
1577
1578 if (!(phy->allocated_src & (1 << log_event_line))) {
1579 phy->allocated_src |= 1 << log_event_line;
1580 goto found;
1581 } else
1582 goto not_found;
1583 } else {
1584 if (phy->allocated_dst == D40_ALLOC_PHY)
1585 goto not_found;
1586
1587 if (phy->allocated_dst == D40_ALLOC_FREE)
1588 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1589
1590 if (!(phy->allocated_dst & (1 << log_event_line))) {
1591 phy->allocated_dst |= 1 << log_event_line;
1592 goto found;
1593 } else
1594 goto not_found;
1595 }
1596
1597not_found:
1598 spin_unlock_irqrestore(&phy->lock, flags);
1599 return false;
1600found:
1601 spin_unlock_irqrestore(&phy->lock, flags);
1602 return true;
1603}
1604
1605static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1606 int log_event_line)
1607{
1608 unsigned long flags;
1609 bool is_free = false;
1610
1611 spin_lock_irqsave(&phy->lock, flags);
1612 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001613 phy->allocated_dst = D40_ALLOC_FREE;
1614 phy->allocated_src = D40_ALLOC_FREE;
1615 is_free = true;
1616 goto out;
1617 }
1618
1619 /* Logical channel */
1620 if (is_src) {
1621 phy->allocated_src &= ~(1 << log_event_line);
1622 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1623 phy->allocated_src = D40_ALLOC_FREE;
1624 } else {
1625 phy->allocated_dst &= ~(1 << log_event_line);
1626 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1627 phy->allocated_dst = D40_ALLOC_FREE;
1628 }
1629
1630 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1631 D40_ALLOC_FREE);
1632
1633out:
1634 spin_unlock_irqrestore(&phy->lock, flags);
1635
1636 return is_free;
1637}
1638
Narayanan G5cd326f2011-11-30 19:20:42 +05301639static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001640{
1641 int dev_type;
1642 int event_group;
1643 int event_line;
1644 struct d40_phy_res *phys;
1645 int i;
1646 int j;
1647 int log_num;
1648 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001649 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001650
1651 phys = d40c->base->phy_res;
1652
1653 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1654 dev_type = d40c->dma_cfg.src_dev_type;
1655 log_num = 2 * dev_type;
1656 is_src = true;
1657 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1658 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1659 /* dst event lines are used for logical memcpy */
1660 dev_type = d40c->dma_cfg.dst_dev_type;
1661 log_num = 2 * dev_type + 1;
1662 is_src = false;
1663 } else
1664 return -EINVAL;
1665
1666 event_group = D40_TYPE_TO_GROUP(dev_type);
1667 event_line = D40_TYPE_TO_EVENT(dev_type);
1668
1669 if (!is_log) {
1670 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1671 /* Find physical half channel */
1672 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1673
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001674 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301675 0, is_log,
1676 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001677 goto found_phy;
1678 }
1679 } else
1680 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1681 int phy_num = j + event_group * 2;
1682 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001683 if (d40_alloc_mask_set(&phys[i],
1684 is_src,
1685 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301686 is_log,
1687 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001688 goto found_phy;
1689 }
1690 }
1691 return -EINVAL;
1692found_phy:
1693 d40c->phy_chan = &phys[i];
1694 d40c->log_num = D40_PHY_CHAN;
1695 goto out;
1696 }
1697 if (dev_type == -1)
1698 return -EINVAL;
1699
1700 /* Find logical channel */
1701 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1702 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301703
1704 if (d40c->dma_cfg.use_fixed_channel) {
1705 i = d40c->dma_cfg.phy_channel;
1706
1707 if ((i != phy_num) && (i != phy_num + 1)) {
1708 dev_err(chan2dev(d40c),
1709 "invalid fixed phy channel %d\n", i);
1710 return -EINVAL;
1711 }
1712
1713 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1714 is_log, first_phy_user))
1715 goto found_log;
1716
1717 dev_err(chan2dev(d40c),
1718 "could not allocate fixed phy channel %d\n", i);
1719 return -EINVAL;
1720 }
1721
Linus Walleij8d318a52010-03-30 15:33:42 +02001722 /*
1723 * Spread logical channels across all available physical rather
1724 * than pack every logical channel at the first available phy
1725 * channels.
1726 */
1727 if (is_src) {
1728 for (i = phy_num; i < phy_num + 2; i++) {
1729 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301730 event_line, is_log,
1731 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001732 goto found_log;
1733 }
1734 } else {
1735 for (i = phy_num + 1; i >= phy_num; i--) {
1736 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301737 event_line, is_log,
1738 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001739 goto found_log;
1740 }
1741 }
1742 }
1743 return -EINVAL;
1744
1745found_log:
1746 d40c->phy_chan = &phys[i];
1747 d40c->log_num = log_num;
1748out:
1749
1750 if (is_log)
1751 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1752 else
1753 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1754
1755 return 0;
1756
1757}
1758
Linus Walleij8d318a52010-03-30 15:33:42 +02001759static int d40_config_memcpy(struct d40_chan *d40c)
1760{
1761 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1762
1763 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1764 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1765 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1766 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1767 memcpy[d40c->chan.chan_id];
1768
1769 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1770 dma_has_cap(DMA_SLAVE, cap)) {
1771 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1772 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001773 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001774 return -EINVAL;
1775 }
1776
1777 return 0;
1778}
1779
1780
1781static int d40_free_dma(struct d40_chan *d40c)
1782{
1783
1784 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001785 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001786 struct d40_phy_res *phy = d40c->phy_chan;
1787 bool is_src;
1788
1789 /* Terminate all queued and active transfers */
1790 d40_term_all(d40c);
1791
1792 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001793 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001794 return -EINVAL;
1795 }
1796
1797 if (phy->allocated_src == D40_ALLOC_FREE &&
1798 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001799 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001800 return -EINVAL;
1801 }
1802
Linus Walleij8d318a52010-03-30 15:33:42 +02001803 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1804 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1805 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001806 is_src = false;
1807 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1808 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001809 is_src = true;
1810 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001811 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001812 return -EINVAL;
1813 }
1814
Narayanan G7fb3e752011-11-17 17:26:41 +05301815 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001816 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1817 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001818 chan_err(d40c, "suspend failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05301819 goto out;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001820 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001821
Rabin Vincent724a8572011-01-25 11:18:08 +01001822 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001823 /* Release logical channel, deactivate the event line */
1824
1825 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001826 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1827
1828 /*
1829 * Check if there are more logical allocation
1830 * on this phy channel.
1831 */
1832 if (!d40_alloc_mask_free(phy, is_src, event)) {
1833 /* Resume the other logical channels if any */
1834 if (d40_chan_has_events(d40c)) {
1835 res = d40_channel_execute_command(d40c,
1836 D40_DMA_RUN);
Narayanan G7fb3e752011-11-17 17:26:41 +05301837 if (res)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001838 chan_err(d40c,
1839 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001840 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301841 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02001842 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001843 } else {
1844 (void) d40_alloc_mask_free(phy, is_src, 0);
1845 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001846
1847 /* Release physical channel */
1848 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1849 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001850 chan_err(d40c, "Failed to stop channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05301851 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02001852 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301853
1854 if (d40c->busy) {
1855 pm_runtime_mark_last_busy(d40c->base->dev);
1856 pm_runtime_put_autosuspend(d40c->base->dev);
1857 }
1858
1859 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001860 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001861 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001862 d40c->base->lookup_phy_chans[phy->num] = NULL;
Narayanan G7fb3e752011-11-17 17:26:41 +05301863out:
Linus Walleij8d318a52010-03-30 15:33:42 +02001864
Narayanan G7fb3e752011-11-17 17:26:41 +05301865 pm_runtime_mark_last_busy(d40c->base->dev);
1866 pm_runtime_put_autosuspend(d40c->base->dev);
1867 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02001868}
1869
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001870static bool d40_is_paused(struct d40_chan *d40c)
1871{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001872 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001873 bool is_paused = false;
1874 unsigned long flags;
1875 void __iomem *active_reg;
1876 u32 status;
1877 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001878
1879 spin_lock_irqsave(&d40c->lock, flags);
1880
Rabin Vincent724a8572011-01-25 11:18:08 +01001881 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001882 if (d40c->phy_chan->num % 2 == 0)
1883 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1884 else
1885 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1886
1887 status = (readl(active_reg) &
1888 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1889 D40_CHAN_POS(d40c->phy_chan->num);
1890 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1891 is_paused = true;
1892
1893 goto _exit;
1894 }
1895
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001896 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001897 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001898 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001899 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001900 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001901 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001902 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001903 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001904 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001905 goto _exit;
1906 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001907
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001908 status = (status & D40_EVENTLINE_MASK(event)) >>
1909 D40_EVENTLINE_POS(event);
1910
1911 if (status != D40_DMA_RUN)
1912 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001913_exit:
1914 spin_unlock_irqrestore(&d40c->lock, flags);
1915 return is_paused;
1916
1917}
1918
1919
Linus Walleij8d318a52010-03-30 15:33:42 +02001920static u32 stedma40_residue(struct dma_chan *chan)
1921{
1922 struct d40_chan *d40c =
1923 container_of(chan, struct d40_chan, chan);
1924 u32 bytes_left;
1925 unsigned long flags;
1926
1927 spin_lock_irqsave(&d40c->lock, flags);
1928 bytes_left = d40_residue(d40c);
1929 spin_unlock_irqrestore(&d40c->lock, flags);
1930
1931 return bytes_left;
1932}
1933
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001934static int
1935d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1936 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001937 unsigned int sg_len, dma_addr_t src_dev_addr,
1938 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001939{
1940 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1941 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1942 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001943 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001944
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001945 ret = d40_log_sg_to_lli(sg_src, sg_len,
1946 src_dev_addr,
1947 desc->lli_log.src,
1948 chan->log_def.lcsp1,
1949 src_info->data_width,
1950 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001951
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001952 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1953 dst_dev_addr,
1954 desc->lli_log.dst,
1955 chan->log_def.lcsp3,
1956 dst_info->data_width,
1957 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001958
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001959 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001960}
1961
1962static int
1963d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1964 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001965 unsigned int sg_len, dma_addr_t src_dev_addr,
1966 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001967{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001968 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1969 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1970 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01001971 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001972 int ret;
1973
Rabin Vincent0c842b52011-01-25 11:18:35 +01001974 if (desc->cyclic)
1975 flags |= LLI_CYCLIC | LLI_TERM_INT;
1976
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001977 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1978 desc->lli_phy.src,
1979 virt_to_phys(desc->lli_phy.src),
1980 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001981 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001982
1983 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1984 desc->lli_phy.dst,
1985 virt_to_phys(desc->lli_phy.dst),
1986 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001987 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001988
1989 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1990 desc->lli_pool.size, DMA_TO_DEVICE);
1991
1992 return ret < 0 ? ret : 0;
1993}
1994
1995
Rabin Vincent5f811582011-01-25 11:18:18 +01001996static struct d40_desc *
1997d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1998 unsigned int sg_len, unsigned long dma_flags)
1999{
2000 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2001 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002002 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002003
2004 desc = d40_desc_get(chan);
2005 if (!desc)
2006 return NULL;
2007
2008 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2009 cfg->dst_info.data_width);
2010 if (desc->lli_len < 0) {
2011 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002012 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002013 }
2014
Rabin Vincentdbd88782011-01-25 11:18:19 +01002015 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2016 if (ret < 0) {
2017 chan_err(chan, "Could not allocate lli\n");
2018 goto err;
2019 }
2020
2021
Rabin Vincent5f811582011-01-25 11:18:18 +01002022 desc->lli_current = 0;
2023 desc->txd.flags = dma_flags;
2024 desc->txd.tx_submit = d40_tx_submit;
2025
2026 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2027
2028 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002029
2030err:
2031 d40_desc_free(chan, desc);
2032 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002033}
2034
Rabin Vincentcade1d32011-01-25 11:18:23 +01002035static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302036d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002037{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002038 struct stedma40_platform_data *plat = chan->base->plat_data;
2039 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002040 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002041
Rabin Vincentcade1d32011-01-25 11:18:23 +01002042 if (chan->runtime_addr)
2043 return chan->runtime_addr;
2044
Vinod Kouldb8196d2011-10-13 22:34:23 +05302045 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002046 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302047 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002048 addr = plat->dev_tx[cfg->dst_dev_type];
2049
2050 return addr;
2051}
2052
2053static struct dma_async_tx_descriptor *
2054d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2055 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302056 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002057{
2058 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002059 dma_addr_t src_dev_addr = 0;
2060 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002061 struct d40_desc *desc;
2062 unsigned long flags;
2063 int ret;
2064
2065 if (!chan->phy_chan) {
2066 chan_err(chan, "Cannot prepare unallocated channel\n");
2067 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002068 }
2069
Rabin Vincent0c842b52011-01-25 11:18:35 +01002070
Rabin Vincentcade1d32011-01-25 11:18:23 +01002071 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002072
Rabin Vincentcade1d32011-01-25 11:18:23 +01002073 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2074 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002075 goto err;
2076
Rabin Vincent0c842b52011-01-25 11:18:35 +01002077 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2078 desc->cyclic = true;
2079
Rabin Vincent822c5672011-01-25 11:18:28 +01002080 if (direction != DMA_NONE) {
2081 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2082
Vinod Kouldb8196d2011-10-13 22:34:23 +05302083 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002084 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302085 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002086 dst_dev_addr = dev_addr;
2087 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002088
2089 if (chan_is_logical(chan))
2090 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002091 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002092 else
2093 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002094 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002095
2096 if (ret) {
2097 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2098 chan_is_logical(chan) ? "log" : "phy", ret);
2099 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002100 }
2101
Per Forlin82babbb362011-08-29 13:33:35 +02002102 /*
2103 * add descriptor to the prepare queue in order to be able
2104 * to free them later in terminate_all
2105 */
2106 list_add_tail(&desc->node, &chan->prepare_queue);
2107
Rabin Vincentcade1d32011-01-25 11:18:23 +01002108 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002109
Rabin Vincentcade1d32011-01-25 11:18:23 +01002110 return &desc->txd;
2111
Linus Walleij8d318a52010-03-30 15:33:42 +02002112err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002113 if (desc)
2114 d40_desc_free(chan, desc);
2115 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002116 return NULL;
2117}
Linus Walleij8d318a52010-03-30 15:33:42 +02002118
2119bool stedma40_filter(struct dma_chan *chan, void *data)
2120{
2121 struct stedma40_chan_cfg *info = data;
2122 struct d40_chan *d40c =
2123 container_of(chan, struct d40_chan, chan);
2124 int err;
2125
2126 if (data) {
2127 err = d40_validate_conf(d40c, info);
2128 if (!err)
2129 d40c->dma_cfg = *info;
2130 } else
2131 err = d40_config_memcpy(d40c);
2132
Rabin Vincentce2ca122010-10-12 13:00:49 +00002133 if (!err)
2134 d40c->configured = true;
2135
Linus Walleij8d318a52010-03-30 15:33:42 +02002136 return err == 0;
2137}
2138EXPORT_SYMBOL(stedma40_filter);
2139
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002140static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2141{
2142 bool realtime = d40c->dma_cfg.realtime;
2143 bool highprio = d40c->dma_cfg.high_priority;
2144 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
2145 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
2146 u32 event = D40_TYPE_TO_EVENT(dev_type);
2147 u32 group = D40_TYPE_TO_GROUP(dev_type);
2148 u32 bit = 1 << event;
2149
2150 /* Destination event lines are stored in the upper halfword */
2151 if (!src)
2152 bit <<= 16;
2153
2154 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2155 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2156}
2157
2158static void d40_set_prio_realtime(struct d40_chan *d40c)
2159{
2160 if (d40c->base->rev < 3)
2161 return;
2162
2163 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2164 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2165 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2166
2167 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2168 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2169 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2170}
2171
Linus Walleij8d318a52010-03-30 15:33:42 +02002172/* DMA ENGINE functions */
2173static int d40_alloc_chan_resources(struct dma_chan *chan)
2174{
2175 int err;
2176 unsigned long flags;
2177 struct d40_chan *d40c =
2178 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002179 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002180 spin_lock_irqsave(&d40c->lock, flags);
2181
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00002182 chan->completed_cookie = chan->cookie = 1;
Linus Walleij8d318a52010-03-30 15:33:42 +02002183
Rabin Vincentce2ca122010-10-12 13:00:49 +00002184 /* If no dma configuration is set use default configuration (memcpy) */
2185 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002186 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002187 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002188 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002189 goto fail;
2190 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002191 }
2192
Narayanan G5cd326f2011-11-30 19:20:42 +05302193 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002194 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002195 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302196 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002197 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002198 }
2199
Narayanan G7fb3e752011-11-17 17:26:41 +05302200 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002201 /* Fill in basic CFG register values */
2202 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002203 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002204
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002205 d40_set_prio_realtime(d40c);
2206
Rabin Vincent724a8572011-01-25 11:18:08 +01002207 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002208 d40_log_cfg(&d40c->dma_cfg,
2209 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2210
2211 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2212 d40c->lcpa = d40c->base->lcpa_base +
2213 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2214 else
2215 d40c->lcpa = d40c->base->lcpa_base +
2216 d40c->dma_cfg.dst_dev_type *
2217 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2218 }
2219
Narayanan G5cd326f2011-11-30 19:20:42 +05302220 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2221 chan_is_logical(d40c) ? "logical" : "physical",
2222 d40c->phy_chan->num,
2223 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2224
2225
Linus Walleijef1872e2010-06-20 21:24:52 +00002226 /*
2227 * Only write channel configuration to the DMA if the physical
2228 * resource is free. In case of multiple logical channels
2229 * on the same physical resource, only the first write is necessary.
2230 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002231 if (is_free_phy)
2232 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002233fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302234 pm_runtime_mark_last_busy(d40c->base->dev);
2235 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002236 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002237 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002238}
2239
2240static void d40_free_chan_resources(struct dma_chan *chan)
2241{
2242 struct d40_chan *d40c =
2243 container_of(chan, struct d40_chan, chan);
2244 int err;
2245 unsigned long flags;
2246
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002247 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002248 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002249 return;
2250 }
2251
2252
Linus Walleij8d318a52010-03-30 15:33:42 +02002253 spin_lock_irqsave(&d40c->lock, flags);
2254
2255 err = d40_free_dma(d40c);
2256
2257 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002258 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002259 spin_unlock_irqrestore(&d40c->lock, flags);
2260}
2261
2262static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2263 dma_addr_t dst,
2264 dma_addr_t src,
2265 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002266 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002267{
Rabin Vincent95944c62011-01-25 11:18:17 +01002268 struct scatterlist dst_sg;
2269 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002270
Rabin Vincent95944c62011-01-25 11:18:17 +01002271 sg_init_table(&dst_sg, 1);
2272 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002273
Rabin Vincent95944c62011-01-25 11:18:17 +01002274 sg_dma_address(&dst_sg) = dst;
2275 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002276
Rabin Vincent95944c62011-01-25 11:18:17 +01002277 sg_dma_len(&dst_sg) = size;
2278 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002279
Rabin Vincentcade1d32011-01-25 11:18:23 +01002280 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002281}
2282
Ira Snyder0d688662010-09-30 11:46:47 +00002283static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002284d40_prep_memcpy_sg(struct dma_chan *chan,
2285 struct scatterlist *dst_sg, unsigned int dst_nents,
2286 struct scatterlist *src_sg, unsigned int src_nents,
2287 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002288{
2289 if (dst_nents != src_nents)
2290 return NULL;
2291
Rabin Vincentcade1d32011-01-25 11:18:23 +01002292 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002293}
2294
Linus Walleij8d318a52010-03-30 15:33:42 +02002295static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2296 struct scatterlist *sgl,
2297 unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302298 enum dma_transfer_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002299 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002300{
Vinod Kouldb8196d2011-10-13 22:34:23 +05302301 if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV)
Rabin Vincent00ac0342011-01-25 11:18:20 +01002302 return NULL;
2303
Rabin Vincentcade1d32011-01-25 11:18:23 +01002304 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002305}
2306
Rabin Vincent0c842b52011-01-25 11:18:35 +01002307static struct dma_async_tx_descriptor *
2308dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2309 size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302310 enum dma_transfer_direction direction)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002311{
2312 unsigned int periods = buf_len / period_len;
2313 struct dma_async_tx_descriptor *txd;
2314 struct scatterlist *sg;
2315 int i;
2316
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002317 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002318 for (i = 0; i < periods; i++) {
2319 sg_dma_address(&sg[i]) = dma_addr;
2320 sg_dma_len(&sg[i]) = period_len;
2321 dma_addr += period_len;
2322 }
2323
2324 sg[periods].offset = 0;
2325 sg[periods].length = 0;
2326 sg[periods].page_link =
2327 ((unsigned long)sg | 0x01) & ~0x02;
2328
2329 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2330 DMA_PREP_INTERRUPT);
2331
2332 kfree(sg);
2333
2334 return txd;
2335}
2336
Linus Walleij8d318a52010-03-30 15:33:42 +02002337static enum dma_status d40_tx_status(struct dma_chan *chan,
2338 dma_cookie_t cookie,
2339 struct dma_tx_state *txstate)
2340{
2341 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2342 dma_cookie_t last_used;
2343 dma_cookie_t last_complete;
2344 int ret;
2345
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002346 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002347 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002348 return -EINVAL;
2349 }
2350
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00002351 last_complete = chan->completed_cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02002352 last_used = chan->cookie;
2353
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002354 if (d40_is_paused(d40c))
2355 ret = DMA_PAUSED;
2356 else
2357 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002358
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002359 dma_set_tx_state(txstate, last_complete, last_used,
2360 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002361
2362 return ret;
2363}
2364
2365static void d40_issue_pending(struct dma_chan *chan)
2366{
2367 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2368 unsigned long flags;
2369
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002370 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002371 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002372 return;
2373 }
2374
Linus Walleij8d318a52010-03-30 15:33:42 +02002375 spin_lock_irqsave(&d40c->lock, flags);
2376
Per Forlina8f30672011-06-26 23:29:52 +02002377 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2378
2379 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002380 if (!d40c->busy)
2381 (void) d40_queue_start(d40c);
2382
2383 spin_unlock_irqrestore(&d40c->lock, flags);
2384}
2385
Rabin Vincent98ca5282011-06-27 11:33:38 +02002386static int
2387dma40_config_to_halfchannel(struct d40_chan *d40c,
2388 struct stedma40_half_channel_info *info,
2389 enum dma_slave_buswidth width,
2390 u32 maxburst)
2391{
2392 enum stedma40_periph_data_width addr_width;
2393 int psize;
2394
2395 switch (width) {
2396 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2397 addr_width = STEDMA40_BYTE_WIDTH;
2398 break;
2399 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2400 addr_width = STEDMA40_HALFWORD_WIDTH;
2401 break;
2402 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2403 addr_width = STEDMA40_WORD_WIDTH;
2404 break;
2405 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2406 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2407 break;
2408 default:
2409 dev_err(d40c->base->dev,
2410 "illegal peripheral address width "
2411 "requested (%d)\n",
2412 width);
2413 return -EINVAL;
2414 }
2415
2416 if (chan_is_logical(d40c)) {
2417 if (maxburst >= 16)
2418 psize = STEDMA40_PSIZE_LOG_16;
2419 else if (maxburst >= 8)
2420 psize = STEDMA40_PSIZE_LOG_8;
2421 else if (maxburst >= 4)
2422 psize = STEDMA40_PSIZE_LOG_4;
2423 else
2424 psize = STEDMA40_PSIZE_LOG_1;
2425 } else {
2426 if (maxburst >= 16)
2427 psize = STEDMA40_PSIZE_PHY_16;
2428 else if (maxburst >= 8)
2429 psize = STEDMA40_PSIZE_PHY_8;
2430 else if (maxburst >= 4)
2431 psize = STEDMA40_PSIZE_PHY_4;
2432 else
2433 psize = STEDMA40_PSIZE_PHY_1;
2434 }
2435
2436 info->data_width = addr_width;
2437 info->psize = psize;
2438 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2439
2440 return 0;
2441}
2442
Linus Walleij95e14002010-08-04 13:37:45 +02002443/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002444static int d40_set_runtime_config(struct dma_chan *chan,
2445 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002446{
2447 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2448 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002449 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002450 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002451 u32 src_maxburst, dst_maxburst;
2452 int ret;
2453
2454 src_addr_width = config->src_addr_width;
2455 src_maxburst = config->src_maxburst;
2456 dst_addr_width = config->dst_addr_width;
2457 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002458
Vinod Kouldb8196d2011-10-13 22:34:23 +05302459 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002460 dma_addr_t dev_addr_rx =
2461 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2462
2463 config_addr = config->src_addr;
2464 if (dev_addr_rx)
2465 dev_dbg(d40c->base->dev,
2466 "channel has a pre-wired RX address %08x "
2467 "overriding with %08x\n",
2468 dev_addr_rx, config_addr);
2469 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2470 dev_dbg(d40c->base->dev,
2471 "channel was not configured for peripheral "
2472 "to memory transfer (%d) overriding\n",
2473 cfg->dir);
2474 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2475
Rabin Vincent98ca5282011-06-27 11:33:38 +02002476 /* Configure the memory side */
2477 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2478 dst_addr_width = src_addr_width;
2479 if (dst_maxburst == 0)
2480 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002481
Vinod Kouldb8196d2011-10-13 22:34:23 +05302482 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002483 dma_addr_t dev_addr_tx =
2484 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2485
2486 config_addr = config->dst_addr;
2487 if (dev_addr_tx)
2488 dev_dbg(d40c->base->dev,
2489 "channel has a pre-wired TX address %08x "
2490 "overriding with %08x\n",
2491 dev_addr_tx, config_addr);
2492 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2493 dev_dbg(d40c->base->dev,
2494 "channel was not configured for memory "
2495 "to peripheral transfer (%d) overriding\n",
2496 cfg->dir);
2497 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2498
Rabin Vincent98ca5282011-06-27 11:33:38 +02002499 /* Configure the memory side */
2500 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2501 src_addr_width = dst_addr_width;
2502 if (src_maxburst == 0)
2503 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002504 } else {
2505 dev_err(d40c->base->dev,
2506 "unrecognized channel direction %d\n",
2507 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002508 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002509 }
2510
Rabin Vincent98ca5282011-06-27 11:33:38 +02002511 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002512 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002513 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2514 src_maxburst,
2515 src_addr_width,
2516 dst_maxburst,
2517 dst_addr_width);
2518 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002519 }
2520
Rabin Vincent98ca5282011-06-27 11:33:38 +02002521 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2522 src_addr_width,
2523 src_maxburst);
2524 if (ret)
2525 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002526
Rabin Vincent98ca5282011-06-27 11:33:38 +02002527 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2528 dst_addr_width,
2529 dst_maxburst);
2530 if (ret)
2531 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002532
Per Forlina59670a2010-10-06 09:05:27 +00002533 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002534 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002535 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2536 else
2537 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2538 &d40c->dst_def_cfg, false);
2539
Linus Walleij95e14002010-08-04 13:37:45 +02002540 /* These settings will take precedence later */
2541 d40c->runtime_addr = config_addr;
2542 d40c->runtime_direction = config->direction;
2543 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002544 "configured channel %s for %s, data width %d/%d, "
2545 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002546 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302547 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002548 src_addr_width, dst_addr_width,
2549 src_maxburst, dst_maxburst);
2550
2551 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002552}
2553
Linus Walleij05827632010-05-17 16:30:42 -07002554static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2555 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002556{
Linus Walleij8d318a52010-03-30 15:33:42 +02002557 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2558
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002559 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002560 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002561 return -EINVAL;
2562 }
2563
Linus Walleij8d318a52010-03-30 15:33:42 +02002564 switch (cmd) {
2565 case DMA_TERMINATE_ALL:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002566 return d40_terminate_all(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002567 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002568 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002569 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002570 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002571 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002572 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002573 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002574 default:
2575 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002576 }
2577
2578 /* Other commands are unimplemented */
2579 return -ENXIO;
2580}
2581
2582/* Initialization functions */
2583
2584static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2585 struct d40_chan *chans, int offset,
2586 int num_chans)
2587{
2588 int i = 0;
2589 struct d40_chan *d40c;
2590
2591 INIT_LIST_HEAD(&dma->channels);
2592
2593 for (i = offset; i < offset + num_chans; i++) {
2594 d40c = &chans[i];
2595 d40c->base = base;
2596 d40c->chan.device = dma;
2597
Linus Walleij8d318a52010-03-30 15:33:42 +02002598 spin_lock_init(&d40c->lock);
2599
2600 d40c->log_num = D40_PHY_CHAN;
2601
Linus Walleij8d318a52010-03-30 15:33:42 +02002602 INIT_LIST_HEAD(&d40c->active);
2603 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002604 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002605 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002606 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002607
Linus Walleij8d318a52010-03-30 15:33:42 +02002608 tasklet_init(&d40c->tasklet, dma_tasklet,
2609 (unsigned long) d40c);
2610
2611 list_add_tail(&d40c->chan.device_node,
2612 &dma->channels);
2613 }
2614}
2615
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002616static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2617{
2618 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2619 dev->device_prep_slave_sg = d40_prep_slave_sg;
2620
2621 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2622 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2623
2624 /*
2625 * This controller can only access address at even
2626 * 32bit boundaries, i.e. 2^2
2627 */
2628 dev->copy_align = 2;
2629 }
2630
2631 if (dma_has_cap(DMA_SG, dev->cap_mask))
2632 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2633
Rabin Vincent0c842b52011-01-25 11:18:35 +01002634 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2635 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2636
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002637 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2638 dev->device_free_chan_resources = d40_free_chan_resources;
2639 dev->device_issue_pending = d40_issue_pending;
2640 dev->device_tx_status = d40_tx_status;
2641 dev->device_control = d40_control;
2642 dev->dev = base->dev;
2643}
2644
Linus Walleij8d318a52010-03-30 15:33:42 +02002645static int __init d40_dmaengine_init(struct d40_base *base,
2646 int num_reserved_chans)
2647{
2648 int err ;
2649
2650 d40_chan_init(base, &base->dma_slave, base->log_chans,
2651 0, base->num_log_chans);
2652
2653 dma_cap_zero(base->dma_slave.cap_mask);
2654 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002655 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002656
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002657 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002658
2659 err = dma_async_device_register(&base->dma_slave);
2660
2661 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002662 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002663 goto failure1;
2664 }
2665
2666 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2667 base->num_log_chans, base->plat_data->memcpy_len);
2668
2669 dma_cap_zero(base->dma_memcpy.cap_mask);
2670 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002671 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002672
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002673 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002674
2675 err = dma_async_device_register(&base->dma_memcpy);
2676
2677 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002678 d40_err(base->dev,
2679 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002680 goto failure2;
2681 }
2682
2683 d40_chan_init(base, &base->dma_both, base->phy_chans,
2684 0, num_reserved_chans);
2685
2686 dma_cap_zero(base->dma_both.cap_mask);
2687 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2688 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002689 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002690 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002691
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002692 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002693 err = dma_async_device_register(&base->dma_both);
2694
2695 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002696 d40_err(base->dev,
2697 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002698 goto failure3;
2699 }
2700 return 0;
2701failure3:
2702 dma_async_device_unregister(&base->dma_memcpy);
2703failure2:
2704 dma_async_device_unregister(&base->dma_slave);
2705failure1:
2706 return err;
2707}
2708
Narayanan G7fb3e752011-11-17 17:26:41 +05302709/* Suspend resume functionality */
2710#ifdef CONFIG_PM
2711static int dma40_pm_suspend(struct device *dev)
2712{
Narayanan G28c7a192011-11-22 13:56:55 +05302713 struct platform_device *pdev = to_platform_device(dev);
2714 struct d40_base *base = platform_get_drvdata(pdev);
2715 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302716 if (!pm_runtime_suspended(dev))
2717 return -EBUSY;
2718
Narayanan G28c7a192011-11-22 13:56:55 +05302719 if (base->lcpa_regulator)
2720 ret = regulator_disable(base->lcpa_regulator);
2721 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302722}
2723
2724static int dma40_runtime_suspend(struct device *dev)
2725{
2726 struct platform_device *pdev = to_platform_device(dev);
2727 struct d40_base *base = platform_get_drvdata(pdev);
2728
2729 d40_save_restore_registers(base, true);
2730
2731 /* Don't disable/enable clocks for v1 due to HW bugs */
2732 if (base->rev != 1)
2733 writel_relaxed(base->gcc_pwr_off_mask,
2734 base->virtbase + D40_DREG_GCC);
2735
2736 return 0;
2737}
2738
2739static int dma40_runtime_resume(struct device *dev)
2740{
2741 struct platform_device *pdev = to_platform_device(dev);
2742 struct d40_base *base = platform_get_drvdata(pdev);
2743
2744 if (base->initialized)
2745 d40_save_restore_registers(base, false);
2746
2747 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
2748 base->virtbase + D40_DREG_GCC);
2749 return 0;
2750}
2751
Narayanan G28c7a192011-11-22 13:56:55 +05302752static int dma40_resume(struct device *dev)
2753{
2754 struct platform_device *pdev = to_platform_device(dev);
2755 struct d40_base *base = platform_get_drvdata(pdev);
2756 int ret = 0;
2757
2758 if (base->lcpa_regulator)
2759 ret = regulator_enable(base->lcpa_regulator);
2760
2761 return ret;
2762}
Narayanan G7fb3e752011-11-17 17:26:41 +05302763
2764static const struct dev_pm_ops dma40_pm_ops = {
2765 .suspend = dma40_pm_suspend,
2766 .runtime_suspend = dma40_runtime_suspend,
2767 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05302768 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05302769};
2770#define DMA40_PM_OPS (&dma40_pm_ops)
2771#else
2772#define DMA40_PM_OPS NULL
2773#endif
2774
Linus Walleij8d318a52010-03-30 15:33:42 +02002775/* Initialization functions. */
2776
2777static int __init d40_phy_res_init(struct d40_base *base)
2778{
2779 int i;
2780 int num_phy_chans_avail = 0;
2781 u32 val[2];
2782 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05302783 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02002784
2785 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2786 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2787
2788 for (i = 0; i < base->num_phy_chans; i++) {
2789 base->phy_res[i].num = i;
2790 odd_even_bit += 2 * ((i % 2) == 0);
2791 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2792 /* Mark security only channels as occupied */
2793 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2794 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05302795 base->phy_res[i].reserved = true;
2796 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
2797 D40_DREG_GCC_SRC);
2798 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
2799 D40_DREG_GCC_DST);
2800
2801
Linus Walleij8d318a52010-03-30 15:33:42 +02002802 } else {
2803 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2804 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05302805 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002806 num_phy_chans_avail++;
2807 }
2808 spin_lock_init(&base->phy_res[i].lock);
2809 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002810
2811 /* Mark disabled channels as occupied */
2812 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002813 int chan = base->plat_data->disabled_channels[i];
2814
2815 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2816 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05302817 base->phy_res[chan].reserved = true;
2818 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
2819 D40_DREG_GCC_SRC);
2820 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
2821 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00002822 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002823 }
2824
Linus Walleij8d318a52010-03-30 15:33:42 +02002825 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2826 num_phy_chans_avail, base->num_phy_chans);
2827
2828 /* Verify settings extended vs standard */
2829 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2830
2831 for (i = 0; i < base->num_phy_chans; i++) {
2832
2833 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2834 (val[0] & 0x3) != 1)
2835 dev_info(base->dev,
2836 "[%s] INFO: channel %d is misconfigured (%d)\n",
2837 __func__, i, val[0] & 0x3);
2838
2839 val[0] = val[0] >> 2;
2840 }
2841
Narayanan G7fb3e752011-11-17 17:26:41 +05302842 /*
2843 * To keep things simple, Enable all clocks initially.
2844 * The clocks will get managed later post channel allocation.
2845 * The clocks for the event lines on which reserved channels exists
2846 * are not managed here.
2847 */
2848 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
2849 base->gcc_pwr_off_mask = gcc;
2850
Linus Walleij8d318a52010-03-30 15:33:42 +02002851 return num_phy_chans_avail;
2852}
2853
2854static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2855{
Linus Walleij8d318a52010-03-30 15:33:42 +02002856 struct stedma40_platform_data *plat_data;
2857 struct clk *clk = NULL;
2858 void __iomem *virtbase = NULL;
2859 struct resource *res = NULL;
2860 struct d40_base *base = NULL;
2861 int num_log_chans = 0;
2862 int num_phy_chans;
2863 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02002864 u32 pid;
2865 u32 cid;
2866 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002867
2868 clk = clk_get(&pdev->dev, NULL);
2869
2870 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002871 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002872 goto failure;
2873 }
2874
2875 clk_enable(clk);
2876
2877 /* Get IO for DMAC base address */
2878 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2879 if (!res)
2880 goto failure;
2881
2882 if (request_mem_region(res->start, resource_size(res),
2883 D40_NAME " I/O base") == NULL)
2884 goto failure;
2885
2886 virtbase = ioremap(res->start, resource_size(res));
2887 if (!virtbase)
2888 goto failure;
2889
Linus Walleijf4b89762011-06-27 11:33:46 +02002890 /* This is just a regular AMBA PrimeCell ID actually */
2891 for (pid = 0, i = 0; i < 4; i++)
2892 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
2893 & 255) << (i * 8);
2894 for (cid = 0, i = 0; i < 4; i++)
2895 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
2896 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02002897
Linus Walleijf4b89762011-06-27 11:33:46 +02002898 if (cid != AMBA_CID) {
2899 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002900 goto failure;
2901 }
Linus Walleijf4b89762011-06-27 11:33:46 +02002902 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
2903 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2904 AMBA_MANF_BITS(pid),
2905 AMBA_VENDOR_ST);
2906 goto failure;
2907 }
2908 /*
2909 * HW revision:
2910 * DB8500ed has revision 0
2911 * ? has revision 1
2912 * DB8500v1 has revision 2
2913 * DB8500v2 has revision 3
2914 */
2915 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002916
Linus Walleij8d318a52010-03-30 15:33:42 +02002917 /* The number of physical channels on this HW */
2918 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2919
2920 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002921 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002922
2923 plat_data = pdev->dev.platform_data;
2924
2925 /* Count the number of logical channels in use */
2926 for (i = 0; i < plat_data->dev_len; i++)
2927 if (plat_data->dev_rx[i] != 0)
2928 num_log_chans++;
2929
2930 for (i = 0; i < plat_data->dev_len; i++)
2931 if (plat_data->dev_tx[i] != 0)
2932 num_log_chans++;
2933
2934 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2935 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2936 sizeof(struct d40_chan), GFP_KERNEL);
2937
2938 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002939 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002940 goto failure;
2941 }
2942
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002943 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002944 base->clk = clk;
2945 base->num_phy_chans = num_phy_chans;
2946 base->num_log_chans = num_log_chans;
2947 base->phy_start = res->start;
2948 base->phy_size = resource_size(res);
2949 base->virtbase = virtbase;
2950 base->plat_data = plat_data;
2951 base->dev = &pdev->dev;
2952 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2953 base->log_chans = &base->phy_chans[num_phy_chans];
2954
2955 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2956 GFP_KERNEL);
2957 if (!base->phy_res)
2958 goto failure;
2959
2960 base->lookup_phy_chans = kzalloc(num_phy_chans *
2961 sizeof(struct d40_chan *),
2962 GFP_KERNEL);
2963 if (!base->lookup_phy_chans)
2964 goto failure;
2965
2966 if (num_log_chans + plat_data->memcpy_len) {
2967 /*
2968 * The max number of logical channels are event lines for all
2969 * src devices and dst devices
2970 */
2971 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2972 sizeof(struct d40_chan *),
2973 GFP_KERNEL);
2974 if (!base->lookup_log_chans)
2975 goto failure;
2976 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002977
Narayanan G7fb3e752011-11-17 17:26:41 +05302978 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
2979 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02002980 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05302981 if (!base->reg_val_backup_chan)
2982 goto failure;
2983
2984 base->lcla_pool.alloc_map =
2985 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
2986 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02002987 if (!base->lcla_pool.alloc_map)
2988 goto failure;
2989
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002990 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2991 0, SLAB_HWCACHE_ALIGN,
2992 NULL);
2993 if (base->desc_slab == NULL)
2994 goto failure;
2995
Linus Walleij8d318a52010-03-30 15:33:42 +02002996 return base;
2997
2998failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002999 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02003000 clk_disable(clk);
3001 clk_put(clk);
3002 }
3003 if (virtbase)
3004 iounmap(virtbase);
3005 if (res)
3006 release_mem_region(res->start,
3007 resource_size(res));
3008 if (virtbase)
3009 iounmap(virtbase);
3010
3011 if (base) {
3012 kfree(base->lcla_pool.alloc_map);
3013 kfree(base->lookup_log_chans);
3014 kfree(base->lookup_phy_chans);
3015 kfree(base->phy_res);
3016 kfree(base);
3017 }
3018
3019 return NULL;
3020}
3021
3022static void __init d40_hw_init(struct d40_base *base)
3023{
3024
Narayanan G7fb3e752011-11-17 17:26:41 +05303025 static struct d40_reg_val dma_init_reg[] = {
Linus Walleij8d318a52010-03-30 15:33:42 +02003026 /* Clock every part of the DMA block from start */
Narayanan G7fb3e752011-11-17 17:26:41 +05303027 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
Linus Walleij8d318a52010-03-30 15:33:42 +02003028
3029 /* Interrupts on all logical channels */
3030 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
3031 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
3032 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
3033 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
3034 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
3035 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
3036 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
3037 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
3038 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
3039 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
3040 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
3041 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
3042 };
3043 int i;
3044 u32 prmseo[2] = {0, 0};
3045 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3046 u32 pcmis = 0;
3047 u32 pcicr = 0;
3048
3049 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
3050 writel(dma_init_reg[i].val,
3051 base->virtbase + dma_init_reg[i].reg);
3052
3053 /* Configure all our dma channels to default settings */
3054 for (i = 0; i < base->num_phy_chans; i++) {
3055
3056 activeo[i % 2] = activeo[i % 2] << 2;
3057
3058 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3059 == D40_ALLOC_PHY) {
3060 activeo[i % 2] |= 3;
3061 continue;
3062 }
3063
3064 /* Enable interrupt # */
3065 pcmis = (pcmis << 1) | 1;
3066
3067 /* Clear interrupt # */
3068 pcicr = (pcicr << 1) | 1;
3069
3070 /* Set channel to physical mode */
3071 prmseo[i % 2] = prmseo[i % 2] << 2;
3072 prmseo[i % 2] |= 1;
3073
3074 }
3075
3076 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3077 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3078 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3079 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3080
3081 /* Write which interrupt to enable */
3082 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
3083
3084 /* Write which interrupt to clear */
3085 writel(pcicr, base->virtbase + D40_DREG_PCICR);
3086
3087}
3088
Linus Walleij508849a2010-06-20 21:26:07 +00003089static int __init d40_lcla_allocate(struct d40_base *base)
3090{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003091 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003092 unsigned long *page_list;
3093 int i, j;
3094 int ret = 0;
3095
3096 /*
3097 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3098 * To full fill this hardware requirement without wasting 256 kb
3099 * we allocate pages until we get an aligned one.
3100 */
3101 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3102 GFP_KERNEL);
3103
3104 if (!page_list) {
3105 ret = -ENOMEM;
3106 goto failure;
3107 }
3108
3109 /* Calculating how many pages that are required */
3110 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3111
3112 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3113 page_list[i] = __get_free_pages(GFP_KERNEL,
3114 base->lcla_pool.pages);
3115 if (!page_list[i]) {
3116
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003117 d40_err(base->dev, "Failed to allocate %d pages.\n",
3118 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003119
3120 for (j = 0; j < i; j++)
3121 free_pages(page_list[j], base->lcla_pool.pages);
3122 goto failure;
3123 }
3124
3125 if ((virt_to_phys((void *)page_list[i]) &
3126 (LCLA_ALIGNMENT - 1)) == 0)
3127 break;
3128 }
3129
3130 for (j = 0; j < i; j++)
3131 free_pages(page_list[j], base->lcla_pool.pages);
3132
3133 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3134 base->lcla_pool.base = (void *)page_list[i];
3135 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003136 /*
3137 * After many attempts and no succees with finding the correct
3138 * alignment, try with allocating a big buffer.
3139 */
Linus Walleij508849a2010-06-20 21:26:07 +00003140 dev_warn(base->dev,
3141 "[%s] Failed to get %d pages @ 18 bit align.\n",
3142 __func__, base->lcla_pool.pages);
3143 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3144 base->num_phy_chans +
3145 LCLA_ALIGNMENT,
3146 GFP_KERNEL);
3147 if (!base->lcla_pool.base_unaligned) {
3148 ret = -ENOMEM;
3149 goto failure;
3150 }
3151
3152 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3153 LCLA_ALIGNMENT);
3154 }
3155
Rabin Vincent026cbc42011-01-25 11:18:14 +01003156 pool->dma_addr = dma_map_single(base->dev, pool->base,
3157 SZ_1K * base->num_phy_chans,
3158 DMA_TO_DEVICE);
3159 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3160 pool->dma_addr = 0;
3161 ret = -ENOMEM;
3162 goto failure;
3163 }
3164
Linus Walleij508849a2010-06-20 21:26:07 +00003165 writel(virt_to_phys(base->lcla_pool.base),
3166 base->virtbase + D40_DREG_LCLA);
3167failure:
3168 kfree(page_list);
3169 return ret;
3170}
3171
Linus Walleij8d318a52010-03-30 15:33:42 +02003172static int __init d40_probe(struct platform_device *pdev)
3173{
3174 int err;
3175 int ret = -ENOENT;
3176 struct d40_base *base;
3177 struct resource *res = NULL;
3178 int num_reserved_chans;
3179 u32 val;
3180
3181 base = d40_hw_detect_init(pdev);
3182
3183 if (!base)
3184 goto failure;
3185
3186 num_reserved_chans = d40_phy_res_init(base);
3187
3188 platform_set_drvdata(pdev, base);
3189
3190 spin_lock_init(&base->interrupt_lock);
3191 spin_lock_init(&base->execmd_lock);
3192
3193 /* Get IO for logical channel parameter address */
3194 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3195 if (!res) {
3196 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003197 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003198 goto failure;
3199 }
3200 base->lcpa_size = resource_size(res);
3201 base->phy_lcpa = res->start;
3202
3203 if (request_mem_region(res->start, resource_size(res),
3204 D40_NAME " I/O lcpa") == NULL) {
3205 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003206 d40_err(&pdev->dev,
3207 "Failed to request LCPA region 0x%x-0x%x\n",
3208 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003209 goto failure;
3210 }
3211
3212 /* We make use of ESRAM memory for this. */
3213 val = readl(base->virtbase + D40_DREG_LCPA);
3214 if (res->start != val && val != 0) {
3215 dev_warn(&pdev->dev,
3216 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3217 __func__, val, res->start);
3218 } else
3219 writel(res->start, base->virtbase + D40_DREG_LCPA);
3220
3221 base->lcpa_base = ioremap(res->start, resource_size(res));
3222 if (!base->lcpa_base) {
3223 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003224 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003225 goto failure;
3226 }
Narayanan G28c7a192011-11-22 13:56:55 +05303227 /* If lcla has to be located in ESRAM we don't need to allocate */
3228 if (base->plat_data->use_esram_lcla) {
3229 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3230 "lcla_esram");
3231 if (!res) {
3232 ret = -ENOENT;
3233 d40_err(&pdev->dev,
3234 "No \"lcla_esram\" memory resource\n");
3235 goto failure;
3236 }
3237 base->lcla_pool.base = ioremap(res->start,
3238 resource_size(res));
3239 if (!base->lcla_pool.base) {
3240 ret = -ENOMEM;
3241 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3242 goto failure;
3243 }
3244 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003245
Narayanan G28c7a192011-11-22 13:56:55 +05303246 } else {
3247 ret = d40_lcla_allocate(base);
3248 if (ret) {
3249 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3250 goto failure;
3251 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003252 }
3253
Linus Walleij8d318a52010-03-30 15:33:42 +02003254 spin_lock_init(&base->lcla_pool.lock);
3255
Linus Walleij8d318a52010-03-30 15:33:42 +02003256 base->irq = platform_get_irq(pdev, 0);
3257
3258 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003259 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003260 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003261 goto failure;
3262 }
3263
Narayanan G7fb3e752011-11-17 17:26:41 +05303264 pm_runtime_irq_safe(base->dev);
3265 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3266 pm_runtime_use_autosuspend(base->dev);
3267 pm_runtime_enable(base->dev);
3268 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303269
3270 if (base->plat_data->use_esram_lcla) {
3271
3272 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3273 if (IS_ERR(base->lcpa_regulator)) {
3274 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3275 base->lcpa_regulator = NULL;
3276 goto failure;
3277 }
3278
3279 ret = regulator_enable(base->lcpa_regulator);
3280 if (ret) {
3281 d40_err(&pdev->dev,
3282 "Failed to enable lcpa_regulator\n");
3283 regulator_put(base->lcpa_regulator);
3284 base->lcpa_regulator = NULL;
3285 goto failure;
3286 }
3287 }
3288
Narayanan G7fb3e752011-11-17 17:26:41 +05303289 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003290 err = d40_dmaengine_init(base, num_reserved_chans);
3291 if (err)
3292 goto failure;
3293
3294 d40_hw_init(base);
3295
3296 dev_info(base->dev, "initialized\n");
3297 return 0;
3298
3299failure:
3300 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003301 if (base->desc_slab)
3302 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003303 if (base->virtbase)
3304 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003305
Narayanan G28c7a192011-11-22 13:56:55 +05303306 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3307 iounmap(base->lcla_pool.base);
3308 base->lcla_pool.base = NULL;
3309 }
3310
Rabin Vincent026cbc42011-01-25 11:18:14 +01003311 if (base->lcla_pool.dma_addr)
3312 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3313 SZ_1K * base->num_phy_chans,
3314 DMA_TO_DEVICE);
3315
Linus Walleij508849a2010-06-20 21:26:07 +00003316 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3317 free_pages((unsigned long)base->lcla_pool.base,
3318 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003319
3320 kfree(base->lcla_pool.base_unaligned);
3321
Linus Walleij8d318a52010-03-30 15:33:42 +02003322 if (base->phy_lcpa)
3323 release_mem_region(base->phy_lcpa,
3324 base->lcpa_size);
3325 if (base->phy_start)
3326 release_mem_region(base->phy_start,
3327 base->phy_size);
3328 if (base->clk) {
3329 clk_disable(base->clk);
3330 clk_put(base->clk);
3331 }
3332
Narayanan G28c7a192011-11-22 13:56:55 +05303333 if (base->lcpa_regulator) {
3334 regulator_disable(base->lcpa_regulator);
3335 regulator_put(base->lcpa_regulator);
3336 }
3337
Linus Walleij8d318a52010-03-30 15:33:42 +02003338 kfree(base->lcla_pool.alloc_map);
3339 kfree(base->lookup_log_chans);
3340 kfree(base->lookup_phy_chans);
3341 kfree(base->phy_res);
3342 kfree(base);
3343 }
3344
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003345 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003346 return ret;
3347}
3348
3349static struct platform_driver d40_driver = {
3350 .driver = {
3351 .owner = THIS_MODULE,
3352 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303353 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003354 },
3355};
3356
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003357static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003358{
3359 return platform_driver_probe(&d40_driver, d40_probe);
3360}
Linus Walleija0eb2212011-05-18 14:18:57 +02003361subsys_initcall(stedma40_init);