blob: 759293e4a1ccfc884d70a0d2c1ee94b651649e41 [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
2043 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2044 dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002045 d40c->dma_cfg = dma40_memcpy_conf_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002046 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002047 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002048 return -EINVAL;
2049 }
2050
2051 return 0;
2052}
2053
Linus Walleij8d318a52010-03-30 15:33:42 +02002054static int d40_free_dma(struct d40_chan *d40c)
2055{
2056
2057 int res = 0;
Lee Jones26955c07d2013-05-03 15:31:56 +01002058 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002059 struct d40_phy_res *phy = d40c->phy_chan;
2060 bool is_src;
2061
2062 /* Terminate all queued and active transfers */
2063 d40_term_all(d40c);
2064
2065 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002066 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002067 return -EINVAL;
2068 }
2069
2070 if (phy->allocated_src == D40_ALLOC_FREE &&
2071 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002072 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002073 return -EINVAL;
2074 }
2075
Linus Walleij8d318a52010-03-30 15:33:42 +02002076 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Lee Jones26955c07d2013-05-03 15:31:56 +01002077 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002078 is_src = false;
Lee Jones26955c07d2013-05-03 15:31:56 +01002079 else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002080 is_src = true;
Lee Jones26955c07d2013-05-03 15:31:56 +01002081 else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002082 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002083 return -EINVAL;
2084 }
2085
Narayanan G7fb3e752011-11-17 17:26:41 +05302086 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002087 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2088 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302089 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302090 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002091 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302092
Narayanan G1bdae6f2012-02-09 12:41:37 +05302093 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2094
2095 if (chan_is_logical(d40c))
2096 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2097 else
2098 d40c->base->lookup_phy_chans[phy->num] = NULL;
2099
Narayanan G7fb3e752011-11-17 17:26:41 +05302100 if (d40c->busy) {
2101 pm_runtime_mark_last_busy(d40c->base->dev);
2102 pm_runtime_put_autosuspend(d40c->base->dev);
2103 }
2104
2105 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002106 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002107 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302108out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002109
Narayanan G7fb3e752011-11-17 17:26:41 +05302110 pm_runtime_mark_last_busy(d40c->base->dev);
2111 pm_runtime_put_autosuspend(d40c->base->dev);
2112 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002113}
2114
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002115static bool d40_is_paused(struct d40_chan *d40c)
2116{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002117 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002118 bool is_paused = false;
2119 unsigned long flags;
2120 void __iomem *active_reg;
2121 u32 status;
Lee Jones26955c07d2013-05-03 15:31:56 +01002122 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002123
2124 spin_lock_irqsave(&d40c->lock, flags);
2125
Rabin Vincent724a8572011-01-25 11:18:08 +01002126 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002127 if (d40c->phy_chan->num % 2 == 0)
2128 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2129 else
2130 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2131
2132 status = (readl(active_reg) &
2133 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2134 D40_CHAN_POS(d40c->phy_chan->num);
2135 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2136 is_paused = true;
2137
2138 goto _exit;
2139 }
2140
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002141 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002142 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002143 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002144 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002145 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002146 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002147 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002148 goto _exit;
2149 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002150
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002151 status = (status & D40_EVENTLINE_MASK(event)) >>
2152 D40_EVENTLINE_POS(event);
2153
2154 if (status != D40_DMA_RUN)
2155 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002156_exit:
2157 spin_unlock_irqrestore(&d40c->lock, flags);
2158 return is_paused;
2159
2160}
2161
Linus Walleij8d318a52010-03-30 15:33:42 +02002162static u32 stedma40_residue(struct dma_chan *chan)
2163{
2164 struct d40_chan *d40c =
2165 container_of(chan, struct d40_chan, chan);
2166 u32 bytes_left;
2167 unsigned long flags;
2168
2169 spin_lock_irqsave(&d40c->lock, flags);
2170 bytes_left = d40_residue(d40c);
2171 spin_unlock_irqrestore(&d40c->lock, flags);
2172
2173 return bytes_left;
2174}
2175
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002176static int
2177d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2178 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002179 unsigned int sg_len, dma_addr_t src_dev_addr,
2180 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002181{
2182 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2183 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2184 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002185 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002186
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002187 ret = d40_log_sg_to_lli(sg_src, sg_len,
2188 src_dev_addr,
2189 desc->lli_log.src,
2190 chan->log_def.lcsp1,
2191 src_info->data_width,
2192 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002193
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002194 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2195 dst_dev_addr,
2196 desc->lli_log.dst,
2197 chan->log_def.lcsp3,
2198 dst_info->data_width,
2199 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002200
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002201 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002202}
2203
2204static int
2205d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2206 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002207 unsigned int sg_len, dma_addr_t src_dev_addr,
2208 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002209{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002210 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2211 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2212 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002213 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002214 int ret;
2215
Rabin Vincent0c842b52011-01-25 11:18:35 +01002216 if (desc->cyclic)
2217 flags |= LLI_CYCLIC | LLI_TERM_INT;
2218
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002219 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2220 desc->lli_phy.src,
2221 virt_to_phys(desc->lli_phy.src),
2222 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002223 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002224
2225 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2226 desc->lli_phy.dst,
2227 virt_to_phys(desc->lli_phy.dst),
2228 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002229 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002230
2231 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2232 desc->lli_pool.size, DMA_TO_DEVICE);
2233
2234 return ret < 0 ? ret : 0;
2235}
2236
Rabin Vincent5f811582011-01-25 11:18:18 +01002237static struct d40_desc *
2238d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2239 unsigned int sg_len, unsigned long dma_flags)
2240{
2241 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2242 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002243 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002244
2245 desc = d40_desc_get(chan);
2246 if (!desc)
2247 return NULL;
2248
2249 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2250 cfg->dst_info.data_width);
2251 if (desc->lli_len < 0) {
2252 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002253 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002254 }
2255
Rabin Vincentdbd88782011-01-25 11:18:19 +01002256 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2257 if (ret < 0) {
2258 chan_err(chan, "Could not allocate lli\n");
2259 goto err;
2260 }
2261
Rabin Vincent5f811582011-01-25 11:18:18 +01002262 desc->lli_current = 0;
2263 desc->txd.flags = dma_flags;
2264 desc->txd.tx_submit = d40_tx_submit;
2265
2266 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2267
2268 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002269
2270err:
2271 d40_desc_free(chan, desc);
2272 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002273}
2274
Rabin Vincentcade1d32011-01-25 11:18:23 +01002275static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302276d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002277{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002278 struct stedma40_platform_data *plat = chan->base->plat_data;
2279 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002280 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002281
Rabin Vincentcade1d32011-01-25 11:18:23 +01002282 if (chan->runtime_addr)
2283 return chan->runtime_addr;
2284
Vinod Kouldb8196d2011-10-13 22:34:23 +05302285 if (direction == DMA_DEV_TO_MEM)
Lee Jones26955c07d2013-05-03 15:31:56 +01002286 addr = plat->dev_rx[cfg->dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302287 else if (direction == DMA_MEM_TO_DEV)
Lee Jones26955c07d2013-05-03 15:31:56 +01002288 addr = plat->dev_tx[cfg->dev_type];
Rabin Vincentcade1d32011-01-25 11:18:23 +01002289
2290 return addr;
2291}
2292
2293static struct dma_async_tx_descriptor *
2294d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2295 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302296 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002297{
2298 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002299 dma_addr_t src_dev_addr = 0;
2300 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002301 struct d40_desc *desc;
2302 unsigned long flags;
2303 int ret;
2304
2305 if (!chan->phy_chan) {
2306 chan_err(chan, "Cannot prepare unallocated channel\n");
2307 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002308 }
2309
Rabin Vincentcade1d32011-01-25 11:18:23 +01002310 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002311
Rabin Vincentcade1d32011-01-25 11:18:23 +01002312 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2313 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002314 goto err;
2315
Rabin Vincent0c842b52011-01-25 11:18:35 +01002316 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2317 desc->cyclic = true;
2318
Linus Walleij7e426da2012-04-12 18:12:52 +02002319 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002320 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2321
Vinod Kouldb8196d2011-10-13 22:34:23 +05302322 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002323 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302324 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002325 dst_dev_addr = dev_addr;
2326 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002327
2328 if (chan_is_logical(chan))
2329 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002330 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002331 else
2332 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002333 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002334
2335 if (ret) {
2336 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2337 chan_is_logical(chan) ? "log" : "phy", ret);
2338 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002339 }
2340
Per Forlin82babbb362011-08-29 13:33:35 +02002341 /*
2342 * add descriptor to the prepare queue in order to be able
2343 * to free them later in terminate_all
2344 */
2345 list_add_tail(&desc->node, &chan->prepare_queue);
2346
Rabin Vincentcade1d32011-01-25 11:18:23 +01002347 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002348
Rabin Vincentcade1d32011-01-25 11:18:23 +01002349 return &desc->txd;
2350
Linus Walleij8d318a52010-03-30 15:33:42 +02002351err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002352 if (desc)
2353 d40_desc_free(chan, desc);
2354 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002355 return NULL;
2356}
Linus Walleij8d318a52010-03-30 15:33:42 +02002357
2358bool stedma40_filter(struct dma_chan *chan, void *data)
2359{
2360 struct stedma40_chan_cfg *info = data;
2361 struct d40_chan *d40c =
2362 container_of(chan, struct d40_chan, chan);
2363 int err;
2364
2365 if (data) {
2366 err = d40_validate_conf(d40c, info);
2367 if (!err)
2368 d40c->dma_cfg = *info;
2369 } else
2370 err = d40_config_memcpy(d40c);
2371
Rabin Vincentce2ca122010-10-12 13:00:49 +00002372 if (!err)
2373 d40c->configured = true;
2374
Linus Walleij8d318a52010-03-30 15:33:42 +02002375 return err == 0;
2376}
2377EXPORT_SYMBOL(stedma40_filter);
2378
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002379static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2380{
2381 bool realtime = d40c->dma_cfg.realtime;
2382 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002383 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002384 u32 event = D40_TYPE_TO_EVENT(dev_type);
2385 u32 group = D40_TYPE_TO_GROUP(dev_type);
2386 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302387 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002388 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302389
Tong Liu3cb645d2012-09-26 10:07:30 +00002390 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302391 /*
2392 * Due to a hardware bug, in some cases a logical channel triggered by
2393 * a high priority destination event line can generate extra packet
2394 * transactions.
2395 *
2396 * The workaround is to not set the high priority level for the
2397 * destination event lines that trigger logical channels.
2398 */
2399 if (!src && chan_is_logical(d40c))
2400 highprio = false;
2401
Tong Liu3cb645d2012-09-26 10:07:30 +00002402 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002403
2404 /* Destination event lines are stored in the upper halfword */
2405 if (!src)
2406 bit <<= 16;
2407
2408 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2409 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2410}
2411
2412static void d40_set_prio_realtime(struct d40_chan *d40c)
2413{
2414 if (d40c->base->rev < 3)
2415 return;
2416
2417 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2418 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002419 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002420
2421 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2422 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
Lee Jones26955c07d2013-05-03 15:31:56 +01002423 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002424}
2425
Lee Jonesfa332de2013-05-03 15:32:12 +01002426#define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2427#define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2428#define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2429#define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2430
2431static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2432 struct of_dma *ofdma)
2433{
2434 struct stedma40_chan_cfg cfg;
2435 dma_cap_mask_t cap;
2436 u32 flags;
2437
2438 memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2439
2440 dma_cap_zero(cap);
2441 dma_cap_set(DMA_SLAVE, cap);
2442
2443 cfg.dev_type = dma_spec->args[0];
2444 flags = dma_spec->args[2];
2445
2446 switch (D40_DT_FLAGS_MODE(flags)) {
2447 case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2448 case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2449 }
2450
2451 switch (D40_DT_FLAGS_DIR(flags)) {
2452 case 0:
2453 cfg.dir = STEDMA40_MEM_TO_PERIPH;
2454 cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2455 break;
2456 case 1:
2457 cfg.dir = STEDMA40_PERIPH_TO_MEM;
2458 cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2459 break;
2460 }
2461
2462 if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2463 cfg.phy_channel = dma_spec->args[1];
2464 cfg.use_fixed_channel = true;
2465 }
2466
2467 return dma_request_channel(cap, stedma40_filter, &cfg);
2468}
2469
Linus Walleij8d318a52010-03-30 15:33:42 +02002470/* DMA ENGINE functions */
2471static int d40_alloc_chan_resources(struct dma_chan *chan)
2472{
2473 int err;
2474 unsigned long flags;
2475 struct d40_chan *d40c =
2476 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002477 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002478 spin_lock_irqsave(&d40c->lock, flags);
2479
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002480 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002481
Rabin Vincentce2ca122010-10-12 13:00:49 +00002482 /* If no dma configuration is set use default configuration (memcpy) */
2483 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002484 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002485 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002486 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002487 goto fail;
2488 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002489 }
2490
Narayanan G5cd326f2011-11-30 19:20:42 +05302491 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002492 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002493 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302494 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002495 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002496 }
2497
Narayanan G7fb3e752011-11-17 17:26:41 +05302498 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002499 /* Fill in basic CFG register values */
2500 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002501 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002502
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002503 d40_set_prio_realtime(d40c);
2504
Rabin Vincent724a8572011-01-25 11:18:08 +01002505 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002506 d40_log_cfg(&d40c->dma_cfg,
2507 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2508
2509 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2510 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002511 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002512 else
2513 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002514 d40c->dma_cfg.dev_type *
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002515 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Lee Jones97782562013-05-15 10:51:24 +01002516
2517 /* Unmask the Global Interrupt Mask. */
2518 d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2519 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
Linus Walleijef1872e2010-06-20 21:24:52 +00002520 }
2521
Narayanan G5cd326f2011-11-30 19:20:42 +05302522 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2523 chan_is_logical(d40c) ? "logical" : "physical",
2524 d40c->phy_chan->num,
2525 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2526
2527
Linus Walleijef1872e2010-06-20 21:24:52 +00002528 /*
2529 * Only write channel configuration to the DMA if the physical
2530 * resource is free. In case of multiple logical channels
2531 * on the same physical resource, only the first write is necessary.
2532 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002533 if (is_free_phy)
2534 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002535fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302536 pm_runtime_mark_last_busy(d40c->base->dev);
2537 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002538 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002539 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002540}
2541
2542static void d40_free_chan_resources(struct dma_chan *chan)
2543{
2544 struct d40_chan *d40c =
2545 container_of(chan, struct d40_chan, chan);
2546 int err;
2547 unsigned long flags;
2548
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002549 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002550 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002551 return;
2552 }
2553
Linus Walleij8d318a52010-03-30 15:33:42 +02002554 spin_lock_irqsave(&d40c->lock, flags);
2555
2556 err = d40_free_dma(d40c);
2557
2558 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002559 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002560 spin_unlock_irqrestore(&d40c->lock, flags);
2561}
2562
2563static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2564 dma_addr_t dst,
2565 dma_addr_t src,
2566 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002567 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002568{
Rabin Vincent95944c62011-01-25 11:18:17 +01002569 struct scatterlist dst_sg;
2570 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002571
Rabin Vincent95944c62011-01-25 11:18:17 +01002572 sg_init_table(&dst_sg, 1);
2573 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002574
Rabin Vincent95944c62011-01-25 11:18:17 +01002575 sg_dma_address(&dst_sg) = dst;
2576 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002577
Rabin Vincent95944c62011-01-25 11:18:17 +01002578 sg_dma_len(&dst_sg) = size;
2579 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002580
Rabin Vincentcade1d32011-01-25 11:18:23 +01002581 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002582}
2583
Ira Snyder0d688662010-09-30 11:46:47 +00002584static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002585d40_prep_memcpy_sg(struct dma_chan *chan,
2586 struct scatterlist *dst_sg, unsigned int dst_nents,
2587 struct scatterlist *src_sg, unsigned int src_nents,
2588 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002589{
2590 if (dst_nents != src_nents)
2591 return NULL;
2592
Rabin Vincentcade1d32011-01-25 11:18:23 +01002593 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002594}
2595
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002596static struct dma_async_tx_descriptor *
2597d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2598 unsigned int sg_len, enum dma_transfer_direction direction,
2599 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002600{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002601 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002602 return NULL;
2603
Rabin Vincentcade1d32011-01-25 11:18:23 +01002604 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002605}
2606
Rabin Vincent0c842b52011-01-25 11:18:35 +01002607static struct dma_async_tx_descriptor *
2608dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2609 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002610 enum dma_transfer_direction direction, unsigned long flags,
2611 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002612{
2613 unsigned int periods = buf_len / period_len;
2614 struct dma_async_tx_descriptor *txd;
2615 struct scatterlist *sg;
2616 int i;
2617
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002618 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002619 for (i = 0; i < periods; i++) {
2620 sg_dma_address(&sg[i]) = dma_addr;
2621 sg_dma_len(&sg[i]) = period_len;
2622 dma_addr += period_len;
2623 }
2624
2625 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002626 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002627 sg[periods].page_link =
2628 ((unsigned long)sg | 0x01) & ~0x02;
2629
2630 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2631 DMA_PREP_INTERRUPT);
2632
2633 kfree(sg);
2634
2635 return txd;
2636}
2637
Linus Walleij8d318a52010-03-30 15:33:42 +02002638static enum dma_status d40_tx_status(struct dma_chan *chan,
2639 dma_cookie_t cookie,
2640 struct dma_tx_state *txstate)
2641{
2642 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002643 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002644
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002645 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002646 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002647 return -EINVAL;
2648 }
2649
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002650 ret = dma_cookie_status(chan, cookie, txstate);
2651 if (ret != DMA_SUCCESS)
2652 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002653
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002654 if (d40_is_paused(d40c))
2655 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002656
2657 return ret;
2658}
2659
2660static void d40_issue_pending(struct dma_chan *chan)
2661{
2662 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2663 unsigned long flags;
2664
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002665 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002666 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002667 return;
2668 }
2669
Linus Walleij8d318a52010-03-30 15:33:42 +02002670 spin_lock_irqsave(&d40c->lock, flags);
2671
Per Forlina8f30672011-06-26 23:29:52 +02002672 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2673
2674 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002675 if (!d40c->busy)
2676 (void) d40_queue_start(d40c);
2677
2678 spin_unlock_irqrestore(&d40c->lock, flags);
2679}
2680
Narayanan G1bdae6f2012-02-09 12:41:37 +05302681static void d40_terminate_all(struct dma_chan *chan)
2682{
2683 unsigned long flags;
2684 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2685 int ret;
2686
2687 spin_lock_irqsave(&d40c->lock, flags);
2688
2689 pm_runtime_get_sync(d40c->base->dev);
2690 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2691 if (ret)
2692 chan_err(d40c, "Failed to stop channel\n");
2693
2694 d40_term_all(d40c);
2695 pm_runtime_mark_last_busy(d40c->base->dev);
2696 pm_runtime_put_autosuspend(d40c->base->dev);
2697 if (d40c->busy) {
2698 pm_runtime_mark_last_busy(d40c->base->dev);
2699 pm_runtime_put_autosuspend(d40c->base->dev);
2700 }
2701 d40c->busy = false;
2702
2703 spin_unlock_irqrestore(&d40c->lock, flags);
2704}
2705
Rabin Vincent98ca5282011-06-27 11:33:38 +02002706static int
2707dma40_config_to_halfchannel(struct d40_chan *d40c,
2708 struct stedma40_half_channel_info *info,
2709 enum dma_slave_buswidth width,
2710 u32 maxburst)
2711{
2712 enum stedma40_periph_data_width addr_width;
2713 int psize;
2714
2715 switch (width) {
2716 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2717 addr_width = STEDMA40_BYTE_WIDTH;
2718 break;
2719 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2720 addr_width = STEDMA40_HALFWORD_WIDTH;
2721 break;
2722 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2723 addr_width = STEDMA40_WORD_WIDTH;
2724 break;
2725 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2726 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2727 break;
2728 default:
2729 dev_err(d40c->base->dev,
2730 "illegal peripheral address width "
2731 "requested (%d)\n",
2732 width);
2733 return -EINVAL;
2734 }
2735
2736 if (chan_is_logical(d40c)) {
2737 if (maxburst >= 16)
2738 psize = STEDMA40_PSIZE_LOG_16;
2739 else if (maxburst >= 8)
2740 psize = STEDMA40_PSIZE_LOG_8;
2741 else if (maxburst >= 4)
2742 psize = STEDMA40_PSIZE_LOG_4;
2743 else
2744 psize = STEDMA40_PSIZE_LOG_1;
2745 } else {
2746 if (maxburst >= 16)
2747 psize = STEDMA40_PSIZE_PHY_16;
2748 else if (maxburst >= 8)
2749 psize = STEDMA40_PSIZE_PHY_8;
2750 else if (maxburst >= 4)
2751 psize = STEDMA40_PSIZE_PHY_4;
2752 else
2753 psize = STEDMA40_PSIZE_PHY_1;
2754 }
2755
2756 info->data_width = addr_width;
2757 info->psize = psize;
2758 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2759
2760 return 0;
2761}
2762
Linus Walleij95e14002010-08-04 13:37:45 +02002763/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002764static int d40_set_runtime_config(struct dma_chan *chan,
2765 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002766{
2767 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2768 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002769 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002770 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002771 u32 src_maxburst, dst_maxburst;
2772 int ret;
2773
2774 src_addr_width = config->src_addr_width;
2775 src_maxburst = config->src_maxburst;
2776 dst_addr_width = config->dst_addr_width;
2777 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002778
Vinod Kouldb8196d2011-10-13 22:34:23 +05302779 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002780 dma_addr_t dev_addr_rx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002781 d40c->base->plat_data->dev_rx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002782
2783 config_addr = config->src_addr;
2784 if (dev_addr_rx)
2785 dev_dbg(d40c->base->dev,
2786 "channel has a pre-wired RX address %08x "
2787 "overriding with %08x\n",
2788 dev_addr_rx, config_addr);
2789 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2790 dev_dbg(d40c->base->dev,
2791 "channel was not configured for peripheral "
2792 "to memory transfer (%d) overriding\n",
2793 cfg->dir);
2794 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2795
Rabin Vincent98ca5282011-06-27 11:33:38 +02002796 /* Configure the memory side */
2797 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2798 dst_addr_width = src_addr_width;
2799 if (dst_maxburst == 0)
2800 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002801
Vinod Kouldb8196d2011-10-13 22:34:23 +05302802 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002803 dma_addr_t dev_addr_tx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002804 d40c->base->plat_data->dev_tx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002805
2806 config_addr = config->dst_addr;
2807 if (dev_addr_tx)
2808 dev_dbg(d40c->base->dev,
2809 "channel has a pre-wired TX address %08x "
2810 "overriding with %08x\n",
2811 dev_addr_tx, config_addr);
2812 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2813 dev_dbg(d40c->base->dev,
2814 "channel was not configured for memory "
2815 "to peripheral transfer (%d) overriding\n",
2816 cfg->dir);
2817 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2818
Rabin Vincent98ca5282011-06-27 11:33:38 +02002819 /* Configure the memory side */
2820 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2821 src_addr_width = dst_addr_width;
2822 if (src_maxburst == 0)
2823 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002824 } else {
2825 dev_err(d40c->base->dev,
2826 "unrecognized channel direction %d\n",
2827 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002828 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002829 }
2830
Rabin Vincent98ca5282011-06-27 11:33:38 +02002831 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002832 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002833 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2834 src_maxburst,
2835 src_addr_width,
2836 dst_maxburst,
2837 dst_addr_width);
2838 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002839 }
2840
Per Forlin92bb6cd2011-10-13 12:11:36 +02002841 if (src_maxburst > 16) {
2842 src_maxburst = 16;
2843 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2844 } else if (dst_maxburst > 16) {
2845 dst_maxburst = 16;
2846 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2847 }
2848
Rabin Vincent98ca5282011-06-27 11:33:38 +02002849 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2850 src_addr_width,
2851 src_maxburst);
2852 if (ret)
2853 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002854
Rabin Vincent98ca5282011-06-27 11:33:38 +02002855 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2856 dst_addr_width,
2857 dst_maxburst);
2858 if (ret)
2859 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002860
Per Forlina59670a2010-10-06 09:05:27 +00002861 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002862 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002863 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2864 else
2865 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2866 &d40c->dst_def_cfg, false);
2867
Linus Walleij95e14002010-08-04 13:37:45 +02002868 /* These settings will take precedence later */
2869 d40c->runtime_addr = config_addr;
2870 d40c->runtime_direction = config->direction;
2871 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002872 "configured channel %s for %s, data width %d/%d, "
2873 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002874 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302875 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002876 src_addr_width, dst_addr_width,
2877 src_maxburst, dst_maxburst);
2878
2879 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002880}
2881
Linus Walleij05827632010-05-17 16:30:42 -07002882static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2883 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002884{
Linus Walleij8d318a52010-03-30 15:33:42 +02002885 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2886
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002887 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002888 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002889 return -EINVAL;
2890 }
2891
Linus Walleij8d318a52010-03-30 15:33:42 +02002892 switch (cmd) {
2893 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302894 d40_terminate_all(chan);
2895 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002896 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002897 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002898 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002899 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002900 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002901 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002902 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002903 default:
2904 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002905 }
2906
2907 /* Other commands are unimplemented */
2908 return -ENXIO;
2909}
2910
2911/* Initialization functions */
2912
2913static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2914 struct d40_chan *chans, int offset,
2915 int num_chans)
2916{
2917 int i = 0;
2918 struct d40_chan *d40c;
2919
2920 INIT_LIST_HEAD(&dma->channels);
2921
2922 for (i = offset; i < offset + num_chans; i++) {
2923 d40c = &chans[i];
2924 d40c->base = base;
2925 d40c->chan.device = dma;
2926
Linus Walleij8d318a52010-03-30 15:33:42 +02002927 spin_lock_init(&d40c->lock);
2928
2929 d40c->log_num = D40_PHY_CHAN;
2930
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002931 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002932 INIT_LIST_HEAD(&d40c->active);
2933 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002934 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002935 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002936 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002937
Linus Walleij8d318a52010-03-30 15:33:42 +02002938 tasklet_init(&d40c->tasklet, dma_tasklet,
2939 (unsigned long) d40c);
2940
2941 list_add_tail(&d40c->chan.device_node,
2942 &dma->channels);
2943 }
2944}
2945
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002946static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2947{
2948 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2949 dev->device_prep_slave_sg = d40_prep_slave_sg;
2950
2951 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2952 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2953
2954 /*
2955 * This controller can only access address at even
2956 * 32bit boundaries, i.e. 2^2
2957 */
2958 dev->copy_align = 2;
2959 }
2960
2961 if (dma_has_cap(DMA_SG, dev->cap_mask))
2962 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2963
Rabin Vincent0c842b52011-01-25 11:18:35 +01002964 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2965 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2966
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002967 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2968 dev->device_free_chan_resources = d40_free_chan_resources;
2969 dev->device_issue_pending = d40_issue_pending;
2970 dev->device_tx_status = d40_tx_status;
2971 dev->device_control = d40_control;
2972 dev->dev = base->dev;
2973}
2974
Linus Walleij8d318a52010-03-30 15:33:42 +02002975static int __init d40_dmaengine_init(struct d40_base *base,
2976 int num_reserved_chans)
2977{
2978 int err ;
2979
2980 d40_chan_init(base, &base->dma_slave, base->log_chans,
2981 0, base->num_log_chans);
2982
2983 dma_cap_zero(base->dma_slave.cap_mask);
2984 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002985 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002986
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002987 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002988
2989 err = dma_async_device_register(&base->dma_slave);
2990
2991 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002992 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002993 goto failure1;
2994 }
2995
2996 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002997 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002998
2999 dma_cap_zero(base->dma_memcpy.cap_mask);
3000 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003001 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02003002
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003003 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02003004
3005 err = dma_async_device_register(&base->dma_memcpy);
3006
3007 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003008 d40_err(base->dev,
3009 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003010 goto failure2;
3011 }
3012
3013 d40_chan_init(base, &base->dma_both, base->phy_chans,
3014 0, num_reserved_chans);
3015
3016 dma_cap_zero(base->dma_both.cap_mask);
3017 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
3018 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003019 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01003020 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02003021
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003022 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02003023 err = dma_async_device_register(&base->dma_both);
3024
3025 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003026 d40_err(base->dev,
3027 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003028 goto failure3;
3029 }
3030 return 0;
3031failure3:
3032 dma_async_device_unregister(&base->dma_memcpy);
3033failure2:
3034 dma_async_device_unregister(&base->dma_slave);
3035failure1:
3036 return err;
3037}
3038
Narayanan G7fb3e752011-11-17 17:26:41 +05303039/* Suspend resume functionality */
3040#ifdef CONFIG_PM
3041static int dma40_pm_suspend(struct device *dev)
3042{
Narayanan G28c7a192011-11-22 13:56:55 +05303043 struct platform_device *pdev = to_platform_device(dev);
3044 struct d40_base *base = platform_get_drvdata(pdev);
3045 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05303046
Narayanan G28c7a192011-11-22 13:56:55 +05303047 if (base->lcpa_regulator)
3048 ret = regulator_disable(base->lcpa_regulator);
3049 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05303050}
3051
3052static int dma40_runtime_suspend(struct device *dev)
3053{
3054 struct platform_device *pdev = to_platform_device(dev);
3055 struct d40_base *base = platform_get_drvdata(pdev);
3056
3057 d40_save_restore_registers(base, true);
3058
3059 /* Don't disable/enable clocks for v1 due to HW bugs */
3060 if (base->rev != 1)
3061 writel_relaxed(base->gcc_pwr_off_mask,
3062 base->virtbase + D40_DREG_GCC);
3063
3064 return 0;
3065}
3066
3067static int dma40_runtime_resume(struct device *dev)
3068{
3069 struct platform_device *pdev = to_platform_device(dev);
3070 struct d40_base *base = platform_get_drvdata(pdev);
3071
3072 if (base->initialized)
3073 d40_save_restore_registers(base, false);
3074
3075 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3076 base->virtbase + D40_DREG_GCC);
3077 return 0;
3078}
3079
Narayanan G28c7a192011-11-22 13:56:55 +05303080static int dma40_resume(struct device *dev)
3081{
3082 struct platform_device *pdev = to_platform_device(dev);
3083 struct d40_base *base = platform_get_drvdata(pdev);
3084 int ret = 0;
3085
3086 if (base->lcpa_regulator)
3087 ret = regulator_enable(base->lcpa_regulator);
3088
3089 return ret;
3090}
Narayanan G7fb3e752011-11-17 17:26:41 +05303091
3092static const struct dev_pm_ops dma40_pm_ops = {
3093 .suspend = dma40_pm_suspend,
3094 .runtime_suspend = dma40_runtime_suspend,
3095 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303096 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303097};
3098#define DMA40_PM_OPS (&dma40_pm_ops)
3099#else
3100#define DMA40_PM_OPS NULL
3101#endif
3102
Linus Walleij8d318a52010-03-30 15:33:42 +02003103/* Initialization functions. */
3104
3105static int __init d40_phy_res_init(struct d40_base *base)
3106{
3107 int i;
3108 int num_phy_chans_avail = 0;
3109 u32 val[2];
3110 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303111 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003112
3113 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3114 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3115
3116 for (i = 0; i < base->num_phy_chans; i++) {
3117 base->phy_res[i].num = i;
3118 odd_even_bit += 2 * ((i % 2) == 0);
3119 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3120 /* Mark security only channels as occupied */
3121 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3122 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303123 base->phy_res[i].reserved = true;
3124 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3125 D40_DREG_GCC_SRC);
3126 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3127 D40_DREG_GCC_DST);
3128
3129
Linus Walleij8d318a52010-03-30 15:33:42 +02003130 } else {
3131 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3132 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303133 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003134 num_phy_chans_avail++;
3135 }
3136 spin_lock_init(&base->phy_res[i].lock);
3137 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003138
3139 /* Mark disabled channels as occupied */
3140 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003141 int chan = base->plat_data->disabled_channels[i];
3142
3143 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3144 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303145 base->phy_res[chan].reserved = true;
3146 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3147 D40_DREG_GCC_SRC);
3148 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3149 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003150 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003151 }
3152
Fabio Baltieri74070482012-12-18 12:25:14 +01003153 /* Mark soft_lli channels */
3154 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3155 int chan = base->plat_data->soft_lli_chans[i];
3156
3157 base->phy_res[chan].use_soft_lli = true;
3158 }
3159
Linus Walleij8d318a52010-03-30 15:33:42 +02003160 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3161 num_phy_chans_avail, base->num_phy_chans);
3162
3163 /* Verify settings extended vs standard */
3164 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3165
3166 for (i = 0; i < base->num_phy_chans; i++) {
3167
3168 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3169 (val[0] & 0x3) != 1)
3170 dev_info(base->dev,
3171 "[%s] INFO: channel %d is misconfigured (%d)\n",
3172 __func__, i, val[0] & 0x3);
3173
3174 val[0] = val[0] >> 2;
3175 }
3176
Narayanan G7fb3e752011-11-17 17:26:41 +05303177 /*
3178 * To keep things simple, Enable all clocks initially.
3179 * The clocks will get managed later post channel allocation.
3180 * The clocks for the event lines on which reserved channels exists
3181 * are not managed here.
3182 */
3183 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3184 base->gcc_pwr_off_mask = gcc;
3185
Linus Walleij8d318a52010-03-30 15:33:42 +02003186 return num_phy_chans_avail;
3187}
3188
3189static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3190{
Lee Jonesbb75d932013-05-03 15:32:10 +01003191 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003192 struct clk *clk = NULL;
3193 void __iomem *virtbase = NULL;
3194 struct resource *res = NULL;
3195 struct d40_base *base = NULL;
3196 int num_log_chans = 0;
3197 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003198 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003199 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003200 u32 pid;
3201 u32 cid;
3202 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003203
3204 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003205 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003206 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003207 goto failure;
3208 }
3209
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003210 clk_ret = clk_prepare_enable(clk);
3211 if (clk_ret) {
3212 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3213 goto failure;
3214 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003215
3216 /* Get IO for DMAC base address */
3217 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3218 if (!res)
3219 goto failure;
3220
3221 if (request_mem_region(res->start, resource_size(res),
3222 D40_NAME " I/O base") == NULL)
3223 goto failure;
3224
3225 virtbase = ioremap(res->start, resource_size(res));
3226 if (!virtbase)
3227 goto failure;
3228
Linus Walleijf4b89762011-06-27 11:33:46 +02003229 /* This is just a regular AMBA PrimeCell ID actually */
3230 for (pid = 0, i = 0; i < 4; i++)
3231 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3232 & 255) << (i * 8);
3233 for (cid = 0, i = 0; i < 4; i++)
3234 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3235 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003236
Linus Walleijf4b89762011-06-27 11:33:46 +02003237 if (cid != AMBA_CID) {
3238 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003239 goto failure;
3240 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003241 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3242 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3243 AMBA_MANF_BITS(pid),
3244 AMBA_VENDOR_ST);
3245 goto failure;
3246 }
3247 /*
3248 * HW revision:
3249 * DB8500ed has revision 0
3250 * ? has revision 1
3251 * DB8500v1 has revision 2
3252 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003253 * AP9540v1 has revision 4
3254 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003255 */
3256 rev = AMBA_REV_BITS(pid);
Lee Jones8b2fe9b2013-05-03 15:32:08 +01003257 if (rev < 2) {
3258 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3259 goto failure;
3260 }
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003261
Gerald Baeza47db92f2012-09-21 21:21:37 +02003262 /* The number of physical channels on this HW */
3263 if (plat_data->num_of_phy_chans)
3264 num_phy_chans = plat_data->num_of_phy_chans;
3265 else
3266 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3267
Lee Jonesdb72da92013-05-03 15:32:03 +01003268 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3269
Lee Jonesb2abb242013-05-03 15:32:09 +01003270 dev_info(&pdev->dev,
3271 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
3272 rev, res->start, num_phy_chans, num_log_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003273
Linus Walleij8d318a52010-03-30 15:33:42 +02003274 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003275 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003276 sizeof(struct d40_chan), GFP_KERNEL);
3277
3278 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003279 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003280 goto failure;
3281 }
3282
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003283 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003284 base->clk = clk;
3285 base->num_phy_chans = num_phy_chans;
3286 base->num_log_chans = num_log_chans;
3287 base->phy_start = res->start;
3288 base->phy_size = resource_size(res);
3289 base->virtbase = virtbase;
3290 base->plat_data = plat_data;
3291 base->dev = &pdev->dev;
3292 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3293 base->log_chans = &base->phy_chans[num_phy_chans];
3294
Tong Liu3cb645d2012-09-26 10:07:30 +00003295 if (base->plat_data->num_of_phy_chans == 14) {
3296 base->gen_dmac.backup = d40_backup_regs_v4b;
3297 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3298 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3299 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3300 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3301 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3302 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3303 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3304 base->gen_dmac.il = il_v4b;
3305 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3306 base->gen_dmac.init_reg = dma_init_reg_v4b;
3307 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3308 } else {
3309 if (base->rev >= 3) {
3310 base->gen_dmac.backup = d40_backup_regs_v4a;
3311 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3312 }
3313 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3314 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3315 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3316 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3317 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3318 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3319 base->gen_dmac.il = il_v4a;
3320 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3321 base->gen_dmac.init_reg = dma_init_reg_v4a;
3322 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3323 }
3324
Linus Walleij8d318a52010-03-30 15:33:42 +02003325 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3326 GFP_KERNEL);
3327 if (!base->phy_res)
3328 goto failure;
3329
3330 base->lookup_phy_chans = kzalloc(num_phy_chans *
3331 sizeof(struct d40_chan *),
3332 GFP_KERNEL);
3333 if (!base->lookup_phy_chans)
3334 goto failure;
3335
Lee Jones8a59fed2013-05-03 15:32:04 +01003336 base->lookup_log_chans = kzalloc(num_log_chans *
3337 sizeof(struct d40_chan *),
3338 GFP_KERNEL);
3339 if (!base->lookup_log_chans)
3340 goto failure;
Jonas Aaberg698e4732010-08-09 12:08:56 +00003341
Narayanan G7fb3e752011-11-17 17:26:41 +05303342 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3343 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003344 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303345 if (!base->reg_val_backup_chan)
3346 goto failure;
3347
3348 base->lcla_pool.alloc_map =
3349 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3350 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003351 if (!base->lcla_pool.alloc_map)
3352 goto failure;
3353
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003354 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3355 0, SLAB_HWCACHE_ALIGN,
3356 NULL);
3357 if (base->desc_slab == NULL)
3358 goto failure;
3359
Linus Walleij8d318a52010-03-30 15:33:42 +02003360 return base;
3361
3362failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003363 if (!clk_ret)
3364 clk_disable_unprepare(clk);
3365 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003366 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003367 if (virtbase)
3368 iounmap(virtbase);
3369 if (res)
3370 release_mem_region(res->start,
3371 resource_size(res));
3372 if (virtbase)
3373 iounmap(virtbase);
3374
3375 if (base) {
3376 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303377 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003378 kfree(base->lookup_log_chans);
3379 kfree(base->lookup_phy_chans);
3380 kfree(base->phy_res);
3381 kfree(base);
3382 }
3383
3384 return NULL;
3385}
3386
3387static void __init d40_hw_init(struct d40_base *base)
3388{
3389
Linus Walleij8d318a52010-03-30 15:33:42 +02003390 int i;
3391 u32 prmseo[2] = {0, 0};
3392 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3393 u32 pcmis = 0;
3394 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003395 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3396 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003397
Tong Liu3cb645d2012-09-26 10:07:30 +00003398 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003399 writel(dma_init_reg[i].val,
3400 base->virtbase + dma_init_reg[i].reg);
3401
3402 /* Configure all our dma channels to default settings */
3403 for (i = 0; i < base->num_phy_chans; i++) {
3404
3405 activeo[i % 2] = activeo[i % 2] << 2;
3406
3407 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3408 == D40_ALLOC_PHY) {
3409 activeo[i % 2] |= 3;
3410 continue;
3411 }
3412
3413 /* Enable interrupt # */
3414 pcmis = (pcmis << 1) | 1;
3415
3416 /* Clear interrupt # */
3417 pcicr = (pcicr << 1) | 1;
3418
3419 /* Set channel to physical mode */
3420 prmseo[i % 2] = prmseo[i % 2] << 2;
3421 prmseo[i % 2] |= 1;
3422
3423 }
3424
3425 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3426 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3427 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3428 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3429
3430 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003431 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003432
3433 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003434 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003435
Tong Liu3cb645d2012-09-26 10:07:30 +00003436 /* These are __initdata and cannot be accessed after init */
3437 base->gen_dmac.init_reg = NULL;
3438 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003439}
3440
Linus Walleij508849a2010-06-20 21:26:07 +00003441static int __init d40_lcla_allocate(struct d40_base *base)
3442{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003443 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003444 unsigned long *page_list;
3445 int i, j;
3446 int ret = 0;
3447
3448 /*
3449 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3450 * To full fill this hardware requirement without wasting 256 kb
3451 * we allocate pages until we get an aligned one.
3452 */
3453 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3454 GFP_KERNEL);
3455
3456 if (!page_list) {
3457 ret = -ENOMEM;
3458 goto failure;
3459 }
3460
3461 /* Calculating how many pages that are required */
3462 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3463
3464 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3465 page_list[i] = __get_free_pages(GFP_KERNEL,
3466 base->lcla_pool.pages);
3467 if (!page_list[i]) {
3468
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003469 d40_err(base->dev, "Failed to allocate %d pages.\n",
3470 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003471
3472 for (j = 0; j < i; j++)
3473 free_pages(page_list[j], base->lcla_pool.pages);
3474 goto failure;
3475 }
3476
3477 if ((virt_to_phys((void *)page_list[i]) &
3478 (LCLA_ALIGNMENT - 1)) == 0)
3479 break;
3480 }
3481
3482 for (j = 0; j < i; j++)
3483 free_pages(page_list[j], base->lcla_pool.pages);
3484
3485 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3486 base->lcla_pool.base = (void *)page_list[i];
3487 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003488 /*
3489 * After many attempts and no succees with finding the correct
3490 * alignment, try with allocating a big buffer.
3491 */
Linus Walleij508849a2010-06-20 21:26:07 +00003492 dev_warn(base->dev,
3493 "[%s] Failed to get %d pages @ 18 bit align.\n",
3494 __func__, base->lcla_pool.pages);
3495 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3496 base->num_phy_chans +
3497 LCLA_ALIGNMENT,
3498 GFP_KERNEL);
3499 if (!base->lcla_pool.base_unaligned) {
3500 ret = -ENOMEM;
3501 goto failure;
3502 }
3503
3504 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3505 LCLA_ALIGNMENT);
3506 }
3507
Rabin Vincent026cbc42011-01-25 11:18:14 +01003508 pool->dma_addr = dma_map_single(base->dev, pool->base,
3509 SZ_1K * base->num_phy_chans,
3510 DMA_TO_DEVICE);
3511 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3512 pool->dma_addr = 0;
3513 ret = -ENOMEM;
3514 goto failure;
3515 }
3516
Linus Walleij508849a2010-06-20 21:26:07 +00003517 writel(virt_to_phys(base->lcla_pool.base),
3518 base->virtbase + D40_DREG_LCLA);
3519failure:
3520 kfree(page_list);
3521 return ret;
3522}
3523
Lee Jones1814a172013-05-03 15:32:11 +01003524static int __init d40_of_probe(struct platform_device *pdev,
3525 struct device_node *np)
3526{
3527 struct stedma40_platform_data *pdata;
3528
3529 /*
3530 * FIXME: Fill in this routine as more support is added.
3531 * First platform enabled (u8500) doens't need any extra
3532 * properties to run, so this is fairly sparce currently.
3533 */
3534
3535 pdata = devm_kzalloc(&pdev->dev,
3536 sizeof(struct stedma40_platform_data),
3537 GFP_KERNEL);
3538 if (!pdata)
3539 return -ENOMEM;
3540
3541 pdev->dev.platform_data = pdata;
3542
3543 return 0;
3544}
3545
Linus Walleij8d318a52010-03-30 15:33:42 +02003546static int __init d40_probe(struct platform_device *pdev)
3547{
Lee Jones1814a172013-05-03 15:32:11 +01003548 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3549 struct device_node *np = pdev->dev.of_node;
Linus Walleij8d318a52010-03-30 15:33:42 +02003550 int err;
3551 int ret = -ENOENT;
Lee Jones1814a172013-05-03 15:32:11 +01003552 struct d40_base *base = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003553 struct resource *res = NULL;
3554 int num_reserved_chans;
3555 u32 val;
3556
Lee Jones1814a172013-05-03 15:32:11 +01003557 if (!plat_data) {
3558 if (np) {
3559 if(d40_of_probe(pdev, np)) {
3560 ret = -ENOMEM;
3561 goto failure;
3562 }
3563 } else {
3564 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3565 goto failure;
3566 }
3567 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003568
Lee Jones1814a172013-05-03 15:32:11 +01003569 base = d40_hw_detect_init(pdev);
Linus Walleij8d318a52010-03-30 15:33:42 +02003570 if (!base)
3571 goto failure;
3572
3573 num_reserved_chans = d40_phy_res_init(base);
3574
3575 platform_set_drvdata(pdev, base);
3576
3577 spin_lock_init(&base->interrupt_lock);
3578 spin_lock_init(&base->execmd_lock);
3579
3580 /* Get IO for logical channel parameter address */
3581 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3582 if (!res) {
3583 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003584 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003585 goto failure;
3586 }
3587 base->lcpa_size = resource_size(res);
3588 base->phy_lcpa = res->start;
3589
3590 if (request_mem_region(res->start, resource_size(res),
3591 D40_NAME " I/O lcpa") == NULL) {
3592 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003593 d40_err(&pdev->dev,
3594 "Failed to request LCPA region 0x%x-0x%x\n",
3595 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003596 goto failure;
3597 }
3598
3599 /* We make use of ESRAM memory for this. */
3600 val = readl(base->virtbase + D40_DREG_LCPA);
3601 if (res->start != val && val != 0) {
3602 dev_warn(&pdev->dev,
3603 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3604 __func__, val, res->start);
3605 } else
3606 writel(res->start, base->virtbase + D40_DREG_LCPA);
3607
3608 base->lcpa_base = ioremap(res->start, resource_size(res));
3609 if (!base->lcpa_base) {
3610 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003611 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003612 goto failure;
3613 }
Narayanan G28c7a192011-11-22 13:56:55 +05303614 /* If lcla has to be located in ESRAM we don't need to allocate */
3615 if (base->plat_data->use_esram_lcla) {
3616 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3617 "lcla_esram");
3618 if (!res) {
3619 ret = -ENOENT;
3620 d40_err(&pdev->dev,
3621 "No \"lcla_esram\" memory resource\n");
3622 goto failure;
3623 }
3624 base->lcla_pool.base = ioremap(res->start,
3625 resource_size(res));
3626 if (!base->lcla_pool.base) {
3627 ret = -ENOMEM;
3628 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3629 goto failure;
3630 }
3631 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003632
Narayanan G28c7a192011-11-22 13:56:55 +05303633 } else {
3634 ret = d40_lcla_allocate(base);
3635 if (ret) {
3636 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3637 goto failure;
3638 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003639 }
3640
Linus Walleij8d318a52010-03-30 15:33:42 +02003641 spin_lock_init(&base->lcla_pool.lock);
3642
Linus Walleij8d318a52010-03-30 15:33:42 +02003643 base->irq = platform_get_irq(pdev, 0);
3644
3645 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003646 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003647 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003648 goto failure;
3649 }
3650
Narayanan G7fb3e752011-11-17 17:26:41 +05303651 pm_runtime_irq_safe(base->dev);
3652 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3653 pm_runtime_use_autosuspend(base->dev);
3654 pm_runtime_enable(base->dev);
3655 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303656
3657 if (base->plat_data->use_esram_lcla) {
3658
3659 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3660 if (IS_ERR(base->lcpa_regulator)) {
3661 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3662 base->lcpa_regulator = NULL;
3663 goto failure;
3664 }
3665
3666 ret = regulator_enable(base->lcpa_regulator);
3667 if (ret) {
3668 d40_err(&pdev->dev,
3669 "Failed to enable lcpa_regulator\n");
3670 regulator_put(base->lcpa_regulator);
3671 base->lcpa_regulator = NULL;
3672 goto failure;
3673 }
3674 }
3675
Narayanan G7fb3e752011-11-17 17:26:41 +05303676 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003677 err = d40_dmaengine_init(base, num_reserved_chans);
3678 if (err)
3679 goto failure;
3680
Per Forlinb96710e2011-10-18 18:39:47 +02003681 base->dev->dma_parms = &base->dma_parms;
3682 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3683 if (err) {
3684 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3685 goto failure;
3686 }
3687
Linus Walleij8d318a52010-03-30 15:33:42 +02003688 d40_hw_init(base);
3689
Lee Jonesfa332de2013-05-03 15:32:12 +01003690 if (np) {
3691 err = of_dma_controller_register(np, d40_xlate, NULL);
3692 if (err && err != -ENODEV)
3693 dev_err(&pdev->dev,
3694 "could not register of_dma_controller\n");
3695 }
3696
Linus Walleij8d318a52010-03-30 15:33:42 +02003697 dev_info(base->dev, "initialized\n");
3698 return 0;
3699
3700failure:
3701 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003702 if (base->desc_slab)
3703 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003704 if (base->virtbase)
3705 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003706
Narayanan G28c7a192011-11-22 13:56:55 +05303707 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3708 iounmap(base->lcla_pool.base);
3709 base->lcla_pool.base = NULL;
3710 }
3711
Rabin Vincent026cbc42011-01-25 11:18:14 +01003712 if (base->lcla_pool.dma_addr)
3713 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3714 SZ_1K * base->num_phy_chans,
3715 DMA_TO_DEVICE);
3716
Linus Walleij508849a2010-06-20 21:26:07 +00003717 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3718 free_pages((unsigned long)base->lcla_pool.base,
3719 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003720
3721 kfree(base->lcla_pool.base_unaligned);
3722
Linus Walleij8d318a52010-03-30 15:33:42 +02003723 if (base->phy_lcpa)
3724 release_mem_region(base->phy_lcpa,
3725 base->lcpa_size);
3726 if (base->phy_start)
3727 release_mem_region(base->phy_start,
3728 base->phy_size);
3729 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003730 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003731 clk_put(base->clk);
3732 }
3733
Narayanan G28c7a192011-11-22 13:56:55 +05303734 if (base->lcpa_regulator) {
3735 regulator_disable(base->lcpa_regulator);
3736 regulator_put(base->lcpa_regulator);
3737 }
3738
Linus Walleij8d318a52010-03-30 15:33:42 +02003739 kfree(base->lcla_pool.alloc_map);
3740 kfree(base->lookup_log_chans);
3741 kfree(base->lookup_phy_chans);
3742 kfree(base->phy_res);
3743 kfree(base);
3744 }
3745
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003746 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003747 return ret;
3748}
3749
Lee Jones1814a172013-05-03 15:32:11 +01003750static const struct of_device_id d40_match[] = {
3751 { .compatible = "stericsson,dma40", },
3752 {}
3753};
3754
Linus Walleij8d318a52010-03-30 15:33:42 +02003755static struct platform_driver d40_driver = {
3756 .driver = {
3757 .owner = THIS_MODULE,
3758 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303759 .pm = DMA40_PM_OPS,
Lee Jones1814a172013-05-03 15:32:11 +01003760 .of_match_table = d40_match,
Linus Walleij8d318a52010-03-30 15:33:42 +02003761 },
3762};
3763
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003764static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003765{
3766 return platform_driver_probe(&d40_driver, d40_probe);
3767}
Linus Walleija0eb2212011-05-18 14:18:57 +02003768subsys_initcall(stedma40_init);