blob: c47139ae8fa825573575345bfb8e787654b944ea [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. */
Linus Walleija2acaa22013-05-03 21:46:09 +020059#define DB8500_DMA_MEMCPY_EV_0 51
60#define DB8500_DMA_MEMCPY_EV_1 56
61#define DB8500_DMA_MEMCPY_EV_2 57
62#define DB8500_DMA_MEMCPY_EV_3 58
63#define DB8500_DMA_MEMCPY_EV_4 59
64#define DB8500_DMA_MEMCPY_EV_5 60
65
66static int dma40_memcpy_channels[] = {
67 DB8500_DMA_MEMCPY_EV_0,
68 DB8500_DMA_MEMCPY_EV_1,
69 DB8500_DMA_MEMCPY_EV_2,
70 DB8500_DMA_MEMCPY_EV_3,
71 DB8500_DMA_MEMCPY_EV_4,
72 DB8500_DMA_MEMCPY_EV_5,
73};
Lee Jones664a57e2013-05-03 15:31:53 +010074
Lee Jones29027a12013-05-03 15:31:54 +010075/* Default configuration for physcial memcpy */
76struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
77 .mode = STEDMA40_MODE_PHYSICAL,
78 .dir = STEDMA40_MEM_TO_MEM,
79
80 .src_info.data_width = STEDMA40_BYTE_WIDTH,
81 .src_info.psize = STEDMA40_PSIZE_PHY_1,
82 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
83
84 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
85 .dst_info.psize = STEDMA40_PSIZE_PHY_1,
86 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
87};
88
89/* Default configuration for logical memcpy */
90struct stedma40_chan_cfg dma40_memcpy_conf_log = {
91 .mode = STEDMA40_MODE_LOGICAL,
92 .dir = STEDMA40_MEM_TO_MEM,
93
94 .src_info.data_width = STEDMA40_BYTE_WIDTH,
95 .src_info.psize = STEDMA40_PSIZE_LOG_1,
96 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
97
98 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
99 .dst_info.psize = STEDMA40_PSIZE_LOG_1,
100 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
101};
102
Linus Walleij8d318a52010-03-30 15:33:42 +0200103/**
104 * enum 40_command - The different commands and/or statuses.
105 *
106 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
107 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
108 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
109 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
110 */
111enum d40_command {
112 D40_DMA_STOP = 0,
113 D40_DMA_RUN = 1,
114 D40_DMA_SUSPEND_REQ = 2,
115 D40_DMA_SUSPENDED = 3
116};
117
Narayanan G7fb3e752011-11-17 17:26:41 +0530118/*
Narayanan G1bdae6f2012-02-09 12:41:37 +0530119 * enum d40_events - The different Event Enables for the event lines.
120 *
121 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
122 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
123 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
124 * @D40_ROUND_EVENTLINE: Status check for event line.
125 */
126
127enum d40_events {
128 D40_DEACTIVATE_EVENTLINE = 0,
129 D40_ACTIVATE_EVENTLINE = 1,
130 D40_SUSPEND_REQ_EVENTLINE = 2,
131 D40_ROUND_EVENTLINE = 3
132};
133
134/*
Narayanan G7fb3e752011-11-17 17:26:41 +0530135 * These are the registers that has to be saved and later restored
136 * when the DMA hw is powered off.
137 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
138 */
139static u32 d40_backup_regs[] = {
140 D40_DREG_LCPA,
141 D40_DREG_LCLA,
142 D40_DREG_PRMSE,
143 D40_DREG_PRMSO,
144 D40_DREG_PRMOE,
145 D40_DREG_PRMOO,
146};
147
148#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
149
Tong Liu3cb645d2012-09-26 10:07:30 +0000150/*
151 * since 9540 and 8540 has the same HW revision
152 * use v4a for 9540 or ealier
153 * use v4b for 8540 or later
154 * HW revision:
155 * DB8500ed has revision 0
156 * DB8500v1 has revision 2
157 * DB8500v2 has revision 3
158 * AP9540v1 has revision 4
159 * DB8540v1 has revision 4
160 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
161 */
162static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530163 D40_DREG_PSEG1,
164 D40_DREG_PSEG2,
165 D40_DREG_PSEG3,
166 D40_DREG_PSEG4,
167 D40_DREG_PCEG1,
168 D40_DREG_PCEG2,
169 D40_DREG_PCEG3,
170 D40_DREG_PCEG4,
171 D40_DREG_RSEG1,
172 D40_DREG_RSEG2,
173 D40_DREG_RSEG3,
174 D40_DREG_RSEG4,
175 D40_DREG_RCEG1,
176 D40_DREG_RCEG2,
177 D40_DREG_RCEG3,
178 D40_DREG_RCEG4,
179};
180
Tong Liu3cb645d2012-09-26 10:07:30 +0000181#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
182
183static u32 d40_backup_regs_v4b[] = {
184 D40_DREG_CPSEG1,
185 D40_DREG_CPSEG2,
186 D40_DREG_CPSEG3,
187 D40_DREG_CPSEG4,
188 D40_DREG_CPSEG5,
189 D40_DREG_CPCEG1,
190 D40_DREG_CPCEG2,
191 D40_DREG_CPCEG3,
192 D40_DREG_CPCEG4,
193 D40_DREG_CPCEG5,
194 D40_DREG_CRSEG1,
195 D40_DREG_CRSEG2,
196 D40_DREG_CRSEG3,
197 D40_DREG_CRSEG4,
198 D40_DREG_CRSEG5,
199 D40_DREG_CRCEG1,
200 D40_DREG_CRCEG2,
201 D40_DREG_CRCEG3,
202 D40_DREG_CRCEG4,
203 D40_DREG_CRCEG5,
204};
205
206#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530207
208static u32 d40_backup_regs_chan[] = {
209 D40_CHAN_REG_SSCFG,
210 D40_CHAN_REG_SSELT,
211 D40_CHAN_REG_SSPTR,
212 D40_CHAN_REG_SSLNK,
213 D40_CHAN_REG_SDCFG,
214 D40_CHAN_REG_SDELT,
215 D40_CHAN_REG_SDPTR,
216 D40_CHAN_REG_SDLNK,
217};
218
Linus Walleij8d318a52010-03-30 15:33:42 +0200219/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000220 * struct d40_interrupt_lookup - lookup table for interrupt handler
221 *
222 * @src: Interrupt mask register.
223 * @clr: Interrupt clear register.
224 * @is_error: true if this is an error interrupt.
225 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
226 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
227 */
228struct d40_interrupt_lookup {
229 u32 src;
230 u32 clr;
231 bool is_error;
232 int offset;
233};
234
235
236static struct d40_interrupt_lookup il_v4a[] = {
237 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
238 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
239 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
240 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
241 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
242 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
243 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
244 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
245 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
246 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
247};
248
249static struct d40_interrupt_lookup il_v4b[] = {
250 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
251 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
252 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
253 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
254 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
255 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
256 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
257 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
258 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
259 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
260 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
261 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
262};
263
264/**
265 * struct d40_reg_val - simple lookup struct
266 *
267 * @reg: The register.
268 * @val: The value that belongs to the register in reg.
269 */
270struct d40_reg_val {
271 unsigned int reg;
272 unsigned int val;
273};
274
275static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
276 /* Clock every part of the DMA block from start */
277 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
278
279 /* Interrupts on all logical channels */
280 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
281 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
282 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
283 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
284 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
285 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
286 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
287 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
288 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
289 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
290 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
291 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
292};
293static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
294 /* Clock every part of the DMA block from start */
295 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
296
297 /* Interrupts on all logical channels */
298 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
299 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
300 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
301 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
302 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
303 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
304 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
305 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
306 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
307 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
308 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
309 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
310 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
311 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
312 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
313};
314
315/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200316 * struct d40_lli_pool - Structure for keeping LLIs in memory
317 *
318 * @base: Pointer to memory area when the pre_alloc_lli's are not large
319 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
320 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100321 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200322 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
323 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
324 * one buffer to one buffer.
325 */
326struct d40_lli_pool {
327 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000328 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100329 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200330 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000331 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200332};
333
334/**
335 * struct d40_desc - A descriptor is one DMA job.
336 *
337 * @lli_phy: LLI settings for physical channel. Both src and dst=
338 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
339 * lli_len equals one.
340 * @lli_log: Same as above but for logical channels.
341 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000342 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300343 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000344 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200345 * @txd: DMA engine struct. Used for among other things for communication
346 * during a transfer.
347 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200348 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530349 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200350 *
351 * This descriptor is used for both logical and physical transfers.
352 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200353struct d40_desc {
354 /* LLI physical */
355 struct d40_phy_lli_bidir lli_phy;
356 /* LLI logical */
357 struct d40_log_lli_bidir lli_log;
358
359 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000360 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000361 int lli_current;
362 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200363
364 struct dma_async_tx_descriptor txd;
365 struct list_head node;
366
Linus Walleij8d318a52010-03-30 15:33:42 +0200367 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100368 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200369};
370
371/**
372 * struct d40_lcla_pool - LCLA pool settings and data.
373 *
Linus Walleij508849a2010-06-20 21:26:07 +0000374 * @base: The virtual address of LCLA. 18 bit aligned.
375 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
376 * This pointer is only there for clean-up on error.
377 * @pages: The number of pages needed for all physical channels.
378 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200379 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000380 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200381 */
382struct d40_lcla_pool {
383 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100384 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000385 void *base_unaligned;
386 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200387 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000388 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200389};
390
391/**
392 * struct d40_phy_res - struct for handling eventlines mapped to physical
393 * channels.
394 *
395 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530396 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200397 * @num: The physical channel number of this entity.
398 * @allocated_src: Bit mapped to show which src event line's are mapped to
399 * this physical channel. Can also be free or physically allocated.
400 * @allocated_dst: Same as for src but is dst.
401 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000402 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100403 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200404 */
405struct d40_phy_res {
406 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530407 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200408 int num;
409 u32 allocated_src;
410 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100411 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200412};
413
414struct d40_base;
415
416/**
417 * struct d40_chan - Struct that describes a channel.
418 *
419 * @lock: A spinlock to protect this struct.
420 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200421 * @pending_tx: The number of pending transfers. Used between interrupt handler
422 * and tasklet.
423 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000424 * @phy_chan: Pointer to physical channel which this instance runs on. If this
425 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200426 * @chan: DMA engine handle.
427 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
428 * transfer and call client callback.
429 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200430 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200431 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100432 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200433 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200434 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200435 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000436 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200437 * @base: Pointer to the device instance struct.
438 * @src_def_cfg: Default cfg register setting for src.
439 * @dst_def_cfg: Default cfg register setting for dst.
440 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200441 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200442 * @runtime_addr: runtime configured address.
443 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200444 *
445 * This struct can either "be" a logical or a physical channel.
446 */
447struct d40_chan {
448 spinlock_t lock;
449 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200450 int pending_tx;
451 bool busy;
452 struct d40_phy_res *phy_chan;
453 struct dma_chan chan;
454 struct tasklet_struct tasklet;
455 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200456 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200457 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100458 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200459 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200460 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200461 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000462 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200463 struct d40_base *base;
464 /* Default register configurations */
465 u32 src_def_cfg;
466 u32 dst_def_cfg;
467 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200468 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200469 /* Runtime reconfiguration */
470 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530471 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200472};
473
474/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000475 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
476 * controller
477 *
478 * @backup: the pointer to the registers address array for backup
479 * @backup_size: the size of the registers address array for backup
480 * @realtime_en: the realtime enable register
481 * @realtime_clear: the realtime clear register
482 * @high_prio_en: the high priority enable register
483 * @high_prio_clear: the high priority clear register
484 * @interrupt_en: the interrupt enable register
485 * @interrupt_clear: the interrupt clear register
486 * @il: the pointer to struct d40_interrupt_lookup
487 * @il_size: the size of d40_interrupt_lookup array
488 * @init_reg: the pointer to the struct d40_reg_val
489 * @init_reg_size: the size of d40_reg_val array
490 */
491struct d40_gen_dmac {
492 u32 *backup;
493 u32 backup_size;
494 u32 realtime_en;
495 u32 realtime_clear;
496 u32 high_prio_en;
497 u32 high_prio_clear;
498 u32 interrupt_en;
499 u32 interrupt_clear;
500 struct d40_interrupt_lookup *il;
501 u32 il_size;
502 struct d40_reg_val *init_reg;
503 u32 init_reg_size;
504};
505
506/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200507 * struct d40_base - The big global struct, one for each probe'd instance.
508 *
509 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
510 * @execmd_lock: Lock for execute command usage since several channels share
511 * the same physical register.
512 * @dev: The device structure.
513 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700514 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200515 * @clk: Pointer to the DMA clock structure.
516 * @phy_start: Physical memory start of the DMA registers.
517 * @phy_size: Size of the DMA register map.
518 * @irq: The IRQ number.
519 * @num_phy_chans: The number of physical channels. Read from HW. This
520 * is the number of available channels for this driver, not counting "Secure
521 * mode" allocated physical channels.
522 * @num_log_chans: The number of logical channels. Calculated from
523 * num_phy_chans.
524 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
525 * @dma_slave: dma_device channels that can do only do slave transfers.
526 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530527 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200528 * @log_chans: Room for all possible logical channels in system.
529 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
530 * to log_chans entries.
531 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
532 * to phy_chans entries.
533 * @plat_data: Pointer to provided platform_data which is the driver
534 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530535 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200536 * @phy_res: Vector containing all physical channels.
537 * @lcla_pool: lcla pool settings and data.
538 * @lcpa_base: The virtual mapped address of LCPA.
539 * @phy_lcpa: The physical address of the LCPA.
540 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000541 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530542 * @reg_val_backup: Here the values of some hardware registers are stored
543 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000544 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
545 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530546 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
547 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
548 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000549 * @gen_dmac: the struct for generic registers values to represent u8500/8540
550 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200551 */
552struct d40_base {
553 spinlock_t interrupt_lock;
554 spinlock_t execmd_lock;
555 struct device *dev;
556 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700557 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200558 struct clk *clk;
559 phys_addr_t phy_start;
560 resource_size_t phy_size;
561 int irq;
562 int num_phy_chans;
563 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200564 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200565 struct dma_device dma_both;
566 struct dma_device dma_slave;
567 struct dma_device dma_memcpy;
568 struct d40_chan *phy_chans;
569 struct d40_chan *log_chans;
570 struct d40_chan **lookup_log_chans;
571 struct d40_chan **lookup_phy_chans;
572 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530573 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200574 /* Physical half channels */
575 struct d40_phy_res *phy_res;
576 struct d40_lcla_pool lcla_pool;
577 void *lcpa_base;
578 dma_addr_t phy_lcpa;
579 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000580 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530581 u32 reg_val_backup[BACKUP_REGS_SZ];
Tong Liu3cb645d2012-09-26 10:07:30 +0000582 u32 reg_val_backup_v4[MAX(BACKUP_REGS_SZ_V4A, BACKUP_REGS_SZ_V4B)];
Narayanan G7fb3e752011-11-17 17:26:41 +0530583 u32 *reg_val_backup_chan;
584 u16 gcc_pwr_off_mask;
585 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000586 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200587};
588
Rabin Vincent262d2912011-01-25 11:18:05 +0100589static struct device *chan2dev(struct d40_chan *d40c)
590{
591 return &d40c->chan.dev->device;
592}
593
Rabin Vincent724a8572011-01-25 11:18:08 +0100594static bool chan_is_physical(struct d40_chan *chan)
595{
596 return chan->log_num == D40_PHY_CHAN;
597}
598
599static bool chan_is_logical(struct d40_chan *chan)
600{
601 return !chan_is_physical(chan);
602}
603
Rabin Vincent8ca84682011-01-25 11:18:07 +0100604static void __iomem *chan_base(struct d40_chan *chan)
605{
606 return chan->base->virtbase + D40_DREG_PCBASE +
607 chan->phy_chan->num * D40_DREG_PCDELTA;
608}
609
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100610#define d40_err(dev, format, arg...) \
611 dev_err(dev, "[%s] " format, __func__, ## arg)
612
613#define chan_err(d40c, format, arg...) \
614 d40_err(chan2dev(d40c), format, ## arg)
615
Rabin Vincentb00f9382011-01-25 11:18:15 +0100616static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100617 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200618{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100619 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200620 u32 align;
621 void *base;
622
623 if (is_log)
624 align = sizeof(struct d40_log_lli);
625 else
626 align = sizeof(struct d40_phy_lli);
627
628 if (lli_len == 1) {
629 base = d40d->lli_pool.pre_alloc_lli;
630 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
631 d40d->lli_pool.base = NULL;
632 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100633 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200634
635 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
636 d40d->lli_pool.base = base;
637
638 if (d40d->lli_pool.base == NULL)
639 return -ENOMEM;
640 }
641
642 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100643 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100644 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100645
646 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200647 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100648 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100649 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100650
651 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
652 d40d->lli_phy.src,
653 d40d->lli_pool.size,
654 DMA_TO_DEVICE);
655
656 if (dma_mapping_error(d40c->base->dev,
657 d40d->lli_pool.dma_addr)) {
658 kfree(d40d->lli_pool.base);
659 d40d->lli_pool.base = NULL;
660 d40d->lli_pool.dma_addr = 0;
661 return -ENOMEM;
662 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200663 }
664
665 return 0;
666}
667
Rabin Vincentb00f9382011-01-25 11:18:15 +0100668static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200669{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100670 if (d40d->lli_pool.dma_addr)
671 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
672 d40d->lli_pool.size, DMA_TO_DEVICE);
673
Linus Walleij8d318a52010-03-30 15:33:42 +0200674 kfree(d40d->lli_pool.base);
675 d40d->lli_pool.base = NULL;
676 d40d->lli_pool.size = 0;
677 d40d->lli_log.src = NULL;
678 d40d->lli_log.dst = NULL;
679 d40d->lli_phy.src = NULL;
680 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200681}
682
Jonas Aaberg698e4732010-08-09 12:08:56 +0000683static int d40_lcla_alloc_one(struct d40_chan *d40c,
684 struct d40_desc *d40d)
685{
686 unsigned long flags;
687 int i;
688 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000689
690 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
691
Jonas Aaberg698e4732010-08-09 12:08:56 +0000692 /*
693 * Allocate both src and dst at the same time, therefore the half
694 * start on 1 since 0 can't be used since zero is used as end marker.
695 */
696 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100697 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
698
699 if (!d40c->base->lcla_pool.alloc_map[idx]) {
700 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000701 d40d->lcla_alloc++;
702 ret = i;
703 break;
704 }
705 }
706
707 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
708
709 return ret;
710}
711
712static int d40_lcla_free_all(struct d40_chan *d40c,
713 struct d40_desc *d40d)
714{
715 unsigned long flags;
716 int i;
717 int ret = -EINVAL;
718
Rabin Vincent724a8572011-01-25 11:18:08 +0100719 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000720 return 0;
721
722 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
723
724 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100725 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
726
727 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
728 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000729 d40d->lcla_alloc--;
730 if (d40d->lcla_alloc == 0) {
731 ret = 0;
732 break;
733 }
734 }
735 }
736
737 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
738
739 return ret;
740
741}
742
Linus Walleij8d318a52010-03-30 15:33:42 +0200743static void d40_desc_remove(struct d40_desc *d40d)
744{
745 list_del(&d40d->node);
746}
747
748static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
749{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000750 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200751
752 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000753 struct d40_desc *d;
754 struct d40_desc *_d;
755
Narayanan G7fb3e752011-11-17 17:26:41 +0530756 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200757 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200758 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000759 desc = d;
760 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000761 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200762 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530763 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200764 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000765
766 if (!desc)
767 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
768
769 if (desc)
770 INIT_LIST_HEAD(&desc->node);
771
772 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200773}
774
775static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
776{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000777
Rabin Vincentb00f9382011-01-25 11:18:15 +0100778 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000779 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000780 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200781}
782
783static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
784{
785 list_add_tail(&desc->node, &d40c->active);
786}
787
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100788static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
789{
790 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
791 struct d40_phy_lli *lli_src = desc->lli_phy.src;
792 void __iomem *base = chan_base(chan);
793
794 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
795 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
796 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
797 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
798
799 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
800 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
801 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
802 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
803}
804
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100805static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
806{
807 list_add_tail(&desc->node, &d40c->done);
808}
809
Rabin Vincente65889c2011-01-25 11:18:31 +0100810static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
811{
812 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
813 struct d40_log_lli_bidir *lli = &desc->lli_log;
814 int lli_current = desc->lli_current;
815 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100816 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100817 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100818 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530819 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100820 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100821
Rabin Vincent0c842b52011-01-25 11:18:35 +0100822 /*
823 * We may have partially running cyclic transfers, in case we did't get
824 * enough LCLA entries.
825 */
826 linkback = cyclic && lli_current == 0;
827
828 /*
829 * For linkback, we need one LCLA even with only one link, because we
830 * can't link back to the one in LCPA space
831 */
832 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100833 /*
834 * If the channel is expected to use only soft_lli don't
835 * allocate a lcla. This is to avoid a HW issue that exists
836 * in some controller during a peripheral to memory transfer
837 * that uses linked lists.
838 */
839 if (!(chan->phy_chan->use_soft_lli &&
840 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
841 curr_lcla = d40_lcla_alloc_one(chan, desc);
842
Rabin Vincent0c842b52011-01-25 11:18:35 +0100843 first_lcla = curr_lcla;
844 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100845
Rabin Vincent0c842b52011-01-25 11:18:35 +0100846 /*
847 * For linkback, we normally load the LCPA in the loop since we need to
848 * link it to the second LCLA and not the first. However, if we
849 * couldn't even get a first LCLA, then we have to run in LCPA and
850 * reload manually.
851 */
852 if (!linkback || curr_lcla == -EINVAL) {
853 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100854
Rabin Vincent0c842b52011-01-25 11:18:35 +0100855 if (curr_lcla == -EINVAL)
856 flags |= LLI_TERM_INT;
857
858 d40_log_lli_lcpa_write(chan->lcpa,
859 &lli->dst[lli_current],
860 &lli->src[lli_current],
861 curr_lcla,
862 flags);
863 lli_current++;
864 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100865
866 if (curr_lcla < 0)
867 goto out;
868
Rabin Vincente65889c2011-01-25 11:18:31 +0100869 for (; lli_current < lli_len; lli_current++) {
870 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
871 8 * curr_lcla * 2;
872 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100873 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100874 int next_lcla;
875
876 if (lli_current + 1 < lli_len)
877 next_lcla = d40_lcla_alloc_one(chan, desc);
878 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100879 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100880
Rabin Vincent0c842b52011-01-25 11:18:35 +0100881 if (cyclic || next_lcla == -EINVAL)
882 flags |= LLI_TERM_INT;
883
884 if (linkback && curr_lcla == first_lcla) {
885 /* First link goes in both LCPA and LCLA */
886 d40_log_lli_lcpa_write(chan->lcpa,
887 &lli->dst[lli_current],
888 &lli->src[lli_current],
889 next_lcla, flags);
890 }
891
892 /*
893 * One unused LCLA in the cyclic case if the very first
894 * next_lcla fails...
895 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100896 d40_log_lli_lcla_write(lcla,
897 &lli->dst[lli_current],
898 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100899 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100900
Narayanan G28c7a192011-11-22 13:56:55 +0530901 /*
902 * Cache maintenance is not needed if lcla is
903 * mapped in esram
904 */
905 if (!use_esram_lcla) {
906 dma_sync_single_range_for_device(chan->base->dev,
907 pool->dma_addr, lcla_offset,
908 2 * sizeof(struct d40_log_lli),
909 DMA_TO_DEVICE);
910 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100911 curr_lcla = next_lcla;
912
Rabin Vincent0c842b52011-01-25 11:18:35 +0100913 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100914 lli_current++;
915 break;
916 }
917 }
918
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100919out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100920 desc->lli_current = lli_current;
921}
922
Jonas Aaberg698e4732010-08-09 12:08:56 +0000923static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
924{
Rabin Vincent724a8572011-01-25 11:18:08 +0100925 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100926 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000927 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100928 } else
929 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000930}
931
Linus Walleij8d318a52010-03-30 15:33:42 +0200932static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
933{
934 struct d40_desc *d;
935
936 if (list_empty(&d40c->active))
937 return NULL;
938
939 d = list_first_entry(&d40c->active,
940 struct d40_desc,
941 node);
942 return d;
943}
944
Per Forlin74043682011-08-29 13:33:34 +0200945/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200946static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
947{
Per Forlin74043682011-08-29 13:33:34 +0200948 d40_desc_remove(desc);
949 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200950 list_add_tail(&desc->node, &d40c->pending_queue);
951}
952
953static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
954{
955 struct d40_desc *d;
956
957 if (list_empty(&d40c->pending_queue))
958 return NULL;
959
960 d = list_first_entry(&d40c->pending_queue,
961 struct d40_desc,
962 node);
963 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200964}
965
966static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
967{
968 struct d40_desc *d;
969
970 if (list_empty(&d40c->queue))
971 return NULL;
972
973 d = list_first_entry(&d40c->queue,
974 struct d40_desc,
975 node);
976 return d;
977}
978
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100979static struct d40_desc *d40_first_done(struct d40_chan *d40c)
980{
981 if (list_empty(&d40c->done))
982 return NULL;
983
984 return list_first_entry(&d40c->done, struct d40_desc, node);
985}
986
Per Forlind49278e2010-12-20 18:31:38 +0100987static int d40_psize_2_burst_size(bool is_log, int psize)
988{
989 if (is_log) {
990 if (psize == STEDMA40_PSIZE_LOG_1)
991 return 1;
992 } else {
993 if (psize == STEDMA40_PSIZE_PHY_1)
994 return 1;
995 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200996
Per Forlind49278e2010-12-20 18:31:38 +0100997 return 2 << psize;
998}
999
1000/*
1001 * The dma only supports transmitting packages up to
1002 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
1003 * dma elements required to send the entire sg list
1004 */
1005static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1006{
1007 int dmalen;
1008 u32 max_w = max(data_width1, data_width2);
1009 u32 min_w = min(data_width1, data_width2);
1010 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
1011
1012 if (seg_max > STEDMA40_MAX_SEG_SIZE)
1013 seg_max -= (1 << max_w);
1014
1015 if (!IS_ALIGNED(size, 1 << max_w))
1016 return -EINVAL;
1017
1018 if (size <= seg_max)
1019 dmalen = 1;
1020 else {
1021 dmalen = size / seg_max;
1022 if (dmalen * seg_max < size)
1023 dmalen++;
1024 }
1025 return dmalen;
1026}
1027
1028static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1029 u32 data_width1, u32 data_width2)
1030{
1031 struct scatterlist *sg;
1032 int i;
1033 int len = 0;
1034 int ret;
1035
1036 for_each_sg(sgl, sg, sg_len, i) {
1037 ret = d40_size_2_dmalen(sg_dma_len(sg),
1038 data_width1, data_width2);
1039 if (ret < 0)
1040 return ret;
1041 len += ret;
1042 }
1043 return len;
1044}
1045
Narayanan G7fb3e752011-11-17 17:26:41 +05301046
1047#ifdef CONFIG_PM
1048static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1049 u32 *regaddr, int num, bool save)
1050{
1051 int i;
1052
1053 for (i = 0; i < num; i++) {
1054 void __iomem *addr = baseaddr + regaddr[i];
1055
1056 if (save)
1057 backup[i] = readl_relaxed(addr);
1058 else
1059 writel_relaxed(backup[i], addr);
1060 }
1061}
1062
1063static void d40_save_restore_registers(struct d40_base *base, bool save)
1064{
1065 int i;
1066
1067 /* Save/Restore channel specific registers */
1068 for (i = 0; i < base->num_phy_chans; i++) {
1069 void __iomem *addr;
1070 int idx;
1071
1072 if (base->phy_res[i].reserved)
1073 continue;
1074
1075 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1076 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1077
1078 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1079 d40_backup_regs_chan,
1080 ARRAY_SIZE(d40_backup_regs_chan),
1081 save);
1082 }
1083
1084 /* Save/Restore global registers */
1085 dma40_backup(base->virtbase, base->reg_val_backup,
1086 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1087 save);
1088
1089 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001090 if (base->gen_dmac.backup)
1091 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1092 base->gen_dmac.backup,
1093 base->gen_dmac.backup_size,
1094 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301095}
1096#else
1097static void d40_save_restore_registers(struct d40_base *base, bool save)
1098{
1099}
1100#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001101
Narayanan G1bdae6f2012-02-09 12:41:37 +05301102static int __d40_execute_command_phy(struct d40_chan *d40c,
1103 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001104{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001105 u32 status;
1106 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001107 void __iomem *active_reg;
1108 int ret = 0;
1109 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001110 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001111
Narayanan G1bdae6f2012-02-09 12:41:37 +05301112 if (command == D40_DMA_STOP) {
1113 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1114 if (ret)
1115 return ret;
1116 }
1117
Linus Walleij8d318a52010-03-30 15:33:42 +02001118 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1119
1120 if (d40c->phy_chan->num % 2 == 0)
1121 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1122 else
1123 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1124
1125 if (command == D40_DMA_SUSPEND_REQ) {
1126 status = (readl(active_reg) &
1127 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1128 D40_CHAN_POS(d40c->phy_chan->num);
1129
1130 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1131 goto done;
1132 }
1133
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001134 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1135 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1136 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001137
1138 if (command == D40_DMA_SUSPEND_REQ) {
1139
1140 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1141 status = (readl(active_reg) &
1142 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1143 D40_CHAN_POS(d40c->phy_chan->num);
1144
1145 cpu_relax();
1146 /*
1147 * Reduce the number of bus accesses while
1148 * waiting for the DMA to suspend.
1149 */
1150 udelay(3);
1151
1152 if (status == D40_DMA_STOP ||
1153 status == D40_DMA_SUSPENDED)
1154 break;
1155 }
1156
1157 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001158 chan_err(d40c,
1159 "unable to suspend the chl %d (log: %d) status %x\n",
1160 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001161 status);
1162 dump_stack();
1163 ret = -EBUSY;
1164 }
1165
1166 }
1167done:
1168 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1169 return ret;
1170}
1171
1172static void d40_term_all(struct d40_chan *d40c)
1173{
1174 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001175 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001176
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001177 /* Release completed descriptors */
1178 while ((d40d = d40_first_done(d40c))) {
1179 d40_desc_remove(d40d);
1180 d40_desc_free(d40c, d40d);
1181 }
1182
Linus Walleij8d318a52010-03-30 15:33:42 +02001183 /* Release active descriptors */
1184 while ((d40d = d40_first_active_get(d40c))) {
1185 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001186 d40_desc_free(d40c, d40d);
1187 }
1188
1189 /* Release queued descriptors waiting for transfer */
1190 while ((d40d = d40_first_queued(d40c))) {
1191 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001192 d40_desc_free(d40c, d40d);
1193 }
1194
Per Forlina8f30672011-06-26 23:29:52 +02001195 /* Release pending descriptors */
1196 while ((d40d = d40_first_pending(d40c))) {
1197 d40_desc_remove(d40d);
1198 d40_desc_free(d40c, d40d);
1199 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001200
Per Forlin74043682011-08-29 13:33:34 +02001201 /* Release client owned descriptors */
1202 if (!list_empty(&d40c->client))
1203 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1204 d40_desc_remove(d40d);
1205 d40_desc_free(d40c, d40d);
1206 }
1207
Per Forlin82babbb362011-08-29 13:33:35 +02001208 /* Release descriptors in prepare queue */
1209 if (!list_empty(&d40c->prepare_queue))
1210 list_for_each_entry_safe(d40d, _d,
1211 &d40c->prepare_queue, node) {
1212 d40_desc_remove(d40d);
1213 d40_desc_free(d40c, d40d);
1214 }
Per Forlin74043682011-08-29 13:33:34 +02001215
Linus Walleij8d318a52010-03-30 15:33:42 +02001216 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001217}
1218
Narayanan G1bdae6f2012-02-09 12:41:37 +05301219static void __d40_config_set_event(struct d40_chan *d40c,
1220 enum d40_events event_type, u32 event,
1221 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001222{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001223 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001224 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301225 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001226
Narayanan G1bdae6f2012-02-09 12:41:37 +05301227 switch (event_type) {
1228
1229 case D40_DEACTIVATE_EVENTLINE:
1230
Rabin Vincent262d2912011-01-25 11:18:05 +01001231 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1232 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301233 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001234
Narayanan G1bdae6f2012-02-09 12:41:37 +05301235 case D40_SUSPEND_REQ_EVENTLINE:
1236 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1237 D40_EVENTLINE_POS(event);
1238
1239 if (status == D40_DEACTIVATE_EVENTLINE ||
1240 status == D40_SUSPEND_REQ_EVENTLINE)
1241 break;
1242
1243 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1244 | ~D40_EVENTLINE_MASK(event), addr);
1245
1246 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1247
1248 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1249 D40_EVENTLINE_POS(event);
1250
1251 cpu_relax();
1252 /*
1253 * Reduce the number of bus accesses while
1254 * waiting for the DMA to suspend.
1255 */
1256 udelay(3);
1257
1258 if (status == D40_DEACTIVATE_EVENTLINE)
1259 break;
1260 }
1261
1262 if (tries == D40_SUSPEND_MAX_IT) {
1263 chan_err(d40c,
1264 "unable to stop the event_line chl %d (log: %d)"
1265 "status %x\n", d40c->phy_chan->num,
1266 d40c->log_num, status);
1267 }
1268 break;
1269
1270 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001271 /*
1272 * The hardware sometimes doesn't register the enable when src and dst
1273 * event lines are active on the same logical channel. Retry to ensure
1274 * it does. Usually only one retry is sufficient.
1275 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301276 tries = 100;
1277 while (--tries) {
1278 writel((D40_ACTIVATE_EVENTLINE <<
1279 D40_EVENTLINE_POS(event)) |
1280 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001281
Narayanan G1bdae6f2012-02-09 12:41:37 +05301282 if (readl(addr) & D40_EVENTLINE_MASK(event))
1283 break;
1284 }
1285
1286 if (tries != 99)
1287 dev_dbg(chan2dev(d40c),
1288 "[%s] workaround enable S%cLNK (%d tries)\n",
1289 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1290 100 - tries);
1291
1292 WARN_ON(!tries);
1293 break;
1294
1295 case D40_ROUND_EVENTLINE:
1296 BUG();
1297 break;
1298
Rabin Vincent262d2912011-01-25 11:18:05 +01001299 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001300}
1301
Narayanan G1bdae6f2012-02-09 12:41:37 +05301302static void d40_config_set_event(struct d40_chan *d40c,
1303 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001304{
Linus Walleij8d318a52010-03-30 15:33:42 +02001305 /* Enable event line connected to device (or memcpy) */
1306 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1307 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1308 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1309
Narayanan G1bdae6f2012-02-09 12:41:37 +05301310 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001311 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001312 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001313
Linus Walleij8d318a52010-03-30 15:33:42 +02001314 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1315 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1316
Narayanan G1bdae6f2012-02-09 12:41:37 +05301317 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001318 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001319 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001320}
1321
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001322static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001323{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001324 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001325 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001326
Rabin Vincent8ca84682011-01-25 11:18:07 +01001327 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1328 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001329
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001330 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001331}
1332
Narayanan G1bdae6f2012-02-09 12:41:37 +05301333static int
1334__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1335{
1336 unsigned long flags;
1337 int ret = 0;
1338 u32 active_status;
1339 void __iomem *active_reg;
1340
1341 if (d40c->phy_chan->num % 2 == 0)
1342 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1343 else
1344 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1345
1346
1347 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1348
1349 switch (command) {
1350 case D40_DMA_STOP:
1351 case D40_DMA_SUSPEND_REQ:
1352
1353 active_status = (readl(active_reg) &
1354 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1355 D40_CHAN_POS(d40c->phy_chan->num);
1356
1357 if (active_status == D40_DMA_RUN)
1358 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1359 else
1360 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1361
1362 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1363 ret = __d40_execute_command_phy(d40c, command);
1364
1365 break;
1366
1367 case D40_DMA_RUN:
1368
1369 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1370 ret = __d40_execute_command_phy(d40c, command);
1371 break;
1372
1373 case D40_DMA_SUSPENDED:
1374 BUG();
1375 break;
1376 }
1377
1378 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1379 return ret;
1380}
1381
1382static int d40_channel_execute_command(struct d40_chan *d40c,
1383 enum d40_command command)
1384{
1385 if (chan_is_logical(d40c))
1386 return __d40_execute_command_log(d40c, command);
1387 else
1388 return __d40_execute_command_phy(d40c, command);
1389}
1390
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001391static u32 d40_get_prmo(struct d40_chan *d40c)
1392{
1393 static const unsigned int phy_map[] = {
1394 [STEDMA40_PCHAN_BASIC_MODE]
1395 = D40_DREG_PRMO_PCHAN_BASIC,
1396 [STEDMA40_PCHAN_MODULO_MODE]
1397 = D40_DREG_PRMO_PCHAN_MODULO,
1398 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1399 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1400 };
1401 static const unsigned int log_map[] = {
1402 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1403 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1404 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1405 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1406 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1407 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1408 };
1409
Rabin Vincent724a8572011-01-25 11:18:08 +01001410 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001411 return phy_map[d40c->dma_cfg.mode_opt];
1412 else
1413 return log_map[d40c->dma_cfg.mode_opt];
1414}
1415
Jonas Aabergb55912c2010-08-09 12:08:02 +00001416static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001417{
1418 u32 addr_base;
1419 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001420
1421 /* Odd addresses are even addresses + 4 */
1422 addr_base = (d40c->phy_chan->num % 2) * 4;
1423 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001424 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001425 D40_CHAN_POS(d40c->phy_chan->num);
1426 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1427
1428 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001429 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001430
1431 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1432
Rabin Vincent724a8572011-01-25 11:18:08 +01001433 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001434 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1435 & D40_SREG_ELEM_LOG_LIDX_MASK;
1436 void __iomem *chanbase = chan_base(d40c);
1437
Linus Walleij8d318a52010-03-30 15:33:42 +02001438 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001439 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1440 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001441
Jonas Aabergb55912c2010-08-09 12:08:02 +00001442 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001443 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1444 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301445
1446 /* Clear LNK which will be used by d40_chan_has_events() */
1447 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1448 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001449 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001450}
1451
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001452static u32 d40_residue(struct d40_chan *d40c)
1453{
1454 u32 num_elt;
1455
Rabin Vincent724a8572011-01-25 11:18:08 +01001456 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001457 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1458 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001459 else {
1460 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1461 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1462 >> D40_SREG_ELEM_PHY_ECNT_POS;
1463 }
1464
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001465 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1466}
1467
1468static bool d40_tx_is_linked(struct d40_chan *d40c)
1469{
1470 bool is_link;
1471
Rabin Vincent724a8572011-01-25 11:18:08 +01001472 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001473 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1474 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001475 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1476 & D40_SREG_LNK_PHYS_LNK_MASK;
1477
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001478 return is_link;
1479}
1480
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001481static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001482{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001483 int res = 0;
1484 unsigned long flags;
1485
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001486 if (!d40c->busy)
1487 return 0;
1488
Narayanan G7fb3e752011-11-17 17:26:41 +05301489 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001490 spin_lock_irqsave(&d40c->lock, flags);
1491
1492 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301493
Narayanan G7fb3e752011-11-17 17:26:41 +05301494 pm_runtime_mark_last_busy(d40c->base->dev);
1495 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001496 spin_unlock_irqrestore(&d40c->lock, flags);
1497 return res;
1498}
1499
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001500static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001501{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001502 int res = 0;
1503 unsigned long flags;
1504
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001505 if (!d40c->busy)
1506 return 0;
1507
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001508 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301509 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001510
1511 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301512 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001513 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001514
Narayanan G7fb3e752011-11-17 17:26:41 +05301515 pm_runtime_mark_last_busy(d40c->base->dev);
1516 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001517 spin_unlock_irqrestore(&d40c->lock, flags);
1518 return res;
1519}
1520
Linus Walleij8d318a52010-03-30 15:33:42 +02001521static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1522{
1523 struct d40_chan *d40c = container_of(tx->chan,
1524 struct d40_chan,
1525 chan);
1526 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1527 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001528 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001529
1530 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001531 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001532 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001533 spin_unlock_irqrestore(&d40c->lock, flags);
1534
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001535 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001536}
1537
1538static int d40_start(struct d40_chan *d40c)
1539{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001540 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001541}
1542
1543static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1544{
1545 struct d40_desc *d40d;
1546 int err;
1547
1548 /* Start queued jobs, if any */
1549 d40d = d40_first_queued(d40c);
1550
1551 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301552 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301553 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301554 pm_runtime_get_sync(d40c->base->dev);
1555 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001556
1557 /* Remove from queue */
1558 d40_desc_remove(d40d);
1559
1560 /* Add to active queue */
1561 d40_desc_submit(d40c, d40d);
1562
Rabin Vincent7d83a852011-01-25 11:18:06 +01001563 /* Initiate DMA job */
1564 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001565
Rabin Vincent7d83a852011-01-25 11:18:06 +01001566 /* Start dma job */
1567 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001568
Rabin Vincent7d83a852011-01-25 11:18:06 +01001569 if (err)
1570 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001571 }
1572
1573 return d40d;
1574}
1575
1576/* called from interrupt context */
1577static void dma_tc_handle(struct d40_chan *d40c)
1578{
1579 struct d40_desc *d40d;
1580
Linus Walleij8d318a52010-03-30 15:33:42 +02001581 /* Get first active entry from list */
1582 d40d = d40_first_active_get(d40c);
1583
1584 if (d40d == NULL)
1585 return;
1586
Rabin Vincent0c842b52011-01-25 11:18:35 +01001587 if (d40d->cyclic) {
1588 /*
1589 * If this was a paritially loaded list, we need to reloaded
1590 * it, and only when the list is completed. We need to check
1591 * for done because the interrupt will hit for every link, and
1592 * not just the last one.
1593 */
1594 if (d40d->lli_current < d40d->lli_len
1595 && !d40_tx_is_linked(d40c)
1596 && !d40_residue(d40c)) {
1597 d40_lcla_free_all(d40c, d40d);
1598 d40_desc_load(d40c, d40d);
1599 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001600
Rabin Vincent0c842b52011-01-25 11:18:35 +01001601 if (d40d->lli_current == d40d->lli_len)
1602 d40d->lli_current = 0;
1603 }
1604 } else {
1605 d40_lcla_free_all(d40c, d40d);
1606
1607 if (d40d->lli_current < d40d->lli_len) {
1608 d40_desc_load(d40c, d40d);
1609 /* Start dma job */
1610 (void) d40_start(d40c);
1611 return;
1612 }
1613
1614 if (d40_queue_start(d40c) == NULL)
1615 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301616 pm_runtime_mark_last_busy(d40c->base->dev);
1617 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001618
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001619 d40_desc_remove(d40d);
1620 d40_desc_done(d40c, d40d);
1621 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001622
Linus Walleij8d318a52010-03-30 15:33:42 +02001623 d40c->pending_tx++;
1624 tasklet_schedule(&d40c->tasklet);
1625
1626}
1627
1628static void dma_tasklet(unsigned long data)
1629{
1630 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001631 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001632 unsigned long flags;
1633 dma_async_tx_callback callback;
1634 void *callback_param;
1635
1636 spin_lock_irqsave(&d40c->lock, flags);
1637
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001638 /* Get first entry from the done list */
1639 d40d = d40_first_done(d40c);
1640 if (d40d == NULL) {
1641 /* Check if we have reached here for cyclic job */
1642 d40d = d40_first_active_get(d40c);
1643 if (d40d == NULL || !d40d->cyclic)
1644 goto err;
1645 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001646
Rabin Vincent0c842b52011-01-25 11:18:35 +01001647 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001648 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001649
1650 /*
1651 * If terminating a channel pending_tx is set to zero.
1652 * This prevents any finished active jobs to return to the client.
1653 */
1654 if (d40c->pending_tx == 0) {
1655 spin_unlock_irqrestore(&d40c->lock, flags);
1656 return;
1657 }
1658
1659 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001660 callback = d40d->txd.callback;
1661 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001662
Rabin Vincent0c842b52011-01-25 11:18:35 +01001663 if (!d40d->cyclic) {
1664 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001665 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001666 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001667 } else if (!d40d->is_in_client_list) {
1668 d40_desc_remove(d40d);
1669 d40_lcla_free_all(d40c, d40d);
1670 list_add_tail(&d40d->node, &d40c->client);
1671 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001672 }
1673 }
1674
1675 d40c->pending_tx--;
1676
1677 if (d40c->pending_tx)
1678 tasklet_schedule(&d40c->tasklet);
1679
1680 spin_unlock_irqrestore(&d40c->lock, flags);
1681
Jonas Aaberg767a9672010-08-09 12:08:34 +00001682 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001683 callback(callback_param);
1684
1685 return;
1686
Narayanan G1bdae6f2012-02-09 12:41:37 +05301687err:
1688 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001689 if (d40c->pending_tx > 0)
1690 d40c->pending_tx--;
1691 spin_unlock_irqrestore(&d40c->lock, flags);
1692}
1693
1694static irqreturn_t d40_handle_interrupt(int irq, void *data)
1695{
Linus Walleij8d318a52010-03-30 15:33:42 +02001696 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001697 u32 idx;
1698 u32 row;
1699 long chan = -1;
1700 struct d40_chan *d40c;
1701 unsigned long flags;
1702 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001703 u32 regs[base->gen_dmac.il_size];
1704 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1705 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001706
1707 spin_lock_irqsave(&base->interrupt_lock, flags);
1708
1709 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001710 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001711 regs[i] = readl(base->virtbase + il[i].src);
1712
1713 for (;;) {
1714
1715 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001716 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001717
1718 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001719 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001720 break;
1721
1722 row = chan / BITS_PER_LONG;
1723 idx = chan & (BITS_PER_LONG - 1);
1724
Linus Walleij8d318a52010-03-30 15:33:42 +02001725 if (il[row].offset == D40_PHY_CHAN)
1726 d40c = base->lookup_phy_chans[idx];
1727 else
1728 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001729
1730 if (!d40c) {
1731 /*
1732 * No error because this can happen if something else
1733 * in the system is using the channel.
1734 */
1735 continue;
1736 }
1737
1738 /* ACK interrupt */
1739 writel(1 << idx, base->virtbase + il[row].clr);
1740
Linus Walleij8d318a52010-03-30 15:33:42 +02001741 spin_lock(&d40c->lock);
1742
1743 if (!il[row].is_error)
1744 dma_tc_handle(d40c);
1745 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001746 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1747 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001748
1749 spin_unlock(&d40c->lock);
1750 }
1751
1752 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1753
1754 return IRQ_HANDLED;
1755}
1756
Linus Walleij8d318a52010-03-30 15:33:42 +02001757static int d40_validate_conf(struct d40_chan *d40c,
1758 struct stedma40_chan_cfg *conf)
1759{
1760 int res = 0;
1761 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1762 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001763 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001764
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001765 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001766 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001767 res = -EINVAL;
1768 }
1769
1770 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1771 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1772 d40c->runtime_addr == 0) {
1773
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001774 chan_err(d40c, "Invalid TX channel address (%d)\n",
1775 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001776 res = -EINVAL;
1777 }
1778
1779 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1780 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1781 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001782 chan_err(d40c, "Invalid RX channel address (%d)\n",
1783 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001784 res = -EINVAL;
1785 }
1786
1787 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001788 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001789 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001790 res = -EINVAL;
1791 }
1792
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001793 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001794 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001795 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001796 res = -EINVAL;
1797 }
1798
1799 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1800 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001801 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001802 res = -EINVAL;
1803 }
1804
1805 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1806 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001807 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001808 res = -EINVAL;
1809 }
1810
1811 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1812 /*
1813 * DMAC HW supports it. Will be added to this driver,
1814 * in case any dma client requires it.
1815 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001816 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001817 res = -EINVAL;
1818 }
1819
Per Forlind49278e2010-12-20 18:31:38 +01001820 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1821 (1 << conf->src_info.data_width) !=
1822 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1823 (1 << conf->dst_info.data_width)) {
1824 /*
1825 * The DMAC hardware only supports
1826 * src (burst x width) == dst (burst x width)
1827 */
1828
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001829 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001830 res = -EINVAL;
1831 }
1832
Linus Walleij8d318a52010-03-30 15:33:42 +02001833 return res;
1834}
1835
Narayanan G5cd326f2011-11-30 19:20:42 +05301836static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1837 bool is_src, int log_event_line, bool is_log,
1838 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001839{
1840 unsigned long flags;
1841 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301842
1843 *first_user = ((phy->allocated_src | phy->allocated_dst)
1844 == D40_ALLOC_FREE);
1845
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001846 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001847 /* Physical interrupts are masked per physical full channel */
1848 if (phy->allocated_src == D40_ALLOC_FREE &&
1849 phy->allocated_dst == D40_ALLOC_FREE) {
1850 phy->allocated_dst = D40_ALLOC_PHY;
1851 phy->allocated_src = D40_ALLOC_PHY;
1852 goto found;
1853 } else
1854 goto not_found;
1855 }
1856
1857 /* Logical channel */
1858 if (is_src) {
1859 if (phy->allocated_src == D40_ALLOC_PHY)
1860 goto not_found;
1861
1862 if (phy->allocated_src == D40_ALLOC_FREE)
1863 phy->allocated_src = D40_ALLOC_LOG_FREE;
1864
1865 if (!(phy->allocated_src & (1 << log_event_line))) {
1866 phy->allocated_src |= 1 << log_event_line;
1867 goto found;
1868 } else
1869 goto not_found;
1870 } else {
1871 if (phy->allocated_dst == D40_ALLOC_PHY)
1872 goto not_found;
1873
1874 if (phy->allocated_dst == D40_ALLOC_FREE)
1875 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1876
1877 if (!(phy->allocated_dst & (1 << log_event_line))) {
1878 phy->allocated_dst |= 1 << log_event_line;
1879 goto found;
1880 } else
1881 goto not_found;
1882 }
1883
1884not_found:
1885 spin_unlock_irqrestore(&phy->lock, flags);
1886 return false;
1887found:
1888 spin_unlock_irqrestore(&phy->lock, flags);
1889 return true;
1890}
1891
1892static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1893 int log_event_line)
1894{
1895 unsigned long flags;
1896 bool is_free = false;
1897
1898 spin_lock_irqsave(&phy->lock, flags);
1899 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001900 phy->allocated_dst = D40_ALLOC_FREE;
1901 phy->allocated_src = D40_ALLOC_FREE;
1902 is_free = true;
1903 goto out;
1904 }
1905
1906 /* Logical channel */
1907 if (is_src) {
1908 phy->allocated_src &= ~(1 << log_event_line);
1909 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1910 phy->allocated_src = D40_ALLOC_FREE;
1911 } else {
1912 phy->allocated_dst &= ~(1 << log_event_line);
1913 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1914 phy->allocated_dst = D40_ALLOC_FREE;
1915 }
1916
1917 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1918 D40_ALLOC_FREE);
1919
1920out:
1921 spin_unlock_irqrestore(&phy->lock, flags);
1922
1923 return is_free;
1924}
1925
Narayanan G5cd326f2011-11-30 19:20:42 +05301926static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001927{
1928 int dev_type;
1929 int event_group;
1930 int event_line;
1931 struct d40_phy_res *phys;
1932 int i;
1933 int j;
1934 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001935 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001936 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001937 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001938
1939 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001940 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001941
1942 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1943 dev_type = d40c->dma_cfg.src_dev_type;
1944 log_num = 2 * dev_type;
1945 is_src = true;
1946 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1947 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1948 /* dst event lines are used for logical memcpy */
1949 dev_type = d40c->dma_cfg.dst_dev_type;
1950 log_num = 2 * dev_type + 1;
1951 is_src = false;
1952 } else
1953 return -EINVAL;
1954
1955 event_group = D40_TYPE_TO_GROUP(dev_type);
1956 event_line = D40_TYPE_TO_EVENT(dev_type);
1957
1958 if (!is_log) {
1959 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1960 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001961 if (d40c->dma_cfg.use_fixed_channel) {
1962 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001963 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301964 0, is_log,
1965 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001966 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001967 } else {
1968 for (i = 0; i < num_phy_chans; i++) {
1969 if (d40_alloc_mask_set(&phys[i], is_src,
1970 0, is_log,
1971 first_phy_user))
1972 goto found_phy;
1973 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001974 }
1975 } else
1976 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1977 int phy_num = j + event_group * 2;
1978 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001979 if (d40_alloc_mask_set(&phys[i],
1980 is_src,
1981 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301982 is_log,
1983 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001984 goto found_phy;
1985 }
1986 }
1987 return -EINVAL;
1988found_phy:
1989 d40c->phy_chan = &phys[i];
1990 d40c->log_num = D40_PHY_CHAN;
1991 goto out;
1992 }
1993 if (dev_type == -1)
1994 return -EINVAL;
1995
1996 /* Find logical channel */
1997 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1998 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301999
2000 if (d40c->dma_cfg.use_fixed_channel) {
2001 i = d40c->dma_cfg.phy_channel;
2002
2003 if ((i != phy_num) && (i != phy_num + 1)) {
2004 dev_err(chan2dev(d40c),
2005 "invalid fixed phy channel %d\n", i);
2006 return -EINVAL;
2007 }
2008
2009 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
2010 is_log, first_phy_user))
2011 goto found_log;
2012
2013 dev_err(chan2dev(d40c),
2014 "could not allocate fixed phy channel %d\n", i);
2015 return -EINVAL;
2016 }
2017
Linus Walleij8d318a52010-03-30 15:33:42 +02002018 /*
2019 * Spread logical channels across all available physical rather
2020 * than pack every logical channel at the first available phy
2021 * channels.
2022 */
2023 if (is_src) {
2024 for (i = phy_num; i < phy_num + 2; i++) {
2025 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302026 event_line, is_log,
2027 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002028 goto found_log;
2029 }
2030 } else {
2031 for (i = phy_num + 1; i >= phy_num; i--) {
2032 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302033 event_line, is_log,
2034 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002035 goto found_log;
2036 }
2037 }
2038 }
2039 return -EINVAL;
2040
2041found_log:
2042 d40c->phy_chan = &phys[i];
2043 d40c->log_num = log_num;
2044out:
2045
2046 if (is_log)
2047 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2048 else
2049 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2050
2051 return 0;
2052
2053}
2054
Linus Walleij8d318a52010-03-30 15:33:42 +02002055static int d40_config_memcpy(struct d40_chan *d40c)
2056{
2057 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2058
2059 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002060 d40c->dma_cfg = dma40_memcpy_conf_log;
Linus Walleij8d318a52010-03-30 15:33:42 +02002061 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
Lee Jones664a57e2013-05-03 15:31:53 +01002062 d40c->dma_cfg.dst_dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002063
2064 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2065 dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002066 d40c->dma_cfg = dma40_memcpy_conf_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002067 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002068 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002069 return -EINVAL;
2070 }
2071
2072 return 0;
2073}
2074
Linus Walleij8d318a52010-03-30 15:33:42 +02002075static int d40_free_dma(struct d40_chan *d40c)
2076{
2077
2078 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00002079 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02002080 struct d40_phy_res *phy = d40c->phy_chan;
2081 bool is_src;
2082
2083 /* Terminate all queued and active transfers */
2084 d40_term_all(d40c);
2085
2086 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002087 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002088 return -EINVAL;
2089 }
2090
2091 if (phy->allocated_src == D40_ALLOC_FREE &&
2092 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002093 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002094 return -EINVAL;
2095 }
2096
Linus Walleij8d318a52010-03-30 15:33:42 +02002097 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
2098 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
2099 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002100 is_src = false;
2101 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
2102 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002103 is_src = true;
2104 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002105 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002106 return -EINVAL;
2107 }
2108
Narayanan G7fb3e752011-11-17 17:26:41 +05302109 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002110 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2111 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302112 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302113 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002114 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302115
Narayanan G1bdae6f2012-02-09 12:41:37 +05302116 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2117
2118 if (chan_is_logical(d40c))
2119 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2120 else
2121 d40c->base->lookup_phy_chans[phy->num] = NULL;
2122
Narayanan G7fb3e752011-11-17 17:26:41 +05302123 if (d40c->busy) {
2124 pm_runtime_mark_last_busy(d40c->base->dev);
2125 pm_runtime_put_autosuspend(d40c->base->dev);
2126 }
2127
2128 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002129 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002130 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302131out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002132
Narayanan G7fb3e752011-11-17 17:26:41 +05302133 pm_runtime_mark_last_busy(d40c->base->dev);
2134 pm_runtime_put_autosuspend(d40c->base->dev);
2135 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002136}
2137
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002138static bool d40_is_paused(struct d40_chan *d40c)
2139{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002140 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002141 bool is_paused = false;
2142 unsigned long flags;
2143 void __iomem *active_reg;
2144 u32 status;
2145 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002146
2147 spin_lock_irqsave(&d40c->lock, flags);
2148
Rabin Vincent724a8572011-01-25 11:18:08 +01002149 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002150 if (d40c->phy_chan->num % 2 == 0)
2151 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2152 else
2153 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2154
2155 status = (readl(active_reg) &
2156 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2157 D40_CHAN_POS(d40c->phy_chan->num);
2158 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2159 is_paused = true;
2160
2161 goto _exit;
2162 }
2163
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002164 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002165 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002166 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002167 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002168 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002169 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002170 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002171 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002172 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002173 goto _exit;
2174 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002175
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002176 status = (status & D40_EVENTLINE_MASK(event)) >>
2177 D40_EVENTLINE_POS(event);
2178
2179 if (status != D40_DMA_RUN)
2180 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002181_exit:
2182 spin_unlock_irqrestore(&d40c->lock, flags);
2183 return is_paused;
2184
2185}
2186
Linus Walleij8d318a52010-03-30 15:33:42 +02002187static u32 stedma40_residue(struct dma_chan *chan)
2188{
2189 struct d40_chan *d40c =
2190 container_of(chan, struct d40_chan, chan);
2191 u32 bytes_left;
2192 unsigned long flags;
2193
2194 spin_lock_irqsave(&d40c->lock, flags);
2195 bytes_left = d40_residue(d40c);
2196 spin_unlock_irqrestore(&d40c->lock, flags);
2197
2198 return bytes_left;
2199}
2200
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002201static int
2202d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2203 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002204 unsigned int sg_len, dma_addr_t src_dev_addr,
2205 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002206{
2207 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2208 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2209 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002210 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002211
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002212 ret = d40_log_sg_to_lli(sg_src, sg_len,
2213 src_dev_addr,
2214 desc->lli_log.src,
2215 chan->log_def.lcsp1,
2216 src_info->data_width,
2217 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002218
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002219 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2220 dst_dev_addr,
2221 desc->lli_log.dst,
2222 chan->log_def.lcsp3,
2223 dst_info->data_width,
2224 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002225
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002226 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002227}
2228
2229static int
2230d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2231 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002232 unsigned int sg_len, dma_addr_t src_dev_addr,
2233 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002234{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002235 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2236 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2237 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002238 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002239 int ret;
2240
Rabin Vincent0c842b52011-01-25 11:18:35 +01002241 if (desc->cyclic)
2242 flags |= LLI_CYCLIC | LLI_TERM_INT;
2243
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002244 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2245 desc->lli_phy.src,
2246 virt_to_phys(desc->lli_phy.src),
2247 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002248 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002249
2250 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2251 desc->lli_phy.dst,
2252 virt_to_phys(desc->lli_phy.dst),
2253 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002254 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002255
2256 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2257 desc->lli_pool.size, DMA_TO_DEVICE);
2258
2259 return ret < 0 ? ret : 0;
2260}
2261
Rabin Vincent5f811582011-01-25 11:18:18 +01002262static struct d40_desc *
2263d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2264 unsigned int sg_len, unsigned long dma_flags)
2265{
2266 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2267 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002268 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002269
2270 desc = d40_desc_get(chan);
2271 if (!desc)
2272 return NULL;
2273
2274 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2275 cfg->dst_info.data_width);
2276 if (desc->lli_len < 0) {
2277 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002278 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002279 }
2280
Rabin Vincentdbd88782011-01-25 11:18:19 +01002281 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2282 if (ret < 0) {
2283 chan_err(chan, "Could not allocate lli\n");
2284 goto err;
2285 }
2286
Rabin Vincent5f811582011-01-25 11:18:18 +01002287 desc->lli_current = 0;
2288 desc->txd.flags = dma_flags;
2289 desc->txd.tx_submit = d40_tx_submit;
2290
2291 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2292
2293 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002294
2295err:
2296 d40_desc_free(chan, desc);
2297 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002298}
2299
Rabin Vincentcade1d32011-01-25 11:18:23 +01002300static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302301d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002302{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002303 struct stedma40_platform_data *plat = chan->base->plat_data;
2304 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002305 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002306
Rabin Vincentcade1d32011-01-25 11:18:23 +01002307 if (chan->runtime_addr)
2308 return chan->runtime_addr;
2309
Vinod Kouldb8196d2011-10-13 22:34:23 +05302310 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002311 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302312 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002313 addr = plat->dev_tx[cfg->dst_dev_type];
2314
2315 return addr;
2316}
2317
2318static struct dma_async_tx_descriptor *
2319d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2320 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302321 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002322{
2323 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002324 dma_addr_t src_dev_addr = 0;
2325 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002326 struct d40_desc *desc;
2327 unsigned long flags;
2328 int ret;
2329
2330 if (!chan->phy_chan) {
2331 chan_err(chan, "Cannot prepare unallocated channel\n");
2332 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002333 }
2334
Rabin Vincentcade1d32011-01-25 11:18:23 +01002335 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002336
Rabin Vincentcade1d32011-01-25 11:18:23 +01002337 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2338 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002339 goto err;
2340
Rabin Vincent0c842b52011-01-25 11:18:35 +01002341 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2342 desc->cyclic = true;
2343
Linus Walleij7e426da2012-04-12 18:12:52 +02002344 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002345 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2346
Vinod Kouldb8196d2011-10-13 22:34:23 +05302347 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002348 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302349 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002350 dst_dev_addr = dev_addr;
2351 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002352
2353 if (chan_is_logical(chan))
2354 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002355 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002356 else
2357 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002358 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002359
2360 if (ret) {
2361 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2362 chan_is_logical(chan) ? "log" : "phy", ret);
2363 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002364 }
2365
Per Forlin82babbb362011-08-29 13:33:35 +02002366 /*
2367 * add descriptor to the prepare queue in order to be able
2368 * to free them later in terminate_all
2369 */
2370 list_add_tail(&desc->node, &chan->prepare_queue);
2371
Rabin Vincentcade1d32011-01-25 11:18:23 +01002372 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002373
Rabin Vincentcade1d32011-01-25 11:18:23 +01002374 return &desc->txd;
2375
Linus Walleij8d318a52010-03-30 15:33:42 +02002376err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002377 if (desc)
2378 d40_desc_free(chan, desc);
2379 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002380 return NULL;
2381}
Linus Walleij8d318a52010-03-30 15:33:42 +02002382
2383bool stedma40_filter(struct dma_chan *chan, void *data)
2384{
2385 struct stedma40_chan_cfg *info = data;
2386 struct d40_chan *d40c =
2387 container_of(chan, struct d40_chan, chan);
2388 int err;
2389
2390 if (data) {
2391 err = d40_validate_conf(d40c, info);
2392 if (!err)
2393 d40c->dma_cfg = *info;
2394 } else
2395 err = d40_config_memcpy(d40c);
2396
Rabin Vincentce2ca122010-10-12 13:00:49 +00002397 if (!err)
2398 d40c->configured = true;
2399
Linus Walleij8d318a52010-03-30 15:33:42 +02002400 return err == 0;
2401}
2402EXPORT_SYMBOL(stedma40_filter);
2403
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002404static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2405{
2406 bool realtime = d40c->dma_cfg.realtime;
2407 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002408 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002409 u32 event = D40_TYPE_TO_EVENT(dev_type);
2410 u32 group = D40_TYPE_TO_GROUP(dev_type);
2411 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302412 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002413 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302414
Tong Liu3cb645d2012-09-26 10:07:30 +00002415 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302416 /*
2417 * Due to a hardware bug, in some cases a logical channel triggered by
2418 * a high priority destination event line can generate extra packet
2419 * transactions.
2420 *
2421 * The workaround is to not set the high priority level for the
2422 * destination event lines that trigger logical channels.
2423 */
2424 if (!src && chan_is_logical(d40c))
2425 highprio = false;
2426
Tong Liu3cb645d2012-09-26 10:07:30 +00002427 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002428
2429 /* Destination event lines are stored in the upper halfword */
2430 if (!src)
2431 bit <<= 16;
2432
2433 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2434 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2435}
2436
2437static void d40_set_prio_realtime(struct d40_chan *d40c)
2438{
2439 if (d40c->base->rev < 3)
2440 return;
2441
2442 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2443 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2444 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2445
2446 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2447 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2448 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2449}
2450
Linus Walleij8d318a52010-03-30 15:33:42 +02002451/* DMA ENGINE functions */
2452static int d40_alloc_chan_resources(struct dma_chan *chan)
2453{
2454 int err;
2455 unsigned long flags;
2456 struct d40_chan *d40c =
2457 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002458 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002459 spin_lock_irqsave(&d40c->lock, flags);
2460
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002461 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002462
Rabin Vincentce2ca122010-10-12 13:00:49 +00002463 /* If no dma configuration is set use default configuration (memcpy) */
2464 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002465 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002466 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002467 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002468 goto fail;
2469 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002470 }
2471
Narayanan G5cd326f2011-11-30 19:20:42 +05302472 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002473 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002474 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302475 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002476 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002477 }
2478
Narayanan G7fb3e752011-11-17 17:26:41 +05302479 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002480 /* Fill in basic CFG register values */
2481 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002482 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002483
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002484 d40_set_prio_realtime(d40c);
2485
Rabin Vincent724a8572011-01-25 11:18:08 +01002486 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002487 d40_log_cfg(&d40c->dma_cfg,
2488 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2489
2490 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2491 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002492 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002493 else
2494 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002495 d40c->dma_cfg.dst_dev_type *
2496 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Linus Walleijef1872e2010-06-20 21:24:52 +00002497 }
2498
Narayanan G5cd326f2011-11-30 19:20:42 +05302499 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2500 chan_is_logical(d40c) ? "logical" : "physical",
2501 d40c->phy_chan->num,
2502 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2503
2504
Linus Walleijef1872e2010-06-20 21:24:52 +00002505 /*
2506 * Only write channel configuration to the DMA if the physical
2507 * resource is free. In case of multiple logical channels
2508 * on the same physical resource, only the first write is necessary.
2509 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002510 if (is_free_phy)
2511 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002512fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302513 pm_runtime_mark_last_busy(d40c->base->dev);
2514 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002515 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002516 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002517}
2518
2519static void d40_free_chan_resources(struct dma_chan *chan)
2520{
2521 struct d40_chan *d40c =
2522 container_of(chan, struct d40_chan, chan);
2523 int err;
2524 unsigned long flags;
2525
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002526 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002527 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002528 return;
2529 }
2530
Linus Walleij8d318a52010-03-30 15:33:42 +02002531 spin_lock_irqsave(&d40c->lock, flags);
2532
2533 err = d40_free_dma(d40c);
2534
2535 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002536 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002537 spin_unlock_irqrestore(&d40c->lock, flags);
2538}
2539
2540static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2541 dma_addr_t dst,
2542 dma_addr_t src,
2543 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002544 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002545{
Rabin Vincent95944c62011-01-25 11:18:17 +01002546 struct scatterlist dst_sg;
2547 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002548
Rabin Vincent95944c62011-01-25 11:18:17 +01002549 sg_init_table(&dst_sg, 1);
2550 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002551
Rabin Vincent95944c62011-01-25 11:18:17 +01002552 sg_dma_address(&dst_sg) = dst;
2553 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002554
Rabin Vincent95944c62011-01-25 11:18:17 +01002555 sg_dma_len(&dst_sg) = size;
2556 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002557
Rabin Vincentcade1d32011-01-25 11:18:23 +01002558 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002559}
2560
Ira Snyder0d688662010-09-30 11:46:47 +00002561static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002562d40_prep_memcpy_sg(struct dma_chan *chan,
2563 struct scatterlist *dst_sg, unsigned int dst_nents,
2564 struct scatterlist *src_sg, unsigned int src_nents,
2565 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002566{
2567 if (dst_nents != src_nents)
2568 return NULL;
2569
Rabin Vincentcade1d32011-01-25 11:18:23 +01002570 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002571}
2572
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002573static struct dma_async_tx_descriptor *
2574d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2575 unsigned int sg_len, enum dma_transfer_direction direction,
2576 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002577{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002578 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002579 return NULL;
2580
Rabin Vincentcade1d32011-01-25 11:18:23 +01002581 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002582}
2583
Rabin Vincent0c842b52011-01-25 11:18:35 +01002584static struct dma_async_tx_descriptor *
2585dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2586 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002587 enum dma_transfer_direction direction, unsigned long flags,
2588 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002589{
2590 unsigned int periods = buf_len / period_len;
2591 struct dma_async_tx_descriptor *txd;
2592 struct scatterlist *sg;
2593 int i;
2594
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002595 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002596 for (i = 0; i < periods; i++) {
2597 sg_dma_address(&sg[i]) = dma_addr;
2598 sg_dma_len(&sg[i]) = period_len;
2599 dma_addr += period_len;
2600 }
2601
2602 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002603 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002604 sg[periods].page_link =
2605 ((unsigned long)sg | 0x01) & ~0x02;
2606
2607 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2608 DMA_PREP_INTERRUPT);
2609
2610 kfree(sg);
2611
2612 return txd;
2613}
2614
Linus Walleij8d318a52010-03-30 15:33:42 +02002615static enum dma_status d40_tx_status(struct dma_chan *chan,
2616 dma_cookie_t cookie,
2617 struct dma_tx_state *txstate)
2618{
2619 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002620 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002621
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002622 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002623 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002624 return -EINVAL;
2625 }
2626
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002627 ret = dma_cookie_status(chan, cookie, txstate);
2628 if (ret != DMA_SUCCESS)
2629 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002630
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002631 if (d40_is_paused(d40c))
2632 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002633
2634 return ret;
2635}
2636
2637static void d40_issue_pending(struct dma_chan *chan)
2638{
2639 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2640 unsigned long flags;
2641
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002642 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002643 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002644 return;
2645 }
2646
Linus Walleij8d318a52010-03-30 15:33:42 +02002647 spin_lock_irqsave(&d40c->lock, flags);
2648
Per Forlina8f30672011-06-26 23:29:52 +02002649 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2650
2651 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002652 if (!d40c->busy)
2653 (void) d40_queue_start(d40c);
2654
2655 spin_unlock_irqrestore(&d40c->lock, flags);
2656}
2657
Narayanan G1bdae6f2012-02-09 12:41:37 +05302658static void d40_terminate_all(struct dma_chan *chan)
2659{
2660 unsigned long flags;
2661 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2662 int ret;
2663
2664 spin_lock_irqsave(&d40c->lock, flags);
2665
2666 pm_runtime_get_sync(d40c->base->dev);
2667 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2668 if (ret)
2669 chan_err(d40c, "Failed to stop channel\n");
2670
2671 d40_term_all(d40c);
2672 pm_runtime_mark_last_busy(d40c->base->dev);
2673 pm_runtime_put_autosuspend(d40c->base->dev);
2674 if (d40c->busy) {
2675 pm_runtime_mark_last_busy(d40c->base->dev);
2676 pm_runtime_put_autosuspend(d40c->base->dev);
2677 }
2678 d40c->busy = false;
2679
2680 spin_unlock_irqrestore(&d40c->lock, flags);
2681}
2682
Rabin Vincent98ca5282011-06-27 11:33:38 +02002683static int
2684dma40_config_to_halfchannel(struct d40_chan *d40c,
2685 struct stedma40_half_channel_info *info,
2686 enum dma_slave_buswidth width,
2687 u32 maxburst)
2688{
2689 enum stedma40_periph_data_width addr_width;
2690 int psize;
2691
2692 switch (width) {
2693 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2694 addr_width = STEDMA40_BYTE_WIDTH;
2695 break;
2696 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2697 addr_width = STEDMA40_HALFWORD_WIDTH;
2698 break;
2699 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2700 addr_width = STEDMA40_WORD_WIDTH;
2701 break;
2702 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2703 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2704 break;
2705 default:
2706 dev_err(d40c->base->dev,
2707 "illegal peripheral address width "
2708 "requested (%d)\n",
2709 width);
2710 return -EINVAL;
2711 }
2712
2713 if (chan_is_logical(d40c)) {
2714 if (maxburst >= 16)
2715 psize = STEDMA40_PSIZE_LOG_16;
2716 else if (maxburst >= 8)
2717 psize = STEDMA40_PSIZE_LOG_8;
2718 else if (maxburst >= 4)
2719 psize = STEDMA40_PSIZE_LOG_4;
2720 else
2721 psize = STEDMA40_PSIZE_LOG_1;
2722 } else {
2723 if (maxburst >= 16)
2724 psize = STEDMA40_PSIZE_PHY_16;
2725 else if (maxburst >= 8)
2726 psize = STEDMA40_PSIZE_PHY_8;
2727 else if (maxburst >= 4)
2728 psize = STEDMA40_PSIZE_PHY_4;
2729 else
2730 psize = STEDMA40_PSIZE_PHY_1;
2731 }
2732
2733 info->data_width = addr_width;
2734 info->psize = psize;
2735 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2736
2737 return 0;
2738}
2739
Linus Walleij95e14002010-08-04 13:37:45 +02002740/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002741static int d40_set_runtime_config(struct dma_chan *chan,
2742 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002743{
2744 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2745 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002746 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002747 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002748 u32 src_maxburst, dst_maxburst;
2749 int ret;
2750
2751 src_addr_width = config->src_addr_width;
2752 src_maxburst = config->src_maxburst;
2753 dst_addr_width = config->dst_addr_width;
2754 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002755
Vinod Kouldb8196d2011-10-13 22:34:23 +05302756 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002757 dma_addr_t dev_addr_rx =
2758 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2759
2760 config_addr = config->src_addr;
2761 if (dev_addr_rx)
2762 dev_dbg(d40c->base->dev,
2763 "channel has a pre-wired RX address %08x "
2764 "overriding with %08x\n",
2765 dev_addr_rx, config_addr);
2766 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2767 dev_dbg(d40c->base->dev,
2768 "channel was not configured for peripheral "
2769 "to memory transfer (%d) overriding\n",
2770 cfg->dir);
2771 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2772
Rabin Vincent98ca5282011-06-27 11:33:38 +02002773 /* Configure the memory side */
2774 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2775 dst_addr_width = src_addr_width;
2776 if (dst_maxburst == 0)
2777 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002778
Vinod Kouldb8196d2011-10-13 22:34:23 +05302779 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002780 dma_addr_t dev_addr_tx =
2781 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2782
2783 config_addr = config->dst_addr;
2784 if (dev_addr_tx)
2785 dev_dbg(d40c->base->dev,
2786 "channel has a pre-wired TX address %08x "
2787 "overriding with %08x\n",
2788 dev_addr_tx, config_addr);
2789 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2790 dev_dbg(d40c->base->dev,
2791 "channel was not configured for memory "
2792 "to peripheral transfer (%d) overriding\n",
2793 cfg->dir);
2794 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2795
Rabin Vincent98ca5282011-06-27 11:33:38 +02002796 /* Configure the memory side */
2797 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2798 src_addr_width = dst_addr_width;
2799 if (src_maxburst == 0)
2800 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002801 } else {
2802 dev_err(d40c->base->dev,
2803 "unrecognized channel direction %d\n",
2804 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002805 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002806 }
2807
Rabin Vincent98ca5282011-06-27 11:33:38 +02002808 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002809 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002810 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2811 src_maxburst,
2812 src_addr_width,
2813 dst_maxburst,
2814 dst_addr_width);
2815 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002816 }
2817
Per Forlin92bb6cd2011-10-13 12:11:36 +02002818 if (src_maxburst > 16) {
2819 src_maxburst = 16;
2820 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2821 } else if (dst_maxburst > 16) {
2822 dst_maxburst = 16;
2823 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2824 }
2825
Rabin Vincent98ca5282011-06-27 11:33:38 +02002826 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2827 src_addr_width,
2828 src_maxburst);
2829 if (ret)
2830 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002831
Rabin Vincent98ca5282011-06-27 11:33:38 +02002832 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2833 dst_addr_width,
2834 dst_maxburst);
2835 if (ret)
2836 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002837
Per Forlina59670a2010-10-06 09:05:27 +00002838 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002839 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002840 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2841 else
2842 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2843 &d40c->dst_def_cfg, false);
2844
Linus Walleij95e14002010-08-04 13:37:45 +02002845 /* These settings will take precedence later */
2846 d40c->runtime_addr = config_addr;
2847 d40c->runtime_direction = config->direction;
2848 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002849 "configured channel %s for %s, data width %d/%d, "
2850 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002851 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302852 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002853 src_addr_width, dst_addr_width,
2854 src_maxburst, dst_maxburst);
2855
2856 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002857}
2858
Linus Walleij05827632010-05-17 16:30:42 -07002859static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2860 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002861{
Linus Walleij8d318a52010-03-30 15:33:42 +02002862 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2863
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002864 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002865 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002866 return -EINVAL;
2867 }
2868
Linus Walleij8d318a52010-03-30 15:33:42 +02002869 switch (cmd) {
2870 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302871 d40_terminate_all(chan);
2872 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002873 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002874 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002875 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002876 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002877 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002878 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002879 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002880 default:
2881 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002882 }
2883
2884 /* Other commands are unimplemented */
2885 return -ENXIO;
2886}
2887
2888/* Initialization functions */
2889
2890static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2891 struct d40_chan *chans, int offset,
2892 int num_chans)
2893{
2894 int i = 0;
2895 struct d40_chan *d40c;
2896
2897 INIT_LIST_HEAD(&dma->channels);
2898
2899 for (i = offset; i < offset + num_chans; i++) {
2900 d40c = &chans[i];
2901 d40c->base = base;
2902 d40c->chan.device = dma;
2903
Linus Walleij8d318a52010-03-30 15:33:42 +02002904 spin_lock_init(&d40c->lock);
2905
2906 d40c->log_num = D40_PHY_CHAN;
2907
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002908 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002909 INIT_LIST_HEAD(&d40c->active);
2910 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002911 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002912 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002913 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002914
Linus Walleij8d318a52010-03-30 15:33:42 +02002915 tasklet_init(&d40c->tasklet, dma_tasklet,
2916 (unsigned long) d40c);
2917
2918 list_add_tail(&d40c->chan.device_node,
2919 &dma->channels);
2920 }
2921}
2922
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002923static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2924{
2925 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2926 dev->device_prep_slave_sg = d40_prep_slave_sg;
2927
2928 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2929 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2930
2931 /*
2932 * This controller can only access address at even
2933 * 32bit boundaries, i.e. 2^2
2934 */
2935 dev->copy_align = 2;
2936 }
2937
2938 if (dma_has_cap(DMA_SG, dev->cap_mask))
2939 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2940
Rabin Vincent0c842b52011-01-25 11:18:35 +01002941 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2942 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2943
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002944 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2945 dev->device_free_chan_resources = d40_free_chan_resources;
2946 dev->device_issue_pending = d40_issue_pending;
2947 dev->device_tx_status = d40_tx_status;
2948 dev->device_control = d40_control;
2949 dev->dev = base->dev;
2950}
2951
Linus Walleij8d318a52010-03-30 15:33:42 +02002952static int __init d40_dmaengine_init(struct d40_base *base,
2953 int num_reserved_chans)
2954{
2955 int err ;
2956
2957 d40_chan_init(base, &base->dma_slave, base->log_chans,
2958 0, base->num_log_chans);
2959
2960 dma_cap_zero(base->dma_slave.cap_mask);
2961 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002962 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002963
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002964 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002965
2966 err = dma_async_device_register(&base->dma_slave);
2967
2968 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002969 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002970 goto failure1;
2971 }
2972
2973 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002974 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002975
2976 dma_cap_zero(base->dma_memcpy.cap_mask);
2977 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002978 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002979
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002980 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002981
2982 err = dma_async_device_register(&base->dma_memcpy);
2983
2984 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002985 d40_err(base->dev,
2986 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002987 goto failure2;
2988 }
2989
2990 d40_chan_init(base, &base->dma_both, base->phy_chans,
2991 0, num_reserved_chans);
2992
2993 dma_cap_zero(base->dma_both.cap_mask);
2994 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2995 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002996 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002997 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002998
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002999 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02003000 err = dma_async_device_register(&base->dma_both);
3001
3002 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003003 d40_err(base->dev,
3004 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003005 goto failure3;
3006 }
3007 return 0;
3008failure3:
3009 dma_async_device_unregister(&base->dma_memcpy);
3010failure2:
3011 dma_async_device_unregister(&base->dma_slave);
3012failure1:
3013 return err;
3014}
3015
Narayanan G7fb3e752011-11-17 17:26:41 +05303016/* Suspend resume functionality */
3017#ifdef CONFIG_PM
3018static int dma40_pm_suspend(struct device *dev)
3019{
Narayanan G28c7a192011-11-22 13:56:55 +05303020 struct platform_device *pdev = to_platform_device(dev);
3021 struct d40_base *base = platform_get_drvdata(pdev);
3022 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05303023
Narayanan G28c7a192011-11-22 13:56:55 +05303024 if (base->lcpa_regulator)
3025 ret = regulator_disable(base->lcpa_regulator);
3026 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05303027}
3028
3029static int dma40_runtime_suspend(struct device *dev)
3030{
3031 struct platform_device *pdev = to_platform_device(dev);
3032 struct d40_base *base = platform_get_drvdata(pdev);
3033
3034 d40_save_restore_registers(base, true);
3035
3036 /* Don't disable/enable clocks for v1 due to HW bugs */
3037 if (base->rev != 1)
3038 writel_relaxed(base->gcc_pwr_off_mask,
3039 base->virtbase + D40_DREG_GCC);
3040
3041 return 0;
3042}
3043
3044static int dma40_runtime_resume(struct device *dev)
3045{
3046 struct platform_device *pdev = to_platform_device(dev);
3047 struct d40_base *base = platform_get_drvdata(pdev);
3048
3049 if (base->initialized)
3050 d40_save_restore_registers(base, false);
3051
3052 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3053 base->virtbase + D40_DREG_GCC);
3054 return 0;
3055}
3056
Narayanan G28c7a192011-11-22 13:56:55 +05303057static int dma40_resume(struct device *dev)
3058{
3059 struct platform_device *pdev = to_platform_device(dev);
3060 struct d40_base *base = platform_get_drvdata(pdev);
3061 int ret = 0;
3062
3063 if (base->lcpa_regulator)
3064 ret = regulator_enable(base->lcpa_regulator);
3065
3066 return ret;
3067}
Narayanan G7fb3e752011-11-17 17:26:41 +05303068
3069static const struct dev_pm_ops dma40_pm_ops = {
3070 .suspend = dma40_pm_suspend,
3071 .runtime_suspend = dma40_runtime_suspend,
3072 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303073 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303074};
3075#define DMA40_PM_OPS (&dma40_pm_ops)
3076#else
3077#define DMA40_PM_OPS NULL
3078#endif
3079
Linus Walleij8d318a52010-03-30 15:33:42 +02003080/* Initialization functions. */
3081
3082static int __init d40_phy_res_init(struct d40_base *base)
3083{
3084 int i;
3085 int num_phy_chans_avail = 0;
3086 u32 val[2];
3087 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303088 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003089
3090 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3091 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3092
3093 for (i = 0; i < base->num_phy_chans; i++) {
3094 base->phy_res[i].num = i;
3095 odd_even_bit += 2 * ((i % 2) == 0);
3096 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3097 /* Mark security only channels as occupied */
3098 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3099 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303100 base->phy_res[i].reserved = true;
3101 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3102 D40_DREG_GCC_SRC);
3103 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3104 D40_DREG_GCC_DST);
3105
3106
Linus Walleij8d318a52010-03-30 15:33:42 +02003107 } else {
3108 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3109 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303110 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003111 num_phy_chans_avail++;
3112 }
3113 spin_lock_init(&base->phy_res[i].lock);
3114 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003115
3116 /* Mark disabled channels as occupied */
3117 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003118 int chan = base->plat_data->disabled_channels[i];
3119
3120 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3121 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303122 base->phy_res[chan].reserved = true;
3123 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3124 D40_DREG_GCC_SRC);
3125 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3126 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003127 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003128 }
3129
Fabio Baltieri74070482012-12-18 12:25:14 +01003130 /* Mark soft_lli channels */
3131 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3132 int chan = base->plat_data->soft_lli_chans[i];
3133
3134 base->phy_res[chan].use_soft_lli = true;
3135 }
3136
Linus Walleij8d318a52010-03-30 15:33:42 +02003137 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3138 num_phy_chans_avail, base->num_phy_chans);
3139
3140 /* Verify settings extended vs standard */
3141 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3142
3143 for (i = 0; i < base->num_phy_chans; i++) {
3144
3145 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3146 (val[0] & 0x3) != 1)
3147 dev_info(base->dev,
3148 "[%s] INFO: channel %d is misconfigured (%d)\n",
3149 __func__, i, val[0] & 0x3);
3150
3151 val[0] = val[0] >> 2;
3152 }
3153
Narayanan G7fb3e752011-11-17 17:26:41 +05303154 /*
3155 * To keep things simple, Enable all clocks initially.
3156 * The clocks will get managed later post channel allocation.
3157 * The clocks for the event lines on which reserved channels exists
3158 * are not managed here.
3159 */
3160 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3161 base->gcc_pwr_off_mask = gcc;
3162
Linus Walleij8d318a52010-03-30 15:33:42 +02003163 return num_phy_chans_avail;
3164}
3165
3166static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3167{
Linus Walleij8d318a52010-03-30 15:33:42 +02003168 struct stedma40_platform_data *plat_data;
3169 struct clk *clk = NULL;
3170 void __iomem *virtbase = NULL;
3171 struct resource *res = NULL;
3172 struct d40_base *base = NULL;
3173 int num_log_chans = 0;
3174 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003175 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003176 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003177 u32 pid;
3178 u32 cid;
3179 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003180
3181 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003182 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003183 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003184 goto failure;
3185 }
3186
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003187 clk_ret = clk_prepare_enable(clk);
3188 if (clk_ret) {
3189 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3190 goto failure;
3191 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003192
3193 /* Get IO for DMAC base address */
3194 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3195 if (!res)
3196 goto failure;
3197
3198 if (request_mem_region(res->start, resource_size(res),
3199 D40_NAME " I/O base") == NULL)
3200 goto failure;
3201
3202 virtbase = ioremap(res->start, resource_size(res));
3203 if (!virtbase)
3204 goto failure;
3205
Linus Walleijf4b89762011-06-27 11:33:46 +02003206 /* This is just a regular AMBA PrimeCell ID actually */
3207 for (pid = 0, i = 0; i < 4; i++)
3208 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3209 & 255) << (i * 8);
3210 for (cid = 0, i = 0; i < 4; i++)
3211 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3212 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003213
Linus Walleijf4b89762011-06-27 11:33:46 +02003214 if (cid != AMBA_CID) {
3215 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003216 goto failure;
3217 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003218 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3219 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3220 AMBA_MANF_BITS(pid),
3221 AMBA_VENDOR_ST);
3222 goto failure;
3223 }
3224 /*
3225 * HW revision:
3226 * DB8500ed has revision 0
3227 * ? has revision 1
3228 * DB8500v1 has revision 2
3229 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003230 * AP9540v1 has revision 4
3231 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003232 */
3233 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003234
Gerald Baeza47db92f2012-09-21 21:21:37 +02003235 plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003236
Gerald Baeza47db92f2012-09-21 21:21:37 +02003237 /* The number of physical channels on this HW */
3238 if (plat_data->num_of_phy_chans)
3239 num_phy_chans = plat_data->num_of_phy_chans;
3240 else
3241 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3242
3243 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x with %d physical channels\n",
3244 rev, res->start, num_phy_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003245
Narayanan G1bdae6f2012-02-09 12:41:37 +05303246 if (rev < 2) {
3247 d40_err(&pdev->dev, "hardware revision: %d is not supported",
3248 rev);
3249 goto failure;
3250 }
3251
Linus Walleij8d318a52010-03-30 15:33:42 +02003252 /* Count the number of logical channels in use */
3253 for (i = 0; i < plat_data->dev_len; i++)
3254 if (plat_data->dev_rx[i] != 0)
3255 num_log_chans++;
3256
3257 for (i = 0; i < plat_data->dev_len; i++)
3258 if (plat_data->dev_tx[i] != 0)
3259 num_log_chans++;
3260
3261 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003262 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003263 sizeof(struct d40_chan), GFP_KERNEL);
3264
3265 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003266 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003267 goto failure;
3268 }
3269
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003270 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003271 base->clk = clk;
3272 base->num_phy_chans = num_phy_chans;
3273 base->num_log_chans = num_log_chans;
3274 base->phy_start = res->start;
3275 base->phy_size = resource_size(res);
3276 base->virtbase = virtbase;
3277 base->plat_data = plat_data;
3278 base->dev = &pdev->dev;
3279 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3280 base->log_chans = &base->phy_chans[num_phy_chans];
3281
Tong Liu3cb645d2012-09-26 10:07:30 +00003282 if (base->plat_data->num_of_phy_chans == 14) {
3283 base->gen_dmac.backup = d40_backup_regs_v4b;
3284 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3285 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3286 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3287 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3288 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3289 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3290 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3291 base->gen_dmac.il = il_v4b;
3292 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3293 base->gen_dmac.init_reg = dma_init_reg_v4b;
3294 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3295 } else {
3296 if (base->rev >= 3) {
3297 base->gen_dmac.backup = d40_backup_regs_v4a;
3298 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3299 }
3300 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3301 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3302 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3303 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3304 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3305 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3306 base->gen_dmac.il = il_v4a;
3307 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3308 base->gen_dmac.init_reg = dma_init_reg_v4a;
3309 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3310 }
3311
Linus Walleij8d318a52010-03-30 15:33:42 +02003312 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3313 GFP_KERNEL);
3314 if (!base->phy_res)
3315 goto failure;
3316
3317 base->lookup_phy_chans = kzalloc(num_phy_chans *
3318 sizeof(struct d40_chan *),
3319 GFP_KERNEL);
3320 if (!base->lookup_phy_chans)
3321 goto failure;
3322
Lee Jones664a57e2013-05-03 15:31:53 +01003323 if (num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02003324 /*
3325 * The max number of logical channels are event lines for all
3326 * src devices and dst devices
3327 */
3328 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
3329 sizeof(struct d40_chan *),
3330 GFP_KERNEL);
3331 if (!base->lookup_log_chans)
3332 goto failure;
3333 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00003334
Narayanan G7fb3e752011-11-17 17:26:41 +05303335 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3336 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003337 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303338 if (!base->reg_val_backup_chan)
3339 goto failure;
3340
3341 base->lcla_pool.alloc_map =
3342 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3343 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003344 if (!base->lcla_pool.alloc_map)
3345 goto failure;
3346
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003347 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3348 0, SLAB_HWCACHE_ALIGN,
3349 NULL);
3350 if (base->desc_slab == NULL)
3351 goto failure;
3352
Linus Walleij8d318a52010-03-30 15:33:42 +02003353 return base;
3354
3355failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003356 if (!clk_ret)
3357 clk_disable_unprepare(clk);
3358 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003359 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003360 if (virtbase)
3361 iounmap(virtbase);
3362 if (res)
3363 release_mem_region(res->start,
3364 resource_size(res));
3365 if (virtbase)
3366 iounmap(virtbase);
3367
3368 if (base) {
3369 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303370 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003371 kfree(base->lookup_log_chans);
3372 kfree(base->lookup_phy_chans);
3373 kfree(base->phy_res);
3374 kfree(base);
3375 }
3376
3377 return NULL;
3378}
3379
3380static void __init d40_hw_init(struct d40_base *base)
3381{
3382
Linus Walleij8d318a52010-03-30 15:33:42 +02003383 int i;
3384 u32 prmseo[2] = {0, 0};
3385 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3386 u32 pcmis = 0;
3387 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003388 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3389 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003390
Tong Liu3cb645d2012-09-26 10:07:30 +00003391 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003392 writel(dma_init_reg[i].val,
3393 base->virtbase + dma_init_reg[i].reg);
3394
3395 /* Configure all our dma channels to default settings */
3396 for (i = 0; i < base->num_phy_chans; i++) {
3397
3398 activeo[i % 2] = activeo[i % 2] << 2;
3399
3400 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3401 == D40_ALLOC_PHY) {
3402 activeo[i % 2] |= 3;
3403 continue;
3404 }
3405
3406 /* Enable interrupt # */
3407 pcmis = (pcmis << 1) | 1;
3408
3409 /* Clear interrupt # */
3410 pcicr = (pcicr << 1) | 1;
3411
3412 /* Set channel to physical mode */
3413 prmseo[i % 2] = prmseo[i % 2] << 2;
3414 prmseo[i % 2] |= 1;
3415
3416 }
3417
3418 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3419 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3420 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3421 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3422
3423 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003424 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003425
3426 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003427 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003428
Tong Liu3cb645d2012-09-26 10:07:30 +00003429 /* These are __initdata and cannot be accessed after init */
3430 base->gen_dmac.init_reg = NULL;
3431 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003432}
3433
Linus Walleij508849a2010-06-20 21:26:07 +00003434static int __init d40_lcla_allocate(struct d40_base *base)
3435{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003436 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003437 unsigned long *page_list;
3438 int i, j;
3439 int ret = 0;
3440
3441 /*
3442 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3443 * To full fill this hardware requirement without wasting 256 kb
3444 * we allocate pages until we get an aligned one.
3445 */
3446 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3447 GFP_KERNEL);
3448
3449 if (!page_list) {
3450 ret = -ENOMEM;
3451 goto failure;
3452 }
3453
3454 /* Calculating how many pages that are required */
3455 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3456
3457 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3458 page_list[i] = __get_free_pages(GFP_KERNEL,
3459 base->lcla_pool.pages);
3460 if (!page_list[i]) {
3461
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003462 d40_err(base->dev, "Failed to allocate %d pages.\n",
3463 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003464
3465 for (j = 0; j < i; j++)
3466 free_pages(page_list[j], base->lcla_pool.pages);
3467 goto failure;
3468 }
3469
3470 if ((virt_to_phys((void *)page_list[i]) &
3471 (LCLA_ALIGNMENT - 1)) == 0)
3472 break;
3473 }
3474
3475 for (j = 0; j < i; j++)
3476 free_pages(page_list[j], base->lcla_pool.pages);
3477
3478 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3479 base->lcla_pool.base = (void *)page_list[i];
3480 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003481 /*
3482 * After many attempts and no succees with finding the correct
3483 * alignment, try with allocating a big buffer.
3484 */
Linus Walleij508849a2010-06-20 21:26:07 +00003485 dev_warn(base->dev,
3486 "[%s] Failed to get %d pages @ 18 bit align.\n",
3487 __func__, base->lcla_pool.pages);
3488 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3489 base->num_phy_chans +
3490 LCLA_ALIGNMENT,
3491 GFP_KERNEL);
3492 if (!base->lcla_pool.base_unaligned) {
3493 ret = -ENOMEM;
3494 goto failure;
3495 }
3496
3497 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3498 LCLA_ALIGNMENT);
3499 }
3500
Rabin Vincent026cbc42011-01-25 11:18:14 +01003501 pool->dma_addr = dma_map_single(base->dev, pool->base,
3502 SZ_1K * base->num_phy_chans,
3503 DMA_TO_DEVICE);
3504 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3505 pool->dma_addr = 0;
3506 ret = -ENOMEM;
3507 goto failure;
3508 }
3509
Linus Walleij508849a2010-06-20 21:26:07 +00003510 writel(virt_to_phys(base->lcla_pool.base),
3511 base->virtbase + D40_DREG_LCLA);
3512failure:
3513 kfree(page_list);
3514 return ret;
3515}
3516
Linus Walleij8d318a52010-03-30 15:33:42 +02003517static int __init d40_probe(struct platform_device *pdev)
3518{
3519 int err;
3520 int ret = -ENOENT;
3521 struct d40_base *base;
3522 struct resource *res = NULL;
3523 int num_reserved_chans;
3524 u32 val;
3525
3526 base = d40_hw_detect_init(pdev);
3527
3528 if (!base)
3529 goto failure;
3530
3531 num_reserved_chans = d40_phy_res_init(base);
3532
3533 platform_set_drvdata(pdev, base);
3534
3535 spin_lock_init(&base->interrupt_lock);
3536 spin_lock_init(&base->execmd_lock);
3537
3538 /* Get IO for logical channel parameter address */
3539 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3540 if (!res) {
3541 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003542 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003543 goto failure;
3544 }
3545 base->lcpa_size = resource_size(res);
3546 base->phy_lcpa = res->start;
3547
3548 if (request_mem_region(res->start, resource_size(res),
3549 D40_NAME " I/O lcpa") == NULL) {
3550 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003551 d40_err(&pdev->dev,
3552 "Failed to request LCPA region 0x%x-0x%x\n",
3553 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003554 goto failure;
3555 }
3556
3557 /* We make use of ESRAM memory for this. */
3558 val = readl(base->virtbase + D40_DREG_LCPA);
3559 if (res->start != val && val != 0) {
3560 dev_warn(&pdev->dev,
3561 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3562 __func__, val, res->start);
3563 } else
3564 writel(res->start, base->virtbase + D40_DREG_LCPA);
3565
3566 base->lcpa_base = ioremap(res->start, resource_size(res));
3567 if (!base->lcpa_base) {
3568 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003569 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003570 goto failure;
3571 }
Narayanan G28c7a192011-11-22 13:56:55 +05303572 /* If lcla has to be located in ESRAM we don't need to allocate */
3573 if (base->plat_data->use_esram_lcla) {
3574 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3575 "lcla_esram");
3576 if (!res) {
3577 ret = -ENOENT;
3578 d40_err(&pdev->dev,
3579 "No \"lcla_esram\" memory resource\n");
3580 goto failure;
3581 }
3582 base->lcla_pool.base = ioremap(res->start,
3583 resource_size(res));
3584 if (!base->lcla_pool.base) {
3585 ret = -ENOMEM;
3586 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3587 goto failure;
3588 }
3589 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003590
Narayanan G28c7a192011-11-22 13:56:55 +05303591 } else {
3592 ret = d40_lcla_allocate(base);
3593 if (ret) {
3594 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3595 goto failure;
3596 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003597 }
3598
Linus Walleij8d318a52010-03-30 15:33:42 +02003599 spin_lock_init(&base->lcla_pool.lock);
3600
Linus Walleij8d318a52010-03-30 15:33:42 +02003601 base->irq = platform_get_irq(pdev, 0);
3602
3603 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003604 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003605 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003606 goto failure;
3607 }
3608
Narayanan G7fb3e752011-11-17 17:26:41 +05303609 pm_runtime_irq_safe(base->dev);
3610 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3611 pm_runtime_use_autosuspend(base->dev);
3612 pm_runtime_enable(base->dev);
3613 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303614
3615 if (base->plat_data->use_esram_lcla) {
3616
3617 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3618 if (IS_ERR(base->lcpa_regulator)) {
3619 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3620 base->lcpa_regulator = NULL;
3621 goto failure;
3622 }
3623
3624 ret = regulator_enable(base->lcpa_regulator);
3625 if (ret) {
3626 d40_err(&pdev->dev,
3627 "Failed to enable lcpa_regulator\n");
3628 regulator_put(base->lcpa_regulator);
3629 base->lcpa_regulator = NULL;
3630 goto failure;
3631 }
3632 }
3633
Narayanan G7fb3e752011-11-17 17:26:41 +05303634 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003635 err = d40_dmaengine_init(base, num_reserved_chans);
3636 if (err)
3637 goto failure;
3638
Per Forlinb96710e2011-10-18 18:39:47 +02003639 base->dev->dma_parms = &base->dma_parms;
3640 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3641 if (err) {
3642 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3643 goto failure;
3644 }
3645
Linus Walleij8d318a52010-03-30 15:33:42 +02003646 d40_hw_init(base);
3647
3648 dev_info(base->dev, "initialized\n");
3649 return 0;
3650
3651failure:
3652 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003653 if (base->desc_slab)
3654 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003655 if (base->virtbase)
3656 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003657
Narayanan G28c7a192011-11-22 13:56:55 +05303658 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3659 iounmap(base->lcla_pool.base);
3660 base->lcla_pool.base = NULL;
3661 }
3662
Rabin Vincent026cbc42011-01-25 11:18:14 +01003663 if (base->lcla_pool.dma_addr)
3664 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3665 SZ_1K * base->num_phy_chans,
3666 DMA_TO_DEVICE);
3667
Linus Walleij508849a2010-06-20 21:26:07 +00003668 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3669 free_pages((unsigned long)base->lcla_pool.base,
3670 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003671
3672 kfree(base->lcla_pool.base_unaligned);
3673
Linus Walleij8d318a52010-03-30 15:33:42 +02003674 if (base->phy_lcpa)
3675 release_mem_region(base->phy_lcpa,
3676 base->lcpa_size);
3677 if (base->phy_start)
3678 release_mem_region(base->phy_start,
3679 base->phy_size);
3680 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003681 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003682 clk_put(base->clk);
3683 }
3684
Narayanan G28c7a192011-11-22 13:56:55 +05303685 if (base->lcpa_regulator) {
3686 regulator_disable(base->lcpa_regulator);
3687 regulator_put(base->lcpa_regulator);
3688 }
3689
Linus Walleij8d318a52010-03-30 15:33:42 +02003690 kfree(base->lcla_pool.alloc_map);
3691 kfree(base->lookup_log_chans);
3692 kfree(base->lookup_phy_chans);
3693 kfree(base->phy_res);
3694 kfree(base);
3695 }
3696
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003697 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003698 return ret;
3699}
3700
3701static struct platform_driver d40_driver = {
3702 .driver = {
3703 .owner = THIS_MODULE,
3704 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303705 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003706 },
3707};
3708
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003709static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003710{
3711 return platform_driver_probe(&d40_driver, d40_probe);
3712}
Linus Walleija0eb2212011-05-18 14:18:57 +02003713subsys_initcall(stedma40_init);