blob: 972dc35770f9f4479ee8fef054f3c0a1676e632e [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
24#include "ste_dma40_ll.h"
25
26#define D40_NAME "dma40"
27
28#define D40_PHY_CHAN -1
29
30/* For masking out/in 2 bit channel positions */
31#define D40_CHAN_POS(chan) (2 * (chan / 2))
32#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
33
34/* Maximum iterations taken before giving up suspending a channel */
35#define D40_SUSPEND_MAX_IT 500
36
Narayanan G7fb3e752011-11-17 17:26:41 +053037/* Milliseconds */
38#define DMA40_AUTOSUSPEND_DELAY 100
39
Linus Walleij508849a2010-06-20 21:26:07 +000040/* Hardware requirement on LCLA alignment */
41#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000042
43/* Max number of links per event group */
44#define D40_LCLA_LINK_PER_EVENT_GRP 128
45#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
46
Linus Walleij508849a2010-06-20 21:26:07 +000047/* Attempts before giving up to trying to get pages that are aligned */
48#define MAX_LCLA_ALLOC_ATTEMPTS 256
49
50/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020051#define D40_ALLOC_FREE (1 << 31)
52#define D40_ALLOC_PHY (1 << 30)
53#define D40_ALLOC_LOG_FREE 0
54
Linus Walleij8d318a52010-03-30 15:33:42 +020055/**
56 * enum 40_command - The different commands and/or statuses.
57 *
58 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
59 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
60 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
61 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
62 */
63enum d40_command {
64 D40_DMA_STOP = 0,
65 D40_DMA_RUN = 1,
66 D40_DMA_SUSPEND_REQ = 2,
67 D40_DMA_SUSPENDED = 3
68};
69
Narayanan G7fb3e752011-11-17 17:26:41 +053070/*
71 * These are the registers that has to be saved and later restored
72 * when the DMA hw is powered off.
73 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
74 */
75static u32 d40_backup_regs[] = {
76 D40_DREG_LCPA,
77 D40_DREG_LCLA,
78 D40_DREG_PRMSE,
79 D40_DREG_PRMSO,
80 D40_DREG_PRMOE,
81 D40_DREG_PRMOO,
82};
83
84#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
85
86/* TODO: Check if all these registers have to be saved/restored on dma40 v3 */
87static u32 d40_backup_regs_v3[] = {
88 D40_DREG_PSEG1,
89 D40_DREG_PSEG2,
90 D40_DREG_PSEG3,
91 D40_DREG_PSEG4,
92 D40_DREG_PCEG1,
93 D40_DREG_PCEG2,
94 D40_DREG_PCEG3,
95 D40_DREG_PCEG4,
96 D40_DREG_RSEG1,
97 D40_DREG_RSEG2,
98 D40_DREG_RSEG3,
99 D40_DREG_RSEG4,
100 D40_DREG_RCEG1,
101 D40_DREG_RCEG2,
102 D40_DREG_RCEG3,
103 D40_DREG_RCEG4,
104};
105
106#define BACKUP_REGS_SZ_V3 ARRAY_SIZE(d40_backup_regs_v3)
107
108static u32 d40_backup_regs_chan[] = {
109 D40_CHAN_REG_SSCFG,
110 D40_CHAN_REG_SSELT,
111 D40_CHAN_REG_SSPTR,
112 D40_CHAN_REG_SSLNK,
113 D40_CHAN_REG_SDCFG,
114 D40_CHAN_REG_SDELT,
115 D40_CHAN_REG_SDPTR,
116 D40_CHAN_REG_SDLNK,
117};
118
Linus Walleij8d318a52010-03-30 15:33:42 +0200119/**
120 * struct d40_lli_pool - Structure for keeping LLIs in memory
121 *
122 * @base: Pointer to memory area when the pre_alloc_lli's are not large
123 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
124 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100125 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200126 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
127 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
128 * one buffer to one buffer.
129 */
130struct d40_lli_pool {
131 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000132 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100133 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200134 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000135 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200136};
137
138/**
139 * struct d40_desc - A descriptor is one DMA job.
140 *
141 * @lli_phy: LLI settings for physical channel. Both src and dst=
142 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
143 * lli_len equals one.
144 * @lli_log: Same as above but for logical channels.
145 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000146 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300147 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000148 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200149 * @txd: DMA engine struct. Used for among other things for communication
150 * during a transfer.
151 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200152 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530153 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200154 *
155 * This descriptor is used for both logical and physical transfers.
156 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200157struct d40_desc {
158 /* LLI physical */
159 struct d40_phy_lli_bidir lli_phy;
160 /* LLI logical */
161 struct d40_log_lli_bidir lli_log;
162
163 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000164 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000165 int lli_current;
166 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200167
168 struct dma_async_tx_descriptor txd;
169 struct list_head node;
170
Linus Walleij8d318a52010-03-30 15:33:42 +0200171 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100172 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200173};
174
175/**
176 * struct d40_lcla_pool - LCLA pool settings and data.
177 *
Linus Walleij508849a2010-06-20 21:26:07 +0000178 * @base: The virtual address of LCLA. 18 bit aligned.
179 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
180 * This pointer is only there for clean-up on error.
181 * @pages: The number of pages needed for all physical channels.
182 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200183 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000184 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200185 */
186struct d40_lcla_pool {
187 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100188 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000189 void *base_unaligned;
190 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200191 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000192 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200193};
194
195/**
196 * struct d40_phy_res - struct for handling eventlines mapped to physical
197 * channels.
198 *
199 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530200 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200201 * @num: The physical channel number of this entity.
202 * @allocated_src: Bit mapped to show which src event line's are mapped to
203 * this physical channel. Can also be free or physically allocated.
204 * @allocated_dst: Same as for src but is dst.
205 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000206 * event line number.
Linus Walleij8d318a52010-03-30 15:33:42 +0200207 */
208struct d40_phy_res {
209 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530210 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200211 int num;
212 u32 allocated_src;
213 u32 allocated_dst;
214};
215
216struct d40_base;
217
218/**
219 * struct d40_chan - Struct that describes a channel.
220 *
221 * @lock: A spinlock to protect this struct.
222 * @log_num: The logical number, if any of this channel.
223 * @completed: Starts with 1, after first interrupt it is set to dma engine's
224 * current cookie.
225 * @pending_tx: The number of pending transfers. Used between interrupt handler
226 * and tasklet.
227 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000228 * @phy_chan: Pointer to physical channel which this instance runs on. If this
229 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200230 * @chan: DMA engine handle.
231 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
232 * transfer and call client callback.
233 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200234 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200235 * @active: Active descriptor.
236 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200237 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200238 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000239 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200240 * @base: Pointer to the device instance struct.
241 * @src_def_cfg: Default cfg register setting for src.
242 * @dst_def_cfg: Default cfg register setting for dst.
243 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200244 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200245 * @runtime_addr: runtime configured address.
246 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200247 *
248 * This struct can either "be" a logical or a physical channel.
249 */
250struct d40_chan {
251 spinlock_t lock;
252 int log_num;
253 /* ID of the most recent completed transfer */
254 int completed;
255 int pending_tx;
256 bool busy;
257 struct d40_phy_res *phy_chan;
258 struct dma_chan chan;
259 struct tasklet_struct tasklet;
260 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200261 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200262 struct list_head active;
263 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200264 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200265 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000266 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200267 struct d40_base *base;
268 /* Default register configurations */
269 u32 src_def_cfg;
270 u32 dst_def_cfg;
271 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200272 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200273 /* Runtime reconfiguration */
274 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530275 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200276};
277
278/**
279 * struct d40_base - The big global struct, one for each probe'd instance.
280 *
281 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
282 * @execmd_lock: Lock for execute command usage since several channels share
283 * the same physical register.
284 * @dev: The device structure.
285 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700286 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200287 * @clk: Pointer to the DMA clock structure.
288 * @phy_start: Physical memory start of the DMA registers.
289 * @phy_size: Size of the DMA register map.
290 * @irq: The IRQ number.
291 * @num_phy_chans: The number of physical channels. Read from HW. This
292 * is the number of available channels for this driver, not counting "Secure
293 * mode" allocated physical channels.
294 * @num_log_chans: The number of logical channels. Calculated from
295 * num_phy_chans.
296 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
297 * @dma_slave: dma_device channels that can do only do slave transfers.
298 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530299 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200300 * @log_chans: Room for all possible logical channels in system.
301 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
302 * to log_chans entries.
303 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
304 * to phy_chans entries.
305 * @plat_data: Pointer to provided platform_data which is the driver
306 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530307 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200308 * @phy_res: Vector containing all physical channels.
309 * @lcla_pool: lcla pool settings and data.
310 * @lcpa_base: The virtual mapped address of LCPA.
311 * @phy_lcpa: The physical address of the LCPA.
312 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000313 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530314 * @reg_val_backup: Here the values of some hardware registers are stored
315 * before the DMA is powered off. They are restored when the power is back on.
316 * @reg_val_backup_v3: Backup of registers that only exits on dma40 v3 and
317 * later.
318 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
319 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
320 * @initialized: true if the dma has been initialized
Linus Walleij8d318a52010-03-30 15:33:42 +0200321 */
322struct d40_base {
323 spinlock_t interrupt_lock;
324 spinlock_t execmd_lock;
325 struct device *dev;
326 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700327 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200328 struct clk *clk;
329 phys_addr_t phy_start;
330 resource_size_t phy_size;
331 int irq;
332 int num_phy_chans;
333 int num_log_chans;
334 struct dma_device dma_both;
335 struct dma_device dma_slave;
336 struct dma_device dma_memcpy;
337 struct d40_chan *phy_chans;
338 struct d40_chan *log_chans;
339 struct d40_chan **lookup_log_chans;
340 struct d40_chan **lookup_phy_chans;
341 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530342 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200343 /* Physical half channels */
344 struct d40_phy_res *phy_res;
345 struct d40_lcla_pool lcla_pool;
346 void *lcpa_base;
347 dma_addr_t phy_lcpa;
348 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000349 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530350 u32 reg_val_backup[BACKUP_REGS_SZ];
351 u32 reg_val_backup_v3[BACKUP_REGS_SZ_V3];
352 u32 *reg_val_backup_chan;
353 u16 gcc_pwr_off_mask;
354 bool initialized;
Linus Walleij8d318a52010-03-30 15:33:42 +0200355};
356
357/**
358 * struct d40_interrupt_lookup - lookup table for interrupt handler
359 *
360 * @src: Interrupt mask register.
361 * @clr: Interrupt clear register.
362 * @is_error: true if this is an error interrupt.
363 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
364 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
365 */
366struct d40_interrupt_lookup {
367 u32 src;
368 u32 clr;
369 bool is_error;
370 int offset;
371};
372
373/**
374 * struct d40_reg_val - simple lookup struct
375 *
376 * @reg: The register.
377 * @val: The value that belongs to the register in reg.
378 */
379struct d40_reg_val {
380 unsigned int reg;
381 unsigned int val;
382};
383
Rabin Vincent262d2912011-01-25 11:18:05 +0100384static struct device *chan2dev(struct d40_chan *d40c)
385{
386 return &d40c->chan.dev->device;
387}
388
Rabin Vincent724a8572011-01-25 11:18:08 +0100389static bool chan_is_physical(struct d40_chan *chan)
390{
391 return chan->log_num == D40_PHY_CHAN;
392}
393
394static bool chan_is_logical(struct d40_chan *chan)
395{
396 return !chan_is_physical(chan);
397}
398
Rabin Vincent8ca84682011-01-25 11:18:07 +0100399static void __iomem *chan_base(struct d40_chan *chan)
400{
401 return chan->base->virtbase + D40_DREG_PCBASE +
402 chan->phy_chan->num * D40_DREG_PCDELTA;
403}
404
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100405#define d40_err(dev, format, arg...) \
406 dev_err(dev, "[%s] " format, __func__, ## arg)
407
408#define chan_err(d40c, format, arg...) \
409 d40_err(chan2dev(d40c), format, ## arg)
410
Rabin Vincentb00f9382011-01-25 11:18:15 +0100411static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100412 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200413{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100414 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200415 u32 align;
416 void *base;
417
418 if (is_log)
419 align = sizeof(struct d40_log_lli);
420 else
421 align = sizeof(struct d40_phy_lli);
422
423 if (lli_len == 1) {
424 base = d40d->lli_pool.pre_alloc_lli;
425 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
426 d40d->lli_pool.base = NULL;
427 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100428 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200429
430 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
431 d40d->lli_pool.base = base;
432
433 if (d40d->lli_pool.base == NULL)
434 return -ENOMEM;
435 }
436
437 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100438 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100439 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100440
441 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200442 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100443 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100444 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100445
446 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
447 d40d->lli_phy.src,
448 d40d->lli_pool.size,
449 DMA_TO_DEVICE);
450
451 if (dma_mapping_error(d40c->base->dev,
452 d40d->lli_pool.dma_addr)) {
453 kfree(d40d->lli_pool.base);
454 d40d->lli_pool.base = NULL;
455 d40d->lli_pool.dma_addr = 0;
456 return -ENOMEM;
457 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200458 }
459
460 return 0;
461}
462
Rabin Vincentb00f9382011-01-25 11:18:15 +0100463static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200464{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100465 if (d40d->lli_pool.dma_addr)
466 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
467 d40d->lli_pool.size, DMA_TO_DEVICE);
468
Linus Walleij8d318a52010-03-30 15:33:42 +0200469 kfree(d40d->lli_pool.base);
470 d40d->lli_pool.base = NULL;
471 d40d->lli_pool.size = 0;
472 d40d->lli_log.src = NULL;
473 d40d->lli_log.dst = NULL;
474 d40d->lli_phy.src = NULL;
475 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200476}
477
Jonas Aaberg698e4732010-08-09 12:08:56 +0000478static int d40_lcla_alloc_one(struct d40_chan *d40c,
479 struct d40_desc *d40d)
480{
481 unsigned long flags;
482 int i;
483 int ret = -EINVAL;
484 int p;
485
486 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
487
488 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
489
490 /*
491 * Allocate both src and dst at the same time, therefore the half
492 * start on 1 since 0 can't be used since zero is used as end marker.
493 */
494 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
495 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
496 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
497 d40d->lcla_alloc++;
498 ret = i;
499 break;
500 }
501 }
502
503 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
504
505 return ret;
506}
507
508static int d40_lcla_free_all(struct d40_chan *d40c,
509 struct d40_desc *d40d)
510{
511 unsigned long flags;
512 int i;
513 int ret = -EINVAL;
514
Rabin Vincent724a8572011-01-25 11:18:08 +0100515 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000516 return 0;
517
518 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
519
520 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
521 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
522 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
523 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
524 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
525 d40d->lcla_alloc--;
526 if (d40d->lcla_alloc == 0) {
527 ret = 0;
528 break;
529 }
530 }
531 }
532
533 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
534
535 return ret;
536
537}
538
Linus Walleij8d318a52010-03-30 15:33:42 +0200539static void d40_desc_remove(struct d40_desc *d40d)
540{
541 list_del(&d40d->node);
542}
543
544static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
545{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000546 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200547
548 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000549 struct d40_desc *d;
550 struct d40_desc *_d;
551
Narayanan G7fb3e752011-11-17 17:26:41 +0530552 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200553 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200554 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000555 desc = d;
556 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000557 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200558 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530559 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200560 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000561
562 if (!desc)
563 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
564
565 if (desc)
566 INIT_LIST_HEAD(&desc->node);
567
568 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200569}
570
571static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
572{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000573
Rabin Vincentb00f9382011-01-25 11:18:15 +0100574 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000575 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000576 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200577}
578
579static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
580{
581 list_add_tail(&desc->node, &d40c->active);
582}
583
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100584static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
585{
586 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
587 struct d40_phy_lli *lli_src = desc->lli_phy.src;
588 void __iomem *base = chan_base(chan);
589
590 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
591 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
592 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
593 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
594
595 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
596 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
597 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
598 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
599}
600
Rabin Vincente65889c2011-01-25 11:18:31 +0100601static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
602{
603 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
604 struct d40_log_lli_bidir *lli = &desc->lli_log;
605 int lli_current = desc->lli_current;
606 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100607 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100608 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100609 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530610 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100611 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100612
Rabin Vincent0c842b52011-01-25 11:18:35 +0100613 /*
614 * We may have partially running cyclic transfers, in case we did't get
615 * enough LCLA entries.
616 */
617 linkback = cyclic && lli_current == 0;
618
619 /*
620 * For linkback, we need one LCLA even with only one link, because we
621 * can't link back to the one in LCPA space
622 */
623 if (linkback || (lli_len - lli_current > 1)) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100624 curr_lcla = d40_lcla_alloc_one(chan, desc);
Rabin Vincent0c842b52011-01-25 11:18:35 +0100625 first_lcla = curr_lcla;
626 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100627
Rabin Vincent0c842b52011-01-25 11:18:35 +0100628 /*
629 * For linkback, we normally load the LCPA in the loop since we need to
630 * link it to the second LCLA and not the first. However, if we
631 * couldn't even get a first LCLA, then we have to run in LCPA and
632 * reload manually.
633 */
634 if (!linkback || curr_lcla == -EINVAL) {
635 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100636
Rabin Vincent0c842b52011-01-25 11:18:35 +0100637 if (curr_lcla == -EINVAL)
638 flags |= LLI_TERM_INT;
639
640 d40_log_lli_lcpa_write(chan->lcpa,
641 &lli->dst[lli_current],
642 &lli->src[lli_current],
643 curr_lcla,
644 flags);
645 lli_current++;
646 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100647
648 if (curr_lcla < 0)
649 goto out;
650
Rabin Vincente65889c2011-01-25 11:18:31 +0100651 for (; lli_current < lli_len; lli_current++) {
652 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
653 8 * curr_lcla * 2;
654 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100655 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100656 int next_lcla;
657
658 if (lli_current + 1 < lli_len)
659 next_lcla = d40_lcla_alloc_one(chan, desc);
660 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100661 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100662
Rabin Vincent0c842b52011-01-25 11:18:35 +0100663 if (cyclic || next_lcla == -EINVAL)
664 flags |= LLI_TERM_INT;
665
666 if (linkback && curr_lcla == first_lcla) {
667 /* First link goes in both LCPA and LCLA */
668 d40_log_lli_lcpa_write(chan->lcpa,
669 &lli->dst[lli_current],
670 &lli->src[lli_current],
671 next_lcla, flags);
672 }
673
674 /*
675 * One unused LCLA in the cyclic case if the very first
676 * next_lcla fails...
677 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100678 d40_log_lli_lcla_write(lcla,
679 &lli->dst[lli_current],
680 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100681 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100682
Narayanan G28c7a192011-11-22 13:56:55 +0530683 /*
684 * Cache maintenance is not needed if lcla is
685 * mapped in esram
686 */
687 if (!use_esram_lcla) {
688 dma_sync_single_range_for_device(chan->base->dev,
689 pool->dma_addr, lcla_offset,
690 2 * sizeof(struct d40_log_lli),
691 DMA_TO_DEVICE);
692 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100693 curr_lcla = next_lcla;
694
Rabin Vincent0c842b52011-01-25 11:18:35 +0100695 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100696 lli_current++;
697 break;
698 }
699 }
700
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100701out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100702 desc->lli_current = lli_current;
703}
704
Jonas Aaberg698e4732010-08-09 12:08:56 +0000705static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
706{
Rabin Vincent724a8572011-01-25 11:18:08 +0100707 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100708 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000709 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100710 } else
711 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000712}
713
Linus Walleij8d318a52010-03-30 15:33:42 +0200714static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
715{
716 struct d40_desc *d;
717
718 if (list_empty(&d40c->active))
719 return NULL;
720
721 d = list_first_entry(&d40c->active,
722 struct d40_desc,
723 node);
724 return d;
725}
726
Per Forlin74043682011-08-29 13:33:34 +0200727/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200728static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
729{
Per Forlin74043682011-08-29 13:33:34 +0200730 d40_desc_remove(desc);
731 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200732 list_add_tail(&desc->node, &d40c->pending_queue);
733}
734
735static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
736{
737 struct d40_desc *d;
738
739 if (list_empty(&d40c->pending_queue))
740 return NULL;
741
742 d = list_first_entry(&d40c->pending_queue,
743 struct d40_desc,
744 node);
745 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200746}
747
748static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
749{
750 struct d40_desc *d;
751
752 if (list_empty(&d40c->queue))
753 return NULL;
754
755 d = list_first_entry(&d40c->queue,
756 struct d40_desc,
757 node);
758 return d;
759}
760
Per Forlind49278e2010-12-20 18:31:38 +0100761static int d40_psize_2_burst_size(bool is_log, int psize)
762{
763 if (is_log) {
764 if (psize == STEDMA40_PSIZE_LOG_1)
765 return 1;
766 } else {
767 if (psize == STEDMA40_PSIZE_PHY_1)
768 return 1;
769 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200770
Per Forlind49278e2010-12-20 18:31:38 +0100771 return 2 << psize;
772}
773
774/*
775 * The dma only supports transmitting packages up to
776 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
777 * dma elements required to send the entire sg list
778 */
779static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
780{
781 int dmalen;
782 u32 max_w = max(data_width1, data_width2);
783 u32 min_w = min(data_width1, data_width2);
784 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
785
786 if (seg_max > STEDMA40_MAX_SEG_SIZE)
787 seg_max -= (1 << max_w);
788
789 if (!IS_ALIGNED(size, 1 << max_w))
790 return -EINVAL;
791
792 if (size <= seg_max)
793 dmalen = 1;
794 else {
795 dmalen = size / seg_max;
796 if (dmalen * seg_max < size)
797 dmalen++;
798 }
799 return dmalen;
800}
801
802static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
803 u32 data_width1, u32 data_width2)
804{
805 struct scatterlist *sg;
806 int i;
807 int len = 0;
808 int ret;
809
810 for_each_sg(sgl, sg, sg_len, i) {
811 ret = d40_size_2_dmalen(sg_dma_len(sg),
812 data_width1, data_width2);
813 if (ret < 0)
814 return ret;
815 len += ret;
816 }
817 return len;
818}
819
Narayanan G7fb3e752011-11-17 17:26:41 +0530820
821#ifdef CONFIG_PM
822static void dma40_backup(void __iomem *baseaddr, u32 *backup,
823 u32 *regaddr, int num, bool save)
824{
825 int i;
826
827 for (i = 0; i < num; i++) {
828 void __iomem *addr = baseaddr + regaddr[i];
829
830 if (save)
831 backup[i] = readl_relaxed(addr);
832 else
833 writel_relaxed(backup[i], addr);
834 }
835}
836
837static void d40_save_restore_registers(struct d40_base *base, bool save)
838{
839 int i;
840
841 /* Save/Restore channel specific registers */
842 for (i = 0; i < base->num_phy_chans; i++) {
843 void __iomem *addr;
844 int idx;
845
846 if (base->phy_res[i].reserved)
847 continue;
848
849 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
850 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
851
852 dma40_backup(addr, &base->reg_val_backup_chan[idx],
853 d40_backup_regs_chan,
854 ARRAY_SIZE(d40_backup_regs_chan),
855 save);
856 }
857
858 /* Save/Restore global registers */
859 dma40_backup(base->virtbase, base->reg_val_backup,
860 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
861 save);
862
863 /* Save/Restore registers only existing on dma40 v3 and later */
864 if (base->rev >= 3)
865 dma40_backup(base->virtbase, base->reg_val_backup_v3,
866 d40_backup_regs_v3,
867 ARRAY_SIZE(d40_backup_regs_v3),
868 save);
869}
870#else
871static void d40_save_restore_registers(struct d40_base *base, bool save)
872{
873}
874#endif
Linus Walleij8d318a52010-03-30 15:33:42 +0200875
876static int d40_channel_execute_command(struct d40_chan *d40c,
877 enum d40_command command)
878{
Jonas Aaberg767a9672010-08-09 12:08:34 +0000879 u32 status;
880 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +0200881 void __iomem *active_reg;
882 int ret = 0;
883 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000884 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +0200885
886 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
887
888 if (d40c->phy_chan->num % 2 == 0)
889 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
890 else
891 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
892
893 if (command == D40_DMA_SUSPEND_REQ) {
894 status = (readl(active_reg) &
895 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
896 D40_CHAN_POS(d40c->phy_chan->num);
897
898 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
899 goto done;
900 }
901
Jonas Aaberg1d392a72010-06-20 21:26:01 +0000902 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
903 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
904 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +0200905
906 if (command == D40_DMA_SUSPEND_REQ) {
907
908 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
909 status = (readl(active_reg) &
910 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
911 D40_CHAN_POS(d40c->phy_chan->num);
912
913 cpu_relax();
914 /*
915 * Reduce the number of bus accesses while
916 * waiting for the DMA to suspend.
917 */
918 udelay(3);
919
920 if (status == D40_DMA_STOP ||
921 status == D40_DMA_SUSPENDED)
922 break;
923 }
924
925 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100926 chan_err(d40c,
927 "unable to suspend the chl %d (log: %d) status %x\n",
928 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +0200929 status);
930 dump_stack();
931 ret = -EBUSY;
932 }
933
934 }
935done:
936 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
937 return ret;
938}
939
940static void d40_term_all(struct d40_chan *d40c)
941{
942 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +0200943 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200944
945 /* Release active descriptors */
946 while ((d40d = d40_first_active_get(d40c))) {
947 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200948 d40_desc_free(d40c, d40d);
949 }
950
951 /* Release queued descriptors waiting for transfer */
952 while ((d40d = d40_first_queued(d40c))) {
953 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200954 d40_desc_free(d40c, d40d);
955 }
956
Per Forlina8f30672011-06-26 23:29:52 +0200957 /* Release pending descriptors */
958 while ((d40d = d40_first_pending(d40c))) {
959 d40_desc_remove(d40d);
960 d40_desc_free(d40c, d40d);
961 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200962
Per Forlin74043682011-08-29 13:33:34 +0200963 /* Release client owned descriptors */
964 if (!list_empty(&d40c->client))
965 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
966 d40_desc_remove(d40d);
967 d40_desc_free(d40c, d40d);
968 }
969
Per Forlin82babbb362011-08-29 13:33:35 +0200970 /* Release descriptors in prepare queue */
971 if (!list_empty(&d40c->prepare_queue))
972 list_for_each_entry_safe(d40d, _d,
973 &d40c->prepare_queue, node) {
974 d40_desc_remove(d40d);
975 d40_desc_free(d40c, d40d);
976 }
Per Forlin74043682011-08-29 13:33:34 +0200977
Linus Walleij8d318a52010-03-30 15:33:42 +0200978 d40c->pending_tx = 0;
979 d40c->busy = false;
980}
981
Rabin Vincent262d2912011-01-25 11:18:05 +0100982static void __d40_config_set_event(struct d40_chan *d40c, bool enable,
983 u32 event, int reg)
984{
Rabin Vincent8ca84682011-01-25 11:18:07 +0100985 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +0100986 int tries;
987
988 if (!enable) {
989 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
990 | ~D40_EVENTLINE_MASK(event), addr);
991 return;
992 }
993
994 /*
995 * The hardware sometimes doesn't register the enable when src and dst
996 * event lines are active on the same logical channel. Retry to ensure
997 * it does. Usually only one retry is sufficient.
998 */
999 tries = 100;
1000 while (--tries) {
1001 writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1002 | ~D40_EVENTLINE_MASK(event), addr);
1003
1004 if (readl(addr) & D40_EVENTLINE_MASK(event))
1005 break;
1006 }
1007
1008 if (tries != 99)
1009 dev_dbg(chan2dev(d40c),
1010 "[%s] workaround enable S%cLNK (%d tries)\n",
1011 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1012 100 - tries);
1013
1014 WARN_ON(!tries);
1015}
1016
Linus Walleij8d318a52010-03-30 15:33:42 +02001017static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
1018{
Linus Walleij8d318a52010-03-30 15:33:42 +02001019 unsigned long flags;
1020
Linus Walleij8d318a52010-03-30 15:33:42 +02001021 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1022
1023 /* Enable event line connected to device (or memcpy) */
1024 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1025 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1026 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1027
Rabin Vincent262d2912011-01-25 11:18:05 +01001028 __d40_config_set_event(d40c, do_enable, event,
1029 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001030 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001031
Linus Walleij8d318a52010-03-30 15:33:42 +02001032 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1033 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1034
Rabin Vincent262d2912011-01-25 11:18:05 +01001035 __d40_config_set_event(d40c, do_enable, event,
1036 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001037 }
1038
1039 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1040}
1041
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001042static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001043{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001044 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001045 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001046
Rabin Vincent8ca84682011-01-25 11:18:07 +01001047 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1048 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001049
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001050 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001051}
1052
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001053static u32 d40_get_prmo(struct d40_chan *d40c)
1054{
1055 static const unsigned int phy_map[] = {
1056 [STEDMA40_PCHAN_BASIC_MODE]
1057 = D40_DREG_PRMO_PCHAN_BASIC,
1058 [STEDMA40_PCHAN_MODULO_MODE]
1059 = D40_DREG_PRMO_PCHAN_MODULO,
1060 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1061 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1062 };
1063 static const unsigned int log_map[] = {
1064 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1065 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1066 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1067 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1068 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1069 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1070 };
1071
Rabin Vincent724a8572011-01-25 11:18:08 +01001072 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001073 return phy_map[d40c->dma_cfg.mode_opt];
1074 else
1075 return log_map[d40c->dma_cfg.mode_opt];
1076}
1077
Jonas Aabergb55912c2010-08-09 12:08:02 +00001078static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001079{
1080 u32 addr_base;
1081 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001082
1083 /* Odd addresses are even addresses + 4 */
1084 addr_base = (d40c->phy_chan->num % 2) * 4;
1085 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001086 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001087 D40_CHAN_POS(d40c->phy_chan->num);
1088 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1089
1090 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001091 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001092
1093 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1094
Rabin Vincent724a8572011-01-25 11:18:08 +01001095 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001096 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1097 & D40_SREG_ELEM_LOG_LIDX_MASK;
1098 void __iomem *chanbase = chan_base(d40c);
1099
Linus Walleij8d318a52010-03-30 15:33:42 +02001100 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001101 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1102 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001103
Jonas Aabergb55912c2010-08-09 12:08:02 +00001104 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001105 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1106 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Linus Walleij8d318a52010-03-30 15:33:42 +02001107 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001108}
1109
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001110static u32 d40_residue(struct d40_chan *d40c)
1111{
1112 u32 num_elt;
1113
Rabin Vincent724a8572011-01-25 11:18:08 +01001114 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001115 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1116 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001117 else {
1118 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1119 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1120 >> D40_SREG_ELEM_PHY_ECNT_POS;
1121 }
1122
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001123 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1124}
1125
1126static bool d40_tx_is_linked(struct d40_chan *d40c)
1127{
1128 bool is_link;
1129
Rabin Vincent724a8572011-01-25 11:18:08 +01001130 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001131 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1132 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001133 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1134 & D40_SREG_LNK_PHYS_LNK_MASK;
1135
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001136 return is_link;
1137}
1138
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001139static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001140{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001141 int res = 0;
1142 unsigned long flags;
1143
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001144 if (!d40c->busy)
1145 return 0;
1146
Narayanan G7fb3e752011-11-17 17:26:41 +05301147 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001148 spin_lock_irqsave(&d40c->lock, flags);
1149
1150 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1151 if (res == 0) {
Rabin Vincent724a8572011-01-25 11:18:08 +01001152 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001153 d40_config_set_event(d40c, false);
1154 /* Resume the other logical channels if any */
1155 if (d40_chan_has_events(d40c))
1156 res = d40_channel_execute_command(d40c,
1157 D40_DMA_RUN);
1158 }
1159 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301160 pm_runtime_mark_last_busy(d40c->base->dev);
1161 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001162 spin_unlock_irqrestore(&d40c->lock, flags);
1163 return res;
1164}
1165
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001166static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001167{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001168 int res = 0;
1169 unsigned long flags;
1170
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001171 if (!d40c->busy)
1172 return 0;
1173
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001174 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301175 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001176 if (d40c->base->rev == 0)
Rabin Vincent724a8572011-01-25 11:18:08 +01001177 if (chan_is_logical(d40c)) {
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001178 res = d40_channel_execute_command(d40c,
1179 D40_DMA_SUSPEND_REQ);
1180 goto no_suspend;
1181 }
1182
1183 /* If bytes left to transfer or linked tx resume job */
1184 if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
1185
Rabin Vincent724a8572011-01-25 11:18:08 +01001186 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001187 d40_config_set_event(d40c, true);
1188
1189 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1190 }
1191
1192no_suspend:
Narayanan G7fb3e752011-11-17 17:26:41 +05301193 pm_runtime_mark_last_busy(d40c->base->dev);
1194 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001195 spin_unlock_irqrestore(&d40c->lock, flags);
1196 return res;
1197}
1198
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001199static int d40_terminate_all(struct d40_chan *chan)
1200{
1201 unsigned long flags;
1202 int ret = 0;
1203
1204 ret = d40_pause(chan);
1205 if (!ret && chan_is_physical(chan))
1206 ret = d40_channel_execute_command(chan, D40_DMA_STOP);
1207
1208 spin_lock_irqsave(&chan->lock, flags);
1209 d40_term_all(chan);
1210 spin_unlock_irqrestore(&chan->lock, flags);
1211
1212 return ret;
1213}
1214
Linus Walleij8d318a52010-03-30 15:33:42 +02001215static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1216{
1217 struct d40_chan *d40c = container_of(tx->chan,
1218 struct d40_chan,
1219 chan);
1220 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1221 unsigned long flags;
1222
1223 spin_lock_irqsave(&d40c->lock, flags);
1224
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001225 d40c->chan.cookie++;
1226
1227 if (d40c->chan.cookie < 0)
1228 d40c->chan.cookie = 1;
1229
1230 d40d->txd.cookie = d40c->chan.cookie;
1231
Linus Walleij8d318a52010-03-30 15:33:42 +02001232 d40_desc_queue(d40c, d40d);
1233
1234 spin_unlock_irqrestore(&d40c->lock, flags);
1235
1236 return tx->cookie;
1237}
1238
1239static int d40_start(struct d40_chan *d40c)
1240{
Linus Walleijf4185592010-06-22 18:06:42 -07001241 if (d40c->base->rev == 0) {
1242 int err;
1243
Rabin Vincent724a8572011-01-25 11:18:08 +01001244 if (chan_is_logical(d40c)) {
Linus Walleijf4185592010-06-22 18:06:42 -07001245 err = d40_channel_execute_command(d40c,
1246 D40_DMA_SUSPEND_REQ);
1247 if (err)
1248 return err;
1249 }
1250 }
1251
Rabin Vincent724a8572011-01-25 11:18:08 +01001252 if (chan_is_logical(d40c))
Linus Walleij8d318a52010-03-30 15:33:42 +02001253 d40_config_set_event(d40c, true);
Linus Walleij8d318a52010-03-30 15:33:42 +02001254
Jonas Aaberg0c322692010-06-20 21:25:46 +00001255 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001256}
1257
1258static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1259{
1260 struct d40_desc *d40d;
1261 int err;
1262
1263 /* Start queued jobs, if any */
1264 d40d = d40_first_queued(d40c);
1265
1266 if (d40d != NULL) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301267 if (!d40c->busy)
1268 d40c->busy = true;
1269
1270 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001271
1272 /* Remove from queue */
1273 d40_desc_remove(d40d);
1274
1275 /* Add to active queue */
1276 d40_desc_submit(d40c, d40d);
1277
Rabin Vincent7d83a852011-01-25 11:18:06 +01001278 /* Initiate DMA job */
1279 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001280
Rabin Vincent7d83a852011-01-25 11:18:06 +01001281 /* Start dma job */
1282 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001283
Rabin Vincent7d83a852011-01-25 11:18:06 +01001284 if (err)
1285 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001286 }
1287
1288 return d40d;
1289}
1290
1291/* called from interrupt context */
1292static void dma_tc_handle(struct d40_chan *d40c)
1293{
1294 struct d40_desc *d40d;
1295
Linus Walleij8d318a52010-03-30 15:33:42 +02001296 /* Get first active entry from list */
1297 d40d = d40_first_active_get(d40c);
1298
1299 if (d40d == NULL)
1300 return;
1301
Rabin Vincent0c842b52011-01-25 11:18:35 +01001302 if (d40d->cyclic) {
1303 /*
1304 * If this was a paritially loaded list, we need to reloaded
1305 * it, and only when the list is completed. We need to check
1306 * for done because the interrupt will hit for every link, and
1307 * not just the last one.
1308 */
1309 if (d40d->lli_current < d40d->lli_len
1310 && !d40_tx_is_linked(d40c)
1311 && !d40_residue(d40c)) {
1312 d40_lcla_free_all(d40c, d40d);
1313 d40_desc_load(d40c, d40d);
1314 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001315
Rabin Vincent0c842b52011-01-25 11:18:35 +01001316 if (d40d->lli_current == d40d->lli_len)
1317 d40d->lli_current = 0;
1318 }
1319 } else {
1320 d40_lcla_free_all(d40c, d40d);
1321
1322 if (d40d->lli_current < d40d->lli_len) {
1323 d40_desc_load(d40c, d40d);
1324 /* Start dma job */
1325 (void) d40_start(d40c);
1326 return;
1327 }
1328
1329 if (d40_queue_start(d40c) == NULL)
1330 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301331 pm_runtime_mark_last_busy(d40c->base->dev);
1332 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001333 }
1334
Linus Walleij8d318a52010-03-30 15:33:42 +02001335 d40c->pending_tx++;
1336 tasklet_schedule(&d40c->tasklet);
1337
1338}
1339
1340static void dma_tasklet(unsigned long data)
1341{
1342 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001343 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001344 unsigned long flags;
1345 dma_async_tx_callback callback;
1346 void *callback_param;
1347
1348 spin_lock_irqsave(&d40c->lock, flags);
1349
1350 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001351 d40d = d40_first_active_get(d40c);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001352 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001353 goto err;
1354
Rabin Vincent0c842b52011-01-25 11:18:35 +01001355 if (!d40d->cyclic)
1356 d40c->completed = d40d->txd.cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001357
1358 /*
1359 * If terminating a channel pending_tx is set to zero.
1360 * This prevents any finished active jobs to return to the client.
1361 */
1362 if (d40c->pending_tx == 0) {
1363 spin_unlock_irqrestore(&d40c->lock, flags);
1364 return;
1365 }
1366
1367 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001368 callback = d40d->txd.callback;
1369 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001370
Rabin Vincent0c842b52011-01-25 11:18:35 +01001371 if (!d40d->cyclic) {
1372 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001373 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001374 d40_desc_free(d40c, d40d);
1375 } else {
1376 if (!d40d->is_in_client_list) {
1377 d40_desc_remove(d40d);
1378 d40_lcla_free_all(d40c, d40d);
1379 list_add_tail(&d40d->node, &d40c->client);
1380 d40d->is_in_client_list = true;
1381 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001382 }
1383 }
1384
1385 d40c->pending_tx--;
1386
1387 if (d40c->pending_tx)
1388 tasklet_schedule(&d40c->tasklet);
1389
1390 spin_unlock_irqrestore(&d40c->lock, flags);
1391
Jonas Aaberg767a9672010-08-09 12:08:34 +00001392 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001393 callback(callback_param);
1394
1395 return;
1396
1397 err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001398 /* Rescue manoeuvre if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001399 if (d40c->pending_tx > 0)
1400 d40c->pending_tx--;
1401 spin_unlock_irqrestore(&d40c->lock, flags);
1402}
1403
1404static irqreturn_t d40_handle_interrupt(int irq, void *data)
1405{
1406 static const struct d40_interrupt_lookup il[] = {
1407 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
1408 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
1409 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
1410 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
1411 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
1412 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
1413 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
1414 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
1415 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
1416 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
1417 };
1418
1419 int i;
1420 u32 regs[ARRAY_SIZE(il)];
Linus Walleij8d318a52010-03-30 15:33:42 +02001421 u32 idx;
1422 u32 row;
1423 long chan = -1;
1424 struct d40_chan *d40c;
1425 unsigned long flags;
1426 struct d40_base *base = data;
1427
1428 spin_lock_irqsave(&base->interrupt_lock, flags);
1429
1430 /* Read interrupt status of both logical and physical channels */
1431 for (i = 0; i < ARRAY_SIZE(il); i++)
1432 regs[i] = readl(base->virtbase + il[i].src);
1433
1434 for (;;) {
1435
1436 chan = find_next_bit((unsigned long *)regs,
1437 BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
1438
1439 /* No more set bits found? */
1440 if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
1441 break;
1442
1443 row = chan / BITS_PER_LONG;
1444 idx = chan & (BITS_PER_LONG - 1);
1445
1446 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001447 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001448
1449 if (il[row].offset == D40_PHY_CHAN)
1450 d40c = base->lookup_phy_chans[idx];
1451 else
1452 d40c = base->lookup_log_chans[il[row].offset + idx];
1453 spin_lock(&d40c->lock);
1454
1455 if (!il[row].is_error)
1456 dma_tc_handle(d40c);
1457 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001458 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1459 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001460
1461 spin_unlock(&d40c->lock);
1462 }
1463
1464 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1465
1466 return IRQ_HANDLED;
1467}
1468
Linus Walleij8d318a52010-03-30 15:33:42 +02001469static int d40_validate_conf(struct d40_chan *d40c,
1470 struct stedma40_chan_cfg *conf)
1471{
1472 int res = 0;
1473 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1474 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001475 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001476
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001477 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001478 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001479 res = -EINVAL;
1480 }
1481
1482 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1483 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1484 d40c->runtime_addr == 0) {
1485
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001486 chan_err(d40c, "Invalid TX channel address (%d)\n",
1487 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001488 res = -EINVAL;
1489 }
1490
1491 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1492 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1493 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001494 chan_err(d40c, "Invalid RX channel address (%d)\n",
1495 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001496 res = -EINVAL;
1497 }
1498
1499 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001500 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001501 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001502 res = -EINVAL;
1503 }
1504
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001505 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001506 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001507 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001508 res = -EINVAL;
1509 }
1510
1511 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1512 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001513 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001514 res = -EINVAL;
1515 }
1516
1517 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1518 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001519 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001520 res = -EINVAL;
1521 }
1522
1523 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1524 /*
1525 * DMAC HW supports it. Will be added to this driver,
1526 * in case any dma client requires it.
1527 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001528 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001529 res = -EINVAL;
1530 }
1531
Per Forlind49278e2010-12-20 18:31:38 +01001532 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1533 (1 << conf->src_info.data_width) !=
1534 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1535 (1 << conf->dst_info.data_width)) {
1536 /*
1537 * The DMAC hardware only supports
1538 * src (burst x width) == dst (burst x width)
1539 */
1540
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001541 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001542 res = -EINVAL;
1543 }
1544
Linus Walleij8d318a52010-03-30 15:33:42 +02001545 return res;
1546}
1547
Narayanan G5cd326f2011-11-30 19:20:42 +05301548static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1549 bool is_src, int log_event_line, bool is_log,
1550 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001551{
1552 unsigned long flags;
1553 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301554
1555 *first_user = ((phy->allocated_src | phy->allocated_dst)
1556 == D40_ALLOC_FREE);
1557
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001558 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001559 /* Physical interrupts are masked per physical full channel */
1560 if (phy->allocated_src == D40_ALLOC_FREE &&
1561 phy->allocated_dst == D40_ALLOC_FREE) {
1562 phy->allocated_dst = D40_ALLOC_PHY;
1563 phy->allocated_src = D40_ALLOC_PHY;
1564 goto found;
1565 } else
1566 goto not_found;
1567 }
1568
1569 /* Logical channel */
1570 if (is_src) {
1571 if (phy->allocated_src == D40_ALLOC_PHY)
1572 goto not_found;
1573
1574 if (phy->allocated_src == D40_ALLOC_FREE)
1575 phy->allocated_src = D40_ALLOC_LOG_FREE;
1576
1577 if (!(phy->allocated_src & (1 << log_event_line))) {
1578 phy->allocated_src |= 1 << log_event_line;
1579 goto found;
1580 } else
1581 goto not_found;
1582 } else {
1583 if (phy->allocated_dst == D40_ALLOC_PHY)
1584 goto not_found;
1585
1586 if (phy->allocated_dst == D40_ALLOC_FREE)
1587 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1588
1589 if (!(phy->allocated_dst & (1 << log_event_line))) {
1590 phy->allocated_dst |= 1 << log_event_line;
1591 goto found;
1592 } else
1593 goto not_found;
1594 }
1595
1596not_found:
1597 spin_unlock_irqrestore(&phy->lock, flags);
1598 return false;
1599found:
1600 spin_unlock_irqrestore(&phy->lock, flags);
1601 return true;
1602}
1603
1604static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1605 int log_event_line)
1606{
1607 unsigned long flags;
1608 bool is_free = false;
1609
1610 spin_lock_irqsave(&phy->lock, flags);
1611 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001612 phy->allocated_dst = D40_ALLOC_FREE;
1613 phy->allocated_src = D40_ALLOC_FREE;
1614 is_free = true;
1615 goto out;
1616 }
1617
1618 /* Logical channel */
1619 if (is_src) {
1620 phy->allocated_src &= ~(1 << log_event_line);
1621 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1622 phy->allocated_src = D40_ALLOC_FREE;
1623 } else {
1624 phy->allocated_dst &= ~(1 << log_event_line);
1625 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1626 phy->allocated_dst = D40_ALLOC_FREE;
1627 }
1628
1629 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1630 D40_ALLOC_FREE);
1631
1632out:
1633 spin_unlock_irqrestore(&phy->lock, flags);
1634
1635 return is_free;
1636}
1637
Narayanan G5cd326f2011-11-30 19:20:42 +05301638static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001639{
1640 int dev_type;
1641 int event_group;
1642 int event_line;
1643 struct d40_phy_res *phys;
1644 int i;
1645 int j;
1646 int log_num;
1647 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001648 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001649
1650 phys = d40c->base->phy_res;
1651
1652 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1653 dev_type = d40c->dma_cfg.src_dev_type;
1654 log_num = 2 * dev_type;
1655 is_src = true;
1656 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1657 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1658 /* dst event lines are used for logical memcpy */
1659 dev_type = d40c->dma_cfg.dst_dev_type;
1660 log_num = 2 * dev_type + 1;
1661 is_src = false;
1662 } else
1663 return -EINVAL;
1664
1665 event_group = D40_TYPE_TO_GROUP(dev_type);
1666 event_line = D40_TYPE_TO_EVENT(dev_type);
1667
1668 if (!is_log) {
1669 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1670 /* Find physical half channel */
1671 for (i = 0; i < d40c->base->num_phy_chans; i++) {
1672
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001673 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301674 0, is_log,
1675 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001676 goto found_phy;
1677 }
1678 } else
1679 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1680 int phy_num = j + event_group * 2;
1681 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001682 if (d40_alloc_mask_set(&phys[i],
1683 is_src,
1684 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301685 is_log,
1686 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001687 goto found_phy;
1688 }
1689 }
1690 return -EINVAL;
1691found_phy:
1692 d40c->phy_chan = &phys[i];
1693 d40c->log_num = D40_PHY_CHAN;
1694 goto out;
1695 }
1696 if (dev_type == -1)
1697 return -EINVAL;
1698
1699 /* Find logical channel */
1700 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1701 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301702
1703 if (d40c->dma_cfg.use_fixed_channel) {
1704 i = d40c->dma_cfg.phy_channel;
1705
1706 if ((i != phy_num) && (i != phy_num + 1)) {
1707 dev_err(chan2dev(d40c),
1708 "invalid fixed phy channel %d\n", i);
1709 return -EINVAL;
1710 }
1711
1712 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1713 is_log, first_phy_user))
1714 goto found_log;
1715
1716 dev_err(chan2dev(d40c),
1717 "could not allocate fixed phy channel %d\n", i);
1718 return -EINVAL;
1719 }
1720
Linus Walleij8d318a52010-03-30 15:33:42 +02001721 /*
1722 * Spread logical channels across all available physical rather
1723 * than pack every logical channel at the first available phy
1724 * channels.
1725 */
1726 if (is_src) {
1727 for (i = phy_num; i < phy_num + 2; i++) {
1728 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301729 event_line, is_log,
1730 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001731 goto found_log;
1732 }
1733 } else {
1734 for (i = phy_num + 1; i >= phy_num; i--) {
1735 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301736 event_line, is_log,
1737 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001738 goto found_log;
1739 }
1740 }
1741 }
1742 return -EINVAL;
1743
1744found_log:
1745 d40c->phy_chan = &phys[i];
1746 d40c->log_num = log_num;
1747out:
1748
1749 if (is_log)
1750 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1751 else
1752 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1753
1754 return 0;
1755
1756}
1757
Linus Walleij8d318a52010-03-30 15:33:42 +02001758static int d40_config_memcpy(struct d40_chan *d40c)
1759{
1760 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1761
1762 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1763 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1764 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1765 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1766 memcpy[d40c->chan.chan_id];
1767
1768 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1769 dma_has_cap(DMA_SLAVE, cap)) {
1770 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1771 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001772 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001773 return -EINVAL;
1774 }
1775
1776 return 0;
1777}
1778
1779
1780static int d40_free_dma(struct d40_chan *d40c)
1781{
1782
1783 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001784 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001785 struct d40_phy_res *phy = d40c->phy_chan;
1786 bool is_src;
1787
1788 /* Terminate all queued and active transfers */
1789 d40_term_all(d40c);
1790
1791 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001792 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001793 return -EINVAL;
1794 }
1795
1796 if (phy->allocated_src == D40_ALLOC_FREE &&
1797 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001798 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001799 return -EINVAL;
1800 }
1801
Linus Walleij8d318a52010-03-30 15:33:42 +02001802 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1803 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1804 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001805 is_src = false;
1806 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1807 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001808 is_src = true;
1809 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001810 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001811 return -EINVAL;
1812 }
1813
Narayanan G7fb3e752011-11-17 17:26:41 +05301814 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001815 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1816 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001817 chan_err(d40c, "suspend failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05301818 goto out;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001819 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001820
Rabin Vincent724a8572011-01-25 11:18:08 +01001821 if (chan_is_logical(d40c)) {
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001822 /* Release logical channel, deactivate the event line */
1823
1824 d40_config_set_event(d40c, false);
Linus Walleij8d318a52010-03-30 15:33:42 +02001825 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
1826
1827 /*
1828 * Check if there are more logical allocation
1829 * on this phy channel.
1830 */
1831 if (!d40_alloc_mask_free(phy, is_src, event)) {
1832 /* Resume the other logical channels if any */
1833 if (d40_chan_has_events(d40c)) {
1834 res = d40_channel_execute_command(d40c,
1835 D40_DMA_RUN);
Narayanan G7fb3e752011-11-17 17:26:41 +05301836 if (res)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001837 chan_err(d40c,
1838 "Executing RUN command\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001839 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301840 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02001841 }
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001842 } else {
1843 (void) d40_alloc_mask_free(phy, is_src, 0);
1844 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001845
1846 /* Release physical channel */
1847 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
1848 if (res) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001849 chan_err(d40c, "Failed to stop channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05301850 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02001851 }
Narayanan G7fb3e752011-11-17 17:26:41 +05301852
1853 if (d40c->busy) {
1854 pm_runtime_mark_last_busy(d40c->base->dev);
1855 pm_runtime_put_autosuspend(d40c->base->dev);
1856 }
1857
1858 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001859 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00001860 d40c->configured = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02001861 d40c->base->lookup_phy_chans[phy->num] = NULL;
Narayanan G7fb3e752011-11-17 17:26:41 +05301862out:
Linus Walleij8d318a52010-03-30 15:33:42 +02001863
Narayanan G7fb3e752011-11-17 17:26:41 +05301864 pm_runtime_mark_last_busy(d40c->base->dev);
1865 pm_runtime_put_autosuspend(d40c->base->dev);
1866 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02001867}
1868
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001869static bool d40_is_paused(struct d40_chan *d40c)
1870{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001871 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001872 bool is_paused = false;
1873 unsigned long flags;
1874 void __iomem *active_reg;
1875 u32 status;
1876 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001877
1878 spin_lock_irqsave(&d40c->lock, flags);
1879
Rabin Vincent724a8572011-01-25 11:18:08 +01001880 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001881 if (d40c->phy_chan->num % 2 == 0)
1882 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1883 else
1884 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1885
1886 status = (readl(active_reg) &
1887 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1888 D40_CHAN_POS(d40c->phy_chan->num);
1889 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1890 is_paused = true;
1891
1892 goto _exit;
1893 }
1894
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001895 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001896 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001897 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001898 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001899 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001900 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01001901 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001902 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001903 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001904 goto _exit;
1905 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00001906
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001907 status = (status & D40_EVENTLINE_MASK(event)) >>
1908 D40_EVENTLINE_POS(event);
1909
1910 if (status != D40_DMA_RUN)
1911 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001912_exit:
1913 spin_unlock_irqrestore(&d40c->lock, flags);
1914 return is_paused;
1915
1916}
1917
1918
Linus Walleij8d318a52010-03-30 15:33:42 +02001919static u32 stedma40_residue(struct dma_chan *chan)
1920{
1921 struct d40_chan *d40c =
1922 container_of(chan, struct d40_chan, chan);
1923 u32 bytes_left;
1924 unsigned long flags;
1925
1926 spin_lock_irqsave(&d40c->lock, flags);
1927 bytes_left = d40_residue(d40c);
1928 spin_unlock_irqrestore(&d40c->lock, flags);
1929
1930 return bytes_left;
1931}
1932
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001933static int
1934d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
1935 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001936 unsigned int sg_len, dma_addr_t src_dev_addr,
1937 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001938{
1939 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1940 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1941 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001942 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001943
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001944 ret = d40_log_sg_to_lli(sg_src, sg_len,
1945 src_dev_addr,
1946 desc->lli_log.src,
1947 chan->log_def.lcsp1,
1948 src_info->data_width,
1949 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001950
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001951 ret = d40_log_sg_to_lli(sg_dst, sg_len,
1952 dst_dev_addr,
1953 desc->lli_log.dst,
1954 chan->log_def.lcsp3,
1955 dst_info->data_width,
1956 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001957
Rabin Vincent5ed04b82011-01-25 11:18:26 +01001958 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001959}
1960
1961static int
1962d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
1963 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01001964 unsigned int sg_len, dma_addr_t src_dev_addr,
1965 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001966{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001967 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
1968 struct stedma40_half_channel_info *src_info = &cfg->src_info;
1969 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01001970 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001971 int ret;
1972
Rabin Vincent0c842b52011-01-25 11:18:35 +01001973 if (desc->cyclic)
1974 flags |= LLI_CYCLIC | LLI_TERM_INT;
1975
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001976 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
1977 desc->lli_phy.src,
1978 virt_to_phys(desc->lli_phy.src),
1979 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001980 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001981
1982 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
1983 desc->lli_phy.dst,
1984 virt_to_phys(desc->lli_phy.dst),
1985 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01001986 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01001987
1988 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
1989 desc->lli_pool.size, DMA_TO_DEVICE);
1990
1991 return ret < 0 ? ret : 0;
1992}
1993
1994
Rabin Vincent5f811582011-01-25 11:18:18 +01001995static struct d40_desc *
1996d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
1997 unsigned int sg_len, unsigned long dma_flags)
1998{
1999 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2000 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002001 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002002
2003 desc = d40_desc_get(chan);
2004 if (!desc)
2005 return NULL;
2006
2007 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2008 cfg->dst_info.data_width);
2009 if (desc->lli_len < 0) {
2010 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002011 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002012 }
2013
Rabin Vincentdbd88782011-01-25 11:18:19 +01002014 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2015 if (ret < 0) {
2016 chan_err(chan, "Could not allocate lli\n");
2017 goto err;
2018 }
2019
2020
Rabin Vincent5f811582011-01-25 11:18:18 +01002021 desc->lli_current = 0;
2022 desc->txd.flags = dma_flags;
2023 desc->txd.tx_submit = d40_tx_submit;
2024
2025 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2026
2027 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002028
2029err:
2030 d40_desc_free(chan, desc);
2031 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002032}
2033
Rabin Vincentcade1d32011-01-25 11:18:23 +01002034static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302035d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002036{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002037 struct stedma40_platform_data *plat = chan->base->plat_data;
2038 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002039 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002040
Rabin Vincentcade1d32011-01-25 11:18:23 +01002041 if (chan->runtime_addr)
2042 return chan->runtime_addr;
2043
Vinod Kouldb8196d2011-10-13 22:34:23 +05302044 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002045 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302046 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002047 addr = plat->dev_tx[cfg->dst_dev_type];
2048
2049 return addr;
2050}
2051
2052static struct dma_async_tx_descriptor *
2053d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2054 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302055 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002056{
2057 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002058 dma_addr_t src_dev_addr = 0;
2059 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002060 struct d40_desc *desc;
2061 unsigned long flags;
2062 int ret;
2063
2064 if (!chan->phy_chan) {
2065 chan_err(chan, "Cannot prepare unallocated channel\n");
2066 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002067 }
2068
Rabin Vincent0c842b52011-01-25 11:18:35 +01002069
Rabin Vincentcade1d32011-01-25 11:18:23 +01002070 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002071
Rabin Vincentcade1d32011-01-25 11:18:23 +01002072 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2073 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002074 goto err;
2075
Rabin Vincent0c842b52011-01-25 11:18:35 +01002076 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2077 desc->cyclic = true;
2078
Rabin Vincent822c5672011-01-25 11:18:28 +01002079 if (direction != DMA_NONE) {
2080 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2081
Vinod Kouldb8196d2011-10-13 22:34:23 +05302082 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002083 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302084 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002085 dst_dev_addr = dev_addr;
2086 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002087
2088 if (chan_is_logical(chan))
2089 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002090 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002091 else
2092 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002093 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002094
2095 if (ret) {
2096 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2097 chan_is_logical(chan) ? "log" : "phy", ret);
2098 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002099 }
2100
Per Forlin82babbb362011-08-29 13:33:35 +02002101 /*
2102 * add descriptor to the prepare queue in order to be able
2103 * to free them later in terminate_all
2104 */
2105 list_add_tail(&desc->node, &chan->prepare_queue);
2106
Rabin Vincentcade1d32011-01-25 11:18:23 +01002107 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002108
Rabin Vincentcade1d32011-01-25 11:18:23 +01002109 return &desc->txd;
2110
Linus Walleij8d318a52010-03-30 15:33:42 +02002111err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002112 if (desc)
2113 d40_desc_free(chan, desc);
2114 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002115 return NULL;
2116}
Linus Walleij8d318a52010-03-30 15:33:42 +02002117
2118bool stedma40_filter(struct dma_chan *chan, void *data)
2119{
2120 struct stedma40_chan_cfg *info = data;
2121 struct d40_chan *d40c =
2122 container_of(chan, struct d40_chan, chan);
2123 int err;
2124
2125 if (data) {
2126 err = d40_validate_conf(d40c, info);
2127 if (!err)
2128 d40c->dma_cfg = *info;
2129 } else
2130 err = d40_config_memcpy(d40c);
2131
Rabin Vincentce2ca122010-10-12 13:00:49 +00002132 if (!err)
2133 d40c->configured = true;
2134
Linus Walleij8d318a52010-03-30 15:33:42 +02002135 return err == 0;
2136}
2137EXPORT_SYMBOL(stedma40_filter);
2138
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002139static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2140{
2141 bool realtime = d40c->dma_cfg.realtime;
2142 bool highprio = d40c->dma_cfg.high_priority;
2143 u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
2144 u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
2145 u32 event = D40_TYPE_TO_EVENT(dev_type);
2146 u32 group = D40_TYPE_TO_GROUP(dev_type);
2147 u32 bit = 1 << event;
2148
2149 /* Destination event lines are stored in the upper halfword */
2150 if (!src)
2151 bit <<= 16;
2152
2153 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2154 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2155}
2156
2157static void d40_set_prio_realtime(struct d40_chan *d40c)
2158{
2159 if (d40c->base->rev < 3)
2160 return;
2161
2162 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2163 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2164 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2165
2166 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2167 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2168 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2169}
2170
Linus Walleij8d318a52010-03-30 15:33:42 +02002171/* DMA ENGINE functions */
2172static int d40_alloc_chan_resources(struct dma_chan *chan)
2173{
2174 int err;
2175 unsigned long flags;
2176 struct d40_chan *d40c =
2177 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002178 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002179 spin_lock_irqsave(&d40c->lock, flags);
2180
2181 d40c->completed = chan->cookie = 1;
2182
Rabin Vincentce2ca122010-10-12 13:00:49 +00002183 /* If no dma configuration is set use default configuration (memcpy) */
2184 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002185 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002186 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002187 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002188 goto fail;
2189 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002190 }
2191
Narayanan G5cd326f2011-11-30 19:20:42 +05302192 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002193 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002194 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302195 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002196 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002197 }
2198
Narayanan G7fb3e752011-11-17 17:26:41 +05302199 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002200 /* Fill in basic CFG register values */
2201 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002202 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002203
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002204 d40_set_prio_realtime(d40c);
2205
Rabin Vincent724a8572011-01-25 11:18:08 +01002206 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002207 d40_log_cfg(&d40c->dma_cfg,
2208 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2209
2210 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2211 d40c->lcpa = d40c->base->lcpa_base +
2212 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2213 else
2214 d40c->lcpa = d40c->base->lcpa_base +
2215 d40c->dma_cfg.dst_dev_type *
2216 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2217 }
2218
Narayanan G5cd326f2011-11-30 19:20:42 +05302219 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2220 chan_is_logical(d40c) ? "logical" : "physical",
2221 d40c->phy_chan->num,
2222 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2223
2224
Linus Walleijef1872e2010-06-20 21:24:52 +00002225 /*
2226 * Only write channel configuration to the DMA if the physical
2227 * resource is free. In case of multiple logical channels
2228 * on the same physical resource, only the first write is necessary.
2229 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002230 if (is_free_phy)
2231 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002232fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302233 pm_runtime_mark_last_busy(d40c->base->dev);
2234 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002235 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002236 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002237}
2238
2239static void d40_free_chan_resources(struct dma_chan *chan)
2240{
2241 struct d40_chan *d40c =
2242 container_of(chan, struct d40_chan, chan);
2243 int err;
2244 unsigned long flags;
2245
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002246 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002247 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002248 return;
2249 }
2250
2251
Linus Walleij8d318a52010-03-30 15:33:42 +02002252 spin_lock_irqsave(&d40c->lock, flags);
2253
2254 err = d40_free_dma(d40c);
2255
2256 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002257 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002258 spin_unlock_irqrestore(&d40c->lock, flags);
2259}
2260
2261static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2262 dma_addr_t dst,
2263 dma_addr_t src,
2264 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002265 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002266{
Rabin Vincent95944c62011-01-25 11:18:17 +01002267 struct scatterlist dst_sg;
2268 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002269
Rabin Vincent95944c62011-01-25 11:18:17 +01002270 sg_init_table(&dst_sg, 1);
2271 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002272
Rabin Vincent95944c62011-01-25 11:18:17 +01002273 sg_dma_address(&dst_sg) = dst;
2274 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002275
Rabin Vincent95944c62011-01-25 11:18:17 +01002276 sg_dma_len(&dst_sg) = size;
2277 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002278
Rabin Vincentcade1d32011-01-25 11:18:23 +01002279 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002280}
2281
Ira Snyder0d688662010-09-30 11:46:47 +00002282static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002283d40_prep_memcpy_sg(struct dma_chan *chan,
2284 struct scatterlist *dst_sg, unsigned int dst_nents,
2285 struct scatterlist *src_sg, unsigned int src_nents,
2286 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002287{
2288 if (dst_nents != src_nents)
2289 return NULL;
2290
Rabin Vincentcade1d32011-01-25 11:18:23 +01002291 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002292}
2293
Linus Walleij8d318a52010-03-30 15:33:42 +02002294static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2295 struct scatterlist *sgl,
2296 unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302297 enum dma_transfer_direction direction,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002298 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002299{
Vinod Kouldb8196d2011-10-13 22:34:23 +05302300 if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV)
Rabin Vincent00ac0342011-01-25 11:18:20 +01002301 return NULL;
2302
Rabin Vincentcade1d32011-01-25 11:18:23 +01002303 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002304}
2305
Rabin Vincent0c842b52011-01-25 11:18:35 +01002306static struct dma_async_tx_descriptor *
2307dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2308 size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302309 enum dma_transfer_direction direction)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002310{
2311 unsigned int periods = buf_len / period_len;
2312 struct dma_async_tx_descriptor *txd;
2313 struct scatterlist *sg;
2314 int i;
2315
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002316 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002317 for (i = 0; i < periods; i++) {
2318 sg_dma_address(&sg[i]) = dma_addr;
2319 sg_dma_len(&sg[i]) = period_len;
2320 dma_addr += period_len;
2321 }
2322
2323 sg[periods].offset = 0;
2324 sg[periods].length = 0;
2325 sg[periods].page_link =
2326 ((unsigned long)sg | 0x01) & ~0x02;
2327
2328 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2329 DMA_PREP_INTERRUPT);
2330
2331 kfree(sg);
2332
2333 return txd;
2334}
2335
Linus Walleij8d318a52010-03-30 15:33:42 +02002336static enum dma_status d40_tx_status(struct dma_chan *chan,
2337 dma_cookie_t cookie,
2338 struct dma_tx_state *txstate)
2339{
2340 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2341 dma_cookie_t last_used;
2342 dma_cookie_t last_complete;
2343 int ret;
2344
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002345 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002346 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002347 return -EINVAL;
2348 }
2349
Linus Walleij8d318a52010-03-30 15:33:42 +02002350 last_complete = d40c->completed;
2351 last_used = chan->cookie;
2352
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002353 if (d40_is_paused(d40c))
2354 ret = DMA_PAUSED;
2355 else
2356 ret = dma_async_is_complete(cookie, last_complete, last_used);
Linus Walleij8d318a52010-03-30 15:33:42 +02002357
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002358 dma_set_tx_state(txstate, last_complete, last_used,
2359 stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002360
2361 return ret;
2362}
2363
2364static void d40_issue_pending(struct dma_chan *chan)
2365{
2366 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2367 unsigned long flags;
2368
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002369 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002370 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002371 return;
2372 }
2373
Linus Walleij8d318a52010-03-30 15:33:42 +02002374 spin_lock_irqsave(&d40c->lock, flags);
2375
Per Forlina8f30672011-06-26 23:29:52 +02002376 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2377
2378 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002379 if (!d40c->busy)
2380 (void) d40_queue_start(d40c);
2381
2382 spin_unlock_irqrestore(&d40c->lock, flags);
2383}
2384
Rabin Vincent98ca5282011-06-27 11:33:38 +02002385static int
2386dma40_config_to_halfchannel(struct d40_chan *d40c,
2387 struct stedma40_half_channel_info *info,
2388 enum dma_slave_buswidth width,
2389 u32 maxburst)
2390{
2391 enum stedma40_periph_data_width addr_width;
2392 int psize;
2393
2394 switch (width) {
2395 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2396 addr_width = STEDMA40_BYTE_WIDTH;
2397 break;
2398 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2399 addr_width = STEDMA40_HALFWORD_WIDTH;
2400 break;
2401 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2402 addr_width = STEDMA40_WORD_WIDTH;
2403 break;
2404 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2405 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2406 break;
2407 default:
2408 dev_err(d40c->base->dev,
2409 "illegal peripheral address width "
2410 "requested (%d)\n",
2411 width);
2412 return -EINVAL;
2413 }
2414
2415 if (chan_is_logical(d40c)) {
2416 if (maxburst >= 16)
2417 psize = STEDMA40_PSIZE_LOG_16;
2418 else if (maxburst >= 8)
2419 psize = STEDMA40_PSIZE_LOG_8;
2420 else if (maxburst >= 4)
2421 psize = STEDMA40_PSIZE_LOG_4;
2422 else
2423 psize = STEDMA40_PSIZE_LOG_1;
2424 } else {
2425 if (maxburst >= 16)
2426 psize = STEDMA40_PSIZE_PHY_16;
2427 else if (maxburst >= 8)
2428 psize = STEDMA40_PSIZE_PHY_8;
2429 else if (maxburst >= 4)
2430 psize = STEDMA40_PSIZE_PHY_4;
2431 else
2432 psize = STEDMA40_PSIZE_PHY_1;
2433 }
2434
2435 info->data_width = addr_width;
2436 info->psize = psize;
2437 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2438
2439 return 0;
2440}
2441
Linus Walleij95e14002010-08-04 13:37:45 +02002442/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002443static int d40_set_runtime_config(struct dma_chan *chan,
2444 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002445{
2446 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2447 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002448 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002449 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002450 u32 src_maxburst, dst_maxburst;
2451 int ret;
2452
2453 src_addr_width = config->src_addr_width;
2454 src_maxburst = config->src_maxburst;
2455 dst_addr_width = config->dst_addr_width;
2456 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002457
Vinod Kouldb8196d2011-10-13 22:34:23 +05302458 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002459 dma_addr_t dev_addr_rx =
2460 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2461
2462 config_addr = config->src_addr;
2463 if (dev_addr_rx)
2464 dev_dbg(d40c->base->dev,
2465 "channel has a pre-wired RX address %08x "
2466 "overriding with %08x\n",
2467 dev_addr_rx, config_addr);
2468 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2469 dev_dbg(d40c->base->dev,
2470 "channel was not configured for peripheral "
2471 "to memory transfer (%d) overriding\n",
2472 cfg->dir);
2473 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2474
Rabin Vincent98ca5282011-06-27 11:33:38 +02002475 /* Configure the memory side */
2476 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2477 dst_addr_width = src_addr_width;
2478 if (dst_maxburst == 0)
2479 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002480
Vinod Kouldb8196d2011-10-13 22:34:23 +05302481 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002482 dma_addr_t dev_addr_tx =
2483 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2484
2485 config_addr = config->dst_addr;
2486 if (dev_addr_tx)
2487 dev_dbg(d40c->base->dev,
2488 "channel has a pre-wired TX address %08x "
2489 "overriding with %08x\n",
2490 dev_addr_tx, config_addr);
2491 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2492 dev_dbg(d40c->base->dev,
2493 "channel was not configured for memory "
2494 "to peripheral transfer (%d) overriding\n",
2495 cfg->dir);
2496 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2497
Rabin Vincent98ca5282011-06-27 11:33:38 +02002498 /* Configure the memory side */
2499 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2500 src_addr_width = dst_addr_width;
2501 if (src_maxburst == 0)
2502 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002503 } else {
2504 dev_err(d40c->base->dev,
2505 "unrecognized channel direction %d\n",
2506 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002507 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002508 }
2509
Rabin Vincent98ca5282011-06-27 11:33:38 +02002510 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002511 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002512 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2513 src_maxburst,
2514 src_addr_width,
2515 dst_maxburst,
2516 dst_addr_width);
2517 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002518 }
2519
Rabin Vincent98ca5282011-06-27 11:33:38 +02002520 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2521 src_addr_width,
2522 src_maxburst);
2523 if (ret)
2524 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002525
Rabin Vincent98ca5282011-06-27 11:33:38 +02002526 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2527 dst_addr_width,
2528 dst_maxburst);
2529 if (ret)
2530 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002531
Per Forlina59670a2010-10-06 09:05:27 +00002532 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002533 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002534 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2535 else
2536 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2537 &d40c->dst_def_cfg, false);
2538
Linus Walleij95e14002010-08-04 13:37:45 +02002539 /* These settings will take precedence later */
2540 d40c->runtime_addr = config_addr;
2541 d40c->runtime_direction = config->direction;
2542 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002543 "configured channel %s for %s, data width %d/%d, "
2544 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002545 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302546 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002547 src_addr_width, dst_addr_width,
2548 src_maxburst, dst_maxburst);
2549
2550 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002551}
2552
Linus Walleij05827632010-05-17 16:30:42 -07002553static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2554 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002555{
Linus Walleij8d318a52010-03-30 15:33:42 +02002556 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2557
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002558 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002559 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002560 return -EINVAL;
2561 }
2562
Linus Walleij8d318a52010-03-30 15:33:42 +02002563 switch (cmd) {
2564 case DMA_TERMINATE_ALL:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002565 return d40_terminate_all(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002566 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002567 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002568 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002569 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002570 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002571 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002572 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002573 default:
2574 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002575 }
2576
2577 /* Other commands are unimplemented */
2578 return -ENXIO;
2579}
2580
2581/* Initialization functions */
2582
2583static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2584 struct d40_chan *chans, int offset,
2585 int num_chans)
2586{
2587 int i = 0;
2588 struct d40_chan *d40c;
2589
2590 INIT_LIST_HEAD(&dma->channels);
2591
2592 for (i = offset; i < offset + num_chans; i++) {
2593 d40c = &chans[i];
2594 d40c->base = base;
2595 d40c->chan.device = dma;
2596
Linus Walleij8d318a52010-03-30 15:33:42 +02002597 spin_lock_init(&d40c->lock);
2598
2599 d40c->log_num = D40_PHY_CHAN;
2600
Linus Walleij8d318a52010-03-30 15:33:42 +02002601 INIT_LIST_HEAD(&d40c->active);
2602 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002603 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002604 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002605 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002606
Linus Walleij8d318a52010-03-30 15:33:42 +02002607 tasklet_init(&d40c->tasklet, dma_tasklet,
2608 (unsigned long) d40c);
2609
2610 list_add_tail(&d40c->chan.device_node,
2611 &dma->channels);
2612 }
2613}
2614
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002615static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2616{
2617 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2618 dev->device_prep_slave_sg = d40_prep_slave_sg;
2619
2620 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2621 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2622
2623 /*
2624 * This controller can only access address at even
2625 * 32bit boundaries, i.e. 2^2
2626 */
2627 dev->copy_align = 2;
2628 }
2629
2630 if (dma_has_cap(DMA_SG, dev->cap_mask))
2631 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2632
Rabin Vincent0c842b52011-01-25 11:18:35 +01002633 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2634 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2635
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002636 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2637 dev->device_free_chan_resources = d40_free_chan_resources;
2638 dev->device_issue_pending = d40_issue_pending;
2639 dev->device_tx_status = d40_tx_status;
2640 dev->device_control = d40_control;
2641 dev->dev = base->dev;
2642}
2643
Linus Walleij8d318a52010-03-30 15:33:42 +02002644static int __init d40_dmaengine_init(struct d40_base *base,
2645 int num_reserved_chans)
2646{
2647 int err ;
2648
2649 d40_chan_init(base, &base->dma_slave, base->log_chans,
2650 0, base->num_log_chans);
2651
2652 dma_cap_zero(base->dma_slave.cap_mask);
2653 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002654 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002655
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002656 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002657
2658 err = dma_async_device_register(&base->dma_slave);
2659
2660 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002661 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002662 goto failure1;
2663 }
2664
2665 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2666 base->num_log_chans, base->plat_data->memcpy_len);
2667
2668 dma_cap_zero(base->dma_memcpy.cap_mask);
2669 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002670 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002671
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002672 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002673
2674 err = dma_async_device_register(&base->dma_memcpy);
2675
2676 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002677 d40_err(base->dev,
2678 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002679 goto failure2;
2680 }
2681
2682 d40_chan_init(base, &base->dma_both, base->phy_chans,
2683 0, num_reserved_chans);
2684
2685 dma_cap_zero(base->dma_both.cap_mask);
2686 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2687 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002688 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002689 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002690
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002691 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002692 err = dma_async_device_register(&base->dma_both);
2693
2694 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002695 d40_err(base->dev,
2696 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002697 goto failure3;
2698 }
2699 return 0;
2700failure3:
2701 dma_async_device_unregister(&base->dma_memcpy);
2702failure2:
2703 dma_async_device_unregister(&base->dma_slave);
2704failure1:
2705 return err;
2706}
2707
Narayanan G7fb3e752011-11-17 17:26:41 +05302708/* Suspend resume functionality */
2709#ifdef CONFIG_PM
2710static int dma40_pm_suspend(struct device *dev)
2711{
Narayanan G28c7a192011-11-22 13:56:55 +05302712 struct platform_device *pdev = to_platform_device(dev);
2713 struct d40_base *base = platform_get_drvdata(pdev);
2714 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302715 if (!pm_runtime_suspended(dev))
2716 return -EBUSY;
2717
Narayanan G28c7a192011-11-22 13:56:55 +05302718 if (base->lcpa_regulator)
2719 ret = regulator_disable(base->lcpa_regulator);
2720 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302721}
2722
2723static int dma40_runtime_suspend(struct device *dev)
2724{
2725 struct platform_device *pdev = to_platform_device(dev);
2726 struct d40_base *base = platform_get_drvdata(pdev);
2727
2728 d40_save_restore_registers(base, true);
2729
2730 /* Don't disable/enable clocks for v1 due to HW bugs */
2731 if (base->rev != 1)
2732 writel_relaxed(base->gcc_pwr_off_mask,
2733 base->virtbase + D40_DREG_GCC);
2734
2735 return 0;
2736}
2737
2738static int dma40_runtime_resume(struct device *dev)
2739{
2740 struct platform_device *pdev = to_platform_device(dev);
2741 struct d40_base *base = platform_get_drvdata(pdev);
2742
2743 if (base->initialized)
2744 d40_save_restore_registers(base, false);
2745
2746 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
2747 base->virtbase + D40_DREG_GCC);
2748 return 0;
2749}
2750
Narayanan G28c7a192011-11-22 13:56:55 +05302751static int dma40_resume(struct device *dev)
2752{
2753 struct platform_device *pdev = to_platform_device(dev);
2754 struct d40_base *base = platform_get_drvdata(pdev);
2755 int ret = 0;
2756
2757 if (base->lcpa_regulator)
2758 ret = regulator_enable(base->lcpa_regulator);
2759
2760 return ret;
2761}
Narayanan G7fb3e752011-11-17 17:26:41 +05302762
2763static const struct dev_pm_ops dma40_pm_ops = {
2764 .suspend = dma40_pm_suspend,
2765 .runtime_suspend = dma40_runtime_suspend,
2766 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05302767 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05302768};
2769#define DMA40_PM_OPS (&dma40_pm_ops)
2770#else
2771#define DMA40_PM_OPS NULL
2772#endif
2773
Linus Walleij8d318a52010-03-30 15:33:42 +02002774/* Initialization functions. */
2775
2776static int __init d40_phy_res_init(struct d40_base *base)
2777{
2778 int i;
2779 int num_phy_chans_avail = 0;
2780 u32 val[2];
2781 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05302782 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02002783
2784 val[0] = readl(base->virtbase + D40_DREG_PRSME);
2785 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
2786
2787 for (i = 0; i < base->num_phy_chans; i++) {
2788 base->phy_res[i].num = i;
2789 odd_even_bit += 2 * ((i % 2) == 0);
2790 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
2791 /* Mark security only channels as occupied */
2792 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
2793 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05302794 base->phy_res[i].reserved = true;
2795 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
2796 D40_DREG_GCC_SRC);
2797 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
2798 D40_DREG_GCC_DST);
2799
2800
Linus Walleij8d318a52010-03-30 15:33:42 +02002801 } else {
2802 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
2803 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05302804 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002805 num_phy_chans_avail++;
2806 }
2807 spin_lock_init(&base->phy_res[i].lock);
2808 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002809
2810 /* Mark disabled channels as occupied */
2811 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00002812 int chan = base->plat_data->disabled_channels[i];
2813
2814 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2815 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05302816 base->phy_res[chan].reserved = true;
2817 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
2818 D40_DREG_GCC_SRC);
2819 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
2820 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00002821 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00002822 }
2823
Linus Walleij8d318a52010-03-30 15:33:42 +02002824 dev_info(base->dev, "%d of %d physical DMA channels available\n",
2825 num_phy_chans_avail, base->num_phy_chans);
2826
2827 /* Verify settings extended vs standard */
2828 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
2829
2830 for (i = 0; i < base->num_phy_chans; i++) {
2831
2832 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
2833 (val[0] & 0x3) != 1)
2834 dev_info(base->dev,
2835 "[%s] INFO: channel %d is misconfigured (%d)\n",
2836 __func__, i, val[0] & 0x3);
2837
2838 val[0] = val[0] >> 2;
2839 }
2840
Narayanan G7fb3e752011-11-17 17:26:41 +05302841 /*
2842 * To keep things simple, Enable all clocks initially.
2843 * The clocks will get managed later post channel allocation.
2844 * The clocks for the event lines on which reserved channels exists
2845 * are not managed here.
2846 */
2847 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
2848 base->gcc_pwr_off_mask = gcc;
2849
Linus Walleij8d318a52010-03-30 15:33:42 +02002850 return num_phy_chans_avail;
2851}
2852
2853static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
2854{
Linus Walleij8d318a52010-03-30 15:33:42 +02002855 struct stedma40_platform_data *plat_data;
2856 struct clk *clk = NULL;
2857 void __iomem *virtbase = NULL;
2858 struct resource *res = NULL;
2859 struct d40_base *base = NULL;
2860 int num_log_chans = 0;
2861 int num_phy_chans;
2862 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02002863 u32 pid;
2864 u32 cid;
2865 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002866
2867 clk = clk_get(&pdev->dev, NULL);
2868
2869 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002870 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002871 goto failure;
2872 }
2873
2874 clk_enable(clk);
2875
2876 /* Get IO for DMAC base address */
2877 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
2878 if (!res)
2879 goto failure;
2880
2881 if (request_mem_region(res->start, resource_size(res),
2882 D40_NAME " I/O base") == NULL)
2883 goto failure;
2884
2885 virtbase = ioremap(res->start, resource_size(res));
2886 if (!virtbase)
2887 goto failure;
2888
Linus Walleijf4b89762011-06-27 11:33:46 +02002889 /* This is just a regular AMBA PrimeCell ID actually */
2890 for (pid = 0, i = 0; i < 4; i++)
2891 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
2892 & 255) << (i * 8);
2893 for (cid = 0, i = 0; i < 4; i++)
2894 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
2895 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02002896
Linus Walleijf4b89762011-06-27 11:33:46 +02002897 if (cid != AMBA_CID) {
2898 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002899 goto failure;
2900 }
Linus Walleijf4b89762011-06-27 11:33:46 +02002901 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
2902 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2903 AMBA_MANF_BITS(pid),
2904 AMBA_VENDOR_ST);
2905 goto failure;
2906 }
2907 /*
2908 * HW revision:
2909 * DB8500ed has revision 0
2910 * ? has revision 1
2911 * DB8500v1 has revision 2
2912 * DB8500v2 has revision 3
2913 */
2914 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002915
Linus Walleij8d318a52010-03-30 15:33:42 +02002916 /* The number of physical channels on this HW */
2917 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
2918
2919 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002920 rev, res->start);
Linus Walleij8d318a52010-03-30 15:33:42 +02002921
2922 plat_data = pdev->dev.platform_data;
2923
2924 /* Count the number of logical channels in use */
2925 for (i = 0; i < plat_data->dev_len; i++)
2926 if (plat_data->dev_rx[i] != 0)
2927 num_log_chans++;
2928
2929 for (i = 0; i < plat_data->dev_len; i++)
2930 if (plat_data->dev_tx[i] != 0)
2931 num_log_chans++;
2932
2933 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
2934 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
2935 sizeof(struct d40_chan), GFP_KERNEL);
2936
2937 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002938 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002939 goto failure;
2940 }
2941
Jonas Aaberg3ae02672010-08-09 12:08:18 +00002942 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02002943 base->clk = clk;
2944 base->num_phy_chans = num_phy_chans;
2945 base->num_log_chans = num_log_chans;
2946 base->phy_start = res->start;
2947 base->phy_size = resource_size(res);
2948 base->virtbase = virtbase;
2949 base->plat_data = plat_data;
2950 base->dev = &pdev->dev;
2951 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
2952 base->log_chans = &base->phy_chans[num_phy_chans];
2953
2954 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
2955 GFP_KERNEL);
2956 if (!base->phy_res)
2957 goto failure;
2958
2959 base->lookup_phy_chans = kzalloc(num_phy_chans *
2960 sizeof(struct d40_chan *),
2961 GFP_KERNEL);
2962 if (!base->lookup_phy_chans)
2963 goto failure;
2964
2965 if (num_log_chans + plat_data->memcpy_len) {
2966 /*
2967 * The max number of logical channels are event lines for all
2968 * src devices and dst devices
2969 */
2970 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
2971 sizeof(struct d40_chan *),
2972 GFP_KERNEL);
2973 if (!base->lookup_log_chans)
2974 goto failure;
2975 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00002976
Narayanan G7fb3e752011-11-17 17:26:41 +05302977 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
2978 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02002979 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05302980 if (!base->reg_val_backup_chan)
2981 goto failure;
2982
2983 base->lcla_pool.alloc_map =
2984 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
2985 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02002986 if (!base->lcla_pool.alloc_map)
2987 goto failure;
2988
Jonas Aabergc675b1b2010-06-20 21:25:08 +00002989 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
2990 0, SLAB_HWCACHE_ALIGN,
2991 NULL);
2992 if (base->desc_slab == NULL)
2993 goto failure;
2994
Linus Walleij8d318a52010-03-30 15:33:42 +02002995 return base;
2996
2997failure:
Rabin Vincentc6134c92010-10-06 08:20:36 +00002998 if (!IS_ERR(clk)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002999 clk_disable(clk);
3000 clk_put(clk);
3001 }
3002 if (virtbase)
3003 iounmap(virtbase);
3004 if (res)
3005 release_mem_region(res->start,
3006 resource_size(res));
3007 if (virtbase)
3008 iounmap(virtbase);
3009
3010 if (base) {
3011 kfree(base->lcla_pool.alloc_map);
3012 kfree(base->lookup_log_chans);
3013 kfree(base->lookup_phy_chans);
3014 kfree(base->phy_res);
3015 kfree(base);
3016 }
3017
3018 return NULL;
3019}
3020
3021static void __init d40_hw_init(struct d40_base *base)
3022{
3023
Narayanan G7fb3e752011-11-17 17:26:41 +05303024 static struct d40_reg_val dma_init_reg[] = {
Linus Walleij8d318a52010-03-30 15:33:42 +02003025 /* Clock every part of the DMA block from start */
Narayanan G7fb3e752011-11-17 17:26:41 +05303026 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
Linus Walleij8d318a52010-03-30 15:33:42 +02003027
3028 /* Interrupts on all logical channels */
3029 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
3030 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
3031 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
3032 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
3033 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
3034 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
3035 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
3036 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
3037 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
3038 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
3039 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
3040 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
3041 };
3042 int i;
3043 u32 prmseo[2] = {0, 0};
3044 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3045 u32 pcmis = 0;
3046 u32 pcicr = 0;
3047
3048 for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
3049 writel(dma_init_reg[i].val,
3050 base->virtbase + dma_init_reg[i].reg);
3051
3052 /* Configure all our dma channels to default settings */
3053 for (i = 0; i < base->num_phy_chans; i++) {
3054
3055 activeo[i % 2] = activeo[i % 2] << 2;
3056
3057 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3058 == D40_ALLOC_PHY) {
3059 activeo[i % 2] |= 3;
3060 continue;
3061 }
3062
3063 /* Enable interrupt # */
3064 pcmis = (pcmis << 1) | 1;
3065
3066 /* Clear interrupt # */
3067 pcicr = (pcicr << 1) | 1;
3068
3069 /* Set channel to physical mode */
3070 prmseo[i % 2] = prmseo[i % 2] << 2;
3071 prmseo[i % 2] |= 1;
3072
3073 }
3074
3075 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3076 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3077 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3078 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3079
3080 /* Write which interrupt to enable */
3081 writel(pcmis, base->virtbase + D40_DREG_PCMIS);
3082
3083 /* Write which interrupt to clear */
3084 writel(pcicr, base->virtbase + D40_DREG_PCICR);
3085
3086}
3087
Linus Walleij508849a2010-06-20 21:26:07 +00003088static int __init d40_lcla_allocate(struct d40_base *base)
3089{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003090 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003091 unsigned long *page_list;
3092 int i, j;
3093 int ret = 0;
3094
3095 /*
3096 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3097 * To full fill this hardware requirement without wasting 256 kb
3098 * we allocate pages until we get an aligned one.
3099 */
3100 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3101 GFP_KERNEL);
3102
3103 if (!page_list) {
3104 ret = -ENOMEM;
3105 goto failure;
3106 }
3107
3108 /* Calculating how many pages that are required */
3109 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3110
3111 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3112 page_list[i] = __get_free_pages(GFP_KERNEL,
3113 base->lcla_pool.pages);
3114 if (!page_list[i]) {
3115
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003116 d40_err(base->dev, "Failed to allocate %d pages.\n",
3117 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003118
3119 for (j = 0; j < i; j++)
3120 free_pages(page_list[j], base->lcla_pool.pages);
3121 goto failure;
3122 }
3123
3124 if ((virt_to_phys((void *)page_list[i]) &
3125 (LCLA_ALIGNMENT - 1)) == 0)
3126 break;
3127 }
3128
3129 for (j = 0; j < i; j++)
3130 free_pages(page_list[j], base->lcla_pool.pages);
3131
3132 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3133 base->lcla_pool.base = (void *)page_list[i];
3134 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003135 /*
3136 * After many attempts and no succees with finding the correct
3137 * alignment, try with allocating a big buffer.
3138 */
Linus Walleij508849a2010-06-20 21:26:07 +00003139 dev_warn(base->dev,
3140 "[%s] Failed to get %d pages @ 18 bit align.\n",
3141 __func__, base->lcla_pool.pages);
3142 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3143 base->num_phy_chans +
3144 LCLA_ALIGNMENT,
3145 GFP_KERNEL);
3146 if (!base->lcla_pool.base_unaligned) {
3147 ret = -ENOMEM;
3148 goto failure;
3149 }
3150
3151 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3152 LCLA_ALIGNMENT);
3153 }
3154
Rabin Vincent026cbc42011-01-25 11:18:14 +01003155 pool->dma_addr = dma_map_single(base->dev, pool->base,
3156 SZ_1K * base->num_phy_chans,
3157 DMA_TO_DEVICE);
3158 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3159 pool->dma_addr = 0;
3160 ret = -ENOMEM;
3161 goto failure;
3162 }
3163
Linus Walleij508849a2010-06-20 21:26:07 +00003164 writel(virt_to_phys(base->lcla_pool.base),
3165 base->virtbase + D40_DREG_LCLA);
3166failure:
3167 kfree(page_list);
3168 return ret;
3169}
3170
Linus Walleij8d318a52010-03-30 15:33:42 +02003171static int __init d40_probe(struct platform_device *pdev)
3172{
3173 int err;
3174 int ret = -ENOENT;
3175 struct d40_base *base;
3176 struct resource *res = NULL;
3177 int num_reserved_chans;
3178 u32 val;
3179
3180 base = d40_hw_detect_init(pdev);
3181
3182 if (!base)
3183 goto failure;
3184
3185 num_reserved_chans = d40_phy_res_init(base);
3186
3187 platform_set_drvdata(pdev, base);
3188
3189 spin_lock_init(&base->interrupt_lock);
3190 spin_lock_init(&base->execmd_lock);
3191
3192 /* Get IO for logical channel parameter address */
3193 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3194 if (!res) {
3195 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003196 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003197 goto failure;
3198 }
3199 base->lcpa_size = resource_size(res);
3200 base->phy_lcpa = res->start;
3201
3202 if (request_mem_region(res->start, resource_size(res),
3203 D40_NAME " I/O lcpa") == NULL) {
3204 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003205 d40_err(&pdev->dev,
3206 "Failed to request LCPA region 0x%x-0x%x\n",
3207 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003208 goto failure;
3209 }
3210
3211 /* We make use of ESRAM memory for this. */
3212 val = readl(base->virtbase + D40_DREG_LCPA);
3213 if (res->start != val && val != 0) {
3214 dev_warn(&pdev->dev,
3215 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3216 __func__, val, res->start);
3217 } else
3218 writel(res->start, base->virtbase + D40_DREG_LCPA);
3219
3220 base->lcpa_base = ioremap(res->start, resource_size(res));
3221 if (!base->lcpa_base) {
3222 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003223 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003224 goto failure;
3225 }
Narayanan G28c7a192011-11-22 13:56:55 +05303226 /* If lcla has to be located in ESRAM we don't need to allocate */
3227 if (base->plat_data->use_esram_lcla) {
3228 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3229 "lcla_esram");
3230 if (!res) {
3231 ret = -ENOENT;
3232 d40_err(&pdev->dev,
3233 "No \"lcla_esram\" memory resource\n");
3234 goto failure;
3235 }
3236 base->lcla_pool.base = ioremap(res->start,
3237 resource_size(res));
3238 if (!base->lcla_pool.base) {
3239 ret = -ENOMEM;
3240 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3241 goto failure;
3242 }
3243 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003244
Narayanan G28c7a192011-11-22 13:56:55 +05303245 } else {
3246 ret = d40_lcla_allocate(base);
3247 if (ret) {
3248 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3249 goto failure;
3250 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003251 }
3252
Linus Walleij8d318a52010-03-30 15:33:42 +02003253 spin_lock_init(&base->lcla_pool.lock);
3254
Linus Walleij8d318a52010-03-30 15:33:42 +02003255 base->irq = platform_get_irq(pdev, 0);
3256
3257 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003258 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003259 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003260 goto failure;
3261 }
3262
Narayanan G7fb3e752011-11-17 17:26:41 +05303263 pm_runtime_irq_safe(base->dev);
3264 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3265 pm_runtime_use_autosuspend(base->dev);
3266 pm_runtime_enable(base->dev);
3267 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303268
3269 if (base->plat_data->use_esram_lcla) {
3270
3271 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3272 if (IS_ERR(base->lcpa_regulator)) {
3273 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3274 base->lcpa_regulator = NULL;
3275 goto failure;
3276 }
3277
3278 ret = regulator_enable(base->lcpa_regulator);
3279 if (ret) {
3280 d40_err(&pdev->dev,
3281 "Failed to enable lcpa_regulator\n");
3282 regulator_put(base->lcpa_regulator);
3283 base->lcpa_regulator = NULL;
3284 goto failure;
3285 }
3286 }
3287
Narayanan G7fb3e752011-11-17 17:26:41 +05303288 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003289 err = d40_dmaengine_init(base, num_reserved_chans);
3290 if (err)
3291 goto failure;
3292
3293 d40_hw_init(base);
3294
3295 dev_info(base->dev, "initialized\n");
3296 return 0;
3297
3298failure:
3299 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003300 if (base->desc_slab)
3301 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003302 if (base->virtbase)
3303 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003304
Narayanan G28c7a192011-11-22 13:56:55 +05303305 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3306 iounmap(base->lcla_pool.base);
3307 base->lcla_pool.base = NULL;
3308 }
3309
Rabin Vincent026cbc42011-01-25 11:18:14 +01003310 if (base->lcla_pool.dma_addr)
3311 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3312 SZ_1K * base->num_phy_chans,
3313 DMA_TO_DEVICE);
3314
Linus Walleij508849a2010-06-20 21:26:07 +00003315 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3316 free_pages((unsigned long)base->lcla_pool.base,
3317 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003318
3319 kfree(base->lcla_pool.base_unaligned);
3320
Linus Walleij8d318a52010-03-30 15:33:42 +02003321 if (base->phy_lcpa)
3322 release_mem_region(base->phy_lcpa,
3323 base->lcpa_size);
3324 if (base->phy_start)
3325 release_mem_region(base->phy_start,
3326 base->phy_size);
3327 if (base->clk) {
3328 clk_disable(base->clk);
3329 clk_put(base->clk);
3330 }
3331
Narayanan G28c7a192011-11-22 13:56:55 +05303332 if (base->lcpa_regulator) {
3333 regulator_disable(base->lcpa_regulator);
3334 regulator_put(base->lcpa_regulator);
3335 }
3336
Linus Walleij8d318a52010-03-30 15:33:42 +02003337 kfree(base->lcla_pool.alloc_map);
3338 kfree(base->lookup_log_chans);
3339 kfree(base->lookup_phy_chans);
3340 kfree(base->phy_res);
3341 kfree(base);
3342 }
3343
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003344 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003345 return ret;
3346}
3347
3348static struct platform_driver d40_driver = {
3349 .driver = {
3350 .owner = THIS_MODULE,
3351 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303352 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003353 },
3354};
3355
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003356static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003357{
3358 return platform_driver_probe(&d40_driver, d40_probe);
3359}
Linus Walleija0eb2212011-05-18 14:18:57 +02003360subsys_initcall(stedma40_init);