blob: e5fe9c764f5333045dedc643d3ee3e3bd4eb1fdb [file] [log] [blame]
Boojin Kimb7d861d2011-12-26 18:49:52 +09001/*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
Jassi Brarb3040e42010-05-23 20:28:19 -07004 *
5 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassi.brar@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Boojin Kimb7d861d2011-12-26 18:49:52 +090014#include <linux/kernel.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070015#include <linux/io.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/module.h>
Boojin Kimb7d861d2011-12-26 18:49:52 +090019#include <linux/string.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/dma-mapping.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070023#include <linux/dmaengine.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070024#include <linux/amba/bus.h>
25#include <linux/amba/pl330.h>
Boojin Kim1b9bb712011-09-02 09:44:30 +090026#include <linux/scatterlist.h>
Thomas Abraham93ed5542011-10-24 11:43:31 +020027#include <linux/of.h>
Padmavathi Vennaa80258f2013-02-14 09:10:06 +053028#include <linux/of_dma.h>
Sachin Kamatbcc7fa92013-03-04 14:36:27 +053029#include <linux/err.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070030
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031#include "dmaengine.h"
Boojin Kimb7d861d2011-12-26 18:49:52 +090032#define PL330_MAX_CHAN 8
33#define PL330_MAX_IRQS 32
34#define PL330_MAX_PERI 32
35
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +020036enum pl330_cachectrl {
37 CCTRL0, /* Noncacheable and nonbufferable */
38 CCTRL1, /* Bufferable only */
39 CCTRL2, /* Cacheable, but do not allocate */
40 CCTRL3, /* Cacheable and bufferable, but do not allocate */
41 INVALID1, /* AWCACHE = 0x1000 */
42 INVALID2,
43 CCTRL6, /* Cacheable write-through, allocate on writes only */
44 CCTRL7, /* Cacheable write-back, allocate on writes only */
Boojin Kimb7d861d2011-12-26 18:49:52 +090045};
46
47enum pl330_byteswap {
48 SWAP_NO,
49 SWAP_2,
50 SWAP_4,
51 SWAP_8,
52 SWAP_16,
53};
54
Boojin Kimb7d861d2011-12-26 18:49:52 +090055/* Register and Bit field Definitions */
56#define DS 0x0
57#define DS_ST_STOP 0x0
58#define DS_ST_EXEC 0x1
59#define DS_ST_CMISS 0x2
60#define DS_ST_UPDTPC 0x3
61#define DS_ST_WFE 0x4
62#define DS_ST_ATBRR 0x5
63#define DS_ST_QBUSY 0x6
64#define DS_ST_WFP 0x7
65#define DS_ST_KILL 0x8
66#define DS_ST_CMPLT 0x9
67#define DS_ST_FLTCMP 0xe
68#define DS_ST_FAULT 0xf
69
70#define DPC 0x4
71#define INTEN 0x20
72#define ES 0x24
73#define INTSTATUS 0x28
74#define INTCLR 0x2c
75#define FSM 0x30
76#define FSC 0x34
77#define FTM 0x38
78
79#define _FTC 0x40
80#define FTC(n) (_FTC + (n)*0x4)
81
82#define _CS 0x100
83#define CS(n) (_CS + (n)*0x8)
84#define CS_CNS (1 << 21)
85
86#define _CPC 0x104
87#define CPC(n) (_CPC + (n)*0x8)
88
89#define _SA 0x400
90#define SA(n) (_SA + (n)*0x20)
91
92#define _DA 0x404
93#define DA(n) (_DA + (n)*0x20)
94
95#define _CC 0x408
96#define CC(n) (_CC + (n)*0x20)
97
98#define CC_SRCINC (1 << 0)
99#define CC_DSTINC (1 << 14)
100#define CC_SRCPRI (1 << 8)
101#define CC_DSTPRI (1 << 22)
102#define CC_SRCNS (1 << 9)
103#define CC_DSTNS (1 << 23)
104#define CC_SRCIA (1 << 10)
105#define CC_DSTIA (1 << 24)
106#define CC_SRCBRSTLEN_SHFT 4
107#define CC_DSTBRSTLEN_SHFT 18
108#define CC_SRCBRSTSIZE_SHFT 1
109#define CC_DSTBRSTSIZE_SHFT 15
110#define CC_SRCCCTRL_SHFT 11
111#define CC_SRCCCTRL_MASK 0x7
112#define CC_DSTCCTRL_SHFT 25
113#define CC_DRCCCTRL_MASK 0x7
114#define CC_SWAP_SHFT 28
115
116#define _LC0 0x40c
117#define LC0(n) (_LC0 + (n)*0x20)
118
119#define _LC1 0x410
120#define LC1(n) (_LC1 + (n)*0x20)
121
122#define DBGSTATUS 0xd00
123#define DBG_BUSY (1 << 0)
124
125#define DBGCMD 0xd04
126#define DBGINST0 0xd08
127#define DBGINST1 0xd0c
128
129#define CR0 0xe00
130#define CR1 0xe04
131#define CR2 0xe08
132#define CR3 0xe0c
133#define CR4 0xe10
134#define CRD 0xe14
135
136#define PERIPH_ID 0xfe0
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900137#define PERIPH_REV_SHIFT 20
138#define PERIPH_REV_MASK 0xf
139#define PERIPH_REV_R0P0 0
140#define PERIPH_REV_R1P0 1
141#define PERIPH_REV_R1P1 2
Boojin Kimb7d861d2011-12-26 18:49:52 +0900142
143#define CR0_PERIPH_REQ_SET (1 << 0)
144#define CR0_BOOT_EN_SET (1 << 1)
145#define CR0_BOOT_MAN_NS (1 << 2)
146#define CR0_NUM_CHANS_SHIFT 4
147#define CR0_NUM_CHANS_MASK 0x7
148#define CR0_NUM_PERIPH_SHIFT 12
149#define CR0_NUM_PERIPH_MASK 0x1f
150#define CR0_NUM_EVENTS_SHIFT 17
151#define CR0_NUM_EVENTS_MASK 0x1f
152
153#define CR1_ICACHE_LEN_SHIFT 0
154#define CR1_ICACHE_LEN_MASK 0x7
155#define CR1_NUM_ICACHELINES_SHIFT 4
156#define CR1_NUM_ICACHELINES_MASK 0xf
157
158#define CRD_DATA_WIDTH_SHIFT 0
159#define CRD_DATA_WIDTH_MASK 0x7
160#define CRD_WR_CAP_SHIFT 4
161#define CRD_WR_CAP_MASK 0x7
162#define CRD_WR_Q_DEP_SHIFT 8
163#define CRD_WR_Q_DEP_MASK 0xf
164#define CRD_RD_CAP_SHIFT 12
165#define CRD_RD_CAP_MASK 0x7
166#define CRD_RD_Q_DEP_SHIFT 16
167#define CRD_RD_Q_DEP_MASK 0xf
168#define CRD_DATA_BUFF_SHIFT 20
169#define CRD_DATA_BUFF_MASK 0x3ff
170
171#define PART 0x330
172#define DESIGNER 0x41
173#define REVISION 0x0
174#define INTEG_CFG 0x0
175#define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
176
Boojin Kimb7d861d2011-12-26 18:49:52 +0900177#define PL330_STATE_STOPPED (1 << 0)
178#define PL330_STATE_EXECUTING (1 << 1)
179#define PL330_STATE_WFE (1 << 2)
180#define PL330_STATE_FAULTING (1 << 3)
181#define PL330_STATE_COMPLETING (1 << 4)
182#define PL330_STATE_WFP (1 << 5)
183#define PL330_STATE_KILLING (1 << 6)
184#define PL330_STATE_FAULT_COMPLETING (1 << 7)
185#define PL330_STATE_CACHEMISS (1 << 8)
186#define PL330_STATE_UPDTPC (1 << 9)
187#define PL330_STATE_ATBARRIER (1 << 10)
188#define PL330_STATE_QUEUEBUSY (1 << 11)
189#define PL330_STATE_INVALID (1 << 15)
190
191#define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
192 | PL330_STATE_WFE | PL330_STATE_FAULTING)
193
194#define CMD_DMAADDH 0x54
195#define CMD_DMAEND 0x00
196#define CMD_DMAFLUSHP 0x35
197#define CMD_DMAGO 0xa0
198#define CMD_DMALD 0x04
199#define CMD_DMALDP 0x25
200#define CMD_DMALP 0x20
201#define CMD_DMALPEND 0x28
202#define CMD_DMAKILL 0x01
203#define CMD_DMAMOV 0xbc
204#define CMD_DMANOP 0x18
205#define CMD_DMARMB 0x12
206#define CMD_DMASEV 0x34
207#define CMD_DMAST 0x08
208#define CMD_DMASTP 0x29
209#define CMD_DMASTZ 0x0c
210#define CMD_DMAWFE 0x36
211#define CMD_DMAWFP 0x30
212#define CMD_DMAWMB 0x13
213
214#define SZ_DMAADDH 3
215#define SZ_DMAEND 1
216#define SZ_DMAFLUSHP 2
217#define SZ_DMALD 1
218#define SZ_DMALDP 2
219#define SZ_DMALP 2
220#define SZ_DMALPEND 2
221#define SZ_DMAKILL 1
222#define SZ_DMAMOV 6
223#define SZ_DMANOP 1
224#define SZ_DMARMB 1
225#define SZ_DMASEV 2
226#define SZ_DMAST 1
227#define SZ_DMASTP 2
228#define SZ_DMASTZ 1
229#define SZ_DMAWFE 2
230#define SZ_DMAWFP 2
231#define SZ_DMAWMB 1
232#define SZ_DMAGO 6
233
234#define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
235#define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
236
237#define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
238#define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
239
240/*
241 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
242 * at 1byte/burst for P<->M and M<->M respectively.
243 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
244 * should be enough for P<->M and M<->M respectively.
245 */
246#define MCODE_BUFF_PER_REQ 256
247
248/* If the _pl330_req is available to the client */
249#define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
250
251/* Use this _only_ to wait on transient states */
252#define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
253
254#ifdef PL330_DEBUG_MCGEN
255static unsigned cmd_line;
256#define PL330_DBGCMD_DUMP(off, x...) do { \
257 printk("%x:", cmd_line); \
258 printk(x); \
259 cmd_line += off; \
260 } while (0)
261#define PL330_DBGMC_START(addr) (cmd_line = addr)
262#else
263#define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
264#define PL330_DBGMC_START(addr) do {} while (0)
265#endif
266
267/* The number of default descriptors */
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +0000268
Jassi Brarb3040e42010-05-23 20:28:19 -0700269#define NR_DEFAULT_DESC 16
270
Boojin Kimb7d861d2011-12-26 18:49:52 +0900271/* Populated by the PL330 core driver for DMA API driver's info */
272struct pl330_config {
273 u32 periph_id;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900274#define DMAC_MODE_NS (1 << 0)
275 unsigned int mode;
276 unsigned int data_bus_width:10; /* In number of bits */
277 unsigned int data_buf_dep:10;
278 unsigned int num_chan:4;
279 unsigned int num_peri:6;
280 u32 peri_ns;
281 unsigned int num_events:6;
282 u32 irq_ns;
283};
284
Boojin Kimb7d861d2011-12-26 18:49:52 +0900285/**
286 * Request Configuration.
287 * The PL330 core does not modify this and uses the last
288 * working configuration if the request doesn't provide any.
289 *
290 * The Client may want to provide this info only for the
291 * first request and a request with new settings.
292 */
293struct pl330_reqcfg {
294 /* Address Incrementing */
295 unsigned dst_inc:1;
296 unsigned src_inc:1;
297
298 /*
299 * For now, the SRC & DST protection levels
300 * and burst size/length are assumed same.
301 */
302 bool nonsecure;
303 bool privileged;
304 bool insnaccess;
305 unsigned brst_len:5;
306 unsigned brst_size:3; /* in power of 2 */
307
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +0200308 enum pl330_cachectrl dcctl;
309 enum pl330_cachectrl scctl;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900310 enum pl330_byteswap swap;
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900311 struct pl330_config *pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900312};
313
314/*
315 * One cycle of DMAC operation.
316 * There may be more than one xfer in a request.
317 */
318struct pl330_xfer {
319 u32 src_addr;
320 u32 dst_addr;
321 /* Size to xfer */
322 u32 bytes;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900323};
324
325/* The xfer callbacks are made with one of these arguments. */
326enum pl330_op_err {
327 /* The all xfers in the request were success. */
328 PL330_ERR_NONE,
329 /* If req aborted due to global error. */
330 PL330_ERR_ABORT,
331 /* If req failed due to problem with Channel. */
332 PL330_ERR_FAIL,
333};
334
335/* A request defining Scatter-Gather List ending with NULL xfer. */
336struct pl330_req {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +0200337 enum dma_transfer_direction rqtype;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900338 /* Index of peripheral for the xfer. */
339 unsigned peri:5;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900340 /* If NULL, req will be done at last set parameters. */
341 struct pl330_reqcfg *cfg;
342 /* Pointer to first xfer in the request. */
343 struct pl330_xfer *x;
Javi Merinofdec53d2012-06-13 15:07:00 +0100344 /* Hook to attach to DMAC's list of reqs with due callback */
345 struct list_head rqd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900346};
347
Boojin Kimb7d861d2011-12-26 18:49:52 +0900348enum pl330_chan_op {
349 /* Start the channel */
350 PL330_OP_START,
351 /* Abort the active xfer */
352 PL330_OP_ABORT,
353 /* Stop xfer and flush queue */
354 PL330_OP_FLUSH,
355};
356
357struct _xfer_spec {
358 u32 ccr;
359 struct pl330_req *r;
360 struct pl330_xfer *x;
361};
362
363enum dmamov_dst {
364 SAR = 0,
365 CCR,
366 DAR,
367};
368
369enum pl330_dst {
370 SRC = 0,
371 DST,
372};
373
374enum pl330_cond {
375 SINGLE,
376 BURST,
377 ALWAYS,
378};
379
380struct _pl330_req {
381 u32 mc_bus;
382 void *mc_cpu;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900383 struct pl330_req *r;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900384};
385
386/* ToBeDone for tasklet */
387struct _pl330_tbd {
388 bool reset_dmac;
389 bool reset_mngr;
390 u8 reset_chan;
391};
392
393/* A DMAC Thread */
394struct pl330_thread {
395 u8 id;
396 int ev;
397 /* If the channel is not yet acquired by any client */
398 bool free;
399 /* Parent DMAC */
400 struct pl330_dmac *dmac;
401 /* Only two at a time */
402 struct _pl330_req req[2];
403 /* Index of the last enqueued request */
404 unsigned lstenq;
405 /* Index of the last submitted request or -1 if the DMA is stopped */
406 int req_running;
407};
408
409enum pl330_dmac_state {
410 UNINIT,
411 INIT,
412 DYING,
413};
414
Jassi Brarb3040e42010-05-23 20:28:19 -0700415enum desc_status {
416 /* In the DMAC pool */
417 FREE,
418 /*
Masanari Iidad73111c2012-08-04 23:37:53 +0900419 * Allocated to some channel during prep_xxx
Jassi Brarb3040e42010-05-23 20:28:19 -0700420 * Also may be sitting on the work_list.
421 */
422 PREP,
423 /*
424 * Sitting on the work_list and already submitted
425 * to the PL330 core. Not more than two descriptors
426 * of a channel can be BUSY at any time.
427 */
428 BUSY,
429 /*
430 * Sitting on the channel work_list but xfer done
431 * by PL330 core
432 */
433 DONE,
434};
435
436struct dma_pl330_chan {
437 /* Schedule desc completion */
438 struct tasklet_struct task;
439
440 /* DMA-Engine Channel */
441 struct dma_chan chan;
442
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +0100443 /* List of submitted descriptors */
444 struct list_head submitted_list;
445 /* List of issued descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700446 struct list_head work_list;
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +0200447 /* List of completed descriptors */
448 struct list_head completed_list;
Jassi Brarb3040e42010-05-23 20:28:19 -0700449
450 /* Pointer to the DMAC that manages this channel,
451 * NULL if the channel is available to be acquired.
452 * As the parent, this DMAC also provides descriptors
453 * to the channel.
454 */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200455 struct pl330_dmac *dmac;
Jassi Brarb3040e42010-05-23 20:28:19 -0700456
457 /* To protect channel manipulation */
458 spinlock_t lock;
459
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200460 /*
461 * Hardware channel thread of PL330 DMAC. NULL if the channel is
462 * available.
Jassi Brarb3040e42010-05-23 20:28:19 -0700463 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200464 struct pl330_thread *thread;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900465
466 /* For D-to-M and M-to-D channels */
467 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900468 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900469 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900470
471 /* for cyclic capability */
472 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700473};
474
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200475struct pl330_dmac {
Jassi Brarb3040e42010-05-23 20:28:19 -0700476 /* DMA-Engine Device */
477 struct dma_device ddma;
478
Lars-Peter Clausenb714b842013-11-25 16:07:46 +0100479 /* Holds info about sg limitations */
480 struct device_dma_parameters dma_parms;
481
Jassi Brarb3040e42010-05-23 20:28:19 -0700482 /* Pool of descriptors available for the DMAC's channels */
483 struct list_head desc_pool;
484 /* To protect desc_pool manipulation */
485 spinlock_t pool_lock;
486
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200487 /* Size of MicroCode buffers for each channel. */
488 unsigned mcbufsz;
489 /* ioremap'ed address of PL330 registers. */
490 void __iomem *base;
491 /* Populated by the PL330 core driver during pl330_add */
492 struct pl330_config pcfg;
493
494 spinlock_t lock;
495 /* Maximum possible events/irqs */
496 int events[32];
497 /* BUS address of MicroCode buffer */
498 dma_addr_t mcode_bus;
499 /* CPU address of MicroCode buffer */
500 void *mcode_cpu;
501 /* List of all Channel threads */
502 struct pl330_thread *channels;
503 /* Pointer to the MANAGER thread */
504 struct pl330_thread *manager;
505 /* To handle bad news in interrupt */
506 struct tasklet_struct tasks;
507 struct _pl330_tbd dmac_tbd;
508 /* State of DMAC operation */
509 enum pl330_dmac_state state;
510 /* Holds list of reqs with due callbacks */
511 struct list_head req_done;
512
Jassi Brarb3040e42010-05-23 20:28:19 -0700513 /* Peripheral channels connected to this DMAC */
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +0100514 unsigned int num_peripherals;
Rob Herring4e0e6102011-07-25 16:05:04 -0500515 struct dma_pl330_chan *peripherals; /* keep at end */
Jassi Brarb3040e42010-05-23 20:28:19 -0700516};
517
518struct dma_pl330_desc {
519 /* To attach to a queue as child */
520 struct list_head node;
521
522 /* Descriptor for the DMA Engine API */
523 struct dma_async_tx_descriptor txd;
524
525 /* Xfer for PL330 core */
526 struct pl330_xfer px;
527
528 struct pl330_reqcfg rqcfg;
529 struct pl330_req req;
530
531 enum desc_status status;
532
533 /* The channel which currently holds this desc */
534 struct dma_pl330_chan *pchan;
535};
536
Boojin Kimb7d861d2011-12-26 18:49:52 +0900537static inline bool _queue_empty(struct pl330_thread *thrd)
538{
539 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
540 ? true : false;
541}
542
543static inline bool _queue_full(struct pl330_thread *thrd)
544{
545 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
546 ? false : true;
547}
548
549static inline bool is_manager(struct pl330_thread *thrd)
550{
Lars-Peter Clausenfbbcd9b2014-07-06 20:32:28 +0200551 return thrd->dmac->manager == thrd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900552}
553
554/* If manager of the thread is in Non-Secure mode */
555static inline bool _manager_ns(struct pl330_thread *thrd)
556{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200557 return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900558}
559
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900560static inline u32 get_revision(u32 periph_id)
561{
562 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
563}
564
Boojin Kimb7d861d2011-12-26 18:49:52 +0900565static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
566 enum pl330_dst da, u16 val)
567{
568 if (dry_run)
569 return SZ_DMAADDH;
570
571 buf[0] = CMD_DMAADDH;
572 buf[0] |= (da << 1);
573 *((u16 *)&buf[1]) = val;
574
575 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
576 da == 1 ? "DA" : "SA", val);
577
578 return SZ_DMAADDH;
579}
580
581static inline u32 _emit_END(unsigned dry_run, u8 buf[])
582{
583 if (dry_run)
584 return SZ_DMAEND;
585
586 buf[0] = CMD_DMAEND;
587
588 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
589
590 return SZ_DMAEND;
591}
592
593static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
594{
595 if (dry_run)
596 return SZ_DMAFLUSHP;
597
598 buf[0] = CMD_DMAFLUSHP;
599
600 peri &= 0x1f;
601 peri <<= 3;
602 buf[1] = peri;
603
604 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
605
606 return SZ_DMAFLUSHP;
607}
608
609static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
610{
611 if (dry_run)
612 return SZ_DMALD;
613
614 buf[0] = CMD_DMALD;
615
616 if (cond == SINGLE)
617 buf[0] |= (0 << 1) | (1 << 0);
618 else if (cond == BURST)
619 buf[0] |= (1 << 1) | (1 << 0);
620
621 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
622 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
623
624 return SZ_DMALD;
625}
626
627static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
628 enum pl330_cond cond, u8 peri)
629{
630 if (dry_run)
631 return SZ_DMALDP;
632
633 buf[0] = CMD_DMALDP;
634
635 if (cond == BURST)
636 buf[0] |= (1 << 1);
637
638 peri &= 0x1f;
639 peri <<= 3;
640 buf[1] = peri;
641
642 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
643 cond == SINGLE ? 'S' : 'B', peri >> 3);
644
645 return SZ_DMALDP;
646}
647
648static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
649 unsigned loop, u8 cnt)
650{
651 if (dry_run)
652 return SZ_DMALP;
653
654 buf[0] = CMD_DMALP;
655
656 if (loop)
657 buf[0] |= (1 << 1);
658
659 cnt--; /* DMAC increments by 1 internally */
660 buf[1] = cnt;
661
662 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
663
664 return SZ_DMALP;
665}
666
667struct _arg_LPEND {
668 enum pl330_cond cond;
669 bool forever;
670 unsigned loop;
671 u8 bjump;
672};
673
674static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
675 const struct _arg_LPEND *arg)
676{
677 enum pl330_cond cond = arg->cond;
678 bool forever = arg->forever;
679 unsigned loop = arg->loop;
680 u8 bjump = arg->bjump;
681
682 if (dry_run)
683 return SZ_DMALPEND;
684
685 buf[0] = CMD_DMALPEND;
686
687 if (loop)
688 buf[0] |= (1 << 2);
689
690 if (!forever)
691 buf[0] |= (1 << 4);
692
693 if (cond == SINGLE)
694 buf[0] |= (0 << 1) | (1 << 0);
695 else if (cond == BURST)
696 buf[0] |= (1 << 1) | (1 << 0);
697
698 buf[1] = bjump;
699
700 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
701 forever ? "FE" : "END",
702 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
703 loop ? '1' : '0',
704 bjump);
705
706 return SZ_DMALPEND;
707}
708
709static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
710{
711 if (dry_run)
712 return SZ_DMAKILL;
713
714 buf[0] = CMD_DMAKILL;
715
716 return SZ_DMAKILL;
717}
718
719static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
720 enum dmamov_dst dst, u32 val)
721{
722 if (dry_run)
723 return SZ_DMAMOV;
724
725 buf[0] = CMD_DMAMOV;
726 buf[1] = dst;
727 *((u32 *)&buf[2]) = val;
728
729 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
730 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
731
732 return SZ_DMAMOV;
733}
734
735static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
736{
737 if (dry_run)
738 return SZ_DMANOP;
739
740 buf[0] = CMD_DMANOP;
741
742 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
743
744 return SZ_DMANOP;
745}
746
747static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
748{
749 if (dry_run)
750 return SZ_DMARMB;
751
752 buf[0] = CMD_DMARMB;
753
754 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
755
756 return SZ_DMARMB;
757}
758
759static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
760{
761 if (dry_run)
762 return SZ_DMASEV;
763
764 buf[0] = CMD_DMASEV;
765
766 ev &= 0x1f;
767 ev <<= 3;
768 buf[1] = ev;
769
770 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
771
772 return SZ_DMASEV;
773}
774
775static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
776{
777 if (dry_run)
778 return SZ_DMAST;
779
780 buf[0] = CMD_DMAST;
781
782 if (cond == SINGLE)
783 buf[0] |= (0 << 1) | (1 << 0);
784 else if (cond == BURST)
785 buf[0] |= (1 << 1) | (1 << 0);
786
787 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
788 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
789
790 return SZ_DMAST;
791}
792
793static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
794 enum pl330_cond cond, u8 peri)
795{
796 if (dry_run)
797 return SZ_DMASTP;
798
799 buf[0] = CMD_DMASTP;
800
801 if (cond == BURST)
802 buf[0] |= (1 << 1);
803
804 peri &= 0x1f;
805 peri <<= 3;
806 buf[1] = peri;
807
808 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
809 cond == SINGLE ? 'S' : 'B', peri >> 3);
810
811 return SZ_DMASTP;
812}
813
814static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
815{
816 if (dry_run)
817 return SZ_DMASTZ;
818
819 buf[0] = CMD_DMASTZ;
820
821 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
822
823 return SZ_DMASTZ;
824}
825
826static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
827 unsigned invalidate)
828{
829 if (dry_run)
830 return SZ_DMAWFE;
831
832 buf[0] = CMD_DMAWFE;
833
834 ev &= 0x1f;
835 ev <<= 3;
836 buf[1] = ev;
837
838 if (invalidate)
839 buf[1] |= (1 << 1);
840
841 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
842 ev >> 3, invalidate ? ", I" : "");
843
844 return SZ_DMAWFE;
845}
846
847static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
848 enum pl330_cond cond, u8 peri)
849{
850 if (dry_run)
851 return SZ_DMAWFP;
852
853 buf[0] = CMD_DMAWFP;
854
855 if (cond == SINGLE)
856 buf[0] |= (0 << 1) | (0 << 0);
857 else if (cond == BURST)
858 buf[0] |= (1 << 1) | (0 << 0);
859 else
860 buf[0] |= (0 << 1) | (1 << 0);
861
862 peri &= 0x1f;
863 peri <<= 3;
864 buf[1] = peri;
865
866 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
867 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
868
869 return SZ_DMAWFP;
870}
871
872static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
873{
874 if (dry_run)
875 return SZ_DMAWMB;
876
877 buf[0] = CMD_DMAWMB;
878
879 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
880
881 return SZ_DMAWMB;
882}
883
884struct _arg_GO {
885 u8 chan;
886 u32 addr;
887 unsigned ns;
888};
889
890static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
891 const struct _arg_GO *arg)
892{
893 u8 chan = arg->chan;
894 u32 addr = arg->addr;
895 unsigned ns = arg->ns;
896
897 if (dry_run)
898 return SZ_DMAGO;
899
900 buf[0] = CMD_DMAGO;
901 buf[0] |= (ns << 1);
902
903 buf[1] = chan & 0x7;
904
905 *((u32 *)&buf[2]) = addr;
906
907 return SZ_DMAGO;
908}
909
910#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
911
912/* Returns Time-Out */
913static bool _until_dmac_idle(struct pl330_thread *thrd)
914{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200915 void __iomem *regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900916 unsigned long loops = msecs_to_loops(5);
917
918 do {
919 /* Until Manager is Idle */
920 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
921 break;
922
923 cpu_relax();
924 } while (--loops);
925
926 if (!loops)
927 return true;
928
929 return false;
930}
931
932static inline void _execute_DBGINSN(struct pl330_thread *thrd,
933 u8 insn[], bool as_manager)
934{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200935 void __iomem *regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900936 u32 val;
937
938 val = (insn[0] << 16) | (insn[1] << 24);
939 if (!as_manager) {
940 val |= (1 << 0);
941 val |= (thrd->id << 8); /* Channel Number */
942 }
943 writel(val, regs + DBGINST0);
944
945 val = *((u32 *)&insn[2]);
946 writel(val, regs + DBGINST1);
947
948 /* If timed out due to halted state-machine */
949 if (_until_dmac_idle(thrd)) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200950 dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
Boojin Kimb7d861d2011-12-26 18:49:52 +0900951 return;
952 }
953
954 /* Get going */
955 writel(0, regs + DBGCMD);
956}
957
958/*
959 * Mark a _pl330_req as free.
960 * We do it by writing DMAEND as the first instruction
961 * because no valid request is going to have DMAEND as
962 * its first instruction to execute.
963 */
964static void mark_free(struct pl330_thread *thrd, int idx)
965{
966 struct _pl330_req *req = &thrd->req[idx];
967
968 _emit_END(0, req->mc_cpu);
Boojin Kimb7d861d2011-12-26 18:49:52 +0900969
970 thrd->req_running = -1;
971}
972
973static inline u32 _state(struct pl330_thread *thrd)
974{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +0200975 void __iomem *regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900976 u32 val;
977
978 if (is_manager(thrd))
979 val = readl(regs + DS) & 0xf;
980 else
981 val = readl(regs + CS(thrd->id)) & 0xf;
982
983 switch (val) {
984 case DS_ST_STOP:
985 return PL330_STATE_STOPPED;
986 case DS_ST_EXEC:
987 return PL330_STATE_EXECUTING;
988 case DS_ST_CMISS:
989 return PL330_STATE_CACHEMISS;
990 case DS_ST_UPDTPC:
991 return PL330_STATE_UPDTPC;
992 case DS_ST_WFE:
993 return PL330_STATE_WFE;
994 case DS_ST_FAULT:
995 return PL330_STATE_FAULTING;
996 case DS_ST_ATBRR:
997 if (is_manager(thrd))
998 return PL330_STATE_INVALID;
999 else
1000 return PL330_STATE_ATBARRIER;
1001 case DS_ST_QBUSY:
1002 if (is_manager(thrd))
1003 return PL330_STATE_INVALID;
1004 else
1005 return PL330_STATE_QUEUEBUSY;
1006 case DS_ST_WFP:
1007 if (is_manager(thrd))
1008 return PL330_STATE_INVALID;
1009 else
1010 return PL330_STATE_WFP;
1011 case DS_ST_KILL:
1012 if (is_manager(thrd))
1013 return PL330_STATE_INVALID;
1014 else
1015 return PL330_STATE_KILLING;
1016 case DS_ST_CMPLT:
1017 if (is_manager(thrd))
1018 return PL330_STATE_INVALID;
1019 else
1020 return PL330_STATE_COMPLETING;
1021 case DS_ST_FLTCMP:
1022 if (is_manager(thrd))
1023 return PL330_STATE_INVALID;
1024 else
1025 return PL330_STATE_FAULT_COMPLETING;
1026 default:
1027 return PL330_STATE_INVALID;
1028 }
1029}
1030
1031static void _stop(struct pl330_thread *thrd)
1032{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001033 void __iomem *regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001034 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1035
1036 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1037 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1038
1039 /* Return if nothing needs to be done */
1040 if (_state(thrd) == PL330_STATE_COMPLETING
1041 || _state(thrd) == PL330_STATE_KILLING
1042 || _state(thrd) == PL330_STATE_STOPPED)
1043 return;
1044
1045 _emit_KILL(0, insn);
1046
1047 /* Stop generating interrupts for SEV */
1048 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1049
1050 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1051}
1052
1053/* Start doing req 'idx' of thread 'thrd' */
1054static bool _trigger(struct pl330_thread *thrd)
1055{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001056 void __iomem *regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001057 struct _pl330_req *req;
1058 struct pl330_req *r;
1059 struct _arg_GO go;
1060 unsigned ns;
1061 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1062 int idx;
1063
1064 /* Return if already ACTIVE */
1065 if (_state(thrd) != PL330_STATE_STOPPED)
1066 return true;
1067
1068 idx = 1 - thrd->lstenq;
1069 if (!IS_FREE(&thrd->req[idx]))
1070 req = &thrd->req[idx];
1071 else {
1072 idx = thrd->lstenq;
1073 if (!IS_FREE(&thrd->req[idx]))
1074 req = &thrd->req[idx];
1075 else
1076 req = NULL;
1077 }
1078
1079 /* Return if no request */
1080 if (!req || !req->r)
1081 return true;
1082
1083 r = req->r;
1084
1085 if (r->cfg)
1086 ns = r->cfg->nonsecure ? 1 : 0;
1087 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1088 ns = 1;
1089 else
1090 ns = 0;
1091
1092 /* See 'Abort Sources' point-4 at Page 2-25 */
1093 if (_manager_ns(thrd) && !ns)
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001094 dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001095 __func__, __LINE__);
1096
1097 go.chan = thrd->id;
1098 go.addr = req->mc_bus;
1099 go.ns = ns;
1100 _emit_GO(0, insn, &go);
1101
1102 /* Set to generate interrupts for SEV */
1103 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1104
1105 /* Only manager can execute GO */
1106 _execute_DBGINSN(thrd, insn, true);
1107
1108 thrd->req_running = idx;
1109
1110 return true;
1111}
1112
1113static bool _start(struct pl330_thread *thrd)
1114{
1115 switch (_state(thrd)) {
1116 case PL330_STATE_FAULT_COMPLETING:
1117 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1118
1119 if (_state(thrd) == PL330_STATE_KILLING)
1120 UNTIL(thrd, PL330_STATE_STOPPED)
1121
1122 case PL330_STATE_FAULTING:
1123 _stop(thrd);
1124
1125 case PL330_STATE_KILLING:
1126 case PL330_STATE_COMPLETING:
1127 UNTIL(thrd, PL330_STATE_STOPPED)
1128
1129 case PL330_STATE_STOPPED:
1130 return _trigger(thrd);
1131
1132 case PL330_STATE_WFP:
1133 case PL330_STATE_QUEUEBUSY:
1134 case PL330_STATE_ATBARRIER:
1135 case PL330_STATE_UPDTPC:
1136 case PL330_STATE_CACHEMISS:
1137 case PL330_STATE_EXECUTING:
1138 return true;
1139
1140 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1141 default:
1142 return false;
1143 }
1144}
1145
1146static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1147 const struct _xfer_spec *pxs, int cyc)
1148{
1149 int off = 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001150 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001151
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001152 /* check lock-up free version */
1153 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1154 while (cyc--) {
1155 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1156 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1157 }
1158 } else {
1159 while (cyc--) {
1160 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1161 off += _emit_RMB(dry_run, &buf[off]);
1162 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1163 off += _emit_WMB(dry_run, &buf[off]);
1164 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001165 }
1166
1167 return off;
1168}
1169
1170static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1171 const struct _xfer_spec *pxs, int cyc)
1172{
1173 int off = 0;
1174
1175 while (cyc--) {
1176 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1177 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1178 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1179 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1180 }
1181
1182 return off;
1183}
1184
1185static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1186 const struct _xfer_spec *pxs, int cyc)
1187{
1188 int off = 0;
1189
1190 while (cyc--) {
1191 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1192 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1193 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1194 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1195 }
1196
1197 return off;
1198}
1199
1200static int _bursts(unsigned dry_run, u8 buf[],
1201 const struct _xfer_spec *pxs, int cyc)
1202{
1203 int off = 0;
1204
1205 switch (pxs->r->rqtype) {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001206 case DMA_MEM_TO_DEV:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001207 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1208 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001209 case DMA_DEV_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001210 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1211 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001212 case DMA_MEM_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001213 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1214 break;
1215 default:
1216 off += 0x40000000; /* Scare off the Client */
1217 break;
1218 }
1219
1220 return off;
1221}
1222
1223/* Returns bytes consumed and updates bursts */
1224static inline int _loop(unsigned dry_run, u8 buf[],
1225 unsigned long *bursts, const struct _xfer_spec *pxs)
1226{
1227 int cyc, cycmax, szlp, szlpend, szbrst, off;
1228 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1229 struct _arg_LPEND lpend;
1230
1231 /* Max iterations possible in DMALP is 256 */
1232 if (*bursts >= 256*256) {
1233 lcnt1 = 256;
1234 lcnt0 = 256;
1235 cyc = *bursts / lcnt1 / lcnt0;
1236 } else if (*bursts > 256) {
1237 lcnt1 = 256;
1238 lcnt0 = *bursts / lcnt1;
1239 cyc = 1;
1240 } else {
1241 lcnt1 = *bursts;
1242 lcnt0 = 0;
1243 cyc = 1;
1244 }
1245
1246 szlp = _emit_LP(1, buf, 0, 0);
1247 szbrst = _bursts(1, buf, pxs, 1);
1248
1249 lpend.cond = ALWAYS;
1250 lpend.forever = false;
1251 lpend.loop = 0;
1252 lpend.bjump = 0;
1253 szlpend = _emit_LPEND(1, buf, &lpend);
1254
1255 if (lcnt0) {
1256 szlp *= 2;
1257 szlpend *= 2;
1258 }
1259
1260 /*
1261 * Max bursts that we can unroll due to limit on the
1262 * size of backward jump that can be encoded in DMALPEND
1263 * which is 8-bits and hence 255
1264 */
1265 cycmax = (255 - (szlp + szlpend)) / szbrst;
1266
1267 cyc = (cycmax < cyc) ? cycmax : cyc;
1268
1269 off = 0;
1270
1271 if (lcnt0) {
1272 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1273 ljmp0 = off;
1274 }
1275
1276 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1277 ljmp1 = off;
1278
1279 off += _bursts(dry_run, &buf[off], pxs, cyc);
1280
1281 lpend.cond = ALWAYS;
1282 lpend.forever = false;
1283 lpend.loop = 1;
1284 lpend.bjump = off - ljmp1;
1285 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1286
1287 if (lcnt0) {
1288 lpend.cond = ALWAYS;
1289 lpend.forever = false;
1290 lpend.loop = 0;
1291 lpend.bjump = off - ljmp0;
1292 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1293 }
1294
1295 *bursts = lcnt1 * cyc;
1296 if (lcnt0)
1297 *bursts *= lcnt0;
1298
1299 return off;
1300}
1301
1302static inline int _setup_loops(unsigned dry_run, u8 buf[],
1303 const struct _xfer_spec *pxs)
1304{
1305 struct pl330_xfer *x = pxs->x;
1306 u32 ccr = pxs->ccr;
1307 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1308 int off = 0;
1309
1310 while (bursts) {
1311 c = bursts;
1312 off += _loop(dry_run, &buf[off], &c, pxs);
1313 bursts -= c;
1314 }
1315
1316 return off;
1317}
1318
1319static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1320 const struct _xfer_spec *pxs)
1321{
1322 struct pl330_xfer *x = pxs->x;
1323 int off = 0;
1324
1325 /* DMAMOV SAR, x->src_addr */
1326 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1327 /* DMAMOV DAR, x->dst_addr */
1328 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1329
1330 /* Setup Loop(s) */
1331 off += _setup_loops(dry_run, &buf[off], pxs);
1332
1333 return off;
1334}
1335
1336/*
1337 * A req is a sequence of one or more xfer units.
1338 * Returns the number of bytes taken to setup the MC for the req.
1339 */
1340static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1341 unsigned index, struct _xfer_spec *pxs)
1342{
1343 struct _pl330_req *req = &thrd->req[index];
1344 struct pl330_xfer *x;
1345 u8 *buf = req->mc_cpu;
1346 int off = 0;
1347
1348 PL330_DBGMC_START(req->mc_bus);
1349
1350 /* DMAMOV CCR, ccr */
1351 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1352
1353 x = pxs->r->x;
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001354 /* Error if xfer length is not aligned at burst size */
1355 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1356 return -EINVAL;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001357
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001358 pxs->x = x;
1359 off += _setup_xfer(dry_run, &buf[off], pxs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001360
1361 /* DMASEV peripheral/event */
1362 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1363 /* DMAEND */
1364 off += _emit_END(dry_run, &buf[off]);
1365
1366 return off;
1367}
1368
1369static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1370{
1371 u32 ccr = 0;
1372
1373 if (rqc->src_inc)
1374 ccr |= CC_SRCINC;
1375
1376 if (rqc->dst_inc)
1377 ccr |= CC_DSTINC;
1378
1379 /* We set same protection levels for Src and DST for now */
1380 if (rqc->privileged)
1381 ccr |= CC_SRCPRI | CC_DSTPRI;
1382 if (rqc->nonsecure)
1383 ccr |= CC_SRCNS | CC_DSTNS;
1384 if (rqc->insnaccess)
1385 ccr |= CC_SRCIA | CC_DSTIA;
1386
1387 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1388 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1389
1390 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1391 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1392
1393 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1394 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1395
1396 ccr |= (rqc->swap << CC_SWAP_SHFT);
1397
1398 return ccr;
1399}
1400
Boojin Kimb7d861d2011-12-26 18:49:52 +09001401/*
1402 * Submit a list of xfers after which the client wants notification.
1403 * Client is not notified after each xfer unit, just once after all
1404 * xfer units are done or some error occurs.
1405 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001406static int pl330_submit_req(struct pl330_thread *thrd, struct pl330_req *r)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001407{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001408 struct pl330_dmac *pl330 = thrd->dmac;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001409 struct _xfer_spec xs;
1410 unsigned long flags;
1411 void __iomem *regs;
1412 unsigned idx;
1413 u32 ccr;
1414 int ret = 0;
1415
1416 /* No Req or Unacquired Channel or DMAC */
1417 if (!r || !thrd || thrd->free)
1418 return -EINVAL;
1419
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001420 regs = thrd->dmac->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001421
1422 if (pl330->state == DYING
1423 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001424 dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001425 __func__, __LINE__);
1426 return -EAGAIN;
1427 }
1428
1429 /* If request for non-existing peripheral */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001430 if (r->rqtype != DMA_MEM_TO_MEM && r->peri >= pl330->pcfg.num_peri) {
1431 dev_info(thrd->dmac->ddma.dev,
Boojin Kimb7d861d2011-12-26 18:49:52 +09001432 "%s:%d Invalid peripheral(%u)!\n",
1433 __func__, __LINE__, r->peri);
1434 return -EINVAL;
1435 }
1436
1437 spin_lock_irqsave(&pl330->lock, flags);
1438
1439 if (_queue_full(thrd)) {
1440 ret = -EAGAIN;
1441 goto xfer_exit;
1442 }
1443
Boojin Kimb7d861d2011-12-26 18:49:52 +09001444
1445 /* Use last settings, if not provided */
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301446 if (r->cfg) {
1447 /* Prefer Secure Channel */
1448 if (!_manager_ns(thrd))
1449 r->cfg->nonsecure = 0;
1450 else
1451 r->cfg->nonsecure = 1;
1452
Boojin Kimb7d861d2011-12-26 18:49:52 +09001453 ccr = _prepare_ccr(r->cfg);
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301454 } else {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001455 ccr = readl(regs + CC(thrd->id));
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301456 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001457
Boojin Kimb7d861d2011-12-26 18:49:52 +09001458 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1459
1460 xs.ccr = ccr;
1461 xs.r = r;
1462
1463 /* First dry run to check if req is acceptable */
1464 ret = _setup_req(1, thrd, idx, &xs);
1465 if (ret < 0)
1466 goto xfer_exit;
1467
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001468 if (ret > pl330->mcbufsz / 2) {
1469 dev_info(pl330->ddma.dev, "%s:%d Trying increasing mcbufsz\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001470 __func__, __LINE__);
1471 ret = -ENOMEM;
1472 goto xfer_exit;
1473 }
1474
1475 /* Hook the request */
1476 thrd->lstenq = idx;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001477 thrd->req[idx].r = r;
Lars-Peter Clausenbe025322014-07-06 20:32:24 +02001478 _setup_req(0, thrd, idx, &xs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001479
1480 ret = 0;
1481
1482xfer_exit:
1483 spin_unlock_irqrestore(&pl330->lock, flags);
1484
1485 return ret;
1486}
1487
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001488static void dma_pl330_rqcb(struct pl330_req *req, enum pl330_op_err err)
1489{
1490 struct dma_pl330_desc *desc = container_of(req, struct dma_pl330_desc, req);
1491 struct dma_pl330_chan *pch = desc->pchan;
1492 unsigned long flags;
1493
1494 /* If desc aborted */
1495 if (!pch)
1496 return;
1497
1498 spin_lock_irqsave(&pch->lock, flags);
1499
1500 desc->status = DONE;
1501
1502 spin_unlock_irqrestore(&pch->lock, flags);
1503
1504 tasklet_schedule(&pch->task);
1505}
1506
Boojin Kimb7d861d2011-12-26 18:49:52 +09001507static void pl330_dotask(unsigned long data)
1508{
1509 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001510 unsigned long flags;
1511 int i;
1512
1513 spin_lock_irqsave(&pl330->lock, flags);
1514
1515 /* The DMAC itself gone nuts */
1516 if (pl330->dmac_tbd.reset_dmac) {
1517 pl330->state = DYING;
1518 /* Reset the manager too */
1519 pl330->dmac_tbd.reset_mngr = true;
1520 /* Clear the reset flag */
1521 pl330->dmac_tbd.reset_dmac = false;
1522 }
1523
1524 if (pl330->dmac_tbd.reset_mngr) {
1525 _stop(pl330->manager);
1526 /* Reset all channels */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001527 pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001528 /* Clear the reset flag */
1529 pl330->dmac_tbd.reset_mngr = false;
1530 }
1531
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001532 for (i = 0; i < pl330->pcfg.num_chan; i++) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001533
1534 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1535 struct pl330_thread *thrd = &pl330->channels[i];
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001536 void __iomem *regs = pl330->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001537 enum pl330_op_err err;
1538
1539 _stop(thrd);
1540
1541 if (readl(regs + FSC) & (1 << thrd->id))
1542 err = PL330_ERR_FAIL;
1543 else
1544 err = PL330_ERR_ABORT;
1545
1546 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001547 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, err);
1548 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, err);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001549 spin_lock_irqsave(&pl330->lock, flags);
1550
1551 thrd->req[0].r = NULL;
1552 thrd->req[1].r = NULL;
1553 mark_free(thrd, 0);
1554 mark_free(thrd, 1);
1555
1556 /* Clear the reset flag */
1557 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1558 }
1559 }
1560
1561 spin_unlock_irqrestore(&pl330->lock, flags);
1562
1563 return;
1564}
1565
1566/* Returns 1 if state was updated, 0 otherwise */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001567static int pl330_update(struct pl330_dmac *pl330)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001568{
Javi Merinofdec53d2012-06-13 15:07:00 +01001569 struct pl330_req *rqdone, *tmp;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001570 unsigned long flags;
1571 void __iomem *regs;
1572 u32 val;
1573 int id, ev, ret = 0;
1574
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001575 regs = pl330->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001576
1577 spin_lock_irqsave(&pl330->lock, flags);
1578
1579 val = readl(regs + FSM) & 0x1;
1580 if (val)
1581 pl330->dmac_tbd.reset_mngr = true;
1582 else
1583 pl330->dmac_tbd.reset_mngr = false;
1584
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001585 val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001586 pl330->dmac_tbd.reset_chan |= val;
1587 if (val) {
1588 int i = 0;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001589 while (i < pl330->pcfg.num_chan) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001590 if (val & (1 << i)) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001591 dev_info(pl330->ddma.dev,
Boojin Kimb7d861d2011-12-26 18:49:52 +09001592 "Reset Channel-%d\t CS-%x FTC-%x\n",
1593 i, readl(regs + CS(i)),
1594 readl(regs + FTC(i)));
1595 _stop(&pl330->channels[i]);
1596 }
1597 i++;
1598 }
1599 }
1600
1601 /* Check which event happened i.e, thread notified */
1602 val = readl(regs + ES);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001603 if (pl330->pcfg.num_events < 32
1604 && val & ~((1 << pl330->pcfg.num_events) - 1)) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001605 pl330->dmac_tbd.reset_dmac = true;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001606 dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
1607 __LINE__);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001608 ret = 1;
1609 goto updt_exit;
1610 }
1611
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001612 for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001613 if (val & (1 << ev)) { /* Event occurred */
1614 struct pl330_thread *thrd;
1615 u32 inten = readl(regs + INTEN);
1616 int active;
1617
1618 /* Clear the event */
1619 if (inten & (1 << ev))
1620 writel(1 << ev, regs + INTCLR);
1621
1622 ret = 1;
1623
1624 id = pl330->events[ev];
1625
1626 thrd = &pl330->channels[id];
1627
1628 active = thrd->req_running;
1629 if (active == -1) /* Aborted */
1630 continue;
1631
Javi Merinofdec53d2012-06-13 15:07:00 +01001632 /* Detach the req */
1633 rqdone = thrd->req[active].r;
1634 thrd->req[active].r = NULL;
1635
Boojin Kimb7d861d2011-12-26 18:49:52 +09001636 mark_free(thrd, active);
1637
1638 /* Get going again ASAP */
1639 _start(thrd);
1640
1641 /* For now, just make a list of callbacks to be done */
1642 list_add_tail(&rqdone->rqd, &pl330->req_done);
1643 }
1644 }
1645
1646 /* Now that we are in no hurry, do the callbacks */
Javi Merinofdec53d2012-06-13 15:07:00 +01001647 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1648 list_del(&rqdone->rqd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001649
1650 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001651 dma_pl330_rqcb(rqdone, PL330_ERR_NONE);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001652 spin_lock_irqsave(&pl330->lock, flags);
1653 }
1654
1655updt_exit:
1656 spin_unlock_irqrestore(&pl330->lock, flags);
1657
1658 if (pl330->dmac_tbd.reset_dmac
1659 || pl330->dmac_tbd.reset_mngr
1660 || pl330->dmac_tbd.reset_chan) {
1661 ret = 1;
1662 tasklet_schedule(&pl330->tasks);
1663 }
1664
1665 return ret;
1666}
1667
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001668static int pl330_chan_ctrl(struct pl330_thread *thrd, enum pl330_chan_op op)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001669{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001670 struct pl330_dmac *pl330;
1671 unsigned long flags;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001672 int ret = 0, active;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001673
1674 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1675 return -EINVAL;
1676
1677 pl330 = thrd->dmac;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001678 active = thrd->req_running;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001679
1680 spin_lock_irqsave(&pl330->lock, flags);
1681
1682 switch (op) {
1683 case PL330_OP_FLUSH:
1684 /* Make sure the channel is stopped */
1685 _stop(thrd);
1686
1687 thrd->req[0].r = NULL;
1688 thrd->req[1].r = NULL;
1689 mark_free(thrd, 0);
1690 mark_free(thrd, 1);
1691 break;
1692
1693 case PL330_OP_ABORT:
1694 /* Make sure the channel is stopped */
1695 _stop(thrd);
1696
1697 /* ABORT is only for the active req */
1698 if (active == -1)
1699 break;
1700
1701 thrd->req[active].r = NULL;
1702 mark_free(thrd, active);
1703
1704 /* Start the next */
1705 case PL330_OP_START:
1706 if ((active == -1) && !_start(thrd))
1707 ret = -EIO;
1708 break;
1709
1710 default:
1711 ret = -EINVAL;
1712 }
1713
1714 spin_unlock_irqrestore(&pl330->lock, flags);
1715 return ret;
1716}
1717
Boojin Kimb7d861d2011-12-26 18:49:52 +09001718/* Reserve an event */
1719static inline int _alloc_event(struct pl330_thread *thrd)
1720{
1721 struct pl330_dmac *pl330 = thrd->dmac;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001722 int ev;
1723
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001724 for (ev = 0; ev < pl330->pcfg.num_events; ev++)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001725 if (pl330->events[ev] == -1) {
1726 pl330->events[ev] = thrd->id;
1727 return ev;
1728 }
1729
1730 return -1;
1731}
1732
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001733static bool _chan_ns(const struct pl330_dmac *pl330, int i)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001734{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001735 return pl330->pcfg.irq_ns & (1 << i);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001736}
1737
1738/* Upon success, returns IdentityToken for the
1739 * allocated channel, NULL otherwise.
1740 */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001741static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001742{
1743 struct pl330_thread *thrd = NULL;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001744 unsigned long flags;
1745 int chans, i;
1746
Boojin Kimb7d861d2011-12-26 18:49:52 +09001747 if (pl330->state == DYING)
1748 return NULL;
1749
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001750 chans = pl330->pcfg.num_chan;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001751
1752 spin_lock_irqsave(&pl330->lock, flags);
1753
1754 for (i = 0; i < chans; i++) {
1755 thrd = &pl330->channels[i];
1756 if ((thrd->free) && (!_manager_ns(thrd) ||
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001757 _chan_ns(pl330, i))) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001758 thrd->ev = _alloc_event(thrd);
1759 if (thrd->ev >= 0) {
1760 thrd->free = false;
1761 thrd->lstenq = 1;
1762 thrd->req[0].r = NULL;
1763 mark_free(thrd, 0);
1764 thrd->req[1].r = NULL;
1765 mark_free(thrd, 1);
1766 break;
1767 }
1768 }
1769 thrd = NULL;
1770 }
1771
1772 spin_unlock_irqrestore(&pl330->lock, flags);
1773
1774 return thrd;
1775}
1776
1777/* Release an event */
1778static inline void _free_event(struct pl330_thread *thrd, int ev)
1779{
1780 struct pl330_dmac *pl330 = thrd->dmac;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001781
1782 /* If the event is valid and was held by the thread */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001783 if (ev >= 0 && ev < pl330->pcfg.num_events
Boojin Kimb7d861d2011-12-26 18:49:52 +09001784 && pl330->events[ev] == thrd->id)
1785 pl330->events[ev] = -1;
1786}
1787
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001788static void pl330_release_channel(struct pl330_thread *thrd)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001789{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001790 struct pl330_dmac *pl330;
1791 unsigned long flags;
1792
1793 if (!thrd || thrd->free)
1794 return;
1795
1796 _stop(thrd);
1797
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001798 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1799 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001800
1801 pl330 = thrd->dmac;
1802
1803 spin_lock_irqsave(&pl330->lock, flags);
1804 _free_event(thrd, thrd->ev);
1805 thrd->free = true;
1806 spin_unlock_irqrestore(&pl330->lock, flags);
1807}
1808
1809/* Initialize the structure for PL330 configuration, that can be used
1810 * by the client driver the make best use of the DMAC
1811 */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001812static void read_dmac_config(struct pl330_dmac *pl330)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001813{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001814 void __iomem *regs = pl330->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001815 u32 val;
1816
1817 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1818 val &= CRD_DATA_WIDTH_MASK;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001819 pl330->pcfg.data_bus_width = 8 * (1 << val);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001820
1821 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1822 val &= CRD_DATA_BUFF_MASK;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001823 pl330->pcfg.data_buf_dep = val + 1;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001824
1825 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1826 val &= CR0_NUM_CHANS_MASK;
1827 val += 1;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001828 pl330->pcfg.num_chan = val;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001829
1830 val = readl(regs + CR0);
1831 if (val & CR0_PERIPH_REQ_SET) {
1832 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1833 val += 1;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001834 pl330->pcfg.num_peri = val;
1835 pl330->pcfg.peri_ns = readl(regs + CR4);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001836 } else {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001837 pl330->pcfg.num_peri = 0;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001838 }
1839
1840 val = readl(regs + CR0);
1841 if (val & CR0_BOOT_MAN_NS)
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001842 pl330->pcfg.mode |= DMAC_MODE_NS;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001843 else
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001844 pl330->pcfg.mode &= ~DMAC_MODE_NS;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001845
1846 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1847 val &= CR0_NUM_EVENTS_MASK;
1848 val += 1;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001849 pl330->pcfg.num_events = val;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001850
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001851 pl330->pcfg.irq_ns = readl(regs + CR3);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001852}
1853
1854static inline void _reset_thread(struct pl330_thread *thrd)
1855{
1856 struct pl330_dmac *pl330 = thrd->dmac;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001857
1858 thrd->req[0].mc_cpu = pl330->mcode_cpu
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001859 + (thrd->id * pl330->mcbufsz);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001860 thrd->req[0].mc_bus = pl330->mcode_bus
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001861 + (thrd->id * pl330->mcbufsz);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001862 thrd->req[0].r = NULL;
1863 mark_free(thrd, 0);
1864
1865 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001866 + pl330->mcbufsz / 2;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001867 thrd->req[1].mc_bus = thrd->req[0].mc_bus
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001868 + pl330->mcbufsz / 2;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001869 thrd->req[1].r = NULL;
1870 mark_free(thrd, 1);
1871}
1872
1873static int dmac_alloc_threads(struct pl330_dmac *pl330)
1874{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001875 int chans = pl330->pcfg.num_chan;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001876 struct pl330_thread *thrd;
1877 int i;
1878
1879 /* Allocate 1 Manager and 'chans' Channel threads */
1880 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
1881 GFP_KERNEL);
1882 if (!pl330->channels)
1883 return -ENOMEM;
1884
1885 /* Init Channel threads */
1886 for (i = 0; i < chans; i++) {
1887 thrd = &pl330->channels[i];
1888 thrd->id = i;
1889 thrd->dmac = pl330;
1890 _reset_thread(thrd);
1891 thrd->free = true;
1892 }
1893
1894 /* MANAGER is indexed at the end */
1895 thrd = &pl330->channels[chans];
1896 thrd->id = chans;
1897 thrd->dmac = pl330;
1898 thrd->free = false;
1899 pl330->manager = thrd;
1900
1901 return 0;
1902}
1903
1904static int dmac_alloc_resources(struct pl330_dmac *pl330)
1905{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001906 int chans = pl330->pcfg.num_chan;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001907 int ret;
1908
1909 /*
1910 * Alloc MicroCode buffer for 'chans' Channel threads.
1911 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1912 */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001913 pl330->mcode_cpu = dma_alloc_coherent(pl330->ddma.dev,
1914 chans * pl330->mcbufsz,
Boojin Kimb7d861d2011-12-26 18:49:52 +09001915 &pl330->mcode_bus, GFP_KERNEL);
1916 if (!pl330->mcode_cpu) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001917 dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001918 __func__, __LINE__);
1919 return -ENOMEM;
1920 }
1921
1922 ret = dmac_alloc_threads(pl330);
1923 if (ret) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001924 dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001925 __func__, __LINE__);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001926 dma_free_coherent(pl330->ddma.dev,
1927 chans * pl330->mcbufsz,
Boojin Kimb7d861d2011-12-26 18:49:52 +09001928 pl330->mcode_cpu, pl330->mcode_bus);
1929 return ret;
1930 }
1931
1932 return 0;
1933}
1934
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001935static int pl330_add(struct pl330_dmac *pl330)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001936{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001937 void __iomem *regs;
1938 int i, ret;
1939
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001940 regs = pl330->base;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001941
1942 /* Check if we can handle this DMAC */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001943 if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
1944 dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
1945 pl330->pcfg.periph_id);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001946 return -EINVAL;
1947 }
1948
1949 /* Read the configuration of the DMAC */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001950 read_dmac_config(pl330);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001951
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001952 if (pl330->pcfg.num_events == 0) {
1953 dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
Boojin Kimb7d861d2011-12-26 18:49:52 +09001954 __func__, __LINE__);
1955 return -EINVAL;
1956 }
1957
Boojin Kimb7d861d2011-12-26 18:49:52 +09001958 spin_lock_init(&pl330->lock);
1959
1960 INIT_LIST_HEAD(&pl330->req_done);
1961
1962 /* Use default MC buffer size if not provided */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001963 if (!pl330->mcbufsz)
1964 pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001965
1966 /* Mark all events as free */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001967 for (i = 0; i < pl330->pcfg.num_events; i++)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001968 pl330->events[i] = -1;
1969
1970 /* Allocate resources needed by the DMAC */
1971 ret = dmac_alloc_resources(pl330);
1972 if (ret) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001973 dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
Boojin Kimb7d861d2011-12-26 18:49:52 +09001974 return ret;
1975 }
1976
1977 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
1978
1979 pl330->state = INIT;
1980
1981 return 0;
1982}
1983
1984static int dmac_free_threads(struct pl330_dmac *pl330)
1985{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001986 struct pl330_thread *thrd;
1987 int i;
1988
1989 /* Release Channel threads */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02001990 for (i = 0; i < pl330->pcfg.num_chan; i++) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001991 thrd = &pl330->channels[i];
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001992 pl330_release_channel(thrd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001993 }
1994
1995 /* Free memory */
1996 kfree(pl330->channels);
1997
1998 return 0;
1999}
2000
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002001static void pl330_del(struct pl330_dmac *pl330)
Boojin Kimb7d861d2011-12-26 18:49:52 +09002002{
Boojin Kimb7d861d2011-12-26 18:49:52 +09002003 pl330->state = UNINIT;
2004
2005 tasklet_kill(&pl330->tasks);
2006
2007 /* Free DMAC resources */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002008 dmac_free_threads(pl330);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002009
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002010 dma_free_coherent(pl330->ddma.dev,
2011 pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
2012 pl330->mcode_bus);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002013}
2014
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002015/* forward declaration */
2016static struct amba_driver pl330_driver;
2017
Jassi Brarb3040e42010-05-23 20:28:19 -07002018static inline struct dma_pl330_chan *
2019to_pchan(struct dma_chan *ch)
2020{
2021 if (!ch)
2022 return NULL;
2023
2024 return container_of(ch, struct dma_pl330_chan, chan);
2025}
2026
2027static inline struct dma_pl330_desc *
2028to_desc(struct dma_async_tx_descriptor *tx)
2029{
2030 return container_of(tx, struct dma_pl330_desc, txd);
2031}
2032
Jassi Brarb3040e42010-05-23 20:28:19 -07002033static inline void fill_queue(struct dma_pl330_chan *pch)
2034{
2035 struct dma_pl330_desc *desc;
2036 int ret;
2037
2038 list_for_each_entry(desc, &pch->work_list, node) {
2039
2040 /* If already submitted */
2041 if (desc->status == BUSY)
Jassi Brar30fb9802013-02-13 16:13:14 +05302042 continue;
Jassi Brarb3040e42010-05-23 20:28:19 -07002043
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002044 ret = pl330_submit_req(pch->thread, &desc->req);
Jassi Brarb3040e42010-05-23 20:28:19 -07002045 if (!ret) {
2046 desc->status = BUSY;
Jassi Brarb3040e42010-05-23 20:28:19 -07002047 } else if (ret == -EAGAIN) {
2048 /* QFull or DMAC Dying */
2049 break;
2050 } else {
2051 /* Unacceptable request */
2052 desc->status = DONE;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002053 dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
Jassi Brarb3040e42010-05-23 20:28:19 -07002054 __func__, __LINE__, desc->txd.cookie);
2055 tasklet_schedule(&pch->task);
2056 }
2057 }
2058}
2059
2060static void pl330_tasklet(unsigned long data)
2061{
2062 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2063 struct dma_pl330_desc *desc, *_dt;
2064 unsigned long flags;
Jassi Brarb3040e42010-05-23 20:28:19 -07002065
2066 spin_lock_irqsave(&pch->lock, flags);
2067
2068 /* Pick up ripe tomatoes */
2069 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2070 if (desc->status == DONE) {
Tushar Behera30c1dc02012-05-23 16:47:31 +05302071 if (!pch->cyclic)
Vinod Kouleab21582012-05-11 11:24:41 +05302072 dma_cookie_complete(&desc->txd);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002073 list_move_tail(&desc->node, &pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002074 }
2075
2076 /* Try to submit a req imm. next to the last completed cookie */
2077 fill_queue(pch);
2078
2079 /* Make sure the PL330 Channel thread is active */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002080 pl330_chan_ctrl(pch->thread, PL330_OP_START);
Jassi Brarb3040e42010-05-23 20:28:19 -07002081
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002082 while (!list_empty(&pch->completed_list)) {
2083 dma_async_tx_callback callback;
2084 void *callback_param;
Jassi Brarb3040e42010-05-23 20:28:19 -07002085
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002086 desc = list_first_entry(&pch->completed_list,
2087 struct dma_pl330_desc, node);
2088
2089 callback = desc->txd.callback;
2090 callback_param = desc->txd.callback_param;
2091
2092 if (pch->cyclic) {
2093 desc->status = PREP;
2094 list_move_tail(&desc->node, &pch->work_list);
2095 } else {
2096 desc->status = FREE;
2097 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2098 }
2099
Dan Williamsd38a8c62013-10-18 19:35:23 +02002100 dma_descriptor_unmap(&desc->txd);
2101
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002102 if (callback) {
2103 spin_unlock_irqrestore(&pch->lock, flags);
2104 callback(callback_param);
2105 spin_lock_irqsave(&pch->lock, flags);
2106 }
2107 }
2108 spin_unlock_irqrestore(&pch->lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002109}
2110
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002111bool pl330_filter(struct dma_chan *chan, void *param)
2112{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002113 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002114
2115 if (chan->device->dev->driver != &pl330_driver.drv)
2116 return false;
2117
Thomas Abrahamcd072512011-10-24 11:43:11 +02002118 peri_id = chan->private;
Dan Carpenter2f986ec2013-11-08 12:51:16 +03002119 return *peri_id == (unsigned long)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002120}
2121EXPORT_SYMBOL(pl330_filter);
2122
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302123static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2124 struct of_dma *ofdma)
2125{
2126 int count = dma_spec->args_count;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002127 struct pl330_dmac *pl330 = ofdma->of_dma_data;
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002128 unsigned int chan_id;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302129
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002130 if (!pl330)
2131 return NULL;
2132
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302133 if (count != 1)
2134 return NULL;
2135
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002136 chan_id = dma_spec->args[0];
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002137 if (chan_id >= pl330->num_peripherals)
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002138 return NULL;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302139
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002140 return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302141}
2142
Jassi Brarb3040e42010-05-23 20:28:19 -07002143static int pl330_alloc_chan_resources(struct dma_chan *chan)
2144{
2145 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002146 struct pl330_dmac *pl330 = pch->dmac;
Jassi Brarb3040e42010-05-23 20:28:19 -07002147 unsigned long flags;
2148
2149 spin_lock_irqsave(&pch->lock, flags);
2150
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002151 dma_cookie_init(chan);
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002152 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002153
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002154 pch->thread = pl330_request_channel(pl330);
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002155 if (!pch->thread) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002156 spin_unlock_irqrestore(&pch->lock, flags);
Inderpal Singh02747882012-09-17 09:57:45 +05302157 return -ENOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002158 }
2159
2160 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2161
2162 spin_unlock_irqrestore(&pch->lock, flags);
2163
2164 return 1;
2165}
2166
2167static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2168{
2169 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002170 struct dma_pl330_desc *desc;
Jassi Brarb3040e42010-05-23 20:28:19 -07002171 unsigned long flags;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002172 struct pl330_dmac *pl330 = pch->dmac;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002173 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002174 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002175
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002176 switch (cmd) {
2177 case DMA_TERMINATE_ALL:
2178 spin_lock_irqsave(&pch->lock, flags);
2179
2180 /* FLUSH the PL330 Channel thread */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002181 pl330_chan_ctrl(pch->thread, PL330_OP_FLUSH);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002182
2183 /* Mark all desc done */
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002184 list_for_each_entry(desc, &pch->submitted_list, node) {
2185 desc->status = FREE;
2186 dma_cookie_complete(&desc->txd);
2187 }
2188
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002189 list_for_each_entry(desc, &pch->work_list , node) {
2190 desc->status = FREE;
2191 dma_cookie_complete(&desc->txd);
Boojin Kimae43b882011-09-02 09:44:32 +09002192 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002193
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002194 list_for_each_entry(desc, &pch->completed_list , node) {
2195 desc->status = FREE;
2196 dma_cookie_complete(&desc->txd);
2197 }
2198
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002199 list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
2200 list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
2201 list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002202 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002203 break;
2204 case DMA_SLAVE_CONFIG:
2205 slave_config = (struct dma_slave_config *)arg;
2206
Vinod Kouldb8196d2011-10-13 22:34:23 +05302207 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002208 if (slave_config->dst_addr)
2209 pch->fifo_addr = slave_config->dst_addr;
2210 if (slave_config->dst_addr_width)
2211 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2212 if (slave_config->dst_maxburst)
2213 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302214 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002215 if (slave_config->src_addr)
2216 pch->fifo_addr = slave_config->src_addr;
2217 if (slave_config->src_addr_width)
2218 pch->burst_sz = __ffs(slave_config->src_addr_width);
2219 if (slave_config->src_maxburst)
2220 pch->burst_len = slave_config->src_maxburst;
2221 }
2222 break;
2223 default:
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002224 dev_err(pch->dmac->ddma.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002225 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002226 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002227
2228 return 0;
2229}
2230
2231static void pl330_free_chan_resources(struct dma_chan *chan)
2232{
2233 struct dma_pl330_chan *pch = to_pchan(chan);
2234 unsigned long flags;
2235
Jassi Brarb3040e42010-05-23 20:28:19 -07002236 tasklet_kill(&pch->task);
2237
Bartlomiej Zolnierkiewiczda331ba2013-07-03 15:00:43 -07002238 spin_lock_irqsave(&pch->lock, flags);
2239
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002240 pl330_release_channel(pch->thread);
2241 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002242
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002243 if (pch->cyclic)
2244 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2245
Jassi Brarb3040e42010-05-23 20:28:19 -07002246 spin_unlock_irqrestore(&pch->lock, flags);
2247}
2248
2249static enum dma_status
2250pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2251 struct dma_tx_state *txstate)
2252{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002253 return dma_cookie_status(chan, cookie, txstate);
Jassi Brarb3040e42010-05-23 20:28:19 -07002254}
2255
2256static void pl330_issue_pending(struct dma_chan *chan)
2257{
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002258 struct dma_pl330_chan *pch = to_pchan(chan);
2259 unsigned long flags;
2260
2261 spin_lock_irqsave(&pch->lock, flags);
2262 list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2263 spin_unlock_irqrestore(&pch->lock, flags);
2264
2265 pl330_tasklet((unsigned long)pch);
Jassi Brarb3040e42010-05-23 20:28:19 -07002266}
2267
2268/*
2269 * We returned the last one of the circular list of descriptor(s)
2270 * from prep_xxx, so the argument to submit corresponds to the last
2271 * descriptor of the list.
2272 */
2273static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2274{
2275 struct dma_pl330_desc *desc, *last = to_desc(tx);
2276 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2277 dma_cookie_t cookie;
2278 unsigned long flags;
2279
2280 spin_lock_irqsave(&pch->lock, flags);
2281
2282 /* Assign cookies to all nodes */
Jassi Brarb3040e42010-05-23 20:28:19 -07002283 while (!list_empty(&last->node)) {
2284 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002285 if (pch->cyclic) {
2286 desc->txd.callback = last->txd.callback;
2287 desc->txd.callback_param = last->txd.callback_param;
2288 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002289
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002290 dma_cookie_assign(&desc->txd);
Jassi Brarb3040e42010-05-23 20:28:19 -07002291
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002292 list_move_tail(&desc->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002293 }
2294
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002295 cookie = dma_cookie_assign(&last->txd);
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002296 list_add_tail(&last->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002297 spin_unlock_irqrestore(&pch->lock, flags);
2298
2299 return cookie;
2300}
2301
2302static inline void _init_desc(struct dma_pl330_desc *desc)
2303{
Jassi Brarb3040e42010-05-23 20:28:19 -07002304 desc->req.x = &desc->px;
Jassi Brarb3040e42010-05-23 20:28:19 -07002305 desc->rqcfg.swap = SWAP_NO;
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02002306 desc->rqcfg.scctl = CCTRL0;
2307 desc->rqcfg.dcctl = CCTRL0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002308 desc->req.cfg = &desc->rqcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002309 desc->txd.tx_submit = pl330_tx_submit;
2310
2311 INIT_LIST_HEAD(&desc->node);
2312}
2313
2314/* Returns the number of descriptors added to the DMAC pool */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002315static int add_desc(struct pl330_dmac *pl330, gfp_t flg, int count)
Jassi Brarb3040e42010-05-23 20:28:19 -07002316{
2317 struct dma_pl330_desc *desc;
2318 unsigned long flags;
2319 int i;
2320
Will Deacon0baf8f62013-12-02 18:01:30 +00002321 desc = kcalloc(count, sizeof(*desc), flg);
Jassi Brarb3040e42010-05-23 20:28:19 -07002322 if (!desc)
2323 return 0;
2324
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002325 spin_lock_irqsave(&pl330->pool_lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002326
2327 for (i = 0; i < count; i++) {
2328 _init_desc(&desc[i]);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002329 list_add_tail(&desc[i].node, &pl330->desc_pool);
Jassi Brarb3040e42010-05-23 20:28:19 -07002330 }
2331
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002332 spin_unlock_irqrestore(&pl330->pool_lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002333
2334 return count;
2335}
2336
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002337static struct dma_pl330_desc *pluck_desc(struct pl330_dmac *pl330)
Jassi Brarb3040e42010-05-23 20:28:19 -07002338{
2339 struct dma_pl330_desc *desc = NULL;
2340 unsigned long flags;
2341
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002342 spin_lock_irqsave(&pl330->pool_lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002343
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002344 if (!list_empty(&pl330->desc_pool)) {
2345 desc = list_entry(pl330->desc_pool.next,
Jassi Brarb3040e42010-05-23 20:28:19 -07002346 struct dma_pl330_desc, node);
2347
2348 list_del_init(&desc->node);
2349
2350 desc->status = PREP;
2351 desc->txd.callback = NULL;
2352 }
2353
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002354 spin_unlock_irqrestore(&pl330->pool_lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002355
2356 return desc;
2357}
2358
2359static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2360{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002361 struct pl330_dmac *pl330 = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002362 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002363 struct dma_pl330_desc *desc;
2364
2365 /* Pluck one desc from the pool of DMAC */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002366 desc = pluck_desc(pl330);
Jassi Brarb3040e42010-05-23 20:28:19 -07002367
2368 /* If the DMAC pool is empty, alloc new */
2369 if (!desc) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002370 if (!add_desc(pl330, GFP_ATOMIC, 1))
Jassi Brarb3040e42010-05-23 20:28:19 -07002371 return NULL;
2372
2373 /* Try again */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002374 desc = pluck_desc(pl330);
Jassi Brarb3040e42010-05-23 20:28:19 -07002375 if (!desc) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002376 dev_err(pch->dmac->ddma.dev,
Jassi Brarb3040e42010-05-23 20:28:19 -07002377 "%s:%d ALERT!\n", __func__, __LINE__);
2378 return NULL;
2379 }
2380 }
2381
2382 /* Initialize the descriptor */
2383 desc->pchan = pch;
2384 desc->txd.cookie = 0;
2385 async_tx_ack(&desc->txd);
2386
Thomas Abrahamcd072512011-10-24 11:43:11 +02002387 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002388 desc->rqcfg.pcfg = &pch->dmac->pcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002389
2390 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2391
2392 return desc;
2393}
2394
2395static inline void fill_px(struct pl330_xfer *px,
2396 dma_addr_t dst, dma_addr_t src, size_t len)
2397{
Jassi Brarb3040e42010-05-23 20:28:19 -07002398 px->bytes = len;
2399 px->dst_addr = dst;
2400 px->src_addr = src;
2401}
2402
2403static struct dma_pl330_desc *
2404__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2405 dma_addr_t src, size_t len)
2406{
2407 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2408
2409 if (!desc) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002410 dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
Jassi Brarb3040e42010-05-23 20:28:19 -07002411 __func__, __LINE__);
2412 return NULL;
2413 }
2414
2415 /*
2416 * Ideally we should lookout for reqs bigger than
2417 * those that can be programmed with 256 bytes of
2418 * MC buffer, but considering a req size is seldom
2419 * going to be word-unaligned and more than 200MB,
2420 * we take it easy.
2421 * Also, should the limit is reached we'd rather
2422 * have the platform increase MC buffer size than
2423 * complicating this API driver.
2424 */
2425 fill_px(&desc->px, dst, src, len);
2426
2427 return desc;
2428}
2429
2430/* Call after fixing burst size */
2431static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2432{
2433 struct dma_pl330_chan *pch = desc->pchan;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002434 struct pl330_dmac *pl330 = pch->dmac;
Jassi Brarb3040e42010-05-23 20:28:19 -07002435 int burst_len;
2436
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002437 burst_len = pl330->pcfg.data_bus_width / 8;
2438 burst_len *= pl330->pcfg.data_buf_dep;
Jassi Brarb3040e42010-05-23 20:28:19 -07002439 burst_len >>= desc->rqcfg.brst_size;
2440
2441 /* src/dst_burst_len can't be more than 16 */
2442 if (burst_len > 16)
2443 burst_len = 16;
2444
2445 while (burst_len > 1) {
2446 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2447 break;
2448 burst_len--;
2449 }
2450
2451 return burst_len;
2452}
2453
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002454static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2455 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002456 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002457 unsigned long flags, void *context)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002458{
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002459 struct dma_pl330_desc *desc = NULL, *first = NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002460 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002461 struct pl330_dmac *pl330 = pch->dmac;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002462 unsigned int i;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002463 dma_addr_t dst;
2464 dma_addr_t src;
2465
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002466 if (len % period_len != 0)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002467 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002468
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002469 if (!is_slave_direction(direction)) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002470 dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002471 __func__, __LINE__);
2472 return NULL;
2473 }
2474
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002475 for (i = 0; i < len / period_len; i++) {
2476 desc = pl330_get_desc(pch);
2477 if (!desc) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002478 dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002479 __func__, __LINE__);
2480
2481 if (!first)
2482 return NULL;
2483
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002484 spin_lock_irqsave(&pl330->pool_lock, flags);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002485
2486 while (!list_empty(&first->node)) {
2487 desc = list_entry(first->node.next,
2488 struct dma_pl330_desc, node);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002489 list_move_tail(&desc->node, &pl330->desc_pool);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002490 }
2491
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002492 list_move_tail(&first->node, &pl330->desc_pool);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002493
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002494 spin_unlock_irqrestore(&pl330->pool_lock, flags);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002495
2496 return NULL;
2497 }
2498
2499 switch (direction) {
2500 case DMA_MEM_TO_DEV:
2501 desc->rqcfg.src_inc = 1;
2502 desc->rqcfg.dst_inc = 0;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002503 src = dma_addr;
2504 dst = pch->fifo_addr;
2505 break;
2506 case DMA_DEV_TO_MEM:
2507 desc->rqcfg.src_inc = 0;
2508 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002509 src = pch->fifo_addr;
2510 dst = dma_addr;
2511 break;
2512 default:
2513 break;
2514 }
2515
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002516 desc->req.rqtype = direction;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002517 desc->rqcfg.brst_size = pch->burst_sz;
2518 desc->rqcfg.brst_len = 1;
2519 fill_px(&desc->px, dst, src, period_len);
2520
2521 if (!first)
2522 first = desc;
2523 else
2524 list_add_tail(&desc->node, &first->node);
2525
2526 dma_addr += period_len;
2527 }
2528
2529 if (!desc)
2530 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002531
2532 pch->cyclic = true;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002533 desc->txd.flags = flags;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002534
2535 return &desc->txd;
2536}
2537
Jassi Brarb3040e42010-05-23 20:28:19 -07002538static struct dma_async_tx_descriptor *
2539pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2540 dma_addr_t src, size_t len, unsigned long flags)
2541{
2542 struct dma_pl330_desc *desc;
2543 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002544 struct pl330_dmac *pl330 = pch->dmac;
Jassi Brarb3040e42010-05-23 20:28:19 -07002545 int burst;
2546
Rob Herring4e0e6102011-07-25 16:05:04 -05002547 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002548 return NULL;
2549
Jassi Brarb3040e42010-05-23 20:28:19 -07002550 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2551 if (!desc)
2552 return NULL;
2553
2554 desc->rqcfg.src_inc = 1;
2555 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002556 desc->req.rqtype = DMA_MEM_TO_MEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002557
2558 /* Select max possible burst size */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002559 burst = pl330->pcfg.data_bus_width / 8;
Jassi Brarb3040e42010-05-23 20:28:19 -07002560
2561 while (burst > 1) {
2562 if (!(len % burst))
2563 break;
2564 burst /= 2;
2565 }
2566
2567 desc->rqcfg.brst_size = 0;
2568 while (burst != (1 << desc->rqcfg.brst_size))
2569 desc->rqcfg.brst_size++;
2570
2571 desc->rqcfg.brst_len = get_burst_len(desc, len);
2572
2573 desc->txd.flags = flags;
2574
2575 return &desc->txd;
2576}
2577
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002578static void __pl330_giveback_desc(struct pl330_dmac *pl330,
Chanho Park52a9d172013-08-09 20:11:33 +09002579 struct dma_pl330_desc *first)
2580{
2581 unsigned long flags;
2582 struct dma_pl330_desc *desc;
2583
2584 if (!first)
2585 return;
2586
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002587 spin_lock_irqsave(&pl330->pool_lock, flags);
Chanho Park52a9d172013-08-09 20:11:33 +09002588
2589 while (!list_empty(&first->node)) {
2590 desc = list_entry(first->node.next,
2591 struct dma_pl330_desc, node);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002592 list_move_tail(&desc->node, &pl330->desc_pool);
Chanho Park52a9d172013-08-09 20:11:33 +09002593 }
2594
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002595 list_move_tail(&first->node, &pl330->desc_pool);
Chanho Park52a9d172013-08-09 20:11:33 +09002596
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002597 spin_unlock_irqrestore(&pl330->pool_lock, flags);
Chanho Park52a9d172013-08-09 20:11:33 +09002598}
2599
Jassi Brarb3040e42010-05-23 20:28:19 -07002600static struct dma_async_tx_descriptor *
2601pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302602 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002603 unsigned long flg, void *context)
Jassi Brarb3040e42010-05-23 20:28:19 -07002604{
2605 struct dma_pl330_desc *first, *desc = NULL;
2606 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002607 struct scatterlist *sg;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002608 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002609 dma_addr_t addr;
2610
Thomas Abrahamcd072512011-10-24 11:43:11 +02002611 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002612 return NULL;
2613
Boojin Kim1b9bb712011-09-02 09:44:30 +09002614 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002615
2616 first = NULL;
2617
2618 for_each_sg(sgl, sg, sg_len, i) {
2619
2620 desc = pl330_get_desc(pch);
2621 if (!desc) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002622 struct pl330_dmac *pl330 = pch->dmac;
Jassi Brarb3040e42010-05-23 20:28:19 -07002623
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002624 dev_err(pch->dmac->ddma.dev,
Jassi Brarb3040e42010-05-23 20:28:19 -07002625 "%s:%d Unable to fetch desc\n",
2626 __func__, __LINE__);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002627 __pl330_giveback_desc(pl330, first);
Jassi Brarb3040e42010-05-23 20:28:19 -07002628
2629 return NULL;
2630 }
2631
2632 if (!first)
2633 first = desc;
2634 else
2635 list_add_tail(&desc->node, &first->node);
2636
Vinod Kouldb8196d2011-10-13 22:34:23 +05302637 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002638 desc->rqcfg.src_inc = 1;
2639 desc->rqcfg.dst_inc = 0;
2640 fill_px(&desc->px,
2641 addr, sg_dma_address(sg), sg_dma_len(sg));
2642 } else {
2643 desc->rqcfg.src_inc = 0;
2644 desc->rqcfg.dst_inc = 1;
2645 fill_px(&desc->px,
2646 sg_dma_address(sg), addr, sg_dma_len(sg));
2647 }
2648
Boojin Kim1b9bb712011-09-02 09:44:30 +09002649 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002650 desc->rqcfg.brst_len = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002651 desc->req.rqtype = direction;
Jassi Brarb3040e42010-05-23 20:28:19 -07002652 }
2653
2654 /* Return the last desc in the chain */
2655 desc->txd.flags = flg;
2656 return &desc->txd;
2657}
2658
2659static irqreturn_t pl330_irq_handler(int irq, void *data)
2660{
2661 if (pl330_update(data))
2662 return IRQ_HANDLED;
2663 else
2664 return IRQ_NONE;
2665}
2666
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002667#define PL330_DMA_BUSWIDTHS \
2668 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2669 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2670 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2671 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2672 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2673
2674static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2675 struct dma_slave_caps *caps)
2676{
2677 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2678 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2679 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2680 caps->cmd_pause = false;
2681 caps->cmd_terminate = true;
Lars-Peter Clausenbfb9bb42014-01-11 14:02:17 +01002682 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002683
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002684 return 0;
2685}
2686
Bill Pemberton463a1f82012-11-19 13:22:55 -05002687static int
Russell Kingaa25afa2011-02-19 15:55:00 +00002688pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002689{
2690 struct dma_pl330_platdata *pdat;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002691 struct pl330_config *pcfg;
2692 struct pl330_dmac *pl330;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302693 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002694 struct dma_device *pd;
2695 struct resource *res;
2696 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002697 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002698
Jingoo Hand4adcc02013-07-30 17:09:11 +09002699 pdat = dev_get_platdata(&adev->dev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002700
Russell King64113012013-06-27 10:29:32 +01002701 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2702 if (ret)
2703 return ret;
2704
Jassi Brarb3040e42010-05-23 20:28:19 -07002705 /* Allocate a new DMAC and its Channels */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002706 pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
2707 if (!pl330) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002708 dev_err(&adev->dev, "unable to allocate mem\n");
2709 return -ENOMEM;
2710 }
2711
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002712 pl330->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002713
2714 res = &adev->res;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002715 pl330->base = devm_ioremap_resource(&adev->dev, res);
2716 if (IS_ERR(pl330->base))
2717 return PTR_ERR(pl330->base);
Jassi Brarb3040e42010-05-23 20:28:19 -07002718
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002719 amba_set_drvdata(adev, pl330);
Boojin Kima2f52032011-09-02 09:44:29 +09002720
Dan Carpenter02808b42013-11-08 12:50:24 +03002721 for (i = 0; i < AMBA_NR_IRQS; i++) {
Michal Simeke98b3ca2013-09-30 08:50:48 +02002722 irq = adev->irq[i];
2723 if (irq) {
2724 ret = devm_request_irq(&adev->dev, irq,
2725 pl330_irq_handler, 0,
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002726 dev_name(&adev->dev), pl330);
Michal Simeke98b3ca2013-09-30 08:50:48 +02002727 if (ret)
2728 return ret;
2729 } else {
2730 break;
2731 }
2732 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002733
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002734 pcfg = &pl330->pcfg;
2735
2736 pcfg->periph_id = adev->periphid;
2737 ret = pl330_add(pl330);
Jassi Brarb3040e42010-05-23 20:28:19 -07002738 if (ret)
Michal Simek173e8382013-09-04 16:40:17 +02002739 return ret;
Jassi Brarb3040e42010-05-23 20:28:19 -07002740
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002741 INIT_LIST_HEAD(&pl330->desc_pool);
2742 spin_lock_init(&pl330->pool_lock);
Jassi Brarb3040e42010-05-23 20:28:19 -07002743
2744 /* Create a descriptor pool of default size */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002745 if (!add_desc(pl330, GFP_KERNEL, NR_DEFAULT_DESC))
Jassi Brarb3040e42010-05-23 20:28:19 -07002746 dev_warn(&adev->dev, "unable to allocate desc\n");
2747
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002748 pd = &pl330->ddma;
Jassi Brarb3040e42010-05-23 20:28:19 -07002749 INIT_LIST_HEAD(&pd->channels);
2750
2751 /* Initialize channel parameters */
Olof Johanssonc8473822012-04-08 16:26:19 -07002752 if (pdat)
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002753 num_chan = max_t(int, pdat->nr_valid_peri, pcfg->num_chan);
Olof Johanssonc8473822012-04-08 16:26:19 -07002754 else
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002755 num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
Olof Johanssonc8473822012-04-08 16:26:19 -07002756
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002757 pl330->num_peripherals = num_chan;
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002758
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002759 pl330->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
2760 if (!pl330->peripherals) {
Sachin Kamat61c6e752012-09-17 15:20:23 +05302761 ret = -ENOMEM;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002762 dev_err(&adev->dev, "unable to allocate pl330->peripherals\n");
Sachin Kamate4d43c12012-11-15 06:27:50 +00002763 goto probe_err2;
Sachin Kamat61c6e752012-09-17 15:20:23 +05302764 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002765
Rob Herring4e0e6102011-07-25 16:05:04 -05002766 for (i = 0; i < num_chan; i++) {
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002767 pch = &pl330->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002768 if (!adev->dev.of_node)
2769 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2770 else
2771 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002772
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002773 INIT_LIST_HEAD(&pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002774 INIT_LIST_HEAD(&pch->work_list);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002775 INIT_LIST_HEAD(&pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002776 spin_lock_init(&pch->lock);
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002777 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002778 pch->chan.device = pd;
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002779 pch->dmac = pl330;
Jassi Brarb3040e42010-05-23 20:28:19 -07002780
2781 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002782 list_add_tail(&pch->chan.device_node, &pd->channels);
2783 }
2784
2785 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002786 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002787 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002788 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002789 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002790 if (pcfg->num_peri) {
Thomas Abraham93ed5542011-10-24 11:43:31 +02002791 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2792 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
Tushar Behera5557a412012-08-29 10:16:25 +05302793 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002794 }
2795 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002796
2797 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2798 pd->device_free_chan_resources = pl330_free_chan_resources;
2799 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002800 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07002801 pd->device_tx_status = pl330_tx_status;
2802 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2803 pd->device_control = pl330_control;
2804 pd->device_issue_pending = pl330_issue_pending;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002805 pd->device_slave_caps = pl330_dma_device_slave_caps;
Jassi Brarb3040e42010-05-23 20:28:19 -07002806
2807 ret = dma_async_device_register(pd);
2808 if (ret) {
2809 dev_err(&adev->dev, "unable to register DMAC\n");
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302810 goto probe_err3;
2811 }
2812
2813 if (adev->dev.of_node) {
2814 ret = of_dma_controller_register(adev->dev.of_node,
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002815 of_dma_pl330_xlate, pl330);
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302816 if (ret) {
2817 dev_err(&adev->dev,
2818 "unable to register DMA to the generic DT DMA helpers\n");
2819 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002820 }
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01002821
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002822 adev->dev.dma_parms = &pl330->dma_parms;
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01002823
Vinod Kouldbaf6d82013-09-02 21:54:48 +05302824 /*
2825 * This is the limit for transfers with a buswidth of 1, larger
2826 * buswidths will have larger limits.
2827 */
2828 ret = dma_set_max_seg_size(&adev->dev, 1900800);
2829 if (ret)
2830 dev_err(&adev->dev, "unable to set the seg size\n");
2831
Jassi Brarb3040e42010-05-23 20:28:19 -07002832
Jassi Brarb3040e42010-05-23 20:28:19 -07002833 dev_info(&adev->dev,
2834 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
2835 dev_info(&adev->dev,
2836 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002837 pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
2838 pcfg->num_peri, pcfg->num_events);
Jassi Brarb3040e42010-05-23 20:28:19 -07002839
2840 return 0;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302841probe_err3:
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302842 /* Idle the DMAC */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002843 list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302844 chan.device_node) {
2845
2846 /* Remove the channel */
2847 list_del(&pch->chan.device_node);
2848
2849 /* Flush the channel */
2850 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2851 pl330_free_chan_resources(&pch->chan);
2852 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002853probe_err2:
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002854 pl330_del(pl330);
Jassi Brarb3040e42010-05-23 20:28:19 -07002855
2856 return ret;
2857}
2858
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002859static int pl330_remove(struct amba_device *adev)
Jassi Brarb3040e42010-05-23 20:28:19 -07002860{
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002861 struct pl330_dmac *pl330 = amba_get_drvdata(adev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002862 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002863
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302864 if (adev->dev.of_node)
2865 of_dma_controller_free(adev->dev.of_node);
Padmavathi Venna421da892013-02-14 09:10:07 +05302866
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002867 dma_async_device_unregister(&pl330->ddma);
Jassi Brarb3040e42010-05-23 20:28:19 -07002868
2869 /* Idle the DMAC */
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002870 list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
Jassi Brarb3040e42010-05-23 20:28:19 -07002871 chan.device_node) {
2872
2873 /* Remove the channel */
2874 list_del(&pch->chan.device_node);
2875
2876 /* Flush the channel */
2877 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2878 pl330_free_chan_resources(&pch->chan);
2879 }
2880
Lars-Peter Clausenf6f24212014-07-06 20:32:29 +02002881 pl330_del(pl330);
Jassi Brarb3040e42010-05-23 20:28:19 -07002882
Jassi Brarb3040e42010-05-23 20:28:19 -07002883 return 0;
2884}
2885
2886static struct amba_id pl330_ids[] = {
2887 {
2888 .id = 0x00041330,
2889 .mask = 0x000fffff,
2890 },
2891 { 0, 0 },
2892};
2893
Dave Martine8fa5162011-10-05 15:15:20 +01002894MODULE_DEVICE_TABLE(amba, pl330_ids);
2895
Jassi Brarb3040e42010-05-23 20:28:19 -07002896static struct amba_driver pl330_driver = {
2897 .drv = {
2898 .owner = THIS_MODULE,
2899 .name = "dma-pl330",
Jassi Brarb3040e42010-05-23 20:28:19 -07002900 },
2901 .id_table = pl330_ids,
2902 .probe = pl330_probe,
2903 .remove = pl330_remove,
2904};
2905
viresh kumar9e5ed092012-03-15 10:40:38 +01002906module_amba_driver(pl330_driver);
Jassi Brarb3040e42010-05-23 20:28:19 -07002907
2908MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
2909MODULE_DESCRIPTION("API Driver for PL330 DMAC");
2910MODULE_LICENSE("GPL");