blob: ba84df8fc9d8b0365255bae01a2faf71a1fb2e56 [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>
Lee Jonesfa332de2013-05-03 15:32:12 +010021#include <linux/of_dma.h>
Linus Walleijf4b89762011-06-27 11:33:46 +020022#include <linux/amba/bus.h>
Linus Walleij15e4b782012-04-12 18:12:43 +020023#include <linux/regulator/consumer.h>
Linus Walleij865fab62012-10-18 14:20:16 +020024#include <linux/platform_data/dma-ste-dma40.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020025
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000026#include "dmaengine.h"
Linus Walleij8d318a52010-03-30 15:33:42 +020027#include "ste_dma40_ll.h"
28
29#define D40_NAME "dma40"
30
31#define D40_PHY_CHAN -1
32
33/* For masking out/in 2 bit channel positions */
34#define D40_CHAN_POS(chan) (2 * (chan / 2))
35#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
36
37/* Maximum iterations taken before giving up suspending a channel */
38#define D40_SUSPEND_MAX_IT 500
39
Narayanan G7fb3e752011-11-17 17:26:41 +053040/* Milliseconds */
41#define DMA40_AUTOSUSPEND_DELAY 100
42
Linus Walleij508849a2010-06-20 21:26:07 +000043/* Hardware requirement on LCLA alignment */
44#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000045
46/* Max number of links per event group */
47#define D40_LCLA_LINK_PER_EVENT_GRP 128
48#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
49
Lee Jonesdb72da92013-05-03 15:32:03 +010050/* Max number of logical channels per physical channel */
51#define D40_MAX_LOG_CHAN_PER_PHY 32
52
Linus Walleij508849a2010-06-20 21:26:07 +000053/* Attempts before giving up to trying to get pages that are aligned */
54#define MAX_LCLA_ALLOC_ATTEMPTS 256
55
56/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020057#define D40_ALLOC_FREE (1 << 31)
58#define D40_ALLOC_PHY (1 << 30)
59#define D40_ALLOC_LOG_FREE 0
60
Lee Jones664a57e2013-05-03 15:31:53 +010061/* Reserved event lines for memcpy only. */
Linus Walleija2acaa22013-05-03 21:46:09 +020062#define DB8500_DMA_MEMCPY_EV_0 51
63#define DB8500_DMA_MEMCPY_EV_1 56
64#define DB8500_DMA_MEMCPY_EV_2 57
65#define DB8500_DMA_MEMCPY_EV_3 58
66#define DB8500_DMA_MEMCPY_EV_4 59
67#define DB8500_DMA_MEMCPY_EV_5 60
68
69static int dma40_memcpy_channels[] = {
70 DB8500_DMA_MEMCPY_EV_0,
71 DB8500_DMA_MEMCPY_EV_1,
72 DB8500_DMA_MEMCPY_EV_2,
73 DB8500_DMA_MEMCPY_EV_3,
74 DB8500_DMA_MEMCPY_EV_4,
75 DB8500_DMA_MEMCPY_EV_5,
76};
Lee Jones664a57e2013-05-03 15:31:53 +010077
Lee Jones29027a12013-05-03 15:31:54 +010078/* Default configuration for physcial memcpy */
79struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
80 .mode = STEDMA40_MODE_PHYSICAL,
81 .dir = STEDMA40_MEM_TO_MEM,
82
83 .src_info.data_width = STEDMA40_BYTE_WIDTH,
84 .src_info.psize = STEDMA40_PSIZE_PHY_1,
85 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
86
87 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
88 .dst_info.psize = STEDMA40_PSIZE_PHY_1,
89 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
90};
91
92/* Default configuration for logical memcpy */
93struct stedma40_chan_cfg dma40_memcpy_conf_log = {
94 .mode = STEDMA40_MODE_LOGICAL,
95 .dir = STEDMA40_MEM_TO_MEM,
96
97 .src_info.data_width = STEDMA40_BYTE_WIDTH,
98 .src_info.psize = STEDMA40_PSIZE_LOG_1,
99 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
100
101 .dst_info.data_width = STEDMA40_BYTE_WIDTH,
102 .dst_info.psize = STEDMA40_PSIZE_LOG_1,
103 .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
104};
105
Linus Walleij8d318a52010-03-30 15:33:42 +0200106/**
107 * enum 40_command - The different commands and/or statuses.
108 *
109 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
110 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
111 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
112 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
113 */
114enum d40_command {
115 D40_DMA_STOP = 0,
116 D40_DMA_RUN = 1,
117 D40_DMA_SUSPEND_REQ = 2,
118 D40_DMA_SUSPENDED = 3
119};
120
Narayanan G7fb3e752011-11-17 17:26:41 +0530121/*
Narayanan G1bdae6f2012-02-09 12:41:37 +0530122 * enum d40_events - The different Event Enables for the event lines.
123 *
124 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
125 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
126 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
127 * @D40_ROUND_EVENTLINE: Status check for event line.
128 */
129
130enum d40_events {
131 D40_DEACTIVATE_EVENTLINE = 0,
132 D40_ACTIVATE_EVENTLINE = 1,
133 D40_SUSPEND_REQ_EVENTLINE = 2,
134 D40_ROUND_EVENTLINE = 3
135};
136
137/*
Narayanan G7fb3e752011-11-17 17:26:41 +0530138 * These are the registers that has to be saved and later restored
139 * when the DMA hw is powered off.
140 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
141 */
142static u32 d40_backup_regs[] = {
143 D40_DREG_LCPA,
144 D40_DREG_LCLA,
145 D40_DREG_PRMSE,
146 D40_DREG_PRMSO,
147 D40_DREG_PRMOE,
148 D40_DREG_PRMOO,
149};
150
151#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
152
Tong Liu3cb645d2012-09-26 10:07:30 +0000153/*
154 * since 9540 and 8540 has the same HW revision
155 * use v4a for 9540 or ealier
156 * use v4b for 8540 or later
157 * HW revision:
158 * DB8500ed has revision 0
159 * DB8500v1 has revision 2
160 * DB8500v2 has revision 3
161 * AP9540v1 has revision 4
162 * DB8540v1 has revision 4
163 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
164 */
165static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530166 D40_DREG_PSEG1,
167 D40_DREG_PSEG2,
168 D40_DREG_PSEG3,
169 D40_DREG_PSEG4,
170 D40_DREG_PCEG1,
171 D40_DREG_PCEG2,
172 D40_DREG_PCEG3,
173 D40_DREG_PCEG4,
174 D40_DREG_RSEG1,
175 D40_DREG_RSEG2,
176 D40_DREG_RSEG3,
177 D40_DREG_RSEG4,
178 D40_DREG_RCEG1,
179 D40_DREG_RCEG2,
180 D40_DREG_RCEG3,
181 D40_DREG_RCEG4,
182};
183
Tong Liu3cb645d2012-09-26 10:07:30 +0000184#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
185
186static u32 d40_backup_regs_v4b[] = {
187 D40_DREG_CPSEG1,
188 D40_DREG_CPSEG2,
189 D40_DREG_CPSEG3,
190 D40_DREG_CPSEG4,
191 D40_DREG_CPSEG5,
192 D40_DREG_CPCEG1,
193 D40_DREG_CPCEG2,
194 D40_DREG_CPCEG3,
195 D40_DREG_CPCEG4,
196 D40_DREG_CPCEG5,
197 D40_DREG_CRSEG1,
198 D40_DREG_CRSEG2,
199 D40_DREG_CRSEG3,
200 D40_DREG_CRSEG4,
201 D40_DREG_CRSEG5,
202 D40_DREG_CRCEG1,
203 D40_DREG_CRCEG2,
204 D40_DREG_CRCEG3,
205 D40_DREG_CRCEG4,
206 D40_DREG_CRCEG5,
207};
208
209#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530210
211static u32 d40_backup_regs_chan[] = {
212 D40_CHAN_REG_SSCFG,
213 D40_CHAN_REG_SSELT,
214 D40_CHAN_REG_SSPTR,
215 D40_CHAN_REG_SSLNK,
216 D40_CHAN_REG_SDCFG,
217 D40_CHAN_REG_SDELT,
218 D40_CHAN_REG_SDPTR,
219 D40_CHAN_REG_SDLNK,
220};
221
Lee Jones84b3da12013-05-03 15:31:58 +0100222#define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
223 BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
224
Linus Walleij8d318a52010-03-30 15:33:42 +0200225/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000226 * struct d40_interrupt_lookup - lookup table for interrupt handler
227 *
228 * @src: Interrupt mask register.
229 * @clr: Interrupt clear register.
230 * @is_error: true if this is an error interrupt.
231 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
232 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
233 */
234struct d40_interrupt_lookup {
235 u32 src;
236 u32 clr;
237 bool is_error;
238 int offset;
239};
240
241
242static struct d40_interrupt_lookup il_v4a[] = {
243 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
244 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
245 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
246 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
247 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
248 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
249 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
250 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
251 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
252 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
253};
254
255static struct d40_interrupt_lookup il_v4b[] = {
256 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
257 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
258 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
259 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
260 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
261 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
262 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
263 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
264 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
265 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
266 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
267 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
268};
269
270/**
271 * struct d40_reg_val - simple lookup struct
272 *
273 * @reg: The register.
274 * @val: The value that belongs to the register in reg.
275 */
276struct d40_reg_val {
277 unsigned int reg;
278 unsigned int val;
279};
280
281static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
282 /* Clock every part of the DMA block from start */
283 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
284
285 /* Interrupts on all logical channels */
286 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
287 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
288 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
289 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
290 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
291 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
292 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
293 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
294 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
295 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
296 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
297 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
298};
299static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
300 /* Clock every part of the DMA block from start */
301 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
302
303 /* Interrupts on all logical channels */
304 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
305 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
306 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
307 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
308 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
309 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
310 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
311 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
312 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
313 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
314 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
315 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
316 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
317 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
318 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
319};
320
321/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200322 * struct d40_lli_pool - Structure for keeping LLIs in memory
323 *
324 * @base: Pointer to memory area when the pre_alloc_lli's are not large
325 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
326 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100327 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200328 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
329 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
330 * one buffer to one buffer.
331 */
332struct d40_lli_pool {
333 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000334 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100335 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200336 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000337 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200338};
339
340/**
341 * struct d40_desc - A descriptor is one DMA job.
342 *
343 * @lli_phy: LLI settings for physical channel. Both src and dst=
344 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
345 * lli_len equals one.
346 * @lli_log: Same as above but for logical channels.
347 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000348 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300349 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000350 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200351 * @txd: DMA engine struct. Used for among other things for communication
352 * during a transfer.
353 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200354 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530355 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200356 *
357 * This descriptor is used for both logical and physical transfers.
358 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200359struct d40_desc {
360 /* LLI physical */
361 struct d40_phy_lli_bidir lli_phy;
362 /* LLI logical */
363 struct d40_log_lli_bidir lli_log;
364
365 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000366 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000367 int lli_current;
368 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200369
370 struct dma_async_tx_descriptor txd;
371 struct list_head node;
372
Linus Walleij8d318a52010-03-30 15:33:42 +0200373 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100374 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200375};
376
377/**
378 * struct d40_lcla_pool - LCLA pool settings and data.
379 *
Linus Walleij508849a2010-06-20 21:26:07 +0000380 * @base: The virtual address of LCLA. 18 bit aligned.
381 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
382 * This pointer is only there for clean-up on error.
383 * @pages: The number of pages needed for all physical channels.
384 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200385 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000386 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200387 */
388struct d40_lcla_pool {
389 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100390 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000391 void *base_unaligned;
392 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200393 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000394 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200395};
396
397/**
398 * struct d40_phy_res - struct for handling eventlines mapped to physical
399 * channels.
400 *
401 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530402 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200403 * @num: The physical channel number of this entity.
404 * @allocated_src: Bit mapped to show which src event line's are mapped to
405 * this physical channel. Can also be free or physically allocated.
406 * @allocated_dst: Same as for src but is dst.
407 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000408 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100409 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200410 */
411struct d40_phy_res {
412 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530413 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200414 int num;
415 u32 allocated_src;
416 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100417 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200418};
419
420struct d40_base;
421
422/**
423 * struct d40_chan - Struct that describes a channel.
424 *
425 * @lock: A spinlock to protect this struct.
426 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200427 * @pending_tx: The number of pending transfers. Used between interrupt handler
428 * and tasklet.
429 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000430 * @phy_chan: Pointer to physical channel which this instance runs on. If this
431 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200432 * @chan: DMA engine handle.
433 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
434 * transfer and call client callback.
435 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200436 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200437 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100438 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200439 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200440 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200441 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000442 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200443 * @base: Pointer to the device instance struct.
444 * @src_def_cfg: Default cfg register setting for src.
445 * @dst_def_cfg: Default cfg register setting for dst.
446 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200447 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200448 * @runtime_addr: runtime configured address.
449 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200450 *
451 * This struct can either "be" a logical or a physical channel.
452 */
453struct d40_chan {
454 spinlock_t lock;
455 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200456 int pending_tx;
457 bool busy;
458 struct d40_phy_res *phy_chan;
459 struct dma_chan chan;
460 struct tasklet_struct tasklet;
461 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200462 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200463 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100464 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200465 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200466 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200467 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000468 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200469 struct d40_base *base;
470 /* Default register configurations */
471 u32 src_def_cfg;
472 u32 dst_def_cfg;
473 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200474 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200475 /* Runtime reconfiguration */
476 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530477 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200478};
479
480/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000481 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
482 * controller
483 *
484 * @backup: the pointer to the registers address array for backup
485 * @backup_size: the size of the registers address array for backup
486 * @realtime_en: the realtime enable register
487 * @realtime_clear: the realtime clear register
488 * @high_prio_en: the high priority enable register
489 * @high_prio_clear: the high priority clear register
490 * @interrupt_en: the interrupt enable register
491 * @interrupt_clear: the interrupt clear register
492 * @il: the pointer to struct d40_interrupt_lookup
493 * @il_size: the size of d40_interrupt_lookup array
494 * @init_reg: the pointer to the struct d40_reg_val
495 * @init_reg_size: the size of d40_reg_val array
496 */
497struct d40_gen_dmac {
498 u32 *backup;
499 u32 backup_size;
500 u32 realtime_en;
501 u32 realtime_clear;
502 u32 high_prio_en;
503 u32 high_prio_clear;
504 u32 interrupt_en;
505 u32 interrupt_clear;
506 struct d40_interrupt_lookup *il;
507 u32 il_size;
508 struct d40_reg_val *init_reg;
509 u32 init_reg_size;
510};
511
512/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200513 * struct d40_base - The big global struct, one for each probe'd instance.
514 *
515 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
516 * @execmd_lock: Lock for execute command usage since several channels share
517 * the same physical register.
518 * @dev: The device structure.
519 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700520 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200521 * @clk: Pointer to the DMA clock structure.
522 * @phy_start: Physical memory start of the DMA registers.
523 * @phy_size: Size of the DMA register map.
524 * @irq: The IRQ number.
525 * @num_phy_chans: The number of physical channels. Read from HW. This
526 * is the number of available channels for this driver, not counting "Secure
527 * mode" allocated physical channels.
528 * @num_log_chans: The number of logical channels. Calculated from
529 * num_phy_chans.
530 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
531 * @dma_slave: dma_device channels that can do only do slave transfers.
532 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530533 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200534 * @log_chans: Room for all possible logical channels in system.
535 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
536 * to log_chans entries.
537 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
538 * to phy_chans entries.
539 * @plat_data: Pointer to provided platform_data which is the driver
540 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530541 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200542 * @phy_res: Vector containing all physical channels.
543 * @lcla_pool: lcla pool settings and data.
544 * @lcpa_base: The virtual mapped address of LCPA.
545 * @phy_lcpa: The physical address of the LCPA.
546 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000547 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530548 * @reg_val_backup: Here the values of some hardware registers are stored
549 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000550 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
551 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530552 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
553 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
554 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000555 * @gen_dmac: the struct for generic registers values to represent u8500/8540
556 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200557 */
558struct d40_base {
559 spinlock_t interrupt_lock;
560 spinlock_t execmd_lock;
561 struct device *dev;
562 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700563 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200564 struct clk *clk;
565 phys_addr_t phy_start;
566 resource_size_t phy_size;
567 int irq;
568 int num_phy_chans;
569 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200570 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200571 struct dma_device dma_both;
572 struct dma_device dma_slave;
573 struct dma_device dma_memcpy;
574 struct d40_chan *phy_chans;
575 struct d40_chan *log_chans;
576 struct d40_chan **lookup_log_chans;
577 struct d40_chan **lookup_phy_chans;
578 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530579 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200580 /* Physical half channels */
581 struct d40_phy_res *phy_res;
582 struct d40_lcla_pool lcla_pool;
583 void *lcpa_base;
584 dma_addr_t phy_lcpa;
585 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000586 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530587 u32 reg_val_backup[BACKUP_REGS_SZ];
Lee Jones84b3da12013-05-03 15:31:58 +0100588 u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
Narayanan G7fb3e752011-11-17 17:26:41 +0530589 u32 *reg_val_backup_chan;
590 u16 gcc_pwr_off_mask;
591 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000592 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200593};
594
Rabin Vincent262d2912011-01-25 11:18:05 +0100595static struct device *chan2dev(struct d40_chan *d40c)
596{
597 return &d40c->chan.dev->device;
598}
599
Rabin Vincent724a8572011-01-25 11:18:08 +0100600static bool chan_is_physical(struct d40_chan *chan)
601{
602 return chan->log_num == D40_PHY_CHAN;
603}
604
605static bool chan_is_logical(struct d40_chan *chan)
606{
607 return !chan_is_physical(chan);
608}
609
Rabin Vincent8ca84682011-01-25 11:18:07 +0100610static void __iomem *chan_base(struct d40_chan *chan)
611{
612 return chan->base->virtbase + D40_DREG_PCBASE +
613 chan->phy_chan->num * D40_DREG_PCDELTA;
614}
615
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100616#define d40_err(dev, format, arg...) \
617 dev_err(dev, "[%s] " format, __func__, ## arg)
618
619#define chan_err(d40c, format, arg...) \
620 d40_err(chan2dev(d40c), format, ## arg)
621
Rabin Vincentb00f9382011-01-25 11:18:15 +0100622static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100623 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200624{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100625 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200626 u32 align;
627 void *base;
628
629 if (is_log)
630 align = sizeof(struct d40_log_lli);
631 else
632 align = sizeof(struct d40_phy_lli);
633
634 if (lli_len == 1) {
635 base = d40d->lli_pool.pre_alloc_lli;
636 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
637 d40d->lli_pool.base = NULL;
638 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100639 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200640
641 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
642 d40d->lli_pool.base = base;
643
644 if (d40d->lli_pool.base == NULL)
645 return -ENOMEM;
646 }
647
648 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100649 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100650 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100651
652 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200653 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100654 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100655 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100656
657 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
658 d40d->lli_phy.src,
659 d40d->lli_pool.size,
660 DMA_TO_DEVICE);
661
662 if (dma_mapping_error(d40c->base->dev,
663 d40d->lli_pool.dma_addr)) {
664 kfree(d40d->lli_pool.base);
665 d40d->lli_pool.base = NULL;
666 d40d->lli_pool.dma_addr = 0;
667 return -ENOMEM;
668 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200669 }
670
671 return 0;
672}
673
Rabin Vincentb00f9382011-01-25 11:18:15 +0100674static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200675{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100676 if (d40d->lli_pool.dma_addr)
677 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
678 d40d->lli_pool.size, DMA_TO_DEVICE);
679
Linus Walleij8d318a52010-03-30 15:33:42 +0200680 kfree(d40d->lli_pool.base);
681 d40d->lli_pool.base = NULL;
682 d40d->lli_pool.size = 0;
683 d40d->lli_log.src = NULL;
684 d40d->lli_log.dst = NULL;
685 d40d->lli_phy.src = NULL;
686 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200687}
688
Jonas Aaberg698e4732010-08-09 12:08:56 +0000689static int d40_lcla_alloc_one(struct d40_chan *d40c,
690 struct d40_desc *d40d)
691{
692 unsigned long flags;
693 int i;
694 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000695
696 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
697
Jonas Aaberg698e4732010-08-09 12:08:56 +0000698 /*
699 * Allocate both src and dst at the same time, therefore the half
700 * start on 1 since 0 can't be used since zero is used as end marker.
701 */
702 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100703 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
704
705 if (!d40c->base->lcla_pool.alloc_map[idx]) {
706 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000707 d40d->lcla_alloc++;
708 ret = i;
709 break;
710 }
711 }
712
713 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
714
715 return ret;
716}
717
718static int d40_lcla_free_all(struct d40_chan *d40c,
719 struct d40_desc *d40d)
720{
721 unsigned long flags;
722 int i;
723 int ret = -EINVAL;
724
Rabin Vincent724a8572011-01-25 11:18:08 +0100725 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000726 return 0;
727
728 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
729
730 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100731 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
732
733 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
734 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000735 d40d->lcla_alloc--;
736 if (d40d->lcla_alloc == 0) {
737 ret = 0;
738 break;
739 }
740 }
741 }
742
743 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
744
745 return ret;
746
747}
748
Linus Walleij8d318a52010-03-30 15:33:42 +0200749static void d40_desc_remove(struct d40_desc *d40d)
750{
751 list_del(&d40d->node);
752}
753
754static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
755{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000756 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200757
758 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000759 struct d40_desc *d;
760 struct d40_desc *_d;
761
Narayanan G7fb3e752011-11-17 17:26:41 +0530762 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200763 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200764 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000765 desc = d;
766 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000767 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200768 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530769 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200770 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000771
772 if (!desc)
773 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
774
775 if (desc)
776 INIT_LIST_HEAD(&desc->node);
777
778 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200779}
780
781static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
782{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000783
Rabin Vincentb00f9382011-01-25 11:18:15 +0100784 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000785 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000786 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200787}
788
789static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
790{
791 list_add_tail(&desc->node, &d40c->active);
792}
793
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100794static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
795{
796 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
797 struct d40_phy_lli *lli_src = desc->lli_phy.src;
798 void __iomem *base = chan_base(chan);
799
800 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
801 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
802 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
803 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
804
805 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
806 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
807 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
808 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
809}
810
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100811static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
812{
813 list_add_tail(&desc->node, &d40c->done);
814}
815
Rabin Vincente65889c2011-01-25 11:18:31 +0100816static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
817{
818 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
819 struct d40_log_lli_bidir *lli = &desc->lli_log;
820 int lli_current = desc->lli_current;
821 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100822 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100823 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100824 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530825 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100826 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100827
Rabin Vincent0c842b52011-01-25 11:18:35 +0100828 /*
829 * We may have partially running cyclic transfers, in case we did't get
830 * enough LCLA entries.
831 */
832 linkback = cyclic && lli_current == 0;
833
834 /*
835 * For linkback, we need one LCLA even with only one link, because we
836 * can't link back to the one in LCPA space
837 */
838 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100839 /*
840 * If the channel is expected to use only soft_lli don't
841 * allocate a lcla. This is to avoid a HW issue that exists
842 * in some controller during a peripheral to memory transfer
843 * that uses linked lists.
844 */
845 if (!(chan->phy_chan->use_soft_lli &&
846 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
847 curr_lcla = d40_lcla_alloc_one(chan, desc);
848
Rabin Vincent0c842b52011-01-25 11:18:35 +0100849 first_lcla = curr_lcla;
850 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100851
Rabin Vincent0c842b52011-01-25 11:18:35 +0100852 /*
853 * For linkback, we normally load the LCPA in the loop since we need to
854 * link it to the second LCLA and not the first. However, if we
855 * couldn't even get a first LCLA, then we have to run in LCPA and
856 * reload manually.
857 */
858 if (!linkback || curr_lcla == -EINVAL) {
859 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100860
Rabin Vincent0c842b52011-01-25 11:18:35 +0100861 if (curr_lcla == -EINVAL)
862 flags |= LLI_TERM_INT;
863
864 d40_log_lli_lcpa_write(chan->lcpa,
865 &lli->dst[lli_current],
866 &lli->src[lli_current],
867 curr_lcla,
868 flags);
869 lli_current++;
870 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100871
872 if (curr_lcla < 0)
873 goto out;
874
Rabin Vincente65889c2011-01-25 11:18:31 +0100875 for (; lli_current < lli_len; lli_current++) {
876 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
877 8 * curr_lcla * 2;
878 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100879 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100880 int next_lcla;
881
882 if (lli_current + 1 < lli_len)
883 next_lcla = d40_lcla_alloc_one(chan, desc);
884 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100885 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100886
Rabin Vincent0c842b52011-01-25 11:18:35 +0100887 if (cyclic || next_lcla == -EINVAL)
888 flags |= LLI_TERM_INT;
889
890 if (linkback && curr_lcla == first_lcla) {
891 /* First link goes in both LCPA and LCLA */
892 d40_log_lli_lcpa_write(chan->lcpa,
893 &lli->dst[lli_current],
894 &lli->src[lli_current],
895 next_lcla, flags);
896 }
897
898 /*
899 * One unused LCLA in the cyclic case if the very first
900 * next_lcla fails...
901 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100902 d40_log_lli_lcla_write(lcla,
903 &lli->dst[lli_current],
904 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100905 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100906
Narayanan G28c7a192011-11-22 13:56:55 +0530907 /*
908 * Cache maintenance is not needed if lcla is
909 * mapped in esram
910 */
911 if (!use_esram_lcla) {
912 dma_sync_single_range_for_device(chan->base->dev,
913 pool->dma_addr, lcla_offset,
914 2 * sizeof(struct d40_log_lli),
915 DMA_TO_DEVICE);
916 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100917 curr_lcla = next_lcla;
918
Rabin Vincent0c842b52011-01-25 11:18:35 +0100919 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100920 lli_current++;
921 break;
922 }
923 }
924
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100925out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100926 desc->lli_current = lli_current;
927}
928
Jonas Aaberg698e4732010-08-09 12:08:56 +0000929static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
930{
Rabin Vincent724a8572011-01-25 11:18:08 +0100931 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100932 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000933 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100934 } else
935 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000936}
937
Linus Walleij8d318a52010-03-30 15:33:42 +0200938static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
939{
940 struct d40_desc *d;
941
942 if (list_empty(&d40c->active))
943 return NULL;
944
945 d = list_first_entry(&d40c->active,
946 struct d40_desc,
947 node);
948 return d;
949}
950
Per Forlin74043682011-08-29 13:33:34 +0200951/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200952static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
953{
Per Forlin74043682011-08-29 13:33:34 +0200954 d40_desc_remove(desc);
955 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200956 list_add_tail(&desc->node, &d40c->pending_queue);
957}
958
959static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
960{
961 struct d40_desc *d;
962
963 if (list_empty(&d40c->pending_queue))
964 return NULL;
965
966 d = list_first_entry(&d40c->pending_queue,
967 struct d40_desc,
968 node);
969 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200970}
971
972static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
973{
974 struct d40_desc *d;
975
976 if (list_empty(&d40c->queue))
977 return NULL;
978
979 d = list_first_entry(&d40c->queue,
980 struct d40_desc,
981 node);
982 return d;
983}
984
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100985static struct d40_desc *d40_first_done(struct d40_chan *d40c)
986{
987 if (list_empty(&d40c->done))
988 return NULL;
989
990 return list_first_entry(&d40c->done, struct d40_desc, node);
991}
992
Per Forlind49278e2010-12-20 18:31:38 +0100993static int d40_psize_2_burst_size(bool is_log, int psize)
994{
995 if (is_log) {
996 if (psize == STEDMA40_PSIZE_LOG_1)
997 return 1;
998 } else {
999 if (psize == STEDMA40_PSIZE_PHY_1)
1000 return 1;
1001 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001002
Per Forlind49278e2010-12-20 18:31:38 +01001003 return 2 << psize;
1004}
1005
1006/*
1007 * The dma only supports transmitting packages up to
1008 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
1009 * dma elements required to send the entire sg list
1010 */
1011static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1012{
1013 int dmalen;
1014 u32 max_w = max(data_width1, data_width2);
1015 u32 min_w = min(data_width1, data_width2);
1016 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
1017
1018 if (seg_max > STEDMA40_MAX_SEG_SIZE)
1019 seg_max -= (1 << max_w);
1020
1021 if (!IS_ALIGNED(size, 1 << max_w))
1022 return -EINVAL;
1023
1024 if (size <= seg_max)
1025 dmalen = 1;
1026 else {
1027 dmalen = size / seg_max;
1028 if (dmalen * seg_max < size)
1029 dmalen++;
1030 }
1031 return dmalen;
1032}
1033
1034static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1035 u32 data_width1, u32 data_width2)
1036{
1037 struct scatterlist *sg;
1038 int i;
1039 int len = 0;
1040 int ret;
1041
1042 for_each_sg(sgl, sg, sg_len, i) {
1043 ret = d40_size_2_dmalen(sg_dma_len(sg),
1044 data_width1, data_width2);
1045 if (ret < 0)
1046 return ret;
1047 len += ret;
1048 }
1049 return len;
1050}
1051
Narayanan G7fb3e752011-11-17 17:26:41 +05301052
1053#ifdef CONFIG_PM
1054static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1055 u32 *regaddr, int num, bool save)
1056{
1057 int i;
1058
1059 for (i = 0; i < num; i++) {
1060 void __iomem *addr = baseaddr + regaddr[i];
1061
1062 if (save)
1063 backup[i] = readl_relaxed(addr);
1064 else
1065 writel_relaxed(backup[i], addr);
1066 }
1067}
1068
1069static void d40_save_restore_registers(struct d40_base *base, bool save)
1070{
1071 int i;
1072
1073 /* Save/Restore channel specific registers */
1074 for (i = 0; i < base->num_phy_chans; i++) {
1075 void __iomem *addr;
1076 int idx;
1077
1078 if (base->phy_res[i].reserved)
1079 continue;
1080
1081 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1082 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1083
1084 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1085 d40_backup_regs_chan,
1086 ARRAY_SIZE(d40_backup_regs_chan),
1087 save);
1088 }
1089
1090 /* Save/Restore global registers */
1091 dma40_backup(base->virtbase, base->reg_val_backup,
1092 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1093 save);
1094
1095 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001096 if (base->gen_dmac.backup)
1097 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1098 base->gen_dmac.backup,
1099 base->gen_dmac.backup_size,
1100 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301101}
1102#else
1103static void d40_save_restore_registers(struct d40_base *base, bool save)
1104{
1105}
1106#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001107
Narayanan G1bdae6f2012-02-09 12:41:37 +05301108static int __d40_execute_command_phy(struct d40_chan *d40c,
1109 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001110{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001111 u32 status;
1112 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001113 void __iomem *active_reg;
1114 int ret = 0;
1115 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001116 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001117
Narayanan G1bdae6f2012-02-09 12:41:37 +05301118 if (command == D40_DMA_STOP) {
1119 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1120 if (ret)
1121 return ret;
1122 }
1123
Linus Walleij8d318a52010-03-30 15:33:42 +02001124 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1125
1126 if (d40c->phy_chan->num % 2 == 0)
1127 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1128 else
1129 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1130
1131 if (command == D40_DMA_SUSPEND_REQ) {
1132 status = (readl(active_reg) &
1133 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1134 D40_CHAN_POS(d40c->phy_chan->num);
1135
1136 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1137 goto done;
1138 }
1139
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001140 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1141 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1142 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001143
1144 if (command == D40_DMA_SUSPEND_REQ) {
1145
1146 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1147 status = (readl(active_reg) &
1148 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1149 D40_CHAN_POS(d40c->phy_chan->num);
1150
1151 cpu_relax();
1152 /*
1153 * Reduce the number of bus accesses while
1154 * waiting for the DMA to suspend.
1155 */
1156 udelay(3);
1157
1158 if (status == D40_DMA_STOP ||
1159 status == D40_DMA_SUSPENDED)
1160 break;
1161 }
1162
1163 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001164 chan_err(d40c,
1165 "unable to suspend the chl %d (log: %d) status %x\n",
1166 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001167 status);
1168 dump_stack();
1169 ret = -EBUSY;
1170 }
1171
1172 }
1173done:
1174 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1175 return ret;
1176}
1177
1178static void d40_term_all(struct d40_chan *d40c)
1179{
1180 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001181 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001182
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001183 /* Release completed descriptors */
1184 while ((d40d = d40_first_done(d40c))) {
1185 d40_desc_remove(d40d);
1186 d40_desc_free(d40c, d40d);
1187 }
1188
Linus Walleij8d318a52010-03-30 15:33:42 +02001189 /* Release active descriptors */
1190 while ((d40d = d40_first_active_get(d40c))) {
1191 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001192 d40_desc_free(d40c, d40d);
1193 }
1194
1195 /* Release queued descriptors waiting for transfer */
1196 while ((d40d = d40_first_queued(d40c))) {
1197 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001198 d40_desc_free(d40c, d40d);
1199 }
1200
Per Forlina8f30672011-06-26 23:29:52 +02001201 /* Release pending descriptors */
1202 while ((d40d = d40_first_pending(d40c))) {
1203 d40_desc_remove(d40d);
1204 d40_desc_free(d40c, d40d);
1205 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001206
Per Forlin74043682011-08-29 13:33:34 +02001207 /* Release client owned descriptors */
1208 if (!list_empty(&d40c->client))
1209 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1210 d40_desc_remove(d40d);
1211 d40_desc_free(d40c, d40d);
1212 }
1213
Per Forlin82babbb362011-08-29 13:33:35 +02001214 /* Release descriptors in prepare queue */
1215 if (!list_empty(&d40c->prepare_queue))
1216 list_for_each_entry_safe(d40d, _d,
1217 &d40c->prepare_queue, node) {
1218 d40_desc_remove(d40d);
1219 d40_desc_free(d40c, d40d);
1220 }
Per Forlin74043682011-08-29 13:33:34 +02001221
Linus Walleij8d318a52010-03-30 15:33:42 +02001222 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001223}
1224
Narayanan G1bdae6f2012-02-09 12:41:37 +05301225static void __d40_config_set_event(struct d40_chan *d40c,
1226 enum d40_events event_type, u32 event,
1227 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001228{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001229 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001230 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301231 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001232
Narayanan G1bdae6f2012-02-09 12:41:37 +05301233 switch (event_type) {
1234
1235 case D40_DEACTIVATE_EVENTLINE:
1236
Rabin Vincent262d2912011-01-25 11:18:05 +01001237 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1238 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301239 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001240
Narayanan G1bdae6f2012-02-09 12:41:37 +05301241 case D40_SUSPEND_REQ_EVENTLINE:
1242 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1243 D40_EVENTLINE_POS(event);
1244
1245 if (status == D40_DEACTIVATE_EVENTLINE ||
1246 status == D40_SUSPEND_REQ_EVENTLINE)
1247 break;
1248
1249 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1250 | ~D40_EVENTLINE_MASK(event), addr);
1251
1252 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1253
1254 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1255 D40_EVENTLINE_POS(event);
1256
1257 cpu_relax();
1258 /*
1259 * Reduce the number of bus accesses while
1260 * waiting for the DMA to suspend.
1261 */
1262 udelay(3);
1263
1264 if (status == D40_DEACTIVATE_EVENTLINE)
1265 break;
1266 }
1267
1268 if (tries == D40_SUSPEND_MAX_IT) {
1269 chan_err(d40c,
1270 "unable to stop the event_line chl %d (log: %d)"
1271 "status %x\n", d40c->phy_chan->num,
1272 d40c->log_num, status);
1273 }
1274 break;
1275
1276 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001277 /*
1278 * The hardware sometimes doesn't register the enable when src and dst
1279 * event lines are active on the same logical channel. Retry to ensure
1280 * it does. Usually only one retry is sufficient.
1281 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301282 tries = 100;
1283 while (--tries) {
1284 writel((D40_ACTIVATE_EVENTLINE <<
1285 D40_EVENTLINE_POS(event)) |
1286 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001287
Narayanan G1bdae6f2012-02-09 12:41:37 +05301288 if (readl(addr) & D40_EVENTLINE_MASK(event))
1289 break;
1290 }
1291
1292 if (tries != 99)
1293 dev_dbg(chan2dev(d40c),
1294 "[%s] workaround enable S%cLNK (%d tries)\n",
1295 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1296 100 - tries);
1297
1298 WARN_ON(!tries);
1299 break;
1300
1301 case D40_ROUND_EVENTLINE:
1302 BUG();
1303 break;
1304
Rabin Vincent262d2912011-01-25 11:18:05 +01001305 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001306}
1307
Narayanan G1bdae6f2012-02-09 12:41:37 +05301308static void d40_config_set_event(struct d40_chan *d40c,
1309 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001310{
Lee Jones26955c07d2013-05-03 15:31:56 +01001311 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
1312
Linus Walleij8d318a52010-03-30 15:33:42 +02001313 /* Enable event line connected to device (or memcpy) */
1314 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
Lee Jones26955c07d2013-05-03 15:31:56 +01001315 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Narayanan G1bdae6f2012-02-09 12:41:37 +05301316 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001317 D40_CHAN_REG_SSLNK);
Rabin Vincent262d2912011-01-25 11:18:05 +01001318
Lee Jones26955c07d2013-05-03 15:31:56 +01001319 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM)
Narayanan G1bdae6f2012-02-09 12:41:37 +05301320 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001321 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001322}
1323
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001324static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001325{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001326 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001327 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001328
Rabin Vincent8ca84682011-01-25 11:18:07 +01001329 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1330 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001331
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001332 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001333}
1334
Narayanan G1bdae6f2012-02-09 12:41:37 +05301335static int
1336__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1337{
1338 unsigned long flags;
1339 int ret = 0;
1340 u32 active_status;
1341 void __iomem *active_reg;
1342
1343 if (d40c->phy_chan->num % 2 == 0)
1344 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1345 else
1346 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1347
1348
1349 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1350
1351 switch (command) {
1352 case D40_DMA_STOP:
1353 case D40_DMA_SUSPEND_REQ:
1354
1355 active_status = (readl(active_reg) &
1356 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1357 D40_CHAN_POS(d40c->phy_chan->num);
1358
1359 if (active_status == D40_DMA_RUN)
1360 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1361 else
1362 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1363
1364 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1365 ret = __d40_execute_command_phy(d40c, command);
1366
1367 break;
1368
1369 case D40_DMA_RUN:
1370
1371 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1372 ret = __d40_execute_command_phy(d40c, command);
1373 break;
1374
1375 case D40_DMA_SUSPENDED:
1376 BUG();
1377 break;
1378 }
1379
1380 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1381 return ret;
1382}
1383
1384static int d40_channel_execute_command(struct d40_chan *d40c,
1385 enum d40_command command)
1386{
1387 if (chan_is_logical(d40c))
1388 return __d40_execute_command_log(d40c, command);
1389 else
1390 return __d40_execute_command_phy(d40c, command);
1391}
1392
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001393static u32 d40_get_prmo(struct d40_chan *d40c)
1394{
1395 static const unsigned int phy_map[] = {
1396 [STEDMA40_PCHAN_BASIC_MODE]
1397 = D40_DREG_PRMO_PCHAN_BASIC,
1398 [STEDMA40_PCHAN_MODULO_MODE]
1399 = D40_DREG_PRMO_PCHAN_MODULO,
1400 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1401 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1402 };
1403 static const unsigned int log_map[] = {
1404 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1405 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1406 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1407 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1408 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1409 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1410 };
1411
Rabin Vincent724a8572011-01-25 11:18:08 +01001412 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001413 return phy_map[d40c->dma_cfg.mode_opt];
1414 else
1415 return log_map[d40c->dma_cfg.mode_opt];
1416}
1417
Jonas Aabergb55912c2010-08-09 12:08:02 +00001418static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001419{
1420 u32 addr_base;
1421 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001422
1423 /* Odd addresses are even addresses + 4 */
1424 addr_base = (d40c->phy_chan->num % 2) * 4;
1425 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001426 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001427 D40_CHAN_POS(d40c->phy_chan->num);
1428 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1429
1430 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001431 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001432
1433 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1434
Rabin Vincent724a8572011-01-25 11:18:08 +01001435 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001436 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1437 & D40_SREG_ELEM_LOG_LIDX_MASK;
1438 void __iomem *chanbase = chan_base(d40c);
1439
Linus Walleij8d318a52010-03-30 15:33:42 +02001440 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001441 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1442 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001443
Jonas Aabergb55912c2010-08-09 12:08:02 +00001444 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001445 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1446 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301447
1448 /* Clear LNK which will be used by d40_chan_has_events() */
1449 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1450 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001451 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001452}
1453
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001454static u32 d40_residue(struct d40_chan *d40c)
1455{
1456 u32 num_elt;
1457
Rabin Vincent724a8572011-01-25 11:18:08 +01001458 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001459 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1460 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001461 else {
1462 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1463 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1464 >> D40_SREG_ELEM_PHY_ECNT_POS;
1465 }
1466
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001467 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1468}
1469
1470static bool d40_tx_is_linked(struct d40_chan *d40c)
1471{
1472 bool is_link;
1473
Rabin Vincent724a8572011-01-25 11:18:08 +01001474 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001475 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1476 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001477 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1478 & D40_SREG_LNK_PHYS_LNK_MASK;
1479
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001480 return is_link;
1481}
1482
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001483static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001484{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001485 int res = 0;
1486 unsigned long flags;
1487
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001488 if (!d40c->busy)
1489 return 0;
1490
Narayanan G7fb3e752011-11-17 17:26:41 +05301491 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001492 spin_lock_irqsave(&d40c->lock, flags);
1493
1494 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301495
Narayanan G7fb3e752011-11-17 17:26:41 +05301496 pm_runtime_mark_last_busy(d40c->base->dev);
1497 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001498 spin_unlock_irqrestore(&d40c->lock, flags);
1499 return res;
1500}
1501
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001502static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001503{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001504 int res = 0;
1505 unsigned long flags;
1506
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001507 if (!d40c->busy)
1508 return 0;
1509
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001510 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301511 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001512
1513 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301514 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001515 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001516
Narayanan G7fb3e752011-11-17 17:26:41 +05301517 pm_runtime_mark_last_busy(d40c->base->dev);
1518 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001519 spin_unlock_irqrestore(&d40c->lock, flags);
1520 return res;
1521}
1522
Linus Walleij8d318a52010-03-30 15:33:42 +02001523static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1524{
1525 struct d40_chan *d40c = container_of(tx->chan,
1526 struct d40_chan,
1527 chan);
1528 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1529 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001530 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001531
1532 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001533 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001534 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 spin_unlock_irqrestore(&d40c->lock, flags);
1536
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001537 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001538}
1539
1540static int d40_start(struct d40_chan *d40c)
1541{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001542 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001543}
1544
1545static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1546{
1547 struct d40_desc *d40d;
1548 int err;
1549
1550 /* Start queued jobs, if any */
1551 d40d = d40_first_queued(d40c);
1552
1553 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301554 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301555 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301556 pm_runtime_get_sync(d40c->base->dev);
1557 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001558
1559 /* Remove from queue */
1560 d40_desc_remove(d40d);
1561
1562 /* Add to active queue */
1563 d40_desc_submit(d40c, d40d);
1564
Rabin Vincent7d83a852011-01-25 11:18:06 +01001565 /* Initiate DMA job */
1566 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001567
Rabin Vincent7d83a852011-01-25 11:18:06 +01001568 /* Start dma job */
1569 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001570
Rabin Vincent7d83a852011-01-25 11:18:06 +01001571 if (err)
1572 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001573 }
1574
1575 return d40d;
1576}
1577
1578/* called from interrupt context */
1579static void dma_tc_handle(struct d40_chan *d40c)
1580{
1581 struct d40_desc *d40d;
1582
Linus Walleij8d318a52010-03-30 15:33:42 +02001583 /* Get first active entry from list */
1584 d40d = d40_first_active_get(d40c);
1585
1586 if (d40d == NULL)
1587 return;
1588
Rabin Vincent0c842b52011-01-25 11:18:35 +01001589 if (d40d->cyclic) {
1590 /*
1591 * If this was a paritially loaded list, we need to reloaded
1592 * it, and only when the list is completed. We need to check
1593 * for done because the interrupt will hit for every link, and
1594 * not just the last one.
1595 */
1596 if (d40d->lli_current < d40d->lli_len
1597 && !d40_tx_is_linked(d40c)
1598 && !d40_residue(d40c)) {
1599 d40_lcla_free_all(d40c, d40d);
1600 d40_desc_load(d40c, d40d);
1601 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001602
Rabin Vincent0c842b52011-01-25 11:18:35 +01001603 if (d40d->lli_current == d40d->lli_len)
1604 d40d->lli_current = 0;
1605 }
1606 } else {
1607 d40_lcla_free_all(d40c, d40d);
1608
1609 if (d40d->lli_current < d40d->lli_len) {
1610 d40_desc_load(d40c, d40d);
1611 /* Start dma job */
1612 (void) d40_start(d40c);
1613 return;
1614 }
1615
1616 if (d40_queue_start(d40c) == NULL)
1617 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301618 pm_runtime_mark_last_busy(d40c->base->dev);
1619 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001620
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001621 d40_desc_remove(d40d);
1622 d40_desc_done(d40c, d40d);
1623 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001624
Linus Walleij8d318a52010-03-30 15:33:42 +02001625 d40c->pending_tx++;
1626 tasklet_schedule(&d40c->tasklet);
1627
1628}
1629
1630static void dma_tasklet(unsigned long data)
1631{
1632 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001633 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001634 unsigned long flags;
1635 dma_async_tx_callback callback;
1636 void *callback_param;
1637
1638 spin_lock_irqsave(&d40c->lock, flags);
1639
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001640 /* Get first entry from the done list */
1641 d40d = d40_first_done(d40c);
1642 if (d40d == NULL) {
1643 /* Check if we have reached here for cyclic job */
1644 d40d = d40_first_active_get(d40c);
1645 if (d40d == NULL || !d40d->cyclic)
1646 goto err;
1647 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001648
Rabin Vincent0c842b52011-01-25 11:18:35 +01001649 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001650 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001651
1652 /*
1653 * If terminating a channel pending_tx is set to zero.
1654 * This prevents any finished active jobs to return to the client.
1655 */
1656 if (d40c->pending_tx == 0) {
1657 spin_unlock_irqrestore(&d40c->lock, flags);
1658 return;
1659 }
1660
1661 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001662 callback = d40d->txd.callback;
1663 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001664
Rabin Vincent0c842b52011-01-25 11:18:35 +01001665 if (!d40d->cyclic) {
1666 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001667 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001668 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001669 } else if (!d40d->is_in_client_list) {
1670 d40_desc_remove(d40d);
1671 d40_lcla_free_all(d40c, d40d);
1672 list_add_tail(&d40d->node, &d40c->client);
1673 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001674 }
1675 }
1676
1677 d40c->pending_tx--;
1678
1679 if (d40c->pending_tx)
1680 tasklet_schedule(&d40c->tasklet);
1681
1682 spin_unlock_irqrestore(&d40c->lock, flags);
1683
Jonas Aaberg767a9672010-08-09 12:08:34 +00001684 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001685 callback(callback_param);
1686
1687 return;
1688
Narayanan G1bdae6f2012-02-09 12:41:37 +05301689err:
1690 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001691 if (d40c->pending_tx > 0)
1692 d40c->pending_tx--;
1693 spin_unlock_irqrestore(&d40c->lock, flags);
1694}
1695
1696static irqreturn_t d40_handle_interrupt(int irq, void *data)
1697{
Linus Walleij8d318a52010-03-30 15:33:42 +02001698 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001699 u32 idx;
1700 u32 row;
1701 long chan = -1;
1702 struct d40_chan *d40c;
1703 unsigned long flags;
1704 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001705 u32 regs[base->gen_dmac.il_size];
1706 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1707 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001708
1709 spin_lock_irqsave(&base->interrupt_lock, flags);
1710
1711 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001712 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001713 regs[i] = readl(base->virtbase + il[i].src);
1714
1715 for (;;) {
1716
1717 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001718 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001719
1720 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001721 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001722 break;
1723
1724 row = chan / BITS_PER_LONG;
1725 idx = chan & (BITS_PER_LONG - 1);
1726
Linus Walleij8d318a52010-03-30 15:33:42 +02001727 if (il[row].offset == D40_PHY_CHAN)
1728 d40c = base->lookup_phy_chans[idx];
1729 else
1730 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001731
1732 if (!d40c) {
1733 /*
1734 * No error because this can happen if something else
1735 * in the system is using the channel.
1736 */
1737 continue;
1738 }
1739
1740 /* ACK interrupt */
1741 writel(1 << idx, base->virtbase + il[row].clr);
1742
Linus Walleij8d318a52010-03-30 15:33:42 +02001743 spin_lock(&d40c->lock);
1744
1745 if (!il[row].is_error)
1746 dma_tc_handle(d40c);
1747 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001748 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1749 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001750
1751 spin_unlock(&d40c->lock);
1752 }
1753
1754 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1755
1756 return IRQ_HANDLED;
1757}
1758
Linus Walleij8d318a52010-03-30 15:33:42 +02001759static int d40_validate_conf(struct d40_chan *d40c,
1760 struct stedma40_chan_cfg *conf)
1761{
1762 int res = 0;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001763 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001764
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001765 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001766 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001767 res = -EINVAL;
1768 }
1769
Lee Jones26955c07d2013-05-03 15:31:56 +01001770 if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
1771 (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
1772 (conf->dev_type < 0)) {
1773 chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001774 res = -EINVAL;
1775 }
1776
1777 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Lee Jones26955c07d2013-05-03 15:31:56 +01001778 d40c->base->plat_data->dev_tx[conf->dev_type] == 0 &&
1779 d40c->runtime_addr == 0) {
1780 chan_err(d40c, "Invalid TX channel address (%d)\n",
1781 conf->dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001782 res = -EINVAL;
1783 }
1784
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001785 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Lee Jones26955c07d2013-05-03 15:31:56 +01001786 d40c->base->plat_data->dev_rx[conf->dev_type] == 0 &&
1787 d40c->runtime_addr == 0) {
1788 chan_err(d40c, "Invalid RX channel address (%d)\n",
1789 conf->dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02001790 res = -EINVAL;
1791 }
1792
1793 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1794 /*
1795 * DMAC HW supports it. Will be added to this driver,
1796 * in case any dma client requires it.
1797 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001798 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001799 res = -EINVAL;
1800 }
1801
Per Forlind49278e2010-12-20 18:31:38 +01001802 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1803 (1 << conf->src_info.data_width) !=
1804 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1805 (1 << conf->dst_info.data_width)) {
1806 /*
1807 * The DMAC hardware only supports
1808 * src (burst x width) == dst (burst x width)
1809 */
1810
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001811 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001812 res = -EINVAL;
1813 }
1814
Linus Walleij8d318a52010-03-30 15:33:42 +02001815 return res;
1816}
1817
Narayanan G5cd326f2011-11-30 19:20:42 +05301818static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1819 bool is_src, int log_event_line, bool is_log,
1820 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001821{
1822 unsigned long flags;
1823 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301824
1825 *first_user = ((phy->allocated_src | phy->allocated_dst)
1826 == D40_ALLOC_FREE);
1827
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001828 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001829 /* Physical interrupts are masked per physical full channel */
1830 if (phy->allocated_src == D40_ALLOC_FREE &&
1831 phy->allocated_dst == D40_ALLOC_FREE) {
1832 phy->allocated_dst = D40_ALLOC_PHY;
1833 phy->allocated_src = D40_ALLOC_PHY;
1834 goto found;
1835 } else
1836 goto not_found;
1837 }
1838
1839 /* Logical channel */
1840 if (is_src) {
1841 if (phy->allocated_src == D40_ALLOC_PHY)
1842 goto not_found;
1843
1844 if (phy->allocated_src == D40_ALLOC_FREE)
1845 phy->allocated_src = D40_ALLOC_LOG_FREE;
1846
1847 if (!(phy->allocated_src & (1 << log_event_line))) {
1848 phy->allocated_src |= 1 << log_event_line;
1849 goto found;
1850 } else
1851 goto not_found;
1852 } else {
1853 if (phy->allocated_dst == D40_ALLOC_PHY)
1854 goto not_found;
1855
1856 if (phy->allocated_dst == D40_ALLOC_FREE)
1857 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1858
1859 if (!(phy->allocated_dst & (1 << log_event_line))) {
1860 phy->allocated_dst |= 1 << log_event_line;
1861 goto found;
1862 } else
1863 goto not_found;
1864 }
1865
1866not_found:
1867 spin_unlock_irqrestore(&phy->lock, flags);
1868 return false;
1869found:
1870 spin_unlock_irqrestore(&phy->lock, flags);
1871 return true;
1872}
1873
1874static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1875 int log_event_line)
1876{
1877 unsigned long flags;
1878 bool is_free = false;
1879
1880 spin_lock_irqsave(&phy->lock, flags);
1881 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001882 phy->allocated_dst = D40_ALLOC_FREE;
1883 phy->allocated_src = D40_ALLOC_FREE;
1884 is_free = true;
1885 goto out;
1886 }
1887
1888 /* Logical channel */
1889 if (is_src) {
1890 phy->allocated_src &= ~(1 << log_event_line);
1891 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1892 phy->allocated_src = D40_ALLOC_FREE;
1893 } else {
1894 phy->allocated_dst &= ~(1 << log_event_line);
1895 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1896 phy->allocated_dst = D40_ALLOC_FREE;
1897 }
1898
1899 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1900 D40_ALLOC_FREE);
1901
1902out:
1903 spin_unlock_irqrestore(&phy->lock, flags);
1904
1905 return is_free;
1906}
1907
Narayanan G5cd326f2011-11-30 19:20:42 +05301908static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001909{
Lee Jones26955c07d2013-05-03 15:31:56 +01001910 int dev_type = d40c->dma_cfg.dev_type;
Linus Walleij8d318a52010-03-30 15:33:42 +02001911 int event_group;
1912 int event_line;
1913 struct d40_phy_res *phys;
1914 int i;
1915 int j;
1916 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001917 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001918 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001919 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001920
1921 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001922 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001923
1924 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001925 log_num = 2 * dev_type;
1926 is_src = true;
1927 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1928 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1929 /* dst event lines are used for logical memcpy */
Linus Walleij8d318a52010-03-30 15:33:42 +02001930 log_num = 2 * dev_type + 1;
1931 is_src = false;
1932 } else
1933 return -EINVAL;
1934
1935 event_group = D40_TYPE_TO_GROUP(dev_type);
1936 event_line = D40_TYPE_TO_EVENT(dev_type);
1937
1938 if (!is_log) {
1939 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1940 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001941 if (d40c->dma_cfg.use_fixed_channel) {
1942 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001943 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301944 0, is_log,
1945 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001946 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001947 } else {
1948 for (i = 0; i < num_phy_chans; i++) {
1949 if (d40_alloc_mask_set(&phys[i], is_src,
1950 0, is_log,
1951 first_phy_user))
1952 goto found_phy;
1953 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001954 }
1955 } else
1956 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1957 int phy_num = j + event_group * 2;
1958 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001959 if (d40_alloc_mask_set(&phys[i],
1960 is_src,
1961 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301962 is_log,
1963 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001964 goto found_phy;
1965 }
1966 }
1967 return -EINVAL;
1968found_phy:
1969 d40c->phy_chan = &phys[i];
1970 d40c->log_num = D40_PHY_CHAN;
1971 goto out;
1972 }
1973 if (dev_type == -1)
1974 return -EINVAL;
1975
1976 /* Find logical channel */
1977 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1978 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301979
1980 if (d40c->dma_cfg.use_fixed_channel) {
1981 i = d40c->dma_cfg.phy_channel;
1982
1983 if ((i != phy_num) && (i != phy_num + 1)) {
1984 dev_err(chan2dev(d40c),
1985 "invalid fixed phy channel %d\n", i);
1986 return -EINVAL;
1987 }
1988
1989 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1990 is_log, first_phy_user))
1991 goto found_log;
1992
1993 dev_err(chan2dev(d40c),
1994 "could not allocate fixed phy channel %d\n", i);
1995 return -EINVAL;
1996 }
1997
Linus Walleij8d318a52010-03-30 15:33:42 +02001998 /*
1999 * Spread logical channels across all available physical rather
2000 * than pack every logical channel at the first available phy
2001 * channels.
2002 */
2003 if (is_src) {
2004 for (i = phy_num; i < phy_num + 2; i++) {
2005 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302006 event_line, is_log,
2007 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002008 goto found_log;
2009 }
2010 } else {
2011 for (i = phy_num + 1; i >= phy_num; i--) {
2012 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302013 event_line, is_log,
2014 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002015 goto found_log;
2016 }
2017 }
2018 }
2019 return -EINVAL;
2020
2021found_log:
2022 d40c->phy_chan = &phys[i];
2023 d40c->log_num = log_num;
2024out:
2025
2026 if (is_log)
2027 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2028 else
2029 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2030
2031 return 0;
2032
2033}
2034
Linus Walleij8d318a52010-03-30 15:33:42 +02002035static int d40_config_memcpy(struct d40_chan *d40c)
2036{
2037 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2038
2039 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002040 d40c->dma_cfg = dma40_memcpy_conf_log;
Lee Jones26955c07d2013-05-03 15:31:56 +01002041 d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002042
Lee Jones9b233f92013-05-15 10:51:26 +01002043 d40_log_cfg(&d40c->dma_cfg,
2044 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2045
Linus Walleij8d318a52010-03-30 15:33:42 +02002046 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2047 dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002048 d40c->dma_cfg = dma40_memcpy_conf_phy;
Lee Jones57e65ad2013-05-15 10:51:25 +01002049
2050 /* Generate interrrupt at end of transfer or relink. */
2051 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
2052
2053 /* Generate interrupt on error. */
2054 d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2055 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2056
Linus Walleij8d318a52010-03-30 15:33:42 +02002057 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002058 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002059 return -EINVAL;
2060 }
2061
2062 return 0;
2063}
2064
Linus Walleij8d318a52010-03-30 15:33:42 +02002065static int d40_free_dma(struct d40_chan *d40c)
2066{
2067
2068 int res = 0;
Lee Jones26955c07d2013-05-03 15:31:56 +01002069 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002070 struct d40_phy_res *phy = d40c->phy_chan;
2071 bool is_src;
2072
2073 /* Terminate all queued and active transfers */
2074 d40_term_all(d40c);
2075
2076 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002077 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002078 return -EINVAL;
2079 }
2080
2081 if (phy->allocated_src == D40_ALLOC_FREE &&
2082 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002083 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002084 return -EINVAL;
2085 }
2086
Linus Walleij8d318a52010-03-30 15:33:42 +02002087 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Lee Jones26955c07d2013-05-03 15:31:56 +01002088 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002089 is_src = false;
Lee Jones26955c07d2013-05-03 15:31:56 +01002090 else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002091 is_src = true;
Lee Jones26955c07d2013-05-03 15:31:56 +01002092 else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002093 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002094 return -EINVAL;
2095 }
2096
Narayanan G7fb3e752011-11-17 17:26:41 +05302097 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002098 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2099 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302100 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302101 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002102 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302103
Narayanan G1bdae6f2012-02-09 12:41:37 +05302104 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2105
2106 if (chan_is_logical(d40c))
2107 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2108 else
2109 d40c->base->lookup_phy_chans[phy->num] = NULL;
2110
Narayanan G7fb3e752011-11-17 17:26:41 +05302111 if (d40c->busy) {
2112 pm_runtime_mark_last_busy(d40c->base->dev);
2113 pm_runtime_put_autosuspend(d40c->base->dev);
2114 }
2115
2116 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002117 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002118 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302119out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002120
Narayanan G7fb3e752011-11-17 17:26:41 +05302121 pm_runtime_mark_last_busy(d40c->base->dev);
2122 pm_runtime_put_autosuspend(d40c->base->dev);
2123 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002124}
2125
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002126static bool d40_is_paused(struct d40_chan *d40c)
2127{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002128 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002129 bool is_paused = false;
2130 unsigned long flags;
2131 void __iomem *active_reg;
2132 u32 status;
Lee Jones26955c07d2013-05-03 15:31:56 +01002133 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002134
2135 spin_lock_irqsave(&d40c->lock, flags);
2136
Rabin Vincent724a8572011-01-25 11:18:08 +01002137 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002138 if (d40c->phy_chan->num % 2 == 0)
2139 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2140 else
2141 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2142
2143 status = (readl(active_reg) &
2144 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2145 D40_CHAN_POS(d40c->phy_chan->num);
2146 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2147 is_paused = true;
2148
2149 goto _exit;
2150 }
2151
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002152 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002153 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002154 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002155 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002156 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002157 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002158 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002159 goto _exit;
2160 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002161
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002162 status = (status & D40_EVENTLINE_MASK(event)) >>
2163 D40_EVENTLINE_POS(event);
2164
2165 if (status != D40_DMA_RUN)
2166 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002167_exit:
2168 spin_unlock_irqrestore(&d40c->lock, flags);
2169 return is_paused;
2170
2171}
2172
Linus Walleij8d318a52010-03-30 15:33:42 +02002173static u32 stedma40_residue(struct dma_chan *chan)
2174{
2175 struct d40_chan *d40c =
2176 container_of(chan, struct d40_chan, chan);
2177 u32 bytes_left;
2178 unsigned long flags;
2179
2180 spin_lock_irqsave(&d40c->lock, flags);
2181 bytes_left = d40_residue(d40c);
2182 spin_unlock_irqrestore(&d40c->lock, flags);
2183
2184 return bytes_left;
2185}
2186
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002187static int
2188d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2189 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002190 unsigned int sg_len, dma_addr_t src_dev_addr,
2191 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002192{
2193 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2194 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2195 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002196 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002197
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002198 ret = d40_log_sg_to_lli(sg_src, sg_len,
2199 src_dev_addr,
2200 desc->lli_log.src,
2201 chan->log_def.lcsp1,
2202 src_info->data_width,
2203 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002204
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002205 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2206 dst_dev_addr,
2207 desc->lli_log.dst,
2208 chan->log_def.lcsp3,
2209 dst_info->data_width,
2210 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002211
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002212 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002213}
2214
2215static int
2216d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2217 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002218 unsigned int sg_len, dma_addr_t src_dev_addr,
2219 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002220{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002221 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2222 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2223 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002224 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002225 int ret;
2226
Rabin Vincent0c842b52011-01-25 11:18:35 +01002227 if (desc->cyclic)
2228 flags |= LLI_CYCLIC | LLI_TERM_INT;
2229
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002230 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2231 desc->lli_phy.src,
2232 virt_to_phys(desc->lli_phy.src),
2233 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002234 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002235
2236 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2237 desc->lli_phy.dst,
2238 virt_to_phys(desc->lli_phy.dst),
2239 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002240 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002241
2242 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2243 desc->lli_pool.size, DMA_TO_DEVICE);
2244
2245 return ret < 0 ? ret : 0;
2246}
2247
Rabin Vincent5f811582011-01-25 11:18:18 +01002248static struct d40_desc *
2249d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2250 unsigned int sg_len, unsigned long dma_flags)
2251{
2252 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2253 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002254 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002255
2256 desc = d40_desc_get(chan);
2257 if (!desc)
2258 return NULL;
2259
2260 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2261 cfg->dst_info.data_width);
2262 if (desc->lli_len < 0) {
2263 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002264 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002265 }
2266
Rabin Vincentdbd88782011-01-25 11:18:19 +01002267 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2268 if (ret < 0) {
2269 chan_err(chan, "Could not allocate lli\n");
2270 goto err;
2271 }
2272
Rabin Vincent5f811582011-01-25 11:18:18 +01002273 desc->lli_current = 0;
2274 desc->txd.flags = dma_flags;
2275 desc->txd.tx_submit = d40_tx_submit;
2276
2277 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2278
2279 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002280
2281err:
2282 d40_desc_free(chan, desc);
2283 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002284}
2285
Rabin Vincentcade1d32011-01-25 11:18:23 +01002286static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302287d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002288{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002289 struct stedma40_platform_data *plat = chan->base->plat_data;
2290 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002291 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002292
Rabin Vincentcade1d32011-01-25 11:18:23 +01002293 if (chan->runtime_addr)
2294 return chan->runtime_addr;
2295
Vinod Kouldb8196d2011-10-13 22:34:23 +05302296 if (direction == DMA_DEV_TO_MEM)
Lee Jones26955c07d2013-05-03 15:31:56 +01002297 addr = plat->dev_rx[cfg->dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302298 else if (direction == DMA_MEM_TO_DEV)
Lee Jones26955c07d2013-05-03 15:31:56 +01002299 addr = plat->dev_tx[cfg->dev_type];
Rabin Vincentcade1d32011-01-25 11:18:23 +01002300
2301 return addr;
2302}
2303
2304static struct dma_async_tx_descriptor *
2305d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2306 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302307 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002308{
2309 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002310 dma_addr_t src_dev_addr = 0;
2311 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002312 struct d40_desc *desc;
2313 unsigned long flags;
2314 int ret;
2315
2316 if (!chan->phy_chan) {
2317 chan_err(chan, "Cannot prepare unallocated channel\n");
2318 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002319 }
2320
Rabin Vincentcade1d32011-01-25 11:18:23 +01002321 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002322
Rabin Vincentcade1d32011-01-25 11:18:23 +01002323 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2324 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002325 goto err;
2326
Rabin Vincent0c842b52011-01-25 11:18:35 +01002327 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2328 desc->cyclic = true;
2329
Linus Walleij7e426da2012-04-12 18:12:52 +02002330 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002331 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2332
Vinod Kouldb8196d2011-10-13 22:34:23 +05302333 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002334 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302335 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002336 dst_dev_addr = dev_addr;
2337 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002338
2339 if (chan_is_logical(chan))
2340 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002341 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002342 else
2343 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002344 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002345
2346 if (ret) {
2347 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2348 chan_is_logical(chan) ? "log" : "phy", ret);
2349 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002350 }
2351
Per Forlin82babbb362011-08-29 13:33:35 +02002352 /*
2353 * add descriptor to the prepare queue in order to be able
2354 * to free them later in terminate_all
2355 */
2356 list_add_tail(&desc->node, &chan->prepare_queue);
2357
Rabin Vincentcade1d32011-01-25 11:18:23 +01002358 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002359
Rabin Vincentcade1d32011-01-25 11:18:23 +01002360 return &desc->txd;
2361
Linus Walleij8d318a52010-03-30 15:33:42 +02002362err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002363 if (desc)
2364 d40_desc_free(chan, desc);
2365 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002366 return NULL;
2367}
Linus Walleij8d318a52010-03-30 15:33:42 +02002368
2369bool stedma40_filter(struct dma_chan *chan, void *data)
2370{
2371 struct stedma40_chan_cfg *info = data;
2372 struct d40_chan *d40c =
2373 container_of(chan, struct d40_chan, chan);
2374 int err;
2375
2376 if (data) {
2377 err = d40_validate_conf(d40c, info);
2378 if (!err)
2379 d40c->dma_cfg = *info;
2380 } else
2381 err = d40_config_memcpy(d40c);
2382
Rabin Vincentce2ca122010-10-12 13:00:49 +00002383 if (!err)
2384 d40c->configured = true;
2385
Linus Walleij8d318a52010-03-30 15:33:42 +02002386 return err == 0;
2387}
2388EXPORT_SYMBOL(stedma40_filter);
2389
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002390static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2391{
2392 bool realtime = d40c->dma_cfg.realtime;
2393 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002394 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002395 u32 event = D40_TYPE_TO_EVENT(dev_type);
2396 u32 group = D40_TYPE_TO_GROUP(dev_type);
2397 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302398 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002399 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302400
Tong Liu3cb645d2012-09-26 10:07:30 +00002401 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302402 /*
2403 * Due to a hardware bug, in some cases a logical channel triggered by
2404 * a high priority destination event line can generate extra packet
2405 * transactions.
2406 *
2407 * The workaround is to not set the high priority level for the
2408 * destination event lines that trigger logical channels.
2409 */
2410 if (!src && chan_is_logical(d40c))
2411 highprio = false;
2412
Tong Liu3cb645d2012-09-26 10:07:30 +00002413 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002414
2415 /* Destination event lines are stored in the upper halfword */
2416 if (!src)
2417 bit <<= 16;
2418
2419 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2420 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2421}
2422
2423static void d40_set_prio_realtime(struct d40_chan *d40c)
2424{
2425 if (d40c->base->rev < 3)
2426 return;
2427
2428 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2429 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002430 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002431
2432 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2433 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002434 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002435}
2436
Lee Jonesfa332de2013-05-03 15:32:12 +01002437#define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2438#define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2439#define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2440#define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2441
2442static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2443 struct of_dma *ofdma)
2444{
2445 struct stedma40_chan_cfg cfg;
2446 dma_cap_mask_t cap;
2447 u32 flags;
2448
2449 memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2450
2451 dma_cap_zero(cap);
2452 dma_cap_set(DMA_SLAVE, cap);
2453
2454 cfg.dev_type = dma_spec->args[0];
2455 flags = dma_spec->args[2];
2456
2457 switch (D40_DT_FLAGS_MODE(flags)) {
2458 case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2459 case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2460 }
2461
2462 switch (D40_DT_FLAGS_DIR(flags)) {
2463 case 0:
2464 cfg.dir = STEDMA40_MEM_TO_PERIPH;
2465 cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2466 break;
2467 case 1:
2468 cfg.dir = STEDMA40_PERIPH_TO_MEM;
2469 cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2470 break;
2471 }
2472
2473 if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2474 cfg.phy_channel = dma_spec->args[1];
2475 cfg.use_fixed_channel = true;
2476 }
2477
2478 return dma_request_channel(cap, stedma40_filter, &cfg);
2479}
2480
Linus Walleij8d318a52010-03-30 15:33:42 +02002481/* DMA ENGINE functions */
2482static int d40_alloc_chan_resources(struct dma_chan *chan)
2483{
2484 int err;
2485 unsigned long flags;
2486 struct d40_chan *d40c =
2487 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002488 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002489 spin_lock_irqsave(&d40c->lock, flags);
2490
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002491 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002492
Rabin Vincentce2ca122010-10-12 13:00:49 +00002493 /* If no dma configuration is set use default configuration (memcpy) */
2494 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002495 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002496 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002497 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002498 goto fail;
2499 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002500 }
2501
Narayanan G5cd326f2011-11-30 19:20:42 +05302502 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002503 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002504 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302505 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002506 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002507 }
2508
Narayanan G7fb3e752011-11-17 17:26:41 +05302509 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002510
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002511 d40_set_prio_realtime(d40c);
2512
Rabin Vincent724a8572011-01-25 11:18:08 +01002513 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002514 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2515 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002516 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002517 else
2518 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002519 d40c->dma_cfg.dev_type *
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002520 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Lee Jones97782562013-05-15 10:51:24 +01002521
2522 /* Unmask the Global Interrupt Mask. */
2523 d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2524 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
Linus Walleijef1872e2010-06-20 21:24:52 +00002525 }
2526
Narayanan G5cd326f2011-11-30 19:20:42 +05302527 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2528 chan_is_logical(d40c) ? "logical" : "physical",
2529 d40c->phy_chan->num,
2530 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2531
2532
Linus Walleijef1872e2010-06-20 21:24:52 +00002533 /*
2534 * Only write channel configuration to the DMA if the physical
2535 * resource is free. In case of multiple logical channels
2536 * on the same physical resource, only the first write is necessary.
2537 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002538 if (is_free_phy)
2539 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002540fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302541 pm_runtime_mark_last_busy(d40c->base->dev);
2542 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002543 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002544 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002545}
2546
2547static void d40_free_chan_resources(struct dma_chan *chan)
2548{
2549 struct d40_chan *d40c =
2550 container_of(chan, struct d40_chan, chan);
2551 int err;
2552 unsigned long flags;
2553
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002554 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002555 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002556 return;
2557 }
2558
Linus Walleij8d318a52010-03-30 15:33:42 +02002559 spin_lock_irqsave(&d40c->lock, flags);
2560
2561 err = d40_free_dma(d40c);
2562
2563 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002564 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002565 spin_unlock_irqrestore(&d40c->lock, flags);
2566}
2567
2568static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2569 dma_addr_t dst,
2570 dma_addr_t src,
2571 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002572 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002573{
Rabin Vincent95944c62011-01-25 11:18:17 +01002574 struct scatterlist dst_sg;
2575 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002576
Rabin Vincent95944c62011-01-25 11:18:17 +01002577 sg_init_table(&dst_sg, 1);
2578 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002579
Rabin Vincent95944c62011-01-25 11:18:17 +01002580 sg_dma_address(&dst_sg) = dst;
2581 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002582
Rabin Vincent95944c62011-01-25 11:18:17 +01002583 sg_dma_len(&dst_sg) = size;
2584 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002585
Rabin Vincentcade1d32011-01-25 11:18:23 +01002586 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002587}
2588
Ira Snyder0d688662010-09-30 11:46:47 +00002589static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002590d40_prep_memcpy_sg(struct dma_chan *chan,
2591 struct scatterlist *dst_sg, unsigned int dst_nents,
2592 struct scatterlist *src_sg, unsigned int src_nents,
2593 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002594{
2595 if (dst_nents != src_nents)
2596 return NULL;
2597
Rabin Vincentcade1d32011-01-25 11:18:23 +01002598 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002599}
2600
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002601static struct dma_async_tx_descriptor *
2602d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2603 unsigned int sg_len, enum dma_transfer_direction direction,
2604 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002605{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002606 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002607 return NULL;
2608
Rabin Vincentcade1d32011-01-25 11:18:23 +01002609 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002610}
2611
Rabin Vincent0c842b52011-01-25 11:18:35 +01002612static struct dma_async_tx_descriptor *
2613dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2614 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002615 enum dma_transfer_direction direction, unsigned long flags,
2616 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002617{
2618 unsigned int periods = buf_len / period_len;
2619 struct dma_async_tx_descriptor *txd;
2620 struct scatterlist *sg;
2621 int i;
2622
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002623 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002624 for (i = 0; i < periods; i++) {
2625 sg_dma_address(&sg[i]) = dma_addr;
2626 sg_dma_len(&sg[i]) = period_len;
2627 dma_addr += period_len;
2628 }
2629
2630 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002631 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002632 sg[periods].page_link =
2633 ((unsigned long)sg | 0x01) & ~0x02;
2634
2635 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2636 DMA_PREP_INTERRUPT);
2637
2638 kfree(sg);
2639
2640 return txd;
2641}
2642
Linus Walleij8d318a52010-03-30 15:33:42 +02002643static enum dma_status d40_tx_status(struct dma_chan *chan,
2644 dma_cookie_t cookie,
2645 struct dma_tx_state *txstate)
2646{
2647 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002648 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002649
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002650 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002651 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002652 return -EINVAL;
2653 }
2654
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002655 ret = dma_cookie_status(chan, cookie, txstate);
2656 if (ret != DMA_SUCCESS)
2657 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002658
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002659 if (d40_is_paused(d40c))
2660 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002661
2662 return ret;
2663}
2664
2665static void d40_issue_pending(struct dma_chan *chan)
2666{
2667 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2668 unsigned long flags;
2669
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002670 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002671 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002672 return;
2673 }
2674
Linus Walleij8d318a52010-03-30 15:33:42 +02002675 spin_lock_irqsave(&d40c->lock, flags);
2676
Per Forlina8f30672011-06-26 23:29:52 +02002677 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2678
2679 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002680 if (!d40c->busy)
2681 (void) d40_queue_start(d40c);
2682
2683 spin_unlock_irqrestore(&d40c->lock, flags);
2684}
2685
Narayanan G1bdae6f2012-02-09 12:41:37 +05302686static void d40_terminate_all(struct dma_chan *chan)
2687{
2688 unsigned long flags;
2689 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2690 int ret;
2691
2692 spin_lock_irqsave(&d40c->lock, flags);
2693
2694 pm_runtime_get_sync(d40c->base->dev);
2695 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2696 if (ret)
2697 chan_err(d40c, "Failed to stop channel\n");
2698
2699 d40_term_all(d40c);
2700 pm_runtime_mark_last_busy(d40c->base->dev);
2701 pm_runtime_put_autosuspend(d40c->base->dev);
2702 if (d40c->busy) {
2703 pm_runtime_mark_last_busy(d40c->base->dev);
2704 pm_runtime_put_autosuspend(d40c->base->dev);
2705 }
2706 d40c->busy = false;
2707
2708 spin_unlock_irqrestore(&d40c->lock, flags);
2709}
2710
Rabin Vincent98ca5282011-06-27 11:33:38 +02002711static int
2712dma40_config_to_halfchannel(struct d40_chan *d40c,
2713 struct stedma40_half_channel_info *info,
2714 enum dma_slave_buswidth width,
2715 u32 maxburst)
2716{
2717 enum stedma40_periph_data_width addr_width;
2718 int psize;
2719
2720 switch (width) {
2721 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2722 addr_width = STEDMA40_BYTE_WIDTH;
2723 break;
2724 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2725 addr_width = STEDMA40_HALFWORD_WIDTH;
2726 break;
2727 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2728 addr_width = STEDMA40_WORD_WIDTH;
2729 break;
2730 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2731 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2732 break;
2733 default:
2734 dev_err(d40c->base->dev,
2735 "illegal peripheral address width "
2736 "requested (%d)\n",
2737 width);
2738 return -EINVAL;
2739 }
2740
2741 if (chan_is_logical(d40c)) {
2742 if (maxburst >= 16)
2743 psize = STEDMA40_PSIZE_LOG_16;
2744 else if (maxburst >= 8)
2745 psize = STEDMA40_PSIZE_LOG_8;
2746 else if (maxburst >= 4)
2747 psize = STEDMA40_PSIZE_LOG_4;
2748 else
2749 psize = STEDMA40_PSIZE_LOG_1;
2750 } else {
2751 if (maxburst >= 16)
2752 psize = STEDMA40_PSIZE_PHY_16;
2753 else if (maxburst >= 8)
2754 psize = STEDMA40_PSIZE_PHY_8;
2755 else if (maxburst >= 4)
2756 psize = STEDMA40_PSIZE_PHY_4;
2757 else
2758 psize = STEDMA40_PSIZE_PHY_1;
2759 }
2760
2761 info->data_width = addr_width;
2762 info->psize = psize;
2763 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2764
2765 return 0;
2766}
2767
Linus Walleij95e14002010-08-04 13:37:45 +02002768/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002769static int d40_set_runtime_config(struct dma_chan *chan,
2770 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002771{
2772 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2773 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002774 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002775 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002776 u32 src_maxburst, dst_maxburst;
2777 int ret;
2778
2779 src_addr_width = config->src_addr_width;
2780 src_maxburst = config->src_maxburst;
2781 dst_addr_width = config->dst_addr_width;
2782 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002783
Vinod Kouldb8196d2011-10-13 22:34:23 +05302784 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002785 dma_addr_t dev_addr_rx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002786 d40c->base->plat_data->dev_rx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002787
2788 config_addr = config->src_addr;
2789 if (dev_addr_rx)
2790 dev_dbg(d40c->base->dev,
2791 "channel has a pre-wired RX address %08x "
2792 "overriding with %08x\n",
2793 dev_addr_rx, config_addr);
2794 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2795 dev_dbg(d40c->base->dev,
2796 "channel was not configured for peripheral "
2797 "to memory transfer (%d) overriding\n",
2798 cfg->dir);
2799 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2800
Rabin Vincent98ca5282011-06-27 11:33:38 +02002801 /* Configure the memory side */
2802 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2803 dst_addr_width = src_addr_width;
2804 if (dst_maxburst == 0)
2805 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002806
Vinod Kouldb8196d2011-10-13 22:34:23 +05302807 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002808 dma_addr_t dev_addr_tx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002809 d40c->base->plat_data->dev_tx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002810
2811 config_addr = config->dst_addr;
2812 if (dev_addr_tx)
2813 dev_dbg(d40c->base->dev,
2814 "channel has a pre-wired TX address %08x "
2815 "overriding with %08x\n",
2816 dev_addr_tx, config_addr);
2817 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2818 dev_dbg(d40c->base->dev,
2819 "channel was not configured for memory "
2820 "to peripheral transfer (%d) overriding\n",
2821 cfg->dir);
2822 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2823
Rabin Vincent98ca5282011-06-27 11:33:38 +02002824 /* Configure the memory side */
2825 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2826 src_addr_width = dst_addr_width;
2827 if (src_maxburst == 0)
2828 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002829 } else {
2830 dev_err(d40c->base->dev,
2831 "unrecognized channel direction %d\n",
2832 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002833 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002834 }
2835
Rabin Vincent98ca5282011-06-27 11:33:38 +02002836 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002837 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002838 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2839 src_maxburst,
2840 src_addr_width,
2841 dst_maxburst,
2842 dst_addr_width);
2843 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002844 }
2845
Per Forlin92bb6cd2011-10-13 12:11:36 +02002846 if (src_maxburst > 16) {
2847 src_maxburst = 16;
2848 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2849 } else if (dst_maxburst > 16) {
2850 dst_maxburst = 16;
2851 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2852 }
2853
Rabin Vincent98ca5282011-06-27 11:33:38 +02002854 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2855 src_addr_width,
2856 src_maxburst);
2857 if (ret)
2858 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002859
Rabin Vincent98ca5282011-06-27 11:33:38 +02002860 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2861 dst_addr_width,
2862 dst_maxburst);
2863 if (ret)
2864 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002865
Per Forlina59670a2010-10-06 09:05:27 +00002866 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002867 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002868 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2869 else
Lee Jones57e65ad2013-05-15 10:51:25 +01002870 d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
Per Forlina59670a2010-10-06 09:05:27 +00002871
Linus Walleij95e14002010-08-04 13:37:45 +02002872 /* These settings will take precedence later */
2873 d40c->runtime_addr = config_addr;
2874 d40c->runtime_direction = config->direction;
2875 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002876 "configured channel %s for %s, data width %d/%d, "
2877 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002878 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302879 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002880 src_addr_width, dst_addr_width,
2881 src_maxburst, dst_maxburst);
2882
2883 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002884}
2885
Linus Walleij05827632010-05-17 16:30:42 -07002886static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2887 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002888{
Linus Walleij8d318a52010-03-30 15:33:42 +02002889 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2890
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002891 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002892 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002893 return -EINVAL;
2894 }
2895
Linus Walleij8d318a52010-03-30 15:33:42 +02002896 switch (cmd) {
2897 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302898 d40_terminate_all(chan);
2899 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002900 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002901 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002902 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002903 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002904 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002905 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002906 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002907 default:
2908 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002909 }
2910
2911 /* Other commands are unimplemented */
2912 return -ENXIO;
2913}
2914
2915/* Initialization functions */
2916
2917static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2918 struct d40_chan *chans, int offset,
2919 int num_chans)
2920{
2921 int i = 0;
2922 struct d40_chan *d40c;
2923
2924 INIT_LIST_HEAD(&dma->channels);
2925
2926 for (i = offset; i < offset + num_chans; i++) {
2927 d40c = &chans[i];
2928 d40c->base = base;
2929 d40c->chan.device = dma;
2930
Linus Walleij8d318a52010-03-30 15:33:42 +02002931 spin_lock_init(&d40c->lock);
2932
2933 d40c->log_num = D40_PHY_CHAN;
2934
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002935 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002936 INIT_LIST_HEAD(&d40c->active);
2937 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002938 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002939 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002940 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002941
Linus Walleij8d318a52010-03-30 15:33:42 +02002942 tasklet_init(&d40c->tasklet, dma_tasklet,
2943 (unsigned long) d40c);
2944
2945 list_add_tail(&d40c->chan.device_node,
2946 &dma->channels);
2947 }
2948}
2949
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002950static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2951{
2952 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2953 dev->device_prep_slave_sg = d40_prep_slave_sg;
2954
2955 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2956 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2957
2958 /*
2959 * This controller can only access address at even
2960 * 32bit boundaries, i.e. 2^2
2961 */
2962 dev->copy_align = 2;
2963 }
2964
2965 if (dma_has_cap(DMA_SG, dev->cap_mask))
2966 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2967
Rabin Vincent0c842b52011-01-25 11:18:35 +01002968 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2969 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2970
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002971 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2972 dev->device_free_chan_resources = d40_free_chan_resources;
2973 dev->device_issue_pending = d40_issue_pending;
2974 dev->device_tx_status = d40_tx_status;
2975 dev->device_control = d40_control;
2976 dev->dev = base->dev;
2977}
2978
Linus Walleij8d318a52010-03-30 15:33:42 +02002979static int __init d40_dmaengine_init(struct d40_base *base,
2980 int num_reserved_chans)
2981{
2982 int err ;
2983
2984 d40_chan_init(base, &base->dma_slave, base->log_chans,
2985 0, base->num_log_chans);
2986
2987 dma_cap_zero(base->dma_slave.cap_mask);
2988 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002989 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002990
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002991 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002992
2993 err = dma_async_device_register(&base->dma_slave);
2994
2995 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002996 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002997 goto failure1;
2998 }
2999
3000 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01003001 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02003002
3003 dma_cap_zero(base->dma_memcpy.cap_mask);
3004 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003005 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02003006
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003007 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02003008
3009 err = dma_async_device_register(&base->dma_memcpy);
3010
3011 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003012 d40_err(base->dev,
3013 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003014 goto failure2;
3015 }
3016
3017 d40_chan_init(base, &base->dma_both, base->phy_chans,
3018 0, num_reserved_chans);
3019
3020 dma_cap_zero(base->dma_both.cap_mask);
3021 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
3022 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003023 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01003024 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02003025
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003026 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02003027 err = dma_async_device_register(&base->dma_both);
3028
3029 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003030 d40_err(base->dev,
3031 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003032 goto failure3;
3033 }
3034 return 0;
3035failure3:
3036 dma_async_device_unregister(&base->dma_memcpy);
3037failure2:
3038 dma_async_device_unregister(&base->dma_slave);
3039failure1:
3040 return err;
3041}
3042
Narayanan G7fb3e752011-11-17 17:26:41 +05303043/* Suspend resume functionality */
3044#ifdef CONFIG_PM
3045static int dma40_pm_suspend(struct device *dev)
3046{
Narayanan G28c7a192011-11-22 13:56:55 +05303047 struct platform_device *pdev = to_platform_device(dev);
3048 struct d40_base *base = platform_get_drvdata(pdev);
3049 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05303050
Narayanan G28c7a192011-11-22 13:56:55 +05303051 if (base->lcpa_regulator)
3052 ret = regulator_disable(base->lcpa_regulator);
3053 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05303054}
3055
3056static int dma40_runtime_suspend(struct device *dev)
3057{
3058 struct platform_device *pdev = to_platform_device(dev);
3059 struct d40_base *base = platform_get_drvdata(pdev);
3060
3061 d40_save_restore_registers(base, true);
3062
3063 /* Don't disable/enable clocks for v1 due to HW bugs */
3064 if (base->rev != 1)
3065 writel_relaxed(base->gcc_pwr_off_mask,
3066 base->virtbase + D40_DREG_GCC);
3067
3068 return 0;
3069}
3070
3071static int dma40_runtime_resume(struct device *dev)
3072{
3073 struct platform_device *pdev = to_platform_device(dev);
3074 struct d40_base *base = platform_get_drvdata(pdev);
3075
3076 if (base->initialized)
3077 d40_save_restore_registers(base, false);
3078
3079 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3080 base->virtbase + D40_DREG_GCC);
3081 return 0;
3082}
3083
Narayanan G28c7a192011-11-22 13:56:55 +05303084static int dma40_resume(struct device *dev)
3085{
3086 struct platform_device *pdev = to_platform_device(dev);
3087 struct d40_base *base = platform_get_drvdata(pdev);
3088 int ret = 0;
3089
3090 if (base->lcpa_regulator)
3091 ret = regulator_enable(base->lcpa_regulator);
3092
3093 return ret;
3094}
Narayanan G7fb3e752011-11-17 17:26:41 +05303095
3096static const struct dev_pm_ops dma40_pm_ops = {
3097 .suspend = dma40_pm_suspend,
3098 .runtime_suspend = dma40_runtime_suspend,
3099 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303100 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303101};
3102#define DMA40_PM_OPS (&dma40_pm_ops)
3103#else
3104#define DMA40_PM_OPS NULL
3105#endif
3106
Linus Walleij8d318a52010-03-30 15:33:42 +02003107/* Initialization functions. */
3108
3109static int __init d40_phy_res_init(struct d40_base *base)
3110{
3111 int i;
3112 int num_phy_chans_avail = 0;
3113 u32 val[2];
3114 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303115 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003116
3117 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3118 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3119
3120 for (i = 0; i < base->num_phy_chans; i++) {
3121 base->phy_res[i].num = i;
3122 odd_even_bit += 2 * ((i % 2) == 0);
3123 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3124 /* Mark security only channels as occupied */
3125 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3126 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303127 base->phy_res[i].reserved = true;
3128 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3129 D40_DREG_GCC_SRC);
3130 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3131 D40_DREG_GCC_DST);
3132
3133
Linus Walleij8d318a52010-03-30 15:33:42 +02003134 } else {
3135 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3136 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303137 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003138 num_phy_chans_avail++;
3139 }
3140 spin_lock_init(&base->phy_res[i].lock);
3141 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003142
3143 /* Mark disabled channels as occupied */
3144 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003145 int chan = base->plat_data->disabled_channels[i];
3146
3147 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3148 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303149 base->phy_res[chan].reserved = true;
3150 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3151 D40_DREG_GCC_SRC);
3152 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3153 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003154 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003155 }
3156
Fabio Baltieri74070482012-12-18 12:25:14 +01003157 /* Mark soft_lli channels */
3158 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3159 int chan = base->plat_data->soft_lli_chans[i];
3160
3161 base->phy_res[chan].use_soft_lli = true;
3162 }
3163
Linus Walleij8d318a52010-03-30 15:33:42 +02003164 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3165 num_phy_chans_avail, base->num_phy_chans);
3166
3167 /* Verify settings extended vs standard */
3168 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3169
3170 for (i = 0; i < base->num_phy_chans; i++) {
3171
3172 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3173 (val[0] & 0x3) != 1)
3174 dev_info(base->dev,
3175 "[%s] INFO: channel %d is misconfigured (%d)\n",
3176 __func__, i, val[0] & 0x3);
3177
3178 val[0] = val[0] >> 2;
3179 }
3180
Narayanan G7fb3e752011-11-17 17:26:41 +05303181 /*
3182 * To keep things simple, Enable all clocks initially.
3183 * The clocks will get managed later post channel allocation.
3184 * The clocks for the event lines on which reserved channels exists
3185 * are not managed here.
3186 */
3187 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3188 base->gcc_pwr_off_mask = gcc;
3189
Linus Walleij8d318a52010-03-30 15:33:42 +02003190 return num_phy_chans_avail;
3191}
3192
3193static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3194{
Lee Jonesbb75d932013-05-03 15:32:10 +01003195 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003196 struct clk *clk = NULL;
3197 void __iomem *virtbase = NULL;
3198 struct resource *res = NULL;
3199 struct d40_base *base = NULL;
3200 int num_log_chans = 0;
3201 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003202 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003203 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003204 u32 pid;
3205 u32 cid;
3206 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003207
3208 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003209 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003210 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003211 goto failure;
3212 }
3213
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003214 clk_ret = clk_prepare_enable(clk);
3215 if (clk_ret) {
3216 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3217 goto failure;
3218 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003219
3220 /* Get IO for DMAC base address */
3221 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3222 if (!res)
3223 goto failure;
3224
3225 if (request_mem_region(res->start, resource_size(res),
3226 D40_NAME " I/O base") == NULL)
3227 goto failure;
3228
3229 virtbase = ioremap(res->start, resource_size(res));
3230 if (!virtbase)
3231 goto failure;
3232
Linus Walleijf4b89762011-06-27 11:33:46 +02003233 /* This is just a regular AMBA PrimeCell ID actually */
3234 for (pid = 0, i = 0; i < 4; i++)
3235 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3236 & 255) << (i * 8);
3237 for (cid = 0, i = 0; i < 4; i++)
3238 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3239 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003240
Linus Walleijf4b89762011-06-27 11:33:46 +02003241 if (cid != AMBA_CID) {
3242 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003243 goto failure;
3244 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003245 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3246 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3247 AMBA_MANF_BITS(pid),
3248 AMBA_VENDOR_ST);
3249 goto failure;
3250 }
3251 /*
3252 * HW revision:
3253 * DB8500ed has revision 0
3254 * ? has revision 1
3255 * DB8500v1 has revision 2
3256 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003257 * AP9540v1 has revision 4
3258 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003259 */
3260 rev = AMBA_REV_BITS(pid);
Lee Jones8b2fe9b2013-05-03 15:32:08 +01003261 if (rev < 2) {
3262 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3263 goto failure;
3264 }
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003265
Gerald Baeza47db92f2012-09-21 21:21:37 +02003266 /* The number of physical channels on this HW */
3267 if (plat_data->num_of_phy_chans)
3268 num_phy_chans = plat_data->num_of_phy_chans;
3269 else
3270 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3271
Lee Jonesdb72da92013-05-03 15:32:03 +01003272 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3273
Lee Jonesb2abb242013-05-03 15:32:09 +01003274 dev_info(&pdev->dev,
3275 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
3276 rev, res->start, num_phy_chans, num_log_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003277
Linus Walleij8d318a52010-03-30 15:33:42 +02003278 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003279 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003280 sizeof(struct d40_chan), GFP_KERNEL);
3281
3282 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003283 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003284 goto failure;
3285 }
3286
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003287 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003288 base->clk = clk;
3289 base->num_phy_chans = num_phy_chans;
3290 base->num_log_chans = num_log_chans;
3291 base->phy_start = res->start;
3292 base->phy_size = resource_size(res);
3293 base->virtbase = virtbase;
3294 base->plat_data = plat_data;
3295 base->dev = &pdev->dev;
3296 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3297 base->log_chans = &base->phy_chans[num_phy_chans];
3298
Tong Liu3cb645d2012-09-26 10:07:30 +00003299 if (base->plat_data->num_of_phy_chans == 14) {
3300 base->gen_dmac.backup = d40_backup_regs_v4b;
3301 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3302 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3303 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3304 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3305 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3306 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3307 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3308 base->gen_dmac.il = il_v4b;
3309 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3310 base->gen_dmac.init_reg = dma_init_reg_v4b;
3311 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3312 } else {
3313 if (base->rev >= 3) {
3314 base->gen_dmac.backup = d40_backup_regs_v4a;
3315 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3316 }
3317 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3318 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3319 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3320 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3321 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3322 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3323 base->gen_dmac.il = il_v4a;
3324 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3325 base->gen_dmac.init_reg = dma_init_reg_v4a;
3326 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3327 }
3328
Linus Walleij8d318a52010-03-30 15:33:42 +02003329 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3330 GFP_KERNEL);
3331 if (!base->phy_res)
3332 goto failure;
3333
3334 base->lookup_phy_chans = kzalloc(num_phy_chans *
3335 sizeof(struct d40_chan *),
3336 GFP_KERNEL);
3337 if (!base->lookup_phy_chans)
3338 goto failure;
3339
Lee Jones8a59fed2013-05-03 15:32:04 +01003340 base->lookup_log_chans = kzalloc(num_log_chans *
3341 sizeof(struct d40_chan *),
3342 GFP_KERNEL);
3343 if (!base->lookup_log_chans)
3344 goto failure;
Jonas Aaberg698e4732010-08-09 12:08:56 +00003345
Narayanan G7fb3e752011-11-17 17:26:41 +05303346 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3347 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003348 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303349 if (!base->reg_val_backup_chan)
3350 goto failure;
3351
3352 base->lcla_pool.alloc_map =
3353 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3354 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003355 if (!base->lcla_pool.alloc_map)
3356 goto failure;
3357
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003358 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3359 0, SLAB_HWCACHE_ALIGN,
3360 NULL);
3361 if (base->desc_slab == NULL)
3362 goto failure;
3363
Linus Walleij8d318a52010-03-30 15:33:42 +02003364 return base;
3365
3366failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003367 if (!clk_ret)
3368 clk_disable_unprepare(clk);
3369 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003370 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003371 if (virtbase)
3372 iounmap(virtbase);
3373 if (res)
3374 release_mem_region(res->start,
3375 resource_size(res));
3376 if (virtbase)
3377 iounmap(virtbase);
3378
3379 if (base) {
3380 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303381 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003382 kfree(base->lookup_log_chans);
3383 kfree(base->lookup_phy_chans);
3384 kfree(base->phy_res);
3385 kfree(base);
3386 }
3387
3388 return NULL;
3389}
3390
3391static void __init d40_hw_init(struct d40_base *base)
3392{
3393
Linus Walleij8d318a52010-03-30 15:33:42 +02003394 int i;
3395 u32 prmseo[2] = {0, 0};
3396 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3397 u32 pcmis = 0;
3398 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003399 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3400 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003401
Tong Liu3cb645d2012-09-26 10:07:30 +00003402 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003403 writel(dma_init_reg[i].val,
3404 base->virtbase + dma_init_reg[i].reg);
3405
3406 /* Configure all our dma channels to default settings */
3407 for (i = 0; i < base->num_phy_chans; i++) {
3408
3409 activeo[i % 2] = activeo[i % 2] << 2;
3410
3411 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3412 == D40_ALLOC_PHY) {
3413 activeo[i % 2] |= 3;
3414 continue;
3415 }
3416
3417 /* Enable interrupt # */
3418 pcmis = (pcmis << 1) | 1;
3419
3420 /* Clear interrupt # */
3421 pcicr = (pcicr << 1) | 1;
3422
3423 /* Set channel to physical mode */
3424 prmseo[i % 2] = prmseo[i % 2] << 2;
3425 prmseo[i % 2] |= 1;
3426
3427 }
3428
3429 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3430 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3431 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3432 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3433
3434 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003435 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003436
3437 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003438 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003439
Tong Liu3cb645d2012-09-26 10:07:30 +00003440 /* These are __initdata and cannot be accessed after init */
3441 base->gen_dmac.init_reg = NULL;
3442 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003443}
3444
Linus Walleij508849a2010-06-20 21:26:07 +00003445static int __init d40_lcla_allocate(struct d40_base *base)
3446{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003447 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003448 unsigned long *page_list;
3449 int i, j;
3450 int ret = 0;
3451
3452 /*
3453 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3454 * To full fill this hardware requirement without wasting 256 kb
3455 * we allocate pages until we get an aligned one.
3456 */
3457 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3458 GFP_KERNEL);
3459
3460 if (!page_list) {
3461 ret = -ENOMEM;
3462 goto failure;
3463 }
3464
3465 /* Calculating how many pages that are required */
3466 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3467
3468 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3469 page_list[i] = __get_free_pages(GFP_KERNEL,
3470 base->lcla_pool.pages);
3471 if (!page_list[i]) {
3472
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003473 d40_err(base->dev, "Failed to allocate %d pages.\n",
3474 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003475
3476 for (j = 0; j < i; j++)
3477 free_pages(page_list[j], base->lcla_pool.pages);
3478 goto failure;
3479 }
3480
3481 if ((virt_to_phys((void *)page_list[i]) &
3482 (LCLA_ALIGNMENT - 1)) == 0)
3483 break;
3484 }
3485
3486 for (j = 0; j < i; j++)
3487 free_pages(page_list[j], base->lcla_pool.pages);
3488
3489 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3490 base->lcla_pool.base = (void *)page_list[i];
3491 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003492 /*
3493 * After many attempts and no succees with finding the correct
3494 * alignment, try with allocating a big buffer.
3495 */
Linus Walleij508849a2010-06-20 21:26:07 +00003496 dev_warn(base->dev,
3497 "[%s] Failed to get %d pages @ 18 bit align.\n",
3498 __func__, base->lcla_pool.pages);
3499 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3500 base->num_phy_chans +
3501 LCLA_ALIGNMENT,
3502 GFP_KERNEL);
3503 if (!base->lcla_pool.base_unaligned) {
3504 ret = -ENOMEM;
3505 goto failure;
3506 }
3507
3508 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3509 LCLA_ALIGNMENT);
3510 }
3511
Rabin Vincent026cbc42011-01-25 11:18:14 +01003512 pool->dma_addr = dma_map_single(base->dev, pool->base,
3513 SZ_1K * base->num_phy_chans,
3514 DMA_TO_DEVICE);
3515 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3516 pool->dma_addr = 0;
3517 ret = -ENOMEM;
3518 goto failure;
3519 }
3520
Linus Walleij508849a2010-06-20 21:26:07 +00003521 writel(virt_to_phys(base->lcla_pool.base),
3522 base->virtbase + D40_DREG_LCLA);
3523failure:
3524 kfree(page_list);
3525 return ret;
3526}
3527
Lee Jones1814a172013-05-03 15:32:11 +01003528static int __init d40_of_probe(struct platform_device *pdev,
3529 struct device_node *np)
3530{
3531 struct stedma40_platform_data *pdata;
3532
3533 /*
3534 * FIXME: Fill in this routine as more support is added.
3535 * First platform enabled (u8500) doens't need any extra
3536 * properties to run, so this is fairly sparce currently.
3537 */
3538
3539 pdata = devm_kzalloc(&pdev->dev,
3540 sizeof(struct stedma40_platform_data),
3541 GFP_KERNEL);
3542 if (!pdata)
3543 return -ENOMEM;
3544
3545 pdev->dev.platform_data = pdata;
3546
3547 return 0;
3548}
3549
Linus Walleij8d318a52010-03-30 15:33:42 +02003550static int __init d40_probe(struct platform_device *pdev)
3551{
Lee Jones1814a172013-05-03 15:32:11 +01003552 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3553 struct device_node *np = pdev->dev.of_node;
Linus Walleij8d318a52010-03-30 15:33:42 +02003554 int err;
3555 int ret = -ENOENT;
Lee Jones1814a172013-05-03 15:32:11 +01003556 struct d40_base *base = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003557 struct resource *res = NULL;
3558 int num_reserved_chans;
3559 u32 val;
3560
Lee Jones1814a172013-05-03 15:32:11 +01003561 if (!plat_data) {
3562 if (np) {
3563 if(d40_of_probe(pdev, np)) {
3564 ret = -ENOMEM;
3565 goto failure;
3566 }
3567 } else {
3568 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3569 goto failure;
3570 }
3571 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003572
Lee Jones1814a172013-05-03 15:32:11 +01003573 base = d40_hw_detect_init(pdev);
Linus Walleij8d318a52010-03-30 15:33:42 +02003574 if (!base)
3575 goto failure;
3576
3577 num_reserved_chans = d40_phy_res_init(base);
3578
3579 platform_set_drvdata(pdev, base);
3580
3581 spin_lock_init(&base->interrupt_lock);
3582 spin_lock_init(&base->execmd_lock);
3583
3584 /* Get IO for logical channel parameter address */
3585 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3586 if (!res) {
3587 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003588 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003589 goto failure;
3590 }
3591 base->lcpa_size = resource_size(res);
3592 base->phy_lcpa = res->start;
3593
3594 if (request_mem_region(res->start, resource_size(res),
3595 D40_NAME " I/O lcpa") == NULL) {
3596 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003597 d40_err(&pdev->dev,
3598 "Failed to request LCPA region 0x%x-0x%x\n",
3599 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003600 goto failure;
3601 }
3602
3603 /* We make use of ESRAM memory for this. */
3604 val = readl(base->virtbase + D40_DREG_LCPA);
3605 if (res->start != val && val != 0) {
3606 dev_warn(&pdev->dev,
3607 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3608 __func__, val, res->start);
3609 } else
3610 writel(res->start, base->virtbase + D40_DREG_LCPA);
3611
3612 base->lcpa_base = ioremap(res->start, resource_size(res));
3613 if (!base->lcpa_base) {
3614 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003615 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003616 goto failure;
3617 }
Narayanan G28c7a192011-11-22 13:56:55 +05303618 /* If lcla has to be located in ESRAM we don't need to allocate */
3619 if (base->plat_data->use_esram_lcla) {
3620 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3621 "lcla_esram");
3622 if (!res) {
3623 ret = -ENOENT;
3624 d40_err(&pdev->dev,
3625 "No \"lcla_esram\" memory resource\n");
3626 goto failure;
3627 }
3628 base->lcla_pool.base = ioremap(res->start,
3629 resource_size(res));
3630 if (!base->lcla_pool.base) {
3631 ret = -ENOMEM;
3632 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3633 goto failure;
3634 }
3635 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003636
Narayanan G28c7a192011-11-22 13:56:55 +05303637 } else {
3638 ret = d40_lcla_allocate(base);
3639 if (ret) {
3640 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3641 goto failure;
3642 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003643 }
3644
Linus Walleij8d318a52010-03-30 15:33:42 +02003645 spin_lock_init(&base->lcla_pool.lock);
3646
Linus Walleij8d318a52010-03-30 15:33:42 +02003647 base->irq = platform_get_irq(pdev, 0);
3648
3649 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003650 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003651 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003652 goto failure;
3653 }
3654
Narayanan G7fb3e752011-11-17 17:26:41 +05303655 pm_runtime_irq_safe(base->dev);
3656 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3657 pm_runtime_use_autosuspend(base->dev);
3658 pm_runtime_enable(base->dev);
3659 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303660
3661 if (base->plat_data->use_esram_lcla) {
3662
3663 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3664 if (IS_ERR(base->lcpa_regulator)) {
3665 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3666 base->lcpa_regulator = NULL;
3667 goto failure;
3668 }
3669
3670 ret = regulator_enable(base->lcpa_regulator);
3671 if (ret) {
3672 d40_err(&pdev->dev,
3673 "Failed to enable lcpa_regulator\n");
3674 regulator_put(base->lcpa_regulator);
3675 base->lcpa_regulator = NULL;
3676 goto failure;
3677 }
3678 }
3679
Narayanan G7fb3e752011-11-17 17:26:41 +05303680 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003681 err = d40_dmaengine_init(base, num_reserved_chans);
3682 if (err)
3683 goto failure;
3684
Per Forlinb96710e2011-10-18 18:39:47 +02003685 base->dev->dma_parms = &base->dma_parms;
3686 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3687 if (err) {
3688 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3689 goto failure;
3690 }
3691
Linus Walleij8d318a52010-03-30 15:33:42 +02003692 d40_hw_init(base);
3693
Lee Jonesfa332de2013-05-03 15:32:12 +01003694 if (np) {
3695 err = of_dma_controller_register(np, d40_xlate, NULL);
3696 if (err && err != -ENODEV)
3697 dev_err(&pdev->dev,
3698 "could not register of_dma_controller\n");
3699 }
3700
Linus Walleij8d318a52010-03-30 15:33:42 +02003701 dev_info(base->dev, "initialized\n");
3702 return 0;
3703
3704failure:
3705 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003706 if (base->desc_slab)
3707 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003708 if (base->virtbase)
3709 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003710
Narayanan G28c7a192011-11-22 13:56:55 +05303711 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3712 iounmap(base->lcla_pool.base);
3713 base->lcla_pool.base = NULL;
3714 }
3715
Rabin Vincent026cbc42011-01-25 11:18:14 +01003716 if (base->lcla_pool.dma_addr)
3717 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3718 SZ_1K * base->num_phy_chans,
3719 DMA_TO_DEVICE);
3720
Linus Walleij508849a2010-06-20 21:26:07 +00003721 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3722 free_pages((unsigned long)base->lcla_pool.base,
3723 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003724
3725 kfree(base->lcla_pool.base_unaligned);
3726
Linus Walleij8d318a52010-03-30 15:33:42 +02003727 if (base->phy_lcpa)
3728 release_mem_region(base->phy_lcpa,
3729 base->lcpa_size);
3730 if (base->phy_start)
3731 release_mem_region(base->phy_start,
3732 base->phy_size);
3733 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003734 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003735 clk_put(base->clk);
3736 }
3737
Narayanan G28c7a192011-11-22 13:56:55 +05303738 if (base->lcpa_regulator) {
3739 regulator_disable(base->lcpa_regulator);
3740 regulator_put(base->lcpa_regulator);
3741 }
3742
Linus Walleij8d318a52010-03-30 15:33:42 +02003743 kfree(base->lcla_pool.alloc_map);
3744 kfree(base->lookup_log_chans);
3745 kfree(base->lookup_phy_chans);
3746 kfree(base->phy_res);
3747 kfree(base);
3748 }
3749
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003750 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003751 return ret;
3752}
3753
Lee Jones1814a172013-05-03 15:32:11 +01003754static const struct of_device_id d40_match[] = {
3755 { .compatible = "stericsson,dma40", },
3756 {}
3757};
3758
Linus Walleij8d318a52010-03-30 15:33:42 +02003759static struct platform_driver d40_driver = {
3760 .driver = {
3761 .owner = THIS_MODULE,
3762 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303763 .pm = DMA40_PM_OPS,
Lee Jones1814a172013-05-03 15:32:11 +01003764 .of_match_table = d40_match,
Linus Walleij8d318a52010-03-30 15:33:42 +02003765 },
3766};
3767
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003768static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003769{
3770 return platform_driver_probe(&d40_driver, d40_probe);
3771}
Linus Walleija0eb2212011-05-18 14:18:57 +02003772subsys_initcall(stedma40_init);