blob: 5e9f6d67089126f98bd66b345cd4055b8dbd915c [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;
Linus Walleijef1872e2010-06-20 21:24:52 +00002516 }
2517
Narayanan G5cd326f2011-11-30 19:20:42 +05302518 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2519 chan_is_logical(d40c) ? "logical" : "physical",
2520 d40c->phy_chan->num,
2521 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2522
2523
Linus Walleijef1872e2010-06-20 21:24:52 +00002524 /*
2525 * Only write channel configuration to the DMA if the physical
2526 * resource is free. In case of multiple logical channels
2527 * on the same physical resource, only the first write is necessary.
2528 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002529 if (is_free_phy)
2530 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002531fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302532 pm_runtime_mark_last_busy(d40c->base->dev);
2533 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002534 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002535 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002536}
2537
2538static void d40_free_chan_resources(struct dma_chan *chan)
2539{
2540 struct d40_chan *d40c =
2541 container_of(chan, struct d40_chan, chan);
2542 int err;
2543 unsigned long flags;
2544
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002545 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002546 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002547 return;
2548 }
2549
Linus Walleij8d318a52010-03-30 15:33:42 +02002550 spin_lock_irqsave(&d40c->lock, flags);
2551
2552 err = d40_free_dma(d40c);
2553
2554 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002555 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002556 spin_unlock_irqrestore(&d40c->lock, flags);
2557}
2558
2559static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2560 dma_addr_t dst,
2561 dma_addr_t src,
2562 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002563 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002564{
Rabin Vincent95944c62011-01-25 11:18:17 +01002565 struct scatterlist dst_sg;
2566 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002567
Rabin Vincent95944c62011-01-25 11:18:17 +01002568 sg_init_table(&dst_sg, 1);
2569 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002570
Rabin Vincent95944c62011-01-25 11:18:17 +01002571 sg_dma_address(&dst_sg) = dst;
2572 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002573
Rabin Vincent95944c62011-01-25 11:18:17 +01002574 sg_dma_len(&dst_sg) = size;
2575 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002576
Rabin Vincentcade1d32011-01-25 11:18:23 +01002577 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002578}
2579
Ira Snyder0d688662010-09-30 11:46:47 +00002580static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002581d40_prep_memcpy_sg(struct dma_chan *chan,
2582 struct scatterlist *dst_sg, unsigned int dst_nents,
2583 struct scatterlist *src_sg, unsigned int src_nents,
2584 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002585{
2586 if (dst_nents != src_nents)
2587 return NULL;
2588
Rabin Vincentcade1d32011-01-25 11:18:23 +01002589 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002590}
2591
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002592static struct dma_async_tx_descriptor *
2593d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2594 unsigned int sg_len, enum dma_transfer_direction direction,
2595 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002596{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002597 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002598 return NULL;
2599
Rabin Vincentcade1d32011-01-25 11:18:23 +01002600 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002601}
2602
Rabin Vincent0c842b52011-01-25 11:18:35 +01002603static struct dma_async_tx_descriptor *
2604dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2605 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002606 enum dma_transfer_direction direction, unsigned long flags,
2607 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002608{
2609 unsigned int periods = buf_len / period_len;
2610 struct dma_async_tx_descriptor *txd;
2611 struct scatterlist *sg;
2612 int i;
2613
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002614 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002615 for (i = 0; i < periods; i++) {
2616 sg_dma_address(&sg[i]) = dma_addr;
2617 sg_dma_len(&sg[i]) = period_len;
2618 dma_addr += period_len;
2619 }
2620
2621 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002622 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002623 sg[periods].page_link =
2624 ((unsigned long)sg | 0x01) & ~0x02;
2625
2626 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2627 DMA_PREP_INTERRUPT);
2628
2629 kfree(sg);
2630
2631 return txd;
2632}
2633
Linus Walleij8d318a52010-03-30 15:33:42 +02002634static enum dma_status d40_tx_status(struct dma_chan *chan,
2635 dma_cookie_t cookie,
2636 struct dma_tx_state *txstate)
2637{
2638 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002639 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002640
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002641 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002642 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002643 return -EINVAL;
2644 }
2645
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002646 ret = dma_cookie_status(chan, cookie, txstate);
2647 if (ret != DMA_SUCCESS)
2648 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002649
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002650 if (d40_is_paused(d40c))
2651 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002652
2653 return ret;
2654}
2655
2656static void d40_issue_pending(struct dma_chan *chan)
2657{
2658 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2659 unsigned long flags;
2660
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002661 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002662 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002663 return;
2664 }
2665
Linus Walleij8d318a52010-03-30 15:33:42 +02002666 spin_lock_irqsave(&d40c->lock, flags);
2667
Per Forlina8f30672011-06-26 23:29:52 +02002668 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2669
2670 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002671 if (!d40c->busy)
2672 (void) d40_queue_start(d40c);
2673
2674 spin_unlock_irqrestore(&d40c->lock, flags);
2675}
2676
Narayanan G1bdae6f2012-02-09 12:41:37 +05302677static void d40_terminate_all(struct dma_chan *chan)
2678{
2679 unsigned long flags;
2680 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2681 int ret;
2682
2683 spin_lock_irqsave(&d40c->lock, flags);
2684
2685 pm_runtime_get_sync(d40c->base->dev);
2686 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2687 if (ret)
2688 chan_err(d40c, "Failed to stop channel\n");
2689
2690 d40_term_all(d40c);
2691 pm_runtime_mark_last_busy(d40c->base->dev);
2692 pm_runtime_put_autosuspend(d40c->base->dev);
2693 if (d40c->busy) {
2694 pm_runtime_mark_last_busy(d40c->base->dev);
2695 pm_runtime_put_autosuspend(d40c->base->dev);
2696 }
2697 d40c->busy = false;
2698
2699 spin_unlock_irqrestore(&d40c->lock, flags);
2700}
2701
Rabin Vincent98ca5282011-06-27 11:33:38 +02002702static int
2703dma40_config_to_halfchannel(struct d40_chan *d40c,
2704 struct stedma40_half_channel_info *info,
2705 enum dma_slave_buswidth width,
2706 u32 maxburst)
2707{
2708 enum stedma40_periph_data_width addr_width;
2709 int psize;
2710
2711 switch (width) {
2712 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2713 addr_width = STEDMA40_BYTE_WIDTH;
2714 break;
2715 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2716 addr_width = STEDMA40_HALFWORD_WIDTH;
2717 break;
2718 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2719 addr_width = STEDMA40_WORD_WIDTH;
2720 break;
2721 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2722 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2723 break;
2724 default:
2725 dev_err(d40c->base->dev,
2726 "illegal peripheral address width "
2727 "requested (%d)\n",
2728 width);
2729 return -EINVAL;
2730 }
2731
2732 if (chan_is_logical(d40c)) {
2733 if (maxburst >= 16)
2734 psize = STEDMA40_PSIZE_LOG_16;
2735 else if (maxburst >= 8)
2736 psize = STEDMA40_PSIZE_LOG_8;
2737 else if (maxburst >= 4)
2738 psize = STEDMA40_PSIZE_LOG_4;
2739 else
2740 psize = STEDMA40_PSIZE_LOG_1;
2741 } else {
2742 if (maxburst >= 16)
2743 psize = STEDMA40_PSIZE_PHY_16;
2744 else if (maxburst >= 8)
2745 psize = STEDMA40_PSIZE_PHY_8;
2746 else if (maxburst >= 4)
2747 psize = STEDMA40_PSIZE_PHY_4;
2748 else
2749 psize = STEDMA40_PSIZE_PHY_1;
2750 }
2751
2752 info->data_width = addr_width;
2753 info->psize = psize;
2754 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2755
2756 return 0;
2757}
2758
Linus Walleij95e14002010-08-04 13:37:45 +02002759/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002760static int d40_set_runtime_config(struct dma_chan *chan,
2761 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002762{
2763 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2764 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002765 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002766 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002767 u32 src_maxburst, dst_maxburst;
2768 int ret;
2769
2770 src_addr_width = config->src_addr_width;
2771 src_maxburst = config->src_maxburst;
2772 dst_addr_width = config->dst_addr_width;
2773 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002774
Vinod Kouldb8196d2011-10-13 22:34:23 +05302775 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002776 dma_addr_t dev_addr_rx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002777 d40c->base->plat_data->dev_rx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002778
2779 config_addr = config->src_addr;
2780 if (dev_addr_rx)
2781 dev_dbg(d40c->base->dev,
2782 "channel has a pre-wired RX address %08x "
2783 "overriding with %08x\n",
2784 dev_addr_rx, config_addr);
2785 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2786 dev_dbg(d40c->base->dev,
2787 "channel was not configured for peripheral "
2788 "to memory transfer (%d) overriding\n",
2789 cfg->dir);
2790 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2791
Rabin Vincent98ca5282011-06-27 11:33:38 +02002792 /* Configure the memory side */
2793 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2794 dst_addr_width = src_addr_width;
2795 if (dst_maxburst == 0)
2796 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002797
Vinod Kouldb8196d2011-10-13 22:34:23 +05302798 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002799 dma_addr_t dev_addr_tx =
Lee Jones26955c07d2013-05-03 15:31:56 +01002800 d40c->base->plat_data->dev_tx[cfg->dev_type];
Linus Walleij95e14002010-08-04 13:37:45 +02002801
2802 config_addr = config->dst_addr;
2803 if (dev_addr_tx)
2804 dev_dbg(d40c->base->dev,
2805 "channel has a pre-wired TX address %08x "
2806 "overriding with %08x\n",
2807 dev_addr_tx, config_addr);
2808 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2809 dev_dbg(d40c->base->dev,
2810 "channel was not configured for memory "
2811 "to peripheral transfer (%d) overriding\n",
2812 cfg->dir);
2813 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2814
Rabin Vincent98ca5282011-06-27 11:33:38 +02002815 /* Configure the memory side */
2816 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2817 src_addr_width = dst_addr_width;
2818 if (src_maxburst == 0)
2819 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002820 } else {
2821 dev_err(d40c->base->dev,
2822 "unrecognized channel direction %d\n",
2823 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002824 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002825 }
2826
Rabin Vincent98ca5282011-06-27 11:33:38 +02002827 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002828 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002829 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2830 src_maxburst,
2831 src_addr_width,
2832 dst_maxburst,
2833 dst_addr_width);
2834 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002835 }
2836
Per Forlin92bb6cd2011-10-13 12:11:36 +02002837 if (src_maxburst > 16) {
2838 src_maxburst = 16;
2839 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2840 } else if (dst_maxburst > 16) {
2841 dst_maxburst = 16;
2842 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2843 }
2844
Rabin Vincent98ca5282011-06-27 11:33:38 +02002845 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2846 src_addr_width,
2847 src_maxburst);
2848 if (ret)
2849 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002850
Rabin Vincent98ca5282011-06-27 11:33:38 +02002851 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2852 dst_addr_width,
2853 dst_maxburst);
2854 if (ret)
2855 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002856
Per Forlina59670a2010-10-06 09:05:27 +00002857 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002858 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002859 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2860 else
2861 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2862 &d40c->dst_def_cfg, false);
2863
Linus Walleij95e14002010-08-04 13:37:45 +02002864 /* These settings will take precedence later */
2865 d40c->runtime_addr = config_addr;
2866 d40c->runtime_direction = config->direction;
2867 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002868 "configured channel %s for %s, data width %d/%d, "
2869 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002870 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302871 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002872 src_addr_width, dst_addr_width,
2873 src_maxburst, dst_maxburst);
2874
2875 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002876}
2877
Linus Walleij05827632010-05-17 16:30:42 -07002878static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2879 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002880{
Linus Walleij8d318a52010-03-30 15:33:42 +02002881 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2882
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002883 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002884 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002885 return -EINVAL;
2886 }
2887
Linus Walleij8d318a52010-03-30 15:33:42 +02002888 switch (cmd) {
2889 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302890 d40_terminate_all(chan);
2891 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002892 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002893 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002894 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002895 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002896 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002897 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002898 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002899 default:
2900 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002901 }
2902
2903 /* Other commands are unimplemented */
2904 return -ENXIO;
2905}
2906
2907/* Initialization functions */
2908
2909static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2910 struct d40_chan *chans, int offset,
2911 int num_chans)
2912{
2913 int i = 0;
2914 struct d40_chan *d40c;
2915
2916 INIT_LIST_HEAD(&dma->channels);
2917
2918 for (i = offset; i < offset + num_chans; i++) {
2919 d40c = &chans[i];
2920 d40c->base = base;
2921 d40c->chan.device = dma;
2922
Linus Walleij8d318a52010-03-30 15:33:42 +02002923 spin_lock_init(&d40c->lock);
2924
2925 d40c->log_num = D40_PHY_CHAN;
2926
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002927 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002928 INIT_LIST_HEAD(&d40c->active);
2929 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002930 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002931 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002932 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002933
Linus Walleij8d318a52010-03-30 15:33:42 +02002934 tasklet_init(&d40c->tasklet, dma_tasklet,
2935 (unsigned long) d40c);
2936
2937 list_add_tail(&d40c->chan.device_node,
2938 &dma->channels);
2939 }
2940}
2941
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002942static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2943{
2944 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2945 dev->device_prep_slave_sg = d40_prep_slave_sg;
2946
2947 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2948 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2949
2950 /*
2951 * This controller can only access address at even
2952 * 32bit boundaries, i.e. 2^2
2953 */
2954 dev->copy_align = 2;
2955 }
2956
2957 if (dma_has_cap(DMA_SG, dev->cap_mask))
2958 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2959
Rabin Vincent0c842b52011-01-25 11:18:35 +01002960 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2961 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2962
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002963 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2964 dev->device_free_chan_resources = d40_free_chan_resources;
2965 dev->device_issue_pending = d40_issue_pending;
2966 dev->device_tx_status = d40_tx_status;
2967 dev->device_control = d40_control;
2968 dev->dev = base->dev;
2969}
2970
Linus Walleij8d318a52010-03-30 15:33:42 +02002971static int __init d40_dmaengine_init(struct d40_base *base,
2972 int num_reserved_chans)
2973{
2974 int err ;
2975
2976 d40_chan_init(base, &base->dma_slave, base->log_chans,
2977 0, base->num_log_chans);
2978
2979 dma_cap_zero(base->dma_slave.cap_mask);
2980 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002981 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002982
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002983 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002984
2985 err = dma_async_device_register(&base->dma_slave);
2986
2987 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002988 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002989 goto failure1;
2990 }
2991
2992 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002993 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002994
2995 dma_cap_zero(base->dma_memcpy.cap_mask);
2996 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002997 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002998
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002999 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02003000
3001 err = dma_async_device_register(&base->dma_memcpy);
3002
3003 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003004 d40_err(base->dev,
3005 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003006 goto failure2;
3007 }
3008
3009 d40_chan_init(base, &base->dma_both, base->phy_chans,
3010 0, num_reserved_chans);
3011
3012 dma_cap_zero(base->dma_both.cap_mask);
3013 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
3014 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003015 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01003016 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02003017
Rabin Vincent7ad74a72011-01-25 11:18:33 +01003018 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02003019 err = dma_async_device_register(&base->dma_both);
3020
3021 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003022 d40_err(base->dev,
3023 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003024 goto failure3;
3025 }
3026 return 0;
3027failure3:
3028 dma_async_device_unregister(&base->dma_memcpy);
3029failure2:
3030 dma_async_device_unregister(&base->dma_slave);
3031failure1:
3032 return err;
3033}
3034
Narayanan G7fb3e752011-11-17 17:26:41 +05303035/* Suspend resume functionality */
3036#ifdef CONFIG_PM
3037static int dma40_pm_suspend(struct device *dev)
3038{
Narayanan G28c7a192011-11-22 13:56:55 +05303039 struct platform_device *pdev = to_platform_device(dev);
3040 struct d40_base *base = platform_get_drvdata(pdev);
3041 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05303042
Narayanan G28c7a192011-11-22 13:56:55 +05303043 if (base->lcpa_regulator)
3044 ret = regulator_disable(base->lcpa_regulator);
3045 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05303046}
3047
3048static int dma40_runtime_suspend(struct device *dev)
3049{
3050 struct platform_device *pdev = to_platform_device(dev);
3051 struct d40_base *base = platform_get_drvdata(pdev);
3052
3053 d40_save_restore_registers(base, true);
3054
3055 /* Don't disable/enable clocks for v1 due to HW bugs */
3056 if (base->rev != 1)
3057 writel_relaxed(base->gcc_pwr_off_mask,
3058 base->virtbase + D40_DREG_GCC);
3059
3060 return 0;
3061}
3062
3063static int dma40_runtime_resume(struct device *dev)
3064{
3065 struct platform_device *pdev = to_platform_device(dev);
3066 struct d40_base *base = platform_get_drvdata(pdev);
3067
3068 if (base->initialized)
3069 d40_save_restore_registers(base, false);
3070
3071 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3072 base->virtbase + D40_DREG_GCC);
3073 return 0;
3074}
3075
Narayanan G28c7a192011-11-22 13:56:55 +05303076static int dma40_resume(struct device *dev)
3077{
3078 struct platform_device *pdev = to_platform_device(dev);
3079 struct d40_base *base = platform_get_drvdata(pdev);
3080 int ret = 0;
3081
3082 if (base->lcpa_regulator)
3083 ret = regulator_enable(base->lcpa_regulator);
3084
3085 return ret;
3086}
Narayanan G7fb3e752011-11-17 17:26:41 +05303087
3088static const struct dev_pm_ops dma40_pm_ops = {
3089 .suspend = dma40_pm_suspend,
3090 .runtime_suspend = dma40_runtime_suspend,
3091 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303092 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303093};
3094#define DMA40_PM_OPS (&dma40_pm_ops)
3095#else
3096#define DMA40_PM_OPS NULL
3097#endif
3098
Linus Walleij8d318a52010-03-30 15:33:42 +02003099/* Initialization functions. */
3100
3101static int __init d40_phy_res_init(struct d40_base *base)
3102{
3103 int i;
3104 int num_phy_chans_avail = 0;
3105 u32 val[2];
3106 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303107 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003108
3109 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3110 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3111
3112 for (i = 0; i < base->num_phy_chans; i++) {
3113 base->phy_res[i].num = i;
3114 odd_even_bit += 2 * ((i % 2) == 0);
3115 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3116 /* Mark security only channels as occupied */
3117 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3118 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303119 base->phy_res[i].reserved = true;
3120 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3121 D40_DREG_GCC_SRC);
3122 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3123 D40_DREG_GCC_DST);
3124
3125
Linus Walleij8d318a52010-03-30 15:33:42 +02003126 } else {
3127 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3128 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303129 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003130 num_phy_chans_avail++;
3131 }
3132 spin_lock_init(&base->phy_res[i].lock);
3133 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003134
3135 /* Mark disabled channels as occupied */
3136 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003137 int chan = base->plat_data->disabled_channels[i];
3138
3139 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3140 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303141 base->phy_res[chan].reserved = true;
3142 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3143 D40_DREG_GCC_SRC);
3144 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3145 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003146 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003147 }
3148
Fabio Baltieri74070482012-12-18 12:25:14 +01003149 /* Mark soft_lli channels */
3150 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3151 int chan = base->plat_data->soft_lli_chans[i];
3152
3153 base->phy_res[chan].use_soft_lli = true;
3154 }
3155
Linus Walleij8d318a52010-03-30 15:33:42 +02003156 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3157 num_phy_chans_avail, base->num_phy_chans);
3158
3159 /* Verify settings extended vs standard */
3160 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3161
3162 for (i = 0; i < base->num_phy_chans; i++) {
3163
3164 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3165 (val[0] & 0x3) != 1)
3166 dev_info(base->dev,
3167 "[%s] INFO: channel %d is misconfigured (%d)\n",
3168 __func__, i, val[0] & 0x3);
3169
3170 val[0] = val[0] >> 2;
3171 }
3172
Narayanan G7fb3e752011-11-17 17:26:41 +05303173 /*
3174 * To keep things simple, Enable all clocks initially.
3175 * The clocks will get managed later post channel allocation.
3176 * The clocks for the event lines on which reserved channels exists
3177 * are not managed here.
3178 */
3179 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3180 base->gcc_pwr_off_mask = gcc;
3181
Linus Walleij8d318a52010-03-30 15:33:42 +02003182 return num_phy_chans_avail;
3183}
3184
3185static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3186{
Lee Jonesbb75d932013-05-03 15:32:10 +01003187 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003188 struct clk *clk = NULL;
3189 void __iomem *virtbase = NULL;
3190 struct resource *res = NULL;
3191 struct d40_base *base = NULL;
3192 int num_log_chans = 0;
3193 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003194 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003195 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003196 u32 pid;
3197 u32 cid;
3198 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003199
3200 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003201 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003202 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003203 goto failure;
3204 }
3205
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003206 clk_ret = clk_prepare_enable(clk);
3207 if (clk_ret) {
3208 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3209 goto failure;
3210 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003211
3212 /* Get IO for DMAC base address */
3213 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3214 if (!res)
3215 goto failure;
3216
3217 if (request_mem_region(res->start, resource_size(res),
3218 D40_NAME " I/O base") == NULL)
3219 goto failure;
3220
3221 virtbase = ioremap(res->start, resource_size(res));
3222 if (!virtbase)
3223 goto failure;
3224
Linus Walleijf4b89762011-06-27 11:33:46 +02003225 /* This is just a regular AMBA PrimeCell ID actually */
3226 for (pid = 0, i = 0; i < 4; i++)
3227 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3228 & 255) << (i * 8);
3229 for (cid = 0, i = 0; i < 4; i++)
3230 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3231 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003232
Linus Walleijf4b89762011-06-27 11:33:46 +02003233 if (cid != AMBA_CID) {
3234 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003235 goto failure;
3236 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003237 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3238 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3239 AMBA_MANF_BITS(pid),
3240 AMBA_VENDOR_ST);
3241 goto failure;
3242 }
3243 /*
3244 * HW revision:
3245 * DB8500ed has revision 0
3246 * ? has revision 1
3247 * DB8500v1 has revision 2
3248 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003249 * AP9540v1 has revision 4
3250 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003251 */
3252 rev = AMBA_REV_BITS(pid);
Lee Jones8b2fe9b2013-05-03 15:32:08 +01003253 if (rev < 2) {
3254 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3255 goto failure;
3256 }
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003257
Gerald Baeza47db92f2012-09-21 21:21:37 +02003258 /* The number of physical channels on this HW */
3259 if (plat_data->num_of_phy_chans)
3260 num_phy_chans = plat_data->num_of_phy_chans;
3261 else
3262 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3263
Lee Jonesdb72da92013-05-03 15:32:03 +01003264 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3265
Lee Jonesb2abb242013-05-03 15:32:09 +01003266 dev_info(&pdev->dev,
3267 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
3268 rev, res->start, num_phy_chans, num_log_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003269
Linus Walleij8d318a52010-03-30 15:33:42 +02003270 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003271 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003272 sizeof(struct d40_chan), GFP_KERNEL);
3273
3274 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003275 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003276 goto failure;
3277 }
3278
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003279 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003280 base->clk = clk;
3281 base->num_phy_chans = num_phy_chans;
3282 base->num_log_chans = num_log_chans;
3283 base->phy_start = res->start;
3284 base->phy_size = resource_size(res);
3285 base->virtbase = virtbase;
3286 base->plat_data = plat_data;
3287 base->dev = &pdev->dev;
3288 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3289 base->log_chans = &base->phy_chans[num_phy_chans];
3290
Tong Liu3cb645d2012-09-26 10:07:30 +00003291 if (base->plat_data->num_of_phy_chans == 14) {
3292 base->gen_dmac.backup = d40_backup_regs_v4b;
3293 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3294 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3295 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3296 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3297 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3298 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3299 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3300 base->gen_dmac.il = il_v4b;
3301 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3302 base->gen_dmac.init_reg = dma_init_reg_v4b;
3303 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3304 } else {
3305 if (base->rev >= 3) {
3306 base->gen_dmac.backup = d40_backup_regs_v4a;
3307 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3308 }
3309 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3310 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3311 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3312 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3313 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3314 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3315 base->gen_dmac.il = il_v4a;
3316 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3317 base->gen_dmac.init_reg = dma_init_reg_v4a;
3318 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3319 }
3320
Linus Walleij8d318a52010-03-30 15:33:42 +02003321 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3322 GFP_KERNEL);
3323 if (!base->phy_res)
3324 goto failure;
3325
3326 base->lookup_phy_chans = kzalloc(num_phy_chans *
3327 sizeof(struct d40_chan *),
3328 GFP_KERNEL);
3329 if (!base->lookup_phy_chans)
3330 goto failure;
3331
Lee Jones8a59fed2013-05-03 15:32:04 +01003332 base->lookup_log_chans = kzalloc(num_log_chans *
3333 sizeof(struct d40_chan *),
3334 GFP_KERNEL);
3335 if (!base->lookup_log_chans)
3336 goto failure;
Jonas Aaberg698e4732010-08-09 12:08:56 +00003337
Narayanan G7fb3e752011-11-17 17:26:41 +05303338 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3339 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003340 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303341 if (!base->reg_val_backup_chan)
3342 goto failure;
3343
3344 base->lcla_pool.alloc_map =
3345 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3346 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003347 if (!base->lcla_pool.alloc_map)
3348 goto failure;
3349
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003350 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3351 0, SLAB_HWCACHE_ALIGN,
3352 NULL);
3353 if (base->desc_slab == NULL)
3354 goto failure;
3355
Linus Walleij8d318a52010-03-30 15:33:42 +02003356 return base;
3357
3358failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003359 if (!clk_ret)
3360 clk_disable_unprepare(clk);
3361 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003362 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003363 if (virtbase)
3364 iounmap(virtbase);
3365 if (res)
3366 release_mem_region(res->start,
3367 resource_size(res));
3368 if (virtbase)
3369 iounmap(virtbase);
3370
3371 if (base) {
3372 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303373 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003374 kfree(base->lookup_log_chans);
3375 kfree(base->lookup_phy_chans);
3376 kfree(base->phy_res);
3377 kfree(base);
3378 }
3379
3380 return NULL;
3381}
3382
3383static void __init d40_hw_init(struct d40_base *base)
3384{
3385
Linus Walleij8d318a52010-03-30 15:33:42 +02003386 int i;
3387 u32 prmseo[2] = {0, 0};
3388 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3389 u32 pcmis = 0;
3390 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003391 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3392 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003393
Tong Liu3cb645d2012-09-26 10:07:30 +00003394 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003395 writel(dma_init_reg[i].val,
3396 base->virtbase + dma_init_reg[i].reg);
3397
3398 /* Configure all our dma channels to default settings */
3399 for (i = 0; i < base->num_phy_chans; i++) {
3400
3401 activeo[i % 2] = activeo[i % 2] << 2;
3402
3403 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3404 == D40_ALLOC_PHY) {
3405 activeo[i % 2] |= 3;
3406 continue;
3407 }
3408
3409 /* Enable interrupt # */
3410 pcmis = (pcmis << 1) | 1;
3411
3412 /* Clear interrupt # */
3413 pcicr = (pcicr << 1) | 1;
3414
3415 /* Set channel to physical mode */
3416 prmseo[i % 2] = prmseo[i % 2] << 2;
3417 prmseo[i % 2] |= 1;
3418
3419 }
3420
3421 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3422 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3423 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3424 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3425
3426 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003427 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003428
3429 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003430 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003431
Tong Liu3cb645d2012-09-26 10:07:30 +00003432 /* These are __initdata and cannot be accessed after init */
3433 base->gen_dmac.init_reg = NULL;
3434 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003435}
3436
Linus Walleij508849a2010-06-20 21:26:07 +00003437static int __init d40_lcla_allocate(struct d40_base *base)
3438{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003439 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003440 unsigned long *page_list;
3441 int i, j;
3442 int ret = 0;
3443
3444 /*
3445 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3446 * To full fill this hardware requirement without wasting 256 kb
3447 * we allocate pages until we get an aligned one.
3448 */
3449 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3450 GFP_KERNEL);
3451
3452 if (!page_list) {
3453 ret = -ENOMEM;
3454 goto failure;
3455 }
3456
3457 /* Calculating how many pages that are required */
3458 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3459
3460 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3461 page_list[i] = __get_free_pages(GFP_KERNEL,
3462 base->lcla_pool.pages);
3463 if (!page_list[i]) {
3464
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003465 d40_err(base->dev, "Failed to allocate %d pages.\n",
3466 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003467
3468 for (j = 0; j < i; j++)
3469 free_pages(page_list[j], base->lcla_pool.pages);
3470 goto failure;
3471 }
3472
3473 if ((virt_to_phys((void *)page_list[i]) &
3474 (LCLA_ALIGNMENT - 1)) == 0)
3475 break;
3476 }
3477
3478 for (j = 0; j < i; j++)
3479 free_pages(page_list[j], base->lcla_pool.pages);
3480
3481 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3482 base->lcla_pool.base = (void *)page_list[i];
3483 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003484 /*
3485 * After many attempts and no succees with finding the correct
3486 * alignment, try with allocating a big buffer.
3487 */
Linus Walleij508849a2010-06-20 21:26:07 +00003488 dev_warn(base->dev,
3489 "[%s] Failed to get %d pages @ 18 bit align.\n",
3490 __func__, base->lcla_pool.pages);
3491 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3492 base->num_phy_chans +
3493 LCLA_ALIGNMENT,
3494 GFP_KERNEL);
3495 if (!base->lcla_pool.base_unaligned) {
3496 ret = -ENOMEM;
3497 goto failure;
3498 }
3499
3500 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3501 LCLA_ALIGNMENT);
3502 }
3503
Rabin Vincent026cbc42011-01-25 11:18:14 +01003504 pool->dma_addr = dma_map_single(base->dev, pool->base,
3505 SZ_1K * base->num_phy_chans,
3506 DMA_TO_DEVICE);
3507 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3508 pool->dma_addr = 0;
3509 ret = -ENOMEM;
3510 goto failure;
3511 }
3512
Linus Walleij508849a2010-06-20 21:26:07 +00003513 writel(virt_to_phys(base->lcla_pool.base),
3514 base->virtbase + D40_DREG_LCLA);
3515failure:
3516 kfree(page_list);
3517 return ret;
3518}
3519
Lee Jones1814a172013-05-03 15:32:11 +01003520static int __init d40_of_probe(struct platform_device *pdev,
3521 struct device_node *np)
3522{
3523 struct stedma40_platform_data *pdata;
3524
3525 /*
3526 * FIXME: Fill in this routine as more support is added.
3527 * First platform enabled (u8500) doens't need any extra
3528 * properties to run, so this is fairly sparce currently.
3529 */
3530
3531 pdata = devm_kzalloc(&pdev->dev,
3532 sizeof(struct stedma40_platform_data),
3533 GFP_KERNEL);
3534 if (!pdata)
3535 return -ENOMEM;
3536
3537 pdev->dev.platform_data = pdata;
3538
3539 return 0;
3540}
3541
Linus Walleij8d318a52010-03-30 15:33:42 +02003542static int __init d40_probe(struct platform_device *pdev)
3543{
Lee Jones1814a172013-05-03 15:32:11 +01003544 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3545 struct device_node *np = pdev->dev.of_node;
Linus Walleij8d318a52010-03-30 15:33:42 +02003546 int err;
3547 int ret = -ENOENT;
Lee Jones1814a172013-05-03 15:32:11 +01003548 struct d40_base *base = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003549 struct resource *res = NULL;
3550 int num_reserved_chans;
3551 u32 val;
3552
Lee Jones1814a172013-05-03 15:32:11 +01003553 if (!plat_data) {
3554 if (np) {
3555 if(d40_of_probe(pdev, np)) {
3556 ret = -ENOMEM;
3557 goto failure;
3558 }
3559 } else {
3560 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3561 goto failure;
3562 }
3563 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003564
Lee Jones1814a172013-05-03 15:32:11 +01003565 base = d40_hw_detect_init(pdev);
Linus Walleij8d318a52010-03-30 15:33:42 +02003566 if (!base)
3567 goto failure;
3568
3569 num_reserved_chans = d40_phy_res_init(base);
3570
3571 platform_set_drvdata(pdev, base);
3572
3573 spin_lock_init(&base->interrupt_lock);
3574 spin_lock_init(&base->execmd_lock);
3575
3576 /* Get IO for logical channel parameter address */
3577 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3578 if (!res) {
3579 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003580 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003581 goto failure;
3582 }
3583 base->lcpa_size = resource_size(res);
3584 base->phy_lcpa = res->start;
3585
3586 if (request_mem_region(res->start, resource_size(res),
3587 D40_NAME " I/O lcpa") == NULL) {
3588 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003589 d40_err(&pdev->dev,
3590 "Failed to request LCPA region 0x%x-0x%x\n",
3591 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003592 goto failure;
3593 }
3594
3595 /* We make use of ESRAM memory for this. */
3596 val = readl(base->virtbase + D40_DREG_LCPA);
3597 if (res->start != val && val != 0) {
3598 dev_warn(&pdev->dev,
3599 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3600 __func__, val, res->start);
3601 } else
3602 writel(res->start, base->virtbase + D40_DREG_LCPA);
3603
3604 base->lcpa_base = ioremap(res->start, resource_size(res));
3605 if (!base->lcpa_base) {
3606 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003607 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003608 goto failure;
3609 }
Narayanan G28c7a192011-11-22 13:56:55 +05303610 /* If lcla has to be located in ESRAM we don't need to allocate */
3611 if (base->plat_data->use_esram_lcla) {
3612 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3613 "lcla_esram");
3614 if (!res) {
3615 ret = -ENOENT;
3616 d40_err(&pdev->dev,
3617 "No \"lcla_esram\" memory resource\n");
3618 goto failure;
3619 }
3620 base->lcla_pool.base = ioremap(res->start,
3621 resource_size(res));
3622 if (!base->lcla_pool.base) {
3623 ret = -ENOMEM;
3624 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3625 goto failure;
3626 }
3627 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003628
Narayanan G28c7a192011-11-22 13:56:55 +05303629 } else {
3630 ret = d40_lcla_allocate(base);
3631 if (ret) {
3632 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3633 goto failure;
3634 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003635 }
3636
Linus Walleij8d318a52010-03-30 15:33:42 +02003637 spin_lock_init(&base->lcla_pool.lock);
3638
Linus Walleij8d318a52010-03-30 15:33:42 +02003639 base->irq = platform_get_irq(pdev, 0);
3640
3641 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003642 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003643 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003644 goto failure;
3645 }
3646
Narayanan G7fb3e752011-11-17 17:26:41 +05303647 pm_runtime_irq_safe(base->dev);
3648 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3649 pm_runtime_use_autosuspend(base->dev);
3650 pm_runtime_enable(base->dev);
3651 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303652
3653 if (base->plat_data->use_esram_lcla) {
3654
3655 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3656 if (IS_ERR(base->lcpa_regulator)) {
3657 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3658 base->lcpa_regulator = NULL;
3659 goto failure;
3660 }
3661
3662 ret = regulator_enable(base->lcpa_regulator);
3663 if (ret) {
3664 d40_err(&pdev->dev,
3665 "Failed to enable lcpa_regulator\n");
3666 regulator_put(base->lcpa_regulator);
3667 base->lcpa_regulator = NULL;
3668 goto failure;
3669 }
3670 }
3671
Narayanan G7fb3e752011-11-17 17:26:41 +05303672 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003673 err = d40_dmaengine_init(base, num_reserved_chans);
3674 if (err)
3675 goto failure;
3676
Per Forlinb96710e2011-10-18 18:39:47 +02003677 base->dev->dma_parms = &base->dma_parms;
3678 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3679 if (err) {
3680 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3681 goto failure;
3682 }
3683
Linus Walleij8d318a52010-03-30 15:33:42 +02003684 d40_hw_init(base);
3685
Lee Jonesfa332de2013-05-03 15:32:12 +01003686 if (np) {
3687 err = of_dma_controller_register(np, d40_xlate, NULL);
3688 if (err && err != -ENODEV)
3689 dev_err(&pdev->dev,
3690 "could not register of_dma_controller\n");
3691 }
3692
Linus Walleij8d318a52010-03-30 15:33:42 +02003693 dev_info(base->dev, "initialized\n");
3694 return 0;
3695
3696failure:
3697 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003698 if (base->desc_slab)
3699 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003700 if (base->virtbase)
3701 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003702
Narayanan G28c7a192011-11-22 13:56:55 +05303703 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3704 iounmap(base->lcla_pool.base);
3705 base->lcla_pool.base = NULL;
3706 }
3707
Rabin Vincent026cbc42011-01-25 11:18:14 +01003708 if (base->lcla_pool.dma_addr)
3709 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3710 SZ_1K * base->num_phy_chans,
3711 DMA_TO_DEVICE);
3712
Linus Walleij508849a2010-06-20 21:26:07 +00003713 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3714 free_pages((unsigned long)base->lcla_pool.base,
3715 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003716
3717 kfree(base->lcla_pool.base_unaligned);
3718
Linus Walleij8d318a52010-03-30 15:33:42 +02003719 if (base->phy_lcpa)
3720 release_mem_region(base->phy_lcpa,
3721 base->lcpa_size);
3722 if (base->phy_start)
3723 release_mem_region(base->phy_start,
3724 base->phy_size);
3725 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003726 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003727 clk_put(base->clk);
3728 }
3729
Narayanan G28c7a192011-11-22 13:56:55 +05303730 if (base->lcpa_regulator) {
3731 regulator_disable(base->lcpa_regulator);
3732 regulator_put(base->lcpa_regulator);
3733 }
3734
Linus Walleij8d318a52010-03-30 15:33:42 +02003735 kfree(base->lcla_pool.alloc_map);
3736 kfree(base->lookup_log_chans);
3737 kfree(base->lookup_phy_chans);
3738 kfree(base->phy_res);
3739 kfree(base);
3740 }
3741
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003742 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003743 return ret;
3744}
3745
Lee Jones1814a172013-05-03 15:32:11 +01003746static const struct of_device_id d40_match[] = {
3747 { .compatible = "stericsson,dma40", },
3748 {}
3749};
3750
Linus Walleij8d318a52010-03-30 15:33:42 +02003751static struct platform_driver d40_driver = {
3752 .driver = {
3753 .owner = THIS_MODULE,
3754 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303755 .pm = DMA40_PM_OPS,
Lee Jones1814a172013-05-03 15:32:11 +01003756 .of_match_table = d40_match,
Linus Walleij8d318a52010-03-30 15:33:42 +02003757 },
3758};
3759
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003760static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003761{
3762 return platform_driver_probe(&d40_driver, d40_probe);
3763}
Linus Walleija0eb2212011-05-18 14:18:57 +02003764subsys_initcall(stedma40_init);