blob: 2ecefb7113b91f6d7536503beb5279cdac900c90 [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 Walleij15e4b782012-04-12 18:12:43 +020021#include <linux/regulator/consumer.h>
Linus Walleij865fab62012-10-18 14:20:16 +020022#include <linux/platform_data/dma-ste-dma40.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020023
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000024#include "dmaengine.h"
Linus Walleij8d318a52010-03-30 15:33:42 +020025#include "ste_dma40_ll.h"
26
27#define D40_NAME "dma40"
28
29#define D40_PHY_CHAN -1
30
31/* For masking out/in 2 bit channel positions */
32#define D40_CHAN_POS(chan) (2 * (chan / 2))
33#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
34
35/* Maximum iterations taken before giving up suspending a channel */
36#define D40_SUSPEND_MAX_IT 500
37
Narayanan G7fb3e752011-11-17 17:26:41 +053038/* Milliseconds */
39#define DMA40_AUTOSUSPEND_DELAY 100
40
Linus Walleij508849a2010-06-20 21:26:07 +000041/* Hardware requirement on LCLA alignment */
42#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000043
44/* Max number of links per event group */
45#define D40_LCLA_LINK_PER_EVENT_GRP 128
46#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
47
Linus Walleij508849a2010-06-20 21:26:07 +000048/* Attempts before giving up to trying to get pages that are aligned */
49#define MAX_LCLA_ALLOC_ATTEMPTS 256
50
51/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020052#define D40_ALLOC_FREE (1 << 31)
53#define D40_ALLOC_PHY (1 << 30)
54#define D40_ALLOC_LOG_FREE 0
55
Tong Liu3cb645d2012-09-26 10:07:30 +000056#define MAX(a, b) (((a) < (b)) ? (b) : (a))
57
Linus Walleij8d318a52010-03-30 15:33:42 +020058/**
59 * enum 40_command - The different commands and/or statuses.
60 *
61 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
62 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
63 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
64 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
65 */
66enum d40_command {
67 D40_DMA_STOP = 0,
68 D40_DMA_RUN = 1,
69 D40_DMA_SUSPEND_REQ = 2,
70 D40_DMA_SUSPENDED = 3
71};
72
Narayanan G7fb3e752011-11-17 17:26:41 +053073/*
Narayanan G1bdae6f2012-02-09 12:41:37 +053074 * enum d40_events - The different Event Enables for the event lines.
75 *
76 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
77 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
78 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
79 * @D40_ROUND_EVENTLINE: Status check for event line.
80 */
81
82enum d40_events {
83 D40_DEACTIVATE_EVENTLINE = 0,
84 D40_ACTIVATE_EVENTLINE = 1,
85 D40_SUSPEND_REQ_EVENTLINE = 2,
86 D40_ROUND_EVENTLINE = 3
87};
88
89/*
Narayanan G7fb3e752011-11-17 17:26:41 +053090 * These are the registers that has to be saved and later restored
91 * when the DMA hw is powered off.
92 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
93 */
94static u32 d40_backup_regs[] = {
95 D40_DREG_LCPA,
96 D40_DREG_LCLA,
97 D40_DREG_PRMSE,
98 D40_DREG_PRMSO,
99 D40_DREG_PRMOE,
100 D40_DREG_PRMOO,
101};
102
103#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
104
Tong Liu3cb645d2012-09-26 10:07:30 +0000105/*
106 * since 9540 and 8540 has the same HW revision
107 * use v4a for 9540 or ealier
108 * use v4b for 8540 or later
109 * HW revision:
110 * DB8500ed has revision 0
111 * DB8500v1 has revision 2
112 * DB8500v2 has revision 3
113 * AP9540v1 has revision 4
114 * DB8540v1 has revision 4
115 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
116 */
117static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530118 D40_DREG_PSEG1,
119 D40_DREG_PSEG2,
120 D40_DREG_PSEG3,
121 D40_DREG_PSEG4,
122 D40_DREG_PCEG1,
123 D40_DREG_PCEG2,
124 D40_DREG_PCEG3,
125 D40_DREG_PCEG4,
126 D40_DREG_RSEG1,
127 D40_DREG_RSEG2,
128 D40_DREG_RSEG3,
129 D40_DREG_RSEG4,
130 D40_DREG_RCEG1,
131 D40_DREG_RCEG2,
132 D40_DREG_RCEG3,
133 D40_DREG_RCEG4,
134};
135
Tong Liu3cb645d2012-09-26 10:07:30 +0000136#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
137
138static u32 d40_backup_regs_v4b[] = {
139 D40_DREG_CPSEG1,
140 D40_DREG_CPSEG2,
141 D40_DREG_CPSEG3,
142 D40_DREG_CPSEG4,
143 D40_DREG_CPSEG5,
144 D40_DREG_CPCEG1,
145 D40_DREG_CPCEG2,
146 D40_DREG_CPCEG3,
147 D40_DREG_CPCEG4,
148 D40_DREG_CPCEG5,
149 D40_DREG_CRSEG1,
150 D40_DREG_CRSEG2,
151 D40_DREG_CRSEG3,
152 D40_DREG_CRSEG4,
153 D40_DREG_CRSEG5,
154 D40_DREG_CRCEG1,
155 D40_DREG_CRCEG2,
156 D40_DREG_CRCEG3,
157 D40_DREG_CRCEG4,
158 D40_DREG_CRCEG5,
159};
160
161#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530162
163static u32 d40_backup_regs_chan[] = {
164 D40_CHAN_REG_SSCFG,
165 D40_CHAN_REG_SSELT,
166 D40_CHAN_REG_SSPTR,
167 D40_CHAN_REG_SSLNK,
168 D40_CHAN_REG_SDCFG,
169 D40_CHAN_REG_SDELT,
170 D40_CHAN_REG_SDPTR,
171 D40_CHAN_REG_SDLNK,
172};
173
Linus Walleij8d318a52010-03-30 15:33:42 +0200174/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000175 * struct d40_interrupt_lookup - lookup table for interrupt handler
176 *
177 * @src: Interrupt mask register.
178 * @clr: Interrupt clear register.
179 * @is_error: true if this is an error interrupt.
180 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
181 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
182 */
183struct d40_interrupt_lookup {
184 u32 src;
185 u32 clr;
186 bool is_error;
187 int offset;
188};
189
190
191static struct d40_interrupt_lookup il_v4a[] = {
192 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
193 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
194 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
195 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
196 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
197 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
198 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
199 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
200 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
201 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
202};
203
204static struct d40_interrupt_lookup il_v4b[] = {
205 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
206 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
207 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
208 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
209 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
210 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
211 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
212 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
213 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
214 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
215 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
216 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
217};
218
219/**
220 * struct d40_reg_val - simple lookup struct
221 *
222 * @reg: The register.
223 * @val: The value that belongs to the register in reg.
224 */
225struct d40_reg_val {
226 unsigned int reg;
227 unsigned int val;
228};
229
230static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
231 /* Clock every part of the DMA block from start */
232 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
233
234 /* Interrupts on all logical channels */
235 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
236 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
237 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
238 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
239 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
240 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
241 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
242 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
243 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
244 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
245 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
246 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
247};
248static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
249 /* Clock every part of the DMA block from start */
250 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
251
252 /* Interrupts on all logical channels */
253 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
254 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
255 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
256 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
257 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
258 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
259 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
260 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
261 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
262 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
263 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
264 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
265 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
266 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
267 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
268};
269
270/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200271 * struct d40_lli_pool - Structure for keeping LLIs in memory
272 *
273 * @base: Pointer to memory area when the pre_alloc_lli's are not large
274 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
275 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100276 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200277 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
278 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
279 * one buffer to one buffer.
280 */
281struct d40_lli_pool {
282 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000283 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100284 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200285 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000286 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200287};
288
289/**
290 * struct d40_desc - A descriptor is one DMA job.
291 *
292 * @lli_phy: LLI settings for physical channel. Both src and dst=
293 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
294 * lli_len equals one.
295 * @lli_log: Same as above but for logical channels.
296 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000297 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300298 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000299 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200300 * @txd: DMA engine struct. Used for among other things for communication
301 * during a transfer.
302 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200303 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530304 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200305 *
306 * This descriptor is used for both logical and physical transfers.
307 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200308struct d40_desc {
309 /* LLI physical */
310 struct d40_phy_lli_bidir lli_phy;
311 /* LLI logical */
312 struct d40_log_lli_bidir lli_log;
313
314 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000315 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000316 int lli_current;
317 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200318
319 struct dma_async_tx_descriptor txd;
320 struct list_head node;
321
Linus Walleij8d318a52010-03-30 15:33:42 +0200322 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100323 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200324};
325
326/**
327 * struct d40_lcla_pool - LCLA pool settings and data.
328 *
Linus Walleij508849a2010-06-20 21:26:07 +0000329 * @base: The virtual address of LCLA. 18 bit aligned.
330 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
331 * This pointer is only there for clean-up on error.
332 * @pages: The number of pages needed for all physical channels.
333 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200334 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000335 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200336 */
337struct d40_lcla_pool {
338 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100339 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000340 void *base_unaligned;
341 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200342 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000343 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200344};
345
346/**
347 * struct d40_phy_res - struct for handling eventlines mapped to physical
348 * channels.
349 *
350 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530351 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200352 * @num: The physical channel number of this entity.
353 * @allocated_src: Bit mapped to show which src event line's are mapped to
354 * this physical channel. Can also be free or physically allocated.
355 * @allocated_dst: Same as for src but is dst.
356 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000357 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100358 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200359 */
360struct d40_phy_res {
361 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530362 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200363 int num;
364 u32 allocated_src;
365 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100366 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200367};
368
369struct d40_base;
370
371/**
372 * struct d40_chan - Struct that describes a channel.
373 *
374 * @lock: A spinlock to protect this struct.
375 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200376 * @pending_tx: The number of pending transfers. Used between interrupt handler
377 * and tasklet.
378 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000379 * @phy_chan: Pointer to physical channel which this instance runs on. If this
380 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200381 * @chan: DMA engine handle.
382 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
383 * transfer and call client callback.
384 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200385 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200386 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100387 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200388 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200389 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200390 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000391 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200392 * @base: Pointer to the device instance struct.
393 * @src_def_cfg: Default cfg register setting for src.
394 * @dst_def_cfg: Default cfg register setting for dst.
395 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200396 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200397 * @runtime_addr: runtime configured address.
398 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200399 *
400 * This struct can either "be" a logical or a physical channel.
401 */
402struct d40_chan {
403 spinlock_t lock;
404 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200405 int pending_tx;
406 bool busy;
407 struct d40_phy_res *phy_chan;
408 struct dma_chan chan;
409 struct tasklet_struct tasklet;
410 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200411 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200412 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100413 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200414 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200415 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200416 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000417 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200418 struct d40_base *base;
419 /* Default register configurations */
420 u32 src_def_cfg;
421 u32 dst_def_cfg;
422 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200423 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200424 /* Runtime reconfiguration */
425 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530426 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200427};
428
429/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000430 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
431 * controller
432 *
433 * @backup: the pointer to the registers address array for backup
434 * @backup_size: the size of the registers address array for backup
435 * @realtime_en: the realtime enable register
436 * @realtime_clear: the realtime clear register
437 * @high_prio_en: the high priority enable register
438 * @high_prio_clear: the high priority clear register
439 * @interrupt_en: the interrupt enable register
440 * @interrupt_clear: the interrupt clear register
441 * @il: the pointer to struct d40_interrupt_lookup
442 * @il_size: the size of d40_interrupt_lookup array
443 * @init_reg: the pointer to the struct d40_reg_val
444 * @init_reg_size: the size of d40_reg_val array
445 */
446struct d40_gen_dmac {
447 u32 *backup;
448 u32 backup_size;
449 u32 realtime_en;
450 u32 realtime_clear;
451 u32 high_prio_en;
452 u32 high_prio_clear;
453 u32 interrupt_en;
454 u32 interrupt_clear;
455 struct d40_interrupt_lookup *il;
456 u32 il_size;
457 struct d40_reg_val *init_reg;
458 u32 init_reg_size;
459};
460
461/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200462 * struct d40_base - The big global struct, one for each probe'd instance.
463 *
464 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
465 * @execmd_lock: Lock for execute command usage since several channels share
466 * the same physical register.
467 * @dev: The device structure.
468 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700469 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200470 * @clk: Pointer to the DMA clock structure.
471 * @phy_start: Physical memory start of the DMA registers.
472 * @phy_size: Size of the DMA register map.
473 * @irq: The IRQ number.
474 * @num_phy_chans: The number of physical channels. Read from HW. This
475 * is the number of available channels for this driver, not counting "Secure
476 * mode" allocated physical channels.
477 * @num_log_chans: The number of logical channels. Calculated from
478 * num_phy_chans.
479 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
480 * @dma_slave: dma_device channels that can do only do slave transfers.
481 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530482 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200483 * @log_chans: Room for all possible logical channels in system.
484 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
485 * to log_chans entries.
486 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
487 * to phy_chans entries.
488 * @plat_data: Pointer to provided platform_data which is the driver
489 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530490 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200491 * @phy_res: Vector containing all physical channels.
492 * @lcla_pool: lcla pool settings and data.
493 * @lcpa_base: The virtual mapped address of LCPA.
494 * @phy_lcpa: The physical address of the LCPA.
495 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000496 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530497 * @reg_val_backup: Here the values of some hardware registers are stored
498 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000499 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
500 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530501 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
502 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
503 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000504 * @gen_dmac: the struct for generic registers values to represent u8500/8540
505 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200506 */
507struct d40_base {
508 spinlock_t interrupt_lock;
509 spinlock_t execmd_lock;
510 struct device *dev;
511 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700512 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200513 struct clk *clk;
514 phys_addr_t phy_start;
515 resource_size_t phy_size;
516 int irq;
517 int num_phy_chans;
518 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200519 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200520 struct dma_device dma_both;
521 struct dma_device dma_slave;
522 struct dma_device dma_memcpy;
523 struct d40_chan *phy_chans;
524 struct d40_chan *log_chans;
525 struct d40_chan **lookup_log_chans;
526 struct d40_chan **lookup_phy_chans;
527 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530528 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200529 /* Physical half channels */
530 struct d40_phy_res *phy_res;
531 struct d40_lcla_pool lcla_pool;
532 void *lcpa_base;
533 dma_addr_t phy_lcpa;
534 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000535 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530536 u32 reg_val_backup[BACKUP_REGS_SZ];
Tong Liu3cb645d2012-09-26 10:07:30 +0000537 u32 reg_val_backup_v4[MAX(BACKUP_REGS_SZ_V4A, BACKUP_REGS_SZ_V4B)];
Narayanan G7fb3e752011-11-17 17:26:41 +0530538 u32 *reg_val_backup_chan;
539 u16 gcc_pwr_off_mask;
540 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000541 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200542};
543
Rabin Vincent262d2912011-01-25 11:18:05 +0100544static struct device *chan2dev(struct d40_chan *d40c)
545{
546 return &d40c->chan.dev->device;
547}
548
Rabin Vincent724a8572011-01-25 11:18:08 +0100549static bool chan_is_physical(struct d40_chan *chan)
550{
551 return chan->log_num == D40_PHY_CHAN;
552}
553
554static bool chan_is_logical(struct d40_chan *chan)
555{
556 return !chan_is_physical(chan);
557}
558
Rabin Vincent8ca84682011-01-25 11:18:07 +0100559static void __iomem *chan_base(struct d40_chan *chan)
560{
561 return chan->base->virtbase + D40_DREG_PCBASE +
562 chan->phy_chan->num * D40_DREG_PCDELTA;
563}
564
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100565#define d40_err(dev, format, arg...) \
566 dev_err(dev, "[%s] " format, __func__, ## arg)
567
568#define chan_err(d40c, format, arg...) \
569 d40_err(chan2dev(d40c), format, ## arg)
570
Rabin Vincentb00f9382011-01-25 11:18:15 +0100571static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100572 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200573{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100574 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200575 u32 align;
576 void *base;
577
578 if (is_log)
579 align = sizeof(struct d40_log_lli);
580 else
581 align = sizeof(struct d40_phy_lli);
582
583 if (lli_len == 1) {
584 base = d40d->lli_pool.pre_alloc_lli;
585 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
586 d40d->lli_pool.base = NULL;
587 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100588 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200589
590 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
591 d40d->lli_pool.base = base;
592
593 if (d40d->lli_pool.base == NULL)
594 return -ENOMEM;
595 }
596
597 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100598 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100599 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100600
601 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200602 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100603 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100604 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100605
606 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
607 d40d->lli_phy.src,
608 d40d->lli_pool.size,
609 DMA_TO_DEVICE);
610
611 if (dma_mapping_error(d40c->base->dev,
612 d40d->lli_pool.dma_addr)) {
613 kfree(d40d->lli_pool.base);
614 d40d->lli_pool.base = NULL;
615 d40d->lli_pool.dma_addr = 0;
616 return -ENOMEM;
617 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200618 }
619
620 return 0;
621}
622
Rabin Vincentb00f9382011-01-25 11:18:15 +0100623static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200624{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100625 if (d40d->lli_pool.dma_addr)
626 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
627 d40d->lli_pool.size, DMA_TO_DEVICE);
628
Linus Walleij8d318a52010-03-30 15:33:42 +0200629 kfree(d40d->lli_pool.base);
630 d40d->lli_pool.base = NULL;
631 d40d->lli_pool.size = 0;
632 d40d->lli_log.src = NULL;
633 d40d->lli_log.dst = NULL;
634 d40d->lli_phy.src = NULL;
635 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200636}
637
Jonas Aaberg698e4732010-08-09 12:08:56 +0000638static int d40_lcla_alloc_one(struct d40_chan *d40c,
639 struct d40_desc *d40d)
640{
641 unsigned long flags;
642 int i;
643 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000644
645 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
646
Jonas Aaberg698e4732010-08-09 12:08:56 +0000647 /*
648 * Allocate both src and dst at the same time, therefore the half
649 * start on 1 since 0 can't be used since zero is used as end marker.
650 */
651 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100652 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
653
654 if (!d40c->base->lcla_pool.alloc_map[idx]) {
655 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000656 d40d->lcla_alloc++;
657 ret = i;
658 break;
659 }
660 }
661
662 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
663
664 return ret;
665}
666
667static int d40_lcla_free_all(struct d40_chan *d40c,
668 struct d40_desc *d40d)
669{
670 unsigned long flags;
671 int i;
672 int ret = -EINVAL;
673
Rabin Vincent724a8572011-01-25 11:18:08 +0100674 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000675 return 0;
676
677 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
678
679 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100680 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
681
682 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
683 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000684 d40d->lcla_alloc--;
685 if (d40d->lcla_alloc == 0) {
686 ret = 0;
687 break;
688 }
689 }
690 }
691
692 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
693
694 return ret;
695
696}
697
Linus Walleij8d318a52010-03-30 15:33:42 +0200698static void d40_desc_remove(struct d40_desc *d40d)
699{
700 list_del(&d40d->node);
701}
702
703static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
704{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000705 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200706
707 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000708 struct d40_desc *d;
709 struct d40_desc *_d;
710
Narayanan G7fb3e752011-11-17 17:26:41 +0530711 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200712 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200713 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000714 desc = d;
715 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000716 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200717 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530718 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200719 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000720
721 if (!desc)
722 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
723
724 if (desc)
725 INIT_LIST_HEAD(&desc->node);
726
727 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200728}
729
730static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
731{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000732
Rabin Vincentb00f9382011-01-25 11:18:15 +0100733 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000734 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000735 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200736}
737
738static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
739{
740 list_add_tail(&desc->node, &d40c->active);
741}
742
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100743static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
744{
745 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
746 struct d40_phy_lli *lli_src = desc->lli_phy.src;
747 void __iomem *base = chan_base(chan);
748
749 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
750 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
751 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
752 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
753
754 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
755 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
756 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
757 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
758}
759
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100760static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
761{
762 list_add_tail(&desc->node, &d40c->done);
763}
764
Rabin Vincente65889c2011-01-25 11:18:31 +0100765static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
766{
767 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
768 struct d40_log_lli_bidir *lli = &desc->lli_log;
769 int lli_current = desc->lli_current;
770 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100771 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100772 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100773 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530774 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100775 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100776
Rabin Vincent0c842b52011-01-25 11:18:35 +0100777 /*
778 * We may have partially running cyclic transfers, in case we did't get
779 * enough LCLA entries.
780 */
781 linkback = cyclic && lli_current == 0;
782
783 /*
784 * For linkback, we need one LCLA even with only one link, because we
785 * can't link back to the one in LCPA space
786 */
787 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100788 /*
789 * If the channel is expected to use only soft_lli don't
790 * allocate a lcla. This is to avoid a HW issue that exists
791 * in some controller during a peripheral to memory transfer
792 * that uses linked lists.
793 */
794 if (!(chan->phy_chan->use_soft_lli &&
795 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
796 curr_lcla = d40_lcla_alloc_one(chan, desc);
797
Rabin Vincent0c842b52011-01-25 11:18:35 +0100798 first_lcla = curr_lcla;
799 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100800
Rabin Vincent0c842b52011-01-25 11:18:35 +0100801 /*
802 * For linkback, we normally load the LCPA in the loop since we need to
803 * link it to the second LCLA and not the first. However, if we
804 * couldn't even get a first LCLA, then we have to run in LCPA and
805 * reload manually.
806 */
807 if (!linkback || curr_lcla == -EINVAL) {
808 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100809
Rabin Vincent0c842b52011-01-25 11:18:35 +0100810 if (curr_lcla == -EINVAL)
811 flags |= LLI_TERM_INT;
812
813 d40_log_lli_lcpa_write(chan->lcpa,
814 &lli->dst[lli_current],
815 &lli->src[lli_current],
816 curr_lcla,
817 flags);
818 lli_current++;
819 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100820
821 if (curr_lcla < 0)
822 goto out;
823
Rabin Vincente65889c2011-01-25 11:18:31 +0100824 for (; lli_current < lli_len; lli_current++) {
825 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
826 8 * curr_lcla * 2;
827 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100828 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100829 int next_lcla;
830
831 if (lli_current + 1 < lli_len)
832 next_lcla = d40_lcla_alloc_one(chan, desc);
833 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100834 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100835
Rabin Vincent0c842b52011-01-25 11:18:35 +0100836 if (cyclic || next_lcla == -EINVAL)
837 flags |= LLI_TERM_INT;
838
839 if (linkback && curr_lcla == first_lcla) {
840 /* First link goes in both LCPA and LCLA */
841 d40_log_lli_lcpa_write(chan->lcpa,
842 &lli->dst[lli_current],
843 &lli->src[lli_current],
844 next_lcla, flags);
845 }
846
847 /*
848 * One unused LCLA in the cyclic case if the very first
849 * next_lcla fails...
850 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100851 d40_log_lli_lcla_write(lcla,
852 &lli->dst[lli_current],
853 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100854 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100855
Narayanan G28c7a192011-11-22 13:56:55 +0530856 /*
857 * Cache maintenance is not needed if lcla is
858 * mapped in esram
859 */
860 if (!use_esram_lcla) {
861 dma_sync_single_range_for_device(chan->base->dev,
862 pool->dma_addr, lcla_offset,
863 2 * sizeof(struct d40_log_lli),
864 DMA_TO_DEVICE);
865 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100866 curr_lcla = next_lcla;
867
Rabin Vincent0c842b52011-01-25 11:18:35 +0100868 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100869 lli_current++;
870 break;
871 }
872 }
873
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100874out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100875 desc->lli_current = lli_current;
876}
877
Jonas Aaberg698e4732010-08-09 12:08:56 +0000878static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
879{
Rabin Vincent724a8572011-01-25 11:18:08 +0100880 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100881 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000882 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100883 } else
884 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000885}
886
Linus Walleij8d318a52010-03-30 15:33:42 +0200887static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
888{
889 struct d40_desc *d;
890
891 if (list_empty(&d40c->active))
892 return NULL;
893
894 d = list_first_entry(&d40c->active,
895 struct d40_desc,
896 node);
897 return d;
898}
899
Per Forlin74043682011-08-29 13:33:34 +0200900/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200901static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
902{
Per Forlin74043682011-08-29 13:33:34 +0200903 d40_desc_remove(desc);
904 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200905 list_add_tail(&desc->node, &d40c->pending_queue);
906}
907
908static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
909{
910 struct d40_desc *d;
911
912 if (list_empty(&d40c->pending_queue))
913 return NULL;
914
915 d = list_first_entry(&d40c->pending_queue,
916 struct d40_desc,
917 node);
918 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200919}
920
921static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
922{
923 struct d40_desc *d;
924
925 if (list_empty(&d40c->queue))
926 return NULL;
927
928 d = list_first_entry(&d40c->queue,
929 struct d40_desc,
930 node);
931 return d;
932}
933
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100934static struct d40_desc *d40_first_done(struct d40_chan *d40c)
935{
936 if (list_empty(&d40c->done))
937 return NULL;
938
939 return list_first_entry(&d40c->done, struct d40_desc, node);
940}
941
Per Forlind49278e2010-12-20 18:31:38 +0100942static int d40_psize_2_burst_size(bool is_log, int psize)
943{
944 if (is_log) {
945 if (psize == STEDMA40_PSIZE_LOG_1)
946 return 1;
947 } else {
948 if (psize == STEDMA40_PSIZE_PHY_1)
949 return 1;
950 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200951
Per Forlind49278e2010-12-20 18:31:38 +0100952 return 2 << psize;
953}
954
955/*
956 * The dma only supports transmitting packages up to
957 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
958 * dma elements required to send the entire sg list
959 */
960static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
961{
962 int dmalen;
963 u32 max_w = max(data_width1, data_width2);
964 u32 min_w = min(data_width1, data_width2);
965 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
966
967 if (seg_max > STEDMA40_MAX_SEG_SIZE)
968 seg_max -= (1 << max_w);
969
970 if (!IS_ALIGNED(size, 1 << max_w))
971 return -EINVAL;
972
973 if (size <= seg_max)
974 dmalen = 1;
975 else {
976 dmalen = size / seg_max;
977 if (dmalen * seg_max < size)
978 dmalen++;
979 }
980 return dmalen;
981}
982
983static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
984 u32 data_width1, u32 data_width2)
985{
986 struct scatterlist *sg;
987 int i;
988 int len = 0;
989 int ret;
990
991 for_each_sg(sgl, sg, sg_len, i) {
992 ret = d40_size_2_dmalen(sg_dma_len(sg),
993 data_width1, data_width2);
994 if (ret < 0)
995 return ret;
996 len += ret;
997 }
998 return len;
999}
1000
Narayanan G7fb3e752011-11-17 17:26:41 +05301001
1002#ifdef CONFIG_PM
1003static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1004 u32 *regaddr, int num, bool save)
1005{
1006 int i;
1007
1008 for (i = 0; i < num; i++) {
1009 void __iomem *addr = baseaddr + regaddr[i];
1010
1011 if (save)
1012 backup[i] = readl_relaxed(addr);
1013 else
1014 writel_relaxed(backup[i], addr);
1015 }
1016}
1017
1018static void d40_save_restore_registers(struct d40_base *base, bool save)
1019{
1020 int i;
1021
1022 /* Save/Restore channel specific registers */
1023 for (i = 0; i < base->num_phy_chans; i++) {
1024 void __iomem *addr;
1025 int idx;
1026
1027 if (base->phy_res[i].reserved)
1028 continue;
1029
1030 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1031 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1032
1033 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1034 d40_backup_regs_chan,
1035 ARRAY_SIZE(d40_backup_regs_chan),
1036 save);
1037 }
1038
1039 /* Save/Restore global registers */
1040 dma40_backup(base->virtbase, base->reg_val_backup,
1041 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1042 save);
1043
1044 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001045 if (base->gen_dmac.backup)
1046 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1047 base->gen_dmac.backup,
1048 base->gen_dmac.backup_size,
1049 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301050}
1051#else
1052static void d40_save_restore_registers(struct d40_base *base, bool save)
1053{
1054}
1055#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001056
Narayanan G1bdae6f2012-02-09 12:41:37 +05301057static int __d40_execute_command_phy(struct d40_chan *d40c,
1058 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001059{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001060 u32 status;
1061 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001062 void __iomem *active_reg;
1063 int ret = 0;
1064 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001065 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001066
Narayanan G1bdae6f2012-02-09 12:41:37 +05301067 if (command == D40_DMA_STOP) {
1068 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1069 if (ret)
1070 return ret;
1071 }
1072
Linus Walleij8d318a52010-03-30 15:33:42 +02001073 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1074
1075 if (d40c->phy_chan->num % 2 == 0)
1076 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1077 else
1078 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1079
1080 if (command == D40_DMA_SUSPEND_REQ) {
1081 status = (readl(active_reg) &
1082 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1083 D40_CHAN_POS(d40c->phy_chan->num);
1084
1085 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1086 goto done;
1087 }
1088
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001089 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1090 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1091 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001092
1093 if (command == D40_DMA_SUSPEND_REQ) {
1094
1095 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1096 status = (readl(active_reg) &
1097 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1098 D40_CHAN_POS(d40c->phy_chan->num);
1099
1100 cpu_relax();
1101 /*
1102 * Reduce the number of bus accesses while
1103 * waiting for the DMA to suspend.
1104 */
1105 udelay(3);
1106
1107 if (status == D40_DMA_STOP ||
1108 status == D40_DMA_SUSPENDED)
1109 break;
1110 }
1111
1112 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001113 chan_err(d40c,
1114 "unable to suspend the chl %d (log: %d) status %x\n",
1115 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001116 status);
1117 dump_stack();
1118 ret = -EBUSY;
1119 }
1120
1121 }
1122done:
1123 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1124 return ret;
1125}
1126
1127static void d40_term_all(struct d40_chan *d40c)
1128{
1129 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001130 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001131
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001132 /* Release completed descriptors */
1133 while ((d40d = d40_first_done(d40c))) {
1134 d40_desc_remove(d40d);
1135 d40_desc_free(d40c, d40d);
1136 }
1137
Linus Walleij8d318a52010-03-30 15:33:42 +02001138 /* Release active descriptors */
1139 while ((d40d = d40_first_active_get(d40c))) {
1140 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001141 d40_desc_free(d40c, d40d);
1142 }
1143
1144 /* Release queued descriptors waiting for transfer */
1145 while ((d40d = d40_first_queued(d40c))) {
1146 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001147 d40_desc_free(d40c, d40d);
1148 }
1149
Per Forlina8f30672011-06-26 23:29:52 +02001150 /* Release pending descriptors */
1151 while ((d40d = d40_first_pending(d40c))) {
1152 d40_desc_remove(d40d);
1153 d40_desc_free(d40c, d40d);
1154 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001155
Per Forlin74043682011-08-29 13:33:34 +02001156 /* Release client owned descriptors */
1157 if (!list_empty(&d40c->client))
1158 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1159 d40_desc_remove(d40d);
1160 d40_desc_free(d40c, d40d);
1161 }
1162
Per Forlin82babbb362011-08-29 13:33:35 +02001163 /* Release descriptors in prepare queue */
1164 if (!list_empty(&d40c->prepare_queue))
1165 list_for_each_entry_safe(d40d, _d,
1166 &d40c->prepare_queue, node) {
1167 d40_desc_remove(d40d);
1168 d40_desc_free(d40c, d40d);
1169 }
Per Forlin74043682011-08-29 13:33:34 +02001170
Linus Walleij8d318a52010-03-30 15:33:42 +02001171 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001172}
1173
Narayanan G1bdae6f2012-02-09 12:41:37 +05301174static void __d40_config_set_event(struct d40_chan *d40c,
1175 enum d40_events event_type, u32 event,
1176 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001177{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001178 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001179 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301180 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001181
Narayanan G1bdae6f2012-02-09 12:41:37 +05301182 switch (event_type) {
1183
1184 case D40_DEACTIVATE_EVENTLINE:
1185
Rabin Vincent262d2912011-01-25 11:18:05 +01001186 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1187 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301188 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001189
Narayanan G1bdae6f2012-02-09 12:41:37 +05301190 case D40_SUSPEND_REQ_EVENTLINE:
1191 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1192 D40_EVENTLINE_POS(event);
1193
1194 if (status == D40_DEACTIVATE_EVENTLINE ||
1195 status == D40_SUSPEND_REQ_EVENTLINE)
1196 break;
1197
1198 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1199 | ~D40_EVENTLINE_MASK(event), addr);
1200
1201 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1202
1203 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1204 D40_EVENTLINE_POS(event);
1205
1206 cpu_relax();
1207 /*
1208 * Reduce the number of bus accesses while
1209 * waiting for the DMA to suspend.
1210 */
1211 udelay(3);
1212
1213 if (status == D40_DEACTIVATE_EVENTLINE)
1214 break;
1215 }
1216
1217 if (tries == D40_SUSPEND_MAX_IT) {
1218 chan_err(d40c,
1219 "unable to stop the event_line chl %d (log: %d)"
1220 "status %x\n", d40c->phy_chan->num,
1221 d40c->log_num, status);
1222 }
1223 break;
1224
1225 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001226 /*
1227 * The hardware sometimes doesn't register the enable when src and dst
1228 * event lines are active on the same logical channel. Retry to ensure
1229 * it does. Usually only one retry is sufficient.
1230 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301231 tries = 100;
1232 while (--tries) {
1233 writel((D40_ACTIVATE_EVENTLINE <<
1234 D40_EVENTLINE_POS(event)) |
1235 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001236
Narayanan G1bdae6f2012-02-09 12:41:37 +05301237 if (readl(addr) & D40_EVENTLINE_MASK(event))
1238 break;
1239 }
1240
1241 if (tries != 99)
1242 dev_dbg(chan2dev(d40c),
1243 "[%s] workaround enable S%cLNK (%d tries)\n",
1244 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1245 100 - tries);
1246
1247 WARN_ON(!tries);
1248 break;
1249
1250 case D40_ROUND_EVENTLINE:
1251 BUG();
1252 break;
1253
Rabin Vincent262d2912011-01-25 11:18:05 +01001254 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001255}
1256
Narayanan G1bdae6f2012-02-09 12:41:37 +05301257static void d40_config_set_event(struct d40_chan *d40c,
1258 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001259{
Linus Walleij8d318a52010-03-30 15:33:42 +02001260 /* Enable event line connected to device (or memcpy) */
1261 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1262 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1263 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1264
Narayanan G1bdae6f2012-02-09 12:41:37 +05301265 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001266 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001267 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001268
Linus Walleij8d318a52010-03-30 15:33:42 +02001269 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1270 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1271
Narayanan G1bdae6f2012-02-09 12:41:37 +05301272 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001273 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001274 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001275}
1276
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001277static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001278{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001279 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001280 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001281
Rabin Vincent8ca84682011-01-25 11:18:07 +01001282 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1283 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001284
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001285 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001286}
1287
Narayanan G1bdae6f2012-02-09 12:41:37 +05301288static int
1289__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1290{
1291 unsigned long flags;
1292 int ret = 0;
1293 u32 active_status;
1294 void __iomem *active_reg;
1295
1296 if (d40c->phy_chan->num % 2 == 0)
1297 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1298 else
1299 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1300
1301
1302 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1303
1304 switch (command) {
1305 case D40_DMA_STOP:
1306 case D40_DMA_SUSPEND_REQ:
1307
1308 active_status = (readl(active_reg) &
1309 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1310 D40_CHAN_POS(d40c->phy_chan->num);
1311
1312 if (active_status == D40_DMA_RUN)
1313 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1314 else
1315 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1316
1317 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1318 ret = __d40_execute_command_phy(d40c, command);
1319
1320 break;
1321
1322 case D40_DMA_RUN:
1323
1324 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1325 ret = __d40_execute_command_phy(d40c, command);
1326 break;
1327
1328 case D40_DMA_SUSPENDED:
1329 BUG();
1330 break;
1331 }
1332
1333 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1334 return ret;
1335}
1336
1337static int d40_channel_execute_command(struct d40_chan *d40c,
1338 enum d40_command command)
1339{
1340 if (chan_is_logical(d40c))
1341 return __d40_execute_command_log(d40c, command);
1342 else
1343 return __d40_execute_command_phy(d40c, command);
1344}
1345
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001346static u32 d40_get_prmo(struct d40_chan *d40c)
1347{
1348 static const unsigned int phy_map[] = {
1349 [STEDMA40_PCHAN_BASIC_MODE]
1350 = D40_DREG_PRMO_PCHAN_BASIC,
1351 [STEDMA40_PCHAN_MODULO_MODE]
1352 = D40_DREG_PRMO_PCHAN_MODULO,
1353 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1354 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1355 };
1356 static const unsigned int log_map[] = {
1357 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1358 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1359 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1360 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1361 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1362 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1363 };
1364
Rabin Vincent724a8572011-01-25 11:18:08 +01001365 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001366 return phy_map[d40c->dma_cfg.mode_opt];
1367 else
1368 return log_map[d40c->dma_cfg.mode_opt];
1369}
1370
Jonas Aabergb55912c2010-08-09 12:08:02 +00001371static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001372{
1373 u32 addr_base;
1374 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001375
1376 /* Odd addresses are even addresses + 4 */
1377 addr_base = (d40c->phy_chan->num % 2) * 4;
1378 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001379 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001380 D40_CHAN_POS(d40c->phy_chan->num);
1381 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1382
1383 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001384 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001385
1386 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1387
Rabin Vincent724a8572011-01-25 11:18:08 +01001388 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001389 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1390 & D40_SREG_ELEM_LOG_LIDX_MASK;
1391 void __iomem *chanbase = chan_base(d40c);
1392
Linus Walleij8d318a52010-03-30 15:33:42 +02001393 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001394 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1395 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001396
Jonas Aabergb55912c2010-08-09 12:08:02 +00001397 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001398 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1399 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301400
1401 /* Clear LNK which will be used by d40_chan_has_events() */
1402 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1403 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001404 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001405}
1406
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001407static u32 d40_residue(struct d40_chan *d40c)
1408{
1409 u32 num_elt;
1410
Rabin Vincent724a8572011-01-25 11:18:08 +01001411 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001412 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1413 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001414 else {
1415 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1416 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1417 >> D40_SREG_ELEM_PHY_ECNT_POS;
1418 }
1419
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001420 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1421}
1422
1423static bool d40_tx_is_linked(struct d40_chan *d40c)
1424{
1425 bool is_link;
1426
Rabin Vincent724a8572011-01-25 11:18:08 +01001427 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001428 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1429 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001430 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1431 & D40_SREG_LNK_PHYS_LNK_MASK;
1432
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001433 return is_link;
1434}
1435
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001436static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001437{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001438 int res = 0;
1439 unsigned long flags;
1440
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001441 if (!d40c->busy)
1442 return 0;
1443
Narayanan G7fb3e752011-11-17 17:26:41 +05301444 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001445 spin_lock_irqsave(&d40c->lock, flags);
1446
1447 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301448
Narayanan G7fb3e752011-11-17 17:26:41 +05301449 pm_runtime_mark_last_busy(d40c->base->dev);
1450 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001451 spin_unlock_irqrestore(&d40c->lock, flags);
1452 return res;
1453}
1454
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001455static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001456{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001457 int res = 0;
1458 unsigned long flags;
1459
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001460 if (!d40c->busy)
1461 return 0;
1462
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001463 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301464 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001465
1466 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301467 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001468 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001469
Narayanan G7fb3e752011-11-17 17:26:41 +05301470 pm_runtime_mark_last_busy(d40c->base->dev);
1471 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001472 spin_unlock_irqrestore(&d40c->lock, flags);
1473 return res;
1474}
1475
Linus Walleij8d318a52010-03-30 15:33:42 +02001476static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1477{
1478 struct d40_chan *d40c = container_of(tx->chan,
1479 struct d40_chan,
1480 chan);
1481 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1482 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001483 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001484
1485 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001486 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001487 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001488 spin_unlock_irqrestore(&d40c->lock, flags);
1489
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001490 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001491}
1492
1493static int d40_start(struct d40_chan *d40c)
1494{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001495 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001496}
1497
1498static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1499{
1500 struct d40_desc *d40d;
1501 int err;
1502
1503 /* Start queued jobs, if any */
1504 d40d = d40_first_queued(d40c);
1505
1506 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301507 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301508 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301509 pm_runtime_get_sync(d40c->base->dev);
1510 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001511
1512 /* Remove from queue */
1513 d40_desc_remove(d40d);
1514
1515 /* Add to active queue */
1516 d40_desc_submit(d40c, d40d);
1517
Rabin Vincent7d83a852011-01-25 11:18:06 +01001518 /* Initiate DMA job */
1519 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001520
Rabin Vincent7d83a852011-01-25 11:18:06 +01001521 /* Start dma job */
1522 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001523
Rabin Vincent7d83a852011-01-25 11:18:06 +01001524 if (err)
1525 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001526 }
1527
1528 return d40d;
1529}
1530
1531/* called from interrupt context */
1532static void dma_tc_handle(struct d40_chan *d40c)
1533{
1534 struct d40_desc *d40d;
1535
Linus Walleij8d318a52010-03-30 15:33:42 +02001536 /* Get first active entry from list */
1537 d40d = d40_first_active_get(d40c);
1538
1539 if (d40d == NULL)
1540 return;
1541
Rabin Vincent0c842b52011-01-25 11:18:35 +01001542 if (d40d->cyclic) {
1543 /*
1544 * If this was a paritially loaded list, we need to reloaded
1545 * it, and only when the list is completed. We need to check
1546 * for done because the interrupt will hit for every link, and
1547 * not just the last one.
1548 */
1549 if (d40d->lli_current < d40d->lli_len
1550 && !d40_tx_is_linked(d40c)
1551 && !d40_residue(d40c)) {
1552 d40_lcla_free_all(d40c, d40d);
1553 d40_desc_load(d40c, d40d);
1554 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001555
Rabin Vincent0c842b52011-01-25 11:18:35 +01001556 if (d40d->lli_current == d40d->lli_len)
1557 d40d->lli_current = 0;
1558 }
1559 } else {
1560 d40_lcla_free_all(d40c, d40d);
1561
1562 if (d40d->lli_current < d40d->lli_len) {
1563 d40_desc_load(d40c, d40d);
1564 /* Start dma job */
1565 (void) d40_start(d40c);
1566 return;
1567 }
1568
1569 if (d40_queue_start(d40c) == NULL)
1570 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301571 pm_runtime_mark_last_busy(d40c->base->dev);
1572 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001573 }
1574
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001575 d40_desc_remove(d40d);
1576 d40_desc_done(d40c, d40d);
1577
Linus Walleij8d318a52010-03-30 15:33:42 +02001578 d40c->pending_tx++;
1579 tasklet_schedule(&d40c->tasklet);
1580
1581}
1582
1583static void dma_tasklet(unsigned long data)
1584{
1585 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001586 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001587 unsigned long flags;
1588 dma_async_tx_callback callback;
1589 void *callback_param;
1590
1591 spin_lock_irqsave(&d40c->lock, flags);
1592
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001593 /* Get first entry from the done list */
1594 d40d = d40_first_done(d40c);
1595 if (d40d == NULL) {
1596 /* Check if we have reached here for cyclic job */
1597 d40d = d40_first_active_get(d40c);
1598 if (d40d == NULL || !d40d->cyclic)
1599 goto err;
1600 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001601
Rabin Vincent0c842b52011-01-25 11:18:35 +01001602 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001603 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001604
1605 /*
1606 * If terminating a channel pending_tx is set to zero.
1607 * This prevents any finished active jobs to return to the client.
1608 */
1609 if (d40c->pending_tx == 0) {
1610 spin_unlock_irqrestore(&d40c->lock, flags);
1611 return;
1612 }
1613
1614 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001615 callback = d40d->txd.callback;
1616 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001617
Rabin Vincent0c842b52011-01-25 11:18:35 +01001618 if (!d40d->cyclic) {
1619 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001620 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001621 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001622 } else if (!d40d->is_in_client_list) {
1623 d40_desc_remove(d40d);
1624 d40_lcla_free_all(d40c, d40d);
1625 list_add_tail(&d40d->node, &d40c->client);
1626 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001627 }
1628 }
1629
1630 d40c->pending_tx--;
1631
1632 if (d40c->pending_tx)
1633 tasklet_schedule(&d40c->tasklet);
1634
1635 spin_unlock_irqrestore(&d40c->lock, flags);
1636
Jonas Aaberg767a9672010-08-09 12:08:34 +00001637 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001638 callback(callback_param);
1639
1640 return;
1641
Narayanan G1bdae6f2012-02-09 12:41:37 +05301642err:
1643 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001644 if (d40c->pending_tx > 0)
1645 d40c->pending_tx--;
1646 spin_unlock_irqrestore(&d40c->lock, flags);
1647}
1648
1649static irqreturn_t d40_handle_interrupt(int irq, void *data)
1650{
Linus Walleij8d318a52010-03-30 15:33:42 +02001651 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001652 u32 idx;
1653 u32 row;
1654 long chan = -1;
1655 struct d40_chan *d40c;
1656 unsigned long flags;
1657 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001658 u32 regs[base->gen_dmac.il_size];
1659 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1660 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001661
1662 spin_lock_irqsave(&base->interrupt_lock, flags);
1663
1664 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001665 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001666 regs[i] = readl(base->virtbase + il[i].src);
1667
1668 for (;;) {
1669
1670 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001671 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001672
1673 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001674 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001675 break;
1676
1677 row = chan / BITS_PER_LONG;
1678 idx = chan & (BITS_PER_LONG - 1);
1679
1680 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001681 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001682
1683 if (il[row].offset == D40_PHY_CHAN)
1684 d40c = base->lookup_phy_chans[idx];
1685 else
1686 d40c = base->lookup_log_chans[il[row].offset + idx];
1687 spin_lock(&d40c->lock);
1688
1689 if (!il[row].is_error)
1690 dma_tc_handle(d40c);
1691 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001692 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1693 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001694
1695 spin_unlock(&d40c->lock);
1696 }
1697
1698 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1699
1700 return IRQ_HANDLED;
1701}
1702
Linus Walleij8d318a52010-03-30 15:33:42 +02001703static int d40_validate_conf(struct d40_chan *d40c,
1704 struct stedma40_chan_cfg *conf)
1705{
1706 int res = 0;
1707 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1708 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001709 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001710
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001711 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001712 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001713 res = -EINVAL;
1714 }
1715
1716 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1717 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1718 d40c->runtime_addr == 0) {
1719
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001720 chan_err(d40c, "Invalid TX channel address (%d)\n",
1721 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001722 res = -EINVAL;
1723 }
1724
1725 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1726 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1727 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001728 chan_err(d40c, "Invalid RX channel address (%d)\n",
1729 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001730 res = -EINVAL;
1731 }
1732
1733 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001734 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001735 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001736 res = -EINVAL;
1737 }
1738
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001739 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001740 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001741 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001742 res = -EINVAL;
1743 }
1744
1745 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1746 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001747 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001748 res = -EINVAL;
1749 }
1750
1751 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1752 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001753 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001754 res = -EINVAL;
1755 }
1756
1757 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1758 /*
1759 * DMAC HW supports it. Will be added to this driver,
1760 * in case any dma client requires it.
1761 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001762 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001763 res = -EINVAL;
1764 }
1765
Per Forlind49278e2010-12-20 18:31:38 +01001766 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1767 (1 << conf->src_info.data_width) !=
1768 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1769 (1 << conf->dst_info.data_width)) {
1770 /*
1771 * The DMAC hardware only supports
1772 * src (burst x width) == dst (burst x width)
1773 */
1774
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001775 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001776 res = -EINVAL;
1777 }
1778
Linus Walleij8d318a52010-03-30 15:33:42 +02001779 return res;
1780}
1781
Narayanan G5cd326f2011-11-30 19:20:42 +05301782static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1783 bool is_src, int log_event_line, bool is_log,
1784 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001785{
1786 unsigned long flags;
1787 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301788
1789 *first_user = ((phy->allocated_src | phy->allocated_dst)
1790 == D40_ALLOC_FREE);
1791
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001792 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001793 /* Physical interrupts are masked per physical full channel */
1794 if (phy->allocated_src == D40_ALLOC_FREE &&
1795 phy->allocated_dst == D40_ALLOC_FREE) {
1796 phy->allocated_dst = D40_ALLOC_PHY;
1797 phy->allocated_src = D40_ALLOC_PHY;
1798 goto found;
1799 } else
1800 goto not_found;
1801 }
1802
1803 /* Logical channel */
1804 if (is_src) {
1805 if (phy->allocated_src == D40_ALLOC_PHY)
1806 goto not_found;
1807
1808 if (phy->allocated_src == D40_ALLOC_FREE)
1809 phy->allocated_src = D40_ALLOC_LOG_FREE;
1810
1811 if (!(phy->allocated_src & (1 << log_event_line))) {
1812 phy->allocated_src |= 1 << log_event_line;
1813 goto found;
1814 } else
1815 goto not_found;
1816 } else {
1817 if (phy->allocated_dst == D40_ALLOC_PHY)
1818 goto not_found;
1819
1820 if (phy->allocated_dst == D40_ALLOC_FREE)
1821 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1822
1823 if (!(phy->allocated_dst & (1 << log_event_line))) {
1824 phy->allocated_dst |= 1 << log_event_line;
1825 goto found;
1826 } else
1827 goto not_found;
1828 }
1829
1830not_found:
1831 spin_unlock_irqrestore(&phy->lock, flags);
1832 return false;
1833found:
1834 spin_unlock_irqrestore(&phy->lock, flags);
1835 return true;
1836}
1837
1838static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1839 int log_event_line)
1840{
1841 unsigned long flags;
1842 bool is_free = false;
1843
1844 spin_lock_irqsave(&phy->lock, flags);
1845 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001846 phy->allocated_dst = D40_ALLOC_FREE;
1847 phy->allocated_src = D40_ALLOC_FREE;
1848 is_free = true;
1849 goto out;
1850 }
1851
1852 /* Logical channel */
1853 if (is_src) {
1854 phy->allocated_src &= ~(1 << log_event_line);
1855 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1856 phy->allocated_src = D40_ALLOC_FREE;
1857 } else {
1858 phy->allocated_dst &= ~(1 << log_event_line);
1859 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1860 phy->allocated_dst = D40_ALLOC_FREE;
1861 }
1862
1863 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1864 D40_ALLOC_FREE);
1865
1866out:
1867 spin_unlock_irqrestore(&phy->lock, flags);
1868
1869 return is_free;
1870}
1871
Narayanan G5cd326f2011-11-30 19:20:42 +05301872static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001873{
1874 int dev_type;
1875 int event_group;
1876 int event_line;
1877 struct d40_phy_res *phys;
1878 int i;
1879 int j;
1880 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001881 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001882 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001883 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001884
1885 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001886 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001887
1888 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1889 dev_type = d40c->dma_cfg.src_dev_type;
1890 log_num = 2 * dev_type;
1891 is_src = true;
1892 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1893 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1894 /* dst event lines are used for logical memcpy */
1895 dev_type = d40c->dma_cfg.dst_dev_type;
1896 log_num = 2 * dev_type + 1;
1897 is_src = false;
1898 } else
1899 return -EINVAL;
1900
1901 event_group = D40_TYPE_TO_GROUP(dev_type);
1902 event_line = D40_TYPE_TO_EVENT(dev_type);
1903
1904 if (!is_log) {
1905 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1906 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001907 if (d40c->dma_cfg.use_fixed_channel) {
1908 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001909 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301910 0, is_log,
1911 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001912 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001913 } else {
1914 for (i = 0; i < num_phy_chans; i++) {
1915 if (d40_alloc_mask_set(&phys[i], is_src,
1916 0, is_log,
1917 first_phy_user))
1918 goto found_phy;
1919 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001920 }
1921 } else
1922 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1923 int phy_num = j + event_group * 2;
1924 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001925 if (d40_alloc_mask_set(&phys[i],
1926 is_src,
1927 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301928 is_log,
1929 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001930 goto found_phy;
1931 }
1932 }
1933 return -EINVAL;
1934found_phy:
1935 d40c->phy_chan = &phys[i];
1936 d40c->log_num = D40_PHY_CHAN;
1937 goto out;
1938 }
1939 if (dev_type == -1)
1940 return -EINVAL;
1941
1942 /* Find logical channel */
1943 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1944 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301945
1946 if (d40c->dma_cfg.use_fixed_channel) {
1947 i = d40c->dma_cfg.phy_channel;
1948
1949 if ((i != phy_num) && (i != phy_num + 1)) {
1950 dev_err(chan2dev(d40c),
1951 "invalid fixed phy channel %d\n", i);
1952 return -EINVAL;
1953 }
1954
1955 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1956 is_log, first_phy_user))
1957 goto found_log;
1958
1959 dev_err(chan2dev(d40c),
1960 "could not allocate fixed phy channel %d\n", i);
1961 return -EINVAL;
1962 }
1963
Linus Walleij8d318a52010-03-30 15:33:42 +02001964 /*
1965 * Spread logical channels across all available physical rather
1966 * than pack every logical channel at the first available phy
1967 * channels.
1968 */
1969 if (is_src) {
1970 for (i = phy_num; i < phy_num + 2; i++) {
1971 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301972 event_line, is_log,
1973 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001974 goto found_log;
1975 }
1976 } else {
1977 for (i = phy_num + 1; i >= phy_num; i--) {
1978 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301979 event_line, is_log,
1980 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001981 goto found_log;
1982 }
1983 }
1984 }
1985 return -EINVAL;
1986
1987found_log:
1988 d40c->phy_chan = &phys[i];
1989 d40c->log_num = log_num;
1990out:
1991
1992 if (is_log)
1993 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1994 else
1995 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1996
1997 return 0;
1998
1999}
2000
Linus Walleij8d318a52010-03-30 15:33:42 +02002001static int d40_config_memcpy(struct d40_chan *d40c)
2002{
2003 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2004
2005 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
2006 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
2007 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
2008 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
2009 memcpy[d40c->chan.chan_id];
2010
2011 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2012 dma_has_cap(DMA_SLAVE, cap)) {
2013 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
2014 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002015 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002016 return -EINVAL;
2017 }
2018
2019 return 0;
2020}
2021
Linus Walleij8d318a52010-03-30 15:33:42 +02002022static int d40_free_dma(struct d40_chan *d40c)
2023{
2024
2025 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00002026 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02002027 struct d40_phy_res *phy = d40c->phy_chan;
2028 bool is_src;
2029
2030 /* Terminate all queued and active transfers */
2031 d40_term_all(d40c);
2032
2033 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002034 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002035 return -EINVAL;
2036 }
2037
2038 if (phy->allocated_src == D40_ALLOC_FREE &&
2039 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002040 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002041 return -EINVAL;
2042 }
2043
Linus Walleij8d318a52010-03-30 15:33:42 +02002044 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
2045 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
2046 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002047 is_src = false;
2048 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
2049 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002050 is_src = true;
2051 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002052 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002053 return -EINVAL;
2054 }
2055
Narayanan G7fb3e752011-11-17 17:26:41 +05302056 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002057 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2058 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302059 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302060 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002061 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302062
Narayanan G1bdae6f2012-02-09 12:41:37 +05302063 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2064
2065 if (chan_is_logical(d40c))
2066 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2067 else
2068 d40c->base->lookup_phy_chans[phy->num] = NULL;
2069
Narayanan G7fb3e752011-11-17 17:26:41 +05302070 if (d40c->busy) {
2071 pm_runtime_mark_last_busy(d40c->base->dev);
2072 pm_runtime_put_autosuspend(d40c->base->dev);
2073 }
2074
2075 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002076 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002077 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302078out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002079
Narayanan G7fb3e752011-11-17 17:26:41 +05302080 pm_runtime_mark_last_busy(d40c->base->dev);
2081 pm_runtime_put_autosuspend(d40c->base->dev);
2082 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002083}
2084
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002085static bool d40_is_paused(struct d40_chan *d40c)
2086{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002087 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002088 bool is_paused = false;
2089 unsigned long flags;
2090 void __iomem *active_reg;
2091 u32 status;
2092 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002093
2094 spin_lock_irqsave(&d40c->lock, flags);
2095
Rabin Vincent724a8572011-01-25 11:18:08 +01002096 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002097 if (d40c->phy_chan->num % 2 == 0)
2098 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2099 else
2100 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2101
2102 status = (readl(active_reg) &
2103 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2104 D40_CHAN_POS(d40c->phy_chan->num);
2105 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2106 is_paused = true;
2107
2108 goto _exit;
2109 }
2110
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002111 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002112 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002113 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002114 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002115 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002116 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002117 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002118 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002119 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002120 goto _exit;
2121 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002122
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002123 status = (status & D40_EVENTLINE_MASK(event)) >>
2124 D40_EVENTLINE_POS(event);
2125
2126 if (status != D40_DMA_RUN)
2127 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002128_exit:
2129 spin_unlock_irqrestore(&d40c->lock, flags);
2130 return is_paused;
2131
2132}
2133
Linus Walleij8d318a52010-03-30 15:33:42 +02002134static u32 stedma40_residue(struct dma_chan *chan)
2135{
2136 struct d40_chan *d40c =
2137 container_of(chan, struct d40_chan, chan);
2138 u32 bytes_left;
2139 unsigned long flags;
2140
2141 spin_lock_irqsave(&d40c->lock, flags);
2142 bytes_left = d40_residue(d40c);
2143 spin_unlock_irqrestore(&d40c->lock, flags);
2144
2145 return bytes_left;
2146}
2147
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002148static int
2149d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2150 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002151 unsigned int sg_len, dma_addr_t src_dev_addr,
2152 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002153{
2154 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2155 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2156 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002157 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002158
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002159 ret = d40_log_sg_to_lli(sg_src, sg_len,
2160 src_dev_addr,
2161 desc->lli_log.src,
2162 chan->log_def.lcsp1,
2163 src_info->data_width,
2164 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002165
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002166 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2167 dst_dev_addr,
2168 desc->lli_log.dst,
2169 chan->log_def.lcsp3,
2170 dst_info->data_width,
2171 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002172
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002173 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002174}
2175
2176static int
2177d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2178 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002179 unsigned int sg_len, dma_addr_t src_dev_addr,
2180 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002181{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002182 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2183 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2184 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002185 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002186 int ret;
2187
Rabin Vincent0c842b52011-01-25 11:18:35 +01002188 if (desc->cyclic)
2189 flags |= LLI_CYCLIC | LLI_TERM_INT;
2190
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002191 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2192 desc->lli_phy.src,
2193 virt_to_phys(desc->lli_phy.src),
2194 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002195 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002196
2197 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2198 desc->lli_phy.dst,
2199 virt_to_phys(desc->lli_phy.dst),
2200 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002201 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002202
2203 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2204 desc->lli_pool.size, DMA_TO_DEVICE);
2205
2206 return ret < 0 ? ret : 0;
2207}
2208
Rabin Vincent5f811582011-01-25 11:18:18 +01002209static struct d40_desc *
2210d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2211 unsigned int sg_len, unsigned long dma_flags)
2212{
2213 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2214 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002215 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002216
2217 desc = d40_desc_get(chan);
2218 if (!desc)
2219 return NULL;
2220
2221 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2222 cfg->dst_info.data_width);
2223 if (desc->lli_len < 0) {
2224 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002225 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002226 }
2227
Rabin Vincentdbd88782011-01-25 11:18:19 +01002228 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2229 if (ret < 0) {
2230 chan_err(chan, "Could not allocate lli\n");
2231 goto err;
2232 }
2233
Rabin Vincent5f811582011-01-25 11:18:18 +01002234 desc->lli_current = 0;
2235 desc->txd.flags = dma_flags;
2236 desc->txd.tx_submit = d40_tx_submit;
2237
2238 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2239
2240 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002241
2242err:
2243 d40_desc_free(chan, desc);
2244 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002245}
2246
Rabin Vincentcade1d32011-01-25 11:18:23 +01002247static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302248d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002249{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002250 struct stedma40_platform_data *plat = chan->base->plat_data;
2251 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002252 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002253
Rabin Vincentcade1d32011-01-25 11:18:23 +01002254 if (chan->runtime_addr)
2255 return chan->runtime_addr;
2256
Vinod Kouldb8196d2011-10-13 22:34:23 +05302257 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002258 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302259 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002260 addr = plat->dev_tx[cfg->dst_dev_type];
2261
2262 return addr;
2263}
2264
2265static struct dma_async_tx_descriptor *
2266d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2267 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302268 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002269{
2270 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002271 dma_addr_t src_dev_addr = 0;
2272 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002273 struct d40_desc *desc;
2274 unsigned long flags;
2275 int ret;
2276
2277 if (!chan->phy_chan) {
2278 chan_err(chan, "Cannot prepare unallocated channel\n");
2279 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002280 }
2281
Rabin Vincentcade1d32011-01-25 11:18:23 +01002282 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002283
Rabin Vincentcade1d32011-01-25 11:18:23 +01002284 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2285 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002286 goto err;
2287
Rabin Vincent0c842b52011-01-25 11:18:35 +01002288 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2289 desc->cyclic = true;
2290
Linus Walleij7e426da2012-04-12 18:12:52 +02002291 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002292 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2293
Vinod Kouldb8196d2011-10-13 22:34:23 +05302294 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002295 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302296 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002297 dst_dev_addr = dev_addr;
2298 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002299
2300 if (chan_is_logical(chan))
2301 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002302 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002303 else
2304 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002305 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002306
2307 if (ret) {
2308 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2309 chan_is_logical(chan) ? "log" : "phy", ret);
2310 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002311 }
2312
Per Forlin82babbb362011-08-29 13:33:35 +02002313 /*
2314 * add descriptor to the prepare queue in order to be able
2315 * to free them later in terminate_all
2316 */
2317 list_add_tail(&desc->node, &chan->prepare_queue);
2318
Rabin Vincentcade1d32011-01-25 11:18:23 +01002319 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002320
Rabin Vincentcade1d32011-01-25 11:18:23 +01002321 return &desc->txd;
2322
Linus Walleij8d318a52010-03-30 15:33:42 +02002323err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002324 if (desc)
2325 d40_desc_free(chan, desc);
2326 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002327 return NULL;
2328}
Linus Walleij8d318a52010-03-30 15:33:42 +02002329
2330bool stedma40_filter(struct dma_chan *chan, void *data)
2331{
2332 struct stedma40_chan_cfg *info = data;
2333 struct d40_chan *d40c =
2334 container_of(chan, struct d40_chan, chan);
2335 int err;
2336
2337 if (data) {
2338 err = d40_validate_conf(d40c, info);
2339 if (!err)
2340 d40c->dma_cfg = *info;
2341 } else
2342 err = d40_config_memcpy(d40c);
2343
Rabin Vincentce2ca122010-10-12 13:00:49 +00002344 if (!err)
2345 d40c->configured = true;
2346
Linus Walleij8d318a52010-03-30 15:33:42 +02002347 return err == 0;
2348}
2349EXPORT_SYMBOL(stedma40_filter);
2350
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002351static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2352{
2353 bool realtime = d40c->dma_cfg.realtime;
2354 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002355 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002356 u32 event = D40_TYPE_TO_EVENT(dev_type);
2357 u32 group = D40_TYPE_TO_GROUP(dev_type);
2358 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302359 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002360 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302361
Tong Liu3cb645d2012-09-26 10:07:30 +00002362 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302363 /*
2364 * Due to a hardware bug, in some cases a logical channel triggered by
2365 * a high priority destination event line can generate extra packet
2366 * transactions.
2367 *
2368 * The workaround is to not set the high priority level for the
2369 * destination event lines that trigger logical channels.
2370 */
2371 if (!src && chan_is_logical(d40c))
2372 highprio = false;
2373
Tong Liu3cb645d2012-09-26 10:07:30 +00002374 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002375
2376 /* Destination event lines are stored in the upper halfword */
2377 if (!src)
2378 bit <<= 16;
2379
2380 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2381 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2382}
2383
2384static void d40_set_prio_realtime(struct d40_chan *d40c)
2385{
2386 if (d40c->base->rev < 3)
2387 return;
2388
2389 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2390 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2391 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2392
2393 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2394 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2395 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2396}
2397
Linus Walleij8d318a52010-03-30 15:33:42 +02002398/* DMA ENGINE functions */
2399static int d40_alloc_chan_resources(struct dma_chan *chan)
2400{
2401 int err;
2402 unsigned long flags;
2403 struct d40_chan *d40c =
2404 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002405 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002406 spin_lock_irqsave(&d40c->lock, flags);
2407
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002408 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002409
Rabin Vincentce2ca122010-10-12 13:00:49 +00002410 /* If no dma configuration is set use default configuration (memcpy) */
2411 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002412 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002413 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002414 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002415 goto fail;
2416 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002417 }
2418
Narayanan G5cd326f2011-11-30 19:20:42 +05302419 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002420 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002421 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302422 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002423 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002424 }
2425
Narayanan G7fb3e752011-11-17 17:26:41 +05302426 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002427 /* Fill in basic CFG register values */
2428 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002429 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002430
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002431 d40_set_prio_realtime(d40c);
2432
Rabin Vincent724a8572011-01-25 11:18:08 +01002433 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002434 d40_log_cfg(&d40c->dma_cfg,
2435 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2436
2437 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2438 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002439 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002440 else
2441 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002442 d40c->dma_cfg.dst_dev_type *
2443 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Linus Walleijef1872e2010-06-20 21:24:52 +00002444 }
2445
Narayanan G5cd326f2011-11-30 19:20:42 +05302446 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2447 chan_is_logical(d40c) ? "logical" : "physical",
2448 d40c->phy_chan->num,
2449 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2450
2451
Linus Walleijef1872e2010-06-20 21:24:52 +00002452 /*
2453 * Only write channel configuration to the DMA if the physical
2454 * resource is free. In case of multiple logical channels
2455 * on the same physical resource, only the first write is necessary.
2456 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002457 if (is_free_phy)
2458 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002459fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302460 pm_runtime_mark_last_busy(d40c->base->dev);
2461 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002462 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002463 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002464}
2465
2466static void d40_free_chan_resources(struct dma_chan *chan)
2467{
2468 struct d40_chan *d40c =
2469 container_of(chan, struct d40_chan, chan);
2470 int err;
2471 unsigned long flags;
2472
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002473 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002474 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002475 return;
2476 }
2477
Linus Walleij8d318a52010-03-30 15:33:42 +02002478 spin_lock_irqsave(&d40c->lock, flags);
2479
2480 err = d40_free_dma(d40c);
2481
2482 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002483 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002484 spin_unlock_irqrestore(&d40c->lock, flags);
2485}
2486
2487static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2488 dma_addr_t dst,
2489 dma_addr_t src,
2490 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002491 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002492{
Rabin Vincent95944c62011-01-25 11:18:17 +01002493 struct scatterlist dst_sg;
2494 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002495
Rabin Vincent95944c62011-01-25 11:18:17 +01002496 sg_init_table(&dst_sg, 1);
2497 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002498
Rabin Vincent95944c62011-01-25 11:18:17 +01002499 sg_dma_address(&dst_sg) = dst;
2500 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002501
Rabin Vincent95944c62011-01-25 11:18:17 +01002502 sg_dma_len(&dst_sg) = size;
2503 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002504
Rabin Vincentcade1d32011-01-25 11:18:23 +01002505 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002506}
2507
Ira Snyder0d688662010-09-30 11:46:47 +00002508static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002509d40_prep_memcpy_sg(struct dma_chan *chan,
2510 struct scatterlist *dst_sg, unsigned int dst_nents,
2511 struct scatterlist *src_sg, unsigned int src_nents,
2512 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002513{
2514 if (dst_nents != src_nents)
2515 return NULL;
2516
Rabin Vincentcade1d32011-01-25 11:18:23 +01002517 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002518}
2519
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002520static struct dma_async_tx_descriptor *
2521d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2522 unsigned int sg_len, enum dma_transfer_direction direction,
2523 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002524{
Vinod Kouldb8196d2011-10-13 22:34:23 +05302525 if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV)
Rabin Vincent00ac0342011-01-25 11:18:20 +01002526 return NULL;
2527
Rabin Vincentcade1d32011-01-25 11:18:23 +01002528 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002529}
2530
Rabin Vincent0c842b52011-01-25 11:18:35 +01002531static struct dma_async_tx_descriptor *
2532dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2533 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002534 enum dma_transfer_direction direction, unsigned long flags,
2535 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002536{
2537 unsigned int periods = buf_len / period_len;
2538 struct dma_async_tx_descriptor *txd;
2539 struct scatterlist *sg;
2540 int i;
2541
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002542 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002543 for (i = 0; i < periods; i++) {
2544 sg_dma_address(&sg[i]) = dma_addr;
2545 sg_dma_len(&sg[i]) = period_len;
2546 dma_addr += period_len;
2547 }
2548
2549 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002550 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002551 sg[periods].page_link =
2552 ((unsigned long)sg | 0x01) & ~0x02;
2553
2554 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2555 DMA_PREP_INTERRUPT);
2556
2557 kfree(sg);
2558
2559 return txd;
2560}
2561
Linus Walleij8d318a52010-03-30 15:33:42 +02002562static enum dma_status d40_tx_status(struct dma_chan *chan,
2563 dma_cookie_t cookie,
2564 struct dma_tx_state *txstate)
2565{
2566 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002567 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002568
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002569 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002570 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002571 return -EINVAL;
2572 }
2573
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002574 ret = dma_cookie_status(chan, cookie, txstate);
2575 if (ret != DMA_SUCCESS)
2576 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002577
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002578 if (d40_is_paused(d40c))
2579 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002580
2581 return ret;
2582}
2583
2584static void d40_issue_pending(struct dma_chan *chan)
2585{
2586 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2587 unsigned long flags;
2588
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002589 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002590 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002591 return;
2592 }
2593
Linus Walleij8d318a52010-03-30 15:33:42 +02002594 spin_lock_irqsave(&d40c->lock, flags);
2595
Per Forlina8f30672011-06-26 23:29:52 +02002596 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2597
2598 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002599 if (!d40c->busy)
2600 (void) d40_queue_start(d40c);
2601
2602 spin_unlock_irqrestore(&d40c->lock, flags);
2603}
2604
Narayanan G1bdae6f2012-02-09 12:41:37 +05302605static void d40_terminate_all(struct dma_chan *chan)
2606{
2607 unsigned long flags;
2608 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2609 int ret;
2610
2611 spin_lock_irqsave(&d40c->lock, flags);
2612
2613 pm_runtime_get_sync(d40c->base->dev);
2614 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2615 if (ret)
2616 chan_err(d40c, "Failed to stop channel\n");
2617
2618 d40_term_all(d40c);
2619 pm_runtime_mark_last_busy(d40c->base->dev);
2620 pm_runtime_put_autosuspend(d40c->base->dev);
2621 if (d40c->busy) {
2622 pm_runtime_mark_last_busy(d40c->base->dev);
2623 pm_runtime_put_autosuspend(d40c->base->dev);
2624 }
2625 d40c->busy = false;
2626
2627 spin_unlock_irqrestore(&d40c->lock, flags);
2628}
2629
Rabin Vincent98ca5282011-06-27 11:33:38 +02002630static int
2631dma40_config_to_halfchannel(struct d40_chan *d40c,
2632 struct stedma40_half_channel_info *info,
2633 enum dma_slave_buswidth width,
2634 u32 maxburst)
2635{
2636 enum stedma40_periph_data_width addr_width;
2637 int psize;
2638
2639 switch (width) {
2640 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2641 addr_width = STEDMA40_BYTE_WIDTH;
2642 break;
2643 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2644 addr_width = STEDMA40_HALFWORD_WIDTH;
2645 break;
2646 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2647 addr_width = STEDMA40_WORD_WIDTH;
2648 break;
2649 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2650 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2651 break;
2652 default:
2653 dev_err(d40c->base->dev,
2654 "illegal peripheral address width "
2655 "requested (%d)\n",
2656 width);
2657 return -EINVAL;
2658 }
2659
2660 if (chan_is_logical(d40c)) {
2661 if (maxburst >= 16)
2662 psize = STEDMA40_PSIZE_LOG_16;
2663 else if (maxburst >= 8)
2664 psize = STEDMA40_PSIZE_LOG_8;
2665 else if (maxburst >= 4)
2666 psize = STEDMA40_PSIZE_LOG_4;
2667 else
2668 psize = STEDMA40_PSIZE_LOG_1;
2669 } else {
2670 if (maxburst >= 16)
2671 psize = STEDMA40_PSIZE_PHY_16;
2672 else if (maxburst >= 8)
2673 psize = STEDMA40_PSIZE_PHY_8;
2674 else if (maxburst >= 4)
2675 psize = STEDMA40_PSIZE_PHY_4;
2676 else
2677 psize = STEDMA40_PSIZE_PHY_1;
2678 }
2679
2680 info->data_width = addr_width;
2681 info->psize = psize;
2682 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2683
2684 return 0;
2685}
2686
Linus Walleij95e14002010-08-04 13:37:45 +02002687/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002688static int d40_set_runtime_config(struct dma_chan *chan,
2689 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002690{
2691 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2692 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002693 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002694 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002695 u32 src_maxburst, dst_maxburst;
2696 int ret;
2697
2698 src_addr_width = config->src_addr_width;
2699 src_maxburst = config->src_maxburst;
2700 dst_addr_width = config->dst_addr_width;
2701 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002702
Vinod Kouldb8196d2011-10-13 22:34:23 +05302703 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002704 dma_addr_t dev_addr_rx =
2705 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2706
2707 config_addr = config->src_addr;
2708 if (dev_addr_rx)
2709 dev_dbg(d40c->base->dev,
2710 "channel has a pre-wired RX address %08x "
2711 "overriding with %08x\n",
2712 dev_addr_rx, config_addr);
2713 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2714 dev_dbg(d40c->base->dev,
2715 "channel was not configured for peripheral "
2716 "to memory transfer (%d) overriding\n",
2717 cfg->dir);
2718 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2719
Rabin Vincent98ca5282011-06-27 11:33:38 +02002720 /* Configure the memory side */
2721 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2722 dst_addr_width = src_addr_width;
2723 if (dst_maxburst == 0)
2724 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002725
Vinod Kouldb8196d2011-10-13 22:34:23 +05302726 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002727 dma_addr_t dev_addr_tx =
2728 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2729
2730 config_addr = config->dst_addr;
2731 if (dev_addr_tx)
2732 dev_dbg(d40c->base->dev,
2733 "channel has a pre-wired TX address %08x "
2734 "overriding with %08x\n",
2735 dev_addr_tx, config_addr);
2736 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2737 dev_dbg(d40c->base->dev,
2738 "channel was not configured for memory "
2739 "to peripheral transfer (%d) overriding\n",
2740 cfg->dir);
2741 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2742
Rabin Vincent98ca5282011-06-27 11:33:38 +02002743 /* Configure the memory side */
2744 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2745 src_addr_width = dst_addr_width;
2746 if (src_maxburst == 0)
2747 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002748 } else {
2749 dev_err(d40c->base->dev,
2750 "unrecognized channel direction %d\n",
2751 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002752 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002753 }
2754
Rabin Vincent98ca5282011-06-27 11:33:38 +02002755 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002756 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002757 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2758 src_maxburst,
2759 src_addr_width,
2760 dst_maxburst,
2761 dst_addr_width);
2762 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002763 }
2764
Per Forlin92bb6cd2011-10-13 12:11:36 +02002765 if (src_maxburst > 16) {
2766 src_maxburst = 16;
2767 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2768 } else if (dst_maxburst > 16) {
2769 dst_maxburst = 16;
2770 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2771 }
2772
Rabin Vincent98ca5282011-06-27 11:33:38 +02002773 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2774 src_addr_width,
2775 src_maxburst);
2776 if (ret)
2777 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002778
Rabin Vincent98ca5282011-06-27 11:33:38 +02002779 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2780 dst_addr_width,
2781 dst_maxburst);
2782 if (ret)
2783 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002784
Per Forlina59670a2010-10-06 09:05:27 +00002785 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002786 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002787 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2788 else
2789 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2790 &d40c->dst_def_cfg, false);
2791
Linus Walleij95e14002010-08-04 13:37:45 +02002792 /* These settings will take precedence later */
2793 d40c->runtime_addr = config_addr;
2794 d40c->runtime_direction = config->direction;
2795 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002796 "configured channel %s for %s, data width %d/%d, "
2797 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002798 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302799 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002800 src_addr_width, dst_addr_width,
2801 src_maxburst, dst_maxburst);
2802
2803 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002804}
2805
Linus Walleij05827632010-05-17 16:30:42 -07002806static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2807 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002808{
Linus Walleij8d318a52010-03-30 15:33:42 +02002809 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2810
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002811 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002812 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002813 return -EINVAL;
2814 }
2815
Linus Walleij8d318a52010-03-30 15:33:42 +02002816 switch (cmd) {
2817 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302818 d40_terminate_all(chan);
2819 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002820 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002821 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002822 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002823 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002824 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002825 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002826 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002827 default:
2828 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002829 }
2830
2831 /* Other commands are unimplemented */
2832 return -ENXIO;
2833}
2834
2835/* Initialization functions */
2836
2837static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2838 struct d40_chan *chans, int offset,
2839 int num_chans)
2840{
2841 int i = 0;
2842 struct d40_chan *d40c;
2843
2844 INIT_LIST_HEAD(&dma->channels);
2845
2846 for (i = offset; i < offset + num_chans; i++) {
2847 d40c = &chans[i];
2848 d40c->base = base;
2849 d40c->chan.device = dma;
2850
Linus Walleij8d318a52010-03-30 15:33:42 +02002851 spin_lock_init(&d40c->lock);
2852
2853 d40c->log_num = D40_PHY_CHAN;
2854
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002855 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002856 INIT_LIST_HEAD(&d40c->active);
2857 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002858 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002859 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002860 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002861
Linus Walleij8d318a52010-03-30 15:33:42 +02002862 tasklet_init(&d40c->tasklet, dma_tasklet,
2863 (unsigned long) d40c);
2864
2865 list_add_tail(&d40c->chan.device_node,
2866 &dma->channels);
2867 }
2868}
2869
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002870static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2871{
2872 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2873 dev->device_prep_slave_sg = d40_prep_slave_sg;
2874
2875 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2876 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2877
2878 /*
2879 * This controller can only access address at even
2880 * 32bit boundaries, i.e. 2^2
2881 */
2882 dev->copy_align = 2;
2883 }
2884
2885 if (dma_has_cap(DMA_SG, dev->cap_mask))
2886 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2887
Rabin Vincent0c842b52011-01-25 11:18:35 +01002888 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2889 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2890
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002891 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2892 dev->device_free_chan_resources = d40_free_chan_resources;
2893 dev->device_issue_pending = d40_issue_pending;
2894 dev->device_tx_status = d40_tx_status;
2895 dev->device_control = d40_control;
2896 dev->dev = base->dev;
2897}
2898
Linus Walleij8d318a52010-03-30 15:33:42 +02002899static int __init d40_dmaengine_init(struct d40_base *base,
2900 int num_reserved_chans)
2901{
2902 int err ;
2903
2904 d40_chan_init(base, &base->dma_slave, base->log_chans,
2905 0, base->num_log_chans);
2906
2907 dma_cap_zero(base->dma_slave.cap_mask);
2908 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002909 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002910
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002911 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002912
2913 err = dma_async_device_register(&base->dma_slave);
2914
2915 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002916 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002917 goto failure1;
2918 }
2919
2920 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2921 base->num_log_chans, base->plat_data->memcpy_len);
2922
2923 dma_cap_zero(base->dma_memcpy.cap_mask);
2924 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002925 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002926
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002927 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002928
2929 err = dma_async_device_register(&base->dma_memcpy);
2930
2931 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002932 d40_err(base->dev,
2933 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002934 goto failure2;
2935 }
2936
2937 d40_chan_init(base, &base->dma_both, base->phy_chans,
2938 0, num_reserved_chans);
2939
2940 dma_cap_zero(base->dma_both.cap_mask);
2941 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2942 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002943 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002944 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002945
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002946 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002947 err = dma_async_device_register(&base->dma_both);
2948
2949 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002950 d40_err(base->dev,
2951 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002952 goto failure3;
2953 }
2954 return 0;
2955failure3:
2956 dma_async_device_unregister(&base->dma_memcpy);
2957failure2:
2958 dma_async_device_unregister(&base->dma_slave);
2959failure1:
2960 return err;
2961}
2962
Narayanan G7fb3e752011-11-17 17:26:41 +05302963/* Suspend resume functionality */
2964#ifdef CONFIG_PM
2965static int dma40_pm_suspend(struct device *dev)
2966{
Narayanan G28c7a192011-11-22 13:56:55 +05302967 struct platform_device *pdev = to_platform_device(dev);
2968 struct d40_base *base = platform_get_drvdata(pdev);
2969 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302970
Narayanan G28c7a192011-11-22 13:56:55 +05302971 if (base->lcpa_regulator)
2972 ret = regulator_disable(base->lcpa_regulator);
2973 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302974}
2975
2976static int dma40_runtime_suspend(struct device *dev)
2977{
2978 struct platform_device *pdev = to_platform_device(dev);
2979 struct d40_base *base = platform_get_drvdata(pdev);
2980
2981 d40_save_restore_registers(base, true);
2982
2983 /* Don't disable/enable clocks for v1 due to HW bugs */
2984 if (base->rev != 1)
2985 writel_relaxed(base->gcc_pwr_off_mask,
2986 base->virtbase + D40_DREG_GCC);
2987
2988 return 0;
2989}
2990
2991static int dma40_runtime_resume(struct device *dev)
2992{
2993 struct platform_device *pdev = to_platform_device(dev);
2994 struct d40_base *base = platform_get_drvdata(pdev);
2995
2996 if (base->initialized)
2997 d40_save_restore_registers(base, false);
2998
2999 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3000 base->virtbase + D40_DREG_GCC);
3001 return 0;
3002}
3003
Narayanan G28c7a192011-11-22 13:56:55 +05303004static int dma40_resume(struct device *dev)
3005{
3006 struct platform_device *pdev = to_platform_device(dev);
3007 struct d40_base *base = platform_get_drvdata(pdev);
3008 int ret = 0;
3009
3010 if (base->lcpa_regulator)
3011 ret = regulator_enable(base->lcpa_regulator);
3012
3013 return ret;
3014}
Narayanan G7fb3e752011-11-17 17:26:41 +05303015
3016static const struct dev_pm_ops dma40_pm_ops = {
3017 .suspend = dma40_pm_suspend,
3018 .runtime_suspend = dma40_runtime_suspend,
3019 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303020 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303021};
3022#define DMA40_PM_OPS (&dma40_pm_ops)
3023#else
3024#define DMA40_PM_OPS NULL
3025#endif
3026
Linus Walleij8d318a52010-03-30 15:33:42 +02003027/* Initialization functions. */
3028
3029static int __init d40_phy_res_init(struct d40_base *base)
3030{
3031 int i;
3032 int num_phy_chans_avail = 0;
3033 u32 val[2];
3034 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303035 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003036
3037 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3038 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3039
3040 for (i = 0; i < base->num_phy_chans; i++) {
3041 base->phy_res[i].num = i;
3042 odd_even_bit += 2 * ((i % 2) == 0);
3043 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3044 /* Mark security only channels as occupied */
3045 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3046 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303047 base->phy_res[i].reserved = true;
3048 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3049 D40_DREG_GCC_SRC);
3050 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3051 D40_DREG_GCC_DST);
3052
3053
Linus Walleij8d318a52010-03-30 15:33:42 +02003054 } else {
3055 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3056 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303057 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003058 num_phy_chans_avail++;
3059 }
3060 spin_lock_init(&base->phy_res[i].lock);
3061 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003062
3063 /* Mark disabled channels as occupied */
3064 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003065 int chan = base->plat_data->disabled_channels[i];
3066
3067 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3068 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303069 base->phy_res[chan].reserved = true;
3070 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3071 D40_DREG_GCC_SRC);
3072 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3073 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003074 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003075 }
3076
Fabio Baltieri74070482012-12-18 12:25:14 +01003077 /* Mark soft_lli channels */
3078 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3079 int chan = base->plat_data->soft_lli_chans[i];
3080
3081 base->phy_res[chan].use_soft_lli = true;
3082 }
3083
Linus Walleij8d318a52010-03-30 15:33:42 +02003084 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3085 num_phy_chans_avail, base->num_phy_chans);
3086
3087 /* Verify settings extended vs standard */
3088 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3089
3090 for (i = 0; i < base->num_phy_chans; i++) {
3091
3092 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3093 (val[0] & 0x3) != 1)
3094 dev_info(base->dev,
3095 "[%s] INFO: channel %d is misconfigured (%d)\n",
3096 __func__, i, val[0] & 0x3);
3097
3098 val[0] = val[0] >> 2;
3099 }
3100
Narayanan G7fb3e752011-11-17 17:26:41 +05303101 /*
3102 * To keep things simple, Enable all clocks initially.
3103 * The clocks will get managed later post channel allocation.
3104 * The clocks for the event lines on which reserved channels exists
3105 * are not managed here.
3106 */
3107 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3108 base->gcc_pwr_off_mask = gcc;
3109
Linus Walleij8d318a52010-03-30 15:33:42 +02003110 return num_phy_chans_avail;
3111}
3112
3113static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3114{
Linus Walleij8d318a52010-03-30 15:33:42 +02003115 struct stedma40_platform_data *plat_data;
3116 struct clk *clk = NULL;
3117 void __iomem *virtbase = NULL;
3118 struct resource *res = NULL;
3119 struct d40_base *base = NULL;
3120 int num_log_chans = 0;
3121 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003122 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003123 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003124 u32 pid;
3125 u32 cid;
3126 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003127
3128 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003129 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003130 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003131 goto failure;
3132 }
3133
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003134 clk_ret = clk_prepare_enable(clk);
3135 if (clk_ret) {
3136 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3137 goto failure;
3138 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003139
3140 /* Get IO for DMAC base address */
3141 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3142 if (!res)
3143 goto failure;
3144
3145 if (request_mem_region(res->start, resource_size(res),
3146 D40_NAME " I/O base") == NULL)
3147 goto failure;
3148
3149 virtbase = ioremap(res->start, resource_size(res));
3150 if (!virtbase)
3151 goto failure;
3152
Linus Walleijf4b89762011-06-27 11:33:46 +02003153 /* This is just a regular AMBA PrimeCell ID actually */
3154 for (pid = 0, i = 0; i < 4; i++)
3155 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3156 & 255) << (i * 8);
3157 for (cid = 0, i = 0; i < 4; i++)
3158 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3159 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003160
Linus Walleijf4b89762011-06-27 11:33:46 +02003161 if (cid != AMBA_CID) {
3162 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003163 goto failure;
3164 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003165 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3166 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3167 AMBA_MANF_BITS(pid),
3168 AMBA_VENDOR_ST);
3169 goto failure;
3170 }
3171 /*
3172 * HW revision:
3173 * DB8500ed has revision 0
3174 * ? has revision 1
3175 * DB8500v1 has revision 2
3176 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003177 * AP9540v1 has revision 4
3178 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003179 */
3180 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003181
Gerald Baeza47db92f2012-09-21 21:21:37 +02003182 plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003183
Gerald Baeza47db92f2012-09-21 21:21:37 +02003184 /* The number of physical channels on this HW */
3185 if (plat_data->num_of_phy_chans)
3186 num_phy_chans = plat_data->num_of_phy_chans;
3187 else
3188 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3189
3190 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x with %d physical channels\n",
3191 rev, res->start, num_phy_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003192
Narayanan G1bdae6f2012-02-09 12:41:37 +05303193 if (rev < 2) {
3194 d40_err(&pdev->dev, "hardware revision: %d is not supported",
3195 rev);
3196 goto failure;
3197 }
3198
Linus Walleij8d318a52010-03-30 15:33:42 +02003199 /* Count the number of logical channels in use */
3200 for (i = 0; i < plat_data->dev_len; i++)
3201 if (plat_data->dev_rx[i] != 0)
3202 num_log_chans++;
3203
3204 for (i = 0; i < plat_data->dev_len; i++)
3205 if (plat_data->dev_tx[i] != 0)
3206 num_log_chans++;
3207
3208 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
3209 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
3210 sizeof(struct d40_chan), GFP_KERNEL);
3211
3212 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003213 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003214 goto failure;
3215 }
3216
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003217 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003218 base->clk = clk;
3219 base->num_phy_chans = num_phy_chans;
3220 base->num_log_chans = num_log_chans;
3221 base->phy_start = res->start;
3222 base->phy_size = resource_size(res);
3223 base->virtbase = virtbase;
3224 base->plat_data = plat_data;
3225 base->dev = &pdev->dev;
3226 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3227 base->log_chans = &base->phy_chans[num_phy_chans];
3228
Tong Liu3cb645d2012-09-26 10:07:30 +00003229 if (base->plat_data->num_of_phy_chans == 14) {
3230 base->gen_dmac.backup = d40_backup_regs_v4b;
3231 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3232 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3233 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3234 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3235 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3236 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3237 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3238 base->gen_dmac.il = il_v4b;
3239 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3240 base->gen_dmac.init_reg = dma_init_reg_v4b;
3241 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3242 } else {
3243 if (base->rev >= 3) {
3244 base->gen_dmac.backup = d40_backup_regs_v4a;
3245 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3246 }
3247 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3248 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3249 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3250 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3251 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3252 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3253 base->gen_dmac.il = il_v4a;
3254 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3255 base->gen_dmac.init_reg = dma_init_reg_v4a;
3256 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3257 }
3258
Linus Walleij8d318a52010-03-30 15:33:42 +02003259 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3260 GFP_KERNEL);
3261 if (!base->phy_res)
3262 goto failure;
3263
3264 base->lookup_phy_chans = kzalloc(num_phy_chans *
3265 sizeof(struct d40_chan *),
3266 GFP_KERNEL);
3267 if (!base->lookup_phy_chans)
3268 goto failure;
3269
3270 if (num_log_chans + plat_data->memcpy_len) {
3271 /*
3272 * The max number of logical channels are event lines for all
3273 * src devices and dst devices
3274 */
3275 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
3276 sizeof(struct d40_chan *),
3277 GFP_KERNEL);
3278 if (!base->lookup_log_chans)
3279 goto failure;
3280 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00003281
Narayanan G7fb3e752011-11-17 17:26:41 +05303282 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3283 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003284 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303285 if (!base->reg_val_backup_chan)
3286 goto failure;
3287
3288 base->lcla_pool.alloc_map =
3289 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3290 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003291 if (!base->lcla_pool.alloc_map)
3292 goto failure;
3293
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003294 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3295 0, SLAB_HWCACHE_ALIGN,
3296 NULL);
3297 if (base->desc_slab == NULL)
3298 goto failure;
3299
Linus Walleij8d318a52010-03-30 15:33:42 +02003300 return base;
3301
3302failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003303 if (!clk_ret)
3304 clk_disable_unprepare(clk);
3305 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003306 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003307 if (virtbase)
3308 iounmap(virtbase);
3309 if (res)
3310 release_mem_region(res->start,
3311 resource_size(res));
3312 if (virtbase)
3313 iounmap(virtbase);
3314
3315 if (base) {
3316 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303317 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003318 kfree(base->lookup_log_chans);
3319 kfree(base->lookup_phy_chans);
3320 kfree(base->phy_res);
3321 kfree(base);
3322 }
3323
3324 return NULL;
3325}
3326
3327static void __init d40_hw_init(struct d40_base *base)
3328{
3329
Linus Walleij8d318a52010-03-30 15:33:42 +02003330 int i;
3331 u32 prmseo[2] = {0, 0};
3332 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3333 u32 pcmis = 0;
3334 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003335 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3336 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003337
Tong Liu3cb645d2012-09-26 10:07:30 +00003338 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003339 writel(dma_init_reg[i].val,
3340 base->virtbase + dma_init_reg[i].reg);
3341
3342 /* Configure all our dma channels to default settings */
3343 for (i = 0; i < base->num_phy_chans; i++) {
3344
3345 activeo[i % 2] = activeo[i % 2] << 2;
3346
3347 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3348 == D40_ALLOC_PHY) {
3349 activeo[i % 2] |= 3;
3350 continue;
3351 }
3352
3353 /* Enable interrupt # */
3354 pcmis = (pcmis << 1) | 1;
3355
3356 /* Clear interrupt # */
3357 pcicr = (pcicr << 1) | 1;
3358
3359 /* Set channel to physical mode */
3360 prmseo[i % 2] = prmseo[i % 2] << 2;
3361 prmseo[i % 2] |= 1;
3362
3363 }
3364
3365 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3366 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3367 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3368 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3369
3370 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003371 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003372
3373 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003374 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003375
Tong Liu3cb645d2012-09-26 10:07:30 +00003376 /* These are __initdata and cannot be accessed after init */
3377 base->gen_dmac.init_reg = NULL;
3378 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003379}
3380
Linus Walleij508849a2010-06-20 21:26:07 +00003381static int __init d40_lcla_allocate(struct d40_base *base)
3382{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003383 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003384 unsigned long *page_list;
3385 int i, j;
3386 int ret = 0;
3387
3388 /*
3389 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3390 * To full fill this hardware requirement without wasting 256 kb
3391 * we allocate pages until we get an aligned one.
3392 */
3393 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3394 GFP_KERNEL);
3395
3396 if (!page_list) {
3397 ret = -ENOMEM;
3398 goto failure;
3399 }
3400
3401 /* Calculating how many pages that are required */
3402 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3403
3404 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3405 page_list[i] = __get_free_pages(GFP_KERNEL,
3406 base->lcla_pool.pages);
3407 if (!page_list[i]) {
3408
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003409 d40_err(base->dev, "Failed to allocate %d pages.\n",
3410 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003411
3412 for (j = 0; j < i; j++)
3413 free_pages(page_list[j], base->lcla_pool.pages);
3414 goto failure;
3415 }
3416
3417 if ((virt_to_phys((void *)page_list[i]) &
3418 (LCLA_ALIGNMENT - 1)) == 0)
3419 break;
3420 }
3421
3422 for (j = 0; j < i; j++)
3423 free_pages(page_list[j], base->lcla_pool.pages);
3424
3425 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3426 base->lcla_pool.base = (void *)page_list[i];
3427 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003428 /*
3429 * After many attempts and no succees with finding the correct
3430 * alignment, try with allocating a big buffer.
3431 */
Linus Walleij508849a2010-06-20 21:26:07 +00003432 dev_warn(base->dev,
3433 "[%s] Failed to get %d pages @ 18 bit align.\n",
3434 __func__, base->lcla_pool.pages);
3435 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3436 base->num_phy_chans +
3437 LCLA_ALIGNMENT,
3438 GFP_KERNEL);
3439 if (!base->lcla_pool.base_unaligned) {
3440 ret = -ENOMEM;
3441 goto failure;
3442 }
3443
3444 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3445 LCLA_ALIGNMENT);
3446 }
3447
Rabin Vincent026cbc42011-01-25 11:18:14 +01003448 pool->dma_addr = dma_map_single(base->dev, pool->base,
3449 SZ_1K * base->num_phy_chans,
3450 DMA_TO_DEVICE);
3451 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3452 pool->dma_addr = 0;
3453 ret = -ENOMEM;
3454 goto failure;
3455 }
3456
Linus Walleij508849a2010-06-20 21:26:07 +00003457 writel(virt_to_phys(base->lcla_pool.base),
3458 base->virtbase + D40_DREG_LCLA);
3459failure:
3460 kfree(page_list);
3461 return ret;
3462}
3463
Linus Walleij8d318a52010-03-30 15:33:42 +02003464static int __init d40_probe(struct platform_device *pdev)
3465{
3466 int err;
3467 int ret = -ENOENT;
3468 struct d40_base *base;
3469 struct resource *res = NULL;
3470 int num_reserved_chans;
3471 u32 val;
3472
3473 base = d40_hw_detect_init(pdev);
3474
3475 if (!base)
3476 goto failure;
3477
3478 num_reserved_chans = d40_phy_res_init(base);
3479
3480 platform_set_drvdata(pdev, base);
3481
3482 spin_lock_init(&base->interrupt_lock);
3483 spin_lock_init(&base->execmd_lock);
3484
3485 /* Get IO for logical channel parameter address */
3486 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3487 if (!res) {
3488 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003489 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003490 goto failure;
3491 }
3492 base->lcpa_size = resource_size(res);
3493 base->phy_lcpa = res->start;
3494
3495 if (request_mem_region(res->start, resource_size(res),
3496 D40_NAME " I/O lcpa") == NULL) {
3497 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003498 d40_err(&pdev->dev,
3499 "Failed to request LCPA region 0x%x-0x%x\n",
3500 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003501 goto failure;
3502 }
3503
3504 /* We make use of ESRAM memory for this. */
3505 val = readl(base->virtbase + D40_DREG_LCPA);
3506 if (res->start != val && val != 0) {
3507 dev_warn(&pdev->dev,
3508 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3509 __func__, val, res->start);
3510 } else
3511 writel(res->start, base->virtbase + D40_DREG_LCPA);
3512
3513 base->lcpa_base = ioremap(res->start, resource_size(res));
3514 if (!base->lcpa_base) {
3515 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003516 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003517 goto failure;
3518 }
Narayanan G28c7a192011-11-22 13:56:55 +05303519 /* If lcla has to be located in ESRAM we don't need to allocate */
3520 if (base->plat_data->use_esram_lcla) {
3521 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3522 "lcla_esram");
3523 if (!res) {
3524 ret = -ENOENT;
3525 d40_err(&pdev->dev,
3526 "No \"lcla_esram\" memory resource\n");
3527 goto failure;
3528 }
3529 base->lcla_pool.base = ioremap(res->start,
3530 resource_size(res));
3531 if (!base->lcla_pool.base) {
3532 ret = -ENOMEM;
3533 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3534 goto failure;
3535 }
3536 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003537
Narayanan G28c7a192011-11-22 13:56:55 +05303538 } else {
3539 ret = d40_lcla_allocate(base);
3540 if (ret) {
3541 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3542 goto failure;
3543 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003544 }
3545
Linus Walleij8d318a52010-03-30 15:33:42 +02003546 spin_lock_init(&base->lcla_pool.lock);
3547
Linus Walleij8d318a52010-03-30 15:33:42 +02003548 base->irq = platform_get_irq(pdev, 0);
3549
3550 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003551 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003552 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003553 goto failure;
3554 }
3555
Narayanan G7fb3e752011-11-17 17:26:41 +05303556 pm_runtime_irq_safe(base->dev);
3557 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3558 pm_runtime_use_autosuspend(base->dev);
3559 pm_runtime_enable(base->dev);
3560 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303561
3562 if (base->plat_data->use_esram_lcla) {
3563
3564 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3565 if (IS_ERR(base->lcpa_regulator)) {
3566 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3567 base->lcpa_regulator = NULL;
3568 goto failure;
3569 }
3570
3571 ret = regulator_enable(base->lcpa_regulator);
3572 if (ret) {
3573 d40_err(&pdev->dev,
3574 "Failed to enable lcpa_regulator\n");
3575 regulator_put(base->lcpa_regulator);
3576 base->lcpa_regulator = NULL;
3577 goto failure;
3578 }
3579 }
3580
Narayanan G7fb3e752011-11-17 17:26:41 +05303581 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003582 err = d40_dmaengine_init(base, num_reserved_chans);
3583 if (err)
3584 goto failure;
3585
Per Forlinb96710e2011-10-18 18:39:47 +02003586 base->dev->dma_parms = &base->dma_parms;
3587 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3588 if (err) {
3589 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3590 goto failure;
3591 }
3592
Linus Walleij8d318a52010-03-30 15:33:42 +02003593 d40_hw_init(base);
3594
3595 dev_info(base->dev, "initialized\n");
3596 return 0;
3597
3598failure:
3599 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003600 if (base->desc_slab)
3601 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003602 if (base->virtbase)
3603 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003604
Narayanan G28c7a192011-11-22 13:56:55 +05303605 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3606 iounmap(base->lcla_pool.base);
3607 base->lcla_pool.base = NULL;
3608 }
3609
Rabin Vincent026cbc42011-01-25 11:18:14 +01003610 if (base->lcla_pool.dma_addr)
3611 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3612 SZ_1K * base->num_phy_chans,
3613 DMA_TO_DEVICE);
3614
Linus Walleij508849a2010-06-20 21:26:07 +00003615 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3616 free_pages((unsigned long)base->lcla_pool.base,
3617 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003618
3619 kfree(base->lcla_pool.base_unaligned);
3620
Linus Walleij8d318a52010-03-30 15:33:42 +02003621 if (base->phy_lcpa)
3622 release_mem_region(base->phy_lcpa,
3623 base->lcpa_size);
3624 if (base->phy_start)
3625 release_mem_region(base->phy_start,
3626 base->phy_size);
3627 if (base->clk) {
3628 clk_disable(base->clk);
3629 clk_put(base->clk);
3630 }
3631
Narayanan G28c7a192011-11-22 13:56:55 +05303632 if (base->lcpa_regulator) {
3633 regulator_disable(base->lcpa_regulator);
3634 regulator_put(base->lcpa_regulator);
3635 }
3636
Linus Walleij8d318a52010-03-30 15:33:42 +02003637 kfree(base->lcla_pool.alloc_map);
3638 kfree(base->lookup_log_chans);
3639 kfree(base->lookup_phy_chans);
3640 kfree(base->phy_res);
3641 kfree(base);
3642 }
3643
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003644 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003645 return ret;
3646}
3647
3648static struct platform_driver d40_driver = {
3649 .driver = {
3650 .owner = THIS_MODULE,
3651 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303652 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003653 },
3654};
3655
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003656static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003657{
3658 return platform_driver_probe(&d40_driver, d40_probe);
3659}
Linus Walleija0eb2212011-05-18 14:18:57 +02003660subsys_initcall(stedma40_init);