blob: 495e8869b9b046e24129847cfc33e573d5afe107 [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>
Lee Jones1814a172013-05-03 15:32:11 +010020#include <linux/of.h>
Linus Walleijf4b89762011-06-27 11:33:46 +020021#include <linux/amba/bus.h>
Linus Walleij15e4b782012-04-12 18:12:43 +020022#include <linux/regulator/consumer.h>
Linus Walleij865fab62012-10-18 14:20:16 +020023#include <linux/platform_data/dma-ste-dma40.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020024
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000025#include "dmaengine.h"
Linus Walleij8d318a52010-03-30 15:33:42 +020026#include "ste_dma40_ll.h"
27
28#define D40_NAME "dma40"
29
30#define D40_PHY_CHAN -1
31
32/* For masking out/in 2 bit channel positions */
33#define D40_CHAN_POS(chan) (2 * (chan / 2))
34#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
35
36/* Maximum iterations taken before giving up suspending a channel */
37#define D40_SUSPEND_MAX_IT 500
38
Narayanan G7fb3e752011-11-17 17:26:41 +053039/* Milliseconds */
40#define DMA40_AUTOSUSPEND_DELAY 100
41
Linus Walleij508849a2010-06-20 21:26:07 +000042/* Hardware requirement on LCLA alignment */
43#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000044
45/* Max number of links per event group */
46#define D40_LCLA_LINK_PER_EVENT_GRP 128
47#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
48
Lee Jonesdb72da92013-05-03 15:32:03 +010049/* Max number of logical channels per physical channel */
50#define D40_MAX_LOG_CHAN_PER_PHY 32
51
Linus Walleij508849a2010-06-20 21:26:07 +000052/* Attempts before giving up to trying to get pages that are aligned */
53#define MAX_LCLA_ALLOC_ATTEMPTS 256
54
55/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020056#define D40_ALLOC_FREE (1 << 31)
57#define D40_ALLOC_PHY (1 << 30)
58#define D40_ALLOC_LOG_FREE 0
59
Lee Jones664a57e2013-05-03 15:31:53 +010060/* Reserved event lines for memcpy only. */
Linus Walleija2acaa22013-05-03 21:46:09 +020061#define DB8500_DMA_MEMCPY_EV_0 51
62#define DB8500_DMA_MEMCPY_EV_1 56
63#define DB8500_DMA_MEMCPY_EV_2 57
64#define DB8500_DMA_MEMCPY_EV_3 58
65#define DB8500_DMA_MEMCPY_EV_4 59
66#define DB8500_DMA_MEMCPY_EV_5 60
67
68static int dma40_memcpy_channels[] = {
69 DB8500_DMA_MEMCPY_EV_0,
70 DB8500_DMA_MEMCPY_EV_1,
71 DB8500_DMA_MEMCPY_EV_2,
72 DB8500_DMA_MEMCPY_EV_3,
73 DB8500_DMA_MEMCPY_EV_4,
74 DB8500_DMA_MEMCPY_EV_5,
75};
Lee Jones664a57e2013-05-03 15:31:53 +010076
Lee Jones29027a12013-05-03 15:31:54 +010077/* Default configuration for physcial memcpy */
78struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
79 .mode = STEDMA40_MODE_PHYSICAL,
80 .dir = STEDMA40_MEM_TO_MEM,
81
82 .src_info.data_width = STEDMA40_BYTE_WIDTH,
83 .src_info.psize = STEDMA40_PSIZE_PHY_1,
84 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
85
86 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
87 .dst_info.psize = STEDMA40_PSIZE_PHY_1,
88 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
89};
90
91/* Default configuration for logical memcpy */
92struct stedma40_chan_cfg dma40_memcpy_conf_log = {
93 .mode = STEDMA40_MODE_LOGICAL,
94 .dir = STEDMA40_MEM_TO_MEM,
95
96 .src_info.data_width = STEDMA40_BYTE_WIDTH,
97 .src_info.psize = STEDMA40_PSIZE_LOG_1,
98 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
99
100 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
101 .dst_info.psize = STEDMA40_PSIZE_LOG_1,
102 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
103};
104
Linus Walleij8d318a52010-03-30 15:33:42 +0200105/**
106 * enum 40_command - The different commands and/or statuses.
107 *
108 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
109 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
110 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
111 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
112 */
113enum d40_command {
114 D40_DMA_STOP = 0,
115 D40_DMA_RUN = 1,
116 D40_DMA_SUSPEND_REQ = 2,
117 D40_DMA_SUSPENDED = 3
118};
119
Narayanan G7fb3e752011-11-17 17:26:41 +0530120/*
Narayanan G1bdae6f2012-02-09 12:41:37 +0530121 * enum d40_events - The different Event Enables for the event lines.
122 *
123 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
124 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
125 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
126 * @D40_ROUND_EVENTLINE: Status check for event line.
127 */
128
129enum d40_events {
130 D40_DEACTIVATE_EVENTLINE = 0,
131 D40_ACTIVATE_EVENTLINE = 1,
132 D40_SUSPEND_REQ_EVENTLINE = 2,
133 D40_ROUND_EVENTLINE = 3
134};
135
136/*
Narayanan G7fb3e752011-11-17 17:26:41 +0530137 * These are the registers that has to be saved and later restored
138 * when the DMA hw is powered off.
139 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
140 */
141static u32 d40_backup_regs[] = {
142 D40_DREG_LCPA,
143 D40_DREG_LCLA,
144 D40_DREG_PRMSE,
145 D40_DREG_PRMSO,
146 D40_DREG_PRMOE,
147 D40_DREG_PRMOO,
148};
149
150#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
151
Tong Liu3cb645d2012-09-26 10:07:30 +0000152/*
153 * since 9540 and 8540 has the same HW revision
154 * use v4a for 9540 or ealier
155 * use v4b for 8540 or later
156 * HW revision:
157 * DB8500ed has revision 0
158 * DB8500v1 has revision 2
159 * DB8500v2 has revision 3
160 * AP9540v1 has revision 4
161 * DB8540v1 has revision 4
162 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
163 */
164static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530165 D40_DREG_PSEG1,
166 D40_DREG_PSEG2,
167 D40_DREG_PSEG3,
168 D40_DREG_PSEG4,
169 D40_DREG_PCEG1,
170 D40_DREG_PCEG2,
171 D40_DREG_PCEG3,
172 D40_DREG_PCEG4,
173 D40_DREG_RSEG1,
174 D40_DREG_RSEG2,
175 D40_DREG_RSEG3,
176 D40_DREG_RSEG4,
177 D40_DREG_RCEG1,
178 D40_DREG_RCEG2,
179 D40_DREG_RCEG3,
180 D40_DREG_RCEG4,
181};
182
Tong Liu3cb645d2012-09-26 10:07:30 +0000183#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
184
185static u32 d40_backup_regs_v4b[] = {
186 D40_DREG_CPSEG1,
187 D40_DREG_CPSEG2,
188 D40_DREG_CPSEG3,
189 D40_DREG_CPSEG4,
190 D40_DREG_CPSEG5,
191 D40_DREG_CPCEG1,
192 D40_DREG_CPCEG2,
193 D40_DREG_CPCEG3,
194 D40_DREG_CPCEG4,
195 D40_DREG_CPCEG5,
196 D40_DREG_CRSEG1,
197 D40_DREG_CRSEG2,
198 D40_DREG_CRSEG3,
199 D40_DREG_CRSEG4,
200 D40_DREG_CRSEG5,
201 D40_DREG_CRCEG1,
202 D40_DREG_CRCEG2,
203 D40_DREG_CRCEG3,
204 D40_DREG_CRCEG4,
205 D40_DREG_CRCEG5,
206};
207
208#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530209
210static u32 d40_backup_regs_chan[] = {
211 D40_CHAN_REG_SSCFG,
212 D40_CHAN_REG_SSELT,
213 D40_CHAN_REG_SSPTR,
214 D40_CHAN_REG_SSLNK,
215 D40_CHAN_REG_SDCFG,
216 D40_CHAN_REG_SDELT,
217 D40_CHAN_REG_SDPTR,
218 D40_CHAN_REG_SDLNK,
219};
220
Lee Jones84b3da12013-05-03 15:31:58 +0100221#define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
222 BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
223
Linus Walleij8d318a52010-03-30 15:33:42 +0200224/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000225 * struct d40_interrupt_lookup - lookup table for interrupt handler
226 *
227 * @src: Interrupt mask register.
228 * @clr: Interrupt clear register.
229 * @is_error: true if this is an error interrupt.
230 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
231 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
232 */
233struct d40_interrupt_lookup {
234 u32 src;
235 u32 clr;
236 bool is_error;
237 int offset;
238};
239
240
241static struct d40_interrupt_lookup il_v4a[] = {
242 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
243 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
244 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
245 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
246 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
247 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
248 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
249 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
250 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
251 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
252};
253
254static struct d40_interrupt_lookup il_v4b[] = {
255 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
256 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
257 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
258 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
259 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
260 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
261 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
262 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
263 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
264 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
265 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
266 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
267};
268
269/**
270 * struct d40_reg_val - simple lookup struct
271 *
272 * @reg: The register.
273 * @val: The value that belongs to the register in reg.
274 */
275struct d40_reg_val {
276 unsigned int reg;
277 unsigned int val;
278};
279
280static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
281 /* Clock every part of the DMA block from start */
282 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
283
284 /* Interrupts on all logical channels */
285 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
286 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
287 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
288 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
289 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
290 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
291 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
292 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
293 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
294 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
295 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
296 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
297};
298static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
299 /* Clock every part of the DMA block from start */
300 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
301
302 /* Interrupts on all logical channels */
303 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
304 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
305 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
306 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
307 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
308 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
309 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
310 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
311 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
312 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
313 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
314 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
315 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
316 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
317 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
318};
319
320/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200321 * struct d40_lli_pool - Structure for keeping LLIs in memory
322 *
323 * @base: Pointer to memory area when the pre_alloc_lli's are not large
324 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
325 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100326 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200327 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
328 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
329 * one buffer to one buffer.
330 */
331struct d40_lli_pool {
332 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000333 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100334 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200335 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000336 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200337};
338
339/**
340 * struct d40_desc - A descriptor is one DMA job.
341 *
342 * @lli_phy: LLI settings for physical channel. Both src and dst=
343 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
344 * lli_len equals one.
345 * @lli_log: Same as above but for logical channels.
346 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000347 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300348 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000349 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200350 * @txd: DMA engine struct. Used for among other things for communication
351 * during a transfer.
352 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200353 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530354 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200355 *
356 * This descriptor is used for both logical and physical transfers.
357 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200358struct d40_desc {
359 /* LLI physical */
360 struct d40_phy_lli_bidir lli_phy;
361 /* LLI logical */
362 struct d40_log_lli_bidir lli_log;
363
364 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000365 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000366 int lli_current;
367 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200368
369 struct dma_async_tx_descriptor txd;
370 struct list_head node;
371
Linus Walleij8d318a52010-03-30 15:33:42 +0200372 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100373 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200374};
375
376/**
377 * struct d40_lcla_pool - LCLA pool settings and data.
378 *
Linus Walleij508849a2010-06-20 21:26:07 +0000379 * @base: The virtual address of LCLA. 18 bit aligned.
380 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
381 * This pointer is only there for clean-up on error.
382 * @pages: The number of pages needed for all physical channels.
383 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200384 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000385 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200386 */
387struct d40_lcla_pool {
388 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100389 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000390 void *base_unaligned;
391 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200392 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000393 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200394};
395
396/**
397 * struct d40_phy_res - struct for handling eventlines mapped to physical
398 * channels.
399 *
400 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530401 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200402 * @num: The physical channel number of this entity.
403 * @allocated_src: Bit mapped to show which src event line's are mapped to
404 * this physical channel. Can also be free or physically allocated.
405 * @allocated_dst: Same as for src but is dst.
406 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000407 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100408 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200409 */
410struct d40_phy_res {
411 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530412 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200413 int num;
414 u32 allocated_src;
415 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100416 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200417};
418
419struct d40_base;
420
421/**
422 * struct d40_chan - Struct that describes a channel.
423 *
424 * @lock: A spinlock to protect this struct.
425 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200426 * @pending_tx: The number of pending transfers. Used between interrupt handler
427 * and tasklet.
428 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000429 * @phy_chan: Pointer to physical channel which this instance runs on. If this
430 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200431 * @chan: DMA engine handle.
432 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
433 * transfer and call client callback.
434 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200435 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200436 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100437 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200438 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200439 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200440 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000441 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200442 * @base: Pointer to the device instance struct.
443 * @src_def_cfg: Default cfg register setting for src.
444 * @dst_def_cfg: Default cfg register setting for dst.
445 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200446 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200447 * @runtime_addr: runtime configured address.
448 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200449 *
450 * This struct can either "be" a logical or a physical channel.
451 */
452struct d40_chan {
453 spinlock_t lock;
454 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200455 int pending_tx;
456 bool busy;
457 struct d40_phy_res *phy_chan;
458 struct dma_chan chan;
459 struct tasklet_struct tasklet;
460 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200461 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200462 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100463 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200464 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200465 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200466 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000467 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200468 struct d40_base *base;
469 /* Default register configurations */
470 u32 src_def_cfg;
471 u32 dst_def_cfg;
472 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200473 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200474 /* Runtime reconfiguration */
475 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530476 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200477};
478
479/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000480 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
481 * controller
482 *
483 * @backup: the pointer to the registers address array for backup
484 * @backup_size: the size of the registers address array for backup
485 * @realtime_en: the realtime enable register
486 * @realtime_clear: the realtime clear register
487 * @high_prio_en: the high priority enable register
488 * @high_prio_clear: the high priority clear register
489 * @interrupt_en: the interrupt enable register
490 * @interrupt_clear: the interrupt clear register
491 * @il: the pointer to struct d40_interrupt_lookup
492 * @il_size: the size of d40_interrupt_lookup array
493 * @init_reg: the pointer to the struct d40_reg_val
494 * @init_reg_size: the size of d40_reg_val array
495 */
496struct d40_gen_dmac {
497 u32 *backup;
498 u32 backup_size;
499 u32 realtime_en;
500 u32 realtime_clear;
501 u32 high_prio_en;
502 u32 high_prio_clear;
503 u32 interrupt_en;
504 u32 interrupt_clear;
505 struct d40_interrupt_lookup *il;
506 u32 il_size;
507 struct d40_reg_val *init_reg;
508 u32 init_reg_size;
509};
510
511/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200512 * struct d40_base - The big global struct, one for each probe'd instance.
513 *
514 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
515 * @execmd_lock: Lock for execute command usage since several channels share
516 * the same physical register.
517 * @dev: The device structure.
518 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700519 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200520 * @clk: Pointer to the DMA clock structure.
521 * @phy_start: Physical memory start of the DMA registers.
522 * @phy_size: Size of the DMA register map.
523 * @irq: The IRQ number.
524 * @num_phy_chans: The number of physical channels. Read from HW. This
525 * is the number of available channels for this driver, not counting "Secure
526 * mode" allocated physical channels.
527 * @num_log_chans: The number of logical channels. Calculated from
528 * num_phy_chans.
529 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
530 * @dma_slave: dma_device channels that can do only do slave transfers.
531 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530532 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200533 * @log_chans: Room for all possible logical channels in system.
534 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
535 * to log_chans entries.
536 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
537 * to phy_chans entries.
538 * @plat_data: Pointer to provided platform_data which is the driver
539 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530540 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200541 * @phy_res: Vector containing all physical channels.
542 * @lcla_pool: lcla pool settings and data.
543 * @lcpa_base: The virtual mapped address of LCPA.
544 * @phy_lcpa: The physical address of the LCPA.
545 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000546 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530547 * @reg_val_backup: Here the values of some hardware registers are stored
548 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000549 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
550 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530551 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
552 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
553 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000554 * @gen_dmac: the struct for generic registers values to represent u8500/8540
555 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200556 */
557struct d40_base {
558 spinlock_t interrupt_lock;
559 spinlock_t execmd_lock;
560 struct device *dev;
561 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700562 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200563 struct clk *clk;
564 phys_addr_t phy_start;
565 resource_size_t phy_size;
566 int irq;
567 int num_phy_chans;
568 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200569 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200570 struct dma_device dma_both;
571 struct dma_device dma_slave;
572 struct dma_device dma_memcpy;
573 struct d40_chan *phy_chans;
574 struct d40_chan *log_chans;
575 struct d40_chan **lookup_log_chans;
576 struct d40_chan **lookup_phy_chans;
577 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530578 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200579 /* Physical half channels */
580 struct d40_phy_res *phy_res;
581 struct d40_lcla_pool lcla_pool;
582 void *lcpa_base;
583 dma_addr_t phy_lcpa;
584 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000585 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530586 u32 reg_val_backup[BACKUP_REGS_SZ];
Lee Jones84b3da12013-05-03 15:31:58 +0100587 u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
Narayanan G7fb3e752011-11-17 17:26:41 +0530588 u32 *reg_val_backup_chan;
589 u16 gcc_pwr_off_mask;
590 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000591 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200592};
593
Rabin Vincent262d2912011-01-25 11:18:05 +0100594static struct device *chan2dev(struct d40_chan *d40c)
595{
596 return &d40c->chan.dev->device;
597}
598
Rabin Vincent724a8572011-01-25 11:18:08 +0100599static bool chan_is_physical(struct d40_chan *chan)
600{
601 return chan->log_num == D40_PHY_CHAN;
602}
603
604static bool chan_is_logical(struct d40_chan *chan)
605{
606 return !chan_is_physical(chan);
607}
608
Rabin Vincent8ca84682011-01-25 11:18:07 +0100609static void __iomem *chan_base(struct d40_chan *chan)
610{
611 return chan->base->virtbase + D40_DREG_PCBASE +
612 chan->phy_chan->num * D40_DREG_PCDELTA;
613}
614
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100615#define d40_err(dev, format, arg...) \
616 dev_err(dev, "[%s] " format, __func__, ## arg)
617
618#define chan_err(d40c, format, arg...) \
619 d40_err(chan2dev(d40c), format, ## arg)
620
Rabin Vincentb00f9382011-01-25 11:18:15 +0100621static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100622 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200623{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100624 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200625 u32 align;
626 void *base;
627
628 if (is_log)
629 align = sizeof(struct d40_log_lli);
630 else
631 align = sizeof(struct d40_phy_lli);
632
633 if (lli_len == 1) {
634 base = d40d->lli_pool.pre_alloc_lli;
635 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
636 d40d->lli_pool.base = NULL;
637 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100638 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200639
640 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
641 d40d->lli_pool.base = base;
642
643 if (d40d->lli_pool.base == NULL)
644 return -ENOMEM;
645 }
646
647 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100648 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100649 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100650
651 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200652 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100653 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100654 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100655
656 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
657 d40d->lli_phy.src,
658 d40d->lli_pool.size,
659 DMA_TO_DEVICE);
660
661 if (dma_mapping_error(d40c->base->dev,
662 d40d->lli_pool.dma_addr)) {
663 kfree(d40d->lli_pool.base);
664 d40d->lli_pool.base = NULL;
665 d40d->lli_pool.dma_addr = 0;
666 return -ENOMEM;
667 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200668 }
669
670 return 0;
671}
672
Rabin Vincentb00f9382011-01-25 11:18:15 +0100673static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200674{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100675 if (d40d->lli_pool.dma_addr)
676 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
677 d40d->lli_pool.size, DMA_TO_DEVICE);
678
Linus Walleij8d318a52010-03-30 15:33:42 +0200679 kfree(d40d->lli_pool.base);
680 d40d->lli_pool.base = NULL;
681 d40d->lli_pool.size = 0;
682 d40d->lli_log.src = NULL;
683 d40d->lli_log.dst = NULL;
684 d40d->lli_phy.src = NULL;
685 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200686}
687
Jonas Aaberg698e4732010-08-09 12:08:56 +0000688static int d40_lcla_alloc_one(struct d40_chan *d40c,
689 struct d40_desc *d40d)
690{
691 unsigned long flags;
692 int i;
693 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000694
695 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
696
Jonas Aaberg698e4732010-08-09 12:08:56 +0000697 /*
698 * Allocate both src and dst at the same time, therefore the half
699 * start on 1 since 0 can't be used since zero is used as end marker.
700 */
701 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100702 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
703
704 if (!d40c->base->lcla_pool.alloc_map[idx]) {
705 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000706 d40d->lcla_alloc++;
707 ret = i;
708 break;
709 }
710 }
711
712 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
713
714 return ret;
715}
716
717static int d40_lcla_free_all(struct d40_chan *d40c,
718 struct d40_desc *d40d)
719{
720 unsigned long flags;
721 int i;
722 int ret = -EINVAL;
723
Rabin Vincent724a8572011-01-25 11:18:08 +0100724 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000725 return 0;
726
727 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
728
729 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100730 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
731
732 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
733 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000734 d40d->lcla_alloc--;
735 if (d40d->lcla_alloc == 0) {
736 ret = 0;
737 break;
738 }
739 }
740 }
741
742 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
743
744 return ret;
745
746}
747
Linus Walleij8d318a52010-03-30 15:33:42 +0200748static void d40_desc_remove(struct d40_desc *d40d)
749{
750 list_del(&d40d->node);
751}
752
753static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
754{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000755 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200756
757 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000758 struct d40_desc *d;
759 struct d40_desc *_d;
760
Narayanan G7fb3e752011-11-17 17:26:41 +0530761 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200762 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200763 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000764 desc = d;
765 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000766 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200767 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530768 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200769 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000770
771 if (!desc)
772 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
773
774 if (desc)
775 INIT_LIST_HEAD(&desc->node);
776
777 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200778}
779
780static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
781{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000782
Rabin Vincentb00f9382011-01-25 11:18:15 +0100783 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000784 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000785 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200786}
787
788static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
789{
790 list_add_tail(&desc->node, &d40c->active);
791}
792
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100793static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
794{
795 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
796 struct d40_phy_lli *lli_src = desc->lli_phy.src;
797 void __iomem *base = chan_base(chan);
798
799 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
800 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
801 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
802 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
803
804 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
805 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
806 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
807 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
808}
809
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100810static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
811{
812 list_add_tail(&desc->node, &d40c->done);
813}
814
Rabin Vincente65889c2011-01-25 11:18:31 +0100815static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
816{
817 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
818 struct d40_log_lli_bidir *lli = &desc->lli_log;
819 int lli_current = desc->lli_current;
820 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100821 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100822 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100823 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530824 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100825 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100826
Rabin Vincent0c842b52011-01-25 11:18:35 +0100827 /*
828 * We may have partially running cyclic transfers, in case we did't get
829 * enough LCLA entries.
830 */
831 linkback = cyclic && lli_current == 0;
832
833 /*
834 * For linkback, we need one LCLA even with only one link, because we
835 * can't link back to the one in LCPA space
836 */
837 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100838 /*
839 * If the channel is expected to use only soft_lli don't
840 * allocate a lcla. This is to avoid a HW issue that exists
841 * in some controller during a peripheral to memory transfer
842 * that uses linked lists.
843 */
844 if (!(chan->phy_chan->use_soft_lli &&
845 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
846 curr_lcla = d40_lcla_alloc_one(chan, desc);
847
Rabin Vincent0c842b52011-01-25 11:18:35 +0100848 first_lcla = curr_lcla;
849 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100850
Rabin Vincent0c842b52011-01-25 11:18:35 +0100851 /*
852 * For linkback, we normally load the LCPA in the loop since we need to
853 * link it to the second LCLA and not the first. However, if we
854 * couldn't even get a first LCLA, then we have to run in LCPA and
855 * reload manually.
856 */
857 if (!linkback || curr_lcla == -EINVAL) {
858 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100859
Rabin Vincent0c842b52011-01-25 11:18:35 +0100860 if (curr_lcla == -EINVAL)
861 flags |= LLI_TERM_INT;
862
863 d40_log_lli_lcpa_write(chan->lcpa,
864 &lli->dst[lli_current],
865 &lli->src[lli_current],
866 curr_lcla,
867 flags);
868 lli_current++;
869 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100870
871 if (curr_lcla < 0)
872 goto out;
873
Rabin Vincente65889c2011-01-25 11:18:31 +0100874 for (; lli_current < lli_len; lli_current++) {
875 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
876 8 * curr_lcla * 2;
877 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100878 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100879 int next_lcla;
880
881 if (lli_current + 1 < lli_len)
882 next_lcla = d40_lcla_alloc_one(chan, desc);
883 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100884 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100885
Rabin Vincent0c842b52011-01-25 11:18:35 +0100886 if (cyclic || next_lcla == -EINVAL)
887 flags |= LLI_TERM_INT;
888
889 if (linkback && curr_lcla == first_lcla) {
890 /* First link goes in both LCPA and LCLA */
891 d40_log_lli_lcpa_write(chan->lcpa,
892 &lli->dst[lli_current],
893 &lli->src[lli_current],
894 next_lcla, flags);
895 }
896
897 /*
898 * One unused LCLA in the cyclic case if the very first
899 * next_lcla fails...
900 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100901 d40_log_lli_lcla_write(lcla,
902 &lli->dst[lli_current],
903 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100904 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100905
Narayanan G28c7a192011-11-22 13:56:55 +0530906 /*
907 * Cache maintenance is not needed if lcla is
908 * mapped in esram
909 */
910 if (!use_esram_lcla) {
911 dma_sync_single_range_for_device(chan->base->dev,
912 pool->dma_addr, lcla_offset,
913 2 * sizeof(struct d40_log_lli),
914 DMA_TO_DEVICE);
915 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100916 curr_lcla = next_lcla;
917
Rabin Vincent0c842b52011-01-25 11:18:35 +0100918 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100919 lli_current++;
920 break;
921 }
922 }
923
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100924out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100925 desc->lli_current = lli_current;
926}
927
Jonas Aaberg698e4732010-08-09 12:08:56 +0000928static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
929{
Rabin Vincent724a8572011-01-25 11:18:08 +0100930 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100931 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000932 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100933 } else
934 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000935}
936
Linus Walleij8d318a52010-03-30 15:33:42 +0200937static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
938{
939 struct d40_desc *d;
940
941 if (list_empty(&d40c->active))
942 return NULL;
943
944 d = list_first_entry(&d40c->active,
945 struct d40_desc,
946 node);
947 return d;
948}
949
Per Forlin74043682011-08-29 13:33:34 +0200950/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200951static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
952{
Per Forlin74043682011-08-29 13:33:34 +0200953 d40_desc_remove(desc);
954 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200955 list_add_tail(&desc->node, &d40c->pending_queue);
956}
957
958static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
959{
960 struct d40_desc *d;
961
962 if (list_empty(&d40c->pending_queue))
963 return NULL;
964
965 d = list_first_entry(&d40c->pending_queue,
966 struct d40_desc,
967 node);
968 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200969}
970
971static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
972{
973 struct d40_desc *d;
974
975 if (list_empty(&d40c->queue))
976 return NULL;
977
978 d = list_first_entry(&d40c->queue,
979 struct d40_desc,
980 node);
981 return d;
982}
983
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100984static struct d40_desc *d40_first_done(struct d40_chan *d40c)
985{
986 if (list_empty(&d40c->done))
987 return NULL;
988
989 return list_first_entry(&d40c->done, struct d40_desc, node);
990}
991
Per Forlind49278e2010-12-20 18:31:38 +0100992static int d40_psize_2_burst_size(bool is_log, int psize)
993{
994 if (is_log) {
995 if (psize == STEDMA40_PSIZE_LOG_1)
996 return 1;
997 } else {
998 if (psize == STEDMA40_PSIZE_PHY_1)
999 return 1;
1000 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001001
Per Forlind49278e2010-12-20 18:31:38 +01001002 return 2 << psize;
1003}
1004
1005/*
1006 * The dma only supports transmitting packages up to
1007 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
1008 * dma elements required to send the entire sg list
1009 */
1010static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1011{
1012 int dmalen;
1013 u32 max_w = max(data_width1, data_width2);
1014 u32 min_w = min(data_width1, data_width2);
1015 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
1016
1017 if (seg_max > STEDMA40_MAX_SEG_SIZE)
1018 seg_max -= (1 << max_w);
1019
1020 if (!IS_ALIGNED(size, 1 << max_w))
1021 return -EINVAL;
1022
1023 if (size <= seg_max)
1024 dmalen = 1;
1025 else {
1026 dmalen = size / seg_max;
1027 if (dmalen * seg_max < size)
1028 dmalen++;
1029 }
1030 return dmalen;
1031}
1032
1033static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1034 u32 data_width1, u32 data_width2)
1035{
1036 struct scatterlist *sg;
1037 int i;
1038 int len = 0;
1039 int ret;
1040
1041 for_each_sg(sgl, sg, sg_len, i) {
1042 ret = d40_size_2_dmalen(sg_dma_len(sg),
1043 data_width1, data_width2);
1044 if (ret < 0)
1045 return ret;
1046 len += ret;
1047 }
1048 return len;
1049}
1050
Narayanan G7fb3e752011-11-17 17:26:41 +05301051
1052#ifdef CONFIG_PM
1053static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1054 u32 *regaddr, int num, bool save)
1055{
1056 int i;
1057
1058 for (i = 0; i < num; i++) {
1059 void __iomem *addr = baseaddr + regaddr[i];
1060
1061 if (save)
1062 backup[i] = readl_relaxed(addr);
1063 else
1064 writel_relaxed(backup[i], addr);
1065 }
1066}
1067
1068static void d40_save_restore_registers(struct d40_base *base, bool save)
1069{
1070 int i;
1071
1072 /* Save/Restore channel specific registers */
1073 for (i = 0; i < base->num_phy_chans; i++) {
1074 void __iomem *addr;
1075 int idx;
1076
1077 if (base->phy_res[i].reserved)
1078 continue;
1079
1080 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1081 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1082
1083 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1084 d40_backup_regs_chan,
1085 ARRAY_SIZE(d40_backup_regs_chan),
1086 save);
1087 }
1088
1089 /* Save/Restore global registers */
1090 dma40_backup(base->virtbase, base->reg_val_backup,
1091 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1092 save);
1093
1094 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001095 if (base->gen_dmac.backup)
1096 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1097 base->gen_dmac.backup,
1098 base->gen_dmac.backup_size,
1099 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301100}
1101#else
1102static void d40_save_restore_registers(struct d40_base *base, bool save)
1103{
1104}
1105#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001106
Narayanan G1bdae6f2012-02-09 12:41:37 +05301107static int __d40_execute_command_phy(struct d40_chan *d40c,
1108 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001109{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001110 u32 status;
1111 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001112 void __iomem *active_reg;
1113 int ret = 0;
1114 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001115 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001116
Narayanan G1bdae6f2012-02-09 12:41:37 +05301117 if (command == D40_DMA_STOP) {
1118 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1119 if (ret)
1120 return ret;
1121 }
1122
Linus Walleij8d318a52010-03-30 15:33:42 +02001123 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1124
1125 if (d40c->phy_chan->num % 2 == 0)
1126 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1127 else
1128 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1129
1130 if (command == D40_DMA_SUSPEND_REQ) {
1131 status = (readl(active_reg) &
1132 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1133 D40_CHAN_POS(d40c->phy_chan->num);
1134
1135 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1136 goto done;
1137 }
1138
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001139 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1140 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1141 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001142
1143 if (command == D40_DMA_SUSPEND_REQ) {
1144
1145 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1146 status = (readl(active_reg) &
1147 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1148 D40_CHAN_POS(d40c->phy_chan->num);
1149
1150 cpu_relax();
1151 /*
1152 * Reduce the number of bus accesses while
1153 * waiting for the DMA to suspend.
1154 */
1155 udelay(3);
1156
1157 if (status == D40_DMA_STOP ||
1158 status == D40_DMA_SUSPENDED)
1159 break;
1160 }
1161
1162 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001163 chan_err(d40c,
1164 "unable to suspend the chl %d (log: %d) status %x\n",
1165 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001166 status);
1167 dump_stack();
1168 ret = -EBUSY;
1169 }
1170
1171 }
1172done:
1173 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1174 return ret;
1175}
1176
1177static void d40_term_all(struct d40_chan *d40c)
1178{
1179 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001180 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001181
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001182 /* Release completed descriptors */
1183 while ((d40d = d40_first_done(d40c))) {
1184 d40_desc_remove(d40d);
1185 d40_desc_free(d40c, d40d);
1186 }
1187
Linus Walleij8d318a52010-03-30 15:33:42 +02001188 /* Release active descriptors */
1189 while ((d40d = d40_first_active_get(d40c))) {
1190 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001191 d40_desc_free(d40c, d40d);
1192 }
1193
1194 /* Release queued descriptors waiting for transfer */
1195 while ((d40d = d40_first_queued(d40c))) {
1196 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001197 d40_desc_free(d40c, d40d);
1198 }
1199
Per Forlina8f30672011-06-26 23:29:52 +02001200 /* Release pending descriptors */
1201 while ((d40d = d40_first_pending(d40c))) {
1202 d40_desc_remove(d40d);
1203 d40_desc_free(d40c, d40d);
1204 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001205
Per Forlin74043682011-08-29 13:33:34 +02001206 /* Release client owned descriptors */
1207 if (!list_empty(&d40c->client))
1208 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1209 d40_desc_remove(d40d);
1210 d40_desc_free(d40c, d40d);
1211 }
1212
Per Forlin82babbb362011-08-29 13:33:35 +02001213 /* Release descriptors in prepare queue */
1214 if (!list_empty(&d40c->prepare_queue))
1215 list_for_each_entry_safe(d40d, _d,
1216 &d40c->prepare_queue, node) {
1217 d40_desc_remove(d40d);
1218 d40_desc_free(d40c, d40d);
1219 }
Per Forlin74043682011-08-29 13:33:34 +02001220
Linus Walleij8d318a52010-03-30 15:33:42 +02001221 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001222}
1223
Narayanan G1bdae6f2012-02-09 12:41:37 +05301224static void __d40_config_set_event(struct d40_chan *d40c,
1225 enum d40_events event_type, u32 event,
1226 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001227{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001228 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001229 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301230 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001231
Narayanan G1bdae6f2012-02-09 12:41:37 +05301232 switch (event_type) {
1233
1234 case D40_DEACTIVATE_EVENTLINE:
1235
Rabin Vincent262d2912011-01-25 11:18:05 +01001236 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1237 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301238 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001239
Narayanan G1bdae6f2012-02-09 12:41:37 +05301240 case D40_SUSPEND_REQ_EVENTLINE:
1241 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1242 D40_EVENTLINE_POS(event);
1243
1244 if (status == D40_DEACTIVATE_EVENTLINE ||
1245 status == D40_SUSPEND_REQ_EVENTLINE)
1246 break;
1247
1248 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1249 | ~D40_EVENTLINE_MASK(event), addr);
1250
1251 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1252
1253 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1254 D40_EVENTLINE_POS(event);
1255
1256 cpu_relax();
1257 /*
1258 * Reduce the number of bus accesses while
1259 * waiting for the DMA to suspend.
1260 */
1261 udelay(3);
1262
1263 if (status == D40_DEACTIVATE_EVENTLINE)
1264 break;
1265 }
1266
1267 if (tries == D40_SUSPEND_MAX_IT) {
1268 chan_err(d40c,
1269 "unable to stop the event_line chl %d (log: %d)"
1270 "status %x\n", d40c->phy_chan->num,
1271 d40c->log_num, status);
1272 }
1273 break;
1274
1275 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001276 /*
1277 * The hardware sometimes doesn't register the enable when src and dst
1278 * event lines are active on the same logical channel. Retry to ensure
1279 * it does. Usually only one retry is sufficient.
1280 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301281 tries = 100;
1282 while (--tries) {
1283 writel((D40_ACTIVATE_EVENTLINE <<
1284 D40_EVENTLINE_POS(event)) |
1285 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001286
Narayanan G1bdae6f2012-02-09 12:41:37 +05301287 if (readl(addr) & D40_EVENTLINE_MASK(event))
1288 break;
1289 }
1290
1291 if (tries != 99)
1292 dev_dbg(chan2dev(d40c),
1293 "[%s] workaround enable S%cLNK (%d tries)\n",
1294 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1295 100 - tries);
1296
1297 WARN_ON(!tries);
1298 break;
1299
1300 case D40_ROUND_EVENTLINE:
1301 BUG();
1302 break;
1303
Rabin Vincent262d2912011-01-25 11:18:05 +01001304 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001305}
1306
Narayanan G1bdae6f2012-02-09 12:41:37 +05301307static void d40_config_set_event(struct d40_chan *d40c,
1308 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001309{
Lee Jones26955c07d2013-05-03 15:31:56 +01001310 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
1311
Linus Walleij8d318a52010-03-30 15:33:42 +02001312 /* Enable event line connected to device (or memcpy) */
1313 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
Lee Jones26955c07d2013-05-03 15:31:56 +01001314 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Narayanan G1bdae6f2012-02-09 12:41:37 +05301315 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001316 D40_CHAN_REG_SSLNK);
Rabin Vincent262d2912011-01-25 11:18:05 +01001317
Lee Jones26955c07d2013-05-03 15:31:56 +01001318 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM)
Narayanan G1bdae6f2012-02-09 12:41:37 +05301319 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001320 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001321}
1322
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001323static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001324{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001325 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001326 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001327
Rabin Vincent8ca84682011-01-25 11:18:07 +01001328 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1329 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001330
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001331 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001332}
1333
Narayanan G1bdae6f2012-02-09 12:41:37 +05301334static int
1335__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1336{
1337 unsigned long flags;
1338 int ret = 0;
1339 u32 active_status;
1340 void __iomem *active_reg;
1341
1342 if (d40c->phy_chan->num % 2 == 0)
1343 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1344 else
1345 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1346
1347
1348 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1349
1350 switch (command) {
1351 case D40_DMA_STOP:
1352 case D40_DMA_SUSPEND_REQ:
1353
1354 active_status = (readl(active_reg) &
1355 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1356 D40_CHAN_POS(d40c->phy_chan->num);
1357
1358 if (active_status == D40_DMA_RUN)
1359 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1360 else
1361 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1362
1363 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1364 ret = __d40_execute_command_phy(d40c, command);
1365
1366 break;
1367
1368 case D40_DMA_RUN:
1369
1370 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1371 ret = __d40_execute_command_phy(d40c, command);
1372 break;
1373
1374 case D40_DMA_SUSPENDED:
1375 BUG();
1376 break;
1377 }
1378
1379 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1380 return ret;
1381}
1382
1383static int d40_channel_execute_command(struct d40_chan *d40c,
1384 enum d40_command command)
1385{
1386 if (chan_is_logical(d40c))
1387 return __d40_execute_command_log(d40c, command);
1388 else
1389 return __d40_execute_command_phy(d40c, command);
1390}
1391
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001392static u32 d40_get_prmo(struct d40_chan *d40c)
1393{
1394 static const unsigned int phy_map[] = {
1395 [STEDMA40_PCHAN_BASIC_MODE]
1396 = D40_DREG_PRMO_PCHAN_BASIC,
1397 [STEDMA40_PCHAN_MODULO_MODE]
1398 = D40_DREG_PRMO_PCHAN_MODULO,
1399 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1400 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1401 };
1402 static const unsigned int log_map[] = {
1403 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1404 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1405 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1406 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1407 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1408 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1409 };
1410
Rabin Vincent724a8572011-01-25 11:18:08 +01001411 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001412 return phy_map[d40c->dma_cfg.mode_opt];
1413 else
1414 return log_map[d40c->dma_cfg.mode_opt];
1415}
1416
Jonas Aabergb55912c2010-08-09 12:08:02 +00001417static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001418{
1419 u32 addr_base;
1420 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001421
1422 /* Odd addresses are even addresses + 4 */
1423 addr_base = (d40c->phy_chan->num % 2) * 4;
1424 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001425 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001426 D40_CHAN_POS(d40c->phy_chan->num);
1427 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1428
1429 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001430 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001431
1432 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1433
Rabin Vincent724a8572011-01-25 11:18:08 +01001434 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001435 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1436 & D40_SREG_ELEM_LOG_LIDX_MASK;
1437 void __iomem *chanbase = chan_base(d40c);
1438
Linus Walleij8d318a52010-03-30 15:33:42 +02001439 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001440 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1441 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001442
Jonas Aabergb55912c2010-08-09 12:08:02 +00001443 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001444 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1445 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301446
1447 /* Clear LNK which will be used by d40_chan_has_events() */
1448 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1449 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001450 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001451}
1452
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001453static u32 d40_residue(struct d40_chan *d40c)
1454{
1455 u32 num_elt;
1456
Rabin Vincent724a8572011-01-25 11:18:08 +01001457 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001458 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1459 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001460 else {
1461 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1462 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1463 >> D40_SREG_ELEM_PHY_ECNT_POS;
1464 }
1465
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001466 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1467}
1468
1469static bool d40_tx_is_linked(struct d40_chan *d40c)
1470{
1471 bool is_link;
1472
Rabin Vincent724a8572011-01-25 11:18:08 +01001473 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001474 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1475 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001476 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1477 & D40_SREG_LNK_PHYS_LNK_MASK;
1478
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001479 return is_link;
1480}
1481
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001482static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001483{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001484 int res = 0;
1485 unsigned long flags;
1486
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001487 if (!d40c->busy)
1488 return 0;
1489
Narayanan G7fb3e752011-11-17 17:26:41 +05301490 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001491 spin_lock_irqsave(&d40c->lock, flags);
1492
1493 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301494
Narayanan G7fb3e752011-11-17 17:26:41 +05301495 pm_runtime_mark_last_busy(d40c->base->dev);
1496 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001497 spin_unlock_irqrestore(&d40c->lock, flags);
1498 return res;
1499}
1500
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001501static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001502{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001503 int res = 0;
1504 unsigned long flags;
1505
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001506 if (!d40c->busy)
1507 return 0;
1508
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001509 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301510 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001511
1512 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301513 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001514 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001515
Narayanan G7fb3e752011-11-17 17:26:41 +05301516 pm_runtime_mark_last_busy(d40c->base->dev);
1517 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001518 spin_unlock_irqrestore(&d40c->lock, flags);
1519 return res;
1520}
1521
Linus Walleij8d318a52010-03-30 15:33:42 +02001522static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1523{
1524 struct d40_chan *d40c = container_of(tx->chan,
1525 struct d40_chan,
1526 chan);
1527 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1528 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001529 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001530
1531 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001532 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001533 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001534 spin_unlock_irqrestore(&d40c->lock, flags);
1535
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001536 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001537}
1538
1539static int d40_start(struct d40_chan *d40c)
1540{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001541 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001542}
1543
1544static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1545{
1546 struct d40_desc *d40d;
1547 int err;
1548
1549 /* Start queued jobs, if any */
1550 d40d = d40_first_queued(d40c);
1551
1552 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301553 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301554 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301555 pm_runtime_get_sync(d40c->base->dev);
1556 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001557
1558 /* Remove from queue */
1559 d40_desc_remove(d40d);
1560
1561 /* Add to active queue */
1562 d40_desc_submit(d40c, d40d);
1563
Rabin Vincent7d83a852011-01-25 11:18:06 +01001564 /* Initiate DMA job */
1565 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001566
Rabin Vincent7d83a852011-01-25 11:18:06 +01001567 /* Start dma job */
1568 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001569
Rabin Vincent7d83a852011-01-25 11:18:06 +01001570 if (err)
1571 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001572 }
1573
1574 return d40d;
1575}
1576
1577/* called from interrupt context */
1578static void dma_tc_handle(struct d40_chan *d40c)
1579{
1580 struct d40_desc *d40d;
1581
Linus Walleij8d318a52010-03-30 15:33:42 +02001582 /* Get first active entry from list */
1583 d40d = d40_first_active_get(d40c);
1584
1585 if (d40d == NULL)
1586 return;
1587
Rabin Vincent0c842b52011-01-25 11:18:35 +01001588 if (d40d->cyclic) {
1589 /*
1590 * If this was a paritially loaded list, we need to reloaded
1591 * it, and only when the list is completed. We need to check
1592 * for done because the interrupt will hit for every link, and
1593 * not just the last one.
1594 */
1595 if (d40d->lli_current < d40d->lli_len
1596 && !d40_tx_is_linked(d40c)
1597 && !d40_residue(d40c)) {
1598 d40_lcla_free_all(d40c, d40d);
1599 d40_desc_load(d40c, d40d);
1600 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001601
Rabin Vincent0c842b52011-01-25 11:18:35 +01001602 if (d40d->lli_current == d40d->lli_len)
1603 d40d->lli_current = 0;
1604 }
1605 } else {
1606 d40_lcla_free_all(d40c, d40d);
1607
1608 if (d40d->lli_current < d40d->lli_len) {
1609 d40_desc_load(d40c, d40d);
1610 /* Start dma job */
1611 (void) d40_start(d40c);
1612 return;
1613 }
1614
1615 if (d40_queue_start(d40c) == NULL)
1616 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301617 pm_runtime_mark_last_busy(d40c->base->dev);
1618 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001619
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001620 d40_desc_remove(d40d);
1621 d40_desc_done(d40c, d40d);
1622 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001623
Linus Walleij8d318a52010-03-30 15:33:42 +02001624 d40c->pending_tx++;
1625 tasklet_schedule(&d40c->tasklet);
1626
1627}
1628
1629static void dma_tasklet(unsigned long data)
1630{
1631 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001632 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001633 unsigned long flags;
1634 dma_async_tx_callback callback;
1635 void *callback_param;
1636
1637 spin_lock_irqsave(&d40c->lock, flags);
1638
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001639 /* Get first entry from the done list */
1640 d40d = d40_first_done(d40c);
1641 if (d40d == NULL) {
1642 /* Check if we have reached here for cyclic job */
1643 d40d = d40_first_active_get(d40c);
1644 if (d40d == NULL || !d40d->cyclic)
1645 goto err;
1646 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001647
Rabin Vincent0c842b52011-01-25 11:18:35 +01001648 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001649 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001650
1651 /*
1652 * If terminating a channel pending_tx is set to zero.
1653 * This prevents any finished active jobs to return to the client.
1654 */
1655 if (d40c->pending_tx == 0) {
1656 spin_unlock_irqrestore(&d40c->lock, flags);
1657 return;
1658 }
1659
1660 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001661 callback = d40d->txd.callback;
1662 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001663
Rabin Vincent0c842b52011-01-25 11:18:35 +01001664 if (!d40d->cyclic) {
1665 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001666 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001667 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001668 } else if (!d40d->is_in_client_list) {
1669 d40_desc_remove(d40d);
1670 d40_lcla_free_all(d40c, d40d);
1671 list_add_tail(&d40d->node, &d40c->client);
1672 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001673 }
1674 }
1675
1676 d40c->pending_tx--;
1677
1678 if (d40c->pending_tx)
1679 tasklet_schedule(&d40c->tasklet);
1680
1681 spin_unlock_irqrestore(&d40c->lock, flags);
1682
Jonas Aaberg767a9672010-08-09 12:08:34 +00001683 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001684 callback(callback_param);
1685
1686 return;
1687
Narayanan G1bdae6f2012-02-09 12:41:37 +05301688err:
1689 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001690 if (d40c->pending_tx > 0)
1691 d40c->pending_tx--;
1692 spin_unlock_irqrestore(&d40c->lock, flags);
1693}
1694
1695static irqreturn_t d40_handle_interrupt(int irq, void *data)
1696{
Linus Walleij8d318a52010-03-30 15:33:42 +02001697 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001698 u32 idx;
1699 u32 row;
1700 long chan = -1;
1701 struct d40_chan *d40c;
1702 unsigned long flags;
1703 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001704 u32 regs[base->gen_dmac.il_size];
1705 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1706 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001707
1708 spin_lock_irqsave(&base->interrupt_lock, flags);
1709
1710 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001711 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001712 regs[i] = readl(base->virtbase + il[i].src);
1713
1714 for (;;) {
1715
1716 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001717 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001718
1719 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001720 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001721 break;
1722
1723 row = chan / BITS_PER_LONG;
1724 idx = chan & (BITS_PER_LONG - 1);
1725
Linus Walleij8d318a52010-03-30 15:33:42 +02001726 if (il[row].offset == D40_PHY_CHAN)
1727 d40c = base->lookup_phy_chans[idx];
1728 else
1729 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001730
1731 if (!d40c) {
1732 /*
1733 * No error because this can happen if something else
1734 * in the system is using the channel.
1735 */
1736 continue;
1737 }
1738
1739 /* ACK interrupt */
1740 writel(1 << idx, base->virtbase + il[row].clr);
1741
Linus Walleij8d318a52010-03-30 15:33:42 +02001742 spin_lock(&d40c->lock);
1743
1744 if (!il[row].is_error)
1745 dma_tc_handle(d40c);
1746 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001747 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1748 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001749
1750 spin_unlock(&d40c->lock);
1751 }
1752
1753 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1754
1755 return IRQ_HANDLED;
1756}
1757
Linus Walleij8d318a52010-03-30 15:33:42 +02001758static int d40_validate_conf(struct d40_chan *d40c,
1759 struct stedma40_chan_cfg *conf)
1760{
1761 int res = 0;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001762 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001763
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001764 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001765 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001766 res = -EINVAL;
1767 }
1768
Lee Jones26955c07d2013-05-03 15:31:56 +01001769 if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
1770 (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
1771 (conf->dev_type < 0)) {
1772 chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001773 res = -EINVAL;
1774 }
1775
1776 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Lee Jones26955c07d2013-05-03 15:31:56 +01001777 d40c->base->plat_data->dev_tx[conf->dev_type] == 0 &&
1778 d40c->runtime_addr == 0) {
1779 chan_err(d40c, "Invalid TX channel address (%d)\n",
1780 conf->dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001781 res = -EINVAL;
1782 }
1783
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001784 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Lee Jones26955c07d2013-05-03 15:31:56 +01001785 d40c->base->plat_data->dev_rx[conf->dev_type] == 0 &&
1786 d40c->runtime_addr == 0) {
1787 chan_err(d40c, "Invalid RX channel address (%d)\n",
1788 conf->dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001789 res = -EINVAL;
1790 }
1791
1792 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1793 /*
1794 * DMAC HW supports it. Will be added to this driver,
1795 * in case any dma client requires it.
1796 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001797 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001798 res = -EINVAL;
1799 }
1800
Per Forlind49278e2010-12-20 18:31:38 +01001801 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1802 (1 << conf->src_info.data_width) !=
1803 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1804 (1 << conf->dst_info.data_width)) {
1805 /*
1806 * The DMAC hardware only supports
1807 * src (burst x width) == dst (burst x width)
1808 */
1809
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001810 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001811 res = -EINVAL;
1812 }
1813
Linus Walleij8d318a52010-03-30 15:33:42 +02001814 return res;
1815}
1816
Narayanan G5cd326f2011-11-30 19:20:42 +05301817static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1818 bool is_src, int log_event_line, bool is_log,
1819 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001820{
1821 unsigned long flags;
1822 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301823
1824 *first_user = ((phy->allocated_src | phy->allocated_dst)
1825 == D40_ALLOC_FREE);
1826
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001827 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001828 /* Physical interrupts are masked per physical full channel */
1829 if (phy->allocated_src == D40_ALLOC_FREE &&
1830 phy->allocated_dst == D40_ALLOC_FREE) {
1831 phy->allocated_dst = D40_ALLOC_PHY;
1832 phy->allocated_src = D40_ALLOC_PHY;
1833 goto found;
1834 } else
1835 goto not_found;
1836 }
1837
1838 /* Logical channel */
1839 if (is_src) {
1840 if (phy->allocated_src == D40_ALLOC_PHY)
1841 goto not_found;
1842
1843 if (phy->allocated_src == D40_ALLOC_FREE)
1844 phy->allocated_src = D40_ALLOC_LOG_FREE;
1845
1846 if (!(phy->allocated_src & (1 << log_event_line))) {
1847 phy->allocated_src |= 1 << log_event_line;
1848 goto found;
1849 } else
1850 goto not_found;
1851 } else {
1852 if (phy->allocated_dst == D40_ALLOC_PHY)
1853 goto not_found;
1854
1855 if (phy->allocated_dst == D40_ALLOC_FREE)
1856 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1857
1858 if (!(phy->allocated_dst & (1 << log_event_line))) {
1859 phy->allocated_dst |= 1 << log_event_line;
1860 goto found;
1861 } else
1862 goto not_found;
1863 }
1864
1865not_found:
1866 spin_unlock_irqrestore(&phy->lock, flags);
1867 return false;
1868found:
1869 spin_unlock_irqrestore(&phy->lock, flags);
1870 return true;
1871}
1872
1873static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1874 int log_event_line)
1875{
1876 unsigned long flags;
1877 bool is_free = false;
1878
1879 spin_lock_irqsave(&phy->lock, flags);
1880 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001881 phy->allocated_dst = D40_ALLOC_FREE;
1882 phy->allocated_src = D40_ALLOC_FREE;
1883 is_free = true;
1884 goto out;
1885 }
1886
1887 /* Logical channel */
1888 if (is_src) {
1889 phy->allocated_src &= ~(1 << log_event_line);
1890 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1891 phy->allocated_src = D40_ALLOC_FREE;
1892 } else {
1893 phy->allocated_dst &= ~(1 << log_event_line);
1894 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1895 phy->allocated_dst = D40_ALLOC_FREE;
1896 }
1897
1898 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1899 D40_ALLOC_FREE);
1900
1901out:
1902 spin_unlock_irqrestore(&phy->lock, flags);
1903
1904 return is_free;
1905}
1906
Narayanan G5cd326f2011-11-30 19:20:42 +05301907static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001908{
Lee Jones26955c07d2013-05-03 15:31:56 +01001909 int dev_type = d40c->dma_cfg.dev_type;
Linus Walleij8d318a52010-03-30 15:33:42 +02001910 int event_group;
1911 int event_line;
1912 struct d40_phy_res *phys;
1913 int i;
1914 int j;
1915 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001916 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001917 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001918 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001919
1920 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001921 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001922
1923 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001924 log_num = 2 * dev_type;
1925 is_src = true;
1926 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1927 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1928 /* dst event lines are used for logical memcpy */
Linus Walleij8d318a52010-03-30 15:33:42 +02001929 log_num = 2 * dev_type + 1;
1930 is_src = false;
1931 } else
1932 return -EINVAL;
1933
1934 event_group = D40_TYPE_TO_GROUP(dev_type);
1935 event_line = D40_TYPE_TO_EVENT(dev_type);
1936
1937 if (!is_log) {
1938 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1939 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001940 if (d40c->dma_cfg.use_fixed_channel) {
1941 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001942 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301943 0, is_log,
1944 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001945 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001946 } else {
1947 for (i = 0; i < num_phy_chans; i++) {
1948 if (d40_alloc_mask_set(&phys[i], is_src,
1949 0, is_log,
1950 first_phy_user))
1951 goto found_phy;
1952 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001953 }
1954 } else
1955 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1956 int phy_num = j + event_group * 2;
1957 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001958 if (d40_alloc_mask_set(&phys[i],
1959 is_src,
1960 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301961 is_log,
1962 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001963 goto found_phy;
1964 }
1965 }
1966 return -EINVAL;
1967found_phy:
1968 d40c->phy_chan = &phys[i];
1969 d40c->log_num = D40_PHY_CHAN;
1970 goto out;
1971 }
1972 if (dev_type == -1)
1973 return -EINVAL;
1974
1975 /* Find logical channel */
1976 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1977 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301978
1979 if (d40c->dma_cfg.use_fixed_channel) {
1980 i = d40c->dma_cfg.phy_channel;
1981
1982 if ((i != phy_num) && (i != phy_num + 1)) {
1983 dev_err(chan2dev(d40c),
1984 "invalid fixed phy channel %d\n", i);
1985 return -EINVAL;
1986 }
1987
1988 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1989 is_log, first_phy_user))
1990 goto found_log;
1991
1992 dev_err(chan2dev(d40c),
1993 "could not allocate fixed phy channel %d\n", i);
1994 return -EINVAL;
1995 }
1996
Linus Walleij8d318a52010-03-30 15:33:42 +02001997 /*
1998 * Spread logical channels across all available physical rather
1999 * than pack every logical channel at the first available phy
2000 * channels.
2001 */
2002 if (is_src) {
2003 for (i = phy_num; i < phy_num + 2; i++) {
2004 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302005 event_line, is_log,
2006 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002007 goto found_log;
2008 }
2009 } else {
2010 for (i = phy_num + 1; i >= phy_num; i--) {
2011 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302012 event_line, is_log,
2013 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002014 goto found_log;
2015 }
2016 }
2017 }
2018 return -EINVAL;
2019
2020found_log:
2021 d40c->phy_chan = &phys[i];
2022 d40c->log_num = log_num;
2023out:
2024
2025 if (is_log)
2026 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2027 else
2028 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2029
2030 return 0;
2031
2032}
2033
Linus Walleij8d318a52010-03-30 15:33:42 +02002034static int d40_config_memcpy(struct d40_chan *d40c)
2035{
2036 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2037
2038 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002039 d40c->dma_cfg = dma40_memcpy_conf_log;
Lee Jones26955c07d2013-05-03 15:31:56 +01002040 d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002041
2042 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2043 dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002044 d40c->dma_cfg = dma40_memcpy_conf_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002045 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002046 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002047 return -EINVAL;
2048 }
2049
2050 return 0;
2051}
2052
Linus Walleij8d318a52010-03-30 15:33:42 +02002053static int d40_free_dma(struct d40_chan *d40c)
2054{
2055
2056 int res = 0;
Lee Jones26955c07d2013-05-03 15:31:56 +01002057 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002058 struct d40_phy_res *phy = d40c->phy_chan;
2059 bool is_src;
2060
2061 /* Terminate all queued and active transfers */
2062 d40_term_all(d40c);
2063
2064 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002065 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002066 return -EINVAL;
2067 }
2068
2069 if (phy->allocated_src == D40_ALLOC_FREE &&
2070 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002071 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002072 return -EINVAL;
2073 }
2074
Linus Walleij8d318a52010-03-30 15:33:42 +02002075 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Lee Jones26955c07d2013-05-03 15:31:56 +01002076 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002077 is_src = false;
Lee Jones26955c07d2013-05-03 15:31:56 +01002078 else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002079 is_src = true;
Lee Jones26955c07d2013-05-03 15:31:56 +01002080 else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002081 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002082 return -EINVAL;
2083 }
2084
Narayanan G7fb3e752011-11-17 17:26:41 +05302085 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002086 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2087 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302088 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302089 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002090 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302091
Narayanan G1bdae6f2012-02-09 12:41:37 +05302092 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2093
2094 if (chan_is_logical(d40c))
2095 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2096 else
2097 d40c->base->lookup_phy_chans[phy->num] = NULL;
2098
Narayanan G7fb3e752011-11-17 17:26:41 +05302099 if (d40c->busy) {
2100 pm_runtime_mark_last_busy(d40c->base->dev);
2101 pm_runtime_put_autosuspend(d40c->base->dev);
2102 }
2103
2104 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002105 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002106 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302107out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002108
Narayanan G7fb3e752011-11-17 17:26:41 +05302109 pm_runtime_mark_last_busy(d40c->base->dev);
2110 pm_runtime_put_autosuspend(d40c->base->dev);
2111 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002112}
2113
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002114static bool d40_is_paused(struct d40_chan *d40c)
2115{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002116 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002117 bool is_paused = false;
2118 unsigned long flags;
2119 void __iomem *active_reg;
2120 u32 status;
Lee Jones26955c07d2013-05-03 15:31:56 +01002121 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002122
2123 spin_lock_irqsave(&d40c->lock, flags);
2124
Rabin Vincent724a8572011-01-25 11:18:08 +01002125 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002126 if (d40c->phy_chan->num % 2 == 0)
2127 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2128 else
2129 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2130
2131 status = (readl(active_reg) &
2132 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2133 D40_CHAN_POS(d40c->phy_chan->num);
2134 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2135 is_paused = true;
2136
2137 goto _exit;
2138 }
2139
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002140 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002141 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002142 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002143 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002144 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002145 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002146 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002147 goto _exit;
2148 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002149
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002150 status = (status & D40_EVENTLINE_MASK(event)) >>
2151 D40_EVENTLINE_POS(event);
2152
2153 if (status != D40_DMA_RUN)
2154 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002155_exit:
2156 spin_unlock_irqrestore(&d40c->lock, flags);
2157 return is_paused;
2158
2159}
2160
Linus Walleij8d318a52010-03-30 15:33:42 +02002161static u32 stedma40_residue(struct dma_chan *chan)
2162{
2163 struct d40_chan *d40c =
2164 container_of(chan, struct d40_chan, chan);
2165 u32 bytes_left;
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(&d40c->lock, flags);
2169 bytes_left = d40_residue(d40c);
2170 spin_unlock_irqrestore(&d40c->lock, flags);
2171
2172 return bytes_left;
2173}
2174
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002175static int
2176d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2177 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002178 unsigned int sg_len, dma_addr_t src_dev_addr,
2179 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002180{
2181 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2182 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2183 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002184 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002185
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002186 ret = d40_log_sg_to_lli(sg_src, sg_len,
2187 src_dev_addr,
2188 desc->lli_log.src,
2189 chan->log_def.lcsp1,
2190 src_info->data_width,
2191 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002192
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002193 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2194 dst_dev_addr,
2195 desc->lli_log.dst,
2196 chan->log_def.lcsp3,
2197 dst_info->data_width,
2198 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002199
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002200 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002201}
2202
2203static int
2204d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2205 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002206 unsigned int sg_len, dma_addr_t src_dev_addr,
2207 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002208{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002209 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2210 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2211 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002212 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002213 int ret;
2214
Rabin Vincent0c842b52011-01-25 11:18:35 +01002215 if (desc->cyclic)
2216 flags |= LLI_CYCLIC | LLI_TERM_INT;
2217
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002218 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2219 desc->lli_phy.src,
2220 virt_to_phys(desc->lli_phy.src),
2221 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002222 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002223
2224 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2225 desc->lli_phy.dst,
2226 virt_to_phys(desc->lli_phy.dst),
2227 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002228 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002229
2230 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2231 desc->lli_pool.size, DMA_TO_DEVICE);
2232
2233 return ret < 0 ? ret : 0;
2234}
2235
Rabin Vincent5f811582011-01-25 11:18:18 +01002236static struct d40_desc *
2237d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2238 unsigned int sg_len, unsigned long dma_flags)
2239{
2240 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2241 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002242 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002243
2244 desc = d40_desc_get(chan);
2245 if (!desc)
2246 return NULL;
2247
2248 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2249 cfg->dst_info.data_width);
2250 if (desc->lli_len < 0) {
2251 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002252 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002253 }
2254
Rabin Vincentdbd88782011-01-25 11:18:19 +01002255 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2256 if (ret < 0) {
2257 chan_err(chan, "Could not allocate lli\n");
2258 goto err;
2259 }
2260
Rabin Vincent5f811582011-01-25 11:18:18 +01002261 desc->lli_current = 0;
2262 desc->txd.flags = dma_flags;
2263 desc->txd.tx_submit = d40_tx_submit;
2264
2265 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2266
2267 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002268
2269err:
2270 d40_desc_free(chan, desc);
2271 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002272}
2273
Rabin Vincentcade1d32011-01-25 11:18:23 +01002274static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302275d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002276{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002277 struct stedma40_platform_data *plat = chan->base->plat_data;
2278 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002279 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002280
Rabin Vincentcade1d32011-01-25 11:18:23 +01002281 if (chan->runtime_addr)
2282 return chan->runtime_addr;
2283
Vinod Kouldb8196d2011-10-13 22:34:23 +05302284 if (direction == DMA_DEV_TO_MEM)
Lee Jones26955c07d2013-05-03 15:31:56 +01002285 addr = plat->dev_rx[cfg->dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302286 else if (direction == DMA_MEM_TO_DEV)
Lee Jones26955c07d2013-05-03 15:31:56 +01002287 addr = plat->dev_tx[cfg->dev_type];
Rabin Vincentcade1d32011-01-25 11:18:23 +01002288
2289 return addr;
2290}
2291
2292static struct dma_async_tx_descriptor *
2293d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2294 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302295 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002296{
2297 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002298 dma_addr_t src_dev_addr = 0;
2299 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002300 struct d40_desc *desc;
2301 unsigned long flags;
2302 int ret;
2303
2304 if (!chan->phy_chan) {
2305 chan_err(chan, "Cannot prepare unallocated channel\n");
2306 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002307 }
2308
Rabin Vincentcade1d32011-01-25 11:18:23 +01002309 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002310
Rabin Vincentcade1d32011-01-25 11:18:23 +01002311 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2312 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002313 goto err;
2314
Rabin Vincent0c842b52011-01-25 11:18:35 +01002315 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2316 desc->cyclic = true;
2317
Linus Walleij7e426da2012-04-12 18:12:52 +02002318 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002319 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2320
Vinod Kouldb8196d2011-10-13 22:34:23 +05302321 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002322 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302323 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002324 dst_dev_addr = dev_addr;
2325 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002326
2327 if (chan_is_logical(chan))
2328 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002329 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002330 else
2331 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002332 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002333
2334 if (ret) {
2335 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2336 chan_is_logical(chan) ? "log" : "phy", ret);
2337 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002338 }
2339
Per Forlin82babbb362011-08-29 13:33:35 +02002340 /*
2341 * add descriptor to the prepare queue in order to be able
2342 * to free them later in terminate_all
2343 */
2344 list_add_tail(&desc->node, &chan->prepare_queue);
2345
Rabin Vincentcade1d32011-01-25 11:18:23 +01002346 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002347
Rabin Vincentcade1d32011-01-25 11:18:23 +01002348 return &desc->txd;
2349
Linus Walleij8d318a52010-03-30 15:33:42 +02002350err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002351 if (desc)
2352 d40_desc_free(chan, desc);
2353 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002354 return NULL;
2355}
Linus Walleij8d318a52010-03-30 15:33:42 +02002356
2357bool stedma40_filter(struct dma_chan *chan, void *data)
2358{
2359 struct stedma40_chan_cfg *info = data;
2360 struct d40_chan *d40c =
2361 container_of(chan, struct d40_chan, chan);
2362 int err;
2363
2364 if (data) {
2365 err = d40_validate_conf(d40c, info);
2366 if (!err)
2367 d40c->dma_cfg = *info;
2368 } else
2369 err = d40_config_memcpy(d40c);
2370
Rabin Vincentce2ca122010-10-12 13:00:49 +00002371 if (!err)
2372 d40c->configured = true;
2373
Linus Walleij8d318a52010-03-30 15:33:42 +02002374 return err == 0;
2375}
2376EXPORT_SYMBOL(stedma40_filter);
2377
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002378static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2379{
2380 bool realtime = d40c->dma_cfg.realtime;
2381 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002382 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002383 u32 event = D40_TYPE_TO_EVENT(dev_type);
2384 u32 group = D40_TYPE_TO_GROUP(dev_type);
2385 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302386 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002387 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302388
Tong Liu3cb645d2012-09-26 10:07:30 +00002389 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302390 /*
2391 * Due to a hardware bug, in some cases a logical channel triggered by
2392 * a high priority destination event line can generate extra packet
2393 * transactions.
2394 *
2395 * The workaround is to not set the high priority level for the
2396 * destination event lines that trigger logical channels.
2397 */
2398 if (!src && chan_is_logical(d40c))
2399 highprio = false;
2400
Tong Liu3cb645d2012-09-26 10:07:30 +00002401 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002402
2403 /* Destination event lines are stored in the upper halfword */
2404 if (!src)
2405 bit <<= 16;
2406
2407 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2408 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2409}
2410
2411static void d40_set_prio_realtime(struct d40_chan *d40c)
2412{
2413 if (d40c->base->rev < 3)
2414 return;
2415
2416 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2417 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002418 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002419
2420 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2421 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002422 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002423}
2424
Linus Walleij8d318a52010-03-30 15:33:42 +02002425/* DMA ENGINE functions */
2426static int d40_alloc_chan_resources(struct dma_chan *chan)
2427{
2428 int err;
2429 unsigned long flags;
2430 struct d40_chan *d40c =
2431 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002432 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002433 spin_lock_irqsave(&d40c->lock, flags);
2434
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002435 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002436
Rabin Vincentce2ca122010-10-12 13:00:49 +00002437 /* If no dma configuration is set use default configuration (memcpy) */
2438 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002439 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002440 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002441 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002442 goto fail;
2443 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002444 }
2445
Narayanan G5cd326f2011-11-30 19:20:42 +05302446 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002447 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002448 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302449 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002450 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002451 }
2452
Narayanan G7fb3e752011-11-17 17:26:41 +05302453 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002454 /* Fill in basic CFG register values */
2455 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002456 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002457
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002458 d40_set_prio_realtime(d40c);
2459
Rabin Vincent724a8572011-01-25 11:18:08 +01002460 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002461 d40_log_cfg(&d40c->dma_cfg,
2462 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2463
2464 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2465 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002466 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002467 else
2468 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002469 d40c->dma_cfg.dev_type *
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002470 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Linus Walleijef1872e2010-06-20 21:24:52 +00002471 }
2472
Narayanan G5cd326f2011-11-30 19:20:42 +05302473 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2474 chan_is_logical(d40c) ? "logical" : "physical",
2475 d40c->phy_chan->num,
2476 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2477
2478
Linus Walleijef1872e2010-06-20 21:24:52 +00002479 /*
2480 * Only write channel configuration to the DMA if the physical
2481 * resource is free. In case of multiple logical channels
2482 * on the same physical resource, only the first write is necessary.
2483 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002484 if (is_free_phy)
2485 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002486fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302487 pm_runtime_mark_last_busy(d40c->base->dev);
2488 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002489 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002490 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002491}
2492
2493static void d40_free_chan_resources(struct dma_chan *chan)
2494{
2495 struct d40_chan *d40c =
2496 container_of(chan, struct d40_chan, chan);
2497 int err;
2498 unsigned long flags;
2499
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002500 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002501 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002502 return;
2503 }
2504
Linus Walleij8d318a52010-03-30 15:33:42 +02002505 spin_lock_irqsave(&d40c->lock, flags);
2506
2507 err = d40_free_dma(d40c);
2508
2509 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002510 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002511 spin_unlock_irqrestore(&d40c->lock, flags);
2512}
2513
2514static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2515 dma_addr_t dst,
2516 dma_addr_t src,
2517 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002518 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002519{
Rabin Vincent95944c62011-01-25 11:18:17 +01002520 struct scatterlist dst_sg;
2521 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002522
Rabin Vincent95944c62011-01-25 11:18:17 +01002523 sg_init_table(&dst_sg, 1);
2524 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002525
Rabin Vincent95944c62011-01-25 11:18:17 +01002526 sg_dma_address(&dst_sg) = dst;
2527 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002528
Rabin Vincent95944c62011-01-25 11:18:17 +01002529 sg_dma_len(&dst_sg) = size;
2530 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002531
Rabin Vincentcade1d32011-01-25 11:18:23 +01002532 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002533}
2534
Ira Snyder0d688662010-09-30 11:46:47 +00002535static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002536d40_prep_memcpy_sg(struct dma_chan *chan,
2537 struct scatterlist *dst_sg, unsigned int dst_nents,
2538 struct scatterlist *src_sg, unsigned int src_nents,
2539 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002540{
2541 if (dst_nents != src_nents)
2542 return NULL;
2543
Rabin Vincentcade1d32011-01-25 11:18:23 +01002544 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002545}
2546
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002547static struct dma_async_tx_descriptor *
2548d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2549 unsigned int sg_len, enum dma_transfer_direction direction,
2550 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002551{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002552 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002553 return NULL;
2554
Rabin Vincentcade1d32011-01-25 11:18:23 +01002555 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002556}
2557
Rabin Vincent0c842b52011-01-25 11:18:35 +01002558static struct dma_async_tx_descriptor *
2559dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2560 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002561 enum dma_transfer_direction direction, unsigned long flags,
2562 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002563{
2564 unsigned int periods = buf_len / period_len;
2565 struct dma_async_tx_descriptor *txd;
2566 struct scatterlist *sg;
2567 int i;
2568
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002569 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002570 for (i = 0; i < periods; i++) {
2571 sg_dma_address(&sg[i]) = dma_addr;
2572 sg_dma_len(&sg[i]) = period_len;
2573 dma_addr += period_len;
2574 }
2575
2576 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002577 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002578 sg[periods].page_link =
2579 ((unsigned long)sg | 0x01) & ~0x02;
2580
2581 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2582 DMA_PREP_INTERRUPT);
2583
2584 kfree(sg);
2585
2586 return txd;
2587}
2588
Linus Walleij8d318a52010-03-30 15:33:42 +02002589static enum dma_status d40_tx_status(struct dma_chan *chan,
2590 dma_cookie_t cookie,
2591 struct dma_tx_state *txstate)
2592{
2593 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002594 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002595
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002596 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002597 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002598 return -EINVAL;
2599 }
2600
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002601 ret = dma_cookie_status(chan, cookie, txstate);
2602 if (ret != DMA_SUCCESS)
2603 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002604
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002605 if (d40_is_paused(d40c))
2606 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002607
2608 return ret;
2609}
2610
2611static void d40_issue_pending(struct dma_chan *chan)
2612{
2613 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2614 unsigned long flags;
2615
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002616 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002617 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002618 return;
2619 }
2620
Linus Walleij8d318a52010-03-30 15:33:42 +02002621 spin_lock_irqsave(&d40c->lock, flags);
2622
Per Forlina8f30672011-06-26 23:29:52 +02002623 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2624
2625 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002626 if (!d40c->busy)
2627 (void) d40_queue_start(d40c);
2628
2629 spin_unlock_irqrestore(&d40c->lock, flags);
2630}
2631
Narayanan G1bdae6f2012-02-09 12:41:37 +05302632static void d40_terminate_all(struct dma_chan *chan)
2633{
2634 unsigned long flags;
2635 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2636 int ret;
2637
2638 spin_lock_irqsave(&d40c->lock, flags);
2639
2640 pm_runtime_get_sync(d40c->base->dev);
2641 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2642 if (ret)
2643 chan_err(d40c, "Failed to stop channel\n");
2644
2645 d40_term_all(d40c);
2646 pm_runtime_mark_last_busy(d40c->base->dev);
2647 pm_runtime_put_autosuspend(d40c->base->dev);
2648 if (d40c->busy) {
2649 pm_runtime_mark_last_busy(d40c->base->dev);
2650 pm_runtime_put_autosuspend(d40c->base->dev);
2651 }
2652 d40c->busy = false;
2653
2654 spin_unlock_irqrestore(&d40c->lock, flags);
2655}
2656
Rabin Vincent98ca5282011-06-27 11:33:38 +02002657static int
2658dma40_config_to_halfchannel(struct d40_chan *d40c,
2659 struct stedma40_half_channel_info *info,
2660 enum dma_slave_buswidth width,
2661 u32 maxburst)
2662{
2663 enum stedma40_periph_data_width addr_width;
2664 int psize;
2665
2666 switch (width) {
2667 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2668 addr_width = STEDMA40_BYTE_WIDTH;
2669 break;
2670 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2671 addr_width = STEDMA40_HALFWORD_WIDTH;
2672 break;
2673 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2674 addr_width = STEDMA40_WORD_WIDTH;
2675 break;
2676 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2677 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2678 break;
2679 default:
2680 dev_err(d40c->base->dev,
2681 "illegal peripheral address width "
2682 "requested (%d)\n",
2683 width);
2684 return -EINVAL;
2685 }
2686
2687 if (chan_is_logical(d40c)) {
2688 if (maxburst >= 16)
2689 psize = STEDMA40_PSIZE_LOG_16;
2690 else if (maxburst >= 8)
2691 psize = STEDMA40_PSIZE_LOG_8;
2692 else if (maxburst >= 4)
2693 psize = STEDMA40_PSIZE_LOG_4;
2694 else
2695 psize = STEDMA40_PSIZE_LOG_1;
2696 } else {
2697 if (maxburst >= 16)
2698 psize = STEDMA40_PSIZE_PHY_16;
2699 else if (maxburst >= 8)
2700 psize = STEDMA40_PSIZE_PHY_8;
2701 else if (maxburst >= 4)
2702 psize = STEDMA40_PSIZE_PHY_4;
2703 else
2704 psize = STEDMA40_PSIZE_PHY_1;
2705 }
2706
2707 info->data_width = addr_width;
2708 info->psize = psize;
2709 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2710
2711 return 0;
2712}
2713
Linus Walleij95e14002010-08-04 13:37:45 +02002714/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002715static int d40_set_runtime_config(struct dma_chan *chan,
2716 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002717{
2718 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2719 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002720 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002721 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002722 u32 src_maxburst, dst_maxburst;
2723 int ret;
2724
2725 src_addr_width = config->src_addr_width;
2726 src_maxburst = config->src_maxburst;
2727 dst_addr_width = config->dst_addr_width;
2728 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002729
Vinod Kouldb8196d2011-10-13 22:34:23 +05302730 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002731 dma_addr_t dev_addr_rx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002732 d40c->base->plat_data->dev_rx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002733
2734 config_addr = config->src_addr;
2735 if (dev_addr_rx)
2736 dev_dbg(d40c->base->dev,
2737 "channel has a pre-wired RX address %08x "
2738 "overriding with %08x\n",
2739 dev_addr_rx, config_addr);
2740 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2741 dev_dbg(d40c->base->dev,
2742 "channel was not configured for peripheral "
2743 "to memory transfer (%d) overriding\n",
2744 cfg->dir);
2745 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2746
Rabin Vincent98ca5282011-06-27 11:33:38 +02002747 /* Configure the memory side */
2748 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2749 dst_addr_width = src_addr_width;
2750 if (dst_maxburst == 0)
2751 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002752
Vinod Kouldb8196d2011-10-13 22:34:23 +05302753 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002754 dma_addr_t dev_addr_tx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002755 d40c->base->plat_data->dev_tx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002756
2757 config_addr = config->dst_addr;
2758 if (dev_addr_tx)
2759 dev_dbg(d40c->base->dev,
2760 "channel has a pre-wired TX address %08x "
2761 "overriding with %08x\n",
2762 dev_addr_tx, config_addr);
2763 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2764 dev_dbg(d40c->base->dev,
2765 "channel was not configured for memory "
2766 "to peripheral transfer (%d) overriding\n",
2767 cfg->dir);
2768 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2769
Rabin Vincent98ca5282011-06-27 11:33:38 +02002770 /* Configure the memory side */
2771 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2772 src_addr_width = dst_addr_width;
2773 if (src_maxburst == 0)
2774 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002775 } else {
2776 dev_err(d40c->base->dev,
2777 "unrecognized channel direction %d\n",
2778 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002779 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002780 }
2781
Rabin Vincent98ca5282011-06-27 11:33:38 +02002782 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002783 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002784 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2785 src_maxburst,
2786 src_addr_width,
2787 dst_maxburst,
2788 dst_addr_width);
2789 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002790 }
2791
Per Forlin92bb6cd2011-10-13 12:11:36 +02002792 if (src_maxburst > 16) {
2793 src_maxburst = 16;
2794 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2795 } else if (dst_maxburst > 16) {
2796 dst_maxburst = 16;
2797 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2798 }
2799
Rabin Vincent98ca5282011-06-27 11:33:38 +02002800 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2801 src_addr_width,
2802 src_maxburst);
2803 if (ret)
2804 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002805
Rabin Vincent98ca5282011-06-27 11:33:38 +02002806 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2807 dst_addr_width,
2808 dst_maxburst);
2809 if (ret)
2810 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002811
Per Forlina59670a2010-10-06 09:05:27 +00002812 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002813 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002814 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2815 else
2816 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2817 &d40c->dst_def_cfg, false);
2818
Linus Walleij95e14002010-08-04 13:37:45 +02002819 /* These settings will take precedence later */
2820 d40c->runtime_addr = config_addr;
2821 d40c->runtime_direction = config->direction;
2822 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002823 "configured channel %s for %s, data width %d/%d, "
2824 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002825 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302826 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002827 src_addr_width, dst_addr_width,
2828 src_maxburst, dst_maxburst);
2829
2830 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002831}
2832
Linus Walleij05827632010-05-17 16:30:42 -07002833static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2834 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002835{
Linus Walleij8d318a52010-03-30 15:33:42 +02002836 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2837
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002838 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002839 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002840 return -EINVAL;
2841 }
2842
Linus Walleij8d318a52010-03-30 15:33:42 +02002843 switch (cmd) {
2844 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302845 d40_terminate_all(chan);
2846 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002847 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002848 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002849 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002850 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002851 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002852 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002853 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002854 default:
2855 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002856 }
2857
2858 /* Other commands are unimplemented */
2859 return -ENXIO;
2860}
2861
2862/* Initialization functions */
2863
2864static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2865 struct d40_chan *chans, int offset,
2866 int num_chans)
2867{
2868 int i = 0;
2869 struct d40_chan *d40c;
2870
2871 INIT_LIST_HEAD(&dma->channels);
2872
2873 for (i = offset; i < offset + num_chans; i++) {
2874 d40c = &chans[i];
2875 d40c->base = base;
2876 d40c->chan.device = dma;
2877
Linus Walleij8d318a52010-03-30 15:33:42 +02002878 spin_lock_init(&d40c->lock);
2879
2880 d40c->log_num = D40_PHY_CHAN;
2881
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002882 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002883 INIT_LIST_HEAD(&d40c->active);
2884 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002885 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002886 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002887 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002888
Linus Walleij8d318a52010-03-30 15:33:42 +02002889 tasklet_init(&d40c->tasklet, dma_tasklet,
2890 (unsigned long) d40c);
2891
2892 list_add_tail(&d40c->chan.device_node,
2893 &dma->channels);
2894 }
2895}
2896
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002897static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2898{
2899 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2900 dev->device_prep_slave_sg = d40_prep_slave_sg;
2901
2902 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2903 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2904
2905 /*
2906 * This controller can only access address at even
2907 * 32bit boundaries, i.e. 2^2
2908 */
2909 dev->copy_align = 2;
2910 }
2911
2912 if (dma_has_cap(DMA_SG, dev->cap_mask))
2913 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2914
Rabin Vincent0c842b52011-01-25 11:18:35 +01002915 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2916 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2917
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002918 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2919 dev->device_free_chan_resources = d40_free_chan_resources;
2920 dev->device_issue_pending = d40_issue_pending;
2921 dev->device_tx_status = d40_tx_status;
2922 dev->device_control = d40_control;
2923 dev->dev = base->dev;
2924}
2925
Linus Walleij8d318a52010-03-30 15:33:42 +02002926static int __init d40_dmaengine_init(struct d40_base *base,
2927 int num_reserved_chans)
2928{
2929 int err ;
2930
2931 d40_chan_init(base, &base->dma_slave, base->log_chans,
2932 0, base->num_log_chans);
2933
2934 dma_cap_zero(base->dma_slave.cap_mask);
2935 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002936 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002937
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002938 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002939
2940 err = dma_async_device_register(&base->dma_slave);
2941
2942 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002943 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002944 goto failure1;
2945 }
2946
2947 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002948 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002949
2950 dma_cap_zero(base->dma_memcpy.cap_mask);
2951 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002952 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002953
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002954 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002955
2956 err = dma_async_device_register(&base->dma_memcpy);
2957
2958 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002959 d40_err(base->dev,
2960 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002961 goto failure2;
2962 }
2963
2964 d40_chan_init(base, &base->dma_both, base->phy_chans,
2965 0, num_reserved_chans);
2966
2967 dma_cap_zero(base->dma_both.cap_mask);
2968 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2969 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002970 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002971 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002972
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002973 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002974 err = dma_async_device_register(&base->dma_both);
2975
2976 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002977 d40_err(base->dev,
2978 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002979 goto failure3;
2980 }
2981 return 0;
2982failure3:
2983 dma_async_device_unregister(&base->dma_memcpy);
2984failure2:
2985 dma_async_device_unregister(&base->dma_slave);
2986failure1:
2987 return err;
2988}
2989
Narayanan G7fb3e752011-11-17 17:26:41 +05302990/* Suspend resume functionality */
2991#ifdef CONFIG_PM
2992static int dma40_pm_suspend(struct device *dev)
2993{
Narayanan G28c7a192011-11-22 13:56:55 +05302994 struct platform_device *pdev = to_platform_device(dev);
2995 struct d40_base *base = platform_get_drvdata(pdev);
2996 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302997
Narayanan G28c7a192011-11-22 13:56:55 +05302998 if (base->lcpa_regulator)
2999 ret = regulator_disable(base->lcpa_regulator);
3000 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05303001}
3002
3003static int dma40_runtime_suspend(struct device *dev)
3004{
3005 struct platform_device *pdev = to_platform_device(dev);
3006 struct d40_base *base = platform_get_drvdata(pdev);
3007
3008 d40_save_restore_registers(base, true);
3009
3010 /* Don't disable/enable clocks for v1 due to HW bugs */
3011 if (base->rev != 1)
3012 writel_relaxed(base->gcc_pwr_off_mask,
3013 base->virtbase + D40_DREG_GCC);
3014
3015 return 0;
3016}
3017
3018static int dma40_runtime_resume(struct device *dev)
3019{
3020 struct platform_device *pdev = to_platform_device(dev);
3021 struct d40_base *base = platform_get_drvdata(pdev);
3022
3023 if (base->initialized)
3024 d40_save_restore_registers(base, false);
3025
3026 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3027 base->virtbase + D40_DREG_GCC);
3028 return 0;
3029}
3030
Narayanan G28c7a192011-11-22 13:56:55 +05303031static int dma40_resume(struct device *dev)
3032{
3033 struct platform_device *pdev = to_platform_device(dev);
3034 struct d40_base *base = platform_get_drvdata(pdev);
3035 int ret = 0;
3036
3037 if (base->lcpa_regulator)
3038 ret = regulator_enable(base->lcpa_regulator);
3039
3040 return ret;
3041}
Narayanan G7fb3e752011-11-17 17:26:41 +05303042
3043static const struct dev_pm_ops dma40_pm_ops = {
3044 .suspend = dma40_pm_suspend,
3045 .runtime_suspend = dma40_runtime_suspend,
3046 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303047 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303048};
3049#define DMA40_PM_OPS (&dma40_pm_ops)
3050#else
3051#define DMA40_PM_OPS NULL
3052#endif
3053
Linus Walleij8d318a52010-03-30 15:33:42 +02003054/* Initialization functions. */
3055
3056static int __init d40_phy_res_init(struct d40_base *base)
3057{
3058 int i;
3059 int num_phy_chans_avail = 0;
3060 u32 val[2];
3061 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303062 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003063
3064 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3065 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3066
3067 for (i = 0; i < base->num_phy_chans; i++) {
3068 base->phy_res[i].num = i;
3069 odd_even_bit += 2 * ((i % 2) == 0);
3070 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3071 /* Mark security only channels as occupied */
3072 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3073 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303074 base->phy_res[i].reserved = true;
3075 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3076 D40_DREG_GCC_SRC);
3077 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3078 D40_DREG_GCC_DST);
3079
3080
Linus Walleij8d318a52010-03-30 15:33:42 +02003081 } else {
3082 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3083 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303084 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003085 num_phy_chans_avail++;
3086 }
3087 spin_lock_init(&base->phy_res[i].lock);
3088 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003089
3090 /* Mark disabled channels as occupied */
3091 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003092 int chan = base->plat_data->disabled_channels[i];
3093
3094 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3095 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303096 base->phy_res[chan].reserved = true;
3097 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3098 D40_DREG_GCC_SRC);
3099 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3100 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003101 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003102 }
3103
Fabio Baltieri74070482012-12-18 12:25:14 +01003104 /* Mark soft_lli channels */
3105 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3106 int chan = base->plat_data->soft_lli_chans[i];
3107
3108 base->phy_res[chan].use_soft_lli = true;
3109 }
3110
Linus Walleij8d318a52010-03-30 15:33:42 +02003111 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3112 num_phy_chans_avail, base->num_phy_chans);
3113
3114 /* Verify settings extended vs standard */
3115 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3116
3117 for (i = 0; i < base->num_phy_chans; i++) {
3118
3119 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3120 (val[0] & 0x3) != 1)
3121 dev_info(base->dev,
3122 "[%s] INFO: channel %d is misconfigured (%d)\n",
3123 __func__, i, val[0] & 0x3);
3124
3125 val[0] = val[0] >> 2;
3126 }
3127
Narayanan G7fb3e752011-11-17 17:26:41 +05303128 /*
3129 * To keep things simple, Enable all clocks initially.
3130 * The clocks will get managed later post channel allocation.
3131 * The clocks for the event lines on which reserved channels exists
3132 * are not managed here.
3133 */
3134 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3135 base->gcc_pwr_off_mask = gcc;
3136
Linus Walleij8d318a52010-03-30 15:33:42 +02003137 return num_phy_chans_avail;
3138}
3139
3140static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3141{
Lee Jonesbb75d932013-05-03 15:32:10 +01003142 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003143 struct clk *clk = NULL;
3144 void __iomem *virtbase = NULL;
3145 struct resource *res = NULL;
3146 struct d40_base *base = NULL;
3147 int num_log_chans = 0;
3148 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003149 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003150 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003151 u32 pid;
3152 u32 cid;
3153 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003154
3155 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003156 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003157 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003158 goto failure;
3159 }
3160
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003161 clk_ret = clk_prepare_enable(clk);
3162 if (clk_ret) {
3163 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3164 goto failure;
3165 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003166
3167 /* Get IO for DMAC base address */
3168 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3169 if (!res)
3170 goto failure;
3171
3172 if (request_mem_region(res->start, resource_size(res),
3173 D40_NAME " I/O base") == NULL)
3174 goto failure;
3175
3176 virtbase = ioremap(res->start, resource_size(res));
3177 if (!virtbase)
3178 goto failure;
3179
Linus Walleijf4b89762011-06-27 11:33:46 +02003180 /* This is just a regular AMBA PrimeCell ID actually */
3181 for (pid = 0, i = 0; i < 4; i++)
3182 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3183 & 255) << (i * 8);
3184 for (cid = 0, i = 0; i < 4; i++)
3185 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3186 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003187
Linus Walleijf4b89762011-06-27 11:33:46 +02003188 if (cid != AMBA_CID) {
3189 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003190 goto failure;
3191 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003192 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3193 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3194 AMBA_MANF_BITS(pid),
3195 AMBA_VENDOR_ST);
3196 goto failure;
3197 }
3198 /*
3199 * HW revision:
3200 * DB8500ed has revision 0
3201 * ? has revision 1
3202 * DB8500v1 has revision 2
3203 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003204 * AP9540v1 has revision 4
3205 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003206 */
3207 rev = AMBA_REV_BITS(pid);
Lee Jones8b2fe9b2013-05-03 15:32:08 +01003208 if (rev < 2) {
3209 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3210 goto failure;
3211 }
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003212
Gerald Baeza47db92f2012-09-21 21:21:37 +02003213 /* The number of physical channels on this HW */
3214 if (plat_data->num_of_phy_chans)
3215 num_phy_chans = plat_data->num_of_phy_chans;
3216 else
3217 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3218
Lee Jonesdb72da92013-05-03 15:32:03 +01003219 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3220
Lee Jonesb2abb242013-05-03 15:32:09 +01003221 dev_info(&pdev->dev,
3222 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
3223 rev, res->start, num_phy_chans, num_log_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003224
Linus Walleij8d318a52010-03-30 15:33:42 +02003225 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003226 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003227 sizeof(struct d40_chan), GFP_KERNEL);
3228
3229 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003230 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003231 goto failure;
3232 }
3233
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003234 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003235 base->clk = clk;
3236 base->num_phy_chans = num_phy_chans;
3237 base->num_log_chans = num_log_chans;
3238 base->phy_start = res->start;
3239 base->phy_size = resource_size(res);
3240 base->virtbase = virtbase;
3241 base->plat_data = plat_data;
3242 base->dev = &pdev->dev;
3243 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3244 base->log_chans = &base->phy_chans[num_phy_chans];
3245
Tong Liu3cb645d2012-09-26 10:07:30 +00003246 if (base->plat_data->num_of_phy_chans == 14) {
3247 base->gen_dmac.backup = d40_backup_regs_v4b;
3248 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3249 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3250 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3251 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3252 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3253 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3254 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3255 base->gen_dmac.il = il_v4b;
3256 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3257 base->gen_dmac.init_reg = dma_init_reg_v4b;
3258 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3259 } else {
3260 if (base->rev >= 3) {
3261 base->gen_dmac.backup = d40_backup_regs_v4a;
3262 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3263 }
3264 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3265 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3266 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3267 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3268 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3269 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3270 base->gen_dmac.il = il_v4a;
3271 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3272 base->gen_dmac.init_reg = dma_init_reg_v4a;
3273 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3274 }
3275
Linus Walleij8d318a52010-03-30 15:33:42 +02003276 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3277 GFP_KERNEL);
3278 if (!base->phy_res)
3279 goto failure;
3280
3281 base->lookup_phy_chans = kzalloc(num_phy_chans *
3282 sizeof(struct d40_chan *),
3283 GFP_KERNEL);
3284 if (!base->lookup_phy_chans)
3285 goto failure;
3286
Lee Jones8a59fed2013-05-03 15:32:04 +01003287 base->lookup_log_chans = kzalloc(num_log_chans *
3288 sizeof(struct d40_chan *),
3289 GFP_KERNEL);
3290 if (!base->lookup_log_chans)
3291 goto failure;
Jonas Aaberg698e4732010-08-09 12:08:56 +00003292
Narayanan G7fb3e752011-11-17 17:26:41 +05303293 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3294 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003295 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303296 if (!base->reg_val_backup_chan)
3297 goto failure;
3298
3299 base->lcla_pool.alloc_map =
3300 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3301 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003302 if (!base->lcla_pool.alloc_map)
3303 goto failure;
3304
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003305 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3306 0, SLAB_HWCACHE_ALIGN,
3307 NULL);
3308 if (base->desc_slab == NULL)
3309 goto failure;
3310
Linus Walleij8d318a52010-03-30 15:33:42 +02003311 return base;
3312
3313failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003314 if (!clk_ret)
3315 clk_disable_unprepare(clk);
3316 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003317 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003318 if (virtbase)
3319 iounmap(virtbase);
3320 if (res)
3321 release_mem_region(res->start,
3322 resource_size(res));
3323 if (virtbase)
3324 iounmap(virtbase);
3325
3326 if (base) {
3327 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303328 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003329 kfree(base->lookup_log_chans);
3330 kfree(base->lookup_phy_chans);
3331 kfree(base->phy_res);
3332 kfree(base);
3333 }
3334
3335 return NULL;
3336}
3337
3338static void __init d40_hw_init(struct d40_base *base)
3339{
3340
Linus Walleij8d318a52010-03-30 15:33:42 +02003341 int i;
3342 u32 prmseo[2] = {0, 0};
3343 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3344 u32 pcmis = 0;
3345 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003346 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3347 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003348
Tong Liu3cb645d2012-09-26 10:07:30 +00003349 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003350 writel(dma_init_reg[i].val,
3351 base->virtbase + dma_init_reg[i].reg);
3352
3353 /* Configure all our dma channels to default settings */
3354 for (i = 0; i < base->num_phy_chans; i++) {
3355
3356 activeo[i % 2] = activeo[i % 2] << 2;
3357
3358 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3359 == D40_ALLOC_PHY) {
3360 activeo[i % 2] |= 3;
3361 continue;
3362 }
3363
3364 /* Enable interrupt # */
3365 pcmis = (pcmis << 1) | 1;
3366
3367 /* Clear interrupt # */
3368 pcicr = (pcicr << 1) | 1;
3369
3370 /* Set channel to physical mode */
3371 prmseo[i % 2] = prmseo[i % 2] << 2;
3372 prmseo[i % 2] |= 1;
3373
3374 }
3375
3376 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3377 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3378 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3379 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3380
3381 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003382 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003383
3384 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003385 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003386
Tong Liu3cb645d2012-09-26 10:07:30 +00003387 /* These are __initdata and cannot be accessed after init */
3388 base->gen_dmac.init_reg = NULL;
3389 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003390}
3391
Linus Walleij508849a2010-06-20 21:26:07 +00003392static int __init d40_lcla_allocate(struct d40_base *base)
3393{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003394 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003395 unsigned long *page_list;
3396 int i, j;
3397 int ret = 0;
3398
3399 /*
3400 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3401 * To full fill this hardware requirement without wasting 256 kb
3402 * we allocate pages until we get an aligned one.
3403 */
3404 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3405 GFP_KERNEL);
3406
3407 if (!page_list) {
3408 ret = -ENOMEM;
3409 goto failure;
3410 }
3411
3412 /* Calculating how many pages that are required */
3413 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3414
3415 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3416 page_list[i] = __get_free_pages(GFP_KERNEL,
3417 base->lcla_pool.pages);
3418 if (!page_list[i]) {
3419
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003420 d40_err(base->dev, "Failed to allocate %d pages.\n",
3421 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003422
3423 for (j = 0; j < i; j++)
3424 free_pages(page_list[j], base->lcla_pool.pages);
3425 goto failure;
3426 }
3427
3428 if ((virt_to_phys((void *)page_list[i]) &
3429 (LCLA_ALIGNMENT - 1)) == 0)
3430 break;
3431 }
3432
3433 for (j = 0; j < i; j++)
3434 free_pages(page_list[j], base->lcla_pool.pages);
3435
3436 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3437 base->lcla_pool.base = (void *)page_list[i];
3438 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003439 /*
3440 * After many attempts and no succees with finding the correct
3441 * alignment, try with allocating a big buffer.
3442 */
Linus Walleij508849a2010-06-20 21:26:07 +00003443 dev_warn(base->dev,
3444 "[%s] Failed to get %d pages @ 18 bit align.\n",
3445 __func__, base->lcla_pool.pages);
3446 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3447 base->num_phy_chans +
3448 LCLA_ALIGNMENT,
3449 GFP_KERNEL);
3450 if (!base->lcla_pool.base_unaligned) {
3451 ret = -ENOMEM;
3452 goto failure;
3453 }
3454
3455 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3456 LCLA_ALIGNMENT);
3457 }
3458
Rabin Vincent026cbc42011-01-25 11:18:14 +01003459 pool->dma_addr = dma_map_single(base->dev, pool->base,
3460 SZ_1K * base->num_phy_chans,
3461 DMA_TO_DEVICE);
3462 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3463 pool->dma_addr = 0;
3464 ret = -ENOMEM;
3465 goto failure;
3466 }
3467
Linus Walleij508849a2010-06-20 21:26:07 +00003468 writel(virt_to_phys(base->lcla_pool.base),
3469 base->virtbase + D40_DREG_LCLA);
3470failure:
3471 kfree(page_list);
3472 return ret;
3473}
3474
Lee Jones1814a172013-05-03 15:32:11 +01003475static int __init d40_of_probe(struct platform_device *pdev,
3476 struct device_node *np)
3477{
3478 struct stedma40_platform_data *pdata;
3479
3480 /*
3481 * FIXME: Fill in this routine as more support is added.
3482 * First platform enabled (u8500) doens't need any extra
3483 * properties to run, so this is fairly sparce currently.
3484 */
3485
3486 pdata = devm_kzalloc(&pdev->dev,
3487 sizeof(struct stedma40_platform_data),
3488 GFP_KERNEL);
3489 if (!pdata)
3490 return -ENOMEM;
3491
3492 pdev->dev.platform_data = pdata;
3493
3494 return 0;
3495}
3496
Linus Walleij8d318a52010-03-30 15:33:42 +02003497static int __init d40_probe(struct platform_device *pdev)
3498{
Lee Jones1814a172013-05-03 15:32:11 +01003499 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3500 struct device_node *np = pdev->dev.of_node;
Linus Walleij8d318a52010-03-30 15:33:42 +02003501 int err;
3502 int ret = -ENOENT;
Lee Jones1814a172013-05-03 15:32:11 +01003503 struct d40_base *base = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003504 struct resource *res = NULL;
3505 int num_reserved_chans;
3506 u32 val;
3507
Lee Jones1814a172013-05-03 15:32:11 +01003508 if (!plat_data) {
3509 if (np) {
3510 if(d40_of_probe(pdev, np)) {
3511 ret = -ENOMEM;
3512 goto failure;
3513 }
3514 } else {
3515 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3516 goto failure;
3517 }
3518 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003519
Lee Jones1814a172013-05-03 15:32:11 +01003520 base = d40_hw_detect_init(pdev);
Linus Walleij8d318a52010-03-30 15:33:42 +02003521 if (!base)
3522 goto failure;
3523
3524 num_reserved_chans = d40_phy_res_init(base);
3525
3526 platform_set_drvdata(pdev, base);
3527
3528 spin_lock_init(&base->interrupt_lock);
3529 spin_lock_init(&base->execmd_lock);
3530
3531 /* Get IO for logical channel parameter address */
3532 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3533 if (!res) {
3534 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003535 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003536 goto failure;
3537 }
3538 base->lcpa_size = resource_size(res);
3539 base->phy_lcpa = res->start;
3540
3541 if (request_mem_region(res->start, resource_size(res),
3542 D40_NAME " I/O lcpa") == NULL) {
3543 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003544 d40_err(&pdev->dev,
3545 "Failed to request LCPA region 0x%x-0x%x\n",
3546 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003547 goto failure;
3548 }
3549
3550 /* We make use of ESRAM memory for this. */
3551 val = readl(base->virtbase + D40_DREG_LCPA);
3552 if (res->start != val && val != 0) {
3553 dev_warn(&pdev->dev,
3554 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3555 __func__, val, res->start);
3556 } else
3557 writel(res->start, base->virtbase + D40_DREG_LCPA);
3558
3559 base->lcpa_base = ioremap(res->start, resource_size(res));
3560 if (!base->lcpa_base) {
3561 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003562 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003563 goto failure;
3564 }
Narayanan G28c7a192011-11-22 13:56:55 +05303565 /* If lcla has to be located in ESRAM we don't need to allocate */
3566 if (base->plat_data->use_esram_lcla) {
3567 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3568 "lcla_esram");
3569 if (!res) {
3570 ret = -ENOENT;
3571 d40_err(&pdev->dev,
3572 "No \"lcla_esram\" memory resource\n");
3573 goto failure;
3574 }
3575 base->lcla_pool.base = ioremap(res->start,
3576 resource_size(res));
3577 if (!base->lcla_pool.base) {
3578 ret = -ENOMEM;
3579 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3580 goto failure;
3581 }
3582 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003583
Narayanan G28c7a192011-11-22 13:56:55 +05303584 } else {
3585 ret = d40_lcla_allocate(base);
3586 if (ret) {
3587 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3588 goto failure;
3589 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003590 }
3591
Linus Walleij8d318a52010-03-30 15:33:42 +02003592 spin_lock_init(&base->lcla_pool.lock);
3593
Linus Walleij8d318a52010-03-30 15:33:42 +02003594 base->irq = platform_get_irq(pdev, 0);
3595
3596 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003597 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003598 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003599 goto failure;
3600 }
3601
Narayanan G7fb3e752011-11-17 17:26:41 +05303602 pm_runtime_irq_safe(base->dev);
3603 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3604 pm_runtime_use_autosuspend(base->dev);
3605 pm_runtime_enable(base->dev);
3606 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303607
3608 if (base->plat_data->use_esram_lcla) {
3609
3610 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3611 if (IS_ERR(base->lcpa_regulator)) {
3612 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3613 base->lcpa_regulator = NULL;
3614 goto failure;
3615 }
3616
3617 ret = regulator_enable(base->lcpa_regulator);
3618 if (ret) {
3619 d40_err(&pdev->dev,
3620 "Failed to enable lcpa_regulator\n");
3621 regulator_put(base->lcpa_regulator);
3622 base->lcpa_regulator = NULL;
3623 goto failure;
3624 }
3625 }
3626
Narayanan G7fb3e752011-11-17 17:26:41 +05303627 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003628 err = d40_dmaengine_init(base, num_reserved_chans);
3629 if (err)
3630 goto failure;
3631
Per Forlinb96710e2011-10-18 18:39:47 +02003632 base->dev->dma_parms = &base->dma_parms;
3633 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3634 if (err) {
3635 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3636 goto failure;
3637 }
3638
Linus Walleij8d318a52010-03-30 15:33:42 +02003639 d40_hw_init(base);
3640
3641 dev_info(base->dev, "initialized\n");
3642 return 0;
3643
3644failure:
3645 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003646 if (base->desc_slab)
3647 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003648 if (base->virtbase)
3649 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003650
Narayanan G28c7a192011-11-22 13:56:55 +05303651 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3652 iounmap(base->lcla_pool.base);
3653 base->lcla_pool.base = NULL;
3654 }
3655
Rabin Vincent026cbc42011-01-25 11:18:14 +01003656 if (base->lcla_pool.dma_addr)
3657 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3658 SZ_1K * base->num_phy_chans,
3659 DMA_TO_DEVICE);
3660
Linus Walleij508849a2010-06-20 21:26:07 +00003661 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3662 free_pages((unsigned long)base->lcla_pool.base,
3663 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003664
3665 kfree(base->lcla_pool.base_unaligned);
3666
Linus Walleij8d318a52010-03-30 15:33:42 +02003667 if (base->phy_lcpa)
3668 release_mem_region(base->phy_lcpa,
3669 base->lcpa_size);
3670 if (base->phy_start)
3671 release_mem_region(base->phy_start,
3672 base->phy_size);
3673 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003674 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003675 clk_put(base->clk);
3676 }
3677
Narayanan G28c7a192011-11-22 13:56:55 +05303678 if (base->lcpa_regulator) {
3679 regulator_disable(base->lcpa_regulator);
3680 regulator_put(base->lcpa_regulator);
3681 }
3682
Linus Walleij8d318a52010-03-30 15:33:42 +02003683 kfree(base->lcla_pool.alloc_map);
3684 kfree(base->lookup_log_chans);
3685 kfree(base->lookup_phy_chans);
3686 kfree(base->phy_res);
3687 kfree(base);
3688 }
3689
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003690 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003691 return ret;
3692}
3693
Lee Jones1814a172013-05-03 15:32:11 +01003694static const struct of_device_id d40_match[] = {
3695 { .compatible = "stericsson,dma40", },
3696 {}
3697};
3698
Linus Walleij8d318a52010-03-30 15:33:42 +02003699static struct platform_driver d40_driver = {
3700 .driver = {
3701 .owner = THIS_MODULE,
3702 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303703 .pm = DMA40_PM_OPS,
Lee Jones1814a172013-05-03 15:32:11 +01003704 .of_match_table = d40_match,
Linus Walleij8d318a52010-03-30 15:33:42 +02003705 },
3706};
3707
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003708static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003709{
3710 return platform_driver_probe(&d40_driver, d40_probe);
3711}
Linus Walleija0eb2212011-05-18 14:18:57 +02003712subsys_initcall(stedma40_init);