blob: 76c255fcdc2d37026de412ae3714a411acdfd3e5 [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 */
Lee Jones8a3b6e12013-05-15 10:51:52 +010057#define D40_ALLOC_FREE BIT(31)
58#define D40_ALLOC_PHY BIT(30)
Linus Walleij8d318a52010-03-30 15:33:42 +020059#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,
Lee Jones2c2b62d2013-05-15 10:51:54 +010081 .dir = DMA_MEM_TO_MEM,
Lee Jones29027a12013-05-03 15:31:54 +010082
Lee Jones43f2e1a2013-05-15 11:51:57 +020083 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Lee Jones29027a12013-05-03 15:31:54 +010084 .src_info.psize = STEDMA40_PSIZE_PHY_1,
85 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
86
Lee Jones43f2e1a2013-05-15 11:51:57 +020087 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Lee Jones29027a12013-05-03 15:31:54 +010088 .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,
Lee Jones2c2b62d2013-05-15 10:51:54 +010095 .dir = DMA_MEM_TO_MEM,
Lee Jones29027a12013-05-03 15:31:54 +010096
Lee Jones43f2e1a2013-05-15 11:51:57 +020097 .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Lee Jones29027a12013-05-03 15:31:54 +010098 .src_info.psize = STEDMA40_PSIZE_LOG_1,
99 .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
100
Lee Jones43f2e1a2013-05-15 11:51:57 +0200101 .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Lee Jones29027a12013-05-03 15:31:54 +0100102 .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 &&
Lee Jones2c2b62d2013-05-15 10:51:54 +0100846 chan->dma_cfg.dir == DMA_DEV_TO_MEM))
Fabio Baltieri74070482012-12-18 12:25:14 +0100847 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
Lee Jones43f2e1a2013-05-15 11:51:57 +02001008 * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
1009 *
1010 * Calculate the total number of dma elements required to send the entire sg list.
Per Forlind49278e2010-12-20 18:31:38 +01001011 */
1012static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1013{
1014 int dmalen;
1015 u32 max_w = max(data_width1, data_width2);
1016 u32 min_w = min(data_width1, data_width2);
Lee Jones43f2e1a2013-05-15 11:51:57 +02001017 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
Per Forlind49278e2010-12-20 18:31:38 +01001018
1019 if (seg_max > STEDMA40_MAX_SEG_SIZE)
Lee Jones43f2e1a2013-05-15 11:51:57 +02001020 seg_max -= max_w;
Per Forlind49278e2010-12-20 18:31:38 +01001021
Lee Jones43f2e1a2013-05-15 11:51:57 +02001022 if (!IS_ALIGNED(size, max_w))
Per Forlind49278e2010-12-20 18:31:38 +01001023 return -EINVAL;
1024
1025 if (size <= seg_max)
1026 dmalen = 1;
1027 else {
1028 dmalen = size / seg_max;
1029 if (dmalen * seg_max < size)
1030 dmalen++;
1031 }
1032 return dmalen;
1033}
1034
1035static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1036 u32 data_width1, u32 data_width2)
1037{
1038 struct scatterlist *sg;
1039 int i;
1040 int len = 0;
1041 int ret;
1042
1043 for_each_sg(sgl, sg, sg_len, i) {
1044 ret = d40_size_2_dmalen(sg_dma_len(sg),
1045 data_width1, data_width2);
1046 if (ret < 0)
1047 return ret;
1048 len += ret;
1049 }
1050 return len;
1051}
1052
Narayanan G7fb3e752011-11-17 17:26:41 +05301053
1054#ifdef CONFIG_PM
1055static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1056 u32 *regaddr, int num, bool save)
1057{
1058 int i;
1059
1060 for (i = 0; i < num; i++) {
1061 void __iomem *addr = baseaddr + regaddr[i];
1062
1063 if (save)
1064 backup[i] = readl_relaxed(addr);
1065 else
1066 writel_relaxed(backup[i], addr);
1067 }
1068}
1069
1070static void d40_save_restore_registers(struct d40_base *base, bool save)
1071{
1072 int i;
1073
1074 /* Save/Restore channel specific registers */
1075 for (i = 0; i < base->num_phy_chans; i++) {
1076 void __iomem *addr;
1077 int idx;
1078
1079 if (base->phy_res[i].reserved)
1080 continue;
1081
1082 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1083 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1084
1085 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1086 d40_backup_regs_chan,
1087 ARRAY_SIZE(d40_backup_regs_chan),
1088 save);
1089 }
1090
1091 /* Save/Restore global registers */
1092 dma40_backup(base->virtbase, base->reg_val_backup,
1093 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1094 save);
1095
1096 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001097 if (base->gen_dmac.backup)
1098 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1099 base->gen_dmac.backup,
1100 base->gen_dmac.backup_size,
1101 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301102}
1103#else
1104static void d40_save_restore_registers(struct d40_base *base, bool save)
1105{
1106}
1107#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001108
Narayanan G1bdae6f2012-02-09 12:41:37 +05301109static int __d40_execute_command_phy(struct d40_chan *d40c,
1110 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001111{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001112 u32 status;
1113 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001114 void __iomem *active_reg;
1115 int ret = 0;
1116 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001117 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001118
Narayanan G1bdae6f2012-02-09 12:41:37 +05301119 if (command == D40_DMA_STOP) {
1120 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1121 if (ret)
1122 return ret;
1123 }
1124
Linus Walleij8d318a52010-03-30 15:33:42 +02001125 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1126
1127 if (d40c->phy_chan->num % 2 == 0)
1128 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1129 else
1130 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1131
1132 if (command == D40_DMA_SUSPEND_REQ) {
1133 status = (readl(active_reg) &
1134 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1135 D40_CHAN_POS(d40c->phy_chan->num);
1136
1137 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1138 goto done;
1139 }
1140
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001141 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1142 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1143 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001144
1145 if (command == D40_DMA_SUSPEND_REQ) {
1146
1147 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1148 status = (readl(active_reg) &
1149 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1150 D40_CHAN_POS(d40c->phy_chan->num);
1151
1152 cpu_relax();
1153 /*
1154 * Reduce the number of bus accesses while
1155 * waiting for the DMA to suspend.
1156 */
1157 udelay(3);
1158
1159 if (status == D40_DMA_STOP ||
1160 status == D40_DMA_SUSPENDED)
1161 break;
1162 }
1163
1164 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001165 chan_err(d40c,
1166 "unable to suspend the chl %d (log: %d) status %x\n",
1167 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001168 status);
1169 dump_stack();
1170 ret = -EBUSY;
1171 }
1172
1173 }
1174done:
1175 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1176 return ret;
1177}
1178
1179static void d40_term_all(struct d40_chan *d40c)
1180{
1181 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001182 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001183
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001184 /* Release completed descriptors */
1185 while ((d40d = d40_first_done(d40c))) {
1186 d40_desc_remove(d40d);
1187 d40_desc_free(d40c, d40d);
1188 }
1189
Linus Walleij8d318a52010-03-30 15:33:42 +02001190 /* Release active descriptors */
1191 while ((d40d = d40_first_active_get(d40c))) {
1192 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001193 d40_desc_free(d40c, d40d);
1194 }
1195
1196 /* Release queued descriptors waiting for transfer */
1197 while ((d40d = d40_first_queued(d40c))) {
1198 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001199 d40_desc_free(d40c, d40d);
1200 }
1201
Per Forlina8f30672011-06-26 23:29:52 +02001202 /* Release pending descriptors */
1203 while ((d40d = d40_first_pending(d40c))) {
1204 d40_desc_remove(d40d);
1205 d40_desc_free(d40c, d40d);
1206 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001207
Per Forlin74043682011-08-29 13:33:34 +02001208 /* Release client owned descriptors */
1209 if (!list_empty(&d40c->client))
1210 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1211 d40_desc_remove(d40d);
1212 d40_desc_free(d40c, d40d);
1213 }
1214
Per Forlin82babbb362011-08-29 13:33:35 +02001215 /* Release descriptors in prepare queue */
1216 if (!list_empty(&d40c->prepare_queue))
1217 list_for_each_entry_safe(d40d, _d,
1218 &d40c->prepare_queue, node) {
1219 d40_desc_remove(d40d);
1220 d40_desc_free(d40c, d40d);
1221 }
Per Forlin74043682011-08-29 13:33:34 +02001222
Linus Walleij8d318a52010-03-30 15:33:42 +02001223 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001224}
1225
Narayanan G1bdae6f2012-02-09 12:41:37 +05301226static void __d40_config_set_event(struct d40_chan *d40c,
1227 enum d40_events event_type, u32 event,
1228 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001229{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001230 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001231 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301232 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001233
Narayanan G1bdae6f2012-02-09 12:41:37 +05301234 switch (event_type) {
1235
1236 case D40_DEACTIVATE_EVENTLINE:
1237
Rabin Vincent262d2912011-01-25 11:18:05 +01001238 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1239 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301240 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001241
Narayanan G1bdae6f2012-02-09 12:41:37 +05301242 case D40_SUSPEND_REQ_EVENTLINE:
1243 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1244 D40_EVENTLINE_POS(event);
1245
1246 if (status == D40_DEACTIVATE_EVENTLINE ||
1247 status == D40_SUSPEND_REQ_EVENTLINE)
1248 break;
1249
1250 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1251 | ~D40_EVENTLINE_MASK(event), addr);
1252
1253 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1254
1255 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1256 D40_EVENTLINE_POS(event);
1257
1258 cpu_relax();
1259 /*
1260 * Reduce the number of bus accesses while
1261 * waiting for the DMA to suspend.
1262 */
1263 udelay(3);
1264
1265 if (status == D40_DEACTIVATE_EVENTLINE)
1266 break;
1267 }
1268
1269 if (tries == D40_SUSPEND_MAX_IT) {
1270 chan_err(d40c,
1271 "unable to stop the event_line chl %d (log: %d)"
1272 "status %x\n", d40c->phy_chan->num,
1273 d40c->log_num, status);
1274 }
1275 break;
1276
1277 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001278 /*
1279 * The hardware sometimes doesn't register the enable when src and dst
1280 * event lines are active on the same logical channel. Retry to ensure
1281 * it does. Usually only one retry is sufficient.
1282 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301283 tries = 100;
1284 while (--tries) {
1285 writel((D40_ACTIVATE_EVENTLINE <<
1286 D40_EVENTLINE_POS(event)) |
1287 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001288
Narayanan G1bdae6f2012-02-09 12:41:37 +05301289 if (readl(addr) & D40_EVENTLINE_MASK(event))
1290 break;
1291 }
1292
1293 if (tries != 99)
1294 dev_dbg(chan2dev(d40c),
1295 "[%s] workaround enable S%cLNK (%d tries)\n",
1296 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1297 100 - tries);
1298
1299 WARN_ON(!tries);
1300 break;
1301
1302 case D40_ROUND_EVENTLINE:
1303 BUG();
1304 break;
1305
Rabin Vincent262d2912011-01-25 11:18:05 +01001306 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001307}
1308
Narayanan G1bdae6f2012-02-09 12:41:37 +05301309static void d40_config_set_event(struct d40_chan *d40c,
1310 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001311{
Lee Jones26955c07d2013-05-03 15:31:56 +01001312 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
1313
Linus Walleij8d318a52010-03-30 15:33:42 +02001314 /* Enable event line connected to device (or memcpy) */
Lee Jones2c2b62d2013-05-15 10:51:54 +01001315 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
1316 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
Narayanan G1bdae6f2012-02-09 12:41:37 +05301317 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001318 D40_CHAN_REG_SSLNK);
Rabin Vincent262d2912011-01-25 11:18:05 +01001319
Lee Jones2c2b62d2013-05-15 10:51:54 +01001320 if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
Narayanan G1bdae6f2012-02-09 12:41:37 +05301321 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001322 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001323}
1324
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001325static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001326{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001327 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001328 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001329
Rabin Vincent8ca84682011-01-25 11:18:07 +01001330 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1331 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001332
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001333 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001334}
1335
Narayanan G1bdae6f2012-02-09 12:41:37 +05301336static int
1337__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1338{
1339 unsigned long flags;
1340 int ret = 0;
1341 u32 active_status;
1342 void __iomem *active_reg;
1343
1344 if (d40c->phy_chan->num % 2 == 0)
1345 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1346 else
1347 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1348
1349
1350 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1351
1352 switch (command) {
1353 case D40_DMA_STOP:
1354 case D40_DMA_SUSPEND_REQ:
1355
1356 active_status = (readl(active_reg) &
1357 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1358 D40_CHAN_POS(d40c->phy_chan->num);
1359
1360 if (active_status == D40_DMA_RUN)
1361 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1362 else
1363 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1364
1365 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1366 ret = __d40_execute_command_phy(d40c, command);
1367
1368 break;
1369
1370 case D40_DMA_RUN:
1371
1372 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1373 ret = __d40_execute_command_phy(d40c, command);
1374 break;
1375
1376 case D40_DMA_SUSPENDED:
1377 BUG();
1378 break;
1379 }
1380
1381 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1382 return ret;
1383}
1384
1385static int d40_channel_execute_command(struct d40_chan *d40c,
1386 enum d40_command command)
1387{
1388 if (chan_is_logical(d40c))
1389 return __d40_execute_command_log(d40c, command);
1390 else
1391 return __d40_execute_command_phy(d40c, command);
1392}
1393
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001394static u32 d40_get_prmo(struct d40_chan *d40c)
1395{
1396 static const unsigned int phy_map[] = {
1397 [STEDMA40_PCHAN_BASIC_MODE]
1398 = D40_DREG_PRMO_PCHAN_BASIC,
1399 [STEDMA40_PCHAN_MODULO_MODE]
1400 = D40_DREG_PRMO_PCHAN_MODULO,
1401 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1402 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1403 };
1404 static const unsigned int log_map[] = {
1405 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1406 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1407 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1408 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1409 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1410 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1411 };
1412
Rabin Vincent724a8572011-01-25 11:18:08 +01001413 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001414 return phy_map[d40c->dma_cfg.mode_opt];
1415 else
1416 return log_map[d40c->dma_cfg.mode_opt];
1417}
1418
Jonas Aabergb55912c2010-08-09 12:08:02 +00001419static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001420{
1421 u32 addr_base;
1422 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001423
1424 /* Odd addresses are even addresses + 4 */
1425 addr_base = (d40c->phy_chan->num % 2) * 4;
1426 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001427 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001428 D40_CHAN_POS(d40c->phy_chan->num);
1429 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1430
1431 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001432 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001433
1434 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1435
Rabin Vincent724a8572011-01-25 11:18:08 +01001436 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001437 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1438 & D40_SREG_ELEM_LOG_LIDX_MASK;
1439 void __iomem *chanbase = chan_base(d40c);
1440
Linus Walleij8d318a52010-03-30 15:33:42 +02001441 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001442 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1443 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001444
Jonas Aabergb55912c2010-08-09 12:08:02 +00001445 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001446 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1447 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301448
1449 /* Clear LNK which will be used by d40_chan_has_events() */
1450 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1451 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001452 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001453}
1454
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001455static u32 d40_residue(struct d40_chan *d40c)
1456{
1457 u32 num_elt;
1458
Rabin Vincent724a8572011-01-25 11:18:08 +01001459 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001460 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1461 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001462 else {
1463 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1464 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1465 >> D40_SREG_ELEM_PHY_ECNT_POS;
1466 }
1467
Lee Jones43f2e1a2013-05-15 11:51:57 +02001468 return num_elt * d40c->dma_cfg.dst_info.data_width;
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001469}
1470
1471static bool d40_tx_is_linked(struct d40_chan *d40c)
1472{
1473 bool is_link;
1474
Rabin Vincent724a8572011-01-25 11:18:08 +01001475 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001476 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1477 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001478 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1479 & D40_SREG_LNK_PHYS_LNK_MASK;
1480
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001481 return is_link;
1482}
1483
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001484static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001485{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001486 int res = 0;
1487 unsigned long flags;
1488
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001489 if (!d40c->busy)
1490 return 0;
1491
Narayanan G7fb3e752011-11-17 17:26:41 +05301492 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001493 spin_lock_irqsave(&d40c->lock, flags);
1494
1495 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301496
Narayanan G7fb3e752011-11-17 17:26:41 +05301497 pm_runtime_mark_last_busy(d40c->base->dev);
1498 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001499 spin_unlock_irqrestore(&d40c->lock, flags);
1500 return res;
1501}
1502
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001503static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001504{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001505 int res = 0;
1506 unsigned long flags;
1507
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001508 if (!d40c->busy)
1509 return 0;
1510
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001511 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301512 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001513
1514 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301515 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001516 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001517
Narayanan G7fb3e752011-11-17 17:26:41 +05301518 pm_runtime_mark_last_busy(d40c->base->dev);
1519 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001520 spin_unlock_irqrestore(&d40c->lock, flags);
1521 return res;
1522}
1523
Linus Walleij8d318a52010-03-30 15:33:42 +02001524static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1525{
1526 struct d40_chan *d40c = container_of(tx->chan,
1527 struct d40_chan,
1528 chan);
1529 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1530 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001531 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001532
1533 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001534 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001535 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001536 spin_unlock_irqrestore(&d40c->lock, flags);
1537
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001538 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001539}
1540
1541static int d40_start(struct d40_chan *d40c)
1542{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001543 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001544}
1545
1546static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1547{
1548 struct d40_desc *d40d;
1549 int err;
1550
1551 /* Start queued jobs, if any */
1552 d40d = d40_first_queued(d40c);
1553
1554 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301555 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301556 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301557 pm_runtime_get_sync(d40c->base->dev);
1558 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001559
1560 /* Remove from queue */
1561 d40_desc_remove(d40d);
1562
1563 /* Add to active queue */
1564 d40_desc_submit(d40c, d40d);
1565
Rabin Vincent7d83a852011-01-25 11:18:06 +01001566 /* Initiate DMA job */
1567 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001568
Rabin Vincent7d83a852011-01-25 11:18:06 +01001569 /* Start dma job */
1570 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001571
Rabin Vincent7d83a852011-01-25 11:18:06 +01001572 if (err)
1573 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001574 }
1575
1576 return d40d;
1577}
1578
1579/* called from interrupt context */
1580static void dma_tc_handle(struct d40_chan *d40c)
1581{
1582 struct d40_desc *d40d;
1583
Linus Walleij8d318a52010-03-30 15:33:42 +02001584 /* Get first active entry from list */
1585 d40d = d40_first_active_get(d40c);
1586
1587 if (d40d == NULL)
1588 return;
1589
Rabin Vincent0c842b52011-01-25 11:18:35 +01001590 if (d40d->cyclic) {
1591 /*
1592 * If this was a paritially loaded list, we need to reloaded
1593 * it, and only when the list is completed. We need to check
1594 * for done because the interrupt will hit for every link, and
1595 * not just the last one.
1596 */
1597 if (d40d->lli_current < d40d->lli_len
1598 && !d40_tx_is_linked(d40c)
1599 && !d40_residue(d40c)) {
1600 d40_lcla_free_all(d40c, d40d);
1601 d40_desc_load(d40c, d40d);
1602 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001603
Rabin Vincent0c842b52011-01-25 11:18:35 +01001604 if (d40d->lli_current == d40d->lli_len)
1605 d40d->lli_current = 0;
1606 }
1607 } else {
1608 d40_lcla_free_all(d40c, d40d);
1609
1610 if (d40d->lli_current < d40d->lli_len) {
1611 d40_desc_load(d40c, d40d);
1612 /* Start dma job */
1613 (void) d40_start(d40c);
1614 return;
1615 }
1616
1617 if (d40_queue_start(d40c) == NULL)
1618 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301619 pm_runtime_mark_last_busy(d40c->base->dev);
1620 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001621
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001622 d40_desc_remove(d40d);
1623 d40_desc_done(d40c, d40d);
1624 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001625
Linus Walleij8d318a52010-03-30 15:33:42 +02001626 d40c->pending_tx++;
1627 tasklet_schedule(&d40c->tasklet);
1628
1629}
1630
1631static void dma_tasklet(unsigned long data)
1632{
1633 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001634 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001635 unsigned long flags;
1636 dma_async_tx_callback callback;
1637 void *callback_param;
1638
1639 spin_lock_irqsave(&d40c->lock, flags);
1640
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001641 /* Get first entry from the done list */
1642 d40d = d40_first_done(d40c);
1643 if (d40d == NULL) {
1644 /* Check if we have reached here for cyclic job */
1645 d40d = d40_first_active_get(d40c);
1646 if (d40d == NULL || !d40d->cyclic)
1647 goto err;
1648 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001649
Rabin Vincent0c842b52011-01-25 11:18:35 +01001650 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001651 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001652
1653 /*
1654 * If terminating a channel pending_tx is set to zero.
1655 * This prevents any finished active jobs to return to the client.
1656 */
1657 if (d40c->pending_tx == 0) {
1658 spin_unlock_irqrestore(&d40c->lock, flags);
1659 return;
1660 }
1661
1662 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001663 callback = d40d->txd.callback;
1664 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001665
Rabin Vincent0c842b52011-01-25 11:18:35 +01001666 if (!d40d->cyclic) {
1667 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001668 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001669 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001670 } else if (!d40d->is_in_client_list) {
1671 d40_desc_remove(d40d);
1672 d40_lcla_free_all(d40c, d40d);
1673 list_add_tail(&d40d->node, &d40c->client);
1674 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001675 }
1676 }
1677
1678 d40c->pending_tx--;
1679
1680 if (d40c->pending_tx)
1681 tasklet_schedule(&d40c->tasklet);
1682
1683 spin_unlock_irqrestore(&d40c->lock, flags);
1684
Jonas Aaberg767a9672010-08-09 12:08:34 +00001685 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001686 callback(callback_param);
1687
1688 return;
1689
Narayanan G1bdae6f2012-02-09 12:41:37 +05301690err:
1691 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001692 if (d40c->pending_tx > 0)
1693 d40c->pending_tx--;
1694 spin_unlock_irqrestore(&d40c->lock, flags);
1695}
1696
1697static irqreturn_t d40_handle_interrupt(int irq, void *data)
1698{
Linus Walleij8d318a52010-03-30 15:33:42 +02001699 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001700 u32 idx;
1701 u32 row;
1702 long chan = -1;
1703 struct d40_chan *d40c;
1704 unsigned long flags;
1705 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001706 u32 regs[base->gen_dmac.il_size];
1707 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1708 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001709
1710 spin_lock_irqsave(&base->interrupt_lock, flags);
1711
1712 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001713 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001714 regs[i] = readl(base->virtbase + il[i].src);
1715
1716 for (;;) {
1717
1718 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001719 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001720
1721 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001722 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001723 break;
1724
1725 row = chan / BITS_PER_LONG;
1726 idx = chan & (BITS_PER_LONG - 1);
1727
Linus Walleij8d318a52010-03-30 15:33:42 +02001728 if (il[row].offset == D40_PHY_CHAN)
1729 d40c = base->lookup_phy_chans[idx];
1730 else
1731 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001732
1733 if (!d40c) {
1734 /*
1735 * No error because this can happen if something else
1736 * in the system is using the channel.
1737 */
1738 continue;
1739 }
1740
1741 /* ACK interrupt */
Lee Jones8a3b6e12013-05-15 10:51:52 +01001742 writel(BIT(idx), base->virtbase + il[row].clr);
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001743
Linus Walleij8d318a52010-03-30 15:33:42 +02001744 spin_lock(&d40c->lock);
1745
1746 if (!il[row].is_error)
1747 dma_tc_handle(d40c);
1748 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001749 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1750 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001751
1752 spin_unlock(&d40c->lock);
1753 }
1754
1755 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1756
1757 return IRQ_HANDLED;
1758}
1759
Linus Walleij8d318a52010-03-30 15:33:42 +02001760static int d40_validate_conf(struct d40_chan *d40c,
1761 struct stedma40_chan_cfg *conf)
1762{
1763 int res = 0;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001764 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001765
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001766 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001767 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001768 res = -EINVAL;
1769 }
1770
Lee Jones26955c07d2013-05-03 15:31:56 +01001771 if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
1772 (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
1773 (conf->dev_type < 0)) {
1774 chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001775 res = -EINVAL;
1776 }
1777
Lee Jones2c2b62d2013-05-15 10:51:54 +01001778 if (conf->dir == DMA_DEV_TO_DEV) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001779 /*
1780 * DMAC HW supports it. Will be added to this driver,
1781 * in case any dma client requires it.
1782 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001783 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001784 res = -EINVAL;
1785 }
1786
Per Forlind49278e2010-12-20 18:31:38 +01001787 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
Lee Jones43f2e1a2013-05-15 11:51:57 +02001788 conf->src_info.data_width !=
Per Forlind49278e2010-12-20 18:31:38 +01001789 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
Lee Jones43f2e1a2013-05-15 11:51:57 +02001790 conf->dst_info.data_width) {
Per Forlind49278e2010-12-20 18:31:38 +01001791 /*
1792 * The DMAC hardware only supports
1793 * src (burst x width) == dst (burst x width)
1794 */
1795
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001796 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001797 res = -EINVAL;
1798 }
1799
Linus Walleij8d318a52010-03-30 15:33:42 +02001800 return res;
1801}
1802
Narayanan G5cd326f2011-11-30 19:20:42 +05301803static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1804 bool is_src, int log_event_line, bool is_log,
1805 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001806{
1807 unsigned long flags;
1808 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301809
1810 *first_user = ((phy->allocated_src | phy->allocated_dst)
1811 == D40_ALLOC_FREE);
1812
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001813 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001814 /* Physical interrupts are masked per physical full channel */
1815 if (phy->allocated_src == D40_ALLOC_FREE &&
1816 phy->allocated_dst == D40_ALLOC_FREE) {
1817 phy->allocated_dst = D40_ALLOC_PHY;
1818 phy->allocated_src = D40_ALLOC_PHY;
1819 goto found;
1820 } else
1821 goto not_found;
1822 }
1823
1824 /* Logical channel */
1825 if (is_src) {
1826 if (phy->allocated_src == D40_ALLOC_PHY)
1827 goto not_found;
1828
1829 if (phy->allocated_src == D40_ALLOC_FREE)
1830 phy->allocated_src = D40_ALLOC_LOG_FREE;
1831
Lee Jones8a3b6e12013-05-15 10:51:52 +01001832 if (!(phy->allocated_src & BIT(log_event_line))) {
1833 phy->allocated_src |= BIT(log_event_line);
Linus Walleij8d318a52010-03-30 15:33:42 +02001834 goto found;
1835 } else
1836 goto not_found;
1837 } else {
1838 if (phy->allocated_dst == D40_ALLOC_PHY)
1839 goto not_found;
1840
1841 if (phy->allocated_dst == D40_ALLOC_FREE)
1842 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1843
Lee Jones8a3b6e12013-05-15 10:51:52 +01001844 if (!(phy->allocated_dst & BIT(log_event_line))) {
1845 phy->allocated_dst |= BIT(log_event_line);
Linus Walleij8d318a52010-03-30 15:33:42 +02001846 goto found;
1847 } else
1848 goto not_found;
1849 }
1850
1851not_found:
1852 spin_unlock_irqrestore(&phy->lock, flags);
1853 return false;
1854found:
1855 spin_unlock_irqrestore(&phy->lock, flags);
1856 return true;
1857}
1858
1859static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1860 int log_event_line)
1861{
1862 unsigned long flags;
1863 bool is_free = false;
1864
1865 spin_lock_irqsave(&phy->lock, flags);
1866 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001867 phy->allocated_dst = D40_ALLOC_FREE;
1868 phy->allocated_src = D40_ALLOC_FREE;
1869 is_free = true;
1870 goto out;
1871 }
1872
1873 /* Logical channel */
1874 if (is_src) {
Lee Jones8a3b6e12013-05-15 10:51:52 +01001875 phy->allocated_src &= ~BIT(log_event_line);
Linus Walleij8d318a52010-03-30 15:33:42 +02001876 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1877 phy->allocated_src = D40_ALLOC_FREE;
1878 } else {
Lee Jones8a3b6e12013-05-15 10:51:52 +01001879 phy->allocated_dst &= ~BIT(log_event_line);
Linus Walleij8d318a52010-03-30 15:33:42 +02001880 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1881 phy->allocated_dst = D40_ALLOC_FREE;
1882 }
1883
1884 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1885 D40_ALLOC_FREE);
1886
1887out:
1888 spin_unlock_irqrestore(&phy->lock, flags);
1889
1890 return is_free;
1891}
1892
Narayanan G5cd326f2011-11-30 19:20:42 +05301893static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001894{
Lee Jones26955c07d2013-05-03 15:31:56 +01001895 int dev_type = d40c->dma_cfg.dev_type;
Linus Walleij8d318a52010-03-30 15:33:42 +02001896 int event_group;
1897 int event_line;
1898 struct d40_phy_res *phys;
1899 int i;
1900 int j;
1901 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001902 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001903 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001904 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001905
1906 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001907 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001908
Lee Jones2c2b62d2013-05-15 10:51:54 +01001909 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001910 log_num = 2 * dev_type;
1911 is_src = true;
Lee Jones2c2b62d2013-05-15 10:51:54 +01001912 } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
1913 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001914 /* dst event lines are used for logical memcpy */
Linus Walleij8d318a52010-03-30 15:33:42 +02001915 log_num = 2 * dev_type + 1;
1916 is_src = false;
1917 } else
1918 return -EINVAL;
1919
1920 event_group = D40_TYPE_TO_GROUP(dev_type);
1921 event_line = D40_TYPE_TO_EVENT(dev_type);
1922
1923 if (!is_log) {
Lee Jones2c2b62d2013-05-15 10:51:54 +01001924 if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001925 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001926 if (d40c->dma_cfg.use_fixed_channel) {
1927 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001928 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301929 0, is_log,
1930 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001931 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001932 } else {
1933 for (i = 0; i < num_phy_chans; i++) {
1934 if (d40_alloc_mask_set(&phys[i], is_src,
1935 0, is_log,
1936 first_phy_user))
1937 goto found_phy;
1938 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001939 }
1940 } else
1941 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1942 int phy_num = j + event_group * 2;
1943 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001944 if (d40_alloc_mask_set(&phys[i],
1945 is_src,
1946 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301947 is_log,
1948 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001949 goto found_phy;
1950 }
1951 }
1952 return -EINVAL;
1953found_phy:
1954 d40c->phy_chan = &phys[i];
1955 d40c->log_num = D40_PHY_CHAN;
1956 goto out;
1957 }
1958 if (dev_type == -1)
1959 return -EINVAL;
1960
1961 /* Find logical channel */
1962 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1963 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301964
1965 if (d40c->dma_cfg.use_fixed_channel) {
1966 i = d40c->dma_cfg.phy_channel;
1967
1968 if ((i != phy_num) && (i != phy_num + 1)) {
1969 dev_err(chan2dev(d40c),
1970 "invalid fixed phy channel %d\n", i);
1971 return -EINVAL;
1972 }
1973
1974 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1975 is_log, first_phy_user))
1976 goto found_log;
1977
1978 dev_err(chan2dev(d40c),
1979 "could not allocate fixed phy channel %d\n", i);
1980 return -EINVAL;
1981 }
1982
Linus Walleij8d318a52010-03-30 15:33:42 +02001983 /*
1984 * Spread logical channels across all available physical rather
1985 * than pack every logical channel at the first available phy
1986 * channels.
1987 */
1988 if (is_src) {
1989 for (i = phy_num; i < phy_num + 2; i++) {
1990 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301991 event_line, is_log,
1992 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001993 goto found_log;
1994 }
1995 } else {
1996 for (i = phy_num + 1; i >= phy_num; i--) {
1997 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301998 event_line, is_log,
1999 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002000 goto found_log;
2001 }
2002 }
2003 }
2004 return -EINVAL;
2005
2006found_log:
2007 d40c->phy_chan = &phys[i];
2008 d40c->log_num = log_num;
2009out:
2010
2011 if (is_log)
2012 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2013 else
2014 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2015
2016 return 0;
2017
2018}
2019
Linus Walleij8d318a52010-03-30 15:33:42 +02002020static int d40_config_memcpy(struct d40_chan *d40c)
2021{
2022 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2023
2024 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002025 d40c->dma_cfg = dma40_memcpy_conf_log;
Lee Jones26955c07d2013-05-03 15:31:56 +01002026 d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002027
Lee Jones9b233f92013-05-15 10:51:26 +01002028 d40_log_cfg(&d40c->dma_cfg,
2029 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2030
Linus Walleij8d318a52010-03-30 15:33:42 +02002031 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2032 dma_has_cap(DMA_SLAVE, cap)) {
Lee Jones29027a12013-05-03 15:31:54 +01002033 d40c->dma_cfg = dma40_memcpy_conf_phy;
Lee Jones57e65ad2013-05-15 10:51:25 +01002034
2035 /* Generate interrrupt at end of transfer or relink. */
2036 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
2037
2038 /* Generate interrupt on error. */
2039 d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2040 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
2041
Linus Walleij8d318a52010-03-30 15:33:42 +02002042 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002043 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002044 return -EINVAL;
2045 }
2046
2047 return 0;
2048}
2049
Linus Walleij8d318a52010-03-30 15:33:42 +02002050static int d40_free_dma(struct d40_chan *d40c)
2051{
2052
2053 int res = 0;
Lee Jones26955c07d2013-05-03 15:31:56 +01002054 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002055 struct d40_phy_res *phy = d40c->phy_chan;
2056 bool is_src;
2057
2058 /* Terminate all queued and active transfers */
2059 d40_term_all(d40c);
2060
2061 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002062 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002063 return -EINVAL;
2064 }
2065
2066 if (phy->allocated_src == D40_ALLOC_FREE &&
2067 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002068 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002069 return -EINVAL;
2070 }
2071
Lee Jones2c2b62d2013-05-15 10:51:54 +01002072 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2073 d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002074 is_src = false;
Lee Jones2c2b62d2013-05-15 10:51:54 +01002075 else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
Linus Walleij8d318a52010-03-30 15:33:42 +02002076 is_src = true;
Lee Jones26955c07d2013-05-03 15:31:56 +01002077 else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002078 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002079 return -EINVAL;
2080 }
2081
Narayanan G7fb3e752011-11-17 17:26:41 +05302082 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002083 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2084 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302085 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302086 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002087 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302088
Narayanan G1bdae6f2012-02-09 12:41:37 +05302089 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2090
2091 if (chan_is_logical(d40c))
2092 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2093 else
2094 d40c->base->lookup_phy_chans[phy->num] = NULL;
2095
Narayanan G7fb3e752011-11-17 17:26:41 +05302096 if (d40c->busy) {
2097 pm_runtime_mark_last_busy(d40c->base->dev);
2098 pm_runtime_put_autosuspend(d40c->base->dev);
2099 }
2100
2101 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002102 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002103 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302104out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002105
Narayanan G7fb3e752011-11-17 17:26:41 +05302106 pm_runtime_mark_last_busy(d40c->base->dev);
2107 pm_runtime_put_autosuspend(d40c->base->dev);
2108 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002109}
2110
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002111static bool d40_is_paused(struct d40_chan *d40c)
2112{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002113 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002114 bool is_paused = false;
2115 unsigned long flags;
2116 void __iomem *active_reg;
2117 u32 status;
Lee Jones26955c07d2013-05-03 15:31:56 +01002118 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002119
2120 spin_lock_irqsave(&d40c->lock, flags);
2121
Rabin Vincent724a8572011-01-25 11:18:08 +01002122 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002123 if (d40c->phy_chan->num % 2 == 0)
2124 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2125 else
2126 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2127
2128 status = (readl(active_reg) &
2129 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2130 D40_CHAN_POS(d40c->phy_chan->num);
2131 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2132 is_paused = true;
2133
2134 goto _exit;
2135 }
2136
Lee Jones2c2b62d2013-05-15 10:51:54 +01002137 if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
2138 d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002139 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Lee Jones2c2b62d2013-05-15 10:51:54 +01002140 } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01002141 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002142 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002143 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002144 goto _exit;
2145 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002146
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002147 status = (status & D40_EVENTLINE_MASK(event)) >>
2148 D40_EVENTLINE_POS(event);
2149
2150 if (status != D40_DMA_RUN)
2151 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002152_exit:
2153 spin_unlock_irqrestore(&d40c->lock, flags);
2154 return is_paused;
2155
2156}
2157
Linus Walleij8d318a52010-03-30 15:33:42 +02002158static u32 stedma40_residue(struct dma_chan *chan)
2159{
2160 struct d40_chan *d40c =
2161 container_of(chan, struct d40_chan, chan);
2162 u32 bytes_left;
2163 unsigned long flags;
2164
2165 spin_lock_irqsave(&d40c->lock, flags);
2166 bytes_left = d40_residue(d40c);
2167 spin_unlock_irqrestore(&d40c->lock, flags);
2168
2169 return bytes_left;
2170}
2171
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002172static int
2173d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2174 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002175 unsigned int sg_len, dma_addr_t src_dev_addr,
2176 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002177{
2178 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2179 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2180 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002181 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002182
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002183 ret = d40_log_sg_to_lli(sg_src, sg_len,
2184 src_dev_addr,
2185 desc->lli_log.src,
2186 chan->log_def.lcsp1,
2187 src_info->data_width,
2188 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002189
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002190 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2191 dst_dev_addr,
2192 desc->lli_log.dst,
2193 chan->log_def.lcsp3,
2194 dst_info->data_width,
2195 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002196
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002197 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002198}
2199
2200static int
2201d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2202 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002203 unsigned int sg_len, dma_addr_t src_dev_addr,
2204 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002205{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002206 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2207 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2208 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002209 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002210 int ret;
2211
Rabin Vincent0c842b52011-01-25 11:18:35 +01002212 if (desc->cyclic)
2213 flags |= LLI_CYCLIC | LLI_TERM_INT;
2214
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002215 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2216 desc->lli_phy.src,
2217 virt_to_phys(desc->lli_phy.src),
2218 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002219 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002220
2221 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2222 desc->lli_phy.dst,
2223 virt_to_phys(desc->lli_phy.dst),
2224 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002225 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002226
2227 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2228 desc->lli_pool.size, DMA_TO_DEVICE);
2229
2230 return ret < 0 ? ret : 0;
2231}
2232
Rabin Vincent5f811582011-01-25 11:18:18 +01002233static struct d40_desc *
2234d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2235 unsigned int sg_len, unsigned long dma_flags)
2236{
2237 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2238 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002239 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002240
2241 desc = d40_desc_get(chan);
2242 if (!desc)
2243 return NULL;
2244
2245 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2246 cfg->dst_info.data_width);
2247 if (desc->lli_len < 0) {
2248 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002249 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002250 }
2251
Rabin Vincentdbd88782011-01-25 11:18:19 +01002252 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2253 if (ret < 0) {
2254 chan_err(chan, "Could not allocate lli\n");
2255 goto err;
2256 }
2257
Rabin Vincent5f811582011-01-25 11:18:18 +01002258 desc->lli_current = 0;
2259 desc->txd.flags = dma_flags;
2260 desc->txd.tx_submit = d40_tx_submit;
2261
2262 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2263
2264 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002265
2266err:
2267 d40_desc_free(chan, desc);
2268 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002269}
2270
Rabin Vincentcade1d32011-01-25 11:18:23 +01002271static struct dma_async_tx_descriptor *
2272d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2273 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302274 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002275{
2276 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002277 dma_addr_t src_dev_addr = 0;
2278 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002279 struct d40_desc *desc;
2280 unsigned long flags;
2281 int ret;
2282
2283 if (!chan->phy_chan) {
2284 chan_err(chan, "Cannot prepare unallocated channel\n");
2285 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002286 }
2287
Rabin Vincentcade1d32011-01-25 11:18:23 +01002288 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002289
Rabin Vincentcade1d32011-01-25 11:18:23 +01002290 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2291 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002292 goto err;
2293
Rabin Vincent0c842b52011-01-25 11:18:35 +01002294 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2295 desc->cyclic = true;
2296
Lee Jonesef9c89b32013-05-15 10:51:30 +01002297 if (direction == DMA_DEV_TO_MEM)
2298 src_dev_addr = chan->runtime_addr;
2299 else if (direction == DMA_MEM_TO_DEV)
2300 dst_dev_addr = chan->runtime_addr;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002301
2302 if (chan_is_logical(chan))
2303 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002304 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002305 else
2306 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002307 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002308
2309 if (ret) {
2310 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2311 chan_is_logical(chan) ? "log" : "phy", ret);
2312 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002313 }
2314
Per Forlin82babbb362011-08-29 13:33:35 +02002315 /*
2316 * add descriptor to the prepare queue in order to be able
2317 * to free them later in terminate_all
2318 */
2319 list_add_tail(&desc->node, &chan->prepare_queue);
2320
Rabin Vincentcade1d32011-01-25 11:18:23 +01002321 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002322
Rabin Vincentcade1d32011-01-25 11:18:23 +01002323 return &desc->txd;
2324
Linus Walleij8d318a52010-03-30 15:33:42 +02002325err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002326 if (desc)
2327 d40_desc_free(chan, desc);
2328 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002329 return NULL;
2330}
Linus Walleij8d318a52010-03-30 15:33:42 +02002331
2332bool stedma40_filter(struct dma_chan *chan, void *data)
2333{
2334 struct stedma40_chan_cfg *info = data;
2335 struct d40_chan *d40c =
2336 container_of(chan, struct d40_chan, chan);
2337 int err;
2338
2339 if (data) {
2340 err = d40_validate_conf(d40c, info);
2341 if (!err)
2342 d40c->dma_cfg = *info;
2343 } else
2344 err = d40_config_memcpy(d40c);
2345
Rabin Vincentce2ca122010-10-12 13:00:49 +00002346 if (!err)
2347 d40c->configured = true;
2348
Linus Walleij8d318a52010-03-30 15:33:42 +02002349 return err == 0;
2350}
2351EXPORT_SYMBOL(stedma40_filter);
2352
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002353static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2354{
2355 bool realtime = d40c->dma_cfg.realtime;
2356 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002357 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002358 u32 event = D40_TYPE_TO_EVENT(dev_type);
2359 u32 group = D40_TYPE_TO_GROUP(dev_type);
Lee Jones8a3b6e12013-05-15 10:51:52 +01002360 u32 bit = BIT(event);
Rabin Vincentccc3d692012-05-17 13:47:38 +05302361 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002362 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302363
Tong Liu3cb645d2012-09-26 10:07:30 +00002364 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302365 /*
2366 * Due to a hardware bug, in some cases a logical channel triggered by
2367 * a high priority destination event line can generate extra packet
2368 * transactions.
2369 *
2370 * The workaround is to not set the high priority level for the
2371 * destination event lines that trigger logical channels.
2372 */
2373 if (!src && chan_is_logical(d40c))
2374 highprio = false;
2375
Tong Liu3cb645d2012-09-26 10:07:30 +00002376 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002377
2378 /* Destination event lines are stored in the upper halfword */
2379 if (!src)
2380 bit <<= 16;
2381
2382 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2383 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2384}
2385
2386static void d40_set_prio_realtime(struct d40_chan *d40c)
2387{
2388 if (d40c->base->rev < 3)
2389 return;
2390
Lee Jones2c2b62d2013-05-15 10:51:54 +01002391 if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
2392 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
Lee Jones26955c07d2013-05-03 15:31:56 +01002393 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002394
Lee Jones2c2b62d2013-05-15 10:51:54 +01002395 if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
2396 (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
Lee Jones26955c07d2013-05-03 15:31:56 +01002397 __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002398}
2399
Lee Jonesfa332de2013-05-03 15:32:12 +01002400#define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2401#define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2402#define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2403#define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2404
2405static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2406 struct of_dma *ofdma)
2407{
2408 struct stedma40_chan_cfg cfg;
2409 dma_cap_mask_t cap;
2410 u32 flags;
2411
2412 memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2413
2414 dma_cap_zero(cap);
2415 dma_cap_set(DMA_SLAVE, cap);
2416
2417 cfg.dev_type = dma_spec->args[0];
2418 flags = dma_spec->args[2];
2419
2420 switch (D40_DT_FLAGS_MODE(flags)) {
2421 case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2422 case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2423 }
2424
2425 switch (D40_DT_FLAGS_DIR(flags)) {
2426 case 0:
Lee Jones2c2b62d2013-05-15 10:51:54 +01002427 cfg.dir = DMA_MEM_TO_DEV;
Lee Jonesfa332de2013-05-03 15:32:12 +01002428 cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2429 break;
2430 case 1:
Lee Jones2c2b62d2013-05-15 10:51:54 +01002431 cfg.dir = DMA_DEV_TO_MEM;
Lee Jonesfa332de2013-05-03 15:32:12 +01002432 cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2433 break;
2434 }
2435
2436 if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2437 cfg.phy_channel = dma_spec->args[1];
2438 cfg.use_fixed_channel = true;
2439 }
2440
2441 return dma_request_channel(cap, stedma40_filter, &cfg);
2442}
2443
Linus Walleij8d318a52010-03-30 15:33:42 +02002444/* DMA ENGINE functions */
2445static int d40_alloc_chan_resources(struct dma_chan *chan)
2446{
2447 int err;
2448 unsigned long flags;
2449 struct d40_chan *d40c =
2450 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002451 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002452 spin_lock_irqsave(&d40c->lock, flags);
2453
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002454 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002455
Rabin Vincentce2ca122010-10-12 13:00:49 +00002456 /* If no dma configuration is set use default configuration (memcpy) */
2457 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002458 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002459 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002460 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002461 goto fail;
2462 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002463 }
2464
Narayanan G5cd326f2011-11-30 19:20:42 +05302465 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002466 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002467 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302468 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002469 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002470 }
2471
Narayanan G7fb3e752011-11-17 17:26:41 +05302472 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002473
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002474 d40_set_prio_realtime(d40c);
2475
Rabin Vincent724a8572011-01-25 11:18:08 +01002476 if (chan_is_logical(d40c)) {
Lee Jones2c2b62d2013-05-15 10:51:54 +01002477 if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
Linus Walleijef1872e2010-06-20 21:24:52 +00002478 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002479 d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002480 else
2481 d40c->lcpa = d40c->base->lcpa_base +
Lee Jones26955c07d2013-05-03 15:31:56 +01002482 d40c->dma_cfg.dev_type *
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002483 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Lee Jones97782562013-05-15 10:51:24 +01002484
2485 /* Unmask the Global Interrupt Mask. */
2486 d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2487 d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
Linus Walleijef1872e2010-06-20 21:24:52 +00002488 }
2489
Narayanan G5cd326f2011-11-30 19:20:42 +05302490 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2491 chan_is_logical(d40c) ? "logical" : "physical",
2492 d40c->phy_chan->num,
2493 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2494
2495
Linus Walleijef1872e2010-06-20 21:24:52 +00002496 /*
2497 * Only write channel configuration to the DMA if the physical
2498 * resource is free. In case of multiple logical channels
2499 * on the same physical resource, only the first write is necessary.
2500 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002501 if (is_free_phy)
2502 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002503fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302504 pm_runtime_mark_last_busy(d40c->base->dev);
2505 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002506 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002507 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002508}
2509
2510static void d40_free_chan_resources(struct dma_chan *chan)
2511{
2512 struct d40_chan *d40c =
2513 container_of(chan, struct d40_chan, chan);
2514 int err;
2515 unsigned long flags;
2516
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002517 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002518 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002519 return;
2520 }
2521
Linus Walleij8d318a52010-03-30 15:33:42 +02002522 spin_lock_irqsave(&d40c->lock, flags);
2523
2524 err = d40_free_dma(d40c);
2525
2526 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002527 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002528 spin_unlock_irqrestore(&d40c->lock, flags);
2529}
2530
2531static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2532 dma_addr_t dst,
2533 dma_addr_t src,
2534 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002535 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002536{
Rabin Vincent95944c62011-01-25 11:18:17 +01002537 struct scatterlist dst_sg;
2538 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002539
Rabin Vincent95944c62011-01-25 11:18:17 +01002540 sg_init_table(&dst_sg, 1);
2541 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002542
Rabin Vincent95944c62011-01-25 11:18:17 +01002543 sg_dma_address(&dst_sg) = dst;
2544 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002545
Rabin Vincent95944c62011-01-25 11:18:17 +01002546 sg_dma_len(&dst_sg) = size;
2547 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002548
Rabin Vincentcade1d32011-01-25 11:18:23 +01002549 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002550}
2551
Ira Snyder0d688662010-09-30 11:46:47 +00002552static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002553d40_prep_memcpy_sg(struct dma_chan *chan,
2554 struct scatterlist *dst_sg, unsigned int dst_nents,
2555 struct scatterlist *src_sg, unsigned int src_nents,
2556 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002557{
2558 if (dst_nents != src_nents)
2559 return NULL;
2560
Rabin Vincentcade1d32011-01-25 11:18:23 +01002561 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002562}
2563
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002564static struct dma_async_tx_descriptor *
2565d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2566 unsigned int sg_len, enum dma_transfer_direction direction,
2567 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002568{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002569 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002570 return NULL;
2571
Rabin Vincentcade1d32011-01-25 11:18:23 +01002572 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002573}
2574
Rabin Vincent0c842b52011-01-25 11:18:35 +01002575static struct dma_async_tx_descriptor *
2576dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2577 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002578 enum dma_transfer_direction direction, unsigned long flags,
2579 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002580{
2581 unsigned int periods = buf_len / period_len;
2582 struct dma_async_tx_descriptor *txd;
2583 struct scatterlist *sg;
2584 int i;
2585
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002586 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002587 for (i = 0; i < periods; i++) {
2588 sg_dma_address(&sg[i]) = dma_addr;
2589 sg_dma_len(&sg[i]) = period_len;
2590 dma_addr += period_len;
2591 }
2592
2593 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002594 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002595 sg[periods].page_link =
2596 ((unsigned long)sg | 0x01) & ~0x02;
2597
2598 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2599 DMA_PREP_INTERRUPT);
2600
2601 kfree(sg);
2602
2603 return txd;
2604}
2605
Linus Walleij8d318a52010-03-30 15:33:42 +02002606static enum dma_status d40_tx_status(struct dma_chan *chan,
2607 dma_cookie_t cookie,
2608 struct dma_tx_state *txstate)
2609{
2610 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002611 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002612
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002613 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002614 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002615 return -EINVAL;
2616 }
2617
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002618 ret = dma_cookie_status(chan, cookie, txstate);
2619 if (ret != DMA_SUCCESS)
2620 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002621
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002622 if (d40_is_paused(d40c))
2623 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002624
2625 return ret;
2626}
2627
2628static void d40_issue_pending(struct dma_chan *chan)
2629{
2630 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2631 unsigned long flags;
2632
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002633 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002634 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002635 return;
2636 }
2637
Linus Walleij8d318a52010-03-30 15:33:42 +02002638 spin_lock_irqsave(&d40c->lock, flags);
2639
Per Forlina8f30672011-06-26 23:29:52 +02002640 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2641
2642 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002643 if (!d40c->busy)
2644 (void) d40_queue_start(d40c);
2645
2646 spin_unlock_irqrestore(&d40c->lock, flags);
2647}
2648
Narayanan G1bdae6f2012-02-09 12:41:37 +05302649static void d40_terminate_all(struct dma_chan *chan)
2650{
2651 unsigned long flags;
2652 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2653 int ret;
2654
2655 spin_lock_irqsave(&d40c->lock, flags);
2656
2657 pm_runtime_get_sync(d40c->base->dev);
2658 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2659 if (ret)
2660 chan_err(d40c, "Failed to stop channel\n");
2661
2662 d40_term_all(d40c);
2663 pm_runtime_mark_last_busy(d40c->base->dev);
2664 pm_runtime_put_autosuspend(d40c->base->dev);
2665 if (d40c->busy) {
2666 pm_runtime_mark_last_busy(d40c->base->dev);
2667 pm_runtime_put_autosuspend(d40c->base->dev);
2668 }
2669 d40c->busy = false;
2670
2671 spin_unlock_irqrestore(&d40c->lock, flags);
2672}
2673
Rabin Vincent98ca5282011-06-27 11:33:38 +02002674static int
2675dma40_config_to_halfchannel(struct d40_chan *d40c,
2676 struct stedma40_half_channel_info *info,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002677 u32 maxburst)
2678{
Rabin Vincent98ca5282011-06-27 11:33:38 +02002679 int psize;
2680
Rabin Vincent98ca5282011-06-27 11:33:38 +02002681 if (chan_is_logical(d40c)) {
2682 if (maxburst >= 16)
2683 psize = STEDMA40_PSIZE_LOG_16;
2684 else if (maxburst >= 8)
2685 psize = STEDMA40_PSIZE_LOG_8;
2686 else if (maxburst >= 4)
2687 psize = STEDMA40_PSIZE_LOG_4;
2688 else
2689 psize = STEDMA40_PSIZE_LOG_1;
2690 } else {
2691 if (maxburst >= 16)
2692 psize = STEDMA40_PSIZE_PHY_16;
2693 else if (maxburst >= 8)
2694 psize = STEDMA40_PSIZE_PHY_8;
2695 else if (maxburst >= 4)
2696 psize = STEDMA40_PSIZE_PHY_4;
2697 else
2698 psize = STEDMA40_PSIZE_PHY_1;
2699 }
2700
Rabin Vincent98ca5282011-06-27 11:33:38 +02002701 info->psize = psize;
2702 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2703
2704 return 0;
2705}
2706
Linus Walleij95e14002010-08-04 13:37:45 +02002707/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002708static int d40_set_runtime_config(struct dma_chan *chan,
2709 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002710{
2711 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2712 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002713 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002714 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002715 u32 src_maxburst, dst_maxburst;
2716 int ret;
2717
2718 src_addr_width = config->src_addr_width;
2719 src_maxburst = config->src_maxburst;
2720 dst_addr_width = config->dst_addr_width;
2721 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002722
Vinod Kouldb8196d2011-10-13 22:34:23 +05302723 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002724 config_addr = config->src_addr;
Lee Jonesef9c89b32013-05-15 10:51:30 +01002725
Lee Jones2c2b62d2013-05-15 10:51:54 +01002726 if (cfg->dir != DMA_DEV_TO_MEM)
Linus Walleij95e14002010-08-04 13:37:45 +02002727 dev_dbg(d40c->base->dev,
2728 "channel was not configured for peripheral "
2729 "to memory transfer (%d) overriding\n",
2730 cfg->dir);
Lee Jones2c2b62d2013-05-15 10:51:54 +01002731 cfg->dir = DMA_DEV_TO_MEM;
Linus Walleij95e14002010-08-04 13:37:45 +02002732
Rabin Vincent98ca5282011-06-27 11:33:38 +02002733 /* Configure the memory side */
2734 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2735 dst_addr_width = src_addr_width;
2736 if (dst_maxburst == 0)
2737 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002738
Vinod Kouldb8196d2011-10-13 22:34:23 +05302739 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002740 config_addr = config->dst_addr;
Lee Jonesef9c89b32013-05-15 10:51:30 +01002741
Lee Jones2c2b62d2013-05-15 10:51:54 +01002742 if (cfg->dir != DMA_MEM_TO_DEV)
Linus Walleij95e14002010-08-04 13:37:45 +02002743 dev_dbg(d40c->base->dev,
2744 "channel was not configured for memory "
2745 "to peripheral transfer (%d) overriding\n",
2746 cfg->dir);
Lee Jones2c2b62d2013-05-15 10:51:54 +01002747 cfg->dir = DMA_MEM_TO_DEV;
Linus Walleij95e14002010-08-04 13:37:45 +02002748
Rabin Vincent98ca5282011-06-27 11:33:38 +02002749 /* Configure the memory side */
2750 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2751 src_addr_width = dst_addr_width;
2752 if (src_maxburst == 0)
2753 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002754 } else {
2755 dev_err(d40c->base->dev,
2756 "unrecognized channel direction %d\n",
2757 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002758 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002759 }
2760
Lee Jonesef9c89b32013-05-15 10:51:30 +01002761 if (config_addr <= 0) {
2762 dev_err(d40c->base->dev, "no address supplied\n");
2763 return -EINVAL;
2764 }
2765
Rabin Vincent98ca5282011-06-27 11:33:38 +02002766 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002767 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002768 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2769 src_maxburst,
2770 src_addr_width,
2771 dst_maxburst,
2772 dst_addr_width);
2773 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002774 }
2775
Per Forlin92bb6cd2011-10-13 12:11:36 +02002776 if (src_maxburst > 16) {
2777 src_maxburst = 16;
2778 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2779 } else if (dst_maxburst > 16) {
2780 dst_maxburst = 16;
2781 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2782 }
2783
Lee Jones43f2e1a2013-05-15 11:51:57 +02002784 /* Only valid widths are; 1, 2, 4 and 8. */
2785 if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2786 src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2787 dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
2788 dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2789 ((src_addr_width > 1) && (src_addr_width & 1)) ||
2790 ((dst_addr_width > 1) && (dst_addr_width & 1)))
2791 return -EINVAL;
2792
2793 cfg->src_info.data_width = src_addr_width;
2794 cfg->dst_info.data_width = dst_addr_width;
2795
Rabin Vincent98ca5282011-06-27 11:33:38 +02002796 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002797 src_maxburst);
2798 if (ret)
2799 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002800
Rabin Vincent98ca5282011-06-27 11:33:38 +02002801 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002802 dst_maxburst);
2803 if (ret)
2804 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002805
Per Forlina59670a2010-10-06 09:05:27 +00002806 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002807 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002808 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2809 else
Lee Jones57e65ad2013-05-15 10:51:25 +01002810 d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
Per Forlina59670a2010-10-06 09:05:27 +00002811
Linus Walleij95e14002010-08-04 13:37:45 +02002812 /* These settings will take precedence later */
2813 d40c->runtime_addr = config_addr;
2814 d40c->runtime_direction = config->direction;
2815 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002816 "configured channel %s for %s, data width %d/%d, "
2817 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002818 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302819 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002820 src_addr_width, dst_addr_width,
2821 src_maxburst, dst_maxburst);
2822
2823 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002824}
2825
Linus Walleij05827632010-05-17 16:30:42 -07002826static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2827 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002828{
Linus Walleij8d318a52010-03-30 15:33:42 +02002829 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2830
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002831 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002832 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002833 return -EINVAL;
2834 }
2835
Linus Walleij8d318a52010-03-30 15:33:42 +02002836 switch (cmd) {
2837 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302838 d40_terminate_all(chan);
2839 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002840 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002841 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002842 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002843 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002844 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002845 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002846 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002847 default:
2848 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002849 }
2850
2851 /* Other commands are unimplemented */
2852 return -ENXIO;
2853}
2854
2855/* Initialization functions */
2856
2857static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2858 struct d40_chan *chans, int offset,
2859 int num_chans)
2860{
2861 int i = 0;
2862 struct d40_chan *d40c;
2863
2864 INIT_LIST_HEAD(&dma->channels);
2865
2866 for (i = offset; i < offset + num_chans; i++) {
2867 d40c = &chans[i];
2868 d40c->base = base;
2869 d40c->chan.device = dma;
2870
Linus Walleij8d318a52010-03-30 15:33:42 +02002871 spin_lock_init(&d40c->lock);
2872
2873 d40c->log_num = D40_PHY_CHAN;
2874
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002875 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002876 INIT_LIST_HEAD(&d40c->active);
2877 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002878 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002879 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002880 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002881
Linus Walleij8d318a52010-03-30 15:33:42 +02002882 tasklet_init(&d40c->tasklet, dma_tasklet,
2883 (unsigned long) d40c);
2884
2885 list_add_tail(&d40c->chan.device_node,
2886 &dma->channels);
2887 }
2888}
2889
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002890static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2891{
2892 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2893 dev->device_prep_slave_sg = d40_prep_slave_sg;
2894
2895 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2896 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2897
2898 /*
2899 * This controller can only access address at even
2900 * 32bit boundaries, i.e. 2^2
2901 */
2902 dev->copy_align = 2;
2903 }
2904
2905 if (dma_has_cap(DMA_SG, dev->cap_mask))
2906 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2907
Rabin Vincent0c842b52011-01-25 11:18:35 +01002908 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2909 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2910
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002911 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2912 dev->device_free_chan_resources = d40_free_chan_resources;
2913 dev->device_issue_pending = d40_issue_pending;
2914 dev->device_tx_status = d40_tx_status;
2915 dev->device_control = d40_control;
2916 dev->dev = base->dev;
2917}
2918
Linus Walleij8d318a52010-03-30 15:33:42 +02002919static int __init d40_dmaengine_init(struct d40_base *base,
2920 int num_reserved_chans)
2921{
2922 int err ;
2923
2924 d40_chan_init(base, &base->dma_slave, base->log_chans,
2925 0, base->num_log_chans);
2926
2927 dma_cap_zero(base->dma_slave.cap_mask);
2928 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002929 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002930
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002931 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002932
2933 err = dma_async_device_register(&base->dma_slave);
2934
2935 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002936 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002937 goto failure1;
2938 }
2939
2940 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002941 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002942
2943 dma_cap_zero(base->dma_memcpy.cap_mask);
2944 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002945 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002946
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002947 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002948
2949 err = dma_async_device_register(&base->dma_memcpy);
2950
2951 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002952 d40_err(base->dev,
2953 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002954 goto failure2;
2955 }
2956
2957 d40_chan_init(base, &base->dma_both, base->phy_chans,
2958 0, num_reserved_chans);
2959
2960 dma_cap_zero(base->dma_both.cap_mask);
2961 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2962 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002963 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002964 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002965
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002966 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002967 err = dma_async_device_register(&base->dma_both);
2968
2969 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002970 d40_err(base->dev,
2971 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002972 goto failure3;
2973 }
2974 return 0;
2975failure3:
2976 dma_async_device_unregister(&base->dma_memcpy);
2977failure2:
2978 dma_async_device_unregister(&base->dma_slave);
2979failure1:
2980 return err;
2981}
2982
Narayanan G7fb3e752011-11-17 17:26:41 +05302983/* Suspend resume functionality */
2984#ifdef CONFIG_PM
2985static int dma40_pm_suspend(struct device *dev)
2986{
Narayanan G28c7a192011-11-22 13:56:55 +05302987 struct platform_device *pdev = to_platform_device(dev);
2988 struct d40_base *base = platform_get_drvdata(pdev);
2989 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302990
Narayanan G28c7a192011-11-22 13:56:55 +05302991 if (base->lcpa_regulator)
2992 ret = regulator_disable(base->lcpa_regulator);
2993 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302994}
2995
2996static int dma40_runtime_suspend(struct device *dev)
2997{
2998 struct platform_device *pdev = to_platform_device(dev);
2999 struct d40_base *base = platform_get_drvdata(pdev);
3000
3001 d40_save_restore_registers(base, true);
3002
3003 /* Don't disable/enable clocks for v1 due to HW bugs */
3004 if (base->rev != 1)
3005 writel_relaxed(base->gcc_pwr_off_mask,
3006 base->virtbase + D40_DREG_GCC);
3007
3008 return 0;
3009}
3010
3011static int dma40_runtime_resume(struct device *dev)
3012{
3013 struct platform_device *pdev = to_platform_device(dev);
3014 struct d40_base *base = platform_get_drvdata(pdev);
3015
3016 if (base->initialized)
3017 d40_save_restore_registers(base, false);
3018
3019 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3020 base->virtbase + D40_DREG_GCC);
3021 return 0;
3022}
3023
Narayanan G28c7a192011-11-22 13:56:55 +05303024static int dma40_resume(struct device *dev)
3025{
3026 struct platform_device *pdev = to_platform_device(dev);
3027 struct d40_base *base = platform_get_drvdata(pdev);
3028 int ret = 0;
3029
3030 if (base->lcpa_regulator)
3031 ret = regulator_enable(base->lcpa_regulator);
3032
3033 return ret;
3034}
Narayanan G7fb3e752011-11-17 17:26:41 +05303035
3036static const struct dev_pm_ops dma40_pm_ops = {
3037 .suspend = dma40_pm_suspend,
3038 .runtime_suspend = dma40_runtime_suspend,
3039 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303040 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303041};
3042#define DMA40_PM_OPS (&dma40_pm_ops)
3043#else
3044#define DMA40_PM_OPS NULL
3045#endif
3046
Linus Walleij8d318a52010-03-30 15:33:42 +02003047/* Initialization functions. */
3048
3049static int __init d40_phy_res_init(struct d40_base *base)
3050{
3051 int i;
3052 int num_phy_chans_avail = 0;
3053 u32 val[2];
3054 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303055 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003056
3057 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3058 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3059
3060 for (i = 0; i < base->num_phy_chans; i++) {
3061 base->phy_res[i].num = i;
3062 odd_even_bit += 2 * ((i % 2) == 0);
3063 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3064 /* Mark security only channels as occupied */
3065 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3066 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303067 base->phy_res[i].reserved = true;
3068 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3069 D40_DREG_GCC_SRC);
3070 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3071 D40_DREG_GCC_DST);
3072
3073
Linus Walleij8d318a52010-03-30 15:33:42 +02003074 } else {
3075 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3076 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303077 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003078 num_phy_chans_avail++;
3079 }
3080 spin_lock_init(&base->phy_res[i].lock);
3081 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003082
3083 /* Mark disabled channels as occupied */
3084 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003085 int chan = base->plat_data->disabled_channels[i];
3086
3087 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3088 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303089 base->phy_res[chan].reserved = true;
3090 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3091 D40_DREG_GCC_SRC);
3092 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3093 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003094 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003095 }
3096
Fabio Baltieri74070482012-12-18 12:25:14 +01003097 /* Mark soft_lli channels */
3098 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3099 int chan = base->plat_data->soft_lli_chans[i];
3100
3101 base->phy_res[chan].use_soft_lli = true;
3102 }
3103
Linus Walleij8d318a52010-03-30 15:33:42 +02003104 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3105 num_phy_chans_avail, base->num_phy_chans);
3106
3107 /* Verify settings extended vs standard */
3108 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3109
3110 for (i = 0; i < base->num_phy_chans; i++) {
3111
3112 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3113 (val[0] & 0x3) != 1)
3114 dev_info(base->dev,
3115 "[%s] INFO: channel %d is misconfigured (%d)\n",
3116 __func__, i, val[0] & 0x3);
3117
3118 val[0] = val[0] >> 2;
3119 }
3120
Narayanan G7fb3e752011-11-17 17:26:41 +05303121 /*
3122 * To keep things simple, Enable all clocks initially.
3123 * The clocks will get managed later post channel allocation.
3124 * The clocks for the event lines on which reserved channels exists
3125 * are not managed here.
3126 */
3127 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3128 base->gcc_pwr_off_mask = gcc;
3129
Linus Walleij8d318a52010-03-30 15:33:42 +02003130 return num_phy_chans_avail;
3131}
3132
3133static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3134{
Lee Jonesbb75d932013-05-03 15:32:10 +01003135 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003136 struct clk *clk = NULL;
3137 void __iomem *virtbase = NULL;
3138 struct resource *res = NULL;
3139 struct d40_base *base = NULL;
3140 int num_log_chans = 0;
3141 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003142 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003143 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003144 u32 pid;
3145 u32 cid;
3146 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003147
3148 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003149 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003150 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003151 goto failure;
3152 }
3153
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003154 clk_ret = clk_prepare_enable(clk);
3155 if (clk_ret) {
3156 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3157 goto failure;
3158 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003159
3160 /* Get IO for DMAC base address */
3161 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3162 if (!res)
3163 goto failure;
3164
3165 if (request_mem_region(res->start, resource_size(res),
3166 D40_NAME " I/O base") == NULL)
3167 goto failure;
3168
3169 virtbase = ioremap(res->start, resource_size(res));
3170 if (!virtbase)
3171 goto failure;
3172
Linus Walleijf4b89762011-06-27 11:33:46 +02003173 /* This is just a regular AMBA PrimeCell ID actually */
3174 for (pid = 0, i = 0; i < 4; i++)
3175 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3176 & 255) << (i * 8);
3177 for (cid = 0, i = 0; i < 4; i++)
3178 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3179 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003180
Linus Walleijf4b89762011-06-27 11:33:46 +02003181 if (cid != AMBA_CID) {
3182 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003183 goto failure;
3184 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003185 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3186 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3187 AMBA_MANF_BITS(pid),
3188 AMBA_VENDOR_ST);
3189 goto failure;
3190 }
3191 /*
3192 * HW revision:
3193 * DB8500ed has revision 0
3194 * ? has revision 1
3195 * DB8500v1 has revision 2
3196 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003197 * AP9540v1 has revision 4
3198 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003199 */
3200 rev = AMBA_REV_BITS(pid);
Lee Jones8b2fe9b2013-05-03 15:32:08 +01003201 if (rev < 2) {
3202 d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
3203 goto failure;
3204 }
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003205
Gerald Baeza47db92f2012-09-21 21:21:37 +02003206 /* The number of physical channels on this HW */
3207 if (plat_data->num_of_phy_chans)
3208 num_phy_chans = plat_data->num_of_phy_chans;
3209 else
3210 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3211
Lee Jonesdb72da92013-05-03 15:32:03 +01003212 num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3213
Lee Jonesb2abb242013-05-03 15:32:09 +01003214 dev_info(&pdev->dev,
3215 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
3216 rev, res->start, num_phy_chans, num_log_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003217
Linus Walleij8d318a52010-03-30 15:33:42 +02003218 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003219 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003220 sizeof(struct d40_chan), GFP_KERNEL);
3221
3222 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003223 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003224 goto failure;
3225 }
3226
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003227 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003228 base->clk = clk;
3229 base->num_phy_chans = num_phy_chans;
3230 base->num_log_chans = num_log_chans;
3231 base->phy_start = res->start;
3232 base->phy_size = resource_size(res);
3233 base->virtbase = virtbase;
3234 base->plat_data = plat_data;
3235 base->dev = &pdev->dev;
3236 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3237 base->log_chans = &base->phy_chans[num_phy_chans];
3238
Tong Liu3cb645d2012-09-26 10:07:30 +00003239 if (base->plat_data->num_of_phy_chans == 14) {
3240 base->gen_dmac.backup = d40_backup_regs_v4b;
3241 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3242 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3243 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3244 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3245 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3246 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3247 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3248 base->gen_dmac.il = il_v4b;
3249 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3250 base->gen_dmac.init_reg = dma_init_reg_v4b;
3251 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3252 } else {
3253 if (base->rev >= 3) {
3254 base->gen_dmac.backup = d40_backup_regs_v4a;
3255 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3256 }
3257 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3258 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3259 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3260 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3261 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3262 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3263 base->gen_dmac.il = il_v4a;
3264 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3265 base->gen_dmac.init_reg = dma_init_reg_v4a;
3266 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3267 }
3268
Linus Walleij8d318a52010-03-30 15:33:42 +02003269 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3270 GFP_KERNEL);
3271 if (!base->phy_res)
3272 goto failure;
3273
3274 base->lookup_phy_chans = kzalloc(num_phy_chans *
3275 sizeof(struct d40_chan *),
3276 GFP_KERNEL);
3277 if (!base->lookup_phy_chans)
3278 goto failure;
3279
Lee Jones8a59fed2013-05-03 15:32:04 +01003280 base->lookup_log_chans = kzalloc(num_log_chans *
3281 sizeof(struct d40_chan *),
3282 GFP_KERNEL);
3283 if (!base->lookup_log_chans)
3284 goto failure;
Jonas Aaberg698e4732010-08-09 12:08:56 +00003285
Narayanan G7fb3e752011-11-17 17:26:41 +05303286 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3287 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003288 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303289 if (!base->reg_val_backup_chan)
3290 goto failure;
3291
3292 base->lcla_pool.alloc_map =
3293 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3294 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003295 if (!base->lcla_pool.alloc_map)
3296 goto failure;
3297
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003298 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3299 0, SLAB_HWCACHE_ALIGN,
3300 NULL);
3301 if (base->desc_slab == NULL)
3302 goto failure;
3303
Linus Walleij8d318a52010-03-30 15:33:42 +02003304 return base;
3305
3306failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003307 if (!clk_ret)
3308 clk_disable_unprepare(clk);
3309 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003310 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003311 if (virtbase)
3312 iounmap(virtbase);
3313 if (res)
3314 release_mem_region(res->start,
3315 resource_size(res));
3316 if (virtbase)
3317 iounmap(virtbase);
3318
3319 if (base) {
3320 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303321 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003322 kfree(base->lookup_log_chans);
3323 kfree(base->lookup_phy_chans);
3324 kfree(base->phy_res);
3325 kfree(base);
3326 }
3327
3328 return NULL;
3329}
3330
3331static void __init d40_hw_init(struct d40_base *base)
3332{
3333
Linus Walleij8d318a52010-03-30 15:33:42 +02003334 int i;
3335 u32 prmseo[2] = {0, 0};
3336 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3337 u32 pcmis = 0;
3338 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003339 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3340 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003341
Tong Liu3cb645d2012-09-26 10:07:30 +00003342 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003343 writel(dma_init_reg[i].val,
3344 base->virtbase + dma_init_reg[i].reg);
3345
3346 /* Configure all our dma channels to default settings */
3347 for (i = 0; i < base->num_phy_chans; i++) {
3348
3349 activeo[i % 2] = activeo[i % 2] << 2;
3350
3351 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3352 == D40_ALLOC_PHY) {
3353 activeo[i % 2] |= 3;
3354 continue;
3355 }
3356
3357 /* Enable interrupt # */
3358 pcmis = (pcmis << 1) | 1;
3359
3360 /* Clear interrupt # */
3361 pcicr = (pcicr << 1) | 1;
3362
3363 /* Set channel to physical mode */
3364 prmseo[i % 2] = prmseo[i % 2] << 2;
3365 prmseo[i % 2] |= 1;
3366
3367 }
3368
3369 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3370 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3371 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3372 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3373
3374 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003375 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003376
3377 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003378 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003379
Tong Liu3cb645d2012-09-26 10:07:30 +00003380 /* These are __initdata and cannot be accessed after init */
3381 base->gen_dmac.init_reg = NULL;
3382 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003383}
3384
Linus Walleij508849a2010-06-20 21:26:07 +00003385static int __init d40_lcla_allocate(struct d40_base *base)
3386{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003387 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003388 unsigned long *page_list;
3389 int i, j;
3390 int ret = 0;
3391
3392 /*
3393 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3394 * To full fill this hardware requirement without wasting 256 kb
3395 * we allocate pages until we get an aligned one.
3396 */
3397 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3398 GFP_KERNEL);
3399
3400 if (!page_list) {
3401 ret = -ENOMEM;
3402 goto failure;
3403 }
3404
3405 /* Calculating how many pages that are required */
3406 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3407
3408 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3409 page_list[i] = __get_free_pages(GFP_KERNEL,
3410 base->lcla_pool.pages);
3411 if (!page_list[i]) {
3412
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003413 d40_err(base->dev, "Failed to allocate %d pages.\n",
3414 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003415
3416 for (j = 0; j < i; j++)
3417 free_pages(page_list[j], base->lcla_pool.pages);
3418 goto failure;
3419 }
3420
3421 if ((virt_to_phys((void *)page_list[i]) &
3422 (LCLA_ALIGNMENT - 1)) == 0)
3423 break;
3424 }
3425
3426 for (j = 0; j < i; j++)
3427 free_pages(page_list[j], base->lcla_pool.pages);
3428
3429 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3430 base->lcla_pool.base = (void *)page_list[i];
3431 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003432 /*
3433 * After many attempts and no succees with finding the correct
3434 * alignment, try with allocating a big buffer.
3435 */
Linus Walleij508849a2010-06-20 21:26:07 +00003436 dev_warn(base->dev,
3437 "[%s] Failed to get %d pages @ 18 bit align.\n",
3438 __func__, base->lcla_pool.pages);
3439 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3440 base->num_phy_chans +
3441 LCLA_ALIGNMENT,
3442 GFP_KERNEL);
3443 if (!base->lcla_pool.base_unaligned) {
3444 ret = -ENOMEM;
3445 goto failure;
3446 }
3447
3448 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3449 LCLA_ALIGNMENT);
3450 }
3451
Rabin Vincent026cbc42011-01-25 11:18:14 +01003452 pool->dma_addr = dma_map_single(base->dev, pool->base,
3453 SZ_1K * base->num_phy_chans,
3454 DMA_TO_DEVICE);
3455 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3456 pool->dma_addr = 0;
3457 ret = -ENOMEM;
3458 goto failure;
3459 }
3460
Linus Walleij508849a2010-06-20 21:26:07 +00003461 writel(virt_to_phys(base->lcla_pool.base),
3462 base->virtbase + D40_DREG_LCLA);
3463failure:
3464 kfree(page_list);
3465 return ret;
3466}
3467
Lee Jones1814a172013-05-03 15:32:11 +01003468static int __init d40_of_probe(struct platform_device *pdev,
3469 struct device_node *np)
3470{
3471 struct stedma40_platform_data *pdata;
3472
3473 /*
3474 * FIXME: Fill in this routine as more support is added.
3475 * First platform enabled (u8500) doens't need any extra
3476 * properties to run, so this is fairly sparce currently.
3477 */
3478
3479 pdata = devm_kzalloc(&pdev->dev,
3480 sizeof(struct stedma40_platform_data),
3481 GFP_KERNEL);
3482 if (!pdata)
3483 return -ENOMEM;
3484
3485 pdev->dev.platform_data = pdata;
3486
3487 return 0;
3488}
3489
Linus Walleij8d318a52010-03-30 15:33:42 +02003490static int __init d40_probe(struct platform_device *pdev)
3491{
Lee Jones1814a172013-05-03 15:32:11 +01003492 struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3493 struct device_node *np = pdev->dev.of_node;
Linus Walleij8d318a52010-03-30 15:33:42 +02003494 int err;
3495 int ret = -ENOENT;
Lee Jones1814a172013-05-03 15:32:11 +01003496 struct d40_base *base = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003497 struct resource *res = NULL;
3498 int num_reserved_chans;
3499 u32 val;
3500
Lee Jones1814a172013-05-03 15:32:11 +01003501 if (!plat_data) {
3502 if (np) {
3503 if(d40_of_probe(pdev, np)) {
3504 ret = -ENOMEM;
3505 goto failure;
3506 }
3507 } else {
3508 d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3509 goto failure;
3510 }
3511 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003512
Lee Jones1814a172013-05-03 15:32:11 +01003513 base = d40_hw_detect_init(pdev);
Linus Walleij8d318a52010-03-30 15:33:42 +02003514 if (!base)
3515 goto failure;
3516
3517 num_reserved_chans = d40_phy_res_init(base);
3518
3519 platform_set_drvdata(pdev, base);
3520
3521 spin_lock_init(&base->interrupt_lock);
3522 spin_lock_init(&base->execmd_lock);
3523
3524 /* Get IO for logical channel parameter address */
3525 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3526 if (!res) {
3527 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003528 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003529 goto failure;
3530 }
3531 base->lcpa_size = resource_size(res);
3532 base->phy_lcpa = res->start;
3533
3534 if (request_mem_region(res->start, resource_size(res),
3535 D40_NAME " I/O lcpa") == NULL) {
3536 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003537 d40_err(&pdev->dev,
3538 "Failed to request LCPA region 0x%x-0x%x\n",
3539 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003540 goto failure;
3541 }
3542
3543 /* We make use of ESRAM memory for this. */
3544 val = readl(base->virtbase + D40_DREG_LCPA);
3545 if (res->start != val && val != 0) {
3546 dev_warn(&pdev->dev,
3547 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3548 __func__, val, res->start);
3549 } else
3550 writel(res->start, base->virtbase + D40_DREG_LCPA);
3551
3552 base->lcpa_base = ioremap(res->start, resource_size(res));
3553 if (!base->lcpa_base) {
3554 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003555 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003556 goto failure;
3557 }
Narayanan G28c7a192011-11-22 13:56:55 +05303558 /* If lcla has to be located in ESRAM we don't need to allocate */
3559 if (base->plat_data->use_esram_lcla) {
3560 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3561 "lcla_esram");
3562 if (!res) {
3563 ret = -ENOENT;
3564 d40_err(&pdev->dev,
3565 "No \"lcla_esram\" memory resource\n");
3566 goto failure;
3567 }
3568 base->lcla_pool.base = ioremap(res->start,
3569 resource_size(res));
3570 if (!base->lcla_pool.base) {
3571 ret = -ENOMEM;
3572 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3573 goto failure;
3574 }
3575 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003576
Narayanan G28c7a192011-11-22 13:56:55 +05303577 } else {
3578 ret = d40_lcla_allocate(base);
3579 if (ret) {
3580 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3581 goto failure;
3582 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003583 }
3584
Linus Walleij8d318a52010-03-30 15:33:42 +02003585 spin_lock_init(&base->lcla_pool.lock);
3586
Linus Walleij8d318a52010-03-30 15:33:42 +02003587 base->irq = platform_get_irq(pdev, 0);
3588
3589 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003590 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003591 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003592 goto failure;
3593 }
3594
Narayanan G7fb3e752011-11-17 17:26:41 +05303595 pm_runtime_irq_safe(base->dev);
3596 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3597 pm_runtime_use_autosuspend(base->dev);
3598 pm_runtime_enable(base->dev);
3599 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303600
3601 if (base->plat_data->use_esram_lcla) {
3602
3603 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3604 if (IS_ERR(base->lcpa_regulator)) {
3605 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3606 base->lcpa_regulator = NULL;
3607 goto failure;
3608 }
3609
3610 ret = regulator_enable(base->lcpa_regulator);
3611 if (ret) {
3612 d40_err(&pdev->dev,
3613 "Failed to enable lcpa_regulator\n");
3614 regulator_put(base->lcpa_regulator);
3615 base->lcpa_regulator = NULL;
3616 goto failure;
3617 }
3618 }
3619
Narayanan G7fb3e752011-11-17 17:26:41 +05303620 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003621 err = d40_dmaengine_init(base, num_reserved_chans);
3622 if (err)
3623 goto failure;
3624
Per Forlinb96710e2011-10-18 18:39:47 +02003625 base->dev->dma_parms = &base->dma_parms;
3626 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3627 if (err) {
3628 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3629 goto failure;
3630 }
3631
Linus Walleij8d318a52010-03-30 15:33:42 +02003632 d40_hw_init(base);
3633
Lee Jonesfa332de2013-05-03 15:32:12 +01003634 if (np) {
3635 err = of_dma_controller_register(np, d40_xlate, NULL);
3636 if (err && err != -ENODEV)
3637 dev_err(&pdev->dev,
3638 "could not register of_dma_controller\n");
3639 }
3640
Linus Walleij8d318a52010-03-30 15:33:42 +02003641 dev_info(base->dev, "initialized\n");
3642 return 0;
3643
3644failure:
3645 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003646 if (base->desc_slab)
3647 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003648 if (base->virtbase)
3649 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003650
Narayanan G28c7a192011-11-22 13:56:55 +05303651 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3652 iounmap(base->lcla_pool.base);
3653 base->lcla_pool.base = NULL;
3654 }
3655
Rabin Vincent026cbc42011-01-25 11:18:14 +01003656 if (base->lcla_pool.dma_addr)
3657 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3658 SZ_1K * base->num_phy_chans,
3659 DMA_TO_DEVICE);
3660
Linus Walleij508849a2010-06-20 21:26:07 +00003661 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3662 free_pages((unsigned long)base->lcla_pool.base,
3663 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003664
3665 kfree(base->lcla_pool.base_unaligned);
3666
Linus Walleij8d318a52010-03-30 15:33:42 +02003667 if (base->phy_lcpa)
3668 release_mem_region(base->phy_lcpa,
3669 base->lcpa_size);
3670 if (base->phy_start)
3671 release_mem_region(base->phy_start,
3672 base->phy_size);
3673 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003674 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003675 clk_put(base->clk);
3676 }
3677
Narayanan G28c7a192011-11-22 13:56:55 +05303678 if (base->lcpa_regulator) {
3679 regulator_disable(base->lcpa_regulator);
3680 regulator_put(base->lcpa_regulator);
3681 }
3682
Linus Walleij8d318a52010-03-30 15:33:42 +02003683 kfree(base->lcla_pool.alloc_map);
3684 kfree(base->lookup_log_chans);
3685 kfree(base->lookup_phy_chans);
3686 kfree(base->phy_res);
3687 kfree(base);
3688 }
3689
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003690 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003691 return ret;
3692}
3693
Lee Jones1814a172013-05-03 15:32:11 +01003694static const struct of_device_id d40_match[] = {
3695 { .compatible = "stericsson,dma40", },
3696 {}
3697};
3698
Linus Walleij8d318a52010-03-30 15:33:42 +02003699static struct platform_driver d40_driver = {
3700 .driver = {
3701 .owner = THIS_MODULE,
3702 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303703 .pm = DMA40_PM_OPS,
Lee Jones1814a172013-05-03 15:32:11 +01003704 .of_match_table = d40_match,
Linus Walleij8d318a52010-03-30 15:33:42 +02003705 },
3706};
3707
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003708static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003709{
3710 return platform_driver_probe(&d40_driver, d40_probe);
3711}
Linus Walleija0eb2212011-05-18 14:18:57 +02003712subsys_initcall(stedma40_init);