blob: 12de79e84b15b07f4a4db56bc77613ec462793ee [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
Lee Jones664a57e2013-05-03 15:31:53 +010058/* Reserved event lines for memcpy only. */
59static int dma40_memcpy_channels[] = { 56, 57, 58, 59, 60 };
60
Linus Walleij8d318a52010-03-30 15:33:42 +020061/**
62 * enum 40_command - The different commands and/or statuses.
63 *
64 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
65 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
66 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
67 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
68 */
69enum d40_command {
70 D40_DMA_STOP = 0,
71 D40_DMA_RUN = 1,
72 D40_DMA_SUSPEND_REQ = 2,
73 D40_DMA_SUSPENDED = 3
74};
75
Narayanan G7fb3e752011-11-17 17:26:41 +053076/*
Narayanan G1bdae6f2012-02-09 12:41:37 +053077 * enum d40_events - The different Event Enables for the event lines.
78 *
79 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
80 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
81 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
82 * @D40_ROUND_EVENTLINE: Status check for event line.
83 */
84
85enum d40_events {
86 D40_DEACTIVATE_EVENTLINE = 0,
87 D40_ACTIVATE_EVENTLINE = 1,
88 D40_SUSPEND_REQ_EVENTLINE = 2,
89 D40_ROUND_EVENTLINE = 3
90};
91
92/*
Narayanan G7fb3e752011-11-17 17:26:41 +053093 * These are the registers that has to be saved and later restored
94 * when the DMA hw is powered off.
95 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
96 */
97static u32 d40_backup_regs[] = {
98 D40_DREG_LCPA,
99 D40_DREG_LCLA,
100 D40_DREG_PRMSE,
101 D40_DREG_PRMSO,
102 D40_DREG_PRMOE,
103 D40_DREG_PRMOO,
104};
105
106#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
107
Tong Liu3cb645d2012-09-26 10:07:30 +0000108/*
109 * since 9540 and 8540 has the same HW revision
110 * use v4a for 9540 or ealier
111 * use v4b for 8540 or later
112 * HW revision:
113 * DB8500ed has revision 0
114 * DB8500v1 has revision 2
115 * DB8500v2 has revision 3
116 * AP9540v1 has revision 4
117 * DB8540v1 has revision 4
118 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
119 */
120static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530121 D40_DREG_PSEG1,
122 D40_DREG_PSEG2,
123 D40_DREG_PSEG3,
124 D40_DREG_PSEG4,
125 D40_DREG_PCEG1,
126 D40_DREG_PCEG2,
127 D40_DREG_PCEG3,
128 D40_DREG_PCEG4,
129 D40_DREG_RSEG1,
130 D40_DREG_RSEG2,
131 D40_DREG_RSEG3,
132 D40_DREG_RSEG4,
133 D40_DREG_RCEG1,
134 D40_DREG_RCEG2,
135 D40_DREG_RCEG3,
136 D40_DREG_RCEG4,
137};
138
Tong Liu3cb645d2012-09-26 10:07:30 +0000139#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
140
141static u32 d40_backup_regs_v4b[] = {
142 D40_DREG_CPSEG1,
143 D40_DREG_CPSEG2,
144 D40_DREG_CPSEG3,
145 D40_DREG_CPSEG4,
146 D40_DREG_CPSEG5,
147 D40_DREG_CPCEG1,
148 D40_DREG_CPCEG2,
149 D40_DREG_CPCEG3,
150 D40_DREG_CPCEG4,
151 D40_DREG_CPCEG5,
152 D40_DREG_CRSEG1,
153 D40_DREG_CRSEG2,
154 D40_DREG_CRSEG3,
155 D40_DREG_CRSEG4,
156 D40_DREG_CRSEG5,
157 D40_DREG_CRCEG1,
158 D40_DREG_CRCEG2,
159 D40_DREG_CRCEG3,
160 D40_DREG_CRCEG4,
161 D40_DREG_CRCEG5,
162};
163
164#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530165
166static u32 d40_backup_regs_chan[] = {
167 D40_CHAN_REG_SSCFG,
168 D40_CHAN_REG_SSELT,
169 D40_CHAN_REG_SSPTR,
170 D40_CHAN_REG_SSLNK,
171 D40_CHAN_REG_SDCFG,
172 D40_CHAN_REG_SDELT,
173 D40_CHAN_REG_SDPTR,
174 D40_CHAN_REG_SDLNK,
175};
176
Linus Walleij8d318a52010-03-30 15:33:42 +0200177/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000178 * struct d40_interrupt_lookup - lookup table for interrupt handler
179 *
180 * @src: Interrupt mask register.
181 * @clr: Interrupt clear register.
182 * @is_error: true if this is an error interrupt.
183 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
184 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
185 */
186struct d40_interrupt_lookup {
187 u32 src;
188 u32 clr;
189 bool is_error;
190 int offset;
191};
192
193
194static struct d40_interrupt_lookup il_v4a[] = {
195 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
196 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
197 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
198 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
199 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
200 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
201 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
202 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
203 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
204 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
205};
206
207static struct d40_interrupt_lookup il_v4b[] = {
208 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
209 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
210 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
211 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
212 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
213 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
214 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
215 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
216 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
217 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
218 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
219 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
220};
221
222/**
223 * struct d40_reg_val - simple lookup struct
224 *
225 * @reg: The register.
226 * @val: The value that belongs to the register in reg.
227 */
228struct d40_reg_val {
229 unsigned int reg;
230 unsigned int val;
231};
232
233static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
234 /* Clock every part of the DMA block from start */
235 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
236
237 /* Interrupts on all logical channels */
238 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
239 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
240 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
241 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
242 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
243 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
244 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
245 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
246 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
247 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
248 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
249 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
250};
251static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
252 /* Clock every part of the DMA block from start */
253 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
254
255 /* Interrupts on all logical channels */
256 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
257 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
258 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
259 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
260 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
261 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
262 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
263 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
264 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
265 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
266 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
267 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
268 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
269 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
270 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
271};
272
273/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200274 * struct d40_lli_pool - Structure for keeping LLIs in memory
275 *
276 * @base: Pointer to memory area when the pre_alloc_lli's are not large
277 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
278 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100279 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200280 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
281 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
282 * one buffer to one buffer.
283 */
284struct d40_lli_pool {
285 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000286 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100287 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200288 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000289 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200290};
291
292/**
293 * struct d40_desc - A descriptor is one DMA job.
294 *
295 * @lli_phy: LLI settings for physical channel. Both src and dst=
296 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
297 * lli_len equals one.
298 * @lli_log: Same as above but for logical channels.
299 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000300 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300301 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000302 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200303 * @txd: DMA engine struct. Used for among other things for communication
304 * during a transfer.
305 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200306 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530307 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200308 *
309 * This descriptor is used for both logical and physical transfers.
310 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200311struct d40_desc {
312 /* LLI physical */
313 struct d40_phy_lli_bidir lli_phy;
314 /* LLI logical */
315 struct d40_log_lli_bidir lli_log;
316
317 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000318 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000319 int lli_current;
320 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200321
322 struct dma_async_tx_descriptor txd;
323 struct list_head node;
324
Linus Walleij8d318a52010-03-30 15:33:42 +0200325 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100326 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200327};
328
329/**
330 * struct d40_lcla_pool - LCLA pool settings and data.
331 *
Linus Walleij508849a2010-06-20 21:26:07 +0000332 * @base: The virtual address of LCLA. 18 bit aligned.
333 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
334 * This pointer is only there for clean-up on error.
335 * @pages: The number of pages needed for all physical channels.
336 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200337 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000338 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200339 */
340struct d40_lcla_pool {
341 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100342 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000343 void *base_unaligned;
344 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200345 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000346 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200347};
348
349/**
350 * struct d40_phy_res - struct for handling eventlines mapped to physical
351 * channels.
352 *
353 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530354 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200355 * @num: The physical channel number of this entity.
356 * @allocated_src: Bit mapped to show which src event line's are mapped to
357 * this physical channel. Can also be free or physically allocated.
358 * @allocated_dst: Same as for src but is dst.
359 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000360 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100361 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200362 */
363struct d40_phy_res {
364 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530365 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200366 int num;
367 u32 allocated_src;
368 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100369 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200370};
371
372struct d40_base;
373
374/**
375 * struct d40_chan - Struct that describes a channel.
376 *
377 * @lock: A spinlock to protect this struct.
378 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200379 * @pending_tx: The number of pending transfers. Used between interrupt handler
380 * and tasklet.
381 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000382 * @phy_chan: Pointer to physical channel which this instance runs on. If this
383 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200384 * @chan: DMA engine handle.
385 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
386 * transfer and call client callback.
387 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200388 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200389 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100390 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200391 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200392 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200393 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000394 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200395 * @base: Pointer to the device instance struct.
396 * @src_def_cfg: Default cfg register setting for src.
397 * @dst_def_cfg: Default cfg register setting for dst.
398 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200399 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200400 * @runtime_addr: runtime configured address.
401 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200402 *
403 * This struct can either "be" a logical or a physical channel.
404 */
405struct d40_chan {
406 spinlock_t lock;
407 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200408 int pending_tx;
409 bool busy;
410 struct d40_phy_res *phy_chan;
411 struct dma_chan chan;
412 struct tasklet_struct tasklet;
413 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200414 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200415 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100416 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200417 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200418 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200419 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000420 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200421 struct d40_base *base;
422 /* Default register configurations */
423 u32 src_def_cfg;
424 u32 dst_def_cfg;
425 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200426 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200427 /* Runtime reconfiguration */
428 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530429 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200430};
431
432/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000433 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
434 * controller
435 *
436 * @backup: the pointer to the registers address array for backup
437 * @backup_size: the size of the registers address array for backup
438 * @realtime_en: the realtime enable register
439 * @realtime_clear: the realtime clear register
440 * @high_prio_en: the high priority enable register
441 * @high_prio_clear: the high priority clear register
442 * @interrupt_en: the interrupt enable register
443 * @interrupt_clear: the interrupt clear register
444 * @il: the pointer to struct d40_interrupt_lookup
445 * @il_size: the size of d40_interrupt_lookup array
446 * @init_reg: the pointer to the struct d40_reg_val
447 * @init_reg_size: the size of d40_reg_val array
448 */
449struct d40_gen_dmac {
450 u32 *backup;
451 u32 backup_size;
452 u32 realtime_en;
453 u32 realtime_clear;
454 u32 high_prio_en;
455 u32 high_prio_clear;
456 u32 interrupt_en;
457 u32 interrupt_clear;
458 struct d40_interrupt_lookup *il;
459 u32 il_size;
460 struct d40_reg_val *init_reg;
461 u32 init_reg_size;
462};
463
464/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200465 * struct d40_base - The big global struct, one for each probe'd instance.
466 *
467 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
468 * @execmd_lock: Lock for execute command usage since several channels share
469 * the same physical register.
470 * @dev: The device structure.
471 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700472 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200473 * @clk: Pointer to the DMA clock structure.
474 * @phy_start: Physical memory start of the DMA registers.
475 * @phy_size: Size of the DMA register map.
476 * @irq: The IRQ number.
477 * @num_phy_chans: The number of physical channels. Read from HW. This
478 * is the number of available channels for this driver, not counting "Secure
479 * mode" allocated physical channels.
480 * @num_log_chans: The number of logical channels. Calculated from
481 * num_phy_chans.
482 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
483 * @dma_slave: dma_device channels that can do only do slave transfers.
484 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530485 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200486 * @log_chans: Room for all possible logical channels in system.
487 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
488 * to log_chans entries.
489 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
490 * to phy_chans entries.
491 * @plat_data: Pointer to provided platform_data which is the driver
492 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530493 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200494 * @phy_res: Vector containing all physical channels.
495 * @lcla_pool: lcla pool settings and data.
496 * @lcpa_base: The virtual mapped address of LCPA.
497 * @phy_lcpa: The physical address of the LCPA.
498 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000499 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530500 * @reg_val_backup: Here the values of some hardware registers are stored
501 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000502 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
503 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530504 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
505 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
506 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000507 * @gen_dmac: the struct for generic registers values to represent u8500/8540
508 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200509 */
510struct d40_base {
511 spinlock_t interrupt_lock;
512 spinlock_t execmd_lock;
513 struct device *dev;
514 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700515 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200516 struct clk *clk;
517 phys_addr_t phy_start;
518 resource_size_t phy_size;
519 int irq;
520 int num_phy_chans;
521 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200522 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200523 struct dma_device dma_both;
524 struct dma_device dma_slave;
525 struct dma_device dma_memcpy;
526 struct d40_chan *phy_chans;
527 struct d40_chan *log_chans;
528 struct d40_chan **lookup_log_chans;
529 struct d40_chan **lookup_phy_chans;
530 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530531 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200532 /* Physical half channels */
533 struct d40_phy_res *phy_res;
534 struct d40_lcla_pool lcla_pool;
535 void *lcpa_base;
536 dma_addr_t phy_lcpa;
537 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000538 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530539 u32 reg_val_backup[BACKUP_REGS_SZ];
Tong Liu3cb645d2012-09-26 10:07:30 +0000540 u32 reg_val_backup_v4[MAX(BACKUP_REGS_SZ_V4A, BACKUP_REGS_SZ_V4B)];
Narayanan G7fb3e752011-11-17 17:26:41 +0530541 u32 *reg_val_backup_chan;
542 u16 gcc_pwr_off_mask;
543 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000544 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200545};
546
Rabin Vincent262d2912011-01-25 11:18:05 +0100547static struct device *chan2dev(struct d40_chan *d40c)
548{
549 return &d40c->chan.dev->device;
550}
551
Rabin Vincent724a8572011-01-25 11:18:08 +0100552static bool chan_is_physical(struct d40_chan *chan)
553{
554 return chan->log_num == D40_PHY_CHAN;
555}
556
557static bool chan_is_logical(struct d40_chan *chan)
558{
559 return !chan_is_physical(chan);
560}
561
Rabin Vincent8ca84682011-01-25 11:18:07 +0100562static void __iomem *chan_base(struct d40_chan *chan)
563{
564 return chan->base->virtbase + D40_DREG_PCBASE +
565 chan->phy_chan->num * D40_DREG_PCDELTA;
566}
567
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100568#define d40_err(dev, format, arg...) \
569 dev_err(dev, "[%s] " format, __func__, ## arg)
570
571#define chan_err(d40c, format, arg...) \
572 d40_err(chan2dev(d40c), format, ## arg)
573
Rabin Vincentb00f9382011-01-25 11:18:15 +0100574static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100575 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200576{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100577 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200578 u32 align;
579 void *base;
580
581 if (is_log)
582 align = sizeof(struct d40_log_lli);
583 else
584 align = sizeof(struct d40_phy_lli);
585
586 if (lli_len == 1) {
587 base = d40d->lli_pool.pre_alloc_lli;
588 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
589 d40d->lli_pool.base = NULL;
590 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100591 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200592
593 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
594 d40d->lli_pool.base = base;
595
596 if (d40d->lli_pool.base == NULL)
597 return -ENOMEM;
598 }
599
600 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100601 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100602 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100603
604 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200605 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100606 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100607 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100608
609 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
610 d40d->lli_phy.src,
611 d40d->lli_pool.size,
612 DMA_TO_DEVICE);
613
614 if (dma_mapping_error(d40c->base->dev,
615 d40d->lli_pool.dma_addr)) {
616 kfree(d40d->lli_pool.base);
617 d40d->lli_pool.base = NULL;
618 d40d->lli_pool.dma_addr = 0;
619 return -ENOMEM;
620 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200621 }
622
623 return 0;
624}
625
Rabin Vincentb00f9382011-01-25 11:18:15 +0100626static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200627{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100628 if (d40d->lli_pool.dma_addr)
629 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
630 d40d->lli_pool.size, DMA_TO_DEVICE);
631
Linus Walleij8d318a52010-03-30 15:33:42 +0200632 kfree(d40d->lli_pool.base);
633 d40d->lli_pool.base = NULL;
634 d40d->lli_pool.size = 0;
635 d40d->lli_log.src = NULL;
636 d40d->lli_log.dst = NULL;
637 d40d->lli_phy.src = NULL;
638 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200639}
640
Jonas Aaberg698e4732010-08-09 12:08:56 +0000641static int d40_lcla_alloc_one(struct d40_chan *d40c,
642 struct d40_desc *d40d)
643{
644 unsigned long flags;
645 int i;
646 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000647
648 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
649
Jonas Aaberg698e4732010-08-09 12:08:56 +0000650 /*
651 * Allocate both src and dst at the same time, therefore the half
652 * start on 1 since 0 can't be used since zero is used as end marker.
653 */
654 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100655 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
656
657 if (!d40c->base->lcla_pool.alloc_map[idx]) {
658 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000659 d40d->lcla_alloc++;
660 ret = i;
661 break;
662 }
663 }
664
665 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
666
667 return ret;
668}
669
670static int d40_lcla_free_all(struct d40_chan *d40c,
671 struct d40_desc *d40d)
672{
673 unsigned long flags;
674 int i;
675 int ret = -EINVAL;
676
Rabin Vincent724a8572011-01-25 11:18:08 +0100677 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000678 return 0;
679
680 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
681
682 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100683 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
684
685 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
686 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000687 d40d->lcla_alloc--;
688 if (d40d->lcla_alloc == 0) {
689 ret = 0;
690 break;
691 }
692 }
693 }
694
695 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
696
697 return ret;
698
699}
700
Linus Walleij8d318a52010-03-30 15:33:42 +0200701static void d40_desc_remove(struct d40_desc *d40d)
702{
703 list_del(&d40d->node);
704}
705
706static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
707{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000708 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200709
710 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000711 struct d40_desc *d;
712 struct d40_desc *_d;
713
Narayanan G7fb3e752011-11-17 17:26:41 +0530714 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200715 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200716 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000717 desc = d;
718 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000719 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200720 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530721 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200722 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000723
724 if (!desc)
725 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
726
727 if (desc)
728 INIT_LIST_HEAD(&desc->node);
729
730 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200731}
732
733static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
734{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000735
Rabin Vincentb00f9382011-01-25 11:18:15 +0100736 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000737 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000738 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200739}
740
741static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
742{
743 list_add_tail(&desc->node, &d40c->active);
744}
745
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100746static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
747{
748 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
749 struct d40_phy_lli *lli_src = desc->lli_phy.src;
750 void __iomem *base = chan_base(chan);
751
752 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
753 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
754 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
755 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
756
757 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
758 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
759 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
760 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
761}
762
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100763static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
764{
765 list_add_tail(&desc->node, &d40c->done);
766}
767
Rabin Vincente65889c2011-01-25 11:18:31 +0100768static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
769{
770 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
771 struct d40_log_lli_bidir *lli = &desc->lli_log;
772 int lli_current = desc->lli_current;
773 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100774 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100775 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100776 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530777 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100778 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100779
Rabin Vincent0c842b52011-01-25 11:18:35 +0100780 /*
781 * We may have partially running cyclic transfers, in case we did't get
782 * enough LCLA entries.
783 */
784 linkback = cyclic && lli_current == 0;
785
786 /*
787 * For linkback, we need one LCLA even with only one link, because we
788 * can't link back to the one in LCPA space
789 */
790 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100791 /*
792 * If the channel is expected to use only soft_lli don't
793 * allocate a lcla. This is to avoid a HW issue that exists
794 * in some controller during a peripheral to memory transfer
795 * that uses linked lists.
796 */
797 if (!(chan->phy_chan->use_soft_lli &&
798 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
799 curr_lcla = d40_lcla_alloc_one(chan, desc);
800
Rabin Vincent0c842b52011-01-25 11:18:35 +0100801 first_lcla = curr_lcla;
802 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100803
Rabin Vincent0c842b52011-01-25 11:18:35 +0100804 /*
805 * For linkback, we normally load the LCPA in the loop since we need to
806 * link it to the second LCLA and not the first. However, if we
807 * couldn't even get a first LCLA, then we have to run in LCPA and
808 * reload manually.
809 */
810 if (!linkback || curr_lcla == -EINVAL) {
811 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100812
Rabin Vincent0c842b52011-01-25 11:18:35 +0100813 if (curr_lcla == -EINVAL)
814 flags |= LLI_TERM_INT;
815
816 d40_log_lli_lcpa_write(chan->lcpa,
817 &lli->dst[lli_current],
818 &lli->src[lli_current],
819 curr_lcla,
820 flags);
821 lli_current++;
822 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100823
824 if (curr_lcla < 0)
825 goto out;
826
Rabin Vincente65889c2011-01-25 11:18:31 +0100827 for (; lli_current < lli_len; lli_current++) {
828 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
829 8 * curr_lcla * 2;
830 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100831 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100832 int next_lcla;
833
834 if (lli_current + 1 < lli_len)
835 next_lcla = d40_lcla_alloc_one(chan, desc);
836 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100837 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100838
Rabin Vincent0c842b52011-01-25 11:18:35 +0100839 if (cyclic || next_lcla == -EINVAL)
840 flags |= LLI_TERM_INT;
841
842 if (linkback && curr_lcla == first_lcla) {
843 /* First link goes in both LCPA and LCLA */
844 d40_log_lli_lcpa_write(chan->lcpa,
845 &lli->dst[lli_current],
846 &lli->src[lli_current],
847 next_lcla, flags);
848 }
849
850 /*
851 * One unused LCLA in the cyclic case if the very first
852 * next_lcla fails...
853 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100854 d40_log_lli_lcla_write(lcla,
855 &lli->dst[lli_current],
856 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100857 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100858
Narayanan G28c7a192011-11-22 13:56:55 +0530859 /*
860 * Cache maintenance is not needed if lcla is
861 * mapped in esram
862 */
863 if (!use_esram_lcla) {
864 dma_sync_single_range_for_device(chan->base->dev,
865 pool->dma_addr, lcla_offset,
866 2 * sizeof(struct d40_log_lli),
867 DMA_TO_DEVICE);
868 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100869 curr_lcla = next_lcla;
870
Rabin Vincent0c842b52011-01-25 11:18:35 +0100871 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100872 lli_current++;
873 break;
874 }
875 }
876
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100877out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100878 desc->lli_current = lli_current;
879}
880
Jonas Aaberg698e4732010-08-09 12:08:56 +0000881static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
882{
Rabin Vincent724a8572011-01-25 11:18:08 +0100883 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100884 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000885 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100886 } else
887 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000888}
889
Linus Walleij8d318a52010-03-30 15:33:42 +0200890static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
891{
892 struct d40_desc *d;
893
894 if (list_empty(&d40c->active))
895 return NULL;
896
897 d = list_first_entry(&d40c->active,
898 struct d40_desc,
899 node);
900 return d;
901}
902
Per Forlin74043682011-08-29 13:33:34 +0200903/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200904static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
905{
Per Forlin74043682011-08-29 13:33:34 +0200906 d40_desc_remove(desc);
907 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200908 list_add_tail(&desc->node, &d40c->pending_queue);
909}
910
911static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
912{
913 struct d40_desc *d;
914
915 if (list_empty(&d40c->pending_queue))
916 return NULL;
917
918 d = list_first_entry(&d40c->pending_queue,
919 struct d40_desc,
920 node);
921 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200922}
923
924static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
925{
926 struct d40_desc *d;
927
928 if (list_empty(&d40c->queue))
929 return NULL;
930
931 d = list_first_entry(&d40c->queue,
932 struct d40_desc,
933 node);
934 return d;
935}
936
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100937static struct d40_desc *d40_first_done(struct d40_chan *d40c)
938{
939 if (list_empty(&d40c->done))
940 return NULL;
941
942 return list_first_entry(&d40c->done, struct d40_desc, node);
943}
944
Per Forlind49278e2010-12-20 18:31:38 +0100945static int d40_psize_2_burst_size(bool is_log, int psize)
946{
947 if (is_log) {
948 if (psize == STEDMA40_PSIZE_LOG_1)
949 return 1;
950 } else {
951 if (psize == STEDMA40_PSIZE_PHY_1)
952 return 1;
953 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200954
Per Forlind49278e2010-12-20 18:31:38 +0100955 return 2 << psize;
956}
957
958/*
959 * The dma only supports transmitting packages up to
960 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
961 * dma elements required to send the entire sg list
962 */
963static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
964{
965 int dmalen;
966 u32 max_w = max(data_width1, data_width2);
967 u32 min_w = min(data_width1, data_width2);
968 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
969
970 if (seg_max > STEDMA40_MAX_SEG_SIZE)
971 seg_max -= (1 << max_w);
972
973 if (!IS_ALIGNED(size, 1 << max_w))
974 return -EINVAL;
975
976 if (size <= seg_max)
977 dmalen = 1;
978 else {
979 dmalen = size / seg_max;
980 if (dmalen * seg_max < size)
981 dmalen++;
982 }
983 return dmalen;
984}
985
986static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
987 u32 data_width1, u32 data_width2)
988{
989 struct scatterlist *sg;
990 int i;
991 int len = 0;
992 int ret;
993
994 for_each_sg(sgl, sg, sg_len, i) {
995 ret = d40_size_2_dmalen(sg_dma_len(sg),
996 data_width1, data_width2);
997 if (ret < 0)
998 return ret;
999 len += ret;
1000 }
1001 return len;
1002}
1003
Narayanan G7fb3e752011-11-17 17:26:41 +05301004
1005#ifdef CONFIG_PM
1006static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1007 u32 *regaddr, int num, bool save)
1008{
1009 int i;
1010
1011 for (i = 0; i < num; i++) {
1012 void __iomem *addr = baseaddr + regaddr[i];
1013
1014 if (save)
1015 backup[i] = readl_relaxed(addr);
1016 else
1017 writel_relaxed(backup[i], addr);
1018 }
1019}
1020
1021static void d40_save_restore_registers(struct d40_base *base, bool save)
1022{
1023 int i;
1024
1025 /* Save/Restore channel specific registers */
1026 for (i = 0; i < base->num_phy_chans; i++) {
1027 void __iomem *addr;
1028 int idx;
1029
1030 if (base->phy_res[i].reserved)
1031 continue;
1032
1033 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1034 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1035
1036 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1037 d40_backup_regs_chan,
1038 ARRAY_SIZE(d40_backup_regs_chan),
1039 save);
1040 }
1041
1042 /* Save/Restore global registers */
1043 dma40_backup(base->virtbase, base->reg_val_backup,
1044 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1045 save);
1046
1047 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001048 if (base->gen_dmac.backup)
1049 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1050 base->gen_dmac.backup,
1051 base->gen_dmac.backup_size,
1052 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301053}
1054#else
1055static void d40_save_restore_registers(struct d40_base *base, bool save)
1056{
1057}
1058#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001059
Narayanan G1bdae6f2012-02-09 12:41:37 +05301060static int __d40_execute_command_phy(struct d40_chan *d40c,
1061 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001062{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001063 u32 status;
1064 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001065 void __iomem *active_reg;
1066 int ret = 0;
1067 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001068 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001069
Narayanan G1bdae6f2012-02-09 12:41:37 +05301070 if (command == D40_DMA_STOP) {
1071 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1072 if (ret)
1073 return ret;
1074 }
1075
Linus Walleij8d318a52010-03-30 15:33:42 +02001076 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1077
1078 if (d40c->phy_chan->num % 2 == 0)
1079 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1080 else
1081 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1082
1083 if (command == D40_DMA_SUSPEND_REQ) {
1084 status = (readl(active_reg) &
1085 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1086 D40_CHAN_POS(d40c->phy_chan->num);
1087
1088 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1089 goto done;
1090 }
1091
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001092 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1093 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1094 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001095
1096 if (command == D40_DMA_SUSPEND_REQ) {
1097
1098 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1099 status = (readl(active_reg) &
1100 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1101 D40_CHAN_POS(d40c->phy_chan->num);
1102
1103 cpu_relax();
1104 /*
1105 * Reduce the number of bus accesses while
1106 * waiting for the DMA to suspend.
1107 */
1108 udelay(3);
1109
1110 if (status == D40_DMA_STOP ||
1111 status == D40_DMA_SUSPENDED)
1112 break;
1113 }
1114
1115 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001116 chan_err(d40c,
1117 "unable to suspend the chl %d (log: %d) status %x\n",
1118 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001119 status);
1120 dump_stack();
1121 ret = -EBUSY;
1122 }
1123
1124 }
1125done:
1126 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1127 return ret;
1128}
1129
1130static void d40_term_all(struct d40_chan *d40c)
1131{
1132 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001133 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001134
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001135 /* Release completed descriptors */
1136 while ((d40d = d40_first_done(d40c))) {
1137 d40_desc_remove(d40d);
1138 d40_desc_free(d40c, d40d);
1139 }
1140
Linus Walleij8d318a52010-03-30 15:33:42 +02001141 /* Release active descriptors */
1142 while ((d40d = d40_first_active_get(d40c))) {
1143 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001144 d40_desc_free(d40c, d40d);
1145 }
1146
1147 /* Release queued descriptors waiting for transfer */
1148 while ((d40d = d40_first_queued(d40c))) {
1149 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001150 d40_desc_free(d40c, d40d);
1151 }
1152
Per Forlina8f30672011-06-26 23:29:52 +02001153 /* Release pending descriptors */
1154 while ((d40d = d40_first_pending(d40c))) {
1155 d40_desc_remove(d40d);
1156 d40_desc_free(d40c, d40d);
1157 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001158
Per Forlin74043682011-08-29 13:33:34 +02001159 /* Release client owned descriptors */
1160 if (!list_empty(&d40c->client))
1161 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1162 d40_desc_remove(d40d);
1163 d40_desc_free(d40c, d40d);
1164 }
1165
Per Forlin82babbb362011-08-29 13:33:35 +02001166 /* Release descriptors in prepare queue */
1167 if (!list_empty(&d40c->prepare_queue))
1168 list_for_each_entry_safe(d40d, _d,
1169 &d40c->prepare_queue, node) {
1170 d40_desc_remove(d40d);
1171 d40_desc_free(d40c, d40d);
1172 }
Per Forlin74043682011-08-29 13:33:34 +02001173
Linus Walleij8d318a52010-03-30 15:33:42 +02001174 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001175}
1176
Narayanan G1bdae6f2012-02-09 12:41:37 +05301177static void __d40_config_set_event(struct d40_chan *d40c,
1178 enum d40_events event_type, u32 event,
1179 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001180{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001181 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001182 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301183 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001184
Narayanan G1bdae6f2012-02-09 12:41:37 +05301185 switch (event_type) {
1186
1187 case D40_DEACTIVATE_EVENTLINE:
1188
Rabin Vincent262d2912011-01-25 11:18:05 +01001189 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1190 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301191 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001192
Narayanan G1bdae6f2012-02-09 12:41:37 +05301193 case D40_SUSPEND_REQ_EVENTLINE:
1194 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1195 D40_EVENTLINE_POS(event);
1196
1197 if (status == D40_DEACTIVATE_EVENTLINE ||
1198 status == D40_SUSPEND_REQ_EVENTLINE)
1199 break;
1200
1201 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1202 | ~D40_EVENTLINE_MASK(event), addr);
1203
1204 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1205
1206 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1207 D40_EVENTLINE_POS(event);
1208
1209 cpu_relax();
1210 /*
1211 * Reduce the number of bus accesses while
1212 * waiting for the DMA to suspend.
1213 */
1214 udelay(3);
1215
1216 if (status == D40_DEACTIVATE_EVENTLINE)
1217 break;
1218 }
1219
1220 if (tries == D40_SUSPEND_MAX_IT) {
1221 chan_err(d40c,
1222 "unable to stop the event_line chl %d (log: %d)"
1223 "status %x\n", d40c->phy_chan->num,
1224 d40c->log_num, status);
1225 }
1226 break;
1227
1228 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001229 /*
1230 * The hardware sometimes doesn't register the enable when src and dst
1231 * event lines are active on the same logical channel. Retry to ensure
1232 * it does. Usually only one retry is sufficient.
1233 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301234 tries = 100;
1235 while (--tries) {
1236 writel((D40_ACTIVATE_EVENTLINE <<
1237 D40_EVENTLINE_POS(event)) |
1238 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001239
Narayanan G1bdae6f2012-02-09 12:41:37 +05301240 if (readl(addr) & D40_EVENTLINE_MASK(event))
1241 break;
1242 }
1243
1244 if (tries != 99)
1245 dev_dbg(chan2dev(d40c),
1246 "[%s] workaround enable S%cLNK (%d tries)\n",
1247 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1248 100 - tries);
1249
1250 WARN_ON(!tries);
1251 break;
1252
1253 case D40_ROUND_EVENTLINE:
1254 BUG();
1255 break;
1256
Rabin Vincent262d2912011-01-25 11:18:05 +01001257 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001258}
1259
Narayanan G1bdae6f2012-02-09 12:41:37 +05301260static void d40_config_set_event(struct d40_chan *d40c,
1261 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001262{
Linus Walleij8d318a52010-03-30 15:33:42 +02001263 /* Enable event line connected to device (or memcpy) */
1264 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1265 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1266 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1267
Narayanan G1bdae6f2012-02-09 12:41:37 +05301268 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001269 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001270 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001271
Linus Walleij8d318a52010-03-30 15:33:42 +02001272 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1273 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1274
Narayanan G1bdae6f2012-02-09 12:41:37 +05301275 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001276 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001277 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001278}
1279
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001280static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001281{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001282 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001283 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001284
Rabin Vincent8ca84682011-01-25 11:18:07 +01001285 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1286 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001287
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001288 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001289}
1290
Narayanan G1bdae6f2012-02-09 12:41:37 +05301291static int
1292__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1293{
1294 unsigned long flags;
1295 int ret = 0;
1296 u32 active_status;
1297 void __iomem *active_reg;
1298
1299 if (d40c->phy_chan->num % 2 == 0)
1300 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1301 else
1302 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1303
1304
1305 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1306
1307 switch (command) {
1308 case D40_DMA_STOP:
1309 case D40_DMA_SUSPEND_REQ:
1310
1311 active_status = (readl(active_reg) &
1312 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1313 D40_CHAN_POS(d40c->phy_chan->num);
1314
1315 if (active_status == D40_DMA_RUN)
1316 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1317 else
1318 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1319
1320 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1321 ret = __d40_execute_command_phy(d40c, command);
1322
1323 break;
1324
1325 case D40_DMA_RUN:
1326
1327 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1328 ret = __d40_execute_command_phy(d40c, command);
1329 break;
1330
1331 case D40_DMA_SUSPENDED:
1332 BUG();
1333 break;
1334 }
1335
1336 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1337 return ret;
1338}
1339
1340static int d40_channel_execute_command(struct d40_chan *d40c,
1341 enum d40_command command)
1342{
1343 if (chan_is_logical(d40c))
1344 return __d40_execute_command_log(d40c, command);
1345 else
1346 return __d40_execute_command_phy(d40c, command);
1347}
1348
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001349static u32 d40_get_prmo(struct d40_chan *d40c)
1350{
1351 static const unsigned int phy_map[] = {
1352 [STEDMA40_PCHAN_BASIC_MODE]
1353 = D40_DREG_PRMO_PCHAN_BASIC,
1354 [STEDMA40_PCHAN_MODULO_MODE]
1355 = D40_DREG_PRMO_PCHAN_MODULO,
1356 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1357 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1358 };
1359 static const unsigned int log_map[] = {
1360 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1361 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1362 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1363 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1364 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1365 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1366 };
1367
Rabin Vincent724a8572011-01-25 11:18:08 +01001368 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001369 return phy_map[d40c->dma_cfg.mode_opt];
1370 else
1371 return log_map[d40c->dma_cfg.mode_opt];
1372}
1373
Jonas Aabergb55912c2010-08-09 12:08:02 +00001374static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001375{
1376 u32 addr_base;
1377 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001378
1379 /* Odd addresses are even addresses + 4 */
1380 addr_base = (d40c->phy_chan->num % 2) * 4;
1381 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001382 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001383 D40_CHAN_POS(d40c->phy_chan->num);
1384 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1385
1386 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001387 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001388
1389 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1390
Rabin Vincent724a8572011-01-25 11:18:08 +01001391 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001392 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1393 & D40_SREG_ELEM_LOG_LIDX_MASK;
1394 void __iomem *chanbase = chan_base(d40c);
1395
Linus Walleij8d318a52010-03-30 15:33:42 +02001396 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001397 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1398 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001399
Jonas Aabergb55912c2010-08-09 12:08:02 +00001400 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001401 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1402 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301403
1404 /* Clear LNK which will be used by d40_chan_has_events() */
1405 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1406 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001407 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001408}
1409
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001410static u32 d40_residue(struct d40_chan *d40c)
1411{
1412 u32 num_elt;
1413
Rabin Vincent724a8572011-01-25 11:18:08 +01001414 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001415 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1416 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001417 else {
1418 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1419 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1420 >> D40_SREG_ELEM_PHY_ECNT_POS;
1421 }
1422
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001423 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1424}
1425
1426static bool d40_tx_is_linked(struct d40_chan *d40c)
1427{
1428 bool is_link;
1429
Rabin Vincent724a8572011-01-25 11:18:08 +01001430 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001431 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1432 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001433 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1434 & D40_SREG_LNK_PHYS_LNK_MASK;
1435
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001436 return is_link;
1437}
1438
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001439static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001440{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001441 int res = 0;
1442 unsigned long flags;
1443
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001444 if (!d40c->busy)
1445 return 0;
1446
Narayanan G7fb3e752011-11-17 17:26:41 +05301447 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001448 spin_lock_irqsave(&d40c->lock, flags);
1449
1450 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301451
Narayanan G7fb3e752011-11-17 17:26:41 +05301452 pm_runtime_mark_last_busy(d40c->base->dev);
1453 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001454 spin_unlock_irqrestore(&d40c->lock, flags);
1455 return res;
1456}
1457
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001458static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001459{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001460 int res = 0;
1461 unsigned long flags;
1462
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001463 if (!d40c->busy)
1464 return 0;
1465
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001466 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301467 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001468
1469 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301470 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001471 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001472
Narayanan G7fb3e752011-11-17 17:26:41 +05301473 pm_runtime_mark_last_busy(d40c->base->dev);
1474 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001475 spin_unlock_irqrestore(&d40c->lock, flags);
1476 return res;
1477}
1478
Linus Walleij8d318a52010-03-30 15:33:42 +02001479static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1480{
1481 struct d40_chan *d40c = container_of(tx->chan,
1482 struct d40_chan,
1483 chan);
1484 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1485 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001486 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001487
1488 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001489 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001490 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001491 spin_unlock_irqrestore(&d40c->lock, flags);
1492
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001493 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001494}
1495
1496static int d40_start(struct d40_chan *d40c)
1497{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001498 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001499}
1500
1501static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1502{
1503 struct d40_desc *d40d;
1504 int err;
1505
1506 /* Start queued jobs, if any */
1507 d40d = d40_first_queued(d40c);
1508
1509 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301510 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301511 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301512 pm_runtime_get_sync(d40c->base->dev);
1513 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001514
1515 /* Remove from queue */
1516 d40_desc_remove(d40d);
1517
1518 /* Add to active queue */
1519 d40_desc_submit(d40c, d40d);
1520
Rabin Vincent7d83a852011-01-25 11:18:06 +01001521 /* Initiate DMA job */
1522 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001523
Rabin Vincent7d83a852011-01-25 11:18:06 +01001524 /* Start dma job */
1525 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001526
Rabin Vincent7d83a852011-01-25 11:18:06 +01001527 if (err)
1528 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001529 }
1530
1531 return d40d;
1532}
1533
1534/* called from interrupt context */
1535static void dma_tc_handle(struct d40_chan *d40c)
1536{
1537 struct d40_desc *d40d;
1538
Linus Walleij8d318a52010-03-30 15:33:42 +02001539 /* Get first active entry from list */
1540 d40d = d40_first_active_get(d40c);
1541
1542 if (d40d == NULL)
1543 return;
1544
Rabin Vincent0c842b52011-01-25 11:18:35 +01001545 if (d40d->cyclic) {
1546 /*
1547 * If this was a paritially loaded list, we need to reloaded
1548 * it, and only when the list is completed. We need to check
1549 * for done because the interrupt will hit for every link, and
1550 * not just the last one.
1551 */
1552 if (d40d->lli_current < d40d->lli_len
1553 && !d40_tx_is_linked(d40c)
1554 && !d40_residue(d40c)) {
1555 d40_lcla_free_all(d40c, d40d);
1556 d40_desc_load(d40c, d40d);
1557 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001558
Rabin Vincent0c842b52011-01-25 11:18:35 +01001559 if (d40d->lli_current == d40d->lli_len)
1560 d40d->lli_current = 0;
1561 }
1562 } else {
1563 d40_lcla_free_all(d40c, d40d);
1564
1565 if (d40d->lli_current < d40d->lli_len) {
1566 d40_desc_load(d40c, d40d);
1567 /* Start dma job */
1568 (void) d40_start(d40c);
1569 return;
1570 }
1571
1572 if (d40_queue_start(d40c) == NULL)
1573 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301574 pm_runtime_mark_last_busy(d40c->base->dev);
1575 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001576
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001577 d40_desc_remove(d40d);
1578 d40_desc_done(d40c, d40d);
1579 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001580
Linus Walleij8d318a52010-03-30 15:33:42 +02001581 d40c->pending_tx++;
1582 tasklet_schedule(&d40c->tasklet);
1583
1584}
1585
1586static void dma_tasklet(unsigned long data)
1587{
1588 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001589 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001590 unsigned long flags;
1591 dma_async_tx_callback callback;
1592 void *callback_param;
1593
1594 spin_lock_irqsave(&d40c->lock, flags);
1595
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001596 /* Get first entry from the done list */
1597 d40d = d40_first_done(d40c);
1598 if (d40d == NULL) {
1599 /* Check if we have reached here for cyclic job */
1600 d40d = d40_first_active_get(d40c);
1601 if (d40d == NULL || !d40d->cyclic)
1602 goto err;
1603 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001604
Rabin Vincent0c842b52011-01-25 11:18:35 +01001605 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001606 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001607
1608 /*
1609 * If terminating a channel pending_tx is set to zero.
1610 * This prevents any finished active jobs to return to the client.
1611 */
1612 if (d40c->pending_tx == 0) {
1613 spin_unlock_irqrestore(&d40c->lock, flags);
1614 return;
1615 }
1616
1617 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001618 callback = d40d->txd.callback;
1619 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001620
Rabin Vincent0c842b52011-01-25 11:18:35 +01001621 if (!d40d->cyclic) {
1622 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001623 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001624 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001625 } else if (!d40d->is_in_client_list) {
1626 d40_desc_remove(d40d);
1627 d40_lcla_free_all(d40c, d40d);
1628 list_add_tail(&d40d->node, &d40c->client);
1629 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001630 }
1631 }
1632
1633 d40c->pending_tx--;
1634
1635 if (d40c->pending_tx)
1636 tasklet_schedule(&d40c->tasklet);
1637
1638 spin_unlock_irqrestore(&d40c->lock, flags);
1639
Jonas Aaberg767a9672010-08-09 12:08:34 +00001640 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001641 callback(callback_param);
1642
1643 return;
1644
Narayanan G1bdae6f2012-02-09 12:41:37 +05301645err:
1646 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001647 if (d40c->pending_tx > 0)
1648 d40c->pending_tx--;
1649 spin_unlock_irqrestore(&d40c->lock, flags);
1650}
1651
1652static irqreturn_t d40_handle_interrupt(int irq, void *data)
1653{
Linus Walleij8d318a52010-03-30 15:33:42 +02001654 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001655 u32 idx;
1656 u32 row;
1657 long chan = -1;
1658 struct d40_chan *d40c;
1659 unsigned long flags;
1660 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001661 u32 regs[base->gen_dmac.il_size];
1662 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1663 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001664
1665 spin_lock_irqsave(&base->interrupt_lock, flags);
1666
1667 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001668 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001669 regs[i] = readl(base->virtbase + il[i].src);
1670
1671 for (;;) {
1672
1673 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001674 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001675
1676 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001677 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001678 break;
1679
1680 row = chan / BITS_PER_LONG;
1681 idx = chan & (BITS_PER_LONG - 1);
1682
Linus Walleij8d318a52010-03-30 15:33:42 +02001683 if (il[row].offset == D40_PHY_CHAN)
1684 d40c = base->lookup_phy_chans[idx];
1685 else
1686 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001687
1688 if (!d40c) {
1689 /*
1690 * No error because this can happen if something else
1691 * in the system is using the channel.
1692 */
1693 continue;
1694 }
1695
1696 /* ACK interrupt */
1697 writel(1 << idx, base->virtbase + il[row].clr);
1698
Linus Walleij8d318a52010-03-30 15:33:42 +02001699 spin_lock(&d40c->lock);
1700
1701 if (!il[row].is_error)
1702 dma_tc_handle(d40c);
1703 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001704 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1705 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001706
1707 spin_unlock(&d40c->lock);
1708 }
1709
1710 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1711
1712 return IRQ_HANDLED;
1713}
1714
Linus Walleij8d318a52010-03-30 15:33:42 +02001715static int d40_validate_conf(struct d40_chan *d40c,
1716 struct stedma40_chan_cfg *conf)
1717{
1718 int res = 0;
1719 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1720 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001721 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001722
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001723 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001724 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001725 res = -EINVAL;
1726 }
1727
1728 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1729 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1730 d40c->runtime_addr == 0) {
1731
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001732 chan_err(d40c, "Invalid TX channel address (%d)\n",
1733 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001734 res = -EINVAL;
1735 }
1736
1737 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1738 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1739 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001740 chan_err(d40c, "Invalid RX channel address (%d)\n",
1741 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001742 res = -EINVAL;
1743 }
1744
1745 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001746 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001747 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001748 res = -EINVAL;
1749 }
1750
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001751 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001752 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001753 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001754 res = -EINVAL;
1755 }
1756
1757 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1758 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001759 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001760 res = -EINVAL;
1761 }
1762
1763 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1764 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001765 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001766 res = -EINVAL;
1767 }
1768
1769 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1770 /*
1771 * DMAC HW supports it. Will be added to this driver,
1772 * in case any dma client requires it.
1773 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001774 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001775 res = -EINVAL;
1776 }
1777
Per Forlind49278e2010-12-20 18:31:38 +01001778 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1779 (1 << conf->src_info.data_width) !=
1780 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1781 (1 << conf->dst_info.data_width)) {
1782 /*
1783 * The DMAC hardware only supports
1784 * src (burst x width) == dst (burst x width)
1785 */
1786
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001787 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001788 res = -EINVAL;
1789 }
1790
Linus Walleij8d318a52010-03-30 15:33:42 +02001791 return res;
1792}
1793
Narayanan G5cd326f2011-11-30 19:20:42 +05301794static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1795 bool is_src, int log_event_line, bool is_log,
1796 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001797{
1798 unsigned long flags;
1799 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301800
1801 *first_user = ((phy->allocated_src | phy->allocated_dst)
1802 == D40_ALLOC_FREE);
1803
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001804 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001805 /* Physical interrupts are masked per physical full channel */
1806 if (phy->allocated_src == D40_ALLOC_FREE &&
1807 phy->allocated_dst == D40_ALLOC_FREE) {
1808 phy->allocated_dst = D40_ALLOC_PHY;
1809 phy->allocated_src = D40_ALLOC_PHY;
1810 goto found;
1811 } else
1812 goto not_found;
1813 }
1814
1815 /* Logical channel */
1816 if (is_src) {
1817 if (phy->allocated_src == D40_ALLOC_PHY)
1818 goto not_found;
1819
1820 if (phy->allocated_src == D40_ALLOC_FREE)
1821 phy->allocated_src = D40_ALLOC_LOG_FREE;
1822
1823 if (!(phy->allocated_src & (1 << log_event_line))) {
1824 phy->allocated_src |= 1 << log_event_line;
1825 goto found;
1826 } else
1827 goto not_found;
1828 } else {
1829 if (phy->allocated_dst == D40_ALLOC_PHY)
1830 goto not_found;
1831
1832 if (phy->allocated_dst == D40_ALLOC_FREE)
1833 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1834
1835 if (!(phy->allocated_dst & (1 << log_event_line))) {
1836 phy->allocated_dst |= 1 << log_event_line;
1837 goto found;
1838 } else
1839 goto not_found;
1840 }
1841
1842not_found:
1843 spin_unlock_irqrestore(&phy->lock, flags);
1844 return false;
1845found:
1846 spin_unlock_irqrestore(&phy->lock, flags);
1847 return true;
1848}
1849
1850static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1851 int log_event_line)
1852{
1853 unsigned long flags;
1854 bool is_free = false;
1855
1856 spin_lock_irqsave(&phy->lock, flags);
1857 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001858 phy->allocated_dst = D40_ALLOC_FREE;
1859 phy->allocated_src = D40_ALLOC_FREE;
1860 is_free = true;
1861 goto out;
1862 }
1863
1864 /* Logical channel */
1865 if (is_src) {
1866 phy->allocated_src &= ~(1 << log_event_line);
1867 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1868 phy->allocated_src = D40_ALLOC_FREE;
1869 } else {
1870 phy->allocated_dst &= ~(1 << log_event_line);
1871 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1872 phy->allocated_dst = D40_ALLOC_FREE;
1873 }
1874
1875 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1876 D40_ALLOC_FREE);
1877
1878out:
1879 spin_unlock_irqrestore(&phy->lock, flags);
1880
1881 return is_free;
1882}
1883
Narayanan G5cd326f2011-11-30 19:20:42 +05301884static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001885{
1886 int dev_type;
1887 int event_group;
1888 int event_line;
1889 struct d40_phy_res *phys;
1890 int i;
1891 int j;
1892 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001893 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001894 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001895 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001896
1897 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001898 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001899
1900 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1901 dev_type = d40c->dma_cfg.src_dev_type;
1902 log_num = 2 * dev_type;
1903 is_src = true;
1904 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1905 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1906 /* dst event lines are used for logical memcpy */
1907 dev_type = d40c->dma_cfg.dst_dev_type;
1908 log_num = 2 * dev_type + 1;
1909 is_src = false;
1910 } else
1911 return -EINVAL;
1912
1913 event_group = D40_TYPE_TO_GROUP(dev_type);
1914 event_line = D40_TYPE_TO_EVENT(dev_type);
1915
1916 if (!is_log) {
1917 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1918 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001919 if (d40c->dma_cfg.use_fixed_channel) {
1920 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001921 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301922 0, is_log,
1923 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001924 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001925 } else {
1926 for (i = 0; i < num_phy_chans; i++) {
1927 if (d40_alloc_mask_set(&phys[i], is_src,
1928 0, is_log,
1929 first_phy_user))
1930 goto found_phy;
1931 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001932 }
1933 } else
1934 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1935 int phy_num = j + event_group * 2;
1936 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001937 if (d40_alloc_mask_set(&phys[i],
1938 is_src,
1939 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301940 is_log,
1941 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001942 goto found_phy;
1943 }
1944 }
1945 return -EINVAL;
1946found_phy:
1947 d40c->phy_chan = &phys[i];
1948 d40c->log_num = D40_PHY_CHAN;
1949 goto out;
1950 }
1951 if (dev_type == -1)
1952 return -EINVAL;
1953
1954 /* Find logical channel */
1955 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1956 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301957
1958 if (d40c->dma_cfg.use_fixed_channel) {
1959 i = d40c->dma_cfg.phy_channel;
1960
1961 if ((i != phy_num) && (i != phy_num + 1)) {
1962 dev_err(chan2dev(d40c),
1963 "invalid fixed phy channel %d\n", i);
1964 return -EINVAL;
1965 }
1966
1967 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1968 is_log, first_phy_user))
1969 goto found_log;
1970
1971 dev_err(chan2dev(d40c),
1972 "could not allocate fixed phy channel %d\n", i);
1973 return -EINVAL;
1974 }
1975
Linus Walleij8d318a52010-03-30 15:33:42 +02001976 /*
1977 * Spread logical channels across all available physical rather
1978 * than pack every logical channel at the first available phy
1979 * channels.
1980 */
1981 if (is_src) {
1982 for (i = phy_num; i < phy_num + 2; i++) {
1983 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301984 event_line, is_log,
1985 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001986 goto found_log;
1987 }
1988 } else {
1989 for (i = phy_num + 1; i >= phy_num; i--) {
1990 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301991 event_line, is_log,
1992 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001993 goto found_log;
1994 }
1995 }
1996 }
1997 return -EINVAL;
1998
1999found_log:
2000 d40c->phy_chan = &phys[i];
2001 d40c->log_num = log_num;
2002out:
2003
2004 if (is_log)
2005 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2006 else
2007 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2008
2009 return 0;
2010
2011}
2012
Linus Walleij8d318a52010-03-30 15:33:42 +02002013static int d40_config_memcpy(struct d40_chan *d40c)
2014{
2015 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2016
2017 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
2018 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
2019 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
Lee Jones664a57e2013-05-03 15:31:53 +01002020 d40c->dma_cfg.dst_dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002021
2022 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2023 dma_has_cap(DMA_SLAVE, cap)) {
2024 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
2025 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002026 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002027 return -EINVAL;
2028 }
2029
2030 return 0;
2031}
2032
Linus Walleij8d318a52010-03-30 15:33:42 +02002033static int d40_free_dma(struct d40_chan *d40c)
2034{
2035
2036 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00002037 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02002038 struct d40_phy_res *phy = d40c->phy_chan;
2039 bool is_src;
2040
2041 /* Terminate all queued and active transfers */
2042 d40_term_all(d40c);
2043
2044 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002045 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002046 return -EINVAL;
2047 }
2048
2049 if (phy->allocated_src == D40_ALLOC_FREE &&
2050 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002051 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002052 return -EINVAL;
2053 }
2054
Linus Walleij8d318a52010-03-30 15:33:42 +02002055 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
2056 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
2057 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002058 is_src = false;
2059 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
2060 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002061 is_src = true;
2062 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002063 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002064 return -EINVAL;
2065 }
2066
Narayanan G7fb3e752011-11-17 17:26:41 +05302067 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002068 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2069 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302070 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302071 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002072 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302073
Narayanan G1bdae6f2012-02-09 12:41:37 +05302074 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2075
2076 if (chan_is_logical(d40c))
2077 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2078 else
2079 d40c->base->lookup_phy_chans[phy->num] = NULL;
2080
Narayanan G7fb3e752011-11-17 17:26:41 +05302081 if (d40c->busy) {
2082 pm_runtime_mark_last_busy(d40c->base->dev);
2083 pm_runtime_put_autosuspend(d40c->base->dev);
2084 }
2085
2086 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002087 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002088 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302089out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002090
Narayanan G7fb3e752011-11-17 17:26:41 +05302091 pm_runtime_mark_last_busy(d40c->base->dev);
2092 pm_runtime_put_autosuspend(d40c->base->dev);
2093 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002094}
2095
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002096static bool d40_is_paused(struct d40_chan *d40c)
2097{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002098 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002099 bool is_paused = false;
2100 unsigned long flags;
2101 void __iomem *active_reg;
2102 u32 status;
2103 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002104
2105 spin_lock_irqsave(&d40c->lock, flags);
2106
Rabin Vincent724a8572011-01-25 11:18:08 +01002107 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002108 if (d40c->phy_chan->num % 2 == 0)
2109 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2110 else
2111 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2112
2113 status = (readl(active_reg) &
2114 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2115 D40_CHAN_POS(d40c->phy_chan->num);
2116 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2117 is_paused = true;
2118
2119 goto _exit;
2120 }
2121
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002122 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002123 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002124 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002125 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002126 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002127 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002128 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002129 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002130 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002131 goto _exit;
2132 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002133
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002134 status = (status & D40_EVENTLINE_MASK(event)) >>
2135 D40_EVENTLINE_POS(event);
2136
2137 if (status != D40_DMA_RUN)
2138 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002139_exit:
2140 spin_unlock_irqrestore(&d40c->lock, flags);
2141 return is_paused;
2142
2143}
2144
Linus Walleij8d318a52010-03-30 15:33:42 +02002145static u32 stedma40_residue(struct dma_chan *chan)
2146{
2147 struct d40_chan *d40c =
2148 container_of(chan, struct d40_chan, chan);
2149 u32 bytes_left;
2150 unsigned long flags;
2151
2152 spin_lock_irqsave(&d40c->lock, flags);
2153 bytes_left = d40_residue(d40c);
2154 spin_unlock_irqrestore(&d40c->lock, flags);
2155
2156 return bytes_left;
2157}
2158
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002159static int
2160d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2161 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002162 unsigned int sg_len, dma_addr_t src_dev_addr,
2163 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002164{
2165 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2166 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2167 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002168 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002169
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002170 ret = d40_log_sg_to_lli(sg_src, sg_len,
2171 src_dev_addr,
2172 desc->lli_log.src,
2173 chan->log_def.lcsp1,
2174 src_info->data_width,
2175 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002176
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002177 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2178 dst_dev_addr,
2179 desc->lli_log.dst,
2180 chan->log_def.lcsp3,
2181 dst_info->data_width,
2182 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002183
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002184 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002185}
2186
2187static int
2188d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2189 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002190 unsigned int sg_len, dma_addr_t src_dev_addr,
2191 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002192{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002193 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2194 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2195 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002196 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002197 int ret;
2198
Rabin Vincent0c842b52011-01-25 11:18:35 +01002199 if (desc->cyclic)
2200 flags |= LLI_CYCLIC | LLI_TERM_INT;
2201
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002202 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2203 desc->lli_phy.src,
2204 virt_to_phys(desc->lli_phy.src),
2205 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002206 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002207
2208 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2209 desc->lli_phy.dst,
2210 virt_to_phys(desc->lli_phy.dst),
2211 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002212 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002213
2214 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2215 desc->lli_pool.size, DMA_TO_DEVICE);
2216
2217 return ret < 0 ? ret : 0;
2218}
2219
Rabin Vincent5f811582011-01-25 11:18:18 +01002220static struct d40_desc *
2221d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2222 unsigned int sg_len, unsigned long dma_flags)
2223{
2224 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2225 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002226 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002227
2228 desc = d40_desc_get(chan);
2229 if (!desc)
2230 return NULL;
2231
2232 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2233 cfg->dst_info.data_width);
2234 if (desc->lli_len < 0) {
2235 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002236 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002237 }
2238
Rabin Vincentdbd88782011-01-25 11:18:19 +01002239 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2240 if (ret < 0) {
2241 chan_err(chan, "Could not allocate lli\n");
2242 goto err;
2243 }
2244
Rabin Vincent5f811582011-01-25 11:18:18 +01002245 desc->lli_current = 0;
2246 desc->txd.flags = dma_flags;
2247 desc->txd.tx_submit = d40_tx_submit;
2248
2249 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2250
2251 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002252
2253err:
2254 d40_desc_free(chan, desc);
2255 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002256}
2257
Rabin Vincentcade1d32011-01-25 11:18:23 +01002258static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302259d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002260{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002261 struct stedma40_platform_data *plat = chan->base->plat_data;
2262 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002263 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002264
Rabin Vincentcade1d32011-01-25 11:18:23 +01002265 if (chan->runtime_addr)
2266 return chan->runtime_addr;
2267
Vinod Kouldb8196d2011-10-13 22:34:23 +05302268 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002269 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302270 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002271 addr = plat->dev_tx[cfg->dst_dev_type];
2272
2273 return addr;
2274}
2275
2276static struct dma_async_tx_descriptor *
2277d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2278 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302279 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002280{
2281 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002282 dma_addr_t src_dev_addr = 0;
2283 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002284 struct d40_desc *desc;
2285 unsigned long flags;
2286 int ret;
2287
2288 if (!chan->phy_chan) {
2289 chan_err(chan, "Cannot prepare unallocated channel\n");
2290 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002291 }
2292
Rabin Vincentcade1d32011-01-25 11:18:23 +01002293 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002294
Rabin Vincentcade1d32011-01-25 11:18:23 +01002295 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2296 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002297 goto err;
2298
Rabin Vincent0c842b52011-01-25 11:18:35 +01002299 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2300 desc->cyclic = true;
2301
Linus Walleij7e426da2012-04-12 18:12:52 +02002302 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002303 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2304
Vinod Kouldb8196d2011-10-13 22:34:23 +05302305 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002306 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302307 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002308 dst_dev_addr = dev_addr;
2309 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002310
2311 if (chan_is_logical(chan))
2312 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002313 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002314 else
2315 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002316 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002317
2318 if (ret) {
2319 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2320 chan_is_logical(chan) ? "log" : "phy", ret);
2321 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002322 }
2323
Per Forlin82babbb362011-08-29 13:33:35 +02002324 /*
2325 * add descriptor to the prepare queue in order to be able
2326 * to free them later in terminate_all
2327 */
2328 list_add_tail(&desc->node, &chan->prepare_queue);
2329
Rabin Vincentcade1d32011-01-25 11:18:23 +01002330 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002331
Rabin Vincentcade1d32011-01-25 11:18:23 +01002332 return &desc->txd;
2333
Linus Walleij8d318a52010-03-30 15:33:42 +02002334err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002335 if (desc)
2336 d40_desc_free(chan, desc);
2337 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002338 return NULL;
2339}
Linus Walleij8d318a52010-03-30 15:33:42 +02002340
2341bool stedma40_filter(struct dma_chan *chan, void *data)
2342{
2343 struct stedma40_chan_cfg *info = data;
2344 struct d40_chan *d40c =
2345 container_of(chan, struct d40_chan, chan);
2346 int err;
2347
2348 if (data) {
2349 err = d40_validate_conf(d40c, info);
2350 if (!err)
2351 d40c->dma_cfg = *info;
2352 } else
2353 err = d40_config_memcpy(d40c);
2354
Rabin Vincentce2ca122010-10-12 13:00:49 +00002355 if (!err)
2356 d40c->configured = true;
2357
Linus Walleij8d318a52010-03-30 15:33:42 +02002358 return err == 0;
2359}
2360EXPORT_SYMBOL(stedma40_filter);
2361
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002362static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2363{
2364 bool realtime = d40c->dma_cfg.realtime;
2365 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002366 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002367 u32 event = D40_TYPE_TO_EVENT(dev_type);
2368 u32 group = D40_TYPE_TO_GROUP(dev_type);
2369 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302370 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002371 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302372
Tong Liu3cb645d2012-09-26 10:07:30 +00002373 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302374 /*
2375 * Due to a hardware bug, in some cases a logical channel triggered by
2376 * a high priority destination event line can generate extra packet
2377 * transactions.
2378 *
2379 * The workaround is to not set the high priority level for the
2380 * destination event lines that trigger logical channels.
2381 */
2382 if (!src && chan_is_logical(d40c))
2383 highprio = false;
2384
Tong Liu3cb645d2012-09-26 10:07:30 +00002385 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002386
2387 /* Destination event lines are stored in the upper halfword */
2388 if (!src)
2389 bit <<= 16;
2390
2391 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2392 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2393}
2394
2395static void d40_set_prio_realtime(struct d40_chan *d40c)
2396{
2397 if (d40c->base->rev < 3)
2398 return;
2399
2400 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2401 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2402 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2403
2404 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2405 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2406 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2407}
2408
Linus Walleij8d318a52010-03-30 15:33:42 +02002409/* DMA ENGINE functions */
2410static int d40_alloc_chan_resources(struct dma_chan *chan)
2411{
2412 int err;
2413 unsigned long flags;
2414 struct d40_chan *d40c =
2415 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002416 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002417 spin_lock_irqsave(&d40c->lock, flags);
2418
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002419 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002420
Rabin Vincentce2ca122010-10-12 13:00:49 +00002421 /* If no dma configuration is set use default configuration (memcpy) */
2422 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002423 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002424 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002425 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002426 goto fail;
2427 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002428 }
2429
Narayanan G5cd326f2011-11-30 19:20:42 +05302430 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002431 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002432 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302433 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002434 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002435 }
2436
Narayanan G7fb3e752011-11-17 17:26:41 +05302437 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002438 /* Fill in basic CFG register values */
2439 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002440 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002441
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002442 d40_set_prio_realtime(d40c);
2443
Rabin Vincent724a8572011-01-25 11:18:08 +01002444 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002445 d40_log_cfg(&d40c->dma_cfg,
2446 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2447
2448 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2449 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002450 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002451 else
2452 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002453 d40c->dma_cfg.dst_dev_type *
2454 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Linus Walleijef1872e2010-06-20 21:24:52 +00002455 }
2456
Narayanan G5cd326f2011-11-30 19:20:42 +05302457 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2458 chan_is_logical(d40c) ? "logical" : "physical",
2459 d40c->phy_chan->num,
2460 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2461
2462
Linus Walleijef1872e2010-06-20 21:24:52 +00002463 /*
2464 * Only write channel configuration to the DMA if the physical
2465 * resource is free. In case of multiple logical channels
2466 * on the same physical resource, only the first write is necessary.
2467 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002468 if (is_free_phy)
2469 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002470fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302471 pm_runtime_mark_last_busy(d40c->base->dev);
2472 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002473 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002474 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002475}
2476
2477static void d40_free_chan_resources(struct dma_chan *chan)
2478{
2479 struct d40_chan *d40c =
2480 container_of(chan, struct d40_chan, chan);
2481 int err;
2482 unsigned long flags;
2483
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002484 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002485 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002486 return;
2487 }
2488
Linus Walleij8d318a52010-03-30 15:33:42 +02002489 spin_lock_irqsave(&d40c->lock, flags);
2490
2491 err = d40_free_dma(d40c);
2492
2493 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002494 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002495 spin_unlock_irqrestore(&d40c->lock, flags);
2496}
2497
2498static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2499 dma_addr_t dst,
2500 dma_addr_t src,
2501 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002502 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002503{
Rabin Vincent95944c62011-01-25 11:18:17 +01002504 struct scatterlist dst_sg;
2505 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002506
Rabin Vincent95944c62011-01-25 11:18:17 +01002507 sg_init_table(&dst_sg, 1);
2508 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002509
Rabin Vincent95944c62011-01-25 11:18:17 +01002510 sg_dma_address(&dst_sg) = dst;
2511 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002512
Rabin Vincent95944c62011-01-25 11:18:17 +01002513 sg_dma_len(&dst_sg) = size;
2514 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002515
Rabin Vincentcade1d32011-01-25 11:18:23 +01002516 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002517}
2518
Ira Snyder0d688662010-09-30 11:46:47 +00002519static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002520d40_prep_memcpy_sg(struct dma_chan *chan,
2521 struct scatterlist *dst_sg, unsigned int dst_nents,
2522 struct scatterlist *src_sg, unsigned int src_nents,
2523 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002524{
2525 if (dst_nents != src_nents)
2526 return NULL;
2527
Rabin Vincentcade1d32011-01-25 11:18:23 +01002528 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002529}
2530
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002531static struct dma_async_tx_descriptor *
2532d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2533 unsigned int sg_len, enum dma_transfer_direction direction,
2534 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002535{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002536 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002537 return NULL;
2538
Rabin Vincentcade1d32011-01-25 11:18:23 +01002539 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002540}
2541
Rabin Vincent0c842b52011-01-25 11:18:35 +01002542static struct dma_async_tx_descriptor *
2543dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2544 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002545 enum dma_transfer_direction direction, unsigned long flags,
2546 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002547{
2548 unsigned int periods = buf_len / period_len;
2549 struct dma_async_tx_descriptor *txd;
2550 struct scatterlist *sg;
2551 int i;
2552
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002553 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002554 for (i = 0; i < periods; i++) {
2555 sg_dma_address(&sg[i]) = dma_addr;
2556 sg_dma_len(&sg[i]) = period_len;
2557 dma_addr += period_len;
2558 }
2559
2560 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002561 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002562 sg[periods].page_link =
2563 ((unsigned long)sg | 0x01) & ~0x02;
2564
2565 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2566 DMA_PREP_INTERRUPT);
2567
2568 kfree(sg);
2569
2570 return txd;
2571}
2572
Linus Walleij8d318a52010-03-30 15:33:42 +02002573static enum dma_status d40_tx_status(struct dma_chan *chan,
2574 dma_cookie_t cookie,
2575 struct dma_tx_state *txstate)
2576{
2577 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002578 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002579
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002580 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002581 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002582 return -EINVAL;
2583 }
2584
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002585 ret = dma_cookie_status(chan, cookie, txstate);
2586 if (ret != DMA_SUCCESS)
2587 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002588
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002589 if (d40_is_paused(d40c))
2590 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002591
2592 return ret;
2593}
2594
2595static void d40_issue_pending(struct dma_chan *chan)
2596{
2597 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2598 unsigned long flags;
2599
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002600 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002601 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002602 return;
2603 }
2604
Linus Walleij8d318a52010-03-30 15:33:42 +02002605 spin_lock_irqsave(&d40c->lock, flags);
2606
Per Forlina8f30672011-06-26 23:29:52 +02002607 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2608
2609 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002610 if (!d40c->busy)
2611 (void) d40_queue_start(d40c);
2612
2613 spin_unlock_irqrestore(&d40c->lock, flags);
2614}
2615
Narayanan G1bdae6f2012-02-09 12:41:37 +05302616static void d40_terminate_all(struct dma_chan *chan)
2617{
2618 unsigned long flags;
2619 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2620 int ret;
2621
2622 spin_lock_irqsave(&d40c->lock, flags);
2623
2624 pm_runtime_get_sync(d40c->base->dev);
2625 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2626 if (ret)
2627 chan_err(d40c, "Failed to stop channel\n");
2628
2629 d40_term_all(d40c);
2630 pm_runtime_mark_last_busy(d40c->base->dev);
2631 pm_runtime_put_autosuspend(d40c->base->dev);
2632 if (d40c->busy) {
2633 pm_runtime_mark_last_busy(d40c->base->dev);
2634 pm_runtime_put_autosuspend(d40c->base->dev);
2635 }
2636 d40c->busy = false;
2637
2638 spin_unlock_irqrestore(&d40c->lock, flags);
2639}
2640
Rabin Vincent98ca5282011-06-27 11:33:38 +02002641static int
2642dma40_config_to_halfchannel(struct d40_chan *d40c,
2643 struct stedma40_half_channel_info *info,
2644 enum dma_slave_buswidth width,
2645 u32 maxburst)
2646{
2647 enum stedma40_periph_data_width addr_width;
2648 int psize;
2649
2650 switch (width) {
2651 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2652 addr_width = STEDMA40_BYTE_WIDTH;
2653 break;
2654 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2655 addr_width = STEDMA40_HALFWORD_WIDTH;
2656 break;
2657 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2658 addr_width = STEDMA40_WORD_WIDTH;
2659 break;
2660 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2661 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2662 break;
2663 default:
2664 dev_err(d40c->base->dev,
2665 "illegal peripheral address width "
2666 "requested (%d)\n",
2667 width);
2668 return -EINVAL;
2669 }
2670
2671 if (chan_is_logical(d40c)) {
2672 if (maxburst >= 16)
2673 psize = STEDMA40_PSIZE_LOG_16;
2674 else if (maxburst >= 8)
2675 psize = STEDMA40_PSIZE_LOG_8;
2676 else if (maxburst >= 4)
2677 psize = STEDMA40_PSIZE_LOG_4;
2678 else
2679 psize = STEDMA40_PSIZE_LOG_1;
2680 } else {
2681 if (maxburst >= 16)
2682 psize = STEDMA40_PSIZE_PHY_16;
2683 else if (maxburst >= 8)
2684 psize = STEDMA40_PSIZE_PHY_8;
2685 else if (maxburst >= 4)
2686 psize = STEDMA40_PSIZE_PHY_4;
2687 else
2688 psize = STEDMA40_PSIZE_PHY_1;
2689 }
2690
2691 info->data_width = addr_width;
2692 info->psize = psize;
2693 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2694
2695 return 0;
2696}
2697
Linus Walleij95e14002010-08-04 13:37:45 +02002698/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002699static int d40_set_runtime_config(struct dma_chan *chan,
2700 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002701{
2702 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2703 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002704 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002705 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002706 u32 src_maxburst, dst_maxburst;
2707 int ret;
2708
2709 src_addr_width = config->src_addr_width;
2710 src_maxburst = config->src_maxburst;
2711 dst_addr_width = config->dst_addr_width;
2712 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002713
Vinod Kouldb8196d2011-10-13 22:34:23 +05302714 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002715 dma_addr_t dev_addr_rx =
2716 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2717
2718 config_addr = config->src_addr;
2719 if (dev_addr_rx)
2720 dev_dbg(d40c->base->dev,
2721 "channel has a pre-wired RX address %08x "
2722 "overriding with %08x\n",
2723 dev_addr_rx, config_addr);
2724 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2725 dev_dbg(d40c->base->dev,
2726 "channel was not configured for peripheral "
2727 "to memory transfer (%d) overriding\n",
2728 cfg->dir);
2729 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2730
Rabin Vincent98ca5282011-06-27 11:33:38 +02002731 /* Configure the memory side */
2732 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2733 dst_addr_width = src_addr_width;
2734 if (dst_maxburst == 0)
2735 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002736
Vinod Kouldb8196d2011-10-13 22:34:23 +05302737 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002738 dma_addr_t dev_addr_tx =
2739 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2740
2741 config_addr = config->dst_addr;
2742 if (dev_addr_tx)
2743 dev_dbg(d40c->base->dev,
2744 "channel has a pre-wired TX address %08x "
2745 "overriding with %08x\n",
2746 dev_addr_tx, config_addr);
2747 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2748 dev_dbg(d40c->base->dev,
2749 "channel was not configured for memory "
2750 "to peripheral transfer (%d) overriding\n",
2751 cfg->dir);
2752 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2753
Rabin Vincent98ca5282011-06-27 11:33:38 +02002754 /* Configure the memory side */
2755 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2756 src_addr_width = dst_addr_width;
2757 if (src_maxburst == 0)
2758 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002759 } else {
2760 dev_err(d40c->base->dev,
2761 "unrecognized channel direction %d\n",
2762 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002763 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002764 }
2765
Rabin Vincent98ca5282011-06-27 11:33:38 +02002766 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002767 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002768 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2769 src_maxburst,
2770 src_addr_width,
2771 dst_maxburst,
2772 dst_addr_width);
2773 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002774 }
2775
Per Forlin92bb6cd2011-10-13 12:11:36 +02002776 if (src_maxburst > 16) {
2777 src_maxburst = 16;
2778 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2779 } else if (dst_maxburst > 16) {
2780 dst_maxburst = 16;
2781 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2782 }
2783
Rabin Vincent98ca5282011-06-27 11:33:38 +02002784 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2785 src_addr_width,
2786 src_maxburst);
2787 if (ret)
2788 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002789
Rabin Vincent98ca5282011-06-27 11:33:38 +02002790 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2791 dst_addr_width,
2792 dst_maxburst);
2793 if (ret)
2794 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002795
Per Forlina59670a2010-10-06 09:05:27 +00002796 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002797 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002798 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2799 else
2800 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2801 &d40c->dst_def_cfg, false);
2802
Linus Walleij95e14002010-08-04 13:37:45 +02002803 /* These settings will take precedence later */
2804 d40c->runtime_addr = config_addr;
2805 d40c->runtime_direction = config->direction;
2806 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002807 "configured channel %s for %s, data width %d/%d, "
2808 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002809 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302810 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002811 src_addr_width, dst_addr_width,
2812 src_maxburst, dst_maxburst);
2813
2814 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002815}
2816
Linus Walleij05827632010-05-17 16:30:42 -07002817static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2818 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002819{
Linus Walleij8d318a52010-03-30 15:33:42 +02002820 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2821
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002822 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002823 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002824 return -EINVAL;
2825 }
2826
Linus Walleij8d318a52010-03-30 15:33:42 +02002827 switch (cmd) {
2828 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302829 d40_terminate_all(chan);
2830 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002831 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002832 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002833 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002834 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002835 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002836 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002837 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002838 default:
2839 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002840 }
2841
2842 /* Other commands are unimplemented */
2843 return -ENXIO;
2844}
2845
2846/* Initialization functions */
2847
2848static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2849 struct d40_chan *chans, int offset,
2850 int num_chans)
2851{
2852 int i = 0;
2853 struct d40_chan *d40c;
2854
2855 INIT_LIST_HEAD(&dma->channels);
2856
2857 for (i = offset; i < offset + num_chans; i++) {
2858 d40c = &chans[i];
2859 d40c->base = base;
2860 d40c->chan.device = dma;
2861
Linus Walleij8d318a52010-03-30 15:33:42 +02002862 spin_lock_init(&d40c->lock);
2863
2864 d40c->log_num = D40_PHY_CHAN;
2865
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002866 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002867 INIT_LIST_HEAD(&d40c->active);
2868 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002869 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002870 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002871 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002872
Linus Walleij8d318a52010-03-30 15:33:42 +02002873 tasklet_init(&d40c->tasklet, dma_tasklet,
2874 (unsigned long) d40c);
2875
2876 list_add_tail(&d40c->chan.device_node,
2877 &dma->channels);
2878 }
2879}
2880
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002881static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2882{
2883 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2884 dev->device_prep_slave_sg = d40_prep_slave_sg;
2885
2886 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2887 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2888
2889 /*
2890 * This controller can only access address at even
2891 * 32bit boundaries, i.e. 2^2
2892 */
2893 dev->copy_align = 2;
2894 }
2895
2896 if (dma_has_cap(DMA_SG, dev->cap_mask))
2897 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2898
Rabin Vincent0c842b52011-01-25 11:18:35 +01002899 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2900 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2901
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002902 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2903 dev->device_free_chan_resources = d40_free_chan_resources;
2904 dev->device_issue_pending = d40_issue_pending;
2905 dev->device_tx_status = d40_tx_status;
2906 dev->device_control = d40_control;
2907 dev->dev = base->dev;
2908}
2909
Linus Walleij8d318a52010-03-30 15:33:42 +02002910static int __init d40_dmaengine_init(struct d40_base *base,
2911 int num_reserved_chans)
2912{
2913 int err ;
2914
2915 d40_chan_init(base, &base->dma_slave, base->log_chans,
2916 0, base->num_log_chans);
2917
2918 dma_cap_zero(base->dma_slave.cap_mask);
2919 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002920 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002921
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002922 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002923
2924 err = dma_async_device_register(&base->dma_slave);
2925
2926 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002927 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002928 goto failure1;
2929 }
2930
2931 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002932 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002933
2934 dma_cap_zero(base->dma_memcpy.cap_mask);
2935 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002936 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002937
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002938 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002939
2940 err = dma_async_device_register(&base->dma_memcpy);
2941
2942 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002943 d40_err(base->dev,
2944 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002945 goto failure2;
2946 }
2947
2948 d40_chan_init(base, &base->dma_both, base->phy_chans,
2949 0, num_reserved_chans);
2950
2951 dma_cap_zero(base->dma_both.cap_mask);
2952 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2953 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002954 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002955 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002956
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002957 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002958 err = dma_async_device_register(&base->dma_both);
2959
2960 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002961 d40_err(base->dev,
2962 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002963 goto failure3;
2964 }
2965 return 0;
2966failure3:
2967 dma_async_device_unregister(&base->dma_memcpy);
2968failure2:
2969 dma_async_device_unregister(&base->dma_slave);
2970failure1:
2971 return err;
2972}
2973
Narayanan G7fb3e752011-11-17 17:26:41 +05302974/* Suspend resume functionality */
2975#ifdef CONFIG_PM
2976static int dma40_pm_suspend(struct device *dev)
2977{
Narayanan G28c7a192011-11-22 13:56:55 +05302978 struct platform_device *pdev = to_platform_device(dev);
2979 struct d40_base *base = platform_get_drvdata(pdev);
2980 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302981
Narayanan G28c7a192011-11-22 13:56:55 +05302982 if (base->lcpa_regulator)
2983 ret = regulator_disable(base->lcpa_regulator);
2984 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302985}
2986
2987static int dma40_runtime_suspend(struct device *dev)
2988{
2989 struct platform_device *pdev = to_platform_device(dev);
2990 struct d40_base *base = platform_get_drvdata(pdev);
2991
2992 d40_save_restore_registers(base, true);
2993
2994 /* Don't disable/enable clocks for v1 due to HW bugs */
2995 if (base->rev != 1)
2996 writel_relaxed(base->gcc_pwr_off_mask,
2997 base->virtbase + D40_DREG_GCC);
2998
2999 return 0;
3000}
3001
3002static int dma40_runtime_resume(struct device *dev)
3003{
3004 struct platform_device *pdev = to_platform_device(dev);
3005 struct d40_base *base = platform_get_drvdata(pdev);
3006
3007 if (base->initialized)
3008 d40_save_restore_registers(base, false);
3009
3010 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3011 base->virtbase + D40_DREG_GCC);
3012 return 0;
3013}
3014
Narayanan G28c7a192011-11-22 13:56:55 +05303015static int dma40_resume(struct device *dev)
3016{
3017 struct platform_device *pdev = to_platform_device(dev);
3018 struct d40_base *base = platform_get_drvdata(pdev);
3019 int ret = 0;
3020
3021 if (base->lcpa_regulator)
3022 ret = regulator_enable(base->lcpa_regulator);
3023
3024 return ret;
3025}
Narayanan G7fb3e752011-11-17 17:26:41 +05303026
3027static const struct dev_pm_ops dma40_pm_ops = {
3028 .suspend = dma40_pm_suspend,
3029 .runtime_suspend = dma40_runtime_suspend,
3030 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303031 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303032};
3033#define DMA40_PM_OPS (&dma40_pm_ops)
3034#else
3035#define DMA40_PM_OPS NULL
3036#endif
3037
Linus Walleij8d318a52010-03-30 15:33:42 +02003038/* Initialization functions. */
3039
3040static int __init d40_phy_res_init(struct d40_base *base)
3041{
3042 int i;
3043 int num_phy_chans_avail = 0;
3044 u32 val[2];
3045 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303046 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003047
3048 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3049 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3050
3051 for (i = 0; i < base->num_phy_chans; i++) {
3052 base->phy_res[i].num = i;
3053 odd_even_bit += 2 * ((i % 2) == 0);
3054 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3055 /* Mark security only channels as occupied */
3056 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3057 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303058 base->phy_res[i].reserved = true;
3059 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3060 D40_DREG_GCC_SRC);
3061 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3062 D40_DREG_GCC_DST);
3063
3064
Linus Walleij8d318a52010-03-30 15:33:42 +02003065 } else {
3066 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3067 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303068 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003069 num_phy_chans_avail++;
3070 }
3071 spin_lock_init(&base->phy_res[i].lock);
3072 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003073
3074 /* Mark disabled channels as occupied */
3075 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003076 int chan = base->plat_data->disabled_channels[i];
3077
3078 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3079 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303080 base->phy_res[chan].reserved = true;
3081 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3082 D40_DREG_GCC_SRC);
3083 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3084 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003085 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003086 }
3087
Fabio Baltieri74070482012-12-18 12:25:14 +01003088 /* Mark soft_lli channels */
3089 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3090 int chan = base->plat_data->soft_lli_chans[i];
3091
3092 base->phy_res[chan].use_soft_lli = true;
3093 }
3094
Linus Walleij8d318a52010-03-30 15:33:42 +02003095 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3096 num_phy_chans_avail, base->num_phy_chans);
3097
3098 /* Verify settings extended vs standard */
3099 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3100
3101 for (i = 0; i < base->num_phy_chans; i++) {
3102
3103 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3104 (val[0] & 0x3) != 1)
3105 dev_info(base->dev,
3106 "[%s] INFO: channel %d is misconfigured (%d)\n",
3107 __func__, i, val[0] & 0x3);
3108
3109 val[0] = val[0] >> 2;
3110 }
3111
Narayanan G7fb3e752011-11-17 17:26:41 +05303112 /*
3113 * To keep things simple, Enable all clocks initially.
3114 * The clocks will get managed later post channel allocation.
3115 * The clocks for the event lines on which reserved channels exists
3116 * are not managed here.
3117 */
3118 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3119 base->gcc_pwr_off_mask = gcc;
3120
Linus Walleij8d318a52010-03-30 15:33:42 +02003121 return num_phy_chans_avail;
3122}
3123
3124static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3125{
Linus Walleij8d318a52010-03-30 15:33:42 +02003126 struct stedma40_platform_data *plat_data;
3127 struct clk *clk = NULL;
3128 void __iomem *virtbase = NULL;
3129 struct resource *res = NULL;
3130 struct d40_base *base = NULL;
3131 int num_log_chans = 0;
3132 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003133 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003134 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003135 u32 pid;
3136 u32 cid;
3137 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003138
3139 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003140 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003141 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003142 goto failure;
3143 }
3144
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003145 clk_ret = clk_prepare_enable(clk);
3146 if (clk_ret) {
3147 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3148 goto failure;
3149 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003150
3151 /* Get IO for DMAC base address */
3152 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3153 if (!res)
3154 goto failure;
3155
3156 if (request_mem_region(res->start, resource_size(res),
3157 D40_NAME " I/O base") == NULL)
3158 goto failure;
3159
3160 virtbase = ioremap(res->start, resource_size(res));
3161 if (!virtbase)
3162 goto failure;
3163
Linus Walleijf4b89762011-06-27 11:33:46 +02003164 /* This is just a regular AMBA PrimeCell ID actually */
3165 for (pid = 0, i = 0; i < 4; i++)
3166 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3167 & 255) << (i * 8);
3168 for (cid = 0, i = 0; i < 4; i++)
3169 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3170 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003171
Linus Walleijf4b89762011-06-27 11:33:46 +02003172 if (cid != AMBA_CID) {
3173 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003174 goto failure;
3175 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003176 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3177 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3178 AMBA_MANF_BITS(pid),
3179 AMBA_VENDOR_ST);
3180 goto failure;
3181 }
3182 /*
3183 * HW revision:
3184 * DB8500ed has revision 0
3185 * ? has revision 1
3186 * DB8500v1 has revision 2
3187 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003188 * AP9540v1 has revision 4
3189 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003190 */
3191 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003192
Gerald Baeza47db92f2012-09-21 21:21:37 +02003193 plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003194
Gerald Baeza47db92f2012-09-21 21:21:37 +02003195 /* The number of physical channels on this HW */
3196 if (plat_data->num_of_phy_chans)
3197 num_phy_chans = plat_data->num_of_phy_chans;
3198 else
3199 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3200
3201 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x with %d physical channels\n",
3202 rev, res->start, num_phy_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003203
Narayanan G1bdae6f2012-02-09 12:41:37 +05303204 if (rev < 2) {
3205 d40_err(&pdev->dev, "hardware revision: %d is not supported",
3206 rev);
3207 goto failure;
3208 }
3209
Linus Walleij8d318a52010-03-30 15:33:42 +02003210 /* Count the number of logical channels in use */
3211 for (i = 0; i < plat_data->dev_len; i++)
3212 if (plat_data->dev_rx[i] != 0)
3213 num_log_chans++;
3214
3215 for (i = 0; i < plat_data->dev_len; i++)
3216 if (plat_data->dev_tx[i] != 0)
3217 num_log_chans++;
3218
3219 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003220 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003221 sizeof(struct d40_chan), GFP_KERNEL);
3222
3223 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003224 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003225 goto failure;
3226 }
3227
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003228 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003229 base->clk = clk;
3230 base->num_phy_chans = num_phy_chans;
3231 base->num_log_chans = num_log_chans;
3232 base->phy_start = res->start;
3233 base->phy_size = resource_size(res);
3234 base->virtbase = virtbase;
3235 base->plat_data = plat_data;
3236 base->dev = &pdev->dev;
3237 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3238 base->log_chans = &base->phy_chans[num_phy_chans];
3239
Tong Liu3cb645d2012-09-26 10:07:30 +00003240 if (base->plat_data->num_of_phy_chans == 14) {
3241 base->gen_dmac.backup = d40_backup_regs_v4b;
3242 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3243 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3244 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3245 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3246 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3247 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3248 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3249 base->gen_dmac.il = il_v4b;
3250 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3251 base->gen_dmac.init_reg = dma_init_reg_v4b;
3252 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3253 } else {
3254 if (base->rev >= 3) {
3255 base->gen_dmac.backup = d40_backup_regs_v4a;
3256 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3257 }
3258 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3259 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3260 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3261 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3262 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3263 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3264 base->gen_dmac.il = il_v4a;
3265 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3266 base->gen_dmac.init_reg = dma_init_reg_v4a;
3267 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3268 }
3269
Linus Walleij8d318a52010-03-30 15:33:42 +02003270 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3271 GFP_KERNEL);
3272 if (!base->phy_res)
3273 goto failure;
3274
3275 base->lookup_phy_chans = kzalloc(num_phy_chans *
3276 sizeof(struct d40_chan *),
3277 GFP_KERNEL);
3278 if (!base->lookup_phy_chans)
3279 goto failure;
3280
Lee Jones664a57e2013-05-03 15:31:53 +01003281 if (num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02003282 /*
3283 * The max number of logical channels are event lines for all
3284 * src devices and dst devices
3285 */
3286 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
3287 sizeof(struct d40_chan *),
3288 GFP_KERNEL);
3289 if (!base->lookup_log_chans)
3290 goto failure;
3291 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00003292
Narayanan G7fb3e752011-11-17 17:26:41 +05303293 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3294 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003295 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303296 if (!base->reg_val_backup_chan)
3297 goto failure;
3298
3299 base->lcla_pool.alloc_map =
3300 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3301 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003302 if (!base->lcla_pool.alloc_map)
3303 goto failure;
3304
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003305 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3306 0, SLAB_HWCACHE_ALIGN,
3307 NULL);
3308 if (base->desc_slab == NULL)
3309 goto failure;
3310
Linus Walleij8d318a52010-03-30 15:33:42 +02003311 return base;
3312
3313failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003314 if (!clk_ret)
3315 clk_disable_unprepare(clk);
3316 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003317 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003318 if (virtbase)
3319 iounmap(virtbase);
3320 if (res)
3321 release_mem_region(res->start,
3322 resource_size(res));
3323 if (virtbase)
3324 iounmap(virtbase);
3325
3326 if (base) {
3327 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303328 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003329 kfree(base->lookup_log_chans);
3330 kfree(base->lookup_phy_chans);
3331 kfree(base->phy_res);
3332 kfree(base);
3333 }
3334
3335 return NULL;
3336}
3337
3338static void __init d40_hw_init(struct d40_base *base)
3339{
3340
Linus Walleij8d318a52010-03-30 15:33:42 +02003341 int i;
3342 u32 prmseo[2] = {0, 0};
3343 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3344 u32 pcmis = 0;
3345 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003346 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3347 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003348
Tong Liu3cb645d2012-09-26 10:07:30 +00003349 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003350 writel(dma_init_reg[i].val,
3351 base->virtbase + dma_init_reg[i].reg);
3352
3353 /* Configure all our dma channels to default settings */
3354 for (i = 0; i < base->num_phy_chans; i++) {
3355
3356 activeo[i % 2] = activeo[i % 2] << 2;
3357
3358 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3359 == D40_ALLOC_PHY) {
3360 activeo[i % 2] |= 3;
3361 continue;
3362 }
3363
3364 /* Enable interrupt # */
3365 pcmis = (pcmis << 1) | 1;
3366
3367 /* Clear interrupt # */
3368 pcicr = (pcicr << 1) | 1;
3369
3370 /* Set channel to physical mode */
3371 prmseo[i % 2] = prmseo[i % 2] << 2;
3372 prmseo[i % 2] |= 1;
3373
3374 }
3375
3376 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3377 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3378 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3379 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3380
3381 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003382 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003383
3384 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003385 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003386
Tong Liu3cb645d2012-09-26 10:07:30 +00003387 /* These are __initdata and cannot be accessed after init */
3388 base->gen_dmac.init_reg = NULL;
3389 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003390}
3391
Linus Walleij508849a2010-06-20 21:26:07 +00003392static int __init d40_lcla_allocate(struct d40_base *base)
3393{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003394 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003395 unsigned long *page_list;
3396 int i, j;
3397 int ret = 0;
3398
3399 /*
3400 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3401 * To full fill this hardware requirement without wasting 256 kb
3402 * we allocate pages until we get an aligned one.
3403 */
3404 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3405 GFP_KERNEL);
3406
3407 if (!page_list) {
3408 ret = -ENOMEM;
3409 goto failure;
3410 }
3411
3412 /* Calculating how many pages that are required */
3413 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3414
3415 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3416 page_list[i] = __get_free_pages(GFP_KERNEL,
3417 base->lcla_pool.pages);
3418 if (!page_list[i]) {
3419
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003420 d40_err(base->dev, "Failed to allocate %d pages.\n",
3421 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003422
3423 for (j = 0; j < i; j++)
3424 free_pages(page_list[j], base->lcla_pool.pages);
3425 goto failure;
3426 }
3427
3428 if ((virt_to_phys((void *)page_list[i]) &
3429 (LCLA_ALIGNMENT - 1)) == 0)
3430 break;
3431 }
3432
3433 for (j = 0; j < i; j++)
3434 free_pages(page_list[j], base->lcla_pool.pages);
3435
3436 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3437 base->lcla_pool.base = (void *)page_list[i];
3438 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003439 /*
3440 * After many attempts and no succees with finding the correct
3441 * alignment, try with allocating a big buffer.
3442 */
Linus Walleij508849a2010-06-20 21:26:07 +00003443 dev_warn(base->dev,
3444 "[%s] Failed to get %d pages @ 18 bit align.\n",
3445 __func__, base->lcla_pool.pages);
3446 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3447 base->num_phy_chans +
3448 LCLA_ALIGNMENT,
3449 GFP_KERNEL);
3450 if (!base->lcla_pool.base_unaligned) {
3451 ret = -ENOMEM;
3452 goto failure;
3453 }
3454
3455 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3456 LCLA_ALIGNMENT);
3457 }
3458
Rabin Vincent026cbc42011-01-25 11:18:14 +01003459 pool->dma_addr = dma_map_single(base->dev, pool->base,
3460 SZ_1K * base->num_phy_chans,
3461 DMA_TO_DEVICE);
3462 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3463 pool->dma_addr = 0;
3464 ret = -ENOMEM;
3465 goto failure;
3466 }
3467
Linus Walleij508849a2010-06-20 21:26:07 +00003468 writel(virt_to_phys(base->lcla_pool.base),
3469 base->virtbase + D40_DREG_LCLA);
3470failure:
3471 kfree(page_list);
3472 return ret;
3473}
3474
Linus Walleij8d318a52010-03-30 15:33:42 +02003475static int __init d40_probe(struct platform_device *pdev)
3476{
3477 int err;
3478 int ret = -ENOENT;
3479 struct d40_base *base;
3480 struct resource *res = NULL;
3481 int num_reserved_chans;
3482 u32 val;
3483
3484 base = d40_hw_detect_init(pdev);
3485
3486 if (!base)
3487 goto failure;
3488
3489 num_reserved_chans = d40_phy_res_init(base);
3490
3491 platform_set_drvdata(pdev, base);
3492
3493 spin_lock_init(&base->interrupt_lock);
3494 spin_lock_init(&base->execmd_lock);
3495
3496 /* Get IO for logical channel parameter address */
3497 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3498 if (!res) {
3499 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003500 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003501 goto failure;
3502 }
3503 base->lcpa_size = resource_size(res);
3504 base->phy_lcpa = res->start;
3505
3506 if (request_mem_region(res->start, resource_size(res),
3507 D40_NAME " I/O lcpa") == NULL) {
3508 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003509 d40_err(&pdev->dev,
3510 "Failed to request LCPA region 0x%x-0x%x\n",
3511 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003512 goto failure;
3513 }
3514
3515 /* We make use of ESRAM memory for this. */
3516 val = readl(base->virtbase + D40_DREG_LCPA);
3517 if (res->start != val && val != 0) {
3518 dev_warn(&pdev->dev,
3519 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3520 __func__, val, res->start);
3521 } else
3522 writel(res->start, base->virtbase + D40_DREG_LCPA);
3523
3524 base->lcpa_base = ioremap(res->start, resource_size(res));
3525 if (!base->lcpa_base) {
3526 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003527 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003528 goto failure;
3529 }
Narayanan G28c7a192011-11-22 13:56:55 +05303530 /* If lcla has to be located in ESRAM we don't need to allocate */
3531 if (base->plat_data->use_esram_lcla) {
3532 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3533 "lcla_esram");
3534 if (!res) {
3535 ret = -ENOENT;
3536 d40_err(&pdev->dev,
3537 "No \"lcla_esram\" memory resource\n");
3538 goto failure;
3539 }
3540 base->lcla_pool.base = ioremap(res->start,
3541 resource_size(res));
3542 if (!base->lcla_pool.base) {
3543 ret = -ENOMEM;
3544 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3545 goto failure;
3546 }
3547 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003548
Narayanan G28c7a192011-11-22 13:56:55 +05303549 } else {
3550 ret = d40_lcla_allocate(base);
3551 if (ret) {
3552 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3553 goto failure;
3554 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003555 }
3556
Linus Walleij8d318a52010-03-30 15:33:42 +02003557 spin_lock_init(&base->lcla_pool.lock);
3558
Linus Walleij8d318a52010-03-30 15:33:42 +02003559 base->irq = platform_get_irq(pdev, 0);
3560
3561 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003562 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003563 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003564 goto failure;
3565 }
3566
Narayanan G7fb3e752011-11-17 17:26:41 +05303567 pm_runtime_irq_safe(base->dev);
3568 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3569 pm_runtime_use_autosuspend(base->dev);
3570 pm_runtime_enable(base->dev);
3571 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303572
3573 if (base->plat_data->use_esram_lcla) {
3574
3575 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3576 if (IS_ERR(base->lcpa_regulator)) {
3577 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3578 base->lcpa_regulator = NULL;
3579 goto failure;
3580 }
3581
3582 ret = regulator_enable(base->lcpa_regulator);
3583 if (ret) {
3584 d40_err(&pdev->dev,
3585 "Failed to enable lcpa_regulator\n");
3586 regulator_put(base->lcpa_regulator);
3587 base->lcpa_regulator = NULL;
3588 goto failure;
3589 }
3590 }
3591
Narayanan G7fb3e752011-11-17 17:26:41 +05303592 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003593 err = d40_dmaengine_init(base, num_reserved_chans);
3594 if (err)
3595 goto failure;
3596
Per Forlinb96710e2011-10-18 18:39:47 +02003597 base->dev->dma_parms = &base->dma_parms;
3598 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3599 if (err) {
3600 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3601 goto failure;
3602 }
3603
Linus Walleij8d318a52010-03-30 15:33:42 +02003604 d40_hw_init(base);
3605
3606 dev_info(base->dev, "initialized\n");
3607 return 0;
3608
3609failure:
3610 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003611 if (base->desc_slab)
3612 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003613 if (base->virtbase)
3614 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003615
Narayanan G28c7a192011-11-22 13:56:55 +05303616 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3617 iounmap(base->lcla_pool.base);
3618 base->lcla_pool.base = NULL;
3619 }
3620
Rabin Vincent026cbc42011-01-25 11:18:14 +01003621 if (base->lcla_pool.dma_addr)
3622 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3623 SZ_1K * base->num_phy_chans,
3624 DMA_TO_DEVICE);
3625
Linus Walleij508849a2010-06-20 21:26:07 +00003626 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3627 free_pages((unsigned long)base->lcla_pool.base,
3628 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003629
3630 kfree(base->lcla_pool.base_unaligned);
3631
Linus Walleij8d318a52010-03-30 15:33:42 +02003632 if (base->phy_lcpa)
3633 release_mem_region(base->phy_lcpa,
3634 base->lcpa_size);
3635 if (base->phy_start)
3636 release_mem_region(base->phy_start,
3637 base->phy_size);
3638 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003639 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003640 clk_put(base->clk);
3641 }
3642
Narayanan G28c7a192011-11-22 13:56:55 +05303643 if (base->lcpa_regulator) {
3644 regulator_disable(base->lcpa_regulator);
3645 regulator_put(base->lcpa_regulator);
3646 }
3647
Linus Walleij8d318a52010-03-30 15:33:42 +02003648 kfree(base->lcla_pool.alloc_map);
3649 kfree(base->lookup_log_chans);
3650 kfree(base->lookup_phy_chans);
3651 kfree(base->phy_res);
3652 kfree(base);
3653 }
3654
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003655 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003656 return ret;
3657}
3658
3659static struct platform_driver d40_driver = {
3660 .driver = {
3661 .owner = THIS_MODULE,
3662 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303663 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003664 },
3665};
3666
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003667static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003668{
3669 return platform_driver_probe(&d40_driver, d40_probe);
3670}
Linus Walleija0eb2212011-05-18 14:18:57 +02003671subsys_initcall(stedma40_init);