blob: c5eeb64dce6f46bbc5a7e9999e49adf84e1482e8 [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
285/* Handle to the DMAC provided to the PL330 core */
286struct pl330_info {
287 /* Owning device */
288 struct device *dev;
289 /* Size of MicroCode buffers for each channel. */
290 unsigned mcbufsz;
291 /* ioremap'ed address of PL330 registers. */
292 void __iomem *base;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900293 /* PL330 core data, Client must not touch it. */
294 void *pl330_data;
295 /* Populated by the PL330 core driver during pl330_add */
296 struct pl330_config pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900297};
298
299/**
300 * Request Configuration.
301 * The PL330 core does not modify this and uses the last
302 * working configuration if the request doesn't provide any.
303 *
304 * The Client may want to provide this info only for the
305 * first request and a request with new settings.
306 */
307struct pl330_reqcfg {
308 /* Address Incrementing */
309 unsigned dst_inc:1;
310 unsigned src_inc:1;
311
312 /*
313 * For now, the SRC & DST protection levels
314 * and burst size/length are assumed same.
315 */
316 bool nonsecure;
317 bool privileged;
318 bool insnaccess;
319 unsigned brst_len:5;
320 unsigned brst_size:3; /* in power of 2 */
321
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +0200322 enum pl330_cachectrl dcctl;
323 enum pl330_cachectrl scctl;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900324 enum pl330_byteswap swap;
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900325 struct pl330_config *pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900326};
327
328/*
329 * One cycle of DMAC operation.
330 * There may be more than one xfer in a request.
331 */
332struct pl330_xfer {
333 u32 src_addr;
334 u32 dst_addr;
335 /* Size to xfer */
336 u32 bytes;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900337};
338
339/* The xfer callbacks are made with one of these arguments. */
340enum pl330_op_err {
341 /* The all xfers in the request were success. */
342 PL330_ERR_NONE,
343 /* If req aborted due to global error. */
344 PL330_ERR_ABORT,
345 /* If req failed due to problem with Channel. */
346 PL330_ERR_FAIL,
347};
348
349/* A request defining Scatter-Gather List ending with NULL xfer. */
350struct pl330_req {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +0200351 enum dma_transfer_direction rqtype;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900352 /* Index of peripheral for the xfer. */
353 unsigned peri:5;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900354 /* If NULL, req will be done at last set parameters. */
355 struct pl330_reqcfg *cfg;
356 /* Pointer to first xfer in the request. */
357 struct pl330_xfer *x;
Javi Merinofdec53d2012-06-13 15:07:00 +0100358 /* Hook to attach to DMAC's list of reqs with due callback */
359 struct list_head rqd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900360};
361
Boojin Kimb7d861d2011-12-26 18:49:52 +0900362enum pl330_chan_op {
363 /* Start the channel */
364 PL330_OP_START,
365 /* Abort the active xfer */
366 PL330_OP_ABORT,
367 /* Stop xfer and flush queue */
368 PL330_OP_FLUSH,
369};
370
371struct _xfer_spec {
372 u32 ccr;
373 struct pl330_req *r;
374 struct pl330_xfer *x;
375};
376
377enum dmamov_dst {
378 SAR = 0,
379 CCR,
380 DAR,
381};
382
383enum pl330_dst {
384 SRC = 0,
385 DST,
386};
387
388enum pl330_cond {
389 SINGLE,
390 BURST,
391 ALWAYS,
392};
393
394struct _pl330_req {
395 u32 mc_bus;
396 void *mc_cpu;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900397 struct pl330_req *r;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900398};
399
400/* ToBeDone for tasklet */
401struct _pl330_tbd {
402 bool reset_dmac;
403 bool reset_mngr;
404 u8 reset_chan;
405};
406
407/* A DMAC Thread */
408struct pl330_thread {
409 u8 id;
410 int ev;
411 /* If the channel is not yet acquired by any client */
412 bool free;
413 /* Parent DMAC */
414 struct pl330_dmac *dmac;
415 /* Only two at a time */
416 struct _pl330_req req[2];
417 /* Index of the last enqueued request */
418 unsigned lstenq;
419 /* Index of the last submitted request or -1 if the DMA is stopped */
420 int req_running;
421};
422
423enum pl330_dmac_state {
424 UNINIT,
425 INIT,
426 DYING,
427};
428
429/* A DMAC */
430struct pl330_dmac {
431 spinlock_t lock;
432 /* Holds list of reqs with due callbacks */
433 struct list_head req_done;
434 /* Pointer to platform specific stuff */
435 struct pl330_info *pinfo;
436 /* Maximum possible events/irqs */
437 int events[32];
438 /* BUS address of MicroCode buffer */
Will Deaconfed8c452013-06-10 19:34:38 +0100439 dma_addr_t mcode_bus;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900440 /* CPU address of MicroCode buffer */
441 void *mcode_cpu;
442 /* List of all Channel threads */
443 struct pl330_thread *channels;
444 /* Pointer to the MANAGER thread */
445 struct pl330_thread *manager;
446 /* To handle bad news in interrupt */
447 struct tasklet_struct tasks;
448 struct _pl330_tbd dmac_tbd;
449 /* State of DMAC operation */
450 enum pl330_dmac_state state;
451};
452
Jassi Brarb3040e42010-05-23 20:28:19 -0700453enum desc_status {
454 /* In the DMAC pool */
455 FREE,
456 /*
Masanari Iidad73111c2012-08-04 23:37:53 +0900457 * Allocated to some channel during prep_xxx
Jassi Brarb3040e42010-05-23 20:28:19 -0700458 * Also may be sitting on the work_list.
459 */
460 PREP,
461 /*
462 * Sitting on the work_list and already submitted
463 * to the PL330 core. Not more than two descriptors
464 * of a channel can be BUSY at any time.
465 */
466 BUSY,
467 /*
468 * Sitting on the channel work_list but xfer done
469 * by PL330 core
470 */
471 DONE,
472};
473
474struct dma_pl330_chan {
475 /* Schedule desc completion */
476 struct tasklet_struct task;
477
478 /* DMA-Engine Channel */
479 struct dma_chan chan;
480
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +0100481 /* List of submitted descriptors */
482 struct list_head submitted_list;
483 /* List of issued descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700484 struct list_head work_list;
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +0200485 /* List of completed descriptors */
486 struct list_head completed_list;
Jassi Brarb3040e42010-05-23 20:28:19 -0700487
488 /* Pointer to the DMAC that manages this channel,
489 * NULL if the channel is available to be acquired.
490 * As the parent, this DMAC also provides descriptors
491 * to the channel.
492 */
493 struct dma_pl330_dmac *dmac;
494
495 /* To protect channel manipulation */
496 spinlock_t lock;
497
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200498 /*
499 * Hardware channel thread of PL330 DMAC. NULL if the channel is
500 * available.
Jassi Brarb3040e42010-05-23 20:28:19 -0700501 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200502 struct pl330_thread *thread;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900503
504 /* For D-to-M and M-to-D channels */
505 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900506 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900507 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900508
509 /* for cyclic capability */
510 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700511};
512
513struct dma_pl330_dmac {
514 struct pl330_info pif;
515
516 /* DMA-Engine Device */
517 struct dma_device ddma;
518
Lars-Peter Clausenb714b842013-11-25 16:07:46 +0100519 /* Holds info about sg limitations */
520 struct device_dma_parameters dma_parms;
521
Jassi Brarb3040e42010-05-23 20:28:19 -0700522 /* Pool of descriptors available for the DMAC's channels */
523 struct list_head desc_pool;
524 /* To protect desc_pool manipulation */
525 spinlock_t pool_lock;
526
527 /* Peripheral channels connected to this DMAC */
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +0100528 unsigned int num_peripherals;
Rob Herring4e0e6102011-07-25 16:05:04 -0500529 struct dma_pl330_chan *peripherals; /* keep at end */
Jassi Brarb3040e42010-05-23 20:28:19 -0700530};
531
532struct dma_pl330_desc {
533 /* To attach to a queue as child */
534 struct list_head node;
535
536 /* Descriptor for the DMA Engine API */
537 struct dma_async_tx_descriptor txd;
538
539 /* Xfer for PL330 core */
540 struct pl330_xfer px;
541
542 struct pl330_reqcfg rqcfg;
543 struct pl330_req req;
544
545 enum desc_status status;
546
547 /* The channel which currently holds this desc */
548 struct dma_pl330_chan *pchan;
549};
550
Boojin Kimb7d861d2011-12-26 18:49:52 +0900551static inline bool _queue_empty(struct pl330_thread *thrd)
552{
553 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
554 ? true : false;
555}
556
557static inline bool _queue_full(struct pl330_thread *thrd)
558{
559 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
560 ? false : true;
561}
562
563static inline bool is_manager(struct pl330_thread *thrd)
564{
Lars-Peter Clausenfbbcd9b2014-07-06 20:32:28 +0200565 return thrd->dmac->manager == thrd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900566}
567
568/* If manager of the thread is in Non-Secure mode */
569static inline bool _manager_ns(struct pl330_thread *thrd)
570{
571 struct pl330_dmac *pl330 = thrd->dmac;
572
573 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
574}
575
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900576static inline u32 get_revision(u32 periph_id)
577{
578 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
579}
580
Boojin Kimb7d861d2011-12-26 18:49:52 +0900581static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
582 enum pl330_dst da, u16 val)
583{
584 if (dry_run)
585 return SZ_DMAADDH;
586
587 buf[0] = CMD_DMAADDH;
588 buf[0] |= (da << 1);
589 *((u16 *)&buf[1]) = val;
590
591 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
592 da == 1 ? "DA" : "SA", val);
593
594 return SZ_DMAADDH;
595}
596
597static inline u32 _emit_END(unsigned dry_run, u8 buf[])
598{
599 if (dry_run)
600 return SZ_DMAEND;
601
602 buf[0] = CMD_DMAEND;
603
604 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
605
606 return SZ_DMAEND;
607}
608
609static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
610{
611 if (dry_run)
612 return SZ_DMAFLUSHP;
613
614 buf[0] = CMD_DMAFLUSHP;
615
616 peri &= 0x1f;
617 peri <<= 3;
618 buf[1] = peri;
619
620 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
621
622 return SZ_DMAFLUSHP;
623}
624
625static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
626{
627 if (dry_run)
628 return SZ_DMALD;
629
630 buf[0] = CMD_DMALD;
631
632 if (cond == SINGLE)
633 buf[0] |= (0 << 1) | (1 << 0);
634 else if (cond == BURST)
635 buf[0] |= (1 << 1) | (1 << 0);
636
637 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
638 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
639
640 return SZ_DMALD;
641}
642
643static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
644 enum pl330_cond cond, u8 peri)
645{
646 if (dry_run)
647 return SZ_DMALDP;
648
649 buf[0] = CMD_DMALDP;
650
651 if (cond == BURST)
652 buf[0] |= (1 << 1);
653
654 peri &= 0x1f;
655 peri <<= 3;
656 buf[1] = peri;
657
658 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
659 cond == SINGLE ? 'S' : 'B', peri >> 3);
660
661 return SZ_DMALDP;
662}
663
664static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
665 unsigned loop, u8 cnt)
666{
667 if (dry_run)
668 return SZ_DMALP;
669
670 buf[0] = CMD_DMALP;
671
672 if (loop)
673 buf[0] |= (1 << 1);
674
675 cnt--; /* DMAC increments by 1 internally */
676 buf[1] = cnt;
677
678 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
679
680 return SZ_DMALP;
681}
682
683struct _arg_LPEND {
684 enum pl330_cond cond;
685 bool forever;
686 unsigned loop;
687 u8 bjump;
688};
689
690static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
691 const struct _arg_LPEND *arg)
692{
693 enum pl330_cond cond = arg->cond;
694 bool forever = arg->forever;
695 unsigned loop = arg->loop;
696 u8 bjump = arg->bjump;
697
698 if (dry_run)
699 return SZ_DMALPEND;
700
701 buf[0] = CMD_DMALPEND;
702
703 if (loop)
704 buf[0] |= (1 << 2);
705
706 if (!forever)
707 buf[0] |= (1 << 4);
708
709 if (cond == SINGLE)
710 buf[0] |= (0 << 1) | (1 << 0);
711 else if (cond == BURST)
712 buf[0] |= (1 << 1) | (1 << 0);
713
714 buf[1] = bjump;
715
716 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
717 forever ? "FE" : "END",
718 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
719 loop ? '1' : '0',
720 bjump);
721
722 return SZ_DMALPEND;
723}
724
725static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
726{
727 if (dry_run)
728 return SZ_DMAKILL;
729
730 buf[0] = CMD_DMAKILL;
731
732 return SZ_DMAKILL;
733}
734
735static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
736 enum dmamov_dst dst, u32 val)
737{
738 if (dry_run)
739 return SZ_DMAMOV;
740
741 buf[0] = CMD_DMAMOV;
742 buf[1] = dst;
743 *((u32 *)&buf[2]) = val;
744
745 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
746 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
747
748 return SZ_DMAMOV;
749}
750
751static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
752{
753 if (dry_run)
754 return SZ_DMANOP;
755
756 buf[0] = CMD_DMANOP;
757
758 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
759
760 return SZ_DMANOP;
761}
762
763static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
764{
765 if (dry_run)
766 return SZ_DMARMB;
767
768 buf[0] = CMD_DMARMB;
769
770 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
771
772 return SZ_DMARMB;
773}
774
775static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
776{
777 if (dry_run)
778 return SZ_DMASEV;
779
780 buf[0] = CMD_DMASEV;
781
782 ev &= 0x1f;
783 ev <<= 3;
784 buf[1] = ev;
785
786 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
787
788 return SZ_DMASEV;
789}
790
791static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
792{
793 if (dry_run)
794 return SZ_DMAST;
795
796 buf[0] = CMD_DMAST;
797
798 if (cond == SINGLE)
799 buf[0] |= (0 << 1) | (1 << 0);
800 else if (cond == BURST)
801 buf[0] |= (1 << 1) | (1 << 0);
802
803 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
804 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
805
806 return SZ_DMAST;
807}
808
809static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
810 enum pl330_cond cond, u8 peri)
811{
812 if (dry_run)
813 return SZ_DMASTP;
814
815 buf[0] = CMD_DMASTP;
816
817 if (cond == BURST)
818 buf[0] |= (1 << 1);
819
820 peri &= 0x1f;
821 peri <<= 3;
822 buf[1] = peri;
823
824 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
825 cond == SINGLE ? 'S' : 'B', peri >> 3);
826
827 return SZ_DMASTP;
828}
829
830static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
831{
832 if (dry_run)
833 return SZ_DMASTZ;
834
835 buf[0] = CMD_DMASTZ;
836
837 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
838
839 return SZ_DMASTZ;
840}
841
842static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
843 unsigned invalidate)
844{
845 if (dry_run)
846 return SZ_DMAWFE;
847
848 buf[0] = CMD_DMAWFE;
849
850 ev &= 0x1f;
851 ev <<= 3;
852 buf[1] = ev;
853
854 if (invalidate)
855 buf[1] |= (1 << 1);
856
857 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
858 ev >> 3, invalidate ? ", I" : "");
859
860 return SZ_DMAWFE;
861}
862
863static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
864 enum pl330_cond cond, u8 peri)
865{
866 if (dry_run)
867 return SZ_DMAWFP;
868
869 buf[0] = CMD_DMAWFP;
870
871 if (cond == SINGLE)
872 buf[0] |= (0 << 1) | (0 << 0);
873 else if (cond == BURST)
874 buf[0] |= (1 << 1) | (0 << 0);
875 else
876 buf[0] |= (0 << 1) | (1 << 0);
877
878 peri &= 0x1f;
879 peri <<= 3;
880 buf[1] = peri;
881
882 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
883 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
884
885 return SZ_DMAWFP;
886}
887
888static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
889{
890 if (dry_run)
891 return SZ_DMAWMB;
892
893 buf[0] = CMD_DMAWMB;
894
895 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
896
897 return SZ_DMAWMB;
898}
899
900struct _arg_GO {
901 u8 chan;
902 u32 addr;
903 unsigned ns;
904};
905
906static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
907 const struct _arg_GO *arg)
908{
909 u8 chan = arg->chan;
910 u32 addr = arg->addr;
911 unsigned ns = arg->ns;
912
913 if (dry_run)
914 return SZ_DMAGO;
915
916 buf[0] = CMD_DMAGO;
917 buf[0] |= (ns << 1);
918
919 buf[1] = chan & 0x7;
920
921 *((u32 *)&buf[2]) = addr;
922
923 return SZ_DMAGO;
924}
925
926#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
927
928/* Returns Time-Out */
929static bool _until_dmac_idle(struct pl330_thread *thrd)
930{
931 void __iomem *regs = thrd->dmac->pinfo->base;
932 unsigned long loops = msecs_to_loops(5);
933
934 do {
935 /* Until Manager is Idle */
936 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
937 break;
938
939 cpu_relax();
940 } while (--loops);
941
942 if (!loops)
943 return true;
944
945 return false;
946}
947
948static inline void _execute_DBGINSN(struct pl330_thread *thrd,
949 u8 insn[], bool as_manager)
950{
951 void __iomem *regs = thrd->dmac->pinfo->base;
952 u32 val;
953
954 val = (insn[0] << 16) | (insn[1] << 24);
955 if (!as_manager) {
956 val |= (1 << 0);
957 val |= (thrd->id << 8); /* Channel Number */
958 }
959 writel(val, regs + DBGINST0);
960
961 val = *((u32 *)&insn[2]);
962 writel(val, regs + DBGINST1);
963
964 /* If timed out due to halted state-machine */
965 if (_until_dmac_idle(thrd)) {
966 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
967 return;
968 }
969
970 /* Get going */
971 writel(0, regs + DBGCMD);
972}
973
974/*
975 * Mark a _pl330_req as free.
976 * We do it by writing DMAEND as the first instruction
977 * because no valid request is going to have DMAEND as
978 * its first instruction to execute.
979 */
980static void mark_free(struct pl330_thread *thrd, int idx)
981{
982 struct _pl330_req *req = &thrd->req[idx];
983
984 _emit_END(0, req->mc_cpu);
Boojin Kimb7d861d2011-12-26 18:49:52 +0900985
986 thrd->req_running = -1;
987}
988
989static inline u32 _state(struct pl330_thread *thrd)
990{
991 void __iomem *regs = thrd->dmac->pinfo->base;
992 u32 val;
993
994 if (is_manager(thrd))
995 val = readl(regs + DS) & 0xf;
996 else
997 val = readl(regs + CS(thrd->id)) & 0xf;
998
999 switch (val) {
1000 case DS_ST_STOP:
1001 return PL330_STATE_STOPPED;
1002 case DS_ST_EXEC:
1003 return PL330_STATE_EXECUTING;
1004 case DS_ST_CMISS:
1005 return PL330_STATE_CACHEMISS;
1006 case DS_ST_UPDTPC:
1007 return PL330_STATE_UPDTPC;
1008 case DS_ST_WFE:
1009 return PL330_STATE_WFE;
1010 case DS_ST_FAULT:
1011 return PL330_STATE_FAULTING;
1012 case DS_ST_ATBRR:
1013 if (is_manager(thrd))
1014 return PL330_STATE_INVALID;
1015 else
1016 return PL330_STATE_ATBARRIER;
1017 case DS_ST_QBUSY:
1018 if (is_manager(thrd))
1019 return PL330_STATE_INVALID;
1020 else
1021 return PL330_STATE_QUEUEBUSY;
1022 case DS_ST_WFP:
1023 if (is_manager(thrd))
1024 return PL330_STATE_INVALID;
1025 else
1026 return PL330_STATE_WFP;
1027 case DS_ST_KILL:
1028 if (is_manager(thrd))
1029 return PL330_STATE_INVALID;
1030 else
1031 return PL330_STATE_KILLING;
1032 case DS_ST_CMPLT:
1033 if (is_manager(thrd))
1034 return PL330_STATE_INVALID;
1035 else
1036 return PL330_STATE_COMPLETING;
1037 case DS_ST_FLTCMP:
1038 if (is_manager(thrd))
1039 return PL330_STATE_INVALID;
1040 else
1041 return PL330_STATE_FAULT_COMPLETING;
1042 default:
1043 return PL330_STATE_INVALID;
1044 }
1045}
1046
1047static void _stop(struct pl330_thread *thrd)
1048{
1049 void __iomem *regs = thrd->dmac->pinfo->base;
1050 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1051
1052 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1053 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1054
1055 /* Return if nothing needs to be done */
1056 if (_state(thrd) == PL330_STATE_COMPLETING
1057 || _state(thrd) == PL330_STATE_KILLING
1058 || _state(thrd) == PL330_STATE_STOPPED)
1059 return;
1060
1061 _emit_KILL(0, insn);
1062
1063 /* Stop generating interrupts for SEV */
1064 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1065
1066 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1067}
1068
1069/* Start doing req 'idx' of thread 'thrd' */
1070static bool _trigger(struct pl330_thread *thrd)
1071{
1072 void __iomem *regs = thrd->dmac->pinfo->base;
1073 struct _pl330_req *req;
1074 struct pl330_req *r;
1075 struct _arg_GO go;
1076 unsigned ns;
1077 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1078 int idx;
1079
1080 /* Return if already ACTIVE */
1081 if (_state(thrd) != PL330_STATE_STOPPED)
1082 return true;
1083
1084 idx = 1 - thrd->lstenq;
1085 if (!IS_FREE(&thrd->req[idx]))
1086 req = &thrd->req[idx];
1087 else {
1088 idx = thrd->lstenq;
1089 if (!IS_FREE(&thrd->req[idx]))
1090 req = &thrd->req[idx];
1091 else
1092 req = NULL;
1093 }
1094
1095 /* Return if no request */
1096 if (!req || !req->r)
1097 return true;
1098
1099 r = req->r;
1100
1101 if (r->cfg)
1102 ns = r->cfg->nonsecure ? 1 : 0;
1103 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1104 ns = 1;
1105 else
1106 ns = 0;
1107
1108 /* See 'Abort Sources' point-4 at Page 2-25 */
1109 if (_manager_ns(thrd) && !ns)
1110 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1111 __func__, __LINE__);
1112
1113 go.chan = thrd->id;
1114 go.addr = req->mc_bus;
1115 go.ns = ns;
1116 _emit_GO(0, insn, &go);
1117
1118 /* Set to generate interrupts for SEV */
1119 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1120
1121 /* Only manager can execute GO */
1122 _execute_DBGINSN(thrd, insn, true);
1123
1124 thrd->req_running = idx;
1125
1126 return true;
1127}
1128
1129static bool _start(struct pl330_thread *thrd)
1130{
1131 switch (_state(thrd)) {
1132 case PL330_STATE_FAULT_COMPLETING:
1133 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1134
1135 if (_state(thrd) == PL330_STATE_KILLING)
1136 UNTIL(thrd, PL330_STATE_STOPPED)
1137
1138 case PL330_STATE_FAULTING:
1139 _stop(thrd);
1140
1141 case PL330_STATE_KILLING:
1142 case PL330_STATE_COMPLETING:
1143 UNTIL(thrd, PL330_STATE_STOPPED)
1144
1145 case PL330_STATE_STOPPED:
1146 return _trigger(thrd);
1147
1148 case PL330_STATE_WFP:
1149 case PL330_STATE_QUEUEBUSY:
1150 case PL330_STATE_ATBARRIER:
1151 case PL330_STATE_UPDTPC:
1152 case PL330_STATE_CACHEMISS:
1153 case PL330_STATE_EXECUTING:
1154 return true;
1155
1156 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1157 default:
1158 return false;
1159 }
1160}
1161
1162static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1163 const struct _xfer_spec *pxs, int cyc)
1164{
1165 int off = 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001166 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001167
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001168 /* check lock-up free version */
1169 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1170 while (cyc--) {
1171 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1172 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1173 }
1174 } else {
1175 while (cyc--) {
1176 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1177 off += _emit_RMB(dry_run, &buf[off]);
1178 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1179 off += _emit_WMB(dry_run, &buf[off]);
1180 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001181 }
1182
1183 return off;
1184}
1185
1186static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1187 const struct _xfer_spec *pxs, int cyc)
1188{
1189 int off = 0;
1190
1191 while (cyc--) {
1192 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1193 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1194 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1195 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1196 }
1197
1198 return off;
1199}
1200
1201static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1202 const struct _xfer_spec *pxs, int cyc)
1203{
1204 int off = 0;
1205
1206 while (cyc--) {
1207 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1208 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1209 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1210 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1211 }
1212
1213 return off;
1214}
1215
1216static int _bursts(unsigned dry_run, u8 buf[],
1217 const struct _xfer_spec *pxs, int cyc)
1218{
1219 int off = 0;
1220
1221 switch (pxs->r->rqtype) {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001222 case DMA_MEM_TO_DEV:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001223 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1224 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001225 case DMA_DEV_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001226 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1227 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001228 case DMA_MEM_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001229 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1230 break;
1231 default:
1232 off += 0x40000000; /* Scare off the Client */
1233 break;
1234 }
1235
1236 return off;
1237}
1238
1239/* Returns bytes consumed and updates bursts */
1240static inline int _loop(unsigned dry_run, u8 buf[],
1241 unsigned long *bursts, const struct _xfer_spec *pxs)
1242{
1243 int cyc, cycmax, szlp, szlpend, szbrst, off;
1244 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1245 struct _arg_LPEND lpend;
1246
1247 /* Max iterations possible in DMALP is 256 */
1248 if (*bursts >= 256*256) {
1249 lcnt1 = 256;
1250 lcnt0 = 256;
1251 cyc = *bursts / lcnt1 / lcnt0;
1252 } else if (*bursts > 256) {
1253 lcnt1 = 256;
1254 lcnt0 = *bursts / lcnt1;
1255 cyc = 1;
1256 } else {
1257 lcnt1 = *bursts;
1258 lcnt0 = 0;
1259 cyc = 1;
1260 }
1261
1262 szlp = _emit_LP(1, buf, 0, 0);
1263 szbrst = _bursts(1, buf, pxs, 1);
1264
1265 lpend.cond = ALWAYS;
1266 lpend.forever = false;
1267 lpend.loop = 0;
1268 lpend.bjump = 0;
1269 szlpend = _emit_LPEND(1, buf, &lpend);
1270
1271 if (lcnt0) {
1272 szlp *= 2;
1273 szlpend *= 2;
1274 }
1275
1276 /*
1277 * Max bursts that we can unroll due to limit on the
1278 * size of backward jump that can be encoded in DMALPEND
1279 * which is 8-bits and hence 255
1280 */
1281 cycmax = (255 - (szlp + szlpend)) / szbrst;
1282
1283 cyc = (cycmax < cyc) ? cycmax : cyc;
1284
1285 off = 0;
1286
1287 if (lcnt0) {
1288 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1289 ljmp0 = off;
1290 }
1291
1292 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1293 ljmp1 = off;
1294
1295 off += _bursts(dry_run, &buf[off], pxs, cyc);
1296
1297 lpend.cond = ALWAYS;
1298 lpend.forever = false;
1299 lpend.loop = 1;
1300 lpend.bjump = off - ljmp1;
1301 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1302
1303 if (lcnt0) {
1304 lpend.cond = ALWAYS;
1305 lpend.forever = false;
1306 lpend.loop = 0;
1307 lpend.bjump = off - ljmp0;
1308 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1309 }
1310
1311 *bursts = lcnt1 * cyc;
1312 if (lcnt0)
1313 *bursts *= lcnt0;
1314
1315 return off;
1316}
1317
1318static inline int _setup_loops(unsigned dry_run, u8 buf[],
1319 const struct _xfer_spec *pxs)
1320{
1321 struct pl330_xfer *x = pxs->x;
1322 u32 ccr = pxs->ccr;
1323 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1324 int off = 0;
1325
1326 while (bursts) {
1327 c = bursts;
1328 off += _loop(dry_run, &buf[off], &c, pxs);
1329 bursts -= c;
1330 }
1331
1332 return off;
1333}
1334
1335static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1336 const struct _xfer_spec *pxs)
1337{
1338 struct pl330_xfer *x = pxs->x;
1339 int off = 0;
1340
1341 /* DMAMOV SAR, x->src_addr */
1342 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1343 /* DMAMOV DAR, x->dst_addr */
1344 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1345
1346 /* Setup Loop(s) */
1347 off += _setup_loops(dry_run, &buf[off], pxs);
1348
1349 return off;
1350}
1351
1352/*
1353 * A req is a sequence of one or more xfer units.
1354 * Returns the number of bytes taken to setup the MC for the req.
1355 */
1356static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1357 unsigned index, struct _xfer_spec *pxs)
1358{
1359 struct _pl330_req *req = &thrd->req[index];
1360 struct pl330_xfer *x;
1361 u8 *buf = req->mc_cpu;
1362 int off = 0;
1363
1364 PL330_DBGMC_START(req->mc_bus);
1365
1366 /* DMAMOV CCR, ccr */
1367 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1368
1369 x = pxs->r->x;
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001370 /* Error if xfer length is not aligned at burst size */
1371 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1372 return -EINVAL;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001373
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001374 pxs->x = x;
1375 off += _setup_xfer(dry_run, &buf[off], pxs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001376
1377 /* DMASEV peripheral/event */
1378 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1379 /* DMAEND */
1380 off += _emit_END(dry_run, &buf[off]);
1381
1382 return off;
1383}
1384
1385static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1386{
1387 u32 ccr = 0;
1388
1389 if (rqc->src_inc)
1390 ccr |= CC_SRCINC;
1391
1392 if (rqc->dst_inc)
1393 ccr |= CC_DSTINC;
1394
1395 /* We set same protection levels for Src and DST for now */
1396 if (rqc->privileged)
1397 ccr |= CC_SRCPRI | CC_DSTPRI;
1398 if (rqc->nonsecure)
1399 ccr |= CC_SRCNS | CC_DSTNS;
1400 if (rqc->insnaccess)
1401 ccr |= CC_SRCIA | CC_DSTIA;
1402
1403 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1404 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1405
1406 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1407 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1408
1409 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1410 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1411
1412 ccr |= (rqc->swap << CC_SWAP_SHFT);
1413
1414 return ccr;
1415}
1416
Boojin Kimb7d861d2011-12-26 18:49:52 +09001417/*
1418 * Submit a list of xfers after which the client wants notification.
1419 * Client is not notified after each xfer unit, just once after all
1420 * xfer units are done or some error occurs.
1421 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001422static int pl330_submit_req(struct pl330_thread *thrd, struct pl330_req *r)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001423{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001424 struct pl330_dmac *pl330;
1425 struct pl330_info *pi;
1426 struct _xfer_spec xs;
1427 unsigned long flags;
1428 void __iomem *regs;
1429 unsigned idx;
1430 u32 ccr;
1431 int ret = 0;
1432
1433 /* No Req or Unacquired Channel or DMAC */
1434 if (!r || !thrd || thrd->free)
1435 return -EINVAL;
1436
1437 pl330 = thrd->dmac;
1438 pi = pl330->pinfo;
1439 regs = pi->base;
1440
1441 if (pl330->state == DYING
1442 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1443 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1444 __func__, __LINE__);
1445 return -EAGAIN;
1446 }
1447
1448 /* If request for non-existing peripheral */
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001449 if (r->rqtype != DMA_MEM_TO_MEM && r->peri >= pi->pcfg.num_peri) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001450 dev_info(thrd->dmac->pinfo->dev,
1451 "%s:%d Invalid peripheral(%u)!\n",
1452 __func__, __LINE__, r->peri);
1453 return -EINVAL;
1454 }
1455
1456 spin_lock_irqsave(&pl330->lock, flags);
1457
1458 if (_queue_full(thrd)) {
1459 ret = -EAGAIN;
1460 goto xfer_exit;
1461 }
1462
Boojin Kimb7d861d2011-12-26 18:49:52 +09001463
1464 /* Use last settings, if not provided */
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301465 if (r->cfg) {
1466 /* Prefer Secure Channel */
1467 if (!_manager_ns(thrd))
1468 r->cfg->nonsecure = 0;
1469 else
1470 r->cfg->nonsecure = 1;
1471
Boojin Kimb7d861d2011-12-26 18:49:52 +09001472 ccr = _prepare_ccr(r->cfg);
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301473 } else {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001474 ccr = readl(regs + CC(thrd->id));
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301475 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001476
Boojin Kimb7d861d2011-12-26 18:49:52 +09001477 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1478
1479 xs.ccr = ccr;
1480 xs.r = r;
1481
1482 /* First dry run to check if req is acceptable */
1483 ret = _setup_req(1, thrd, idx, &xs);
1484 if (ret < 0)
1485 goto xfer_exit;
1486
1487 if (ret > pi->mcbufsz / 2) {
1488 dev_info(thrd->dmac->pinfo->dev,
1489 "%s:%d Trying increasing mcbufsz\n",
1490 __func__, __LINE__);
1491 ret = -ENOMEM;
1492 goto xfer_exit;
1493 }
1494
1495 /* Hook the request */
1496 thrd->lstenq = idx;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001497 thrd->req[idx].r = r;
Lars-Peter Clausenbe025322014-07-06 20:32:24 +02001498 _setup_req(0, thrd, idx, &xs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001499
1500 ret = 0;
1501
1502xfer_exit:
1503 spin_unlock_irqrestore(&pl330->lock, flags);
1504
1505 return ret;
1506}
1507
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001508static void dma_pl330_rqcb(struct pl330_req *req, enum pl330_op_err err)
1509{
1510 struct dma_pl330_desc *desc = container_of(req, struct dma_pl330_desc, req);
1511 struct dma_pl330_chan *pch = desc->pchan;
1512 unsigned long flags;
1513
1514 /* If desc aborted */
1515 if (!pch)
1516 return;
1517
1518 spin_lock_irqsave(&pch->lock, flags);
1519
1520 desc->status = DONE;
1521
1522 spin_unlock_irqrestore(&pch->lock, flags);
1523
1524 tasklet_schedule(&pch->task);
1525}
1526
Boojin Kimb7d861d2011-12-26 18:49:52 +09001527static void pl330_dotask(unsigned long data)
1528{
1529 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1530 struct pl330_info *pi = pl330->pinfo;
1531 unsigned long flags;
1532 int i;
1533
1534 spin_lock_irqsave(&pl330->lock, flags);
1535
1536 /* The DMAC itself gone nuts */
1537 if (pl330->dmac_tbd.reset_dmac) {
1538 pl330->state = DYING;
1539 /* Reset the manager too */
1540 pl330->dmac_tbd.reset_mngr = true;
1541 /* Clear the reset flag */
1542 pl330->dmac_tbd.reset_dmac = false;
1543 }
1544
1545 if (pl330->dmac_tbd.reset_mngr) {
1546 _stop(pl330->manager);
1547 /* Reset all channels */
1548 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1549 /* Clear the reset flag */
1550 pl330->dmac_tbd.reset_mngr = false;
1551 }
1552
1553 for (i = 0; i < pi->pcfg.num_chan; i++) {
1554
1555 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1556 struct pl330_thread *thrd = &pl330->channels[i];
1557 void __iomem *regs = pi->base;
1558 enum pl330_op_err err;
1559
1560 _stop(thrd);
1561
1562 if (readl(regs + FSC) & (1 << thrd->id))
1563 err = PL330_ERR_FAIL;
1564 else
1565 err = PL330_ERR_ABORT;
1566
1567 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001568 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, err);
1569 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, err);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001570 spin_lock_irqsave(&pl330->lock, flags);
1571
1572 thrd->req[0].r = NULL;
1573 thrd->req[1].r = NULL;
1574 mark_free(thrd, 0);
1575 mark_free(thrd, 1);
1576
1577 /* Clear the reset flag */
1578 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1579 }
1580 }
1581
1582 spin_unlock_irqrestore(&pl330->lock, flags);
1583
1584 return;
1585}
1586
1587/* Returns 1 if state was updated, 0 otherwise */
1588static int pl330_update(const struct pl330_info *pi)
1589{
Javi Merinofdec53d2012-06-13 15:07:00 +01001590 struct pl330_req *rqdone, *tmp;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001591 struct pl330_dmac *pl330;
1592 unsigned long flags;
1593 void __iomem *regs;
1594 u32 val;
1595 int id, ev, ret = 0;
1596
1597 if (!pi || !pi->pl330_data)
1598 return 0;
1599
1600 regs = pi->base;
1601 pl330 = pi->pl330_data;
1602
1603 spin_lock_irqsave(&pl330->lock, flags);
1604
1605 val = readl(regs + FSM) & 0x1;
1606 if (val)
1607 pl330->dmac_tbd.reset_mngr = true;
1608 else
1609 pl330->dmac_tbd.reset_mngr = false;
1610
1611 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1612 pl330->dmac_tbd.reset_chan |= val;
1613 if (val) {
1614 int i = 0;
1615 while (i < pi->pcfg.num_chan) {
1616 if (val & (1 << i)) {
1617 dev_info(pi->dev,
1618 "Reset Channel-%d\t CS-%x FTC-%x\n",
1619 i, readl(regs + CS(i)),
1620 readl(regs + FTC(i)));
1621 _stop(&pl330->channels[i]);
1622 }
1623 i++;
1624 }
1625 }
1626
1627 /* Check which event happened i.e, thread notified */
1628 val = readl(regs + ES);
1629 if (pi->pcfg.num_events < 32
1630 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1631 pl330->dmac_tbd.reset_dmac = true;
1632 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1633 ret = 1;
1634 goto updt_exit;
1635 }
1636
1637 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1638 if (val & (1 << ev)) { /* Event occurred */
1639 struct pl330_thread *thrd;
1640 u32 inten = readl(regs + INTEN);
1641 int active;
1642
1643 /* Clear the event */
1644 if (inten & (1 << ev))
1645 writel(1 << ev, regs + INTCLR);
1646
1647 ret = 1;
1648
1649 id = pl330->events[ev];
1650
1651 thrd = &pl330->channels[id];
1652
1653 active = thrd->req_running;
1654 if (active == -1) /* Aborted */
1655 continue;
1656
Javi Merinofdec53d2012-06-13 15:07:00 +01001657 /* Detach the req */
1658 rqdone = thrd->req[active].r;
1659 thrd->req[active].r = NULL;
1660
Boojin Kimb7d861d2011-12-26 18:49:52 +09001661 mark_free(thrd, active);
1662
1663 /* Get going again ASAP */
1664 _start(thrd);
1665
1666 /* For now, just make a list of callbacks to be done */
1667 list_add_tail(&rqdone->rqd, &pl330->req_done);
1668 }
1669 }
1670
1671 /* Now that we are in no hurry, do the callbacks */
Javi Merinofdec53d2012-06-13 15:07:00 +01001672 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1673 list_del(&rqdone->rqd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001674
1675 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001676 dma_pl330_rqcb(rqdone, PL330_ERR_NONE);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001677 spin_lock_irqsave(&pl330->lock, flags);
1678 }
1679
1680updt_exit:
1681 spin_unlock_irqrestore(&pl330->lock, flags);
1682
1683 if (pl330->dmac_tbd.reset_dmac
1684 || pl330->dmac_tbd.reset_mngr
1685 || pl330->dmac_tbd.reset_chan) {
1686 ret = 1;
1687 tasklet_schedule(&pl330->tasks);
1688 }
1689
1690 return ret;
1691}
1692
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001693static int pl330_chan_ctrl(struct pl330_thread *thrd, enum pl330_chan_op op)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001694{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001695 struct pl330_dmac *pl330;
1696 unsigned long flags;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001697 int ret = 0, active;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001698
1699 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1700 return -EINVAL;
1701
1702 pl330 = thrd->dmac;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001703 active = thrd->req_running;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001704
1705 spin_lock_irqsave(&pl330->lock, flags);
1706
1707 switch (op) {
1708 case PL330_OP_FLUSH:
1709 /* Make sure the channel is stopped */
1710 _stop(thrd);
1711
1712 thrd->req[0].r = NULL;
1713 thrd->req[1].r = NULL;
1714 mark_free(thrd, 0);
1715 mark_free(thrd, 1);
1716 break;
1717
1718 case PL330_OP_ABORT:
1719 /* Make sure the channel is stopped */
1720 _stop(thrd);
1721
1722 /* ABORT is only for the active req */
1723 if (active == -1)
1724 break;
1725
1726 thrd->req[active].r = NULL;
1727 mark_free(thrd, active);
1728
1729 /* Start the next */
1730 case PL330_OP_START:
1731 if ((active == -1) && !_start(thrd))
1732 ret = -EIO;
1733 break;
1734
1735 default:
1736 ret = -EINVAL;
1737 }
1738
1739 spin_unlock_irqrestore(&pl330->lock, flags);
1740 return ret;
1741}
1742
Boojin Kimb7d861d2011-12-26 18:49:52 +09001743/* Reserve an event */
1744static inline int _alloc_event(struct pl330_thread *thrd)
1745{
1746 struct pl330_dmac *pl330 = thrd->dmac;
1747 struct pl330_info *pi = pl330->pinfo;
1748 int ev;
1749
1750 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1751 if (pl330->events[ev] == -1) {
1752 pl330->events[ev] = thrd->id;
1753 return ev;
1754 }
1755
1756 return -1;
1757}
1758
1759static bool _chan_ns(const struct pl330_info *pi, int i)
1760{
1761 return pi->pcfg.irq_ns & (1 << i);
1762}
1763
1764/* Upon success, returns IdentityToken for the
1765 * allocated channel, NULL otherwise.
1766 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001767static struct pl330_thread *pl330_request_channel(const struct pl330_info *pi)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001768{
1769 struct pl330_thread *thrd = NULL;
1770 struct pl330_dmac *pl330;
1771 unsigned long flags;
1772 int chans, i;
1773
1774 if (!pi || !pi->pl330_data)
1775 return NULL;
1776
1777 pl330 = pi->pl330_data;
1778
1779 if (pl330->state == DYING)
1780 return NULL;
1781
1782 chans = pi->pcfg.num_chan;
1783
1784 spin_lock_irqsave(&pl330->lock, flags);
1785
1786 for (i = 0; i < chans; i++) {
1787 thrd = &pl330->channels[i];
1788 if ((thrd->free) && (!_manager_ns(thrd) ||
1789 _chan_ns(pi, i))) {
1790 thrd->ev = _alloc_event(thrd);
1791 if (thrd->ev >= 0) {
1792 thrd->free = false;
1793 thrd->lstenq = 1;
1794 thrd->req[0].r = NULL;
1795 mark_free(thrd, 0);
1796 thrd->req[1].r = NULL;
1797 mark_free(thrd, 1);
1798 break;
1799 }
1800 }
1801 thrd = NULL;
1802 }
1803
1804 spin_unlock_irqrestore(&pl330->lock, flags);
1805
1806 return thrd;
1807}
1808
1809/* Release an event */
1810static inline void _free_event(struct pl330_thread *thrd, int ev)
1811{
1812 struct pl330_dmac *pl330 = thrd->dmac;
1813 struct pl330_info *pi = pl330->pinfo;
1814
1815 /* If the event is valid and was held by the thread */
1816 if (ev >= 0 && ev < pi->pcfg.num_events
1817 && pl330->events[ev] == thrd->id)
1818 pl330->events[ev] = -1;
1819}
1820
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001821static void pl330_release_channel(struct pl330_thread *thrd)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001822{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001823 struct pl330_dmac *pl330;
1824 unsigned long flags;
1825
1826 if (!thrd || thrd->free)
1827 return;
1828
1829 _stop(thrd);
1830
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001831 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1832 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001833
1834 pl330 = thrd->dmac;
1835
1836 spin_lock_irqsave(&pl330->lock, flags);
1837 _free_event(thrd, thrd->ev);
1838 thrd->free = true;
1839 spin_unlock_irqrestore(&pl330->lock, flags);
1840}
1841
1842/* Initialize the structure for PL330 configuration, that can be used
1843 * by the client driver the make best use of the DMAC
1844 */
1845static void read_dmac_config(struct pl330_info *pi)
1846{
1847 void __iomem *regs = pi->base;
1848 u32 val;
1849
1850 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1851 val &= CRD_DATA_WIDTH_MASK;
1852 pi->pcfg.data_bus_width = 8 * (1 << val);
1853
1854 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1855 val &= CRD_DATA_BUFF_MASK;
1856 pi->pcfg.data_buf_dep = val + 1;
1857
1858 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1859 val &= CR0_NUM_CHANS_MASK;
1860 val += 1;
1861 pi->pcfg.num_chan = val;
1862
1863 val = readl(regs + CR0);
1864 if (val & CR0_PERIPH_REQ_SET) {
1865 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1866 val += 1;
1867 pi->pcfg.num_peri = val;
1868 pi->pcfg.peri_ns = readl(regs + CR4);
1869 } else {
1870 pi->pcfg.num_peri = 0;
1871 }
1872
1873 val = readl(regs + CR0);
1874 if (val & CR0_BOOT_MAN_NS)
1875 pi->pcfg.mode |= DMAC_MODE_NS;
1876 else
1877 pi->pcfg.mode &= ~DMAC_MODE_NS;
1878
1879 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1880 val &= CR0_NUM_EVENTS_MASK;
1881 val += 1;
1882 pi->pcfg.num_events = val;
1883
1884 pi->pcfg.irq_ns = readl(regs + CR3);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001885}
1886
1887static inline void _reset_thread(struct pl330_thread *thrd)
1888{
1889 struct pl330_dmac *pl330 = thrd->dmac;
1890 struct pl330_info *pi = pl330->pinfo;
1891
1892 thrd->req[0].mc_cpu = pl330->mcode_cpu
1893 + (thrd->id * pi->mcbufsz);
1894 thrd->req[0].mc_bus = pl330->mcode_bus
1895 + (thrd->id * pi->mcbufsz);
1896 thrd->req[0].r = NULL;
1897 mark_free(thrd, 0);
1898
1899 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1900 + pi->mcbufsz / 2;
1901 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1902 + pi->mcbufsz / 2;
1903 thrd->req[1].r = NULL;
1904 mark_free(thrd, 1);
1905}
1906
1907static int dmac_alloc_threads(struct pl330_dmac *pl330)
1908{
1909 struct pl330_info *pi = pl330->pinfo;
1910 int chans = pi->pcfg.num_chan;
1911 struct pl330_thread *thrd;
1912 int i;
1913
1914 /* Allocate 1 Manager and 'chans' Channel threads */
1915 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
1916 GFP_KERNEL);
1917 if (!pl330->channels)
1918 return -ENOMEM;
1919
1920 /* Init Channel threads */
1921 for (i = 0; i < chans; i++) {
1922 thrd = &pl330->channels[i];
1923 thrd->id = i;
1924 thrd->dmac = pl330;
1925 _reset_thread(thrd);
1926 thrd->free = true;
1927 }
1928
1929 /* MANAGER is indexed at the end */
1930 thrd = &pl330->channels[chans];
1931 thrd->id = chans;
1932 thrd->dmac = pl330;
1933 thrd->free = false;
1934 pl330->manager = thrd;
1935
1936 return 0;
1937}
1938
1939static int dmac_alloc_resources(struct pl330_dmac *pl330)
1940{
1941 struct pl330_info *pi = pl330->pinfo;
1942 int chans = pi->pcfg.num_chan;
1943 int ret;
1944
1945 /*
1946 * Alloc MicroCode buffer for 'chans' Channel threads.
1947 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1948 */
1949 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
1950 chans * pi->mcbufsz,
1951 &pl330->mcode_bus, GFP_KERNEL);
1952 if (!pl330->mcode_cpu) {
1953 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
1954 __func__, __LINE__);
1955 return -ENOMEM;
1956 }
1957
1958 ret = dmac_alloc_threads(pl330);
1959 if (ret) {
1960 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
1961 __func__, __LINE__);
1962 dma_free_coherent(pi->dev,
1963 chans * pi->mcbufsz,
1964 pl330->mcode_cpu, pl330->mcode_bus);
1965 return ret;
1966 }
1967
1968 return 0;
1969}
1970
1971static int pl330_add(struct pl330_info *pi)
1972{
1973 struct pl330_dmac *pl330;
1974 void __iomem *regs;
1975 int i, ret;
1976
1977 if (!pi || !pi->dev)
1978 return -EINVAL;
1979
1980 /* If already added */
1981 if (pi->pl330_data)
1982 return -EINVAL;
1983
Boojin Kimb7d861d2011-12-26 18:49:52 +09001984 regs = pi->base;
1985
1986 /* Check if we can handle this DMAC */
Will Deacon09677172013-06-10 19:34:37 +01001987 if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
1988 dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001989 return -EINVAL;
1990 }
1991
1992 /* Read the configuration of the DMAC */
1993 read_dmac_config(pi);
1994
1995 if (pi->pcfg.num_events == 0) {
1996 dev_err(pi->dev, "%s:%d Can't work without events!\n",
1997 __func__, __LINE__);
1998 return -EINVAL;
1999 }
2000
2001 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2002 if (!pl330) {
2003 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2004 __func__, __LINE__);
2005 return -ENOMEM;
2006 }
2007
2008 /* Assign the info structure and private data */
2009 pl330->pinfo = pi;
2010 pi->pl330_data = pl330;
2011
2012 spin_lock_init(&pl330->lock);
2013
2014 INIT_LIST_HEAD(&pl330->req_done);
2015
2016 /* Use default MC buffer size if not provided */
2017 if (!pi->mcbufsz)
2018 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2019
2020 /* Mark all events as free */
2021 for (i = 0; i < pi->pcfg.num_events; i++)
2022 pl330->events[i] = -1;
2023
2024 /* Allocate resources needed by the DMAC */
2025 ret = dmac_alloc_resources(pl330);
2026 if (ret) {
2027 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2028 kfree(pl330);
2029 return ret;
2030 }
2031
2032 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2033
2034 pl330->state = INIT;
2035
2036 return 0;
2037}
2038
2039static int dmac_free_threads(struct pl330_dmac *pl330)
2040{
2041 struct pl330_info *pi = pl330->pinfo;
2042 int chans = pi->pcfg.num_chan;
2043 struct pl330_thread *thrd;
2044 int i;
2045
2046 /* Release Channel threads */
2047 for (i = 0; i < chans; i++) {
2048 thrd = &pl330->channels[i];
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002049 pl330_release_channel(thrd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002050 }
2051
2052 /* Free memory */
2053 kfree(pl330->channels);
2054
2055 return 0;
2056}
2057
2058static void dmac_free_resources(struct pl330_dmac *pl330)
2059{
2060 struct pl330_info *pi = pl330->pinfo;
2061 int chans = pi->pcfg.num_chan;
2062
2063 dmac_free_threads(pl330);
2064
2065 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2066 pl330->mcode_cpu, pl330->mcode_bus);
2067}
2068
2069static void pl330_del(struct pl330_info *pi)
2070{
2071 struct pl330_dmac *pl330;
2072
2073 if (!pi || !pi->pl330_data)
2074 return;
2075
2076 pl330 = pi->pl330_data;
2077
2078 pl330->state = UNINIT;
2079
2080 tasklet_kill(&pl330->tasks);
2081
2082 /* Free DMAC resources */
2083 dmac_free_resources(pl330);
2084
2085 kfree(pl330);
2086 pi->pl330_data = NULL;
2087}
2088
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002089/* forward declaration */
2090static struct amba_driver pl330_driver;
2091
Jassi Brarb3040e42010-05-23 20:28:19 -07002092static inline struct dma_pl330_chan *
2093to_pchan(struct dma_chan *ch)
2094{
2095 if (!ch)
2096 return NULL;
2097
2098 return container_of(ch, struct dma_pl330_chan, chan);
2099}
2100
2101static inline struct dma_pl330_desc *
2102to_desc(struct dma_async_tx_descriptor *tx)
2103{
2104 return container_of(tx, struct dma_pl330_desc, txd);
2105}
2106
Jassi Brarb3040e42010-05-23 20:28:19 -07002107static inline void fill_queue(struct dma_pl330_chan *pch)
2108{
2109 struct dma_pl330_desc *desc;
2110 int ret;
2111
2112 list_for_each_entry(desc, &pch->work_list, node) {
2113
2114 /* If already submitted */
2115 if (desc->status == BUSY)
Jassi Brar30fb9802013-02-13 16:13:14 +05302116 continue;
Jassi Brarb3040e42010-05-23 20:28:19 -07002117
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002118 ret = pl330_submit_req(pch->thread, &desc->req);
Jassi Brarb3040e42010-05-23 20:28:19 -07002119 if (!ret) {
2120 desc->status = BUSY;
Jassi Brarb3040e42010-05-23 20:28:19 -07002121 } else if (ret == -EAGAIN) {
2122 /* QFull or DMAC Dying */
2123 break;
2124 } else {
2125 /* Unacceptable request */
2126 desc->status = DONE;
2127 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2128 __func__, __LINE__, desc->txd.cookie);
2129 tasklet_schedule(&pch->task);
2130 }
2131 }
2132}
2133
2134static void pl330_tasklet(unsigned long data)
2135{
2136 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2137 struct dma_pl330_desc *desc, *_dt;
2138 unsigned long flags;
Jassi Brarb3040e42010-05-23 20:28:19 -07002139
2140 spin_lock_irqsave(&pch->lock, flags);
2141
2142 /* Pick up ripe tomatoes */
2143 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2144 if (desc->status == DONE) {
Tushar Behera30c1dc02012-05-23 16:47:31 +05302145 if (!pch->cyclic)
Vinod Kouleab21582012-05-11 11:24:41 +05302146 dma_cookie_complete(&desc->txd);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002147 list_move_tail(&desc->node, &pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002148 }
2149
2150 /* Try to submit a req imm. next to the last completed cookie */
2151 fill_queue(pch);
2152
2153 /* Make sure the PL330 Channel thread is active */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002154 pl330_chan_ctrl(pch->thread, PL330_OP_START);
Jassi Brarb3040e42010-05-23 20:28:19 -07002155
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002156 while (!list_empty(&pch->completed_list)) {
2157 dma_async_tx_callback callback;
2158 void *callback_param;
Jassi Brarb3040e42010-05-23 20:28:19 -07002159
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002160 desc = list_first_entry(&pch->completed_list,
2161 struct dma_pl330_desc, node);
2162
2163 callback = desc->txd.callback;
2164 callback_param = desc->txd.callback_param;
2165
2166 if (pch->cyclic) {
2167 desc->status = PREP;
2168 list_move_tail(&desc->node, &pch->work_list);
2169 } else {
2170 desc->status = FREE;
2171 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2172 }
2173
Dan Williamsd38a8c62013-10-18 19:35:23 +02002174 dma_descriptor_unmap(&desc->txd);
2175
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002176 if (callback) {
2177 spin_unlock_irqrestore(&pch->lock, flags);
2178 callback(callback_param);
2179 spin_lock_irqsave(&pch->lock, flags);
2180 }
2181 }
2182 spin_unlock_irqrestore(&pch->lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002183}
2184
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002185bool pl330_filter(struct dma_chan *chan, void *param)
2186{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002187 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002188
2189 if (chan->device->dev->driver != &pl330_driver.drv)
2190 return false;
2191
Thomas Abrahamcd072512011-10-24 11:43:11 +02002192 peri_id = chan->private;
Dan Carpenter2f986ec2013-11-08 12:51:16 +03002193 return *peri_id == (unsigned long)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002194}
2195EXPORT_SYMBOL(pl330_filter);
2196
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302197static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2198 struct of_dma *ofdma)
2199{
2200 int count = dma_spec->args_count;
2201 struct dma_pl330_dmac *pdmac = ofdma->of_dma_data;
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002202 unsigned int chan_id;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302203
2204 if (count != 1)
2205 return NULL;
2206
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002207 chan_id = dma_spec->args[0];
2208 if (chan_id >= pdmac->num_peripherals)
2209 return NULL;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302210
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002211 return dma_get_slave_channel(&pdmac->peripherals[chan_id].chan);
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302212}
2213
Jassi Brarb3040e42010-05-23 20:28:19 -07002214static int pl330_alloc_chan_resources(struct dma_chan *chan)
2215{
2216 struct dma_pl330_chan *pch = to_pchan(chan);
2217 struct dma_pl330_dmac *pdmac = pch->dmac;
2218 unsigned long flags;
2219
2220 spin_lock_irqsave(&pch->lock, flags);
2221
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002222 dma_cookie_init(chan);
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002223 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002224
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002225 pch->thread = pl330_request_channel(&pdmac->pif);
2226 if (!pch->thread) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002227 spin_unlock_irqrestore(&pch->lock, flags);
Inderpal Singh02747882012-09-17 09:57:45 +05302228 return -ENOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002229 }
2230
2231 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2232
2233 spin_unlock_irqrestore(&pch->lock, flags);
2234
2235 return 1;
2236}
2237
2238static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2239{
2240 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002241 struct dma_pl330_desc *desc;
Jassi Brarb3040e42010-05-23 20:28:19 -07002242 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002243 struct dma_pl330_dmac *pdmac = pch->dmac;
2244 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002245 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002246
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002247 switch (cmd) {
2248 case DMA_TERMINATE_ALL:
2249 spin_lock_irqsave(&pch->lock, flags);
2250
2251 /* FLUSH the PL330 Channel thread */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002252 pl330_chan_ctrl(pch->thread, PL330_OP_FLUSH);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002253
2254 /* Mark all desc done */
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002255 list_for_each_entry(desc, &pch->submitted_list, node) {
2256 desc->status = FREE;
2257 dma_cookie_complete(&desc->txd);
2258 }
2259
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002260 list_for_each_entry(desc, &pch->work_list , node) {
2261 desc->status = FREE;
2262 dma_cookie_complete(&desc->txd);
Boojin Kimae43b882011-09-02 09:44:32 +09002263 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002264
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002265 list_for_each_entry(desc, &pch->completed_list , node) {
2266 desc->status = FREE;
2267 dma_cookie_complete(&desc->txd);
2268 }
2269
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002270 list_splice_tail_init(&pch->submitted_list, &pdmac->desc_pool);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002271 list_splice_tail_init(&pch->work_list, &pdmac->desc_pool);
2272 list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002273 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002274 break;
2275 case DMA_SLAVE_CONFIG:
2276 slave_config = (struct dma_slave_config *)arg;
2277
Vinod Kouldb8196d2011-10-13 22:34:23 +05302278 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002279 if (slave_config->dst_addr)
2280 pch->fifo_addr = slave_config->dst_addr;
2281 if (slave_config->dst_addr_width)
2282 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2283 if (slave_config->dst_maxburst)
2284 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302285 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002286 if (slave_config->src_addr)
2287 pch->fifo_addr = slave_config->src_addr;
2288 if (slave_config->src_addr_width)
2289 pch->burst_sz = __ffs(slave_config->src_addr_width);
2290 if (slave_config->src_maxburst)
2291 pch->burst_len = slave_config->src_maxburst;
2292 }
2293 break;
2294 default:
2295 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002296 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002297 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002298
2299 return 0;
2300}
2301
2302static void pl330_free_chan_resources(struct dma_chan *chan)
2303{
2304 struct dma_pl330_chan *pch = to_pchan(chan);
2305 unsigned long flags;
2306
Jassi Brarb3040e42010-05-23 20:28:19 -07002307 tasklet_kill(&pch->task);
2308
Bartlomiej Zolnierkiewiczda331ba2013-07-03 15:00:43 -07002309 spin_lock_irqsave(&pch->lock, flags);
2310
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002311 pl330_release_channel(pch->thread);
2312 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002313
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002314 if (pch->cyclic)
2315 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2316
Jassi Brarb3040e42010-05-23 20:28:19 -07002317 spin_unlock_irqrestore(&pch->lock, flags);
2318}
2319
2320static enum dma_status
2321pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2322 struct dma_tx_state *txstate)
2323{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002324 return dma_cookie_status(chan, cookie, txstate);
Jassi Brarb3040e42010-05-23 20:28:19 -07002325}
2326
2327static void pl330_issue_pending(struct dma_chan *chan)
2328{
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002329 struct dma_pl330_chan *pch = to_pchan(chan);
2330 unsigned long flags;
2331
2332 spin_lock_irqsave(&pch->lock, flags);
2333 list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2334 spin_unlock_irqrestore(&pch->lock, flags);
2335
2336 pl330_tasklet((unsigned long)pch);
Jassi Brarb3040e42010-05-23 20:28:19 -07002337}
2338
2339/*
2340 * We returned the last one of the circular list of descriptor(s)
2341 * from prep_xxx, so the argument to submit corresponds to the last
2342 * descriptor of the list.
2343 */
2344static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2345{
2346 struct dma_pl330_desc *desc, *last = to_desc(tx);
2347 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2348 dma_cookie_t cookie;
2349 unsigned long flags;
2350
2351 spin_lock_irqsave(&pch->lock, flags);
2352
2353 /* Assign cookies to all nodes */
Jassi Brarb3040e42010-05-23 20:28:19 -07002354 while (!list_empty(&last->node)) {
2355 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002356 if (pch->cyclic) {
2357 desc->txd.callback = last->txd.callback;
2358 desc->txd.callback_param = last->txd.callback_param;
2359 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002360
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002361 dma_cookie_assign(&desc->txd);
Jassi Brarb3040e42010-05-23 20:28:19 -07002362
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002363 list_move_tail(&desc->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002364 }
2365
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002366 cookie = dma_cookie_assign(&last->txd);
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002367 list_add_tail(&last->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002368 spin_unlock_irqrestore(&pch->lock, flags);
2369
2370 return cookie;
2371}
2372
2373static inline void _init_desc(struct dma_pl330_desc *desc)
2374{
Jassi Brarb3040e42010-05-23 20:28:19 -07002375 desc->req.x = &desc->px;
Jassi Brarb3040e42010-05-23 20:28:19 -07002376 desc->rqcfg.swap = SWAP_NO;
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02002377 desc->rqcfg.scctl = CCTRL0;
2378 desc->rqcfg.dcctl = CCTRL0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002379 desc->req.cfg = &desc->rqcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002380 desc->txd.tx_submit = pl330_tx_submit;
2381
2382 INIT_LIST_HEAD(&desc->node);
2383}
2384
2385/* Returns the number of descriptors added to the DMAC pool */
Sachin Kamat5a67ac52012-06-04 17:09:45 +05302386static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
Jassi Brarb3040e42010-05-23 20:28:19 -07002387{
2388 struct dma_pl330_desc *desc;
2389 unsigned long flags;
2390 int i;
2391
2392 if (!pdmac)
2393 return 0;
2394
Will Deacon0baf8f62013-12-02 18:01:30 +00002395 desc = kcalloc(count, sizeof(*desc), flg);
Jassi Brarb3040e42010-05-23 20:28:19 -07002396 if (!desc)
2397 return 0;
2398
2399 spin_lock_irqsave(&pdmac->pool_lock, flags);
2400
2401 for (i = 0; i < count; i++) {
2402 _init_desc(&desc[i]);
2403 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2404 }
2405
2406 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2407
2408 return count;
2409}
2410
2411static struct dma_pl330_desc *
2412pluck_desc(struct dma_pl330_dmac *pdmac)
2413{
2414 struct dma_pl330_desc *desc = NULL;
2415 unsigned long flags;
2416
2417 if (!pdmac)
2418 return NULL;
2419
2420 spin_lock_irqsave(&pdmac->pool_lock, flags);
2421
2422 if (!list_empty(&pdmac->desc_pool)) {
2423 desc = list_entry(pdmac->desc_pool.next,
2424 struct dma_pl330_desc, node);
2425
2426 list_del_init(&desc->node);
2427
2428 desc->status = PREP;
2429 desc->txd.callback = NULL;
2430 }
2431
2432 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2433
2434 return desc;
2435}
2436
2437static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2438{
2439 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002440 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002441 struct dma_pl330_desc *desc;
2442
2443 /* Pluck one desc from the pool of DMAC */
2444 desc = pluck_desc(pdmac);
2445
2446 /* If the DMAC pool is empty, alloc new */
2447 if (!desc) {
2448 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2449 return NULL;
2450
2451 /* Try again */
2452 desc = pluck_desc(pdmac);
2453 if (!desc) {
2454 dev_err(pch->dmac->pif.dev,
2455 "%s:%d ALERT!\n", __func__, __LINE__);
2456 return NULL;
2457 }
2458 }
2459
2460 /* Initialize the descriptor */
2461 desc->pchan = pch;
2462 desc->txd.cookie = 0;
2463 async_tx_ack(&desc->txd);
2464
Thomas Abrahamcd072512011-10-24 11:43:11 +02002465 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09002466 desc->rqcfg.pcfg = &pch->dmac->pif.pcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002467
2468 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2469
2470 return desc;
2471}
2472
2473static inline void fill_px(struct pl330_xfer *px,
2474 dma_addr_t dst, dma_addr_t src, size_t len)
2475{
Jassi Brarb3040e42010-05-23 20:28:19 -07002476 px->bytes = len;
2477 px->dst_addr = dst;
2478 px->src_addr = src;
2479}
2480
2481static struct dma_pl330_desc *
2482__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2483 dma_addr_t src, size_t len)
2484{
2485 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2486
2487 if (!desc) {
2488 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2489 __func__, __LINE__);
2490 return NULL;
2491 }
2492
2493 /*
2494 * Ideally we should lookout for reqs bigger than
2495 * those that can be programmed with 256 bytes of
2496 * MC buffer, but considering a req size is seldom
2497 * going to be word-unaligned and more than 200MB,
2498 * we take it easy.
2499 * Also, should the limit is reached we'd rather
2500 * have the platform increase MC buffer size than
2501 * complicating this API driver.
2502 */
2503 fill_px(&desc->px, dst, src, len);
2504
2505 return desc;
2506}
2507
2508/* Call after fixing burst size */
2509static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2510{
2511 struct dma_pl330_chan *pch = desc->pchan;
2512 struct pl330_info *pi = &pch->dmac->pif;
2513 int burst_len;
2514
2515 burst_len = pi->pcfg.data_bus_width / 8;
2516 burst_len *= pi->pcfg.data_buf_dep;
2517 burst_len >>= desc->rqcfg.brst_size;
2518
2519 /* src/dst_burst_len can't be more than 16 */
2520 if (burst_len > 16)
2521 burst_len = 16;
2522
2523 while (burst_len > 1) {
2524 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2525 break;
2526 burst_len--;
2527 }
2528
2529 return burst_len;
2530}
2531
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002532static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2533 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002534 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002535 unsigned long flags, void *context)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002536{
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002537 struct dma_pl330_desc *desc = NULL, *first = NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002538 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002539 struct dma_pl330_dmac *pdmac = pch->dmac;
2540 unsigned int i;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002541 dma_addr_t dst;
2542 dma_addr_t src;
2543
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002544 if (len % period_len != 0)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002545 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002546
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002547 if (!is_slave_direction(direction)) {
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002548 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2549 __func__, __LINE__);
2550 return NULL;
2551 }
2552
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002553 for (i = 0; i < len / period_len; i++) {
2554 desc = pl330_get_desc(pch);
2555 if (!desc) {
2556 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2557 __func__, __LINE__);
2558
2559 if (!first)
2560 return NULL;
2561
2562 spin_lock_irqsave(&pdmac->pool_lock, flags);
2563
2564 while (!list_empty(&first->node)) {
2565 desc = list_entry(first->node.next,
2566 struct dma_pl330_desc, node);
2567 list_move_tail(&desc->node, &pdmac->desc_pool);
2568 }
2569
2570 list_move_tail(&first->node, &pdmac->desc_pool);
2571
2572 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2573
2574 return NULL;
2575 }
2576
2577 switch (direction) {
2578 case DMA_MEM_TO_DEV:
2579 desc->rqcfg.src_inc = 1;
2580 desc->rqcfg.dst_inc = 0;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002581 src = dma_addr;
2582 dst = pch->fifo_addr;
2583 break;
2584 case DMA_DEV_TO_MEM:
2585 desc->rqcfg.src_inc = 0;
2586 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002587 src = pch->fifo_addr;
2588 dst = dma_addr;
2589 break;
2590 default:
2591 break;
2592 }
2593
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002594 desc->req.rqtype = direction;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002595 desc->rqcfg.brst_size = pch->burst_sz;
2596 desc->rqcfg.brst_len = 1;
2597 fill_px(&desc->px, dst, src, period_len);
2598
2599 if (!first)
2600 first = desc;
2601 else
2602 list_add_tail(&desc->node, &first->node);
2603
2604 dma_addr += period_len;
2605 }
2606
2607 if (!desc)
2608 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002609
2610 pch->cyclic = true;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002611 desc->txd.flags = flags;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002612
2613 return &desc->txd;
2614}
2615
Jassi Brarb3040e42010-05-23 20:28:19 -07002616static struct dma_async_tx_descriptor *
2617pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2618 dma_addr_t src, size_t len, unsigned long flags)
2619{
2620 struct dma_pl330_desc *desc;
2621 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002622 struct pl330_info *pi;
2623 int burst;
2624
Rob Herring4e0e6102011-07-25 16:05:04 -05002625 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002626 return NULL;
2627
Jassi Brarb3040e42010-05-23 20:28:19 -07002628 pi = &pch->dmac->pif;
2629
2630 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2631 if (!desc)
2632 return NULL;
2633
2634 desc->rqcfg.src_inc = 1;
2635 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002636 desc->req.rqtype = DMA_MEM_TO_MEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002637
2638 /* Select max possible burst size */
2639 burst = pi->pcfg.data_bus_width / 8;
2640
2641 while (burst > 1) {
2642 if (!(len % burst))
2643 break;
2644 burst /= 2;
2645 }
2646
2647 desc->rqcfg.brst_size = 0;
2648 while (burst != (1 << desc->rqcfg.brst_size))
2649 desc->rqcfg.brst_size++;
2650
2651 desc->rqcfg.brst_len = get_burst_len(desc, len);
2652
2653 desc->txd.flags = flags;
2654
2655 return &desc->txd;
2656}
2657
Chanho Park52a9d172013-08-09 20:11:33 +09002658static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac,
2659 struct dma_pl330_desc *first)
2660{
2661 unsigned long flags;
2662 struct dma_pl330_desc *desc;
2663
2664 if (!first)
2665 return;
2666
2667 spin_lock_irqsave(&pdmac->pool_lock, flags);
2668
2669 while (!list_empty(&first->node)) {
2670 desc = list_entry(first->node.next,
2671 struct dma_pl330_desc, node);
2672 list_move_tail(&desc->node, &pdmac->desc_pool);
2673 }
2674
2675 list_move_tail(&first->node, &pdmac->desc_pool);
2676
2677 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2678}
2679
Jassi Brarb3040e42010-05-23 20:28:19 -07002680static struct dma_async_tx_descriptor *
2681pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302682 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002683 unsigned long flg, void *context)
Jassi Brarb3040e42010-05-23 20:28:19 -07002684{
2685 struct dma_pl330_desc *first, *desc = NULL;
2686 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002687 struct scatterlist *sg;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002688 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002689 dma_addr_t addr;
2690
Thomas Abrahamcd072512011-10-24 11:43:11 +02002691 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002692 return NULL;
2693
Boojin Kim1b9bb712011-09-02 09:44:30 +09002694 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002695
2696 first = NULL;
2697
2698 for_each_sg(sgl, sg, sg_len, i) {
2699
2700 desc = pl330_get_desc(pch);
2701 if (!desc) {
2702 struct dma_pl330_dmac *pdmac = pch->dmac;
2703
2704 dev_err(pch->dmac->pif.dev,
2705 "%s:%d Unable to fetch desc\n",
2706 __func__, __LINE__);
Chanho Park52a9d172013-08-09 20:11:33 +09002707 __pl330_giveback_desc(pdmac, first);
Jassi Brarb3040e42010-05-23 20:28:19 -07002708
2709 return NULL;
2710 }
2711
2712 if (!first)
2713 first = desc;
2714 else
2715 list_add_tail(&desc->node, &first->node);
2716
Vinod Kouldb8196d2011-10-13 22:34:23 +05302717 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002718 desc->rqcfg.src_inc = 1;
2719 desc->rqcfg.dst_inc = 0;
2720 fill_px(&desc->px,
2721 addr, sg_dma_address(sg), sg_dma_len(sg));
2722 } else {
2723 desc->rqcfg.src_inc = 0;
2724 desc->rqcfg.dst_inc = 1;
2725 fill_px(&desc->px,
2726 sg_dma_address(sg), addr, sg_dma_len(sg));
2727 }
2728
Boojin Kim1b9bb712011-09-02 09:44:30 +09002729 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002730 desc->rqcfg.brst_len = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002731 desc->req.rqtype = direction;
Jassi Brarb3040e42010-05-23 20:28:19 -07002732 }
2733
2734 /* Return the last desc in the chain */
2735 desc->txd.flags = flg;
2736 return &desc->txd;
2737}
2738
2739static irqreturn_t pl330_irq_handler(int irq, void *data)
2740{
2741 if (pl330_update(data))
2742 return IRQ_HANDLED;
2743 else
2744 return IRQ_NONE;
2745}
2746
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002747#define PL330_DMA_BUSWIDTHS \
2748 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2749 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2750 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2751 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2752 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2753
2754static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2755 struct dma_slave_caps *caps)
2756{
2757 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2758 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2759 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2760 caps->cmd_pause = false;
2761 caps->cmd_terminate = true;
Lars-Peter Clausenbfb9bb42014-01-11 14:02:17 +01002762 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002763
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002764 return 0;
2765}
2766
Bill Pemberton463a1f82012-11-19 13:22:55 -05002767static int
Russell Kingaa25afa2011-02-19 15:55:00 +00002768pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002769{
2770 struct dma_pl330_platdata *pdat;
2771 struct dma_pl330_dmac *pdmac;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302772 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002773 struct pl330_info *pi;
2774 struct dma_device *pd;
2775 struct resource *res;
2776 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002777 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002778
Jingoo Hand4adcc02013-07-30 17:09:11 +09002779 pdat = dev_get_platdata(&adev->dev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002780
Russell King64113012013-06-27 10:29:32 +01002781 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2782 if (ret)
2783 return ret;
2784
Jassi Brarb3040e42010-05-23 20:28:19 -07002785 /* Allocate a new DMAC and its Channels */
Sachin Kamate4d43c12012-11-15 06:27:50 +00002786 pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002787 if (!pdmac) {
2788 dev_err(&adev->dev, "unable to allocate mem\n");
2789 return -ENOMEM;
2790 }
2791
2792 pi = &pdmac->pif;
2793 pi->dev = &adev->dev;
2794 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002795 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002796
2797 res = &adev->res;
Sachin Kamatbcc7fa92013-03-04 14:36:27 +05302798 pi->base = devm_ioremap_resource(&adev->dev, res);
2799 if (IS_ERR(pi->base))
2800 return PTR_ERR(pi->base);
Jassi Brarb3040e42010-05-23 20:28:19 -07002801
Boojin Kima2f52032011-09-02 09:44:29 +09002802 amba_set_drvdata(adev, pdmac);
2803
Dan Carpenter02808b42013-11-08 12:50:24 +03002804 for (i = 0; i < AMBA_NR_IRQS; i++) {
Michal Simeke98b3ca2013-09-30 08:50:48 +02002805 irq = adev->irq[i];
2806 if (irq) {
2807 ret = devm_request_irq(&adev->dev, irq,
2808 pl330_irq_handler, 0,
2809 dev_name(&adev->dev), pi);
2810 if (ret)
2811 return ret;
2812 } else {
2813 break;
2814 }
2815 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002816
Will Deacon09677172013-06-10 19:34:37 +01002817 pi->pcfg.periph_id = adev->periphid;
Jassi Brarb3040e42010-05-23 20:28:19 -07002818 ret = pl330_add(pi);
2819 if (ret)
Michal Simek173e8382013-09-04 16:40:17 +02002820 return ret;
Jassi Brarb3040e42010-05-23 20:28:19 -07002821
2822 INIT_LIST_HEAD(&pdmac->desc_pool);
2823 spin_lock_init(&pdmac->pool_lock);
2824
2825 /* Create a descriptor pool of default size */
2826 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2827 dev_warn(&adev->dev, "unable to allocate desc\n");
2828
2829 pd = &pdmac->ddma;
2830 INIT_LIST_HEAD(&pd->channels);
2831
2832 /* Initialize channel parameters */
Olof Johanssonc8473822012-04-08 16:26:19 -07002833 if (pdat)
2834 num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan);
2835 else
2836 num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan);
2837
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002838 pdmac->num_peripherals = num_chan;
2839
Rob Herring4e0e6102011-07-25 16:05:04 -05002840 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Sachin Kamat61c6e752012-09-17 15:20:23 +05302841 if (!pdmac->peripherals) {
2842 ret = -ENOMEM;
2843 dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n");
Sachin Kamate4d43c12012-11-15 06:27:50 +00002844 goto probe_err2;
Sachin Kamat61c6e752012-09-17 15:20:23 +05302845 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002846
Rob Herring4e0e6102011-07-25 16:05:04 -05002847 for (i = 0; i < num_chan; i++) {
2848 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002849 if (!adev->dev.of_node)
2850 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2851 else
2852 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002853
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002854 INIT_LIST_HEAD(&pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002855 INIT_LIST_HEAD(&pch->work_list);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002856 INIT_LIST_HEAD(&pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002857 spin_lock_init(&pch->lock);
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002858 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002859 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07002860 pch->dmac = pdmac;
2861
2862 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002863 list_add_tail(&pch->chan.device_node, &pd->channels);
2864 }
2865
2866 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002867 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002868 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002869 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002870 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002871 if (pi->pcfg.num_peri) {
2872 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2873 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
Tushar Behera5557a412012-08-29 10:16:25 +05302874 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002875 }
2876 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002877
2878 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2879 pd->device_free_chan_resources = pl330_free_chan_resources;
2880 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002881 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07002882 pd->device_tx_status = pl330_tx_status;
2883 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2884 pd->device_control = pl330_control;
2885 pd->device_issue_pending = pl330_issue_pending;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002886 pd->device_slave_caps = pl330_dma_device_slave_caps;
Jassi Brarb3040e42010-05-23 20:28:19 -07002887
2888 ret = dma_async_device_register(pd);
2889 if (ret) {
2890 dev_err(&adev->dev, "unable to register DMAC\n");
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302891 goto probe_err3;
2892 }
2893
2894 if (adev->dev.of_node) {
2895 ret = of_dma_controller_register(adev->dev.of_node,
2896 of_dma_pl330_xlate, pdmac);
2897 if (ret) {
2898 dev_err(&adev->dev,
2899 "unable to register DMA to the generic DT DMA helpers\n");
2900 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002901 }
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01002902
2903 adev->dev.dma_parms = &pdmac->dma_parms;
2904
Vinod Kouldbaf6d82013-09-02 21:54:48 +05302905 /*
2906 * This is the limit for transfers with a buswidth of 1, larger
2907 * buswidths will have larger limits.
2908 */
2909 ret = dma_set_max_seg_size(&adev->dev, 1900800);
2910 if (ret)
2911 dev_err(&adev->dev, "unable to set the seg size\n");
2912
Jassi Brarb3040e42010-05-23 20:28:19 -07002913
Jassi Brarb3040e42010-05-23 20:28:19 -07002914 dev_info(&adev->dev,
2915 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
2916 dev_info(&adev->dev,
2917 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
2918 pi->pcfg.data_buf_dep,
2919 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
2920 pi->pcfg.num_peri, pi->pcfg.num_events);
2921
2922 return 0;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302923probe_err3:
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302924 /* Idle the DMAC */
2925 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
2926 chan.device_node) {
2927
2928 /* Remove the channel */
2929 list_del(&pch->chan.device_node);
2930
2931 /* Flush the channel */
2932 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2933 pl330_free_chan_resources(&pch->chan);
2934 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002935probe_err2:
Sachin Kamate4d43c12012-11-15 06:27:50 +00002936 pl330_del(pi);
Jassi Brarb3040e42010-05-23 20:28:19 -07002937
2938 return ret;
2939}
2940
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002941static int pl330_remove(struct amba_device *adev)
Jassi Brarb3040e42010-05-23 20:28:19 -07002942{
2943 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
2944 struct dma_pl330_chan *pch, *_p;
2945 struct pl330_info *pi;
Jassi Brarb3040e42010-05-23 20:28:19 -07002946
2947 if (!pdmac)
2948 return 0;
2949
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302950 if (adev->dev.of_node)
2951 of_dma_controller_free(adev->dev.of_node);
Padmavathi Venna421da892013-02-14 09:10:07 +05302952
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302953 dma_async_device_unregister(&pdmac->ddma);
Jassi Brarb3040e42010-05-23 20:28:19 -07002954
2955 /* Idle the DMAC */
2956 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
2957 chan.device_node) {
2958
2959 /* Remove the channel */
2960 list_del(&pch->chan.device_node);
2961
2962 /* Flush the channel */
2963 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2964 pl330_free_chan_resources(&pch->chan);
2965 }
2966
2967 pi = &pdmac->pif;
2968
2969 pl330_del(pi);
2970
Jassi Brarb3040e42010-05-23 20:28:19 -07002971 return 0;
2972}
2973
2974static struct amba_id pl330_ids[] = {
2975 {
2976 .id = 0x00041330,
2977 .mask = 0x000fffff,
2978 },
2979 { 0, 0 },
2980};
2981
Dave Martine8fa5162011-10-05 15:15:20 +01002982MODULE_DEVICE_TABLE(amba, pl330_ids);
2983
Jassi Brarb3040e42010-05-23 20:28:19 -07002984static struct amba_driver pl330_driver = {
2985 .drv = {
2986 .owner = THIS_MODULE,
2987 .name = "dma-pl330",
Jassi Brarb3040e42010-05-23 20:28:19 -07002988 },
2989 .id_table = pl330_ids,
2990 .probe = pl330_probe,
2991 .remove = pl330_remove,
2992};
2993
viresh kumar9e5ed092012-03-15 10:40:38 +01002994module_amba_driver(pl330_driver);
Jassi Brarb3040e42010-05-23 20:28:19 -07002995
2996MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
2997MODULE_DESCRIPTION("API Driver for PL330 DMAC");
2998MODULE_LICENSE("GPL");