blob: 67e565bffe16d3a9ff07fe988b6ccb799df1563e [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.
Linus Walleij8d318a52010-03-30 15:33:42 +0200358 */
359struct d40_phy_res {
360 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530361 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200362 int num;
363 u32 allocated_src;
364 u32 allocated_dst;
365};
366
367struct d40_base;
368
369/**
370 * struct d40_chan - Struct that describes a channel.
371 *
372 * @lock: A spinlock to protect this struct.
373 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200374 * @pending_tx: The number of pending transfers. Used between interrupt handler
375 * and tasklet.
376 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000377 * @phy_chan: Pointer to physical channel which this instance runs on. If this
378 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200379 * @chan: DMA engine handle.
380 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
381 * transfer and call client callback.
382 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200383 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200384 * @active: Active descriptor.
385 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200386 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200387 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000388 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200389 * @base: Pointer to the device instance struct.
390 * @src_def_cfg: Default cfg register setting for src.
391 * @dst_def_cfg: Default cfg register setting for dst.
392 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200393 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200394 * @runtime_addr: runtime configured address.
395 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200396 *
397 * This struct can either "be" a logical or a physical channel.
398 */
399struct d40_chan {
400 spinlock_t lock;
401 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200402 int pending_tx;
403 bool busy;
404 struct d40_phy_res *phy_chan;
405 struct dma_chan chan;
406 struct tasklet_struct tasklet;
407 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200408 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200409 struct list_head active;
410 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200411 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200412 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000413 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200414 struct d40_base *base;
415 /* Default register configurations */
416 u32 src_def_cfg;
417 u32 dst_def_cfg;
418 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200419 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200420 /* Runtime reconfiguration */
421 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530422 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200423};
424
425/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000426 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
427 * controller
428 *
429 * @backup: the pointer to the registers address array for backup
430 * @backup_size: the size of the registers address array for backup
431 * @realtime_en: the realtime enable register
432 * @realtime_clear: the realtime clear register
433 * @high_prio_en: the high priority enable register
434 * @high_prio_clear: the high priority clear register
435 * @interrupt_en: the interrupt enable register
436 * @interrupt_clear: the interrupt clear register
437 * @il: the pointer to struct d40_interrupt_lookup
438 * @il_size: the size of d40_interrupt_lookup array
439 * @init_reg: the pointer to the struct d40_reg_val
440 * @init_reg_size: the size of d40_reg_val array
441 */
442struct d40_gen_dmac {
443 u32 *backup;
444 u32 backup_size;
445 u32 realtime_en;
446 u32 realtime_clear;
447 u32 high_prio_en;
448 u32 high_prio_clear;
449 u32 interrupt_en;
450 u32 interrupt_clear;
451 struct d40_interrupt_lookup *il;
452 u32 il_size;
453 struct d40_reg_val *init_reg;
454 u32 init_reg_size;
455};
456
457/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200458 * struct d40_base - The big global struct, one for each probe'd instance.
459 *
460 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
461 * @execmd_lock: Lock for execute command usage since several channels share
462 * the same physical register.
463 * @dev: The device structure.
464 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700465 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200466 * @clk: Pointer to the DMA clock structure.
467 * @phy_start: Physical memory start of the DMA registers.
468 * @phy_size: Size of the DMA register map.
469 * @irq: The IRQ number.
470 * @num_phy_chans: The number of physical channels. Read from HW. This
471 * is the number of available channels for this driver, not counting "Secure
472 * mode" allocated physical channels.
473 * @num_log_chans: The number of logical channels. Calculated from
474 * num_phy_chans.
475 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
476 * @dma_slave: dma_device channels that can do only do slave transfers.
477 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530478 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200479 * @log_chans: Room for all possible logical channels in system.
480 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
481 * to log_chans entries.
482 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
483 * to phy_chans entries.
484 * @plat_data: Pointer to provided platform_data which is the driver
485 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530486 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200487 * @phy_res: Vector containing all physical channels.
488 * @lcla_pool: lcla pool settings and data.
489 * @lcpa_base: The virtual mapped address of LCPA.
490 * @phy_lcpa: The physical address of the LCPA.
491 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000492 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530493 * @reg_val_backup: Here the values of some hardware registers are stored
494 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000495 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
496 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530497 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
498 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
499 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000500 * @gen_dmac: the struct for generic registers values to represent u8500/8540
501 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200502 */
503struct d40_base {
504 spinlock_t interrupt_lock;
505 spinlock_t execmd_lock;
506 struct device *dev;
507 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700508 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200509 struct clk *clk;
510 phys_addr_t phy_start;
511 resource_size_t phy_size;
512 int irq;
513 int num_phy_chans;
514 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200515 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200516 struct dma_device dma_both;
517 struct dma_device dma_slave;
518 struct dma_device dma_memcpy;
519 struct d40_chan *phy_chans;
520 struct d40_chan *log_chans;
521 struct d40_chan **lookup_log_chans;
522 struct d40_chan **lookup_phy_chans;
523 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530524 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200525 /* Physical half channels */
526 struct d40_phy_res *phy_res;
527 struct d40_lcla_pool lcla_pool;
528 void *lcpa_base;
529 dma_addr_t phy_lcpa;
530 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000531 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530532 u32 reg_val_backup[BACKUP_REGS_SZ];
Tong Liu3cb645d2012-09-26 10:07:30 +0000533 u32 reg_val_backup_v4[MAX(BACKUP_REGS_SZ_V4A, BACKUP_REGS_SZ_V4B)];
Narayanan G7fb3e752011-11-17 17:26:41 +0530534 u32 *reg_val_backup_chan;
535 u16 gcc_pwr_off_mask;
536 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000537 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200538};
539
Rabin Vincent262d2912011-01-25 11:18:05 +0100540static struct device *chan2dev(struct d40_chan *d40c)
541{
542 return &d40c->chan.dev->device;
543}
544
Rabin Vincent724a8572011-01-25 11:18:08 +0100545static bool chan_is_physical(struct d40_chan *chan)
546{
547 return chan->log_num == D40_PHY_CHAN;
548}
549
550static bool chan_is_logical(struct d40_chan *chan)
551{
552 return !chan_is_physical(chan);
553}
554
Rabin Vincent8ca84682011-01-25 11:18:07 +0100555static void __iomem *chan_base(struct d40_chan *chan)
556{
557 return chan->base->virtbase + D40_DREG_PCBASE +
558 chan->phy_chan->num * D40_DREG_PCDELTA;
559}
560
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100561#define d40_err(dev, format, arg...) \
562 dev_err(dev, "[%s] " format, __func__, ## arg)
563
564#define chan_err(d40c, format, arg...) \
565 d40_err(chan2dev(d40c), format, ## arg)
566
Rabin Vincentb00f9382011-01-25 11:18:15 +0100567static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100568 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200569{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100570 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200571 u32 align;
572 void *base;
573
574 if (is_log)
575 align = sizeof(struct d40_log_lli);
576 else
577 align = sizeof(struct d40_phy_lli);
578
579 if (lli_len == 1) {
580 base = d40d->lli_pool.pre_alloc_lli;
581 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
582 d40d->lli_pool.base = NULL;
583 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100584 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200585
586 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
587 d40d->lli_pool.base = base;
588
589 if (d40d->lli_pool.base == NULL)
590 return -ENOMEM;
591 }
592
593 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100594 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100595 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100596
597 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200598 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100599 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100600 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100601
602 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
603 d40d->lli_phy.src,
604 d40d->lli_pool.size,
605 DMA_TO_DEVICE);
606
607 if (dma_mapping_error(d40c->base->dev,
608 d40d->lli_pool.dma_addr)) {
609 kfree(d40d->lli_pool.base);
610 d40d->lli_pool.base = NULL;
611 d40d->lli_pool.dma_addr = 0;
612 return -ENOMEM;
613 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200614 }
615
616 return 0;
617}
618
Rabin Vincentb00f9382011-01-25 11:18:15 +0100619static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200620{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100621 if (d40d->lli_pool.dma_addr)
622 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
623 d40d->lli_pool.size, DMA_TO_DEVICE);
624
Linus Walleij8d318a52010-03-30 15:33:42 +0200625 kfree(d40d->lli_pool.base);
626 d40d->lli_pool.base = NULL;
627 d40d->lli_pool.size = 0;
628 d40d->lli_log.src = NULL;
629 d40d->lli_log.dst = NULL;
630 d40d->lli_phy.src = NULL;
631 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200632}
633
Jonas Aaberg698e4732010-08-09 12:08:56 +0000634static int d40_lcla_alloc_one(struct d40_chan *d40c,
635 struct d40_desc *d40d)
636{
637 unsigned long flags;
638 int i;
639 int ret = -EINVAL;
640 int p;
641
642 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
643
644 p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
645
646 /*
647 * Allocate both src and dst at the same time, therefore the half
648 * start on 1 since 0 can't be used since zero is used as end marker.
649 */
650 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
651 if (!d40c->base->lcla_pool.alloc_map[p + i]) {
652 d40c->base->lcla_pool.alloc_map[p + i] = d40d;
653 d40d->lcla_alloc++;
654 ret = i;
655 break;
656 }
657 }
658
659 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
660
661 return ret;
662}
663
664static int d40_lcla_free_all(struct d40_chan *d40c,
665 struct d40_desc *d40d)
666{
667 unsigned long flags;
668 int i;
669 int ret = -EINVAL;
670
Rabin Vincent724a8572011-01-25 11:18:08 +0100671 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000672 return 0;
673
674 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
675
676 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
677 if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
678 D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
679 d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
680 D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
681 d40d->lcla_alloc--;
682 if (d40d->lcla_alloc == 0) {
683 ret = 0;
684 break;
685 }
686 }
687 }
688
689 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
690
691 return ret;
692
693}
694
Linus Walleij8d318a52010-03-30 15:33:42 +0200695static void d40_desc_remove(struct d40_desc *d40d)
696{
697 list_del(&d40d->node);
698}
699
700static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
701{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000702 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200703
704 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000705 struct d40_desc *d;
706 struct d40_desc *_d;
707
Narayanan G7fb3e752011-11-17 17:26:41 +0530708 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200709 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200710 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000711 desc = d;
712 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000713 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200714 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530715 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200716 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000717
718 if (!desc)
719 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
720
721 if (desc)
722 INIT_LIST_HEAD(&desc->node);
723
724 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200725}
726
727static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
728{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000729
Rabin Vincentb00f9382011-01-25 11:18:15 +0100730 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000731 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000732 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200733}
734
735static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
736{
737 list_add_tail(&desc->node, &d40c->active);
738}
739
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100740static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
741{
742 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
743 struct d40_phy_lli *lli_src = desc->lli_phy.src;
744 void __iomem *base = chan_base(chan);
745
746 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
747 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
748 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
749 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
750
751 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
752 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
753 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
754 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
755}
756
Rabin Vincente65889c2011-01-25 11:18:31 +0100757static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
758{
759 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
760 struct d40_log_lli_bidir *lli = &desc->lli_log;
761 int lli_current = desc->lli_current;
762 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100763 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100764 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100765 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530766 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100767 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100768
Rabin Vincent0c842b52011-01-25 11:18:35 +0100769 /*
770 * We may have partially running cyclic transfers, in case we did't get
771 * enough LCLA entries.
772 */
773 linkback = cyclic && lli_current == 0;
774
775 /*
776 * For linkback, we need one LCLA even with only one link, because we
777 * can't link back to the one in LCPA space
778 */
779 if (linkback || (lli_len - lli_current > 1)) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100780 curr_lcla = d40_lcla_alloc_one(chan, desc);
Rabin Vincent0c842b52011-01-25 11:18:35 +0100781 first_lcla = curr_lcla;
782 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100783
Rabin Vincent0c842b52011-01-25 11:18:35 +0100784 /*
785 * For linkback, we normally load the LCPA in the loop since we need to
786 * link it to the second LCLA and not the first. However, if we
787 * couldn't even get a first LCLA, then we have to run in LCPA and
788 * reload manually.
789 */
790 if (!linkback || curr_lcla == -EINVAL) {
791 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100792
Rabin Vincent0c842b52011-01-25 11:18:35 +0100793 if (curr_lcla == -EINVAL)
794 flags |= LLI_TERM_INT;
795
796 d40_log_lli_lcpa_write(chan->lcpa,
797 &lli->dst[lli_current],
798 &lli->src[lli_current],
799 curr_lcla,
800 flags);
801 lli_current++;
802 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100803
804 if (curr_lcla < 0)
805 goto out;
806
Rabin Vincente65889c2011-01-25 11:18:31 +0100807 for (; lli_current < lli_len; lli_current++) {
808 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
809 8 * curr_lcla * 2;
810 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100811 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100812 int next_lcla;
813
814 if (lli_current + 1 < lli_len)
815 next_lcla = d40_lcla_alloc_one(chan, desc);
816 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100817 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100818
Rabin Vincent0c842b52011-01-25 11:18:35 +0100819 if (cyclic || next_lcla == -EINVAL)
820 flags |= LLI_TERM_INT;
821
822 if (linkback && curr_lcla == first_lcla) {
823 /* First link goes in both LCPA and LCLA */
824 d40_log_lli_lcpa_write(chan->lcpa,
825 &lli->dst[lli_current],
826 &lli->src[lli_current],
827 next_lcla, flags);
828 }
829
830 /*
831 * One unused LCLA in the cyclic case if the very first
832 * next_lcla fails...
833 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100834 d40_log_lli_lcla_write(lcla,
835 &lli->dst[lli_current],
836 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100837 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100838
Narayanan G28c7a192011-11-22 13:56:55 +0530839 /*
840 * Cache maintenance is not needed if lcla is
841 * mapped in esram
842 */
843 if (!use_esram_lcla) {
844 dma_sync_single_range_for_device(chan->base->dev,
845 pool->dma_addr, lcla_offset,
846 2 * sizeof(struct d40_log_lli),
847 DMA_TO_DEVICE);
848 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100849 curr_lcla = next_lcla;
850
Rabin Vincent0c842b52011-01-25 11:18:35 +0100851 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100852 lli_current++;
853 break;
854 }
855 }
856
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100857out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100858 desc->lli_current = lli_current;
859}
860
Jonas Aaberg698e4732010-08-09 12:08:56 +0000861static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
862{
Rabin Vincent724a8572011-01-25 11:18:08 +0100863 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100864 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000865 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100866 } else
867 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000868}
869
Linus Walleij8d318a52010-03-30 15:33:42 +0200870static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
871{
872 struct d40_desc *d;
873
874 if (list_empty(&d40c->active))
875 return NULL;
876
877 d = list_first_entry(&d40c->active,
878 struct d40_desc,
879 node);
880 return d;
881}
882
Per Forlin74043682011-08-29 13:33:34 +0200883/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200884static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
885{
Per Forlin74043682011-08-29 13:33:34 +0200886 d40_desc_remove(desc);
887 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200888 list_add_tail(&desc->node, &d40c->pending_queue);
889}
890
891static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
892{
893 struct d40_desc *d;
894
895 if (list_empty(&d40c->pending_queue))
896 return NULL;
897
898 d = list_first_entry(&d40c->pending_queue,
899 struct d40_desc,
900 node);
901 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200902}
903
904static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
905{
906 struct d40_desc *d;
907
908 if (list_empty(&d40c->queue))
909 return NULL;
910
911 d = list_first_entry(&d40c->queue,
912 struct d40_desc,
913 node);
914 return d;
915}
916
Per Forlind49278e2010-12-20 18:31:38 +0100917static int d40_psize_2_burst_size(bool is_log, int psize)
918{
919 if (is_log) {
920 if (psize == STEDMA40_PSIZE_LOG_1)
921 return 1;
922 } else {
923 if (psize == STEDMA40_PSIZE_PHY_1)
924 return 1;
925 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200926
Per Forlind49278e2010-12-20 18:31:38 +0100927 return 2 << psize;
928}
929
930/*
931 * The dma only supports transmitting packages up to
932 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
933 * dma elements required to send the entire sg list
934 */
935static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
936{
937 int dmalen;
938 u32 max_w = max(data_width1, data_width2);
939 u32 min_w = min(data_width1, data_width2);
940 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
941
942 if (seg_max > STEDMA40_MAX_SEG_SIZE)
943 seg_max -= (1 << max_w);
944
945 if (!IS_ALIGNED(size, 1 << max_w))
946 return -EINVAL;
947
948 if (size <= seg_max)
949 dmalen = 1;
950 else {
951 dmalen = size / seg_max;
952 if (dmalen * seg_max < size)
953 dmalen++;
954 }
955 return dmalen;
956}
957
958static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
959 u32 data_width1, u32 data_width2)
960{
961 struct scatterlist *sg;
962 int i;
963 int len = 0;
964 int ret;
965
966 for_each_sg(sgl, sg, sg_len, i) {
967 ret = d40_size_2_dmalen(sg_dma_len(sg),
968 data_width1, data_width2);
969 if (ret < 0)
970 return ret;
971 len += ret;
972 }
973 return len;
974}
975
Narayanan G7fb3e752011-11-17 17:26:41 +0530976
977#ifdef CONFIG_PM
978static void dma40_backup(void __iomem *baseaddr, u32 *backup,
979 u32 *regaddr, int num, bool save)
980{
981 int i;
982
983 for (i = 0; i < num; i++) {
984 void __iomem *addr = baseaddr + regaddr[i];
985
986 if (save)
987 backup[i] = readl_relaxed(addr);
988 else
989 writel_relaxed(backup[i], addr);
990 }
991}
992
993static void d40_save_restore_registers(struct d40_base *base, bool save)
994{
995 int i;
996
997 /* Save/Restore channel specific registers */
998 for (i = 0; i < base->num_phy_chans; i++) {
999 void __iomem *addr;
1000 int idx;
1001
1002 if (base->phy_res[i].reserved)
1003 continue;
1004
1005 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1006 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1007
1008 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1009 d40_backup_regs_chan,
1010 ARRAY_SIZE(d40_backup_regs_chan),
1011 save);
1012 }
1013
1014 /* Save/Restore global registers */
1015 dma40_backup(base->virtbase, base->reg_val_backup,
1016 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1017 save);
1018
1019 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001020 if (base->gen_dmac.backup)
1021 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1022 base->gen_dmac.backup,
1023 base->gen_dmac.backup_size,
1024 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301025}
1026#else
1027static void d40_save_restore_registers(struct d40_base *base, bool save)
1028{
1029}
1030#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001031
Narayanan G1bdae6f2012-02-09 12:41:37 +05301032static int __d40_execute_command_phy(struct d40_chan *d40c,
1033 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001034{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001035 u32 status;
1036 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001037 void __iomem *active_reg;
1038 int ret = 0;
1039 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001040 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001041
Narayanan G1bdae6f2012-02-09 12:41:37 +05301042 if (command == D40_DMA_STOP) {
1043 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1044 if (ret)
1045 return ret;
1046 }
1047
Linus Walleij8d318a52010-03-30 15:33:42 +02001048 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1049
1050 if (d40c->phy_chan->num % 2 == 0)
1051 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1052 else
1053 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1054
1055 if (command == D40_DMA_SUSPEND_REQ) {
1056 status = (readl(active_reg) &
1057 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1058 D40_CHAN_POS(d40c->phy_chan->num);
1059
1060 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1061 goto done;
1062 }
1063
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001064 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1065 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1066 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001067
1068 if (command == D40_DMA_SUSPEND_REQ) {
1069
1070 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1071 status = (readl(active_reg) &
1072 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1073 D40_CHAN_POS(d40c->phy_chan->num);
1074
1075 cpu_relax();
1076 /*
1077 * Reduce the number of bus accesses while
1078 * waiting for the DMA to suspend.
1079 */
1080 udelay(3);
1081
1082 if (status == D40_DMA_STOP ||
1083 status == D40_DMA_SUSPENDED)
1084 break;
1085 }
1086
1087 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001088 chan_err(d40c,
1089 "unable to suspend the chl %d (log: %d) status %x\n",
1090 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001091 status);
1092 dump_stack();
1093 ret = -EBUSY;
1094 }
1095
1096 }
1097done:
1098 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1099 return ret;
1100}
1101
1102static void d40_term_all(struct d40_chan *d40c)
1103{
1104 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001105 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001106
1107 /* Release active descriptors */
1108 while ((d40d = d40_first_active_get(d40c))) {
1109 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001110 d40_desc_free(d40c, d40d);
1111 }
1112
1113 /* Release queued descriptors waiting for transfer */
1114 while ((d40d = d40_first_queued(d40c))) {
1115 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001116 d40_desc_free(d40c, d40d);
1117 }
1118
Per Forlina8f30672011-06-26 23:29:52 +02001119 /* Release pending descriptors */
1120 while ((d40d = d40_first_pending(d40c))) {
1121 d40_desc_remove(d40d);
1122 d40_desc_free(d40c, d40d);
1123 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001124
Per Forlin74043682011-08-29 13:33:34 +02001125 /* Release client owned descriptors */
1126 if (!list_empty(&d40c->client))
1127 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1128 d40_desc_remove(d40d);
1129 d40_desc_free(d40c, d40d);
1130 }
1131
Per Forlin82babbb362011-08-29 13:33:35 +02001132 /* Release descriptors in prepare queue */
1133 if (!list_empty(&d40c->prepare_queue))
1134 list_for_each_entry_safe(d40d, _d,
1135 &d40c->prepare_queue, node) {
1136 d40_desc_remove(d40d);
1137 d40_desc_free(d40c, d40d);
1138 }
Per Forlin74043682011-08-29 13:33:34 +02001139
Linus Walleij8d318a52010-03-30 15:33:42 +02001140 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001141}
1142
Narayanan G1bdae6f2012-02-09 12:41:37 +05301143static void __d40_config_set_event(struct d40_chan *d40c,
1144 enum d40_events event_type, u32 event,
1145 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001146{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001147 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001148 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301149 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001150
Narayanan G1bdae6f2012-02-09 12:41:37 +05301151 switch (event_type) {
1152
1153 case D40_DEACTIVATE_EVENTLINE:
1154
Rabin Vincent262d2912011-01-25 11:18:05 +01001155 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1156 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301157 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001158
Narayanan G1bdae6f2012-02-09 12:41:37 +05301159 case D40_SUSPEND_REQ_EVENTLINE:
1160 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1161 D40_EVENTLINE_POS(event);
1162
1163 if (status == D40_DEACTIVATE_EVENTLINE ||
1164 status == D40_SUSPEND_REQ_EVENTLINE)
1165 break;
1166
1167 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1168 | ~D40_EVENTLINE_MASK(event), addr);
1169
1170 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1171
1172 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1173 D40_EVENTLINE_POS(event);
1174
1175 cpu_relax();
1176 /*
1177 * Reduce the number of bus accesses while
1178 * waiting for the DMA to suspend.
1179 */
1180 udelay(3);
1181
1182 if (status == D40_DEACTIVATE_EVENTLINE)
1183 break;
1184 }
1185
1186 if (tries == D40_SUSPEND_MAX_IT) {
1187 chan_err(d40c,
1188 "unable to stop the event_line chl %d (log: %d)"
1189 "status %x\n", d40c->phy_chan->num,
1190 d40c->log_num, status);
1191 }
1192 break;
1193
1194 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001195 /*
1196 * The hardware sometimes doesn't register the enable when src and dst
1197 * event lines are active on the same logical channel. Retry to ensure
1198 * it does. Usually only one retry is sufficient.
1199 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301200 tries = 100;
1201 while (--tries) {
1202 writel((D40_ACTIVATE_EVENTLINE <<
1203 D40_EVENTLINE_POS(event)) |
1204 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001205
Narayanan G1bdae6f2012-02-09 12:41:37 +05301206 if (readl(addr) & D40_EVENTLINE_MASK(event))
1207 break;
1208 }
1209
1210 if (tries != 99)
1211 dev_dbg(chan2dev(d40c),
1212 "[%s] workaround enable S%cLNK (%d tries)\n",
1213 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1214 100 - tries);
1215
1216 WARN_ON(!tries);
1217 break;
1218
1219 case D40_ROUND_EVENTLINE:
1220 BUG();
1221 break;
1222
Rabin Vincent262d2912011-01-25 11:18:05 +01001223 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001224}
1225
Narayanan G1bdae6f2012-02-09 12:41:37 +05301226static void d40_config_set_event(struct d40_chan *d40c,
1227 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001228{
Linus Walleij8d318a52010-03-30 15:33:42 +02001229 /* Enable event line connected to device (or memcpy) */
1230 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1231 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1232 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1233
Narayanan G1bdae6f2012-02-09 12:41:37 +05301234 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001235 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001236 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001237
Linus Walleij8d318a52010-03-30 15:33:42 +02001238 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1239 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1240
Narayanan G1bdae6f2012-02-09 12:41:37 +05301241 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001242 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001243 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001244}
1245
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001246static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001247{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001248 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001249 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001250
Rabin Vincent8ca84682011-01-25 11:18:07 +01001251 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1252 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001253
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001254 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001255}
1256
Narayanan G1bdae6f2012-02-09 12:41:37 +05301257static int
1258__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1259{
1260 unsigned long flags;
1261 int ret = 0;
1262 u32 active_status;
1263 void __iomem *active_reg;
1264
1265 if (d40c->phy_chan->num % 2 == 0)
1266 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1267 else
1268 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1269
1270
1271 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1272
1273 switch (command) {
1274 case D40_DMA_STOP:
1275 case D40_DMA_SUSPEND_REQ:
1276
1277 active_status = (readl(active_reg) &
1278 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1279 D40_CHAN_POS(d40c->phy_chan->num);
1280
1281 if (active_status == D40_DMA_RUN)
1282 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1283 else
1284 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1285
1286 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1287 ret = __d40_execute_command_phy(d40c, command);
1288
1289 break;
1290
1291 case D40_DMA_RUN:
1292
1293 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1294 ret = __d40_execute_command_phy(d40c, command);
1295 break;
1296
1297 case D40_DMA_SUSPENDED:
1298 BUG();
1299 break;
1300 }
1301
1302 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1303 return ret;
1304}
1305
1306static int d40_channel_execute_command(struct d40_chan *d40c,
1307 enum d40_command command)
1308{
1309 if (chan_is_logical(d40c))
1310 return __d40_execute_command_log(d40c, command);
1311 else
1312 return __d40_execute_command_phy(d40c, command);
1313}
1314
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001315static u32 d40_get_prmo(struct d40_chan *d40c)
1316{
1317 static const unsigned int phy_map[] = {
1318 [STEDMA40_PCHAN_BASIC_MODE]
1319 = D40_DREG_PRMO_PCHAN_BASIC,
1320 [STEDMA40_PCHAN_MODULO_MODE]
1321 = D40_DREG_PRMO_PCHAN_MODULO,
1322 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1323 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1324 };
1325 static const unsigned int log_map[] = {
1326 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1327 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1328 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1329 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1330 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1331 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1332 };
1333
Rabin Vincent724a8572011-01-25 11:18:08 +01001334 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001335 return phy_map[d40c->dma_cfg.mode_opt];
1336 else
1337 return log_map[d40c->dma_cfg.mode_opt];
1338}
1339
Jonas Aabergb55912c2010-08-09 12:08:02 +00001340static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001341{
1342 u32 addr_base;
1343 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001344
1345 /* Odd addresses are even addresses + 4 */
1346 addr_base = (d40c->phy_chan->num % 2) * 4;
1347 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001348 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001349 D40_CHAN_POS(d40c->phy_chan->num);
1350 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1351
1352 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001353 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001354
1355 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1356
Rabin Vincent724a8572011-01-25 11:18:08 +01001357 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001358 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1359 & D40_SREG_ELEM_LOG_LIDX_MASK;
1360 void __iomem *chanbase = chan_base(d40c);
1361
Linus Walleij8d318a52010-03-30 15:33:42 +02001362 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001363 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1364 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001365
Jonas Aabergb55912c2010-08-09 12:08:02 +00001366 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001367 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1368 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301369
1370 /* Clear LNK which will be used by d40_chan_has_events() */
1371 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1372 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001373 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001374}
1375
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001376static u32 d40_residue(struct d40_chan *d40c)
1377{
1378 u32 num_elt;
1379
Rabin Vincent724a8572011-01-25 11:18:08 +01001380 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001381 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1382 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001383 else {
1384 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1385 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1386 >> D40_SREG_ELEM_PHY_ECNT_POS;
1387 }
1388
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001389 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1390}
1391
1392static bool d40_tx_is_linked(struct d40_chan *d40c)
1393{
1394 bool is_link;
1395
Rabin Vincent724a8572011-01-25 11:18:08 +01001396 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001397 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1398 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001399 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1400 & D40_SREG_LNK_PHYS_LNK_MASK;
1401
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001402 return is_link;
1403}
1404
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001405static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001406{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001407 int res = 0;
1408 unsigned long flags;
1409
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001410 if (!d40c->busy)
1411 return 0;
1412
Narayanan G7fb3e752011-11-17 17:26:41 +05301413 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001414 spin_lock_irqsave(&d40c->lock, flags);
1415
1416 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301417
Narayanan G7fb3e752011-11-17 17:26:41 +05301418 pm_runtime_mark_last_busy(d40c->base->dev);
1419 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001420 spin_unlock_irqrestore(&d40c->lock, flags);
1421 return res;
1422}
1423
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001424static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001425{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001426 int res = 0;
1427 unsigned long flags;
1428
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001429 if (!d40c->busy)
1430 return 0;
1431
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001432 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301433 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001434
1435 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301436 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001437 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001438
Narayanan G7fb3e752011-11-17 17:26:41 +05301439 pm_runtime_mark_last_busy(d40c->base->dev);
1440 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001441 spin_unlock_irqrestore(&d40c->lock, flags);
1442 return res;
1443}
1444
Linus Walleij8d318a52010-03-30 15:33:42 +02001445static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1446{
1447 struct d40_chan *d40c = container_of(tx->chan,
1448 struct d40_chan,
1449 chan);
1450 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1451 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001452 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001453
1454 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001455 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001456 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001457 spin_unlock_irqrestore(&d40c->lock, flags);
1458
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001459 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001460}
1461
1462static int d40_start(struct d40_chan *d40c)
1463{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001464 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001465}
1466
1467static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1468{
1469 struct d40_desc *d40d;
1470 int err;
1471
1472 /* Start queued jobs, if any */
1473 d40d = d40_first_queued(d40c);
1474
1475 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301476 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301477 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301478 pm_runtime_get_sync(d40c->base->dev);
1479 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001480
1481 /* Remove from queue */
1482 d40_desc_remove(d40d);
1483
1484 /* Add to active queue */
1485 d40_desc_submit(d40c, d40d);
1486
Rabin Vincent7d83a852011-01-25 11:18:06 +01001487 /* Initiate DMA job */
1488 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001489
Rabin Vincent7d83a852011-01-25 11:18:06 +01001490 /* Start dma job */
1491 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001492
Rabin Vincent7d83a852011-01-25 11:18:06 +01001493 if (err)
1494 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001495 }
1496
1497 return d40d;
1498}
1499
1500/* called from interrupt context */
1501static void dma_tc_handle(struct d40_chan *d40c)
1502{
1503 struct d40_desc *d40d;
1504
Linus Walleij8d318a52010-03-30 15:33:42 +02001505 /* Get first active entry from list */
1506 d40d = d40_first_active_get(d40c);
1507
1508 if (d40d == NULL)
1509 return;
1510
Rabin Vincent0c842b52011-01-25 11:18:35 +01001511 if (d40d->cyclic) {
1512 /*
1513 * If this was a paritially loaded list, we need to reloaded
1514 * it, and only when the list is completed. We need to check
1515 * for done because the interrupt will hit for every link, and
1516 * not just the last one.
1517 */
1518 if (d40d->lli_current < d40d->lli_len
1519 && !d40_tx_is_linked(d40c)
1520 && !d40_residue(d40c)) {
1521 d40_lcla_free_all(d40c, d40d);
1522 d40_desc_load(d40c, d40d);
1523 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001524
Rabin Vincent0c842b52011-01-25 11:18:35 +01001525 if (d40d->lli_current == d40d->lli_len)
1526 d40d->lli_current = 0;
1527 }
1528 } else {
1529 d40_lcla_free_all(d40c, d40d);
1530
1531 if (d40d->lli_current < d40d->lli_len) {
1532 d40_desc_load(d40c, d40d);
1533 /* Start dma job */
1534 (void) d40_start(d40c);
1535 return;
1536 }
1537
1538 if (d40_queue_start(d40c) == NULL)
1539 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301540 pm_runtime_mark_last_busy(d40c->base->dev);
1541 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001542 }
1543
Linus Walleij8d318a52010-03-30 15:33:42 +02001544 d40c->pending_tx++;
1545 tasklet_schedule(&d40c->tasklet);
1546
1547}
1548
1549static void dma_tasklet(unsigned long data)
1550{
1551 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001552 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001553 unsigned long flags;
1554 dma_async_tx_callback callback;
1555 void *callback_param;
1556
1557 spin_lock_irqsave(&d40c->lock, flags);
1558
1559 /* Get first active entry from list */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001560 d40d = d40_first_active_get(d40c);
Jonas Aaberg767a9672010-08-09 12:08:34 +00001561 if (d40d == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02001562 goto err;
1563
Rabin Vincent0c842b52011-01-25 11:18:35 +01001564 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001565 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001566
1567 /*
1568 * If terminating a channel pending_tx is set to zero.
1569 * This prevents any finished active jobs to return to the client.
1570 */
1571 if (d40c->pending_tx == 0) {
1572 spin_unlock_irqrestore(&d40c->lock, flags);
1573 return;
1574 }
1575
1576 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001577 callback = d40d->txd.callback;
1578 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001579
Rabin Vincent0c842b52011-01-25 11:18:35 +01001580 if (!d40d->cyclic) {
1581 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001582 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001583 d40_desc_free(d40c, d40d);
1584 } else {
1585 if (!d40d->is_in_client_list) {
1586 d40_desc_remove(d40d);
1587 d40_lcla_free_all(d40c, d40d);
1588 list_add_tail(&d40d->node, &d40c->client);
1589 d40d->is_in_client_list = true;
1590 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001591 }
1592 }
1593
1594 d40c->pending_tx--;
1595
1596 if (d40c->pending_tx)
1597 tasklet_schedule(&d40c->tasklet);
1598
1599 spin_unlock_irqrestore(&d40c->lock, flags);
1600
Jonas Aaberg767a9672010-08-09 12:08:34 +00001601 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001602 callback(callback_param);
1603
1604 return;
1605
Narayanan G1bdae6f2012-02-09 12:41:37 +05301606err:
1607 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001608 if (d40c->pending_tx > 0)
1609 d40c->pending_tx--;
1610 spin_unlock_irqrestore(&d40c->lock, flags);
1611}
1612
1613static irqreturn_t d40_handle_interrupt(int irq, void *data)
1614{
Linus Walleij8d318a52010-03-30 15:33:42 +02001615 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001616 u32 idx;
1617 u32 row;
1618 long chan = -1;
1619 struct d40_chan *d40c;
1620 unsigned long flags;
1621 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001622 u32 regs[base->gen_dmac.il_size];
1623 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1624 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001625
1626 spin_lock_irqsave(&base->interrupt_lock, flags);
1627
1628 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001629 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001630 regs[i] = readl(base->virtbase + il[i].src);
1631
1632 for (;;) {
1633
1634 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001635 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001636
1637 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001638 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001639 break;
1640
1641 row = chan / BITS_PER_LONG;
1642 idx = chan & (BITS_PER_LONG - 1);
1643
1644 /* ACK interrupt */
Jonas Aaberg1b003482010-08-09 12:07:54 +00001645 writel(1 << idx, base->virtbase + il[row].clr);
Linus Walleij8d318a52010-03-30 15:33:42 +02001646
1647 if (il[row].offset == D40_PHY_CHAN)
1648 d40c = base->lookup_phy_chans[idx];
1649 else
1650 d40c = base->lookup_log_chans[il[row].offset + idx];
1651 spin_lock(&d40c->lock);
1652
1653 if (!il[row].is_error)
1654 dma_tc_handle(d40c);
1655 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001656 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1657 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001658
1659 spin_unlock(&d40c->lock);
1660 }
1661
1662 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1663
1664 return IRQ_HANDLED;
1665}
1666
Linus Walleij8d318a52010-03-30 15:33:42 +02001667static int d40_validate_conf(struct d40_chan *d40c,
1668 struct stedma40_chan_cfg *conf)
1669{
1670 int res = 0;
1671 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1672 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001673 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001674
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001675 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001676 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001677 res = -EINVAL;
1678 }
1679
1680 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1681 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1682 d40c->runtime_addr == 0) {
1683
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001684 chan_err(d40c, "Invalid TX channel address (%d)\n",
1685 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001686 res = -EINVAL;
1687 }
1688
1689 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1690 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1691 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001692 chan_err(d40c, "Invalid RX channel address (%d)\n",
1693 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001694 res = -EINVAL;
1695 }
1696
1697 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001698 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001699 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001700 res = -EINVAL;
1701 }
1702
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001703 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001704 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001705 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001706 res = -EINVAL;
1707 }
1708
1709 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1710 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001711 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001712 res = -EINVAL;
1713 }
1714
1715 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1716 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001717 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001718 res = -EINVAL;
1719 }
1720
1721 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1722 /*
1723 * DMAC HW supports it. Will be added to this driver,
1724 * in case any dma client requires it.
1725 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001726 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001727 res = -EINVAL;
1728 }
1729
Per Forlind49278e2010-12-20 18:31:38 +01001730 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1731 (1 << conf->src_info.data_width) !=
1732 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1733 (1 << conf->dst_info.data_width)) {
1734 /*
1735 * The DMAC hardware only supports
1736 * src (burst x width) == dst (burst x width)
1737 */
1738
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001739 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001740 res = -EINVAL;
1741 }
1742
Linus Walleij8d318a52010-03-30 15:33:42 +02001743 return res;
1744}
1745
Narayanan G5cd326f2011-11-30 19:20:42 +05301746static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1747 bool is_src, int log_event_line, bool is_log,
1748 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001749{
1750 unsigned long flags;
1751 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301752
1753 *first_user = ((phy->allocated_src | phy->allocated_dst)
1754 == D40_ALLOC_FREE);
1755
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001756 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001757 /* Physical interrupts are masked per physical full channel */
1758 if (phy->allocated_src == D40_ALLOC_FREE &&
1759 phy->allocated_dst == D40_ALLOC_FREE) {
1760 phy->allocated_dst = D40_ALLOC_PHY;
1761 phy->allocated_src = D40_ALLOC_PHY;
1762 goto found;
1763 } else
1764 goto not_found;
1765 }
1766
1767 /* Logical channel */
1768 if (is_src) {
1769 if (phy->allocated_src == D40_ALLOC_PHY)
1770 goto not_found;
1771
1772 if (phy->allocated_src == D40_ALLOC_FREE)
1773 phy->allocated_src = D40_ALLOC_LOG_FREE;
1774
1775 if (!(phy->allocated_src & (1 << log_event_line))) {
1776 phy->allocated_src |= 1 << log_event_line;
1777 goto found;
1778 } else
1779 goto not_found;
1780 } else {
1781 if (phy->allocated_dst == D40_ALLOC_PHY)
1782 goto not_found;
1783
1784 if (phy->allocated_dst == D40_ALLOC_FREE)
1785 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1786
1787 if (!(phy->allocated_dst & (1 << log_event_line))) {
1788 phy->allocated_dst |= 1 << log_event_line;
1789 goto found;
1790 } else
1791 goto not_found;
1792 }
1793
1794not_found:
1795 spin_unlock_irqrestore(&phy->lock, flags);
1796 return false;
1797found:
1798 spin_unlock_irqrestore(&phy->lock, flags);
1799 return true;
1800}
1801
1802static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1803 int log_event_line)
1804{
1805 unsigned long flags;
1806 bool is_free = false;
1807
1808 spin_lock_irqsave(&phy->lock, flags);
1809 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001810 phy->allocated_dst = D40_ALLOC_FREE;
1811 phy->allocated_src = D40_ALLOC_FREE;
1812 is_free = true;
1813 goto out;
1814 }
1815
1816 /* Logical channel */
1817 if (is_src) {
1818 phy->allocated_src &= ~(1 << log_event_line);
1819 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1820 phy->allocated_src = D40_ALLOC_FREE;
1821 } else {
1822 phy->allocated_dst &= ~(1 << log_event_line);
1823 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1824 phy->allocated_dst = D40_ALLOC_FREE;
1825 }
1826
1827 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1828 D40_ALLOC_FREE);
1829
1830out:
1831 spin_unlock_irqrestore(&phy->lock, flags);
1832
1833 return is_free;
1834}
1835
Narayanan G5cd326f2011-11-30 19:20:42 +05301836static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001837{
1838 int dev_type;
1839 int event_group;
1840 int event_line;
1841 struct d40_phy_res *phys;
1842 int i;
1843 int j;
1844 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001845 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001846 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001847 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001848
1849 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001850 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001851
1852 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1853 dev_type = d40c->dma_cfg.src_dev_type;
1854 log_num = 2 * dev_type;
1855 is_src = true;
1856 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1857 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1858 /* dst event lines are used for logical memcpy */
1859 dev_type = d40c->dma_cfg.dst_dev_type;
1860 log_num = 2 * dev_type + 1;
1861 is_src = false;
1862 } else
1863 return -EINVAL;
1864
1865 event_group = D40_TYPE_TO_GROUP(dev_type);
1866 event_line = D40_TYPE_TO_EVENT(dev_type);
1867
1868 if (!is_log) {
1869 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1870 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001871 if (d40c->dma_cfg.use_fixed_channel) {
1872 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001873 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301874 0, is_log,
1875 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001876 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001877 } else {
1878 for (i = 0; i < num_phy_chans; i++) {
1879 if (d40_alloc_mask_set(&phys[i], is_src,
1880 0, is_log,
1881 first_phy_user))
1882 goto found_phy;
1883 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001884 }
1885 } else
1886 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1887 int phy_num = j + event_group * 2;
1888 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001889 if (d40_alloc_mask_set(&phys[i],
1890 is_src,
1891 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301892 is_log,
1893 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001894 goto found_phy;
1895 }
1896 }
1897 return -EINVAL;
1898found_phy:
1899 d40c->phy_chan = &phys[i];
1900 d40c->log_num = D40_PHY_CHAN;
1901 goto out;
1902 }
1903 if (dev_type == -1)
1904 return -EINVAL;
1905
1906 /* Find logical channel */
1907 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1908 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301909
1910 if (d40c->dma_cfg.use_fixed_channel) {
1911 i = d40c->dma_cfg.phy_channel;
1912
1913 if ((i != phy_num) && (i != phy_num + 1)) {
1914 dev_err(chan2dev(d40c),
1915 "invalid fixed phy channel %d\n", i);
1916 return -EINVAL;
1917 }
1918
1919 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1920 is_log, first_phy_user))
1921 goto found_log;
1922
1923 dev_err(chan2dev(d40c),
1924 "could not allocate fixed phy channel %d\n", i);
1925 return -EINVAL;
1926 }
1927
Linus Walleij8d318a52010-03-30 15:33:42 +02001928 /*
1929 * Spread logical channels across all available physical rather
1930 * than pack every logical channel at the first available phy
1931 * channels.
1932 */
1933 if (is_src) {
1934 for (i = phy_num; i < phy_num + 2; i++) {
1935 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301936 event_line, is_log,
1937 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001938 goto found_log;
1939 }
1940 } else {
1941 for (i = phy_num + 1; i >= phy_num; i--) {
1942 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301943 event_line, is_log,
1944 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001945 goto found_log;
1946 }
1947 }
1948 }
1949 return -EINVAL;
1950
1951found_log:
1952 d40c->phy_chan = &phys[i];
1953 d40c->log_num = log_num;
1954out:
1955
1956 if (is_log)
1957 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
1958 else
1959 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
1960
1961 return 0;
1962
1963}
1964
Linus Walleij8d318a52010-03-30 15:33:42 +02001965static int d40_config_memcpy(struct d40_chan *d40c)
1966{
1967 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
1968
1969 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
1970 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
1971 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
1972 d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
1973 memcpy[d40c->chan.chan_id];
1974
1975 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
1976 dma_has_cap(DMA_SLAVE, cap)) {
1977 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
1978 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001979 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001980 return -EINVAL;
1981 }
1982
1983 return 0;
1984}
1985
Linus Walleij8d318a52010-03-30 15:33:42 +02001986static int d40_free_dma(struct d40_chan *d40c)
1987{
1988
1989 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00001990 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02001991 struct d40_phy_res *phy = d40c->phy_chan;
1992 bool is_src;
1993
1994 /* Terminate all queued and active transfers */
1995 d40_term_all(d40c);
1996
1997 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001998 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001999 return -EINVAL;
2000 }
2001
2002 if (phy->allocated_src == D40_ALLOC_FREE &&
2003 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002004 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002005 return -EINVAL;
2006 }
2007
Linus Walleij8d318a52010-03-30 15:33:42 +02002008 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
2009 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
2010 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002011 is_src = false;
2012 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
2013 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002014 is_src = true;
2015 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002016 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002017 return -EINVAL;
2018 }
2019
Narayanan G7fb3e752011-11-17 17:26:41 +05302020 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002021 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2022 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302023 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302024 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002025 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302026
Narayanan G1bdae6f2012-02-09 12:41:37 +05302027 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2028
2029 if (chan_is_logical(d40c))
2030 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2031 else
2032 d40c->base->lookup_phy_chans[phy->num] = NULL;
2033
Narayanan G7fb3e752011-11-17 17:26:41 +05302034 if (d40c->busy) {
2035 pm_runtime_mark_last_busy(d40c->base->dev);
2036 pm_runtime_put_autosuspend(d40c->base->dev);
2037 }
2038
2039 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002040 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002041 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302042out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002043
Narayanan G7fb3e752011-11-17 17:26:41 +05302044 pm_runtime_mark_last_busy(d40c->base->dev);
2045 pm_runtime_put_autosuspend(d40c->base->dev);
2046 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002047}
2048
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002049static bool d40_is_paused(struct d40_chan *d40c)
2050{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002051 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002052 bool is_paused = false;
2053 unsigned long flags;
2054 void __iomem *active_reg;
2055 u32 status;
2056 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002057
2058 spin_lock_irqsave(&d40c->lock, flags);
2059
Rabin Vincent724a8572011-01-25 11:18:08 +01002060 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002061 if (d40c->phy_chan->num % 2 == 0)
2062 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2063 else
2064 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2065
2066 status = (readl(active_reg) &
2067 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2068 D40_CHAN_POS(d40c->phy_chan->num);
2069 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2070 is_paused = true;
2071
2072 goto _exit;
2073 }
2074
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002075 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002076 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002077 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002078 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002079 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002080 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002081 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002082 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002083 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002084 goto _exit;
2085 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002086
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002087 status = (status & D40_EVENTLINE_MASK(event)) >>
2088 D40_EVENTLINE_POS(event);
2089
2090 if (status != D40_DMA_RUN)
2091 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002092_exit:
2093 spin_unlock_irqrestore(&d40c->lock, flags);
2094 return is_paused;
2095
2096}
2097
2098
Linus Walleij8d318a52010-03-30 15:33:42 +02002099static u32 stedma40_residue(struct dma_chan *chan)
2100{
2101 struct d40_chan *d40c =
2102 container_of(chan, struct d40_chan, chan);
2103 u32 bytes_left;
2104 unsigned long flags;
2105
2106 spin_lock_irqsave(&d40c->lock, flags);
2107 bytes_left = d40_residue(d40c);
2108 spin_unlock_irqrestore(&d40c->lock, flags);
2109
2110 return bytes_left;
2111}
2112
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002113static int
2114d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2115 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002116 unsigned int sg_len, dma_addr_t src_dev_addr,
2117 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002118{
2119 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2120 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2121 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002122 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002123
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002124 ret = d40_log_sg_to_lli(sg_src, sg_len,
2125 src_dev_addr,
2126 desc->lli_log.src,
2127 chan->log_def.lcsp1,
2128 src_info->data_width,
2129 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002130
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002131 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2132 dst_dev_addr,
2133 desc->lli_log.dst,
2134 chan->log_def.lcsp3,
2135 dst_info->data_width,
2136 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002137
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002138 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002139}
2140
2141static int
2142d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2143 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002144 unsigned int sg_len, dma_addr_t src_dev_addr,
2145 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002146{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002147 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2148 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2149 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002150 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002151 int ret;
2152
Rabin Vincent0c842b52011-01-25 11:18:35 +01002153 if (desc->cyclic)
2154 flags |= LLI_CYCLIC | LLI_TERM_INT;
2155
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002156 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2157 desc->lli_phy.src,
2158 virt_to_phys(desc->lli_phy.src),
2159 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002160 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002161
2162 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2163 desc->lli_phy.dst,
2164 virt_to_phys(desc->lli_phy.dst),
2165 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002166 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002167
2168 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2169 desc->lli_pool.size, DMA_TO_DEVICE);
2170
2171 return ret < 0 ? ret : 0;
2172}
2173
2174
Rabin Vincent5f811582011-01-25 11:18:18 +01002175static struct d40_desc *
2176d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2177 unsigned int sg_len, unsigned long dma_flags)
2178{
2179 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2180 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002181 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002182
2183 desc = d40_desc_get(chan);
2184 if (!desc)
2185 return NULL;
2186
2187 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2188 cfg->dst_info.data_width);
2189 if (desc->lli_len < 0) {
2190 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002191 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002192 }
2193
Rabin Vincentdbd88782011-01-25 11:18:19 +01002194 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2195 if (ret < 0) {
2196 chan_err(chan, "Could not allocate lli\n");
2197 goto err;
2198 }
2199
2200
Rabin Vincent5f811582011-01-25 11:18:18 +01002201 desc->lli_current = 0;
2202 desc->txd.flags = dma_flags;
2203 desc->txd.tx_submit = d40_tx_submit;
2204
2205 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2206
2207 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002208
2209err:
2210 d40_desc_free(chan, desc);
2211 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002212}
2213
Rabin Vincentcade1d32011-01-25 11:18:23 +01002214static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302215d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002216{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002217 struct stedma40_platform_data *plat = chan->base->plat_data;
2218 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002219 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002220
Rabin Vincentcade1d32011-01-25 11:18:23 +01002221 if (chan->runtime_addr)
2222 return chan->runtime_addr;
2223
Vinod Kouldb8196d2011-10-13 22:34:23 +05302224 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002225 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302226 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002227 addr = plat->dev_tx[cfg->dst_dev_type];
2228
2229 return addr;
2230}
2231
2232static struct dma_async_tx_descriptor *
2233d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2234 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302235 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002236{
2237 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002238 dma_addr_t src_dev_addr = 0;
2239 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002240 struct d40_desc *desc;
2241 unsigned long flags;
2242 int ret;
2243
2244 if (!chan->phy_chan) {
2245 chan_err(chan, "Cannot prepare unallocated channel\n");
2246 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002247 }
2248
Rabin Vincent0c842b52011-01-25 11:18:35 +01002249
Rabin Vincentcade1d32011-01-25 11:18:23 +01002250 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002251
Rabin Vincentcade1d32011-01-25 11:18:23 +01002252 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2253 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002254 goto err;
2255
Rabin Vincent0c842b52011-01-25 11:18:35 +01002256 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2257 desc->cyclic = true;
2258
Linus Walleij7e426da2012-04-12 18:12:52 +02002259 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002260 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2261
Vinod Kouldb8196d2011-10-13 22:34:23 +05302262 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002263 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302264 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002265 dst_dev_addr = dev_addr;
2266 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002267
2268 if (chan_is_logical(chan))
2269 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002270 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002271 else
2272 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002273 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002274
2275 if (ret) {
2276 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2277 chan_is_logical(chan) ? "log" : "phy", ret);
2278 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002279 }
2280
Per Forlin82babbb362011-08-29 13:33:35 +02002281 /*
2282 * add descriptor to the prepare queue in order to be able
2283 * to free them later in terminate_all
2284 */
2285 list_add_tail(&desc->node, &chan->prepare_queue);
2286
Rabin Vincentcade1d32011-01-25 11:18:23 +01002287 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002288
Rabin Vincentcade1d32011-01-25 11:18:23 +01002289 return &desc->txd;
2290
Linus Walleij8d318a52010-03-30 15:33:42 +02002291err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002292 if (desc)
2293 d40_desc_free(chan, desc);
2294 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002295 return NULL;
2296}
Linus Walleij8d318a52010-03-30 15:33:42 +02002297
2298bool stedma40_filter(struct dma_chan *chan, void *data)
2299{
2300 struct stedma40_chan_cfg *info = data;
2301 struct d40_chan *d40c =
2302 container_of(chan, struct d40_chan, chan);
2303 int err;
2304
2305 if (data) {
2306 err = d40_validate_conf(d40c, info);
2307 if (!err)
2308 d40c->dma_cfg = *info;
2309 } else
2310 err = d40_config_memcpy(d40c);
2311
Rabin Vincentce2ca122010-10-12 13:00:49 +00002312 if (!err)
2313 d40c->configured = true;
2314
Linus Walleij8d318a52010-03-30 15:33:42 +02002315 return err == 0;
2316}
2317EXPORT_SYMBOL(stedma40_filter);
2318
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002319static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2320{
2321 bool realtime = d40c->dma_cfg.realtime;
2322 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002323 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002324 u32 event = D40_TYPE_TO_EVENT(dev_type);
2325 u32 group = D40_TYPE_TO_GROUP(dev_type);
2326 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302327 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002328 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302329
Tong Liu3cb645d2012-09-26 10:07:30 +00002330 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302331 /*
2332 * Due to a hardware bug, in some cases a logical channel triggered by
2333 * a high priority destination event line can generate extra packet
2334 * transactions.
2335 *
2336 * The workaround is to not set the high priority level for the
2337 * destination event lines that trigger logical channels.
2338 */
2339 if (!src && chan_is_logical(d40c))
2340 highprio = false;
2341
Tong Liu3cb645d2012-09-26 10:07:30 +00002342 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002343
2344 /* Destination event lines are stored in the upper halfword */
2345 if (!src)
2346 bit <<= 16;
2347
2348 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2349 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2350}
2351
2352static void d40_set_prio_realtime(struct d40_chan *d40c)
2353{
2354 if (d40c->base->rev < 3)
2355 return;
2356
2357 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2358 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2359 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2360
2361 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2362 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2363 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2364}
2365
Linus Walleij8d318a52010-03-30 15:33:42 +02002366/* DMA ENGINE functions */
2367static int d40_alloc_chan_resources(struct dma_chan *chan)
2368{
2369 int err;
2370 unsigned long flags;
2371 struct d40_chan *d40c =
2372 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002373 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002374 spin_lock_irqsave(&d40c->lock, flags);
2375
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002376 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002377
Rabin Vincentce2ca122010-10-12 13:00:49 +00002378 /* If no dma configuration is set use default configuration (memcpy) */
2379 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002380 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002381 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002382 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002383 goto fail;
2384 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002385 }
2386
Narayanan G5cd326f2011-11-30 19:20:42 +05302387 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002388 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002389 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302390 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002391 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002392 }
2393
Narayanan G7fb3e752011-11-17 17:26:41 +05302394 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002395 /* Fill in basic CFG register values */
2396 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002397 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002398
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002399 d40_set_prio_realtime(d40c);
2400
Rabin Vincent724a8572011-01-25 11:18:08 +01002401 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002402 d40_log_cfg(&d40c->dma_cfg,
2403 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2404
2405 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2406 d40c->lcpa = d40c->base->lcpa_base +
2407 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2408 else
2409 d40c->lcpa = d40c->base->lcpa_base +
2410 d40c->dma_cfg.dst_dev_type *
2411 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2412 }
2413
Narayanan G5cd326f2011-11-30 19:20:42 +05302414 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2415 chan_is_logical(d40c) ? "logical" : "physical",
2416 d40c->phy_chan->num,
2417 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2418
2419
Linus Walleijef1872e2010-06-20 21:24:52 +00002420 /*
2421 * Only write channel configuration to the DMA if the physical
2422 * resource is free. In case of multiple logical channels
2423 * on the same physical resource, only the first write is necessary.
2424 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002425 if (is_free_phy)
2426 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002427fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302428 pm_runtime_mark_last_busy(d40c->base->dev);
2429 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002430 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002431 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002432}
2433
2434static void d40_free_chan_resources(struct dma_chan *chan)
2435{
2436 struct d40_chan *d40c =
2437 container_of(chan, struct d40_chan, chan);
2438 int err;
2439 unsigned long flags;
2440
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002441 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002442 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002443 return;
2444 }
2445
2446
Linus Walleij8d318a52010-03-30 15:33:42 +02002447 spin_lock_irqsave(&d40c->lock, flags);
2448
2449 err = d40_free_dma(d40c);
2450
2451 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002452 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002453 spin_unlock_irqrestore(&d40c->lock, flags);
2454}
2455
2456static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2457 dma_addr_t dst,
2458 dma_addr_t src,
2459 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002460 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002461{
Rabin Vincent95944c62011-01-25 11:18:17 +01002462 struct scatterlist dst_sg;
2463 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002464
Rabin Vincent95944c62011-01-25 11:18:17 +01002465 sg_init_table(&dst_sg, 1);
2466 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002467
Rabin Vincent95944c62011-01-25 11:18:17 +01002468 sg_dma_address(&dst_sg) = dst;
2469 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002470
Rabin Vincent95944c62011-01-25 11:18:17 +01002471 sg_dma_len(&dst_sg) = size;
2472 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002473
Rabin Vincentcade1d32011-01-25 11:18:23 +01002474 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002475}
2476
Ira Snyder0d688662010-09-30 11:46:47 +00002477static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002478d40_prep_memcpy_sg(struct dma_chan *chan,
2479 struct scatterlist *dst_sg, unsigned int dst_nents,
2480 struct scatterlist *src_sg, unsigned int src_nents,
2481 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002482{
2483 if (dst_nents != src_nents)
2484 return NULL;
2485
Rabin Vincentcade1d32011-01-25 11:18:23 +01002486 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002487}
2488
Linus Walleij8d318a52010-03-30 15:33:42 +02002489static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
2490 struct scatterlist *sgl,
2491 unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302492 enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002493 unsigned long dma_flags,
2494 void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002495{
Vinod Kouldb8196d2011-10-13 22:34:23 +05302496 if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV)
Rabin Vincent00ac0342011-01-25 11:18:20 +01002497 return NULL;
2498
Rabin Vincentcade1d32011-01-25 11:18:23 +01002499 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002500}
2501
Rabin Vincent0c842b52011-01-25 11:18:35 +01002502static struct dma_async_tx_descriptor *
2503dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2504 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002505 enum dma_transfer_direction direction, unsigned long flags,
2506 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002507{
2508 unsigned int periods = buf_len / period_len;
2509 struct dma_async_tx_descriptor *txd;
2510 struct scatterlist *sg;
2511 int i;
2512
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002513 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002514 for (i = 0; i < periods; i++) {
2515 sg_dma_address(&sg[i]) = dma_addr;
2516 sg_dma_len(&sg[i]) = period_len;
2517 dma_addr += period_len;
2518 }
2519
2520 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002521 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002522 sg[periods].page_link =
2523 ((unsigned long)sg | 0x01) & ~0x02;
2524
2525 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2526 DMA_PREP_INTERRUPT);
2527
2528 kfree(sg);
2529
2530 return txd;
2531}
2532
Linus Walleij8d318a52010-03-30 15:33:42 +02002533static enum dma_status d40_tx_status(struct dma_chan *chan,
2534 dma_cookie_t cookie,
2535 struct dma_tx_state *txstate)
2536{
2537 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002538 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002539
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002540 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002541 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002542 return -EINVAL;
2543 }
2544
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002545 ret = dma_cookie_status(chan, cookie, txstate);
2546 if (ret != DMA_SUCCESS)
2547 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002548
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002549 if (d40_is_paused(d40c))
2550 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002551
2552 return ret;
2553}
2554
2555static void d40_issue_pending(struct dma_chan *chan)
2556{
2557 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2558 unsigned long flags;
2559
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002560 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002561 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002562 return;
2563 }
2564
Linus Walleij8d318a52010-03-30 15:33:42 +02002565 spin_lock_irqsave(&d40c->lock, flags);
2566
Per Forlina8f30672011-06-26 23:29:52 +02002567 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2568
2569 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002570 if (!d40c->busy)
2571 (void) d40_queue_start(d40c);
2572
2573 spin_unlock_irqrestore(&d40c->lock, flags);
2574}
2575
Narayanan G1bdae6f2012-02-09 12:41:37 +05302576static void d40_terminate_all(struct dma_chan *chan)
2577{
2578 unsigned long flags;
2579 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2580 int ret;
2581
2582 spin_lock_irqsave(&d40c->lock, flags);
2583
2584 pm_runtime_get_sync(d40c->base->dev);
2585 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2586 if (ret)
2587 chan_err(d40c, "Failed to stop channel\n");
2588
2589 d40_term_all(d40c);
2590 pm_runtime_mark_last_busy(d40c->base->dev);
2591 pm_runtime_put_autosuspend(d40c->base->dev);
2592 if (d40c->busy) {
2593 pm_runtime_mark_last_busy(d40c->base->dev);
2594 pm_runtime_put_autosuspend(d40c->base->dev);
2595 }
2596 d40c->busy = false;
2597
2598 spin_unlock_irqrestore(&d40c->lock, flags);
2599}
2600
Rabin Vincent98ca5282011-06-27 11:33:38 +02002601static int
2602dma40_config_to_halfchannel(struct d40_chan *d40c,
2603 struct stedma40_half_channel_info *info,
2604 enum dma_slave_buswidth width,
2605 u32 maxburst)
2606{
2607 enum stedma40_periph_data_width addr_width;
2608 int psize;
2609
2610 switch (width) {
2611 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2612 addr_width = STEDMA40_BYTE_WIDTH;
2613 break;
2614 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2615 addr_width = STEDMA40_HALFWORD_WIDTH;
2616 break;
2617 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2618 addr_width = STEDMA40_WORD_WIDTH;
2619 break;
2620 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2621 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2622 break;
2623 default:
2624 dev_err(d40c->base->dev,
2625 "illegal peripheral address width "
2626 "requested (%d)\n",
2627 width);
2628 return -EINVAL;
2629 }
2630
2631 if (chan_is_logical(d40c)) {
2632 if (maxburst >= 16)
2633 psize = STEDMA40_PSIZE_LOG_16;
2634 else if (maxburst >= 8)
2635 psize = STEDMA40_PSIZE_LOG_8;
2636 else if (maxburst >= 4)
2637 psize = STEDMA40_PSIZE_LOG_4;
2638 else
2639 psize = STEDMA40_PSIZE_LOG_1;
2640 } else {
2641 if (maxburst >= 16)
2642 psize = STEDMA40_PSIZE_PHY_16;
2643 else if (maxburst >= 8)
2644 psize = STEDMA40_PSIZE_PHY_8;
2645 else if (maxburst >= 4)
2646 psize = STEDMA40_PSIZE_PHY_4;
2647 else
2648 psize = STEDMA40_PSIZE_PHY_1;
2649 }
2650
2651 info->data_width = addr_width;
2652 info->psize = psize;
2653 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2654
2655 return 0;
2656}
2657
Linus Walleij95e14002010-08-04 13:37:45 +02002658/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002659static int d40_set_runtime_config(struct dma_chan *chan,
2660 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002661{
2662 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2663 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002664 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002665 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002666 u32 src_maxburst, dst_maxburst;
2667 int ret;
2668
2669 src_addr_width = config->src_addr_width;
2670 src_maxburst = config->src_maxburst;
2671 dst_addr_width = config->dst_addr_width;
2672 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002673
Vinod Kouldb8196d2011-10-13 22:34:23 +05302674 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002675 dma_addr_t dev_addr_rx =
2676 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2677
2678 config_addr = config->src_addr;
2679 if (dev_addr_rx)
2680 dev_dbg(d40c->base->dev,
2681 "channel has a pre-wired RX address %08x "
2682 "overriding with %08x\n",
2683 dev_addr_rx, config_addr);
2684 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2685 dev_dbg(d40c->base->dev,
2686 "channel was not configured for peripheral "
2687 "to memory transfer (%d) overriding\n",
2688 cfg->dir);
2689 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2690
Rabin Vincent98ca5282011-06-27 11:33:38 +02002691 /* Configure the memory side */
2692 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2693 dst_addr_width = src_addr_width;
2694 if (dst_maxburst == 0)
2695 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002696
Vinod Kouldb8196d2011-10-13 22:34:23 +05302697 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002698 dma_addr_t dev_addr_tx =
2699 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2700
2701 config_addr = config->dst_addr;
2702 if (dev_addr_tx)
2703 dev_dbg(d40c->base->dev,
2704 "channel has a pre-wired TX address %08x "
2705 "overriding with %08x\n",
2706 dev_addr_tx, config_addr);
2707 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2708 dev_dbg(d40c->base->dev,
2709 "channel was not configured for memory "
2710 "to peripheral transfer (%d) overriding\n",
2711 cfg->dir);
2712 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2713
Rabin Vincent98ca5282011-06-27 11:33:38 +02002714 /* Configure the memory side */
2715 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2716 src_addr_width = dst_addr_width;
2717 if (src_maxburst == 0)
2718 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002719 } else {
2720 dev_err(d40c->base->dev,
2721 "unrecognized channel direction %d\n",
2722 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002723 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002724 }
2725
Rabin Vincent98ca5282011-06-27 11:33:38 +02002726 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002727 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002728 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2729 src_maxburst,
2730 src_addr_width,
2731 dst_maxburst,
2732 dst_addr_width);
2733 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002734 }
2735
Per Forlin92bb6cd2011-10-13 12:11:36 +02002736 if (src_maxburst > 16) {
2737 src_maxburst = 16;
2738 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2739 } else if (dst_maxburst > 16) {
2740 dst_maxburst = 16;
2741 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2742 }
2743
Rabin Vincent98ca5282011-06-27 11:33:38 +02002744 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2745 src_addr_width,
2746 src_maxburst);
2747 if (ret)
2748 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002749
Rabin Vincent98ca5282011-06-27 11:33:38 +02002750 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2751 dst_addr_width,
2752 dst_maxburst);
2753 if (ret)
2754 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002755
Per Forlina59670a2010-10-06 09:05:27 +00002756 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002757 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002758 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2759 else
2760 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2761 &d40c->dst_def_cfg, false);
2762
Linus Walleij95e14002010-08-04 13:37:45 +02002763 /* These settings will take precedence later */
2764 d40c->runtime_addr = config_addr;
2765 d40c->runtime_direction = config->direction;
2766 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002767 "configured channel %s for %s, data width %d/%d, "
2768 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002769 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302770 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002771 src_addr_width, dst_addr_width,
2772 src_maxburst, dst_maxburst);
2773
2774 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002775}
2776
Linus Walleij05827632010-05-17 16:30:42 -07002777static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2778 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002779{
Linus Walleij8d318a52010-03-30 15:33:42 +02002780 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2781
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002782 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002783 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002784 return -EINVAL;
2785 }
2786
Linus Walleij8d318a52010-03-30 15:33:42 +02002787 switch (cmd) {
2788 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302789 d40_terminate_all(chan);
2790 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002791 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002792 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002793 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002794 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002795 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002796 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002797 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002798 default:
2799 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002800 }
2801
2802 /* Other commands are unimplemented */
2803 return -ENXIO;
2804}
2805
2806/* Initialization functions */
2807
2808static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2809 struct d40_chan *chans, int offset,
2810 int num_chans)
2811{
2812 int i = 0;
2813 struct d40_chan *d40c;
2814
2815 INIT_LIST_HEAD(&dma->channels);
2816
2817 for (i = offset; i < offset + num_chans; i++) {
2818 d40c = &chans[i];
2819 d40c->base = base;
2820 d40c->chan.device = dma;
2821
Linus Walleij8d318a52010-03-30 15:33:42 +02002822 spin_lock_init(&d40c->lock);
2823
2824 d40c->log_num = D40_PHY_CHAN;
2825
Linus Walleij8d318a52010-03-30 15:33:42 +02002826 INIT_LIST_HEAD(&d40c->active);
2827 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002828 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002829 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002830 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002831
Linus Walleij8d318a52010-03-30 15:33:42 +02002832 tasklet_init(&d40c->tasklet, dma_tasklet,
2833 (unsigned long) d40c);
2834
2835 list_add_tail(&d40c->chan.device_node,
2836 &dma->channels);
2837 }
2838}
2839
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002840static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2841{
2842 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2843 dev->device_prep_slave_sg = d40_prep_slave_sg;
2844
2845 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2846 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2847
2848 /*
2849 * This controller can only access address at even
2850 * 32bit boundaries, i.e. 2^2
2851 */
2852 dev->copy_align = 2;
2853 }
2854
2855 if (dma_has_cap(DMA_SG, dev->cap_mask))
2856 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2857
Rabin Vincent0c842b52011-01-25 11:18:35 +01002858 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2859 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2860
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002861 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2862 dev->device_free_chan_resources = d40_free_chan_resources;
2863 dev->device_issue_pending = d40_issue_pending;
2864 dev->device_tx_status = d40_tx_status;
2865 dev->device_control = d40_control;
2866 dev->dev = base->dev;
2867}
2868
Linus Walleij8d318a52010-03-30 15:33:42 +02002869static int __init d40_dmaengine_init(struct d40_base *base,
2870 int num_reserved_chans)
2871{
2872 int err ;
2873
2874 d40_chan_init(base, &base->dma_slave, base->log_chans,
2875 0, base->num_log_chans);
2876
2877 dma_cap_zero(base->dma_slave.cap_mask);
2878 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002879 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002880
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002881 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002882
2883 err = dma_async_device_register(&base->dma_slave);
2884
2885 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002886 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002887 goto failure1;
2888 }
2889
2890 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2891 base->num_log_chans, base->plat_data->memcpy_len);
2892
2893 dma_cap_zero(base->dma_memcpy.cap_mask);
2894 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002895 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002896
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002897 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002898
2899 err = dma_async_device_register(&base->dma_memcpy);
2900
2901 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002902 d40_err(base->dev,
2903 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002904 goto failure2;
2905 }
2906
2907 d40_chan_init(base, &base->dma_both, base->phy_chans,
2908 0, num_reserved_chans);
2909
2910 dma_cap_zero(base->dma_both.cap_mask);
2911 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2912 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002913 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002914 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002915
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002916 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002917 err = dma_async_device_register(&base->dma_both);
2918
2919 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002920 d40_err(base->dev,
2921 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002922 goto failure3;
2923 }
2924 return 0;
2925failure3:
2926 dma_async_device_unregister(&base->dma_memcpy);
2927failure2:
2928 dma_async_device_unregister(&base->dma_slave);
2929failure1:
2930 return err;
2931}
2932
Narayanan G7fb3e752011-11-17 17:26:41 +05302933/* Suspend resume functionality */
2934#ifdef CONFIG_PM
2935static int dma40_pm_suspend(struct device *dev)
2936{
Narayanan G28c7a192011-11-22 13:56:55 +05302937 struct platform_device *pdev = to_platform_device(dev);
2938 struct d40_base *base = platform_get_drvdata(pdev);
2939 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302940
Narayanan G28c7a192011-11-22 13:56:55 +05302941 if (base->lcpa_regulator)
2942 ret = regulator_disable(base->lcpa_regulator);
2943 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302944}
2945
2946static int dma40_runtime_suspend(struct device *dev)
2947{
2948 struct platform_device *pdev = to_platform_device(dev);
2949 struct d40_base *base = platform_get_drvdata(pdev);
2950
2951 d40_save_restore_registers(base, true);
2952
2953 /* Don't disable/enable clocks for v1 due to HW bugs */
2954 if (base->rev != 1)
2955 writel_relaxed(base->gcc_pwr_off_mask,
2956 base->virtbase + D40_DREG_GCC);
2957
2958 return 0;
2959}
2960
2961static int dma40_runtime_resume(struct device *dev)
2962{
2963 struct platform_device *pdev = to_platform_device(dev);
2964 struct d40_base *base = platform_get_drvdata(pdev);
2965
2966 if (base->initialized)
2967 d40_save_restore_registers(base, false);
2968
2969 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
2970 base->virtbase + D40_DREG_GCC);
2971 return 0;
2972}
2973
Narayanan G28c7a192011-11-22 13:56:55 +05302974static int dma40_resume(struct device *dev)
2975{
2976 struct platform_device *pdev = to_platform_device(dev);
2977 struct d40_base *base = platform_get_drvdata(pdev);
2978 int ret = 0;
2979
2980 if (base->lcpa_regulator)
2981 ret = regulator_enable(base->lcpa_regulator);
2982
2983 return ret;
2984}
Narayanan G7fb3e752011-11-17 17:26:41 +05302985
2986static const struct dev_pm_ops dma40_pm_ops = {
2987 .suspend = dma40_pm_suspend,
2988 .runtime_suspend = dma40_runtime_suspend,
2989 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05302990 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05302991};
2992#define DMA40_PM_OPS (&dma40_pm_ops)
2993#else
2994#define DMA40_PM_OPS NULL
2995#endif
2996
Linus Walleij8d318a52010-03-30 15:33:42 +02002997/* Initialization functions. */
2998
2999static int __init d40_phy_res_init(struct d40_base *base)
3000{
3001 int i;
3002 int num_phy_chans_avail = 0;
3003 u32 val[2];
3004 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303005 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003006
3007 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3008 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3009
3010 for (i = 0; i < base->num_phy_chans; i++) {
3011 base->phy_res[i].num = i;
3012 odd_even_bit += 2 * ((i % 2) == 0);
3013 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3014 /* Mark security only channels as occupied */
3015 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3016 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303017 base->phy_res[i].reserved = true;
3018 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3019 D40_DREG_GCC_SRC);
3020 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3021 D40_DREG_GCC_DST);
3022
3023
Linus Walleij8d318a52010-03-30 15:33:42 +02003024 } else {
3025 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3026 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303027 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003028 num_phy_chans_avail++;
3029 }
3030 spin_lock_init(&base->phy_res[i].lock);
3031 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003032
3033 /* Mark disabled channels as occupied */
3034 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003035 int chan = base->plat_data->disabled_channels[i];
3036
3037 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3038 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303039 base->phy_res[chan].reserved = true;
3040 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3041 D40_DREG_GCC_SRC);
3042 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3043 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003044 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003045 }
3046
Linus Walleij8d318a52010-03-30 15:33:42 +02003047 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3048 num_phy_chans_avail, base->num_phy_chans);
3049
3050 /* Verify settings extended vs standard */
3051 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3052
3053 for (i = 0; i < base->num_phy_chans; i++) {
3054
3055 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3056 (val[0] & 0x3) != 1)
3057 dev_info(base->dev,
3058 "[%s] INFO: channel %d is misconfigured (%d)\n",
3059 __func__, i, val[0] & 0x3);
3060
3061 val[0] = val[0] >> 2;
3062 }
3063
Narayanan G7fb3e752011-11-17 17:26:41 +05303064 /*
3065 * To keep things simple, Enable all clocks initially.
3066 * The clocks will get managed later post channel allocation.
3067 * The clocks for the event lines on which reserved channels exists
3068 * are not managed here.
3069 */
3070 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3071 base->gcc_pwr_off_mask = gcc;
3072
Linus Walleij8d318a52010-03-30 15:33:42 +02003073 return num_phy_chans_avail;
3074}
3075
3076static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3077{
Linus Walleij8d318a52010-03-30 15:33:42 +02003078 struct stedma40_platform_data *plat_data;
3079 struct clk *clk = NULL;
3080 void __iomem *virtbase = NULL;
3081 struct resource *res = NULL;
3082 struct d40_base *base = NULL;
3083 int num_log_chans = 0;
3084 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003085 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003086 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003087 u32 pid;
3088 u32 cid;
3089 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003090
3091 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003092 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003093 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003094 goto failure;
3095 }
3096
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003097 clk_ret = clk_prepare_enable(clk);
3098 if (clk_ret) {
3099 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3100 goto failure;
3101 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003102
3103 /* Get IO for DMAC base address */
3104 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3105 if (!res)
3106 goto failure;
3107
3108 if (request_mem_region(res->start, resource_size(res),
3109 D40_NAME " I/O base") == NULL)
3110 goto failure;
3111
3112 virtbase = ioremap(res->start, resource_size(res));
3113 if (!virtbase)
3114 goto failure;
3115
Linus Walleijf4b89762011-06-27 11:33:46 +02003116 /* This is just a regular AMBA PrimeCell ID actually */
3117 for (pid = 0, i = 0; i < 4; i++)
3118 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3119 & 255) << (i * 8);
3120 for (cid = 0, i = 0; i < 4; i++)
3121 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3122 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003123
Linus Walleijf4b89762011-06-27 11:33:46 +02003124 if (cid != AMBA_CID) {
3125 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003126 goto failure;
3127 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003128 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3129 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3130 AMBA_MANF_BITS(pid),
3131 AMBA_VENDOR_ST);
3132 goto failure;
3133 }
3134 /*
3135 * HW revision:
3136 * DB8500ed has revision 0
3137 * ? has revision 1
3138 * DB8500v1 has revision 2
3139 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003140 * AP9540v1 has revision 4
3141 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003142 */
3143 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003144
Gerald Baeza47db92f2012-09-21 21:21:37 +02003145 plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003146
Gerald Baeza47db92f2012-09-21 21:21:37 +02003147 /* The number of physical channels on this HW */
3148 if (plat_data->num_of_phy_chans)
3149 num_phy_chans = plat_data->num_of_phy_chans;
3150 else
3151 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3152
3153 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x with %d physical channels\n",
3154 rev, res->start, num_phy_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003155
Narayanan G1bdae6f2012-02-09 12:41:37 +05303156 if (rev < 2) {
3157 d40_err(&pdev->dev, "hardware revision: %d is not supported",
3158 rev);
3159 goto failure;
3160 }
3161
Linus Walleij8d318a52010-03-30 15:33:42 +02003162 /* Count the number of logical channels in use */
3163 for (i = 0; i < plat_data->dev_len; i++)
3164 if (plat_data->dev_rx[i] != 0)
3165 num_log_chans++;
3166
3167 for (i = 0; i < plat_data->dev_len; i++)
3168 if (plat_data->dev_tx[i] != 0)
3169 num_log_chans++;
3170
3171 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
3172 (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
3173 sizeof(struct d40_chan), GFP_KERNEL);
3174
3175 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003176 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003177 goto failure;
3178 }
3179
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003180 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003181 base->clk = clk;
3182 base->num_phy_chans = num_phy_chans;
3183 base->num_log_chans = num_log_chans;
3184 base->phy_start = res->start;
3185 base->phy_size = resource_size(res);
3186 base->virtbase = virtbase;
3187 base->plat_data = plat_data;
3188 base->dev = &pdev->dev;
3189 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3190 base->log_chans = &base->phy_chans[num_phy_chans];
3191
Tong Liu3cb645d2012-09-26 10:07:30 +00003192 if (base->plat_data->num_of_phy_chans == 14) {
3193 base->gen_dmac.backup = d40_backup_regs_v4b;
3194 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3195 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3196 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3197 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3198 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3199 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3200 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3201 base->gen_dmac.il = il_v4b;
3202 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3203 base->gen_dmac.init_reg = dma_init_reg_v4b;
3204 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3205 } else {
3206 if (base->rev >= 3) {
3207 base->gen_dmac.backup = d40_backup_regs_v4a;
3208 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3209 }
3210 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3211 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3212 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3213 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3214 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3215 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3216 base->gen_dmac.il = il_v4a;
3217 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3218 base->gen_dmac.init_reg = dma_init_reg_v4a;
3219 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3220 }
3221
Linus Walleij8d318a52010-03-30 15:33:42 +02003222 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3223 GFP_KERNEL);
3224 if (!base->phy_res)
3225 goto failure;
3226
3227 base->lookup_phy_chans = kzalloc(num_phy_chans *
3228 sizeof(struct d40_chan *),
3229 GFP_KERNEL);
3230 if (!base->lookup_phy_chans)
3231 goto failure;
3232
3233 if (num_log_chans + plat_data->memcpy_len) {
3234 /*
3235 * The max number of logical channels are event lines for all
3236 * src devices and dst devices
3237 */
3238 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
3239 sizeof(struct d40_chan *),
3240 GFP_KERNEL);
3241 if (!base->lookup_log_chans)
3242 goto failure;
3243 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00003244
Narayanan G7fb3e752011-11-17 17:26:41 +05303245 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3246 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003247 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303248 if (!base->reg_val_backup_chan)
3249 goto failure;
3250
3251 base->lcla_pool.alloc_map =
3252 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3253 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003254 if (!base->lcla_pool.alloc_map)
3255 goto failure;
3256
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003257 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3258 0, SLAB_HWCACHE_ALIGN,
3259 NULL);
3260 if (base->desc_slab == NULL)
3261 goto failure;
3262
Linus Walleij8d318a52010-03-30 15:33:42 +02003263 return base;
3264
3265failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003266 if (!clk_ret)
3267 clk_disable_unprepare(clk);
3268 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003269 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003270 if (virtbase)
3271 iounmap(virtbase);
3272 if (res)
3273 release_mem_region(res->start,
3274 resource_size(res));
3275 if (virtbase)
3276 iounmap(virtbase);
3277
3278 if (base) {
3279 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303280 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003281 kfree(base->lookup_log_chans);
3282 kfree(base->lookup_phy_chans);
3283 kfree(base->phy_res);
3284 kfree(base);
3285 }
3286
3287 return NULL;
3288}
3289
3290static void __init d40_hw_init(struct d40_base *base)
3291{
3292
Linus Walleij8d318a52010-03-30 15:33:42 +02003293 int i;
3294 u32 prmseo[2] = {0, 0};
3295 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3296 u32 pcmis = 0;
3297 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003298 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3299 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003300
Tong Liu3cb645d2012-09-26 10:07:30 +00003301 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003302 writel(dma_init_reg[i].val,
3303 base->virtbase + dma_init_reg[i].reg);
3304
3305 /* Configure all our dma channels to default settings */
3306 for (i = 0; i < base->num_phy_chans; i++) {
3307
3308 activeo[i % 2] = activeo[i % 2] << 2;
3309
3310 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3311 == D40_ALLOC_PHY) {
3312 activeo[i % 2] |= 3;
3313 continue;
3314 }
3315
3316 /* Enable interrupt # */
3317 pcmis = (pcmis << 1) | 1;
3318
3319 /* Clear interrupt # */
3320 pcicr = (pcicr << 1) | 1;
3321
3322 /* Set channel to physical mode */
3323 prmseo[i % 2] = prmseo[i % 2] << 2;
3324 prmseo[i % 2] |= 1;
3325
3326 }
3327
3328 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3329 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3330 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3331 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3332
3333 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003334 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003335
3336 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003337 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003338
Tong Liu3cb645d2012-09-26 10:07:30 +00003339 /* These are __initdata and cannot be accessed after init */
3340 base->gen_dmac.init_reg = NULL;
3341 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003342}
3343
Linus Walleij508849a2010-06-20 21:26:07 +00003344static int __init d40_lcla_allocate(struct d40_base *base)
3345{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003346 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003347 unsigned long *page_list;
3348 int i, j;
3349 int ret = 0;
3350
3351 /*
3352 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3353 * To full fill this hardware requirement without wasting 256 kb
3354 * we allocate pages until we get an aligned one.
3355 */
3356 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3357 GFP_KERNEL);
3358
3359 if (!page_list) {
3360 ret = -ENOMEM;
3361 goto failure;
3362 }
3363
3364 /* Calculating how many pages that are required */
3365 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3366
3367 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3368 page_list[i] = __get_free_pages(GFP_KERNEL,
3369 base->lcla_pool.pages);
3370 if (!page_list[i]) {
3371
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003372 d40_err(base->dev, "Failed to allocate %d pages.\n",
3373 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003374
3375 for (j = 0; j < i; j++)
3376 free_pages(page_list[j], base->lcla_pool.pages);
3377 goto failure;
3378 }
3379
3380 if ((virt_to_phys((void *)page_list[i]) &
3381 (LCLA_ALIGNMENT - 1)) == 0)
3382 break;
3383 }
3384
3385 for (j = 0; j < i; j++)
3386 free_pages(page_list[j], base->lcla_pool.pages);
3387
3388 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3389 base->lcla_pool.base = (void *)page_list[i];
3390 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003391 /*
3392 * After many attempts and no succees with finding the correct
3393 * alignment, try with allocating a big buffer.
3394 */
Linus Walleij508849a2010-06-20 21:26:07 +00003395 dev_warn(base->dev,
3396 "[%s] Failed to get %d pages @ 18 bit align.\n",
3397 __func__, base->lcla_pool.pages);
3398 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3399 base->num_phy_chans +
3400 LCLA_ALIGNMENT,
3401 GFP_KERNEL);
3402 if (!base->lcla_pool.base_unaligned) {
3403 ret = -ENOMEM;
3404 goto failure;
3405 }
3406
3407 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3408 LCLA_ALIGNMENT);
3409 }
3410
Rabin Vincent026cbc42011-01-25 11:18:14 +01003411 pool->dma_addr = dma_map_single(base->dev, pool->base,
3412 SZ_1K * base->num_phy_chans,
3413 DMA_TO_DEVICE);
3414 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3415 pool->dma_addr = 0;
3416 ret = -ENOMEM;
3417 goto failure;
3418 }
3419
Linus Walleij508849a2010-06-20 21:26:07 +00003420 writel(virt_to_phys(base->lcla_pool.base),
3421 base->virtbase + D40_DREG_LCLA);
3422failure:
3423 kfree(page_list);
3424 return ret;
3425}
3426
Linus Walleij8d318a52010-03-30 15:33:42 +02003427static int __init d40_probe(struct platform_device *pdev)
3428{
3429 int err;
3430 int ret = -ENOENT;
3431 struct d40_base *base;
3432 struct resource *res = NULL;
3433 int num_reserved_chans;
3434 u32 val;
3435
3436 base = d40_hw_detect_init(pdev);
3437
3438 if (!base)
3439 goto failure;
3440
3441 num_reserved_chans = d40_phy_res_init(base);
3442
3443 platform_set_drvdata(pdev, base);
3444
3445 spin_lock_init(&base->interrupt_lock);
3446 spin_lock_init(&base->execmd_lock);
3447
3448 /* Get IO for logical channel parameter address */
3449 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3450 if (!res) {
3451 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003452 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003453 goto failure;
3454 }
3455 base->lcpa_size = resource_size(res);
3456 base->phy_lcpa = res->start;
3457
3458 if (request_mem_region(res->start, resource_size(res),
3459 D40_NAME " I/O lcpa") == NULL) {
3460 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003461 d40_err(&pdev->dev,
3462 "Failed to request LCPA region 0x%x-0x%x\n",
3463 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003464 goto failure;
3465 }
3466
3467 /* We make use of ESRAM memory for this. */
3468 val = readl(base->virtbase + D40_DREG_LCPA);
3469 if (res->start != val && val != 0) {
3470 dev_warn(&pdev->dev,
3471 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3472 __func__, val, res->start);
3473 } else
3474 writel(res->start, base->virtbase + D40_DREG_LCPA);
3475
3476 base->lcpa_base = ioremap(res->start, resource_size(res));
3477 if (!base->lcpa_base) {
3478 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003479 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003480 goto failure;
3481 }
Narayanan G28c7a192011-11-22 13:56:55 +05303482 /* If lcla has to be located in ESRAM we don't need to allocate */
3483 if (base->plat_data->use_esram_lcla) {
3484 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3485 "lcla_esram");
3486 if (!res) {
3487 ret = -ENOENT;
3488 d40_err(&pdev->dev,
3489 "No \"lcla_esram\" memory resource\n");
3490 goto failure;
3491 }
3492 base->lcla_pool.base = ioremap(res->start,
3493 resource_size(res));
3494 if (!base->lcla_pool.base) {
3495 ret = -ENOMEM;
3496 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3497 goto failure;
3498 }
3499 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003500
Narayanan G28c7a192011-11-22 13:56:55 +05303501 } else {
3502 ret = d40_lcla_allocate(base);
3503 if (ret) {
3504 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3505 goto failure;
3506 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003507 }
3508
Linus Walleij8d318a52010-03-30 15:33:42 +02003509 spin_lock_init(&base->lcla_pool.lock);
3510
Linus Walleij8d318a52010-03-30 15:33:42 +02003511 base->irq = platform_get_irq(pdev, 0);
3512
3513 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003514 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003515 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003516 goto failure;
3517 }
3518
Narayanan G7fb3e752011-11-17 17:26:41 +05303519 pm_runtime_irq_safe(base->dev);
3520 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3521 pm_runtime_use_autosuspend(base->dev);
3522 pm_runtime_enable(base->dev);
3523 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303524
3525 if (base->plat_data->use_esram_lcla) {
3526
3527 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3528 if (IS_ERR(base->lcpa_regulator)) {
3529 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3530 base->lcpa_regulator = NULL;
3531 goto failure;
3532 }
3533
3534 ret = regulator_enable(base->lcpa_regulator);
3535 if (ret) {
3536 d40_err(&pdev->dev,
3537 "Failed to enable lcpa_regulator\n");
3538 regulator_put(base->lcpa_regulator);
3539 base->lcpa_regulator = NULL;
3540 goto failure;
3541 }
3542 }
3543
Narayanan G7fb3e752011-11-17 17:26:41 +05303544 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003545 err = d40_dmaengine_init(base, num_reserved_chans);
3546 if (err)
3547 goto failure;
3548
Per Forlinb96710e2011-10-18 18:39:47 +02003549 base->dev->dma_parms = &base->dma_parms;
3550 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3551 if (err) {
3552 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3553 goto failure;
3554 }
3555
Linus Walleij8d318a52010-03-30 15:33:42 +02003556 d40_hw_init(base);
3557
3558 dev_info(base->dev, "initialized\n");
3559 return 0;
3560
3561failure:
3562 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003563 if (base->desc_slab)
3564 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003565 if (base->virtbase)
3566 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003567
Narayanan G28c7a192011-11-22 13:56:55 +05303568 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3569 iounmap(base->lcla_pool.base);
3570 base->lcla_pool.base = NULL;
3571 }
3572
Rabin Vincent026cbc42011-01-25 11:18:14 +01003573 if (base->lcla_pool.dma_addr)
3574 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3575 SZ_1K * base->num_phy_chans,
3576 DMA_TO_DEVICE);
3577
Linus Walleij508849a2010-06-20 21:26:07 +00003578 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3579 free_pages((unsigned long)base->lcla_pool.base,
3580 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003581
3582 kfree(base->lcla_pool.base_unaligned);
3583
Linus Walleij8d318a52010-03-30 15:33:42 +02003584 if (base->phy_lcpa)
3585 release_mem_region(base->phy_lcpa,
3586 base->lcpa_size);
3587 if (base->phy_start)
3588 release_mem_region(base->phy_start,
3589 base->phy_size);
3590 if (base->clk) {
3591 clk_disable(base->clk);
3592 clk_put(base->clk);
3593 }
3594
Narayanan G28c7a192011-11-22 13:56:55 +05303595 if (base->lcpa_regulator) {
3596 regulator_disable(base->lcpa_regulator);
3597 regulator_put(base->lcpa_regulator);
3598 }
3599
Linus Walleij8d318a52010-03-30 15:33:42 +02003600 kfree(base->lcla_pool.alloc_map);
3601 kfree(base->lookup_log_chans);
3602 kfree(base->lookup_phy_chans);
3603 kfree(base->phy_res);
3604 kfree(base);
3605 }
3606
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003607 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003608 return ret;
3609}
3610
3611static struct platform_driver d40_driver = {
3612 .driver = {
3613 .owner = THIS_MODULE,
3614 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303615 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003616 },
3617};
3618
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003619static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003620{
3621 return platform_driver_probe(&d40_driver, d40_probe);
3622}
Linus Walleija0eb2212011-05-18 14:18:57 +02003623subsys_initcall(stedma40_init);