blob: cd7b4808d08cc97c5b172347c1dea956bf67a8b1 [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>
Linus Walleijf4b89762011-06-27 11:33:46 +020020#include <linux/amba/bus.h>
Linus Walleij15e4b782012-04-12 18:12:43 +020021#include <linux/regulator/consumer.h>
Linus Walleij865fab62012-10-18 14:20:16 +020022#include <linux/platform_data/dma-ste-dma40.h>
Linus Walleij8d318a52010-03-30 15:33:42 +020023
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000024#include "dmaengine.h"
Linus Walleij8d318a52010-03-30 15:33:42 +020025#include "ste_dma40_ll.h"
26
27#define D40_NAME "dma40"
28
29#define D40_PHY_CHAN -1
30
31/* For masking out/in 2 bit channel positions */
32#define D40_CHAN_POS(chan) (2 * (chan / 2))
33#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
34
35/* Maximum iterations taken before giving up suspending a channel */
36#define D40_SUSPEND_MAX_IT 500
37
Narayanan G7fb3e752011-11-17 17:26:41 +053038/* Milliseconds */
39#define DMA40_AUTOSUSPEND_DELAY 100
40
Linus Walleij508849a2010-06-20 21:26:07 +000041/* Hardware requirement on LCLA alignment */
42#define LCLA_ALIGNMENT 0x40000
Jonas Aaberg698e4732010-08-09 12:08:56 +000043
44/* Max number of links per event group */
45#define D40_LCLA_LINK_PER_EVENT_GRP 128
46#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
47
Linus Walleij508849a2010-06-20 21:26:07 +000048/* Attempts before giving up to trying to get pages that are aligned */
49#define MAX_LCLA_ALLOC_ATTEMPTS 256
50
51/* Bit markings for allocation map */
Linus Walleij8d318a52010-03-30 15:33:42 +020052#define D40_ALLOC_FREE (1 << 31)
53#define D40_ALLOC_PHY (1 << 30)
54#define D40_ALLOC_LOG_FREE 0
55
Tong Liu3cb645d2012-09-26 10:07:30 +000056#define MAX(a, b) (((a) < (b)) ? (b) : (a))
57
Lee Jones664a57e2013-05-03 15:31:53 +010058/* Reserved event lines for memcpy only. */
Linus Walleija2acaa22013-05-03 21:46:09 +020059#define DB8500_DMA_MEMCPY_EV_0 51
60#define DB8500_DMA_MEMCPY_EV_1 56
61#define DB8500_DMA_MEMCPY_EV_2 57
62#define DB8500_DMA_MEMCPY_EV_3 58
63#define DB8500_DMA_MEMCPY_EV_4 59
64#define DB8500_DMA_MEMCPY_EV_5 60
65
66static int dma40_memcpy_channels[] = {
67 DB8500_DMA_MEMCPY_EV_0,
68 DB8500_DMA_MEMCPY_EV_1,
69 DB8500_DMA_MEMCPY_EV_2,
70 DB8500_DMA_MEMCPY_EV_3,
71 DB8500_DMA_MEMCPY_EV_4,
72 DB8500_DMA_MEMCPY_EV_5,
73};
Lee Jones664a57e2013-05-03 15:31:53 +010074
Linus Walleij8d318a52010-03-30 15:33:42 +020075/**
76 * enum 40_command - The different commands and/or statuses.
77 *
78 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
79 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
80 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
81 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
82 */
83enum d40_command {
84 D40_DMA_STOP = 0,
85 D40_DMA_RUN = 1,
86 D40_DMA_SUSPEND_REQ = 2,
87 D40_DMA_SUSPENDED = 3
88};
89
Narayanan G7fb3e752011-11-17 17:26:41 +053090/*
Narayanan G1bdae6f2012-02-09 12:41:37 +053091 * enum d40_events - The different Event Enables for the event lines.
92 *
93 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
94 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
95 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
96 * @D40_ROUND_EVENTLINE: Status check for event line.
97 */
98
99enum d40_events {
100 D40_DEACTIVATE_EVENTLINE = 0,
101 D40_ACTIVATE_EVENTLINE = 1,
102 D40_SUSPEND_REQ_EVENTLINE = 2,
103 D40_ROUND_EVENTLINE = 3
104};
105
106/*
Narayanan G7fb3e752011-11-17 17:26:41 +0530107 * These are the registers that has to be saved and later restored
108 * when the DMA hw is powered off.
109 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
110 */
111static u32 d40_backup_regs[] = {
112 D40_DREG_LCPA,
113 D40_DREG_LCLA,
114 D40_DREG_PRMSE,
115 D40_DREG_PRMSO,
116 D40_DREG_PRMOE,
117 D40_DREG_PRMOO,
118};
119
120#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
121
Tong Liu3cb645d2012-09-26 10:07:30 +0000122/*
123 * since 9540 and 8540 has the same HW revision
124 * use v4a for 9540 or ealier
125 * use v4b for 8540 or later
126 * HW revision:
127 * DB8500ed has revision 0
128 * DB8500v1 has revision 2
129 * DB8500v2 has revision 3
130 * AP9540v1 has revision 4
131 * DB8540v1 has revision 4
132 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
133 */
134static u32 d40_backup_regs_v4a[] = {
Narayanan G7fb3e752011-11-17 17:26:41 +0530135 D40_DREG_PSEG1,
136 D40_DREG_PSEG2,
137 D40_DREG_PSEG3,
138 D40_DREG_PSEG4,
139 D40_DREG_PCEG1,
140 D40_DREG_PCEG2,
141 D40_DREG_PCEG3,
142 D40_DREG_PCEG4,
143 D40_DREG_RSEG1,
144 D40_DREG_RSEG2,
145 D40_DREG_RSEG3,
146 D40_DREG_RSEG4,
147 D40_DREG_RCEG1,
148 D40_DREG_RCEG2,
149 D40_DREG_RCEG3,
150 D40_DREG_RCEG4,
151};
152
Tong Liu3cb645d2012-09-26 10:07:30 +0000153#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
154
155static u32 d40_backup_regs_v4b[] = {
156 D40_DREG_CPSEG1,
157 D40_DREG_CPSEG2,
158 D40_DREG_CPSEG3,
159 D40_DREG_CPSEG4,
160 D40_DREG_CPSEG5,
161 D40_DREG_CPCEG1,
162 D40_DREG_CPCEG2,
163 D40_DREG_CPCEG3,
164 D40_DREG_CPCEG4,
165 D40_DREG_CPCEG5,
166 D40_DREG_CRSEG1,
167 D40_DREG_CRSEG2,
168 D40_DREG_CRSEG3,
169 D40_DREG_CRSEG4,
170 D40_DREG_CRSEG5,
171 D40_DREG_CRCEG1,
172 D40_DREG_CRCEG2,
173 D40_DREG_CRCEG3,
174 D40_DREG_CRCEG4,
175 D40_DREG_CRCEG5,
176};
177
178#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
Narayanan G7fb3e752011-11-17 17:26:41 +0530179
180static u32 d40_backup_regs_chan[] = {
181 D40_CHAN_REG_SSCFG,
182 D40_CHAN_REG_SSELT,
183 D40_CHAN_REG_SSPTR,
184 D40_CHAN_REG_SSLNK,
185 D40_CHAN_REG_SDCFG,
186 D40_CHAN_REG_SDELT,
187 D40_CHAN_REG_SDPTR,
188 D40_CHAN_REG_SDLNK,
189};
190
Linus Walleij8d318a52010-03-30 15:33:42 +0200191/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000192 * struct d40_interrupt_lookup - lookup table for interrupt handler
193 *
194 * @src: Interrupt mask register.
195 * @clr: Interrupt clear register.
196 * @is_error: true if this is an error interrupt.
197 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
198 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
199 */
200struct d40_interrupt_lookup {
201 u32 src;
202 u32 clr;
203 bool is_error;
204 int offset;
205};
206
207
208static struct d40_interrupt_lookup il_v4a[] = {
209 {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
210 {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
211 {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
212 {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
213 {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
214 {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
215 {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
216 {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
217 {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
218 {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
219};
220
221static struct d40_interrupt_lookup il_v4b[] = {
222 {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
223 {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
224 {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
225 {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
226 {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
227 {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
228 {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
229 {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
230 {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
231 {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
232 {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
233 {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
234};
235
236/**
237 * struct d40_reg_val - simple lookup struct
238 *
239 * @reg: The register.
240 * @val: The value that belongs to the register in reg.
241 */
242struct d40_reg_val {
243 unsigned int reg;
244 unsigned int val;
245};
246
247static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
248 /* Clock every part of the DMA block from start */
249 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
250
251 /* Interrupts on all logical channels */
252 { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
253 { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
254 { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
255 { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
256 { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
257 { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
258 { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
259 { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
260 { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
261 { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
262 { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
263 { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
264};
265static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
266 /* Clock every part of the DMA block from start */
267 { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
268
269 /* Interrupts on all logical channels */
270 { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
271 { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
272 { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
273 { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
274 { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
275 { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
276 { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
277 { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
278 { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
279 { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
280 { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
281 { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
282 { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
283 { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
284 { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
285};
286
287/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200288 * struct d40_lli_pool - Structure for keeping LLIs in memory
289 *
290 * @base: Pointer to memory area when the pre_alloc_lli's are not large
291 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
292 * pre_alloc_lli is used.
Rabin Vincentb00f9382011-01-25 11:18:15 +0100293 * @dma_addr: DMA address, if mapped
Linus Walleij8d318a52010-03-30 15:33:42 +0200294 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
295 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
296 * one buffer to one buffer.
297 */
298struct d40_lli_pool {
299 void *base;
Linus Walleij508849a2010-06-20 21:26:07 +0000300 int size;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100301 dma_addr_t dma_addr;
Linus Walleij8d318a52010-03-30 15:33:42 +0200302 /* Space for dst and src, plus an extra for padding */
Linus Walleij508849a2010-06-20 21:26:07 +0000303 u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
Linus Walleij8d318a52010-03-30 15:33:42 +0200304};
305
306/**
307 * struct d40_desc - A descriptor is one DMA job.
308 *
309 * @lli_phy: LLI settings for physical channel. Both src and dst=
310 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
311 * lli_len equals one.
312 * @lli_log: Same as above but for logical channels.
313 * @lli_pool: The pool with two entries pre-allocated.
Per Friden941b77a2010-06-20 21:24:45 +0000314 * @lli_len: Number of llis of current descriptor.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300315 * @lli_current: Number of transferred llis.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000316 * @lcla_alloc: Number of LCLA entries allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200317 * @txd: DMA engine struct. Used for among other things for communication
318 * during a transfer.
319 * @node: List entry.
Linus Walleij8d318a52010-03-30 15:33:42 +0200320 * @is_in_client_list: true if the client owns this descriptor.
Narayanan G7fb3e752011-11-17 17:26:41 +0530321 * @cyclic: true if this is a cyclic job
Linus Walleij8d318a52010-03-30 15:33:42 +0200322 *
323 * This descriptor is used for both logical and physical transfers.
324 */
Linus Walleij8d318a52010-03-30 15:33:42 +0200325struct d40_desc {
326 /* LLI physical */
327 struct d40_phy_lli_bidir lli_phy;
328 /* LLI logical */
329 struct d40_log_lli_bidir lli_log;
330
331 struct d40_lli_pool lli_pool;
Per Friden941b77a2010-06-20 21:24:45 +0000332 int lli_len;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000333 int lli_current;
334 int lcla_alloc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200335
336 struct dma_async_tx_descriptor txd;
337 struct list_head node;
338
Linus Walleij8d318a52010-03-30 15:33:42 +0200339 bool is_in_client_list;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100340 bool cyclic;
Linus Walleij8d318a52010-03-30 15:33:42 +0200341};
342
343/**
344 * struct d40_lcla_pool - LCLA pool settings and data.
345 *
Linus Walleij508849a2010-06-20 21:26:07 +0000346 * @base: The virtual address of LCLA. 18 bit aligned.
347 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
348 * This pointer is only there for clean-up on error.
349 * @pages: The number of pages needed for all physical channels.
350 * Only used later for clean-up on error
Linus Walleij8d318a52010-03-30 15:33:42 +0200351 * @lock: Lock to protect the content in this struct.
Jonas Aaberg698e4732010-08-09 12:08:56 +0000352 * @alloc_map: big map over which LCLA entry is own by which job.
Linus Walleij8d318a52010-03-30 15:33:42 +0200353 */
354struct d40_lcla_pool {
355 void *base;
Rabin Vincent026cbc42011-01-25 11:18:14 +0100356 dma_addr_t dma_addr;
Linus Walleij508849a2010-06-20 21:26:07 +0000357 void *base_unaligned;
358 int pages;
Linus Walleij8d318a52010-03-30 15:33:42 +0200359 spinlock_t lock;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000360 struct d40_desc **alloc_map;
Linus Walleij8d318a52010-03-30 15:33:42 +0200361};
362
363/**
364 * struct d40_phy_res - struct for handling eventlines mapped to physical
365 * channels.
366 *
367 * @lock: A lock protection this entity.
Narayanan G7fb3e752011-11-17 17:26:41 +0530368 * @reserved: True if used by secure world or otherwise.
Linus Walleij8d318a52010-03-30 15:33:42 +0200369 * @num: The physical channel number of this entity.
370 * @allocated_src: Bit mapped to show which src event line's are mapped to
371 * this physical channel. Can also be free or physically allocated.
372 * @allocated_dst: Same as for src but is dst.
373 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
Jonas Aaberg767a9672010-08-09 12:08:34 +0000374 * event line number.
Fabio Baltieri74070482012-12-18 12:25:14 +0100375 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
Linus Walleij8d318a52010-03-30 15:33:42 +0200376 */
377struct d40_phy_res {
378 spinlock_t lock;
Narayanan G7fb3e752011-11-17 17:26:41 +0530379 bool reserved;
Linus Walleij8d318a52010-03-30 15:33:42 +0200380 int num;
381 u32 allocated_src;
382 u32 allocated_dst;
Fabio Baltieri74070482012-12-18 12:25:14 +0100383 bool use_soft_lli;
Linus Walleij8d318a52010-03-30 15:33:42 +0200384};
385
386struct d40_base;
387
388/**
389 * struct d40_chan - Struct that describes a channel.
390 *
391 * @lock: A spinlock to protect this struct.
392 * @log_num: The logical number, if any of this channel.
Linus Walleij8d318a52010-03-30 15:33:42 +0200393 * @pending_tx: The number of pending transfers. Used between interrupt handler
394 * and tasklet.
395 * @busy: Set to true when transfer is ongoing on this channel.
Jonas Aaberg2a614342010-06-20 21:25:24 +0000396 * @phy_chan: Pointer to physical channel which this instance runs on. If this
397 * point is NULL, then the channel is not allocated.
Linus Walleij8d318a52010-03-30 15:33:42 +0200398 * @chan: DMA engine handle.
399 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
400 * transfer and call client callback.
401 * @client: Cliented owned descriptor list.
Per Forlinda063d22011-08-29 13:33:32 +0200402 * @pending_queue: Submitted jobs, to be issued by issue_pending()
Linus Walleij8d318a52010-03-30 15:33:42 +0200403 * @active: Active descriptor.
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100404 * @done: Completed jobs
Linus Walleij8d318a52010-03-30 15:33:42 +0200405 * @queue: Queued jobs.
Per Forlin82babbb362011-08-29 13:33:35 +0200406 * @prepare_queue: Prepared jobs.
Linus Walleij8d318a52010-03-30 15:33:42 +0200407 * @dma_cfg: The client configuration of this dma channel.
Rabin Vincentce2ca122010-10-12 13:00:49 +0000408 * @configured: whether the dma_cfg configuration is valid
Linus Walleij8d318a52010-03-30 15:33:42 +0200409 * @base: Pointer to the device instance struct.
410 * @src_def_cfg: Default cfg register setting for src.
411 * @dst_def_cfg: Default cfg register setting for dst.
412 * @log_def: Default logical channel settings.
Linus Walleij8d318a52010-03-30 15:33:42 +0200413 * @lcpa: Pointer to dst and src lcpa settings.
om prakashae752bf2011-06-27 11:33:31 +0200414 * @runtime_addr: runtime configured address.
415 * @runtime_direction: runtime configured direction.
Linus Walleij8d318a52010-03-30 15:33:42 +0200416 *
417 * This struct can either "be" a logical or a physical channel.
418 */
419struct d40_chan {
420 spinlock_t lock;
421 int log_num;
Linus Walleij8d318a52010-03-30 15:33:42 +0200422 int pending_tx;
423 bool busy;
424 struct d40_phy_res *phy_chan;
425 struct dma_chan chan;
426 struct tasklet_struct tasklet;
427 struct list_head client;
Per Forlina8f30672011-06-26 23:29:52 +0200428 struct list_head pending_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200429 struct list_head active;
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100430 struct list_head done;
Linus Walleij8d318a52010-03-30 15:33:42 +0200431 struct list_head queue;
Per Forlin82babbb362011-08-29 13:33:35 +0200432 struct list_head prepare_queue;
Linus Walleij8d318a52010-03-30 15:33:42 +0200433 struct stedma40_chan_cfg dma_cfg;
Rabin Vincentce2ca122010-10-12 13:00:49 +0000434 bool configured;
Linus Walleij8d318a52010-03-30 15:33:42 +0200435 struct d40_base *base;
436 /* Default register configurations */
437 u32 src_def_cfg;
438 u32 dst_def_cfg;
439 struct d40_def_lcsp log_def;
Linus Walleij8d318a52010-03-30 15:33:42 +0200440 struct d40_log_lli_full *lcpa;
Linus Walleij95e14002010-08-04 13:37:45 +0200441 /* Runtime reconfiguration */
442 dma_addr_t runtime_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530443 enum dma_transfer_direction runtime_direction;
Linus Walleij8d318a52010-03-30 15:33:42 +0200444};
445
446/**
Tong Liu3cb645d2012-09-26 10:07:30 +0000447 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
448 * controller
449 *
450 * @backup: the pointer to the registers address array for backup
451 * @backup_size: the size of the registers address array for backup
452 * @realtime_en: the realtime enable register
453 * @realtime_clear: the realtime clear register
454 * @high_prio_en: the high priority enable register
455 * @high_prio_clear: the high priority clear register
456 * @interrupt_en: the interrupt enable register
457 * @interrupt_clear: the interrupt clear register
458 * @il: the pointer to struct d40_interrupt_lookup
459 * @il_size: the size of d40_interrupt_lookup array
460 * @init_reg: the pointer to the struct d40_reg_val
461 * @init_reg_size: the size of d40_reg_val array
462 */
463struct d40_gen_dmac {
464 u32 *backup;
465 u32 backup_size;
466 u32 realtime_en;
467 u32 realtime_clear;
468 u32 high_prio_en;
469 u32 high_prio_clear;
470 u32 interrupt_en;
471 u32 interrupt_clear;
472 struct d40_interrupt_lookup *il;
473 u32 il_size;
474 struct d40_reg_val *init_reg;
475 u32 init_reg_size;
476};
477
478/**
Linus Walleij8d318a52010-03-30 15:33:42 +0200479 * struct d40_base - The big global struct, one for each probe'd instance.
480 *
481 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
482 * @execmd_lock: Lock for execute command usage since several channels share
483 * the same physical register.
484 * @dev: The device structure.
485 * @virtbase: The virtual base address of the DMA's register.
Linus Walleijf4185592010-06-22 18:06:42 -0700486 * @rev: silicon revision detected.
Linus Walleij8d318a52010-03-30 15:33:42 +0200487 * @clk: Pointer to the DMA clock structure.
488 * @phy_start: Physical memory start of the DMA registers.
489 * @phy_size: Size of the DMA register map.
490 * @irq: The IRQ number.
491 * @num_phy_chans: The number of physical channels. Read from HW. This
492 * is the number of available channels for this driver, not counting "Secure
493 * mode" allocated physical channels.
494 * @num_log_chans: The number of logical channels. Calculated from
495 * num_phy_chans.
496 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
497 * @dma_slave: dma_device channels that can do only do slave transfers.
498 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
Narayanan G7fb3e752011-11-17 17:26:41 +0530499 * @phy_chans: Room for all possible physical channels in system.
Linus Walleij8d318a52010-03-30 15:33:42 +0200500 * @log_chans: Room for all possible logical channels in system.
501 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
502 * to log_chans entries.
503 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
504 * to phy_chans entries.
505 * @plat_data: Pointer to provided platform_data which is the driver
506 * configuration.
Narayanan G28c7a192011-11-22 13:56:55 +0530507 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
Linus Walleij8d318a52010-03-30 15:33:42 +0200508 * @phy_res: Vector containing all physical channels.
509 * @lcla_pool: lcla pool settings and data.
510 * @lcpa_base: The virtual mapped address of LCPA.
511 * @phy_lcpa: The physical address of the LCPA.
512 * @lcpa_size: The size of the LCPA area.
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000513 * @desc_slab: cache for descriptors.
Narayanan G7fb3e752011-11-17 17:26:41 +0530514 * @reg_val_backup: Here the values of some hardware registers are stored
515 * before the DMA is powered off. They are restored when the power is back on.
Tong Liu3cb645d2012-09-26 10:07:30 +0000516 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
517 * later
Narayanan G7fb3e752011-11-17 17:26:41 +0530518 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
519 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
520 * @initialized: true if the dma has been initialized
Tong Liu3cb645d2012-09-26 10:07:30 +0000521 * @gen_dmac: the struct for generic registers values to represent u8500/8540
522 * DMA controller
Linus Walleij8d318a52010-03-30 15:33:42 +0200523 */
524struct d40_base {
525 spinlock_t interrupt_lock;
526 spinlock_t execmd_lock;
527 struct device *dev;
528 void __iomem *virtbase;
Linus Walleijf4185592010-06-22 18:06:42 -0700529 u8 rev:4;
Linus Walleij8d318a52010-03-30 15:33:42 +0200530 struct clk *clk;
531 phys_addr_t phy_start;
532 resource_size_t phy_size;
533 int irq;
534 int num_phy_chans;
535 int num_log_chans;
Per Forlinb96710e2011-10-18 18:39:47 +0200536 struct device_dma_parameters dma_parms;
Linus Walleij8d318a52010-03-30 15:33:42 +0200537 struct dma_device dma_both;
538 struct dma_device dma_slave;
539 struct dma_device dma_memcpy;
540 struct d40_chan *phy_chans;
541 struct d40_chan *log_chans;
542 struct d40_chan **lookup_log_chans;
543 struct d40_chan **lookup_phy_chans;
544 struct stedma40_platform_data *plat_data;
Narayanan G28c7a192011-11-22 13:56:55 +0530545 struct regulator *lcpa_regulator;
Linus Walleij8d318a52010-03-30 15:33:42 +0200546 /* Physical half channels */
547 struct d40_phy_res *phy_res;
548 struct d40_lcla_pool lcla_pool;
549 void *lcpa_base;
550 dma_addr_t phy_lcpa;
551 resource_size_t lcpa_size;
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000552 struct kmem_cache *desc_slab;
Narayanan G7fb3e752011-11-17 17:26:41 +0530553 u32 reg_val_backup[BACKUP_REGS_SZ];
Tong Liu3cb645d2012-09-26 10:07:30 +0000554 u32 reg_val_backup_v4[MAX(BACKUP_REGS_SZ_V4A, BACKUP_REGS_SZ_V4B)];
Narayanan G7fb3e752011-11-17 17:26:41 +0530555 u32 *reg_val_backup_chan;
556 u16 gcc_pwr_off_mask;
557 bool initialized;
Tong Liu3cb645d2012-09-26 10:07:30 +0000558 struct d40_gen_dmac gen_dmac;
Linus Walleij8d318a52010-03-30 15:33:42 +0200559};
560
Rabin Vincent262d2912011-01-25 11:18:05 +0100561static struct device *chan2dev(struct d40_chan *d40c)
562{
563 return &d40c->chan.dev->device;
564}
565
Rabin Vincent724a8572011-01-25 11:18:08 +0100566static bool chan_is_physical(struct d40_chan *chan)
567{
568 return chan->log_num == D40_PHY_CHAN;
569}
570
571static bool chan_is_logical(struct d40_chan *chan)
572{
573 return !chan_is_physical(chan);
574}
575
Rabin Vincent8ca84682011-01-25 11:18:07 +0100576static void __iomem *chan_base(struct d40_chan *chan)
577{
578 return chan->base->virtbase + D40_DREG_PCBASE +
579 chan->phy_chan->num * D40_DREG_PCDELTA;
580}
581
Rabin Vincent6db5a8b2011-01-25 11:18:09 +0100582#define d40_err(dev, format, arg...) \
583 dev_err(dev, "[%s] " format, __func__, ## arg)
584
585#define chan_err(d40c, format, arg...) \
586 d40_err(chan2dev(d40c), format, ## arg)
587
Rabin Vincentb00f9382011-01-25 11:18:15 +0100588static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
Rabin Vincentdbd88782011-01-25 11:18:19 +0100589 int lli_len)
Linus Walleij8d318a52010-03-30 15:33:42 +0200590{
Rabin Vincentdbd88782011-01-25 11:18:19 +0100591 bool is_log = chan_is_logical(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +0200592 u32 align;
593 void *base;
594
595 if (is_log)
596 align = sizeof(struct d40_log_lli);
597 else
598 align = sizeof(struct d40_phy_lli);
599
600 if (lli_len == 1) {
601 base = d40d->lli_pool.pre_alloc_lli;
602 d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
603 d40d->lli_pool.base = NULL;
604 } else {
Rabin Vincent594ece42011-01-25 11:18:12 +0100605 d40d->lli_pool.size = lli_len * 2 * align;
Linus Walleij8d318a52010-03-30 15:33:42 +0200606
607 base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
608 d40d->lli_pool.base = base;
609
610 if (d40d->lli_pool.base == NULL)
611 return -ENOMEM;
612 }
613
614 if (is_log) {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100615 d40d->lli_log.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100616 d40d->lli_log.dst = d40d->lli_log.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100617
618 d40d->lli_pool.dma_addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +0200619 } else {
Rabin Vincentd924aba2011-01-25 11:18:16 +0100620 d40d->lli_phy.src = PTR_ALIGN(base, align);
Rabin Vincent594ece42011-01-25 11:18:12 +0100621 d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
Rabin Vincentb00f9382011-01-25 11:18:15 +0100622
623 d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
624 d40d->lli_phy.src,
625 d40d->lli_pool.size,
626 DMA_TO_DEVICE);
627
628 if (dma_mapping_error(d40c->base->dev,
629 d40d->lli_pool.dma_addr)) {
630 kfree(d40d->lli_pool.base);
631 d40d->lli_pool.base = NULL;
632 d40d->lli_pool.dma_addr = 0;
633 return -ENOMEM;
634 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200635 }
636
637 return 0;
638}
639
Rabin Vincentb00f9382011-01-25 11:18:15 +0100640static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
Linus Walleij8d318a52010-03-30 15:33:42 +0200641{
Rabin Vincentb00f9382011-01-25 11:18:15 +0100642 if (d40d->lli_pool.dma_addr)
643 dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
644 d40d->lli_pool.size, DMA_TO_DEVICE);
645
Linus Walleij8d318a52010-03-30 15:33:42 +0200646 kfree(d40d->lli_pool.base);
647 d40d->lli_pool.base = NULL;
648 d40d->lli_pool.size = 0;
649 d40d->lli_log.src = NULL;
650 d40d->lli_log.dst = NULL;
651 d40d->lli_phy.src = NULL;
652 d40d->lli_phy.dst = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200653}
654
Jonas Aaberg698e4732010-08-09 12:08:56 +0000655static int d40_lcla_alloc_one(struct d40_chan *d40c,
656 struct d40_desc *d40d)
657{
658 unsigned long flags;
659 int i;
660 int ret = -EINVAL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000661
662 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
663
Jonas Aaberg698e4732010-08-09 12:08:56 +0000664 /*
665 * Allocate both src and dst at the same time, therefore the half
666 * start on 1 since 0 can't be used since zero is used as end marker.
667 */
668 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100669 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
670
671 if (!d40c->base->lcla_pool.alloc_map[idx]) {
672 d40c->base->lcla_pool.alloc_map[idx] = d40d;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000673 d40d->lcla_alloc++;
674 ret = i;
675 break;
676 }
677 }
678
679 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
680
681 return ret;
682}
683
684static int d40_lcla_free_all(struct d40_chan *d40c,
685 struct d40_desc *d40d)
686{
687 unsigned long flags;
688 int i;
689 int ret = -EINVAL;
690
Rabin Vincent724a8572011-01-25 11:18:08 +0100691 if (chan_is_physical(d40c))
Jonas Aaberg698e4732010-08-09 12:08:56 +0000692 return 0;
693
694 spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
695
696 for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
Fabio Baltieri7ce529e2012-12-18 16:59:09 +0100697 int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
698
699 if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
700 d40c->base->lcla_pool.alloc_map[idx] = NULL;
Jonas Aaberg698e4732010-08-09 12:08:56 +0000701 d40d->lcla_alloc--;
702 if (d40d->lcla_alloc == 0) {
703 ret = 0;
704 break;
705 }
706 }
707 }
708
709 spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
710
711 return ret;
712
713}
714
Linus Walleij8d318a52010-03-30 15:33:42 +0200715static void d40_desc_remove(struct d40_desc *d40d)
716{
717 list_del(&d40d->node);
718}
719
720static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
721{
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000722 struct d40_desc *desc = NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +0200723
724 if (!list_empty(&d40c->client)) {
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000725 struct d40_desc *d;
726 struct d40_desc *_d;
727
Narayanan G7fb3e752011-11-17 17:26:41 +0530728 list_for_each_entry_safe(d, _d, &d40c->client, node) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200729 if (async_tx_test_ack(&d->txd)) {
Linus Walleij8d318a52010-03-30 15:33:42 +0200730 d40_desc_remove(d);
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000731 desc = d;
732 memset(desc, 0, sizeof(*desc));
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000733 break;
Linus Walleij8d318a52010-03-30 15:33:42 +0200734 }
Narayanan G7fb3e752011-11-17 17:26:41 +0530735 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200736 }
Rabin Vincenta2c15fa2010-10-06 08:20:37 +0000737
738 if (!desc)
739 desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
740
741 if (desc)
742 INIT_LIST_HEAD(&desc->node);
743
744 return desc;
Linus Walleij8d318a52010-03-30 15:33:42 +0200745}
746
747static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
748{
Jonas Aaberg698e4732010-08-09 12:08:56 +0000749
Rabin Vincentb00f9382011-01-25 11:18:15 +0100750 d40_pool_lli_free(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000751 d40_lcla_free_all(d40c, d40d);
Jonas Aabergc675b1b2010-06-20 21:25:08 +0000752 kmem_cache_free(d40c->base->desc_slab, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +0200753}
754
755static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
756{
757 list_add_tail(&desc->node, &d40c->active);
758}
759
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100760static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
761{
762 struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
763 struct d40_phy_lli *lli_src = desc->lli_phy.src;
764 void __iomem *base = chan_base(chan);
765
766 writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
767 writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
768 writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
769 writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
770
771 writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
772 writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
773 writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
774 writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
775}
776
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100777static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
778{
779 list_add_tail(&desc->node, &d40c->done);
780}
781
Rabin Vincente65889c2011-01-25 11:18:31 +0100782static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
783{
784 struct d40_lcla_pool *pool = &chan->base->lcla_pool;
785 struct d40_log_lli_bidir *lli = &desc->lli_log;
786 int lli_current = desc->lli_current;
787 int lli_len = desc->lli_len;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100788 bool cyclic = desc->cyclic;
Rabin Vincente65889c2011-01-25 11:18:31 +0100789 int curr_lcla = -EINVAL;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100790 int first_lcla = 0;
Narayanan G28c7a192011-11-22 13:56:55 +0530791 bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100792 bool linkback;
Rabin Vincente65889c2011-01-25 11:18:31 +0100793
Rabin Vincent0c842b52011-01-25 11:18:35 +0100794 /*
795 * We may have partially running cyclic transfers, in case we did't get
796 * enough LCLA entries.
797 */
798 linkback = cyclic && lli_current == 0;
799
800 /*
801 * For linkback, we need one LCLA even with only one link, because we
802 * can't link back to the one in LCPA space
803 */
804 if (linkback || (lli_len - lli_current > 1)) {
Fabio Baltieri74070482012-12-18 12:25:14 +0100805 /*
806 * If the channel is expected to use only soft_lli don't
807 * allocate a lcla. This is to avoid a HW issue that exists
808 * in some controller during a peripheral to memory transfer
809 * that uses linked lists.
810 */
811 if (!(chan->phy_chan->use_soft_lli &&
812 chan->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM))
813 curr_lcla = d40_lcla_alloc_one(chan, desc);
814
Rabin Vincent0c842b52011-01-25 11:18:35 +0100815 first_lcla = curr_lcla;
816 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100817
Rabin Vincent0c842b52011-01-25 11:18:35 +0100818 /*
819 * For linkback, we normally load the LCPA in the loop since we need to
820 * link it to the second LCLA and not the first. However, if we
821 * couldn't even get a first LCLA, then we have to run in LCPA and
822 * reload manually.
823 */
824 if (!linkback || curr_lcla == -EINVAL) {
825 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100826
Rabin Vincent0c842b52011-01-25 11:18:35 +0100827 if (curr_lcla == -EINVAL)
828 flags |= LLI_TERM_INT;
829
830 d40_log_lli_lcpa_write(chan->lcpa,
831 &lli->dst[lli_current],
832 &lli->src[lli_current],
833 curr_lcla,
834 flags);
835 lli_current++;
836 }
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100837
838 if (curr_lcla < 0)
839 goto out;
840
Rabin Vincente65889c2011-01-25 11:18:31 +0100841 for (; lli_current < lli_len; lli_current++) {
842 unsigned int lcla_offset = chan->phy_chan->num * 1024 +
843 8 * curr_lcla * 2;
844 struct d40_log_lli *lcla = pool->base + lcla_offset;
Rabin Vincent0c842b52011-01-25 11:18:35 +0100845 unsigned int flags = 0;
Rabin Vincente65889c2011-01-25 11:18:31 +0100846 int next_lcla;
847
848 if (lli_current + 1 < lli_len)
849 next_lcla = d40_lcla_alloc_one(chan, desc);
850 else
Rabin Vincent0c842b52011-01-25 11:18:35 +0100851 next_lcla = linkback ? first_lcla : -EINVAL;
Rabin Vincente65889c2011-01-25 11:18:31 +0100852
Rabin Vincent0c842b52011-01-25 11:18:35 +0100853 if (cyclic || next_lcla == -EINVAL)
854 flags |= LLI_TERM_INT;
855
856 if (linkback && curr_lcla == first_lcla) {
857 /* First link goes in both LCPA and LCLA */
858 d40_log_lli_lcpa_write(chan->lcpa,
859 &lli->dst[lli_current],
860 &lli->src[lli_current],
861 next_lcla, flags);
862 }
863
864 /*
865 * One unused LCLA in the cyclic case if the very first
866 * next_lcla fails...
867 */
Rabin Vincente65889c2011-01-25 11:18:31 +0100868 d40_log_lli_lcla_write(lcla,
869 &lli->dst[lli_current],
870 &lli->src[lli_current],
Rabin Vincent0c842b52011-01-25 11:18:35 +0100871 next_lcla, flags);
Rabin Vincente65889c2011-01-25 11:18:31 +0100872
Narayanan G28c7a192011-11-22 13:56:55 +0530873 /*
874 * Cache maintenance is not needed if lcla is
875 * mapped in esram
876 */
877 if (!use_esram_lcla) {
878 dma_sync_single_range_for_device(chan->base->dev,
879 pool->dma_addr, lcla_offset,
880 2 * sizeof(struct d40_log_lli),
881 DMA_TO_DEVICE);
882 }
Rabin Vincente65889c2011-01-25 11:18:31 +0100883 curr_lcla = next_lcla;
884
Rabin Vincent0c842b52011-01-25 11:18:35 +0100885 if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
Rabin Vincente65889c2011-01-25 11:18:31 +0100886 lli_current++;
887 break;
888 }
889 }
890
Rabin Vincent6045f0b2011-01-25 11:18:32 +0100891out:
Rabin Vincente65889c2011-01-25 11:18:31 +0100892 desc->lli_current = lli_current;
893}
894
Jonas Aaberg698e4732010-08-09 12:08:56 +0000895static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
896{
Rabin Vincent724a8572011-01-25 11:18:08 +0100897 if (chan_is_physical(d40c)) {
Rabin Vincent1c4b0922011-01-25 11:18:24 +0100898 d40_phy_lli_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000899 d40d->lli_current = d40d->lli_len;
Rabin Vincente65889c2011-01-25 11:18:31 +0100900 } else
901 d40_log_lli_to_lcxa(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +0000902}
903
Linus Walleij8d318a52010-03-30 15:33:42 +0200904static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
905{
906 struct d40_desc *d;
907
908 if (list_empty(&d40c->active))
909 return NULL;
910
911 d = list_first_entry(&d40c->active,
912 struct d40_desc,
913 node);
914 return d;
915}
916
Per Forlin74043682011-08-29 13:33:34 +0200917/* remove desc from current queue and add it to the pending_queue */
Linus Walleij8d318a52010-03-30 15:33:42 +0200918static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
919{
Per Forlin74043682011-08-29 13:33:34 +0200920 d40_desc_remove(desc);
921 desc->is_in_client_list = false;
Per Forlina8f30672011-06-26 23:29:52 +0200922 list_add_tail(&desc->node, &d40c->pending_queue);
923}
924
925static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
926{
927 struct d40_desc *d;
928
929 if (list_empty(&d40c->pending_queue))
930 return NULL;
931
932 d = list_first_entry(&d40c->pending_queue,
933 struct d40_desc,
934 node);
935 return d;
Linus Walleij8d318a52010-03-30 15:33:42 +0200936}
937
938static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
939{
940 struct d40_desc *d;
941
942 if (list_empty(&d40c->queue))
943 return NULL;
944
945 d = list_first_entry(&d40c->queue,
946 struct d40_desc,
947 node);
948 return d;
949}
950
Fabio Baltieri4226dd82012-12-13 13:46:16 +0100951static struct d40_desc *d40_first_done(struct d40_chan *d40c)
952{
953 if (list_empty(&d40c->done))
954 return NULL;
955
956 return list_first_entry(&d40c->done, struct d40_desc, node);
957}
958
Per Forlind49278e2010-12-20 18:31:38 +0100959static int d40_psize_2_burst_size(bool is_log, int psize)
960{
961 if (is_log) {
962 if (psize == STEDMA40_PSIZE_LOG_1)
963 return 1;
964 } else {
965 if (psize == STEDMA40_PSIZE_PHY_1)
966 return 1;
967 }
Linus Walleij8d318a52010-03-30 15:33:42 +0200968
Per Forlind49278e2010-12-20 18:31:38 +0100969 return 2 << psize;
970}
971
972/*
973 * The dma only supports transmitting packages up to
974 * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
975 * dma elements required to send the entire sg list
976 */
977static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
978{
979 int dmalen;
980 u32 max_w = max(data_width1, data_width2);
981 u32 min_w = min(data_width1, data_width2);
982 u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
983
984 if (seg_max > STEDMA40_MAX_SEG_SIZE)
985 seg_max -= (1 << max_w);
986
987 if (!IS_ALIGNED(size, 1 << max_w))
988 return -EINVAL;
989
990 if (size <= seg_max)
991 dmalen = 1;
992 else {
993 dmalen = size / seg_max;
994 if (dmalen * seg_max < size)
995 dmalen++;
996 }
997 return dmalen;
998}
999
1000static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1001 u32 data_width1, u32 data_width2)
1002{
1003 struct scatterlist *sg;
1004 int i;
1005 int len = 0;
1006 int ret;
1007
1008 for_each_sg(sgl, sg, sg_len, i) {
1009 ret = d40_size_2_dmalen(sg_dma_len(sg),
1010 data_width1, data_width2);
1011 if (ret < 0)
1012 return ret;
1013 len += ret;
1014 }
1015 return len;
1016}
1017
Narayanan G7fb3e752011-11-17 17:26:41 +05301018
1019#ifdef CONFIG_PM
1020static void dma40_backup(void __iomem *baseaddr, u32 *backup,
1021 u32 *regaddr, int num, bool save)
1022{
1023 int i;
1024
1025 for (i = 0; i < num; i++) {
1026 void __iomem *addr = baseaddr + regaddr[i];
1027
1028 if (save)
1029 backup[i] = readl_relaxed(addr);
1030 else
1031 writel_relaxed(backup[i], addr);
1032 }
1033}
1034
1035static void d40_save_restore_registers(struct d40_base *base, bool save)
1036{
1037 int i;
1038
1039 /* Save/Restore channel specific registers */
1040 for (i = 0; i < base->num_phy_chans; i++) {
1041 void __iomem *addr;
1042 int idx;
1043
1044 if (base->phy_res[i].reserved)
1045 continue;
1046
1047 addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
1048 idx = i * ARRAY_SIZE(d40_backup_regs_chan);
1049
1050 dma40_backup(addr, &base->reg_val_backup_chan[idx],
1051 d40_backup_regs_chan,
1052 ARRAY_SIZE(d40_backup_regs_chan),
1053 save);
1054 }
1055
1056 /* Save/Restore global registers */
1057 dma40_backup(base->virtbase, base->reg_val_backup,
1058 d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
1059 save);
1060
1061 /* Save/Restore registers only existing on dma40 v3 and later */
Tong Liu3cb645d2012-09-26 10:07:30 +00001062 if (base->gen_dmac.backup)
1063 dma40_backup(base->virtbase, base->reg_val_backup_v4,
1064 base->gen_dmac.backup,
1065 base->gen_dmac.backup_size,
1066 save);
Narayanan G7fb3e752011-11-17 17:26:41 +05301067}
1068#else
1069static void d40_save_restore_registers(struct d40_base *base, bool save)
1070{
1071}
1072#endif
Linus Walleij8d318a52010-03-30 15:33:42 +02001073
Narayanan G1bdae6f2012-02-09 12:41:37 +05301074static int __d40_execute_command_phy(struct d40_chan *d40c,
1075 enum d40_command command)
Linus Walleij8d318a52010-03-30 15:33:42 +02001076{
Jonas Aaberg767a9672010-08-09 12:08:34 +00001077 u32 status;
1078 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001079 void __iomem *active_reg;
1080 int ret = 0;
1081 unsigned long flags;
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001082 u32 wmask;
Linus Walleij8d318a52010-03-30 15:33:42 +02001083
Narayanan G1bdae6f2012-02-09 12:41:37 +05301084 if (command == D40_DMA_STOP) {
1085 ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
1086 if (ret)
1087 return ret;
1088 }
1089
Linus Walleij8d318a52010-03-30 15:33:42 +02001090 spin_lock_irqsave(&d40c->base->execmd_lock, flags);
1091
1092 if (d40c->phy_chan->num % 2 == 0)
1093 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1094 else
1095 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1096
1097 if (command == D40_DMA_SUSPEND_REQ) {
1098 status = (readl(active_reg) &
1099 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1100 D40_CHAN_POS(d40c->phy_chan->num);
1101
1102 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1103 goto done;
1104 }
1105
Jonas Aaberg1d392a72010-06-20 21:26:01 +00001106 wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
1107 writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
1108 active_reg);
Linus Walleij8d318a52010-03-30 15:33:42 +02001109
1110 if (command == D40_DMA_SUSPEND_REQ) {
1111
1112 for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
1113 status = (readl(active_reg) &
1114 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1115 D40_CHAN_POS(d40c->phy_chan->num);
1116
1117 cpu_relax();
1118 /*
1119 * Reduce the number of bus accesses while
1120 * waiting for the DMA to suspend.
1121 */
1122 udelay(3);
1123
1124 if (status == D40_DMA_STOP ||
1125 status == D40_DMA_SUSPENDED)
1126 break;
1127 }
1128
1129 if (i == D40_SUSPEND_MAX_IT) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001130 chan_err(d40c,
1131 "unable to suspend the chl %d (log: %d) status %x\n",
1132 d40c->phy_chan->num, d40c->log_num,
Linus Walleij8d318a52010-03-30 15:33:42 +02001133 status);
1134 dump_stack();
1135 ret = -EBUSY;
1136 }
1137
1138 }
1139done:
1140 spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
1141 return ret;
1142}
1143
1144static void d40_term_all(struct d40_chan *d40c)
1145{
1146 struct d40_desc *d40d;
Per Forlin74043682011-08-29 13:33:34 +02001147 struct d40_desc *_d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001148
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001149 /* Release completed descriptors */
1150 while ((d40d = d40_first_done(d40c))) {
1151 d40_desc_remove(d40d);
1152 d40_desc_free(d40c, d40d);
1153 }
1154
Linus Walleij8d318a52010-03-30 15:33:42 +02001155 /* Release active descriptors */
1156 while ((d40d = d40_first_active_get(d40c))) {
1157 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001158 d40_desc_free(d40c, d40d);
1159 }
1160
1161 /* Release queued descriptors waiting for transfer */
1162 while ((d40d = d40_first_queued(d40c))) {
1163 d40_desc_remove(d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001164 d40_desc_free(d40c, d40d);
1165 }
1166
Per Forlina8f30672011-06-26 23:29:52 +02001167 /* Release pending descriptors */
1168 while ((d40d = d40_first_pending(d40c))) {
1169 d40_desc_remove(d40d);
1170 d40_desc_free(d40c, d40d);
1171 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001172
Per Forlin74043682011-08-29 13:33:34 +02001173 /* Release client owned descriptors */
1174 if (!list_empty(&d40c->client))
1175 list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
1176 d40_desc_remove(d40d);
1177 d40_desc_free(d40c, d40d);
1178 }
1179
Per Forlin82babbb362011-08-29 13:33:35 +02001180 /* Release descriptors in prepare queue */
1181 if (!list_empty(&d40c->prepare_queue))
1182 list_for_each_entry_safe(d40d, _d,
1183 &d40c->prepare_queue, node) {
1184 d40_desc_remove(d40d);
1185 d40_desc_free(d40c, d40d);
1186 }
Per Forlin74043682011-08-29 13:33:34 +02001187
Linus Walleij8d318a52010-03-30 15:33:42 +02001188 d40c->pending_tx = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02001189}
1190
Narayanan G1bdae6f2012-02-09 12:41:37 +05301191static void __d40_config_set_event(struct d40_chan *d40c,
1192 enum d40_events event_type, u32 event,
1193 int reg)
Rabin Vincent262d2912011-01-25 11:18:05 +01001194{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001195 void __iomem *addr = chan_base(d40c) + reg;
Rabin Vincent262d2912011-01-25 11:18:05 +01001196 int tries;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301197 u32 status;
Rabin Vincent262d2912011-01-25 11:18:05 +01001198
Narayanan G1bdae6f2012-02-09 12:41:37 +05301199 switch (event_type) {
1200
1201 case D40_DEACTIVATE_EVENTLINE:
1202
Rabin Vincent262d2912011-01-25 11:18:05 +01001203 writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1204 | ~D40_EVENTLINE_MASK(event), addr);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301205 break;
Rabin Vincent262d2912011-01-25 11:18:05 +01001206
Narayanan G1bdae6f2012-02-09 12:41:37 +05301207 case D40_SUSPEND_REQ_EVENTLINE:
1208 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1209 D40_EVENTLINE_POS(event);
1210
1211 if (status == D40_DEACTIVATE_EVENTLINE ||
1212 status == D40_SUSPEND_REQ_EVENTLINE)
1213 break;
1214
1215 writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
1216 | ~D40_EVENTLINE_MASK(event), addr);
1217
1218 for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
1219
1220 status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
1221 D40_EVENTLINE_POS(event);
1222
1223 cpu_relax();
1224 /*
1225 * Reduce the number of bus accesses while
1226 * waiting for the DMA to suspend.
1227 */
1228 udelay(3);
1229
1230 if (status == D40_DEACTIVATE_EVENTLINE)
1231 break;
1232 }
1233
1234 if (tries == D40_SUSPEND_MAX_IT) {
1235 chan_err(d40c,
1236 "unable to stop the event_line chl %d (log: %d)"
1237 "status %x\n", d40c->phy_chan->num,
1238 d40c->log_num, status);
1239 }
1240 break;
1241
1242 case D40_ACTIVATE_EVENTLINE:
Rabin Vincent262d2912011-01-25 11:18:05 +01001243 /*
1244 * The hardware sometimes doesn't register the enable when src and dst
1245 * event lines are active on the same logical channel. Retry to ensure
1246 * it does. Usually only one retry is sufficient.
1247 */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301248 tries = 100;
1249 while (--tries) {
1250 writel((D40_ACTIVATE_EVENTLINE <<
1251 D40_EVENTLINE_POS(event)) |
1252 ~D40_EVENTLINE_MASK(event), addr);
Rabin Vincent262d2912011-01-25 11:18:05 +01001253
Narayanan G1bdae6f2012-02-09 12:41:37 +05301254 if (readl(addr) & D40_EVENTLINE_MASK(event))
1255 break;
1256 }
1257
1258 if (tries != 99)
1259 dev_dbg(chan2dev(d40c),
1260 "[%s] workaround enable S%cLNK (%d tries)\n",
1261 __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1262 100 - tries);
1263
1264 WARN_ON(!tries);
1265 break;
1266
1267 case D40_ROUND_EVENTLINE:
1268 BUG();
1269 break;
1270
Rabin Vincent262d2912011-01-25 11:18:05 +01001271 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001272}
1273
Narayanan G1bdae6f2012-02-09 12:41:37 +05301274static void d40_config_set_event(struct d40_chan *d40c,
1275 enum d40_events event_type)
Linus Walleij8d318a52010-03-30 15:33:42 +02001276{
Linus Walleij8d318a52010-03-30 15:33:42 +02001277 /* Enable event line connected to device (or memcpy) */
1278 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
1279 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
1280 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
1281
Narayanan G1bdae6f2012-02-09 12:41:37 +05301282 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001283 D40_CHAN_REG_SSLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001284 }
Rabin Vincent262d2912011-01-25 11:18:05 +01001285
Linus Walleij8d318a52010-03-30 15:33:42 +02001286 if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
1287 u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
1288
Narayanan G1bdae6f2012-02-09 12:41:37 +05301289 __d40_config_set_event(d40c, event_type, event,
Rabin Vincent262d2912011-01-25 11:18:05 +01001290 D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001291 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001292}
1293
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001294static u32 d40_chan_has_events(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001295{
Rabin Vincent8ca84682011-01-25 11:18:07 +01001296 void __iomem *chanbase = chan_base(d40c);
Jonas Aabergbe8cb7d2010-08-09 12:07:44 +00001297 u32 val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001298
Rabin Vincent8ca84682011-01-25 11:18:07 +01001299 val = readl(chanbase + D40_CHAN_REG_SSLNK);
1300 val |= readl(chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001301
Jonas Aaberga5ebca42010-05-18 00:41:09 +02001302 return val;
Linus Walleij8d318a52010-03-30 15:33:42 +02001303}
1304
Narayanan G1bdae6f2012-02-09 12:41:37 +05301305static int
1306__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
1307{
1308 unsigned long flags;
1309 int ret = 0;
1310 u32 active_status;
1311 void __iomem *active_reg;
1312
1313 if (d40c->phy_chan->num % 2 == 0)
1314 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1315 else
1316 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1317
1318
1319 spin_lock_irqsave(&d40c->phy_chan->lock, flags);
1320
1321 switch (command) {
1322 case D40_DMA_STOP:
1323 case D40_DMA_SUSPEND_REQ:
1324
1325 active_status = (readl(active_reg) &
1326 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1327 D40_CHAN_POS(d40c->phy_chan->num);
1328
1329 if (active_status == D40_DMA_RUN)
1330 d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
1331 else
1332 d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
1333
1334 if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
1335 ret = __d40_execute_command_phy(d40c, command);
1336
1337 break;
1338
1339 case D40_DMA_RUN:
1340
1341 d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
1342 ret = __d40_execute_command_phy(d40c, command);
1343 break;
1344
1345 case D40_DMA_SUSPENDED:
1346 BUG();
1347 break;
1348 }
1349
1350 spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
1351 return ret;
1352}
1353
1354static int d40_channel_execute_command(struct d40_chan *d40c,
1355 enum d40_command command)
1356{
1357 if (chan_is_logical(d40c))
1358 return __d40_execute_command_log(d40c, command);
1359 else
1360 return __d40_execute_command_phy(d40c, command);
1361}
1362
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001363static u32 d40_get_prmo(struct d40_chan *d40c)
1364{
1365 static const unsigned int phy_map[] = {
1366 [STEDMA40_PCHAN_BASIC_MODE]
1367 = D40_DREG_PRMO_PCHAN_BASIC,
1368 [STEDMA40_PCHAN_MODULO_MODE]
1369 = D40_DREG_PRMO_PCHAN_MODULO,
1370 [STEDMA40_PCHAN_DOUBLE_DST_MODE]
1371 = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
1372 };
1373 static const unsigned int log_map[] = {
1374 [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
1375 = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
1376 [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
1377 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
1378 [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
1379 = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
1380 };
1381
Rabin Vincent724a8572011-01-25 11:18:08 +01001382 if (chan_is_physical(d40c))
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001383 return phy_map[d40c->dma_cfg.mode_opt];
1384 else
1385 return log_map[d40c->dma_cfg.mode_opt];
1386}
1387
Jonas Aabergb55912c2010-08-09 12:08:02 +00001388static void d40_config_write(struct d40_chan *d40c)
Linus Walleij8d318a52010-03-30 15:33:42 +02001389{
1390 u32 addr_base;
1391 u32 var;
Linus Walleij8d318a52010-03-30 15:33:42 +02001392
1393 /* Odd addresses are even addresses + 4 */
1394 addr_base = (d40c->phy_chan->num % 2) * 4;
1395 /* Setup channel mode to logical or physical */
Rabin Vincent724a8572011-01-25 11:18:08 +01001396 var = ((u32)(chan_is_logical(d40c)) + 1) <<
Linus Walleij8d318a52010-03-30 15:33:42 +02001397 D40_CHAN_POS(d40c->phy_chan->num);
1398 writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
1399
1400 /* Setup operational mode option register */
Rabin Vincent20a5b6d2010-10-12 13:00:52 +00001401 var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
Linus Walleij8d318a52010-03-30 15:33:42 +02001402
1403 writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
1404
Rabin Vincent724a8572011-01-25 11:18:08 +01001405 if (chan_is_logical(d40c)) {
Rabin Vincent8ca84682011-01-25 11:18:07 +01001406 int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
1407 & D40_SREG_ELEM_LOG_LIDX_MASK;
1408 void __iomem *chanbase = chan_base(d40c);
1409
Linus Walleij8d318a52010-03-30 15:33:42 +02001410 /* Set default config for CFG reg */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001411 writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
1412 writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
Linus Walleij8d318a52010-03-30 15:33:42 +02001413
Jonas Aabergb55912c2010-08-09 12:08:02 +00001414 /* Set LIDX for lcla */
Rabin Vincent8ca84682011-01-25 11:18:07 +01001415 writel(lidx, chanbase + D40_CHAN_REG_SSELT);
1416 writel(lidx, chanbase + D40_CHAN_REG_SDELT);
Rabin Vincente9f3a492011-12-28 11:27:40 +05301417
1418 /* Clear LNK which will be used by d40_chan_has_events() */
1419 writel(0, chanbase + D40_CHAN_REG_SSLNK);
1420 writel(0, chanbase + D40_CHAN_REG_SDLNK);
Linus Walleij8d318a52010-03-30 15:33:42 +02001421 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001422}
1423
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001424static u32 d40_residue(struct d40_chan *d40c)
1425{
1426 u32 num_elt;
1427
Rabin Vincent724a8572011-01-25 11:18:08 +01001428 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001429 num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1430 >> D40_MEM_LCSP2_ECNT_POS;
Rabin Vincent8ca84682011-01-25 11:18:07 +01001431 else {
1432 u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
1433 num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
1434 >> D40_SREG_ELEM_PHY_ECNT_POS;
1435 }
1436
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001437 return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1438}
1439
1440static bool d40_tx_is_linked(struct d40_chan *d40c)
1441{
1442 bool is_link;
1443
Rabin Vincent724a8572011-01-25 11:18:08 +01001444 if (chan_is_logical(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001445 is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1446 else
Rabin Vincent8ca84682011-01-25 11:18:07 +01001447 is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
1448 & D40_SREG_LNK_PHYS_LNK_MASK;
1449
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001450 return is_link;
1451}
1452
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001453static int d40_pause(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001454{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001455 int res = 0;
1456 unsigned long flags;
1457
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001458 if (!d40c->busy)
1459 return 0;
1460
Narayanan G7fb3e752011-11-17 17:26:41 +05301461 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001462 spin_lock_irqsave(&d40c->lock, flags);
1463
1464 res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
Narayanan G1bdae6f2012-02-09 12:41:37 +05301465
Narayanan G7fb3e752011-11-17 17:26:41 +05301466 pm_runtime_mark_last_busy(d40c->base->dev);
1467 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001468 spin_unlock_irqrestore(&d40c->lock, flags);
1469 return res;
1470}
1471
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01001472static int d40_resume(struct d40_chan *d40c)
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001473{
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001474 int res = 0;
1475 unsigned long flags;
1476
Jonas Aaberg3ac012a2010-08-09 12:09:12 +00001477 if (!d40c->busy)
1478 return 0;
1479
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001480 spin_lock_irqsave(&d40c->lock, flags);
Narayanan G7fb3e752011-11-17 17:26:41 +05301481 pm_runtime_get_sync(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001482
1483 /* If bytes left to transfer or linked tx resume job */
Narayanan G1bdae6f2012-02-09 12:41:37 +05301484 if (d40_residue(d40c) || d40_tx_is_linked(d40c))
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001485 res = d40_channel_execute_command(d40c, D40_DMA_RUN);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001486
Narayanan G7fb3e752011-11-17 17:26:41 +05301487 pm_runtime_mark_last_busy(d40c->base->dev);
1488 pm_runtime_put_autosuspend(d40c->base->dev);
Jonas Aabergaa182ae2010-08-09 12:08:26 +00001489 spin_unlock_irqrestore(&d40c->lock, flags);
1490 return res;
1491}
1492
Linus Walleij8d318a52010-03-30 15:33:42 +02001493static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
1494{
1495 struct d40_chan *d40c = container_of(tx->chan,
1496 struct d40_chan,
1497 chan);
1498 struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
1499 unsigned long flags;
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001500 dma_cookie_t cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001501
1502 spin_lock_irqsave(&d40c->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001503 cookie = dma_cookie_assign(tx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001504 d40_desc_queue(d40c, d40d);
Linus Walleij8d318a52010-03-30 15:33:42 +02001505 spin_unlock_irqrestore(&d40c->lock, flags);
1506
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001507 return cookie;
Linus Walleij8d318a52010-03-30 15:33:42 +02001508}
1509
1510static int d40_start(struct d40_chan *d40c)
1511{
Jonas Aaberg0c322692010-06-20 21:25:46 +00001512 return d40_channel_execute_command(d40c, D40_DMA_RUN);
Linus Walleij8d318a52010-03-30 15:33:42 +02001513}
1514
1515static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
1516{
1517 struct d40_desc *d40d;
1518 int err;
1519
1520 /* Start queued jobs, if any */
1521 d40d = d40_first_queued(d40c);
1522
1523 if (d40d != NULL) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05301524 if (!d40c->busy) {
Narayanan G7fb3e752011-11-17 17:26:41 +05301525 d40c->busy = true;
Narayanan G1bdae6f2012-02-09 12:41:37 +05301526 pm_runtime_get_sync(d40c->base->dev);
1527 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001528
1529 /* Remove from queue */
1530 d40_desc_remove(d40d);
1531
1532 /* Add to active queue */
1533 d40_desc_submit(d40c, d40d);
1534
Rabin Vincent7d83a852011-01-25 11:18:06 +01001535 /* Initiate DMA job */
1536 d40_desc_load(d40c, d40d);
Jonas Aaberg698e4732010-08-09 12:08:56 +00001537
Rabin Vincent7d83a852011-01-25 11:18:06 +01001538 /* Start dma job */
1539 err = d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001540
Rabin Vincent7d83a852011-01-25 11:18:06 +01001541 if (err)
1542 return NULL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001543 }
1544
1545 return d40d;
1546}
1547
1548/* called from interrupt context */
1549static void dma_tc_handle(struct d40_chan *d40c)
1550{
1551 struct d40_desc *d40d;
1552
Linus Walleij8d318a52010-03-30 15:33:42 +02001553 /* Get first active entry from list */
1554 d40d = d40_first_active_get(d40c);
1555
1556 if (d40d == NULL)
1557 return;
1558
Rabin Vincent0c842b52011-01-25 11:18:35 +01001559 if (d40d->cyclic) {
1560 /*
1561 * If this was a paritially loaded list, we need to reloaded
1562 * it, and only when the list is completed. We need to check
1563 * for done because the interrupt will hit for every link, and
1564 * not just the last one.
1565 */
1566 if (d40d->lli_current < d40d->lli_len
1567 && !d40_tx_is_linked(d40c)
1568 && !d40_residue(d40c)) {
1569 d40_lcla_free_all(d40c, d40d);
1570 d40_desc_load(d40c, d40d);
1571 (void) d40_start(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02001572
Rabin Vincent0c842b52011-01-25 11:18:35 +01001573 if (d40d->lli_current == d40d->lli_len)
1574 d40d->lli_current = 0;
1575 }
1576 } else {
1577 d40_lcla_free_all(d40c, d40d);
1578
1579 if (d40d->lli_current < d40d->lli_len) {
1580 d40_desc_load(d40c, d40d);
1581 /* Start dma job */
1582 (void) d40_start(d40c);
1583 return;
1584 }
1585
1586 if (d40_queue_start(d40c) == NULL)
1587 d40c->busy = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05301588 pm_runtime_mark_last_busy(d40c->base->dev);
1589 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02001590
Fabio Baltieri7dd14522013-02-14 10:03:10 +01001591 d40_desc_remove(d40d);
1592 d40_desc_done(d40c, d40d);
1593 }
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001594
Linus Walleij8d318a52010-03-30 15:33:42 +02001595 d40c->pending_tx++;
1596 tasklet_schedule(&d40c->tasklet);
1597
1598}
1599
1600static void dma_tasklet(unsigned long data)
1601{
1602 struct d40_chan *d40c = (struct d40_chan *) data;
Jonas Aaberg767a9672010-08-09 12:08:34 +00001603 struct d40_desc *d40d;
Linus Walleij8d318a52010-03-30 15:33:42 +02001604 unsigned long flags;
1605 dma_async_tx_callback callback;
1606 void *callback_param;
1607
1608 spin_lock_irqsave(&d40c->lock, flags);
1609
Fabio Baltieri4226dd82012-12-13 13:46:16 +01001610 /* Get first entry from the done list */
1611 d40d = d40_first_done(d40c);
1612 if (d40d == NULL) {
1613 /* Check if we have reached here for cyclic job */
1614 d40d = d40_first_active_get(d40c);
1615 if (d40d == NULL || !d40d->cyclic)
1616 goto err;
1617 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001618
Rabin Vincent0c842b52011-01-25 11:18:35 +01001619 if (!d40d->cyclic)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +00001620 dma_cookie_complete(&d40d->txd);
Linus Walleij8d318a52010-03-30 15:33:42 +02001621
1622 /*
1623 * If terminating a channel pending_tx is set to zero.
1624 * This prevents any finished active jobs to return to the client.
1625 */
1626 if (d40c->pending_tx == 0) {
1627 spin_unlock_irqrestore(&d40c->lock, flags);
1628 return;
1629 }
1630
1631 /* Callback to client */
Jonas Aaberg767a9672010-08-09 12:08:34 +00001632 callback = d40d->txd.callback;
1633 callback_param = d40d->txd.callback_param;
Linus Walleij8d318a52010-03-30 15:33:42 +02001634
Rabin Vincent0c842b52011-01-25 11:18:35 +01001635 if (!d40d->cyclic) {
1636 if (async_tx_test_ack(&d40d->txd)) {
Jonas Aaberg767a9672010-08-09 12:08:34 +00001637 d40_desc_remove(d40d);
Rabin Vincent0c842b52011-01-25 11:18:35 +01001638 d40_desc_free(d40c, d40d);
Fabio Baltierif26e03a2012-12-13 17:12:37 +01001639 } else if (!d40d->is_in_client_list) {
1640 d40_desc_remove(d40d);
1641 d40_lcla_free_all(d40c, d40d);
1642 list_add_tail(&d40d->node, &d40c->client);
1643 d40d->is_in_client_list = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02001644 }
1645 }
1646
1647 d40c->pending_tx--;
1648
1649 if (d40c->pending_tx)
1650 tasklet_schedule(&d40c->tasklet);
1651
1652 spin_unlock_irqrestore(&d40c->lock, flags);
1653
Jonas Aaberg767a9672010-08-09 12:08:34 +00001654 if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
Linus Walleij8d318a52010-03-30 15:33:42 +02001655 callback(callback_param);
1656
1657 return;
1658
Narayanan G1bdae6f2012-02-09 12:41:37 +05301659err:
1660 /* Rescue manouver if receiving double interrupts */
Linus Walleij8d318a52010-03-30 15:33:42 +02001661 if (d40c->pending_tx > 0)
1662 d40c->pending_tx--;
1663 spin_unlock_irqrestore(&d40c->lock, flags);
1664}
1665
1666static irqreturn_t d40_handle_interrupt(int irq, void *data)
1667{
Linus Walleij8d318a52010-03-30 15:33:42 +02001668 int i;
Linus Walleij8d318a52010-03-30 15:33:42 +02001669 u32 idx;
1670 u32 row;
1671 long chan = -1;
1672 struct d40_chan *d40c;
1673 unsigned long flags;
1674 struct d40_base *base = data;
Tong Liu3cb645d2012-09-26 10:07:30 +00001675 u32 regs[base->gen_dmac.il_size];
1676 struct d40_interrupt_lookup *il = base->gen_dmac.il;
1677 u32 il_size = base->gen_dmac.il_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02001678
1679 spin_lock_irqsave(&base->interrupt_lock, flags);
1680
1681 /* Read interrupt status of both logical and physical channels */
Tong Liu3cb645d2012-09-26 10:07:30 +00001682 for (i = 0; i < il_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02001683 regs[i] = readl(base->virtbase + il[i].src);
1684
1685 for (;;) {
1686
1687 chan = find_next_bit((unsigned long *)regs,
Tong Liu3cb645d2012-09-26 10:07:30 +00001688 BITS_PER_LONG * il_size, chan + 1);
Linus Walleij8d318a52010-03-30 15:33:42 +02001689
1690 /* No more set bits found? */
Tong Liu3cb645d2012-09-26 10:07:30 +00001691 if (chan == BITS_PER_LONG * il_size)
Linus Walleij8d318a52010-03-30 15:33:42 +02001692 break;
1693
1694 row = chan / BITS_PER_LONG;
1695 idx = chan & (BITS_PER_LONG - 1);
1696
Linus Walleij8d318a52010-03-30 15:33:42 +02001697 if (il[row].offset == D40_PHY_CHAN)
1698 d40c = base->lookup_phy_chans[idx];
1699 else
1700 d40c = base->lookup_log_chans[il[row].offset + idx];
Fabio Baltieri53d6d682012-12-19 14:41:56 +01001701
1702 if (!d40c) {
1703 /*
1704 * No error because this can happen if something else
1705 * in the system is using the channel.
1706 */
1707 continue;
1708 }
1709
1710 /* ACK interrupt */
1711 writel(1 << idx, base->virtbase + il[row].clr);
1712
Linus Walleij8d318a52010-03-30 15:33:42 +02001713 spin_lock(&d40c->lock);
1714
1715 if (!il[row].is_error)
1716 dma_tc_handle(d40c);
1717 else
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001718 d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
1719 chan, il[row].offset, idx);
Linus Walleij8d318a52010-03-30 15:33:42 +02001720
1721 spin_unlock(&d40c->lock);
1722 }
1723
1724 spin_unlock_irqrestore(&base->interrupt_lock, flags);
1725
1726 return IRQ_HANDLED;
1727}
1728
Linus Walleij8d318a52010-03-30 15:33:42 +02001729static int d40_validate_conf(struct d40_chan *d40c,
1730 struct stedma40_chan_cfg *conf)
1731{
1732 int res = 0;
1733 u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
1734 u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001735 bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001736
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001737 if (!conf->dir) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001738 chan_err(d40c, "Invalid direction.\n");
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001739 res = -EINVAL;
1740 }
1741
1742 if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
1743 d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
1744 d40c->runtime_addr == 0) {
1745
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001746 chan_err(d40c, "Invalid TX channel address (%d)\n",
1747 conf->dst_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001748 res = -EINVAL;
1749 }
1750
1751 if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
1752 d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
1753 d40c->runtime_addr == 0) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001754 chan_err(d40c, "Invalid RX channel address (%d)\n",
1755 conf->src_dev_type);
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001756 res = -EINVAL;
1757 }
1758
1759 if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001760 dst_event_group == STEDMA40_DEV_DST_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001761 chan_err(d40c, "Invalid dst\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001762 res = -EINVAL;
1763 }
1764
Linus Walleij0747c7ba2010-08-09 12:07:36 +00001765 if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
Linus Walleij8d318a52010-03-30 15:33:42 +02001766 src_event_group == STEDMA40_DEV_SRC_MEMORY) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001767 chan_err(d40c, "Invalid src\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001768 res = -EINVAL;
1769 }
1770
1771 if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
1772 dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001773 chan_err(d40c, "No event line\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001774 res = -EINVAL;
1775 }
1776
1777 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
1778 (src_event_group != dst_event_group)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001779 chan_err(d40c, "Invalid event group\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001780 res = -EINVAL;
1781 }
1782
1783 if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
1784 /*
1785 * DMAC HW supports it. Will be added to this driver,
1786 * in case any dma client requires it.
1787 */
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001788 chan_err(d40c, "periph to periph not supported\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02001789 res = -EINVAL;
1790 }
1791
Per Forlind49278e2010-12-20 18:31:38 +01001792 if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1793 (1 << conf->src_info.data_width) !=
1794 d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1795 (1 << conf->dst_info.data_width)) {
1796 /*
1797 * The DMAC hardware only supports
1798 * src (burst x width) == dst (burst x width)
1799 */
1800
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01001801 chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
Per Forlind49278e2010-12-20 18:31:38 +01001802 res = -EINVAL;
1803 }
1804
Linus Walleij8d318a52010-03-30 15:33:42 +02001805 return res;
1806}
1807
Narayanan G5cd326f2011-11-30 19:20:42 +05301808static bool d40_alloc_mask_set(struct d40_phy_res *phy,
1809 bool is_src, int log_event_line, bool is_log,
1810 bool *first_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001811{
1812 unsigned long flags;
1813 spin_lock_irqsave(&phy->lock, flags);
Narayanan G5cd326f2011-11-30 19:20:42 +05301814
1815 *first_user = ((phy->allocated_src | phy->allocated_dst)
1816 == D40_ALLOC_FREE);
1817
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001818 if (!is_log) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001819 /* Physical interrupts are masked per physical full channel */
1820 if (phy->allocated_src == D40_ALLOC_FREE &&
1821 phy->allocated_dst == D40_ALLOC_FREE) {
1822 phy->allocated_dst = D40_ALLOC_PHY;
1823 phy->allocated_src = D40_ALLOC_PHY;
1824 goto found;
1825 } else
1826 goto not_found;
1827 }
1828
1829 /* Logical channel */
1830 if (is_src) {
1831 if (phy->allocated_src == D40_ALLOC_PHY)
1832 goto not_found;
1833
1834 if (phy->allocated_src == D40_ALLOC_FREE)
1835 phy->allocated_src = D40_ALLOC_LOG_FREE;
1836
1837 if (!(phy->allocated_src & (1 << log_event_line))) {
1838 phy->allocated_src |= 1 << log_event_line;
1839 goto found;
1840 } else
1841 goto not_found;
1842 } else {
1843 if (phy->allocated_dst == D40_ALLOC_PHY)
1844 goto not_found;
1845
1846 if (phy->allocated_dst == D40_ALLOC_FREE)
1847 phy->allocated_dst = D40_ALLOC_LOG_FREE;
1848
1849 if (!(phy->allocated_dst & (1 << log_event_line))) {
1850 phy->allocated_dst |= 1 << log_event_line;
1851 goto found;
1852 } else
1853 goto not_found;
1854 }
1855
1856not_found:
1857 spin_unlock_irqrestore(&phy->lock, flags);
1858 return false;
1859found:
1860 spin_unlock_irqrestore(&phy->lock, flags);
1861 return true;
1862}
1863
1864static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
1865 int log_event_line)
1866{
1867 unsigned long flags;
1868 bool is_free = false;
1869
1870 spin_lock_irqsave(&phy->lock, flags);
1871 if (!log_event_line) {
Linus Walleij8d318a52010-03-30 15:33:42 +02001872 phy->allocated_dst = D40_ALLOC_FREE;
1873 phy->allocated_src = D40_ALLOC_FREE;
1874 is_free = true;
1875 goto out;
1876 }
1877
1878 /* Logical channel */
1879 if (is_src) {
1880 phy->allocated_src &= ~(1 << log_event_line);
1881 if (phy->allocated_src == D40_ALLOC_LOG_FREE)
1882 phy->allocated_src = D40_ALLOC_FREE;
1883 } else {
1884 phy->allocated_dst &= ~(1 << log_event_line);
1885 if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
1886 phy->allocated_dst = D40_ALLOC_FREE;
1887 }
1888
1889 is_free = ((phy->allocated_src | phy->allocated_dst) ==
1890 D40_ALLOC_FREE);
1891
1892out:
1893 spin_unlock_irqrestore(&phy->lock, flags);
1894
1895 return is_free;
1896}
1897
Narayanan G5cd326f2011-11-30 19:20:42 +05301898static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
Linus Walleij8d318a52010-03-30 15:33:42 +02001899{
1900 int dev_type;
1901 int event_group;
1902 int event_line;
1903 struct d40_phy_res *phys;
1904 int i;
1905 int j;
1906 int log_num;
Gerald Baezaf000df82012-11-08 14:39:07 +01001907 int num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001908 bool is_src;
Rabin Vincent38bdbf02010-10-12 13:00:51 +00001909 bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02001910
1911 phys = d40c->base->phy_res;
Gerald Baezaf000df82012-11-08 14:39:07 +01001912 num_phy_chans = d40c->base->num_phy_chans;
Linus Walleij8d318a52010-03-30 15:33:42 +02001913
1914 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1915 dev_type = d40c->dma_cfg.src_dev_type;
1916 log_num = 2 * dev_type;
1917 is_src = true;
1918 } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
1919 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1920 /* dst event lines are used for logical memcpy */
1921 dev_type = d40c->dma_cfg.dst_dev_type;
1922 log_num = 2 * dev_type + 1;
1923 is_src = false;
1924 } else
1925 return -EINVAL;
1926
1927 event_group = D40_TYPE_TO_GROUP(dev_type);
1928 event_line = D40_TYPE_TO_EVENT(dev_type);
1929
1930 if (!is_log) {
1931 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1932 /* Find physical half channel */
Gerald Baezaf000df82012-11-08 14:39:07 +01001933 if (d40c->dma_cfg.use_fixed_channel) {
1934 i = d40c->dma_cfg.phy_channel;
Marcin Mielczarczyk4aed79b2010-05-18 00:41:21 +02001935 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05301936 0, is_log,
1937 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001938 goto found_phy;
Gerald Baezaf000df82012-11-08 14:39:07 +01001939 } else {
1940 for (i = 0; i < num_phy_chans; i++) {
1941 if (d40_alloc_mask_set(&phys[i], is_src,
1942 0, is_log,
1943 first_phy_user))
1944 goto found_phy;
1945 }
Linus Walleij8d318a52010-03-30 15:33:42 +02001946 }
1947 } else
1948 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1949 int phy_num = j + event_group * 2;
1950 for (i = phy_num; i < phy_num + 2; i++) {
Linus Walleij508849a2010-06-20 21:26:07 +00001951 if (d40_alloc_mask_set(&phys[i],
1952 is_src,
1953 0,
Narayanan G5cd326f2011-11-30 19:20:42 +05301954 is_log,
1955 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02001956 goto found_phy;
1957 }
1958 }
1959 return -EINVAL;
1960found_phy:
1961 d40c->phy_chan = &phys[i];
1962 d40c->log_num = D40_PHY_CHAN;
1963 goto out;
1964 }
1965 if (dev_type == -1)
1966 return -EINVAL;
1967
1968 /* Find logical channel */
1969 for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
1970 int phy_num = j + event_group * 2;
Narayanan G5cd326f2011-11-30 19:20:42 +05301971
1972 if (d40c->dma_cfg.use_fixed_channel) {
1973 i = d40c->dma_cfg.phy_channel;
1974
1975 if ((i != phy_num) && (i != phy_num + 1)) {
1976 dev_err(chan2dev(d40c),
1977 "invalid fixed phy channel %d\n", i);
1978 return -EINVAL;
1979 }
1980
1981 if (d40_alloc_mask_set(&phys[i], is_src, event_line,
1982 is_log, first_phy_user))
1983 goto found_log;
1984
1985 dev_err(chan2dev(d40c),
1986 "could not allocate fixed phy channel %d\n", i);
1987 return -EINVAL;
1988 }
1989
Linus Walleij8d318a52010-03-30 15:33:42 +02001990 /*
1991 * Spread logical channels across all available physical rather
1992 * than pack every logical channel at the first available phy
1993 * channels.
1994 */
1995 if (is_src) {
1996 for (i = phy_num; i < phy_num + 2; 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 } else {
2003 for (i = phy_num + 1; i >= phy_num; i--) {
2004 if (d40_alloc_mask_set(&phys[i], is_src,
Narayanan G5cd326f2011-11-30 19:20:42 +05302005 event_line, is_log,
2006 first_phy_user))
Linus Walleij8d318a52010-03-30 15:33:42 +02002007 goto found_log;
2008 }
2009 }
2010 }
2011 return -EINVAL;
2012
2013found_log:
2014 d40c->phy_chan = &phys[i];
2015 d40c->log_num = log_num;
2016out:
2017
2018 if (is_log)
2019 d40c->base->lookup_log_chans[d40c->log_num] = d40c;
2020 else
2021 d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
2022
2023 return 0;
2024
2025}
2026
Linus Walleij8d318a52010-03-30 15:33:42 +02002027static int d40_config_memcpy(struct d40_chan *d40c)
2028{
2029 dma_cap_mask_t cap = d40c->chan.device->cap_mask;
2030
2031 if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
2032 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
2033 d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
Lee Jones664a57e2013-05-03 15:31:53 +01002034 d40c->dma_cfg.dst_dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
Linus Walleij8d318a52010-03-30 15:33:42 +02002035
2036 } else if (dma_has_cap(DMA_MEMCPY, cap) &&
2037 dma_has_cap(DMA_SLAVE, cap)) {
2038 d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
2039 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002040 chan_err(d40c, "No memcpy\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002041 return -EINVAL;
2042 }
2043
2044 return 0;
2045}
2046
Linus Walleij8d318a52010-03-30 15:33:42 +02002047static int d40_free_dma(struct d40_chan *d40c)
2048{
2049
2050 int res = 0;
Jonas Aabergd181b3a2010-06-20 21:26:38 +00002051 u32 event;
Linus Walleij8d318a52010-03-30 15:33:42 +02002052 struct d40_phy_res *phy = d40c->phy_chan;
2053 bool is_src;
2054
2055 /* Terminate all queued and active transfers */
2056 d40_term_all(d40c);
2057
2058 if (phy == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002059 chan_err(d40c, "phy == null\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002060 return -EINVAL;
2061 }
2062
2063 if (phy->allocated_src == D40_ALLOC_FREE &&
2064 phy->allocated_dst == D40_ALLOC_FREE) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002065 chan_err(d40c, "channel already free\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002066 return -EINVAL;
2067 }
2068
Linus Walleij8d318a52010-03-30 15:33:42 +02002069 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
2070 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
2071 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002072 is_src = false;
2073 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
2074 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Linus Walleij8d318a52010-03-30 15:33:42 +02002075 is_src = true;
2076 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002077 chan_err(d40c, "Unknown direction\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002078 return -EINVAL;
2079 }
2080
Narayanan G7fb3e752011-11-17 17:26:41 +05302081 pm_runtime_get_sync(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002082 res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2083 if (res) {
Narayanan G1bdae6f2012-02-09 12:41:37 +05302084 chan_err(d40c, "stop failed\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302085 goto out;
Linus Walleij8d318a52010-03-30 15:33:42 +02002086 }
Narayanan G7fb3e752011-11-17 17:26:41 +05302087
Narayanan G1bdae6f2012-02-09 12:41:37 +05302088 d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2089
2090 if (chan_is_logical(d40c))
2091 d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2092 else
2093 d40c->base->lookup_phy_chans[phy->num] = NULL;
2094
Narayanan G7fb3e752011-11-17 17:26:41 +05302095 if (d40c->busy) {
2096 pm_runtime_mark_last_busy(d40c->base->dev);
2097 pm_runtime_put_autosuspend(d40c->base->dev);
2098 }
2099
2100 d40c->busy = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02002101 d40c->phy_chan = NULL;
Rabin Vincentce2ca122010-10-12 13:00:49 +00002102 d40c->configured = false;
Narayanan G7fb3e752011-11-17 17:26:41 +05302103out:
Linus Walleij8d318a52010-03-30 15:33:42 +02002104
Narayanan G7fb3e752011-11-17 17:26:41 +05302105 pm_runtime_mark_last_busy(d40c->base->dev);
2106 pm_runtime_put_autosuspend(d40c->base->dev);
2107 return res;
Linus Walleij8d318a52010-03-30 15:33:42 +02002108}
2109
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002110static bool d40_is_paused(struct d40_chan *d40c)
2111{
Rabin Vincent8ca84682011-01-25 11:18:07 +01002112 void __iomem *chanbase = chan_base(d40c);
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002113 bool is_paused = false;
2114 unsigned long flags;
2115 void __iomem *active_reg;
2116 u32 status;
2117 u32 event;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002118
2119 spin_lock_irqsave(&d40c->lock, flags);
2120
Rabin Vincent724a8572011-01-25 11:18:08 +01002121 if (chan_is_physical(d40c)) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002122 if (d40c->phy_chan->num % 2 == 0)
2123 active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2124 else
2125 active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2126
2127 status = (readl(active_reg) &
2128 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2129 D40_CHAN_POS(d40c->phy_chan->num);
2130 if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2131 is_paused = true;
2132
2133 goto _exit;
2134 }
2135
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002136 if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002137 d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002138 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002139 status = readl(chanbase + D40_CHAN_REG_SDLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002140 } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002141 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
Rabin Vincent8ca84682011-01-25 11:18:07 +01002142 status = readl(chanbase + D40_CHAN_REG_SSLNK);
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002143 } else {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002144 chan_err(d40c, "Unknown direction\n");
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002145 goto _exit;
2146 }
Jonas Aaberg9dbfbd35c2010-08-09 12:08:41 +00002147
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002148 status = (status & D40_EVENTLINE_MASK(event)) >>
2149 D40_EVENTLINE_POS(event);
2150
2151 if (status != D40_DMA_RUN)
2152 is_paused = true;
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002153_exit:
2154 spin_unlock_irqrestore(&d40c->lock, flags);
2155 return is_paused;
2156
2157}
2158
Linus Walleij8d318a52010-03-30 15:33:42 +02002159static u32 stedma40_residue(struct dma_chan *chan)
2160{
2161 struct d40_chan *d40c =
2162 container_of(chan, struct d40_chan, chan);
2163 u32 bytes_left;
2164 unsigned long flags;
2165
2166 spin_lock_irqsave(&d40c->lock, flags);
2167 bytes_left = d40_residue(d40c);
2168 spin_unlock_irqrestore(&d40c->lock, flags);
2169
2170 return bytes_left;
2171}
2172
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002173static int
2174d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
2175 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002176 unsigned int sg_len, dma_addr_t src_dev_addr,
2177 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002178{
2179 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2180 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2181 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002182 int ret;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002183
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002184 ret = d40_log_sg_to_lli(sg_src, sg_len,
2185 src_dev_addr,
2186 desc->lli_log.src,
2187 chan->log_def.lcsp1,
2188 src_info->data_width,
2189 dst_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002190
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002191 ret = d40_log_sg_to_lli(sg_dst, sg_len,
2192 dst_dev_addr,
2193 desc->lli_log.dst,
2194 chan->log_def.lcsp3,
2195 dst_info->data_width,
2196 src_info->data_width);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002197
Rabin Vincent5ed04b82011-01-25 11:18:26 +01002198 return ret < 0 ? ret : 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002199}
2200
2201static int
2202d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
2203 struct scatterlist *sg_src, struct scatterlist *sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002204 unsigned int sg_len, dma_addr_t src_dev_addr,
2205 dma_addr_t dst_dev_addr)
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002206{
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002207 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2208 struct stedma40_half_channel_info *src_info = &cfg->src_info;
2209 struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002210 unsigned long flags = 0;
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002211 int ret;
2212
Rabin Vincent0c842b52011-01-25 11:18:35 +01002213 if (desc->cyclic)
2214 flags |= LLI_CYCLIC | LLI_TERM_INT;
2215
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002216 ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
2217 desc->lli_phy.src,
2218 virt_to_phys(desc->lli_phy.src),
2219 chan->src_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002220 src_info, dst_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002221
2222 ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
2223 desc->lli_phy.dst,
2224 virt_to_phys(desc->lli_phy.dst),
2225 chan->dst_def_cfg,
Rabin Vincent0c842b52011-01-25 11:18:35 +01002226 dst_info, src_info, flags);
Rabin Vincent3e3a0762011-01-25 11:18:21 +01002227
2228 dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
2229 desc->lli_pool.size, DMA_TO_DEVICE);
2230
2231 return ret < 0 ? ret : 0;
2232}
2233
Rabin Vincent5f811582011-01-25 11:18:18 +01002234static struct d40_desc *
2235d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
2236 unsigned int sg_len, unsigned long dma_flags)
2237{
2238 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2239 struct d40_desc *desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002240 int ret;
Rabin Vincent5f811582011-01-25 11:18:18 +01002241
2242 desc = d40_desc_get(chan);
2243 if (!desc)
2244 return NULL;
2245
2246 desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
2247 cfg->dst_info.data_width);
2248 if (desc->lli_len < 0) {
2249 chan_err(chan, "Unaligned size\n");
Rabin Vincentdbd88782011-01-25 11:18:19 +01002250 goto err;
Rabin Vincent5f811582011-01-25 11:18:18 +01002251 }
2252
Rabin Vincentdbd88782011-01-25 11:18:19 +01002253 ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2254 if (ret < 0) {
2255 chan_err(chan, "Could not allocate lli\n");
2256 goto err;
2257 }
2258
Rabin Vincent5f811582011-01-25 11:18:18 +01002259 desc->lli_current = 0;
2260 desc->txd.flags = dma_flags;
2261 desc->txd.tx_submit = d40_tx_submit;
2262
2263 dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
2264
2265 return desc;
Rabin Vincentdbd88782011-01-25 11:18:19 +01002266
2267err:
2268 d40_desc_free(chan, desc);
2269 return NULL;
Rabin Vincent5f811582011-01-25 11:18:18 +01002270}
2271
Rabin Vincentcade1d32011-01-25 11:18:23 +01002272static dma_addr_t
Vinod Kouldb8196d2011-10-13 22:34:23 +05302273d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
Linus Walleij8d318a52010-03-30 15:33:42 +02002274{
Rabin Vincentcade1d32011-01-25 11:18:23 +01002275 struct stedma40_platform_data *plat = chan->base->plat_data;
2276 struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
Philippe Langlais711b9ce2011-05-07 17:09:43 +02002277 dma_addr_t addr = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002278
Rabin Vincentcade1d32011-01-25 11:18:23 +01002279 if (chan->runtime_addr)
2280 return chan->runtime_addr;
2281
Vinod Kouldb8196d2011-10-13 22:34:23 +05302282 if (direction == DMA_DEV_TO_MEM)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002283 addr = plat->dev_rx[cfg->src_dev_type];
Vinod Kouldb8196d2011-10-13 22:34:23 +05302284 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002285 addr = plat->dev_tx[cfg->dst_dev_type];
2286
2287 return addr;
2288}
2289
2290static struct dma_async_tx_descriptor *
2291d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2292 struct scatterlist *sg_dst, unsigned int sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302293 enum dma_transfer_direction direction, unsigned long dma_flags)
Rabin Vincentcade1d32011-01-25 11:18:23 +01002294{
2295 struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
Rabin Vincent822c5672011-01-25 11:18:28 +01002296 dma_addr_t src_dev_addr = 0;
2297 dma_addr_t dst_dev_addr = 0;
Rabin Vincentcade1d32011-01-25 11:18:23 +01002298 struct d40_desc *desc;
2299 unsigned long flags;
2300 int ret;
2301
2302 if (!chan->phy_chan) {
2303 chan_err(chan, "Cannot prepare unallocated channel\n");
2304 return NULL;
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002305 }
2306
Rabin Vincentcade1d32011-01-25 11:18:23 +01002307 spin_lock_irqsave(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002308
Rabin Vincentcade1d32011-01-25 11:18:23 +01002309 desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2310 if (desc == NULL)
Linus Walleij8d318a52010-03-30 15:33:42 +02002311 goto err;
2312
Rabin Vincent0c842b52011-01-25 11:18:35 +01002313 if (sg_next(&sg_src[sg_len - 1]) == sg_src)
2314 desc->cyclic = true;
2315
Linus Walleij7e426da2012-04-12 18:12:52 +02002316 if (direction != DMA_TRANS_NONE) {
Rabin Vincent822c5672011-01-25 11:18:28 +01002317 dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2318
Vinod Kouldb8196d2011-10-13 22:34:23 +05302319 if (direction == DMA_DEV_TO_MEM)
Rabin Vincent822c5672011-01-25 11:18:28 +01002320 src_dev_addr = dev_addr;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302321 else if (direction == DMA_MEM_TO_DEV)
Rabin Vincent822c5672011-01-25 11:18:28 +01002322 dst_dev_addr = dev_addr;
2323 }
Rabin Vincentcade1d32011-01-25 11:18:23 +01002324
2325 if (chan_is_logical(chan))
2326 ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002327 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002328 else
2329 ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
Rabin Vincent822c5672011-01-25 11:18:28 +01002330 sg_len, src_dev_addr, dst_dev_addr);
Rabin Vincentcade1d32011-01-25 11:18:23 +01002331
2332 if (ret) {
2333 chan_err(chan, "Failed to prepare %s sg job: %d\n",
2334 chan_is_logical(chan) ? "log" : "phy", ret);
2335 goto err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002336 }
2337
Per Forlin82babbb362011-08-29 13:33:35 +02002338 /*
2339 * add descriptor to the prepare queue in order to be able
2340 * to free them later in terminate_all
2341 */
2342 list_add_tail(&desc->node, &chan->prepare_queue);
2343
Rabin Vincentcade1d32011-01-25 11:18:23 +01002344 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002345
Rabin Vincentcade1d32011-01-25 11:18:23 +01002346 return &desc->txd;
2347
Linus Walleij8d318a52010-03-30 15:33:42 +02002348err:
Rabin Vincentcade1d32011-01-25 11:18:23 +01002349 if (desc)
2350 d40_desc_free(chan, desc);
2351 spin_unlock_irqrestore(&chan->lock, flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002352 return NULL;
2353}
Linus Walleij8d318a52010-03-30 15:33:42 +02002354
2355bool stedma40_filter(struct dma_chan *chan, void *data)
2356{
2357 struct stedma40_chan_cfg *info = data;
2358 struct d40_chan *d40c =
2359 container_of(chan, struct d40_chan, chan);
2360 int err;
2361
2362 if (data) {
2363 err = d40_validate_conf(d40c, info);
2364 if (!err)
2365 d40c->dma_cfg = *info;
2366 } else
2367 err = d40_config_memcpy(d40c);
2368
Rabin Vincentce2ca122010-10-12 13:00:49 +00002369 if (!err)
2370 d40c->configured = true;
2371
Linus Walleij8d318a52010-03-30 15:33:42 +02002372 return err == 0;
2373}
2374EXPORT_SYMBOL(stedma40_filter);
2375
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002376static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2377{
2378 bool realtime = d40c->dma_cfg.realtime;
2379 bool highprio = d40c->dma_cfg.high_priority;
Tong Liu3cb645d2012-09-26 10:07:30 +00002380 u32 rtreg;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002381 u32 event = D40_TYPE_TO_EVENT(dev_type);
2382 u32 group = D40_TYPE_TO_GROUP(dev_type);
2383 u32 bit = 1 << event;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302384 u32 prioreg;
Tong Liu3cb645d2012-09-26 10:07:30 +00002385 struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302386
Tong Liu3cb645d2012-09-26 10:07:30 +00002387 rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
Rabin Vincentccc3d692012-05-17 13:47:38 +05302388 /*
2389 * Due to a hardware bug, in some cases a logical channel triggered by
2390 * a high priority destination event line can generate extra packet
2391 * transactions.
2392 *
2393 * The workaround is to not set the high priority level for the
2394 * destination event lines that trigger logical channels.
2395 */
2396 if (!src && chan_is_logical(d40c))
2397 highprio = false;
2398
Tong Liu3cb645d2012-09-26 10:07:30 +00002399 prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002400
2401 /* Destination event lines are stored in the upper halfword */
2402 if (!src)
2403 bit <<= 16;
2404
2405 writel(bit, d40c->base->virtbase + prioreg + group * 4);
2406 writel(bit, d40c->base->virtbase + rtreg + group * 4);
2407}
2408
2409static void d40_set_prio_realtime(struct d40_chan *d40c)
2410{
2411 if (d40c->base->rev < 3)
2412 return;
2413
2414 if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
2415 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2416 __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2417
2418 if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) ||
2419 (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2420 __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2421}
2422
Linus Walleij8d318a52010-03-30 15:33:42 +02002423/* DMA ENGINE functions */
2424static int d40_alloc_chan_resources(struct dma_chan *chan)
2425{
2426 int err;
2427 unsigned long flags;
2428 struct d40_chan *d40c =
2429 container_of(chan, struct d40_chan, chan);
Linus Walleijef1872e2010-06-20 21:24:52 +00002430 bool is_free_phy;
Linus Walleij8d318a52010-03-30 15:33:42 +02002431 spin_lock_irqsave(&d40c->lock, flags);
2432
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002433 dma_cookie_init(chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02002434
Rabin Vincentce2ca122010-10-12 13:00:49 +00002435 /* If no dma configuration is set use default configuration (memcpy) */
2436 if (!d40c->configured) {
Linus Walleij8d318a52010-03-30 15:33:42 +02002437 err = d40_config_memcpy(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002438 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002439 chan_err(d40c, "Failed to configure memcpy channel\n");
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002440 goto fail;
2441 }
Linus Walleij8d318a52010-03-30 15:33:42 +02002442 }
2443
Narayanan G5cd326f2011-11-30 19:20:42 +05302444 err = d40_allocate_channel(d40c, &is_free_phy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002445 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002446 chan_err(d40c, "Failed to allocate channel\n");
Narayanan G7fb3e752011-11-17 17:26:41 +05302447 d40c->configured = false;
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002448 goto fail;
Linus Walleij8d318a52010-03-30 15:33:42 +02002449 }
2450
Narayanan G7fb3e752011-11-17 17:26:41 +05302451 pm_runtime_get_sync(d40c->base->dev);
Linus Walleijef1872e2010-06-20 21:24:52 +00002452 /* Fill in basic CFG register values */
2453 d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
Rabin Vincent724a8572011-01-25 11:18:08 +01002454 &d40c->dst_def_cfg, chan_is_logical(d40c));
Linus Walleijef1872e2010-06-20 21:24:52 +00002455
Rabin Vincentac2c0a32011-01-25 11:18:11 +01002456 d40_set_prio_realtime(d40c);
2457
Rabin Vincent724a8572011-01-25 11:18:08 +01002458 if (chan_is_logical(d40c)) {
Linus Walleijef1872e2010-06-20 21:24:52 +00002459 d40_log_cfg(&d40c->dma_cfg,
2460 &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2461
2462 if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2463 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002464 d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
Linus Walleijef1872e2010-06-20 21:24:52 +00002465 else
2466 d40c->lcpa = d40c->base->lcpa_base +
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002467 d40c->dma_cfg.dst_dev_type *
2468 D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
Linus Walleijef1872e2010-06-20 21:24:52 +00002469 }
2470
Narayanan G5cd326f2011-11-30 19:20:42 +05302471 dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
2472 chan_is_logical(d40c) ? "logical" : "physical",
2473 d40c->phy_chan->num,
2474 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
2475
2476
Linus Walleijef1872e2010-06-20 21:24:52 +00002477 /*
2478 * Only write channel configuration to the DMA if the physical
2479 * resource is free. In case of multiple logical channels
2480 * on the same physical resource, only the first write is necessary.
2481 */
Jonas Aabergb55912c2010-08-09 12:08:02 +00002482 if (is_free_phy)
2483 d40_config_write(d40c);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002484fail:
Narayanan G7fb3e752011-11-17 17:26:41 +05302485 pm_runtime_mark_last_busy(d40c->base->dev);
2486 pm_runtime_put_autosuspend(d40c->base->dev);
Linus Walleij8d318a52010-03-30 15:33:42 +02002487 spin_unlock_irqrestore(&d40c->lock, flags);
Jonas Aabergff0b12b2010-06-20 21:25:15 +00002488 return err;
Linus Walleij8d318a52010-03-30 15:33:42 +02002489}
2490
2491static void d40_free_chan_resources(struct dma_chan *chan)
2492{
2493 struct d40_chan *d40c =
2494 container_of(chan, struct d40_chan, chan);
2495 int err;
2496 unsigned long flags;
2497
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002498 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002499 chan_err(d40c, "Cannot free unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002500 return;
2501 }
2502
Linus Walleij8d318a52010-03-30 15:33:42 +02002503 spin_lock_irqsave(&d40c->lock, flags);
2504
2505 err = d40_free_dma(d40c);
2506
2507 if (err)
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002508 chan_err(d40c, "Failed to free channel\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002509 spin_unlock_irqrestore(&d40c->lock, flags);
2510}
2511
2512static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
2513 dma_addr_t dst,
2514 dma_addr_t src,
2515 size_t size,
Jonas Aaberg2a614342010-06-20 21:25:24 +00002516 unsigned long dma_flags)
Linus Walleij8d318a52010-03-30 15:33:42 +02002517{
Rabin Vincent95944c62011-01-25 11:18:17 +01002518 struct scatterlist dst_sg;
2519 struct scatterlist src_sg;
Linus Walleij8d318a52010-03-30 15:33:42 +02002520
Rabin Vincent95944c62011-01-25 11:18:17 +01002521 sg_init_table(&dst_sg, 1);
2522 sg_init_table(&src_sg, 1);
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002523
Rabin Vincent95944c62011-01-25 11:18:17 +01002524 sg_dma_address(&dst_sg) = dst;
2525 sg_dma_address(&src_sg) = src;
Linus Walleij8d318a52010-03-30 15:33:42 +02002526
Rabin Vincent95944c62011-01-25 11:18:17 +01002527 sg_dma_len(&dst_sg) = size;
2528 sg_dma_len(&src_sg) = size;
Linus Walleij8d318a52010-03-30 15:33:42 +02002529
Rabin Vincentcade1d32011-01-25 11:18:23 +01002530 return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002531}
2532
Ira Snyder0d688662010-09-30 11:46:47 +00002533static struct dma_async_tx_descriptor *
Rabin Vincentcade1d32011-01-25 11:18:23 +01002534d40_prep_memcpy_sg(struct dma_chan *chan,
2535 struct scatterlist *dst_sg, unsigned int dst_nents,
2536 struct scatterlist *src_sg, unsigned int src_nents,
2537 unsigned long dma_flags)
Ira Snyder0d688662010-09-30 11:46:47 +00002538{
2539 if (dst_nents != src_nents)
2540 return NULL;
2541
Rabin Vincentcade1d32011-01-25 11:18:23 +01002542 return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
Rabin Vincent00ac0342011-01-25 11:18:20 +01002543}
2544
Fabio Baltierif26e03a2012-12-13 17:12:37 +01002545static struct dma_async_tx_descriptor *
2546d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2547 unsigned int sg_len, enum dma_transfer_direction direction,
2548 unsigned long dma_flags, void *context)
Linus Walleij8d318a52010-03-30 15:33:42 +02002549{
Andy Shevchenkoa725dcc2013-01-10 10:53:01 +02002550 if (!is_slave_direction(direction))
Rabin Vincent00ac0342011-01-25 11:18:20 +01002551 return NULL;
2552
Rabin Vincentcade1d32011-01-25 11:18:23 +01002553 return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
Linus Walleij8d318a52010-03-30 15:33:42 +02002554}
2555
Rabin Vincent0c842b52011-01-25 11:18:35 +01002556static struct dma_async_tx_descriptor *
2557dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
2558 size_t buf_len, size_t period_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002559 enum dma_transfer_direction direction, unsigned long flags,
2560 void *context)
Rabin Vincent0c842b52011-01-25 11:18:35 +01002561{
2562 unsigned int periods = buf_len / period_len;
2563 struct dma_async_tx_descriptor *txd;
2564 struct scatterlist *sg;
2565 int i;
2566
Robert Marklund79ca7ec2011-06-27 11:33:24 +02002567 sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002568 for (i = 0; i < periods; i++) {
2569 sg_dma_address(&sg[i]) = dma_addr;
2570 sg_dma_len(&sg[i]) = period_len;
2571 dma_addr += period_len;
2572 }
2573
2574 sg[periods].offset = 0;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02002575 sg_dma_len(&sg[periods]) = 0;
Rabin Vincent0c842b52011-01-25 11:18:35 +01002576 sg[periods].page_link =
2577 ((unsigned long)sg | 0x01) & ~0x02;
2578
2579 txd = d40_prep_sg(chan, sg, sg, periods, direction,
2580 DMA_PREP_INTERRUPT);
2581
2582 kfree(sg);
2583
2584 return txd;
2585}
2586
Linus Walleij8d318a52010-03-30 15:33:42 +02002587static enum dma_status d40_tx_status(struct dma_chan *chan,
2588 dma_cookie_t cookie,
2589 struct dma_tx_state *txstate)
2590{
2591 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002592 enum dma_status ret;
Linus Walleij8d318a52010-03-30 15:33:42 +02002593
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002594 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002595 chan_err(d40c, "Cannot read status of unallocated channel\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002596 return -EINVAL;
2597 }
2598
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002599 ret = dma_cookie_status(chan, cookie, txstate);
2600 if (ret != DMA_SUCCESS)
2601 dma_set_residue(txstate, stedma40_residue(chan));
Linus Walleij8d318a52010-03-30 15:33:42 +02002602
Jonas Aaberga5ebca42010-05-18 00:41:09 +02002603 if (d40_is_paused(d40c))
2604 ret = DMA_PAUSED;
Linus Walleij8d318a52010-03-30 15:33:42 +02002605
2606 return ret;
2607}
2608
2609static void d40_issue_pending(struct dma_chan *chan)
2610{
2611 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2612 unsigned long flags;
2613
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002614 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002615 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002616 return;
2617 }
2618
Linus Walleij8d318a52010-03-30 15:33:42 +02002619 spin_lock_irqsave(&d40c->lock, flags);
2620
Per Forlina8f30672011-06-26 23:29:52 +02002621 list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2622
2623 /* Busy means that queued jobs are already being processed */
Linus Walleij8d318a52010-03-30 15:33:42 +02002624 if (!d40c->busy)
2625 (void) d40_queue_start(d40c);
2626
2627 spin_unlock_irqrestore(&d40c->lock, flags);
2628}
2629
Narayanan G1bdae6f2012-02-09 12:41:37 +05302630static void d40_terminate_all(struct dma_chan *chan)
2631{
2632 unsigned long flags;
2633 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2634 int ret;
2635
2636 spin_lock_irqsave(&d40c->lock, flags);
2637
2638 pm_runtime_get_sync(d40c->base->dev);
2639 ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
2640 if (ret)
2641 chan_err(d40c, "Failed to stop channel\n");
2642
2643 d40_term_all(d40c);
2644 pm_runtime_mark_last_busy(d40c->base->dev);
2645 pm_runtime_put_autosuspend(d40c->base->dev);
2646 if (d40c->busy) {
2647 pm_runtime_mark_last_busy(d40c->base->dev);
2648 pm_runtime_put_autosuspend(d40c->base->dev);
2649 }
2650 d40c->busy = false;
2651
2652 spin_unlock_irqrestore(&d40c->lock, flags);
2653}
2654
Rabin Vincent98ca5282011-06-27 11:33:38 +02002655static int
2656dma40_config_to_halfchannel(struct d40_chan *d40c,
2657 struct stedma40_half_channel_info *info,
2658 enum dma_slave_buswidth width,
2659 u32 maxburst)
2660{
2661 enum stedma40_periph_data_width addr_width;
2662 int psize;
2663
2664 switch (width) {
2665 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2666 addr_width = STEDMA40_BYTE_WIDTH;
2667 break;
2668 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2669 addr_width = STEDMA40_HALFWORD_WIDTH;
2670 break;
2671 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2672 addr_width = STEDMA40_WORD_WIDTH;
2673 break;
2674 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2675 addr_width = STEDMA40_DOUBLEWORD_WIDTH;
2676 break;
2677 default:
2678 dev_err(d40c->base->dev,
2679 "illegal peripheral address width "
2680 "requested (%d)\n",
2681 width);
2682 return -EINVAL;
2683 }
2684
2685 if (chan_is_logical(d40c)) {
2686 if (maxburst >= 16)
2687 psize = STEDMA40_PSIZE_LOG_16;
2688 else if (maxburst >= 8)
2689 psize = STEDMA40_PSIZE_LOG_8;
2690 else if (maxburst >= 4)
2691 psize = STEDMA40_PSIZE_LOG_4;
2692 else
2693 psize = STEDMA40_PSIZE_LOG_1;
2694 } else {
2695 if (maxburst >= 16)
2696 psize = STEDMA40_PSIZE_PHY_16;
2697 else if (maxburst >= 8)
2698 psize = STEDMA40_PSIZE_PHY_8;
2699 else if (maxburst >= 4)
2700 psize = STEDMA40_PSIZE_PHY_4;
2701 else
2702 psize = STEDMA40_PSIZE_PHY_1;
2703 }
2704
2705 info->data_width = addr_width;
2706 info->psize = psize;
2707 info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
2708
2709 return 0;
2710}
2711
Linus Walleij95e14002010-08-04 13:37:45 +02002712/* Runtime reconfiguration extension */
Rabin Vincent98ca5282011-06-27 11:33:38 +02002713static int d40_set_runtime_config(struct dma_chan *chan,
2714 struct dma_slave_config *config)
Linus Walleij95e14002010-08-04 13:37:45 +02002715{
2716 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2717 struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002718 enum dma_slave_buswidth src_addr_width, dst_addr_width;
Linus Walleij95e14002010-08-04 13:37:45 +02002719 dma_addr_t config_addr;
Rabin Vincent98ca5282011-06-27 11:33:38 +02002720 u32 src_maxburst, dst_maxburst;
2721 int ret;
2722
2723 src_addr_width = config->src_addr_width;
2724 src_maxburst = config->src_maxburst;
2725 dst_addr_width = config->dst_addr_width;
2726 dst_maxburst = config->dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002727
Vinod Kouldb8196d2011-10-13 22:34:23 +05302728 if (config->direction == DMA_DEV_TO_MEM) {
Linus Walleij95e14002010-08-04 13:37:45 +02002729 dma_addr_t dev_addr_rx =
2730 d40c->base->plat_data->dev_rx[cfg->src_dev_type];
2731
2732 config_addr = config->src_addr;
2733 if (dev_addr_rx)
2734 dev_dbg(d40c->base->dev,
2735 "channel has a pre-wired RX address %08x "
2736 "overriding with %08x\n",
2737 dev_addr_rx, config_addr);
2738 if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
2739 dev_dbg(d40c->base->dev,
2740 "channel was not configured for peripheral "
2741 "to memory transfer (%d) overriding\n",
2742 cfg->dir);
2743 cfg->dir = STEDMA40_PERIPH_TO_MEM;
2744
Rabin Vincent98ca5282011-06-27 11:33:38 +02002745 /* Configure the memory side */
2746 if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2747 dst_addr_width = src_addr_width;
2748 if (dst_maxburst == 0)
2749 dst_maxburst = src_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002750
Vinod Kouldb8196d2011-10-13 22:34:23 +05302751 } else if (config->direction == DMA_MEM_TO_DEV) {
Linus Walleij95e14002010-08-04 13:37:45 +02002752 dma_addr_t dev_addr_tx =
2753 d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
2754
2755 config_addr = config->dst_addr;
2756 if (dev_addr_tx)
2757 dev_dbg(d40c->base->dev,
2758 "channel has a pre-wired TX address %08x "
2759 "overriding with %08x\n",
2760 dev_addr_tx, config_addr);
2761 if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
2762 dev_dbg(d40c->base->dev,
2763 "channel was not configured for memory "
2764 "to peripheral transfer (%d) overriding\n",
2765 cfg->dir);
2766 cfg->dir = STEDMA40_MEM_TO_PERIPH;
2767
Rabin Vincent98ca5282011-06-27 11:33:38 +02002768 /* Configure the memory side */
2769 if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
2770 src_addr_width = dst_addr_width;
2771 if (src_maxburst == 0)
2772 src_maxburst = dst_maxburst;
Linus Walleij95e14002010-08-04 13:37:45 +02002773 } else {
2774 dev_err(d40c->base->dev,
2775 "unrecognized channel direction %d\n",
2776 config->direction);
Rabin Vincent98ca5282011-06-27 11:33:38 +02002777 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002778 }
2779
Rabin Vincent98ca5282011-06-27 11:33:38 +02002780 if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
Linus Walleij95e14002010-08-04 13:37:45 +02002781 dev_err(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002782 "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
2783 src_maxburst,
2784 src_addr_width,
2785 dst_maxburst,
2786 dst_addr_width);
2787 return -EINVAL;
Linus Walleij95e14002010-08-04 13:37:45 +02002788 }
2789
Per Forlin92bb6cd2011-10-13 12:11:36 +02002790 if (src_maxburst > 16) {
2791 src_maxburst = 16;
2792 dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
2793 } else if (dst_maxburst > 16) {
2794 dst_maxburst = 16;
2795 src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
2796 }
2797
Rabin Vincent98ca5282011-06-27 11:33:38 +02002798 ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
2799 src_addr_width,
2800 src_maxburst);
2801 if (ret)
2802 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002803
Rabin Vincent98ca5282011-06-27 11:33:38 +02002804 ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
2805 dst_addr_width,
2806 dst_maxburst);
2807 if (ret)
2808 return ret;
Linus Walleij95e14002010-08-04 13:37:45 +02002809
Per Forlina59670a2010-10-06 09:05:27 +00002810 /* Fill in register values */
Rabin Vincent724a8572011-01-25 11:18:08 +01002811 if (chan_is_logical(d40c))
Per Forlina59670a2010-10-06 09:05:27 +00002812 d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2813 else
2814 d40_phy_cfg(cfg, &d40c->src_def_cfg,
2815 &d40c->dst_def_cfg, false);
2816
Linus Walleij95e14002010-08-04 13:37:45 +02002817 /* These settings will take precedence later */
2818 d40c->runtime_addr = config_addr;
2819 d40c->runtime_direction = config->direction;
2820 dev_dbg(d40c->base->dev,
Rabin Vincent98ca5282011-06-27 11:33:38 +02002821 "configured channel %s for %s, data width %d/%d, "
2822 "maxburst %d/%d elements, LE, no flow control\n",
Linus Walleij95e14002010-08-04 13:37:45 +02002823 dma_chan_name(chan),
Vinod Kouldb8196d2011-10-13 22:34:23 +05302824 (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
Rabin Vincent98ca5282011-06-27 11:33:38 +02002825 src_addr_width, dst_addr_width,
2826 src_maxburst, dst_maxburst);
2827
2828 return 0;
Linus Walleij95e14002010-08-04 13:37:45 +02002829}
2830
Linus Walleij05827632010-05-17 16:30:42 -07002831static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
2832 unsigned long arg)
Linus Walleij8d318a52010-03-30 15:33:42 +02002833{
Linus Walleij8d318a52010-03-30 15:33:42 +02002834 struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2835
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002836 if (d40c->phy_chan == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002837 chan_err(d40c, "Channel is not allocated!\n");
Jonas Aaberg0d0f6b82010-06-20 21:25:31 +00002838 return -EINVAL;
2839 }
2840
Linus Walleij8d318a52010-03-30 15:33:42 +02002841 switch (cmd) {
2842 case DMA_TERMINATE_ALL:
Narayanan G1bdae6f2012-02-09 12:41:37 +05302843 d40_terminate_all(chan);
2844 return 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02002845 case DMA_PAUSE:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002846 return d40_pause(d40c);
Linus Walleij8d318a52010-03-30 15:33:42 +02002847 case DMA_RESUME:
Rabin Vincent86eb5fb2011-01-25 11:18:34 +01002848 return d40_resume(d40c);
Linus Walleij95e14002010-08-04 13:37:45 +02002849 case DMA_SLAVE_CONFIG:
Rabin Vincent98ca5282011-06-27 11:33:38 +02002850 return d40_set_runtime_config(chan,
Linus Walleij95e14002010-08-04 13:37:45 +02002851 (struct dma_slave_config *) arg);
Linus Walleij95e14002010-08-04 13:37:45 +02002852 default:
2853 break;
Linus Walleij8d318a52010-03-30 15:33:42 +02002854 }
2855
2856 /* Other commands are unimplemented */
2857 return -ENXIO;
2858}
2859
2860/* Initialization functions */
2861
2862static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
2863 struct d40_chan *chans, int offset,
2864 int num_chans)
2865{
2866 int i = 0;
2867 struct d40_chan *d40c;
2868
2869 INIT_LIST_HEAD(&dma->channels);
2870
2871 for (i = offset; i < offset + num_chans; i++) {
2872 d40c = &chans[i];
2873 d40c->base = base;
2874 d40c->chan.device = dma;
2875
Linus Walleij8d318a52010-03-30 15:33:42 +02002876 spin_lock_init(&d40c->lock);
2877
2878 d40c->log_num = D40_PHY_CHAN;
2879
Fabio Baltieri4226dd82012-12-13 13:46:16 +01002880 INIT_LIST_HEAD(&d40c->done);
Linus Walleij8d318a52010-03-30 15:33:42 +02002881 INIT_LIST_HEAD(&d40c->active);
2882 INIT_LIST_HEAD(&d40c->queue);
Per Forlina8f30672011-06-26 23:29:52 +02002883 INIT_LIST_HEAD(&d40c->pending_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002884 INIT_LIST_HEAD(&d40c->client);
Per Forlin82babbb362011-08-29 13:33:35 +02002885 INIT_LIST_HEAD(&d40c->prepare_queue);
Linus Walleij8d318a52010-03-30 15:33:42 +02002886
Linus Walleij8d318a52010-03-30 15:33:42 +02002887 tasklet_init(&d40c->tasklet, dma_tasklet,
2888 (unsigned long) d40c);
2889
2890 list_add_tail(&d40c->chan.device_node,
2891 &dma->channels);
2892 }
2893}
2894
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002895static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
2896{
2897 if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
2898 dev->device_prep_slave_sg = d40_prep_slave_sg;
2899
2900 if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
2901 dev->device_prep_dma_memcpy = d40_prep_memcpy;
2902
2903 /*
2904 * This controller can only access address at even
2905 * 32bit boundaries, i.e. 2^2
2906 */
2907 dev->copy_align = 2;
2908 }
2909
2910 if (dma_has_cap(DMA_SG, dev->cap_mask))
2911 dev->device_prep_dma_sg = d40_prep_memcpy_sg;
2912
Rabin Vincent0c842b52011-01-25 11:18:35 +01002913 if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
2914 dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
2915
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002916 dev->device_alloc_chan_resources = d40_alloc_chan_resources;
2917 dev->device_free_chan_resources = d40_free_chan_resources;
2918 dev->device_issue_pending = d40_issue_pending;
2919 dev->device_tx_status = d40_tx_status;
2920 dev->device_control = d40_control;
2921 dev->dev = base->dev;
2922}
2923
Linus Walleij8d318a52010-03-30 15:33:42 +02002924static int __init d40_dmaengine_init(struct d40_base *base,
2925 int num_reserved_chans)
2926{
2927 int err ;
2928
2929 d40_chan_init(base, &base->dma_slave, base->log_chans,
2930 0, base->num_log_chans);
2931
2932 dma_cap_zero(base->dma_slave.cap_mask);
2933 dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002934 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002935
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002936 d40_ops_init(base, &base->dma_slave);
Linus Walleij8d318a52010-03-30 15:33:42 +02002937
2938 err = dma_async_device_register(&base->dma_slave);
2939
2940 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002941 d40_err(base->dev, "Failed to register slave channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002942 goto failure1;
2943 }
2944
2945 d40_chan_init(base, &base->dma_memcpy, base->log_chans,
Lee Jones664a57e2013-05-03 15:31:53 +01002946 base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
Linus Walleij8d318a52010-03-30 15:33:42 +02002947
2948 dma_cap_zero(base->dma_memcpy.cap_mask);
2949 dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002950 dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002951
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002952 d40_ops_init(base, &base->dma_memcpy);
Linus Walleij8d318a52010-03-30 15:33:42 +02002953
2954 err = dma_async_device_register(&base->dma_memcpy);
2955
2956 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002957 d40_err(base->dev,
2958 "Failed to regsiter memcpy only channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002959 goto failure2;
2960 }
2961
2962 d40_chan_init(base, &base->dma_both, base->phy_chans,
2963 0, num_reserved_chans);
2964
2965 dma_cap_zero(base->dma_both.cap_mask);
2966 dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
2967 dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002968 dma_cap_set(DMA_SG, base->dma_both.cap_mask);
Rabin Vincent0c842b52011-01-25 11:18:35 +01002969 dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
Linus Walleij8d318a52010-03-30 15:33:42 +02002970
Rabin Vincent7ad74a72011-01-25 11:18:33 +01002971 d40_ops_init(base, &base->dma_both);
Linus Walleij8d318a52010-03-30 15:33:42 +02002972 err = dma_async_device_register(&base->dma_both);
2973
2974 if (err) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01002975 d40_err(base->dev,
2976 "Failed to register logical and physical capable channels\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02002977 goto failure3;
2978 }
2979 return 0;
2980failure3:
2981 dma_async_device_unregister(&base->dma_memcpy);
2982failure2:
2983 dma_async_device_unregister(&base->dma_slave);
2984failure1:
2985 return err;
2986}
2987
Narayanan G7fb3e752011-11-17 17:26:41 +05302988/* Suspend resume functionality */
2989#ifdef CONFIG_PM
2990static int dma40_pm_suspend(struct device *dev)
2991{
Narayanan G28c7a192011-11-22 13:56:55 +05302992 struct platform_device *pdev = to_platform_device(dev);
2993 struct d40_base *base = platform_get_drvdata(pdev);
2994 int ret = 0;
Narayanan G7fb3e752011-11-17 17:26:41 +05302995
Narayanan G28c7a192011-11-22 13:56:55 +05302996 if (base->lcpa_regulator)
2997 ret = regulator_disable(base->lcpa_regulator);
2998 return ret;
Narayanan G7fb3e752011-11-17 17:26:41 +05302999}
3000
3001static int dma40_runtime_suspend(struct device *dev)
3002{
3003 struct platform_device *pdev = to_platform_device(dev);
3004 struct d40_base *base = platform_get_drvdata(pdev);
3005
3006 d40_save_restore_registers(base, true);
3007
3008 /* Don't disable/enable clocks for v1 due to HW bugs */
3009 if (base->rev != 1)
3010 writel_relaxed(base->gcc_pwr_off_mask,
3011 base->virtbase + D40_DREG_GCC);
3012
3013 return 0;
3014}
3015
3016static int dma40_runtime_resume(struct device *dev)
3017{
3018 struct platform_device *pdev = to_platform_device(dev);
3019 struct d40_base *base = platform_get_drvdata(pdev);
3020
3021 if (base->initialized)
3022 d40_save_restore_registers(base, false);
3023
3024 writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
3025 base->virtbase + D40_DREG_GCC);
3026 return 0;
3027}
3028
Narayanan G28c7a192011-11-22 13:56:55 +05303029static int dma40_resume(struct device *dev)
3030{
3031 struct platform_device *pdev = to_platform_device(dev);
3032 struct d40_base *base = platform_get_drvdata(pdev);
3033 int ret = 0;
3034
3035 if (base->lcpa_regulator)
3036 ret = regulator_enable(base->lcpa_regulator);
3037
3038 return ret;
3039}
Narayanan G7fb3e752011-11-17 17:26:41 +05303040
3041static const struct dev_pm_ops dma40_pm_ops = {
3042 .suspend = dma40_pm_suspend,
3043 .runtime_suspend = dma40_runtime_suspend,
3044 .runtime_resume = dma40_runtime_resume,
Narayanan G28c7a192011-11-22 13:56:55 +05303045 .resume = dma40_resume,
Narayanan G7fb3e752011-11-17 17:26:41 +05303046};
3047#define DMA40_PM_OPS (&dma40_pm_ops)
3048#else
3049#define DMA40_PM_OPS NULL
3050#endif
3051
Linus Walleij8d318a52010-03-30 15:33:42 +02003052/* Initialization functions. */
3053
3054static int __init d40_phy_res_init(struct d40_base *base)
3055{
3056 int i;
3057 int num_phy_chans_avail = 0;
3058 u32 val[2];
3059 int odd_even_bit = -2;
Narayanan G7fb3e752011-11-17 17:26:41 +05303060 int gcc = D40_DREG_GCC_ENA;
Linus Walleij8d318a52010-03-30 15:33:42 +02003061
3062 val[0] = readl(base->virtbase + D40_DREG_PRSME);
3063 val[1] = readl(base->virtbase + D40_DREG_PRSMO);
3064
3065 for (i = 0; i < base->num_phy_chans; i++) {
3066 base->phy_res[i].num = i;
3067 odd_even_bit += 2 * ((i % 2) == 0);
3068 if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
3069 /* Mark security only channels as occupied */
3070 base->phy_res[i].allocated_src = D40_ALLOC_PHY;
3071 base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303072 base->phy_res[i].reserved = true;
3073 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3074 D40_DREG_GCC_SRC);
3075 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
3076 D40_DREG_GCC_DST);
3077
3078
Linus Walleij8d318a52010-03-30 15:33:42 +02003079 } else {
3080 base->phy_res[i].allocated_src = D40_ALLOC_FREE;
3081 base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
Narayanan G7fb3e752011-11-17 17:26:41 +05303082 base->phy_res[i].reserved = false;
Linus Walleij8d318a52010-03-30 15:33:42 +02003083 num_phy_chans_avail++;
3084 }
3085 spin_lock_init(&base->phy_res[i].lock);
3086 }
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003087
3088 /* Mark disabled channels as occupied */
3089 for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
Rabin Vincentf57b4072010-10-06 08:20:35 +00003090 int chan = base->plat_data->disabled_channels[i];
3091
3092 base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3093 base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
Narayanan G7fb3e752011-11-17 17:26:41 +05303094 base->phy_res[chan].reserved = true;
3095 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3096 D40_DREG_GCC_SRC);
3097 gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
3098 D40_DREG_GCC_DST);
Rabin Vincentf57b4072010-10-06 08:20:35 +00003099 num_phy_chans_avail--;
Jonas Aaberg6b7acd82010-06-20 21:26:59 +00003100 }
3101
Fabio Baltieri74070482012-12-18 12:25:14 +01003102 /* Mark soft_lli channels */
3103 for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
3104 int chan = base->plat_data->soft_lli_chans[i];
3105
3106 base->phy_res[chan].use_soft_lli = true;
3107 }
3108
Linus Walleij8d318a52010-03-30 15:33:42 +02003109 dev_info(base->dev, "%d of %d physical DMA channels available\n",
3110 num_phy_chans_avail, base->num_phy_chans);
3111
3112 /* Verify settings extended vs standard */
3113 val[0] = readl(base->virtbase + D40_DREG_PRTYP);
3114
3115 for (i = 0; i < base->num_phy_chans; i++) {
3116
3117 if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
3118 (val[0] & 0x3) != 1)
3119 dev_info(base->dev,
3120 "[%s] INFO: channel %d is misconfigured (%d)\n",
3121 __func__, i, val[0] & 0x3);
3122
3123 val[0] = val[0] >> 2;
3124 }
3125
Narayanan G7fb3e752011-11-17 17:26:41 +05303126 /*
3127 * To keep things simple, Enable all clocks initially.
3128 * The clocks will get managed later post channel allocation.
3129 * The clocks for the event lines on which reserved channels exists
3130 * are not managed here.
3131 */
3132 writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
3133 base->gcc_pwr_off_mask = gcc;
3134
Linus Walleij8d318a52010-03-30 15:33:42 +02003135 return num_phy_chans_avail;
3136}
3137
3138static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
3139{
Linus Walleij8d318a52010-03-30 15:33:42 +02003140 struct stedma40_platform_data *plat_data;
3141 struct clk *clk = NULL;
3142 void __iomem *virtbase = NULL;
3143 struct resource *res = NULL;
3144 struct d40_base *base = NULL;
3145 int num_log_chans = 0;
3146 int num_phy_chans;
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003147 int clk_ret = -EINVAL;
Linus Walleij8d318a52010-03-30 15:33:42 +02003148 int i;
Linus Walleijf4b89762011-06-27 11:33:46 +02003149 u32 pid;
3150 u32 cid;
3151 u8 rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003152
3153 clk = clk_get(&pdev->dev, NULL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003154 if (IS_ERR(clk)) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003155 d40_err(&pdev->dev, "No matching clock found\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003156 goto failure;
3157 }
3158
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003159 clk_ret = clk_prepare_enable(clk);
3160 if (clk_ret) {
3161 d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3162 goto failure;
3163 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003164
3165 /* Get IO for DMAC base address */
3166 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3167 if (!res)
3168 goto failure;
3169
3170 if (request_mem_region(res->start, resource_size(res),
3171 D40_NAME " I/O base") == NULL)
3172 goto failure;
3173
3174 virtbase = ioremap(res->start, resource_size(res));
3175 if (!virtbase)
3176 goto failure;
3177
Linus Walleijf4b89762011-06-27 11:33:46 +02003178 /* This is just a regular AMBA PrimeCell ID actually */
3179 for (pid = 0, i = 0; i < 4; i++)
3180 pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3181 & 255) << (i * 8);
3182 for (cid = 0, i = 0; i < 4; i++)
3183 cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3184 & 255) << (i * 8);
Linus Walleij8d318a52010-03-30 15:33:42 +02003185
Linus Walleijf4b89762011-06-27 11:33:46 +02003186 if (cid != AMBA_CID) {
3187 d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003188 goto failure;
3189 }
Linus Walleijf4b89762011-06-27 11:33:46 +02003190 if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3191 d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3192 AMBA_MANF_BITS(pid),
3193 AMBA_VENDOR_ST);
3194 goto failure;
3195 }
3196 /*
3197 * HW revision:
3198 * DB8500ed has revision 0
3199 * ? has revision 1
3200 * DB8500v1 has revision 2
3201 * DB8500v2 has revision 3
Gerald Baeza47db92f2012-09-21 21:21:37 +02003202 * AP9540v1 has revision 4
3203 * DB8540v1 has revision 4
Linus Walleijf4b89762011-06-27 11:33:46 +02003204 */
3205 rev = AMBA_REV_BITS(pid);
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003206
Gerald Baeza47db92f2012-09-21 21:21:37 +02003207 plat_data = pdev->dev.platform_data;
Linus Walleij8d318a52010-03-30 15:33:42 +02003208
Gerald Baeza47db92f2012-09-21 21:21:37 +02003209 /* The number of physical channels on this HW */
3210 if (plat_data->num_of_phy_chans)
3211 num_phy_chans = plat_data->num_of_phy_chans;
3212 else
3213 num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3214
3215 dev_info(&pdev->dev, "hardware revision: %d @ 0x%x with %d physical channels\n",
3216 rev, res->start, num_phy_chans);
Linus Walleij8d318a52010-03-30 15:33:42 +02003217
Narayanan G1bdae6f2012-02-09 12:41:37 +05303218 if (rev < 2) {
3219 d40_err(&pdev->dev, "hardware revision: %d is not supported",
3220 rev);
3221 goto failure;
3222 }
3223
Linus Walleij8d318a52010-03-30 15:33:42 +02003224 /* Count the number of logical channels in use */
3225 for (i = 0; i < plat_data->dev_len; i++)
3226 if (plat_data->dev_rx[i] != 0)
3227 num_log_chans++;
3228
3229 for (i = 0; i < plat_data->dev_len; i++)
3230 if (plat_data->dev_tx[i] != 0)
3231 num_log_chans++;
3232
3233 base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
Lee Jones664a57e2013-05-03 15:31:53 +01003234 (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
Linus Walleij8d318a52010-03-30 15:33:42 +02003235 sizeof(struct d40_chan), GFP_KERNEL);
3236
3237 if (base == NULL) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003238 d40_err(&pdev->dev, "Out of memory\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003239 goto failure;
3240 }
3241
Jonas Aaberg3ae02672010-08-09 12:08:18 +00003242 base->rev = rev;
Linus Walleij8d318a52010-03-30 15:33:42 +02003243 base->clk = clk;
3244 base->num_phy_chans = num_phy_chans;
3245 base->num_log_chans = num_log_chans;
3246 base->phy_start = res->start;
3247 base->phy_size = resource_size(res);
3248 base->virtbase = virtbase;
3249 base->plat_data = plat_data;
3250 base->dev = &pdev->dev;
3251 base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
3252 base->log_chans = &base->phy_chans[num_phy_chans];
3253
Tong Liu3cb645d2012-09-26 10:07:30 +00003254 if (base->plat_data->num_of_phy_chans == 14) {
3255 base->gen_dmac.backup = d40_backup_regs_v4b;
3256 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
3257 base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
3258 base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
3259 base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
3260 base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
3261 base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
3262 base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
3263 base->gen_dmac.il = il_v4b;
3264 base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
3265 base->gen_dmac.init_reg = dma_init_reg_v4b;
3266 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
3267 } else {
3268 if (base->rev >= 3) {
3269 base->gen_dmac.backup = d40_backup_regs_v4a;
3270 base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
3271 }
3272 base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
3273 base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
3274 base->gen_dmac.realtime_en = D40_DREG_RSEG1;
3275 base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
3276 base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
3277 base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
3278 base->gen_dmac.il = il_v4a;
3279 base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
3280 base->gen_dmac.init_reg = dma_init_reg_v4a;
3281 base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
3282 }
3283
Linus Walleij8d318a52010-03-30 15:33:42 +02003284 base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
3285 GFP_KERNEL);
3286 if (!base->phy_res)
3287 goto failure;
3288
3289 base->lookup_phy_chans = kzalloc(num_phy_chans *
3290 sizeof(struct d40_chan *),
3291 GFP_KERNEL);
3292 if (!base->lookup_phy_chans)
3293 goto failure;
3294
Lee Jones664a57e2013-05-03 15:31:53 +01003295 if (num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) {
Linus Walleij8d318a52010-03-30 15:33:42 +02003296 /*
3297 * The max number of logical channels are event lines for all
3298 * src devices and dst devices
3299 */
3300 base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
3301 sizeof(struct d40_chan *),
3302 GFP_KERNEL);
3303 if (!base->lookup_log_chans)
3304 goto failure;
3305 }
Jonas Aaberg698e4732010-08-09 12:08:56 +00003306
Narayanan G7fb3e752011-11-17 17:26:41 +05303307 base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
3308 sizeof(d40_backup_regs_chan),
Linus Walleij8d318a52010-03-30 15:33:42 +02003309 GFP_KERNEL);
Narayanan G7fb3e752011-11-17 17:26:41 +05303310 if (!base->reg_val_backup_chan)
3311 goto failure;
3312
3313 base->lcla_pool.alloc_map =
3314 kzalloc(num_phy_chans * sizeof(struct d40_desc *)
3315 * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
Linus Walleij8d318a52010-03-30 15:33:42 +02003316 if (!base->lcla_pool.alloc_map)
3317 goto failure;
3318
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003319 base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3320 0, SLAB_HWCACHE_ALIGN,
3321 NULL);
3322 if (base->desc_slab == NULL)
3323 goto failure;
3324
Linus Walleij8d318a52010-03-30 15:33:42 +02003325 return base;
3326
3327failure:
Ulf Hanssonb707c6582012-08-23 13:41:58 +02003328 if (!clk_ret)
3329 clk_disable_unprepare(clk);
3330 if (!IS_ERR(clk))
Linus Walleij8d318a52010-03-30 15:33:42 +02003331 clk_put(clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003332 if (virtbase)
3333 iounmap(virtbase);
3334 if (res)
3335 release_mem_region(res->start,
3336 resource_size(res));
3337 if (virtbase)
3338 iounmap(virtbase);
3339
3340 if (base) {
3341 kfree(base->lcla_pool.alloc_map);
Narayanan G1bdae6f2012-02-09 12:41:37 +05303342 kfree(base->reg_val_backup_chan);
Linus Walleij8d318a52010-03-30 15:33:42 +02003343 kfree(base->lookup_log_chans);
3344 kfree(base->lookup_phy_chans);
3345 kfree(base->phy_res);
3346 kfree(base);
3347 }
3348
3349 return NULL;
3350}
3351
3352static void __init d40_hw_init(struct d40_base *base)
3353{
3354
Linus Walleij8d318a52010-03-30 15:33:42 +02003355 int i;
3356 u32 prmseo[2] = {0, 0};
3357 u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
3358 u32 pcmis = 0;
3359 u32 pcicr = 0;
Tong Liu3cb645d2012-09-26 10:07:30 +00003360 struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
3361 u32 reg_size = base->gen_dmac.init_reg_size;
Linus Walleij8d318a52010-03-30 15:33:42 +02003362
Tong Liu3cb645d2012-09-26 10:07:30 +00003363 for (i = 0; i < reg_size; i++)
Linus Walleij8d318a52010-03-30 15:33:42 +02003364 writel(dma_init_reg[i].val,
3365 base->virtbase + dma_init_reg[i].reg);
3366
3367 /* Configure all our dma channels to default settings */
3368 for (i = 0; i < base->num_phy_chans; i++) {
3369
3370 activeo[i % 2] = activeo[i % 2] << 2;
3371
3372 if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
3373 == D40_ALLOC_PHY) {
3374 activeo[i % 2] |= 3;
3375 continue;
3376 }
3377
3378 /* Enable interrupt # */
3379 pcmis = (pcmis << 1) | 1;
3380
3381 /* Clear interrupt # */
3382 pcicr = (pcicr << 1) | 1;
3383
3384 /* Set channel to physical mode */
3385 prmseo[i % 2] = prmseo[i % 2] << 2;
3386 prmseo[i % 2] |= 1;
3387
3388 }
3389
3390 writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
3391 writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
3392 writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
3393 writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
3394
3395 /* Write which interrupt to enable */
Tong Liu3cb645d2012-09-26 10:07:30 +00003396 writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
Linus Walleij8d318a52010-03-30 15:33:42 +02003397
3398 /* Write which interrupt to clear */
Tong Liu3cb645d2012-09-26 10:07:30 +00003399 writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
Linus Walleij8d318a52010-03-30 15:33:42 +02003400
Tong Liu3cb645d2012-09-26 10:07:30 +00003401 /* These are __initdata and cannot be accessed after init */
3402 base->gen_dmac.init_reg = NULL;
3403 base->gen_dmac.init_reg_size = 0;
Linus Walleij8d318a52010-03-30 15:33:42 +02003404}
3405
Linus Walleij508849a2010-06-20 21:26:07 +00003406static int __init d40_lcla_allocate(struct d40_base *base)
3407{
Rabin Vincent026cbc42011-01-25 11:18:14 +01003408 struct d40_lcla_pool *pool = &base->lcla_pool;
Linus Walleij508849a2010-06-20 21:26:07 +00003409 unsigned long *page_list;
3410 int i, j;
3411 int ret = 0;
3412
3413 /*
3414 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3415 * To full fill this hardware requirement without wasting 256 kb
3416 * we allocate pages until we get an aligned one.
3417 */
3418 page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3419 GFP_KERNEL);
3420
3421 if (!page_list) {
3422 ret = -ENOMEM;
3423 goto failure;
3424 }
3425
3426 /* Calculating how many pages that are required */
3427 base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3428
3429 for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3430 page_list[i] = __get_free_pages(GFP_KERNEL,
3431 base->lcla_pool.pages);
3432 if (!page_list[i]) {
3433
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003434 d40_err(base->dev, "Failed to allocate %d pages.\n",
3435 base->lcla_pool.pages);
Linus Walleij508849a2010-06-20 21:26:07 +00003436
3437 for (j = 0; j < i; j++)
3438 free_pages(page_list[j], base->lcla_pool.pages);
3439 goto failure;
3440 }
3441
3442 if ((virt_to_phys((void *)page_list[i]) &
3443 (LCLA_ALIGNMENT - 1)) == 0)
3444 break;
3445 }
3446
3447 for (j = 0; j < i; j++)
3448 free_pages(page_list[j], base->lcla_pool.pages);
3449
3450 if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3451 base->lcla_pool.base = (void *)page_list[i];
3452 } else {
Jonas Aaberg767a9672010-08-09 12:08:34 +00003453 /*
3454 * After many attempts and no succees with finding the correct
3455 * alignment, try with allocating a big buffer.
3456 */
Linus Walleij508849a2010-06-20 21:26:07 +00003457 dev_warn(base->dev,
3458 "[%s] Failed to get %d pages @ 18 bit align.\n",
3459 __func__, base->lcla_pool.pages);
3460 base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3461 base->num_phy_chans +
3462 LCLA_ALIGNMENT,
3463 GFP_KERNEL);
3464 if (!base->lcla_pool.base_unaligned) {
3465 ret = -ENOMEM;
3466 goto failure;
3467 }
3468
3469 base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3470 LCLA_ALIGNMENT);
3471 }
3472
Rabin Vincent026cbc42011-01-25 11:18:14 +01003473 pool->dma_addr = dma_map_single(base->dev, pool->base,
3474 SZ_1K * base->num_phy_chans,
3475 DMA_TO_DEVICE);
3476 if (dma_mapping_error(base->dev, pool->dma_addr)) {
3477 pool->dma_addr = 0;
3478 ret = -ENOMEM;
3479 goto failure;
3480 }
3481
Linus Walleij508849a2010-06-20 21:26:07 +00003482 writel(virt_to_phys(base->lcla_pool.base),
3483 base->virtbase + D40_DREG_LCLA);
3484failure:
3485 kfree(page_list);
3486 return ret;
3487}
3488
Linus Walleij8d318a52010-03-30 15:33:42 +02003489static int __init d40_probe(struct platform_device *pdev)
3490{
3491 int err;
3492 int ret = -ENOENT;
3493 struct d40_base *base;
3494 struct resource *res = NULL;
3495 int num_reserved_chans;
3496 u32 val;
3497
3498 base = d40_hw_detect_init(pdev);
3499
3500 if (!base)
3501 goto failure;
3502
3503 num_reserved_chans = d40_phy_res_init(base);
3504
3505 platform_set_drvdata(pdev, base);
3506
3507 spin_lock_init(&base->interrupt_lock);
3508 spin_lock_init(&base->execmd_lock);
3509
3510 /* Get IO for logical channel parameter address */
3511 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
3512 if (!res) {
3513 ret = -ENOENT;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003514 d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003515 goto failure;
3516 }
3517 base->lcpa_size = resource_size(res);
3518 base->phy_lcpa = res->start;
3519
3520 if (request_mem_region(res->start, resource_size(res),
3521 D40_NAME " I/O lcpa") == NULL) {
3522 ret = -EBUSY;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003523 d40_err(&pdev->dev,
3524 "Failed to request LCPA region 0x%x-0x%x\n",
3525 res->start, res->end);
Linus Walleij8d318a52010-03-30 15:33:42 +02003526 goto failure;
3527 }
3528
3529 /* We make use of ESRAM memory for this. */
3530 val = readl(base->virtbase + D40_DREG_LCPA);
3531 if (res->start != val && val != 0) {
3532 dev_warn(&pdev->dev,
3533 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
3534 __func__, val, res->start);
3535 } else
3536 writel(res->start, base->virtbase + D40_DREG_LCPA);
3537
3538 base->lcpa_base = ioremap(res->start, resource_size(res));
3539 if (!base->lcpa_base) {
3540 ret = -ENOMEM;
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003541 d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003542 goto failure;
3543 }
Narayanan G28c7a192011-11-22 13:56:55 +05303544 /* If lcla has to be located in ESRAM we don't need to allocate */
3545 if (base->plat_data->use_esram_lcla) {
3546 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3547 "lcla_esram");
3548 if (!res) {
3549 ret = -ENOENT;
3550 d40_err(&pdev->dev,
3551 "No \"lcla_esram\" memory resource\n");
3552 goto failure;
3553 }
3554 base->lcla_pool.base = ioremap(res->start,
3555 resource_size(res));
3556 if (!base->lcla_pool.base) {
3557 ret = -ENOMEM;
3558 d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
3559 goto failure;
3560 }
3561 writel(res->start, base->virtbase + D40_DREG_LCLA);
Linus Walleij508849a2010-06-20 21:26:07 +00003562
Narayanan G28c7a192011-11-22 13:56:55 +05303563 } else {
3564 ret = d40_lcla_allocate(base);
3565 if (ret) {
3566 d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
3567 goto failure;
3568 }
Linus Walleij8d318a52010-03-30 15:33:42 +02003569 }
3570
Linus Walleij8d318a52010-03-30 15:33:42 +02003571 spin_lock_init(&base->lcla_pool.lock);
3572
Linus Walleij8d318a52010-03-30 15:33:42 +02003573 base->irq = platform_get_irq(pdev, 0);
3574
3575 ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
Linus Walleij8d318a52010-03-30 15:33:42 +02003576 if (ret) {
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003577 d40_err(&pdev->dev, "No IRQ defined\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003578 goto failure;
3579 }
3580
Narayanan G7fb3e752011-11-17 17:26:41 +05303581 pm_runtime_irq_safe(base->dev);
3582 pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
3583 pm_runtime_use_autosuspend(base->dev);
3584 pm_runtime_enable(base->dev);
3585 pm_runtime_resume(base->dev);
Narayanan G28c7a192011-11-22 13:56:55 +05303586
3587 if (base->plat_data->use_esram_lcla) {
3588
3589 base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
3590 if (IS_ERR(base->lcpa_regulator)) {
3591 d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
3592 base->lcpa_regulator = NULL;
3593 goto failure;
3594 }
3595
3596 ret = regulator_enable(base->lcpa_regulator);
3597 if (ret) {
3598 d40_err(&pdev->dev,
3599 "Failed to enable lcpa_regulator\n");
3600 regulator_put(base->lcpa_regulator);
3601 base->lcpa_regulator = NULL;
3602 goto failure;
3603 }
3604 }
3605
Narayanan G7fb3e752011-11-17 17:26:41 +05303606 base->initialized = true;
Linus Walleij8d318a52010-03-30 15:33:42 +02003607 err = d40_dmaengine_init(base, num_reserved_chans);
3608 if (err)
3609 goto failure;
3610
Per Forlinb96710e2011-10-18 18:39:47 +02003611 base->dev->dma_parms = &base->dma_parms;
3612 err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3613 if (err) {
3614 d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3615 goto failure;
3616 }
3617
Linus Walleij8d318a52010-03-30 15:33:42 +02003618 d40_hw_init(base);
3619
3620 dev_info(base->dev, "initialized\n");
3621 return 0;
3622
3623failure:
3624 if (base) {
Jonas Aabergc675b1b2010-06-20 21:25:08 +00003625 if (base->desc_slab)
3626 kmem_cache_destroy(base->desc_slab);
Linus Walleij8d318a52010-03-30 15:33:42 +02003627 if (base->virtbase)
3628 iounmap(base->virtbase);
Rabin Vincent026cbc42011-01-25 11:18:14 +01003629
Narayanan G28c7a192011-11-22 13:56:55 +05303630 if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
3631 iounmap(base->lcla_pool.base);
3632 base->lcla_pool.base = NULL;
3633 }
3634
Rabin Vincent026cbc42011-01-25 11:18:14 +01003635 if (base->lcla_pool.dma_addr)
3636 dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3637 SZ_1K * base->num_phy_chans,
3638 DMA_TO_DEVICE);
3639
Linus Walleij508849a2010-06-20 21:26:07 +00003640 if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3641 free_pages((unsigned long)base->lcla_pool.base,
3642 base->lcla_pool.pages);
Jonas Aaberg767a9672010-08-09 12:08:34 +00003643
3644 kfree(base->lcla_pool.base_unaligned);
3645
Linus Walleij8d318a52010-03-30 15:33:42 +02003646 if (base->phy_lcpa)
3647 release_mem_region(base->phy_lcpa,
3648 base->lcpa_size);
3649 if (base->phy_start)
3650 release_mem_region(base->phy_start,
3651 base->phy_size);
3652 if (base->clk) {
Fabio Baltierida2ac562013-01-07 10:58:35 +01003653 clk_disable_unprepare(base->clk);
Linus Walleij8d318a52010-03-30 15:33:42 +02003654 clk_put(base->clk);
3655 }
3656
Narayanan G28c7a192011-11-22 13:56:55 +05303657 if (base->lcpa_regulator) {
3658 regulator_disable(base->lcpa_regulator);
3659 regulator_put(base->lcpa_regulator);
3660 }
3661
Linus Walleij8d318a52010-03-30 15:33:42 +02003662 kfree(base->lcla_pool.alloc_map);
3663 kfree(base->lookup_log_chans);
3664 kfree(base->lookup_phy_chans);
3665 kfree(base->phy_res);
3666 kfree(base);
3667 }
3668
Rabin Vincent6db5a8b2011-01-25 11:18:09 +01003669 d40_err(&pdev->dev, "probe failed\n");
Linus Walleij8d318a52010-03-30 15:33:42 +02003670 return ret;
3671}
3672
3673static struct platform_driver d40_driver = {
3674 .driver = {
3675 .owner = THIS_MODULE,
3676 .name = D40_NAME,
Narayanan G7fb3e752011-11-17 17:26:41 +05303677 .pm = DMA40_PM_OPS,
Linus Walleij8d318a52010-03-30 15:33:42 +02003678 },
3679};
3680
Rabin Vincentcb9ab2d2011-01-25 11:18:04 +01003681static int __init stedma40_init(void)
Linus Walleij8d318a52010-03-30 15:33:42 +02003682{
3683 return platform_driver_probe(&d40_driver, d40_probe);
3684}
Linus Walleija0eb2212011-05-18 14:18:57 +02003685subsys_initcall(stedma40_init);