blob: 6b61c578c8689df32141ca41c17eba5cb726256f [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;
337 /*
338 * Pointer to next xfer in the list.
339 * The last xfer in the req must point to NULL.
340 */
341 struct pl330_xfer *next;
342};
343
344/* The xfer callbacks are made with one of these arguments. */
345enum pl330_op_err {
346 /* The all xfers in the request were success. */
347 PL330_ERR_NONE,
348 /* If req aborted due to global error. */
349 PL330_ERR_ABORT,
350 /* If req failed due to problem with Channel. */
351 PL330_ERR_FAIL,
352};
353
354/* A request defining Scatter-Gather List ending with NULL xfer. */
355struct pl330_req {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +0200356 enum dma_transfer_direction rqtype;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900357 /* Index of peripheral for the xfer. */
358 unsigned peri:5;
359 /* Unique token for this xfer, set by the client. */
360 void *token;
361 /* Callback to be called after xfer. */
362 void (*xfer_cb)(void *token, enum pl330_op_err err);
363 /* If NULL, req will be done at last set parameters. */
364 struct pl330_reqcfg *cfg;
365 /* Pointer to first xfer in the request. */
366 struct pl330_xfer *x;
Javi Merinofdec53d2012-06-13 15:07:00 +0100367 /* Hook to attach to DMAC's list of reqs with due callback */
368 struct list_head rqd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900369};
370
Boojin Kimb7d861d2011-12-26 18:49:52 +0900371enum pl330_chan_op {
372 /* Start the channel */
373 PL330_OP_START,
374 /* Abort the active xfer */
375 PL330_OP_ABORT,
376 /* Stop xfer and flush queue */
377 PL330_OP_FLUSH,
378};
379
380struct _xfer_spec {
381 u32 ccr;
382 struct pl330_req *r;
383 struct pl330_xfer *x;
384};
385
386enum dmamov_dst {
387 SAR = 0,
388 CCR,
389 DAR,
390};
391
392enum pl330_dst {
393 SRC = 0,
394 DST,
395};
396
397enum pl330_cond {
398 SINGLE,
399 BURST,
400 ALWAYS,
401};
402
403struct _pl330_req {
404 u32 mc_bus;
405 void *mc_cpu;
406 /* Number of bytes taken to setup MC for the req */
407 u32 mc_len;
408 struct pl330_req *r;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900409};
410
411/* ToBeDone for tasklet */
412struct _pl330_tbd {
413 bool reset_dmac;
414 bool reset_mngr;
415 u8 reset_chan;
416};
417
418/* A DMAC Thread */
419struct pl330_thread {
420 u8 id;
421 int ev;
422 /* If the channel is not yet acquired by any client */
423 bool free;
424 /* Parent DMAC */
425 struct pl330_dmac *dmac;
426 /* Only two at a time */
427 struct _pl330_req req[2];
428 /* Index of the last enqueued request */
429 unsigned lstenq;
430 /* Index of the last submitted request or -1 if the DMA is stopped */
431 int req_running;
432};
433
434enum pl330_dmac_state {
435 UNINIT,
436 INIT,
437 DYING,
438};
439
440/* A DMAC */
441struct pl330_dmac {
442 spinlock_t lock;
443 /* Holds list of reqs with due callbacks */
444 struct list_head req_done;
445 /* Pointer to platform specific stuff */
446 struct pl330_info *pinfo;
447 /* Maximum possible events/irqs */
448 int events[32];
449 /* BUS address of MicroCode buffer */
Will Deaconfed8c452013-06-10 19:34:38 +0100450 dma_addr_t mcode_bus;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900451 /* CPU address of MicroCode buffer */
452 void *mcode_cpu;
453 /* List of all Channel threads */
454 struct pl330_thread *channels;
455 /* Pointer to the MANAGER thread */
456 struct pl330_thread *manager;
457 /* To handle bad news in interrupt */
458 struct tasklet_struct tasks;
459 struct _pl330_tbd dmac_tbd;
460 /* State of DMAC operation */
461 enum pl330_dmac_state state;
462};
463
Jassi Brarb3040e42010-05-23 20:28:19 -0700464enum desc_status {
465 /* In the DMAC pool */
466 FREE,
467 /*
Masanari Iidad73111c2012-08-04 23:37:53 +0900468 * Allocated to some channel during prep_xxx
Jassi Brarb3040e42010-05-23 20:28:19 -0700469 * Also may be sitting on the work_list.
470 */
471 PREP,
472 /*
473 * Sitting on the work_list and already submitted
474 * to the PL330 core. Not more than two descriptors
475 * of a channel can be BUSY at any time.
476 */
477 BUSY,
478 /*
479 * Sitting on the channel work_list but xfer done
480 * by PL330 core
481 */
482 DONE,
483};
484
485struct dma_pl330_chan {
486 /* Schedule desc completion */
487 struct tasklet_struct task;
488
489 /* DMA-Engine Channel */
490 struct dma_chan chan;
491
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +0100492 /* List of submitted descriptors */
493 struct list_head submitted_list;
494 /* List of issued descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700495 struct list_head work_list;
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +0200496 /* List of completed descriptors */
497 struct list_head completed_list;
Jassi Brarb3040e42010-05-23 20:28:19 -0700498
499 /* Pointer to the DMAC that manages this channel,
500 * NULL if the channel is available to be acquired.
501 * As the parent, this DMAC also provides descriptors
502 * to the channel.
503 */
504 struct dma_pl330_dmac *dmac;
505
506 /* To protect channel manipulation */
507 spinlock_t lock;
508
509 /* Token of a hardware channel thread of PL330 DMAC
510 * NULL if the channel is available to be acquired.
511 */
512 void *pl330_chid;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900513
514 /* For D-to-M and M-to-D channels */
515 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900516 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900517 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900518
519 /* for cyclic capability */
520 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700521};
522
523struct dma_pl330_dmac {
524 struct pl330_info pif;
525
526 /* DMA-Engine Device */
527 struct dma_device ddma;
528
Lars-Peter Clausenb714b842013-11-25 16:07:46 +0100529 /* Holds info about sg limitations */
530 struct device_dma_parameters dma_parms;
531
Jassi Brarb3040e42010-05-23 20:28:19 -0700532 /* Pool of descriptors available for the DMAC's channels */
533 struct list_head desc_pool;
534 /* To protect desc_pool manipulation */
535 spinlock_t pool_lock;
536
537 /* Peripheral channels connected to this DMAC */
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +0100538 unsigned int num_peripherals;
Rob Herring4e0e6102011-07-25 16:05:04 -0500539 struct dma_pl330_chan *peripherals; /* keep at end */
Jassi Brarb3040e42010-05-23 20:28:19 -0700540};
541
542struct dma_pl330_desc {
543 /* To attach to a queue as child */
544 struct list_head node;
545
546 /* Descriptor for the DMA Engine API */
547 struct dma_async_tx_descriptor txd;
548
549 /* Xfer for PL330 core */
550 struct pl330_xfer px;
551
552 struct pl330_reqcfg rqcfg;
553 struct pl330_req req;
554
555 enum desc_status status;
556
557 /* The channel which currently holds this desc */
558 struct dma_pl330_chan *pchan;
559};
560
Boojin Kimb7d861d2011-12-26 18:49:52 +0900561static inline void _callback(struct pl330_req *r, enum pl330_op_err err)
562{
563 if (r && r->xfer_cb)
564 r->xfer_cb(r->token, err);
565}
566
567static inline bool _queue_empty(struct pl330_thread *thrd)
568{
569 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
570 ? true : false;
571}
572
573static inline bool _queue_full(struct pl330_thread *thrd)
574{
575 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
576 ? false : true;
577}
578
579static inline bool is_manager(struct pl330_thread *thrd)
580{
581 struct pl330_dmac *pl330 = thrd->dmac;
582
583 /* MANAGER is indexed at the end */
584 if (thrd->id == pl330->pinfo->pcfg.num_chan)
585 return true;
586 else
587 return false;
588}
589
590/* If manager of the thread is in Non-Secure mode */
591static inline bool _manager_ns(struct pl330_thread *thrd)
592{
593 struct pl330_dmac *pl330 = thrd->dmac;
594
595 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
596}
597
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900598static inline u32 get_revision(u32 periph_id)
599{
600 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
601}
602
Boojin Kimb7d861d2011-12-26 18:49:52 +0900603static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
604 enum pl330_dst da, u16 val)
605{
606 if (dry_run)
607 return SZ_DMAADDH;
608
609 buf[0] = CMD_DMAADDH;
610 buf[0] |= (da << 1);
611 *((u16 *)&buf[1]) = val;
612
613 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
614 da == 1 ? "DA" : "SA", val);
615
616 return SZ_DMAADDH;
617}
618
619static inline u32 _emit_END(unsigned dry_run, u8 buf[])
620{
621 if (dry_run)
622 return SZ_DMAEND;
623
624 buf[0] = CMD_DMAEND;
625
626 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
627
628 return SZ_DMAEND;
629}
630
631static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
632{
633 if (dry_run)
634 return SZ_DMAFLUSHP;
635
636 buf[0] = CMD_DMAFLUSHP;
637
638 peri &= 0x1f;
639 peri <<= 3;
640 buf[1] = peri;
641
642 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
643
644 return SZ_DMAFLUSHP;
645}
646
647static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
648{
649 if (dry_run)
650 return SZ_DMALD;
651
652 buf[0] = CMD_DMALD;
653
654 if (cond == SINGLE)
655 buf[0] |= (0 << 1) | (1 << 0);
656 else if (cond == BURST)
657 buf[0] |= (1 << 1) | (1 << 0);
658
659 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
660 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
661
662 return SZ_DMALD;
663}
664
665static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
666 enum pl330_cond cond, u8 peri)
667{
668 if (dry_run)
669 return SZ_DMALDP;
670
671 buf[0] = CMD_DMALDP;
672
673 if (cond == BURST)
674 buf[0] |= (1 << 1);
675
676 peri &= 0x1f;
677 peri <<= 3;
678 buf[1] = peri;
679
680 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
681 cond == SINGLE ? 'S' : 'B', peri >> 3);
682
683 return SZ_DMALDP;
684}
685
686static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
687 unsigned loop, u8 cnt)
688{
689 if (dry_run)
690 return SZ_DMALP;
691
692 buf[0] = CMD_DMALP;
693
694 if (loop)
695 buf[0] |= (1 << 1);
696
697 cnt--; /* DMAC increments by 1 internally */
698 buf[1] = cnt;
699
700 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
701
702 return SZ_DMALP;
703}
704
705struct _arg_LPEND {
706 enum pl330_cond cond;
707 bool forever;
708 unsigned loop;
709 u8 bjump;
710};
711
712static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
713 const struct _arg_LPEND *arg)
714{
715 enum pl330_cond cond = arg->cond;
716 bool forever = arg->forever;
717 unsigned loop = arg->loop;
718 u8 bjump = arg->bjump;
719
720 if (dry_run)
721 return SZ_DMALPEND;
722
723 buf[0] = CMD_DMALPEND;
724
725 if (loop)
726 buf[0] |= (1 << 2);
727
728 if (!forever)
729 buf[0] |= (1 << 4);
730
731 if (cond == SINGLE)
732 buf[0] |= (0 << 1) | (1 << 0);
733 else if (cond == BURST)
734 buf[0] |= (1 << 1) | (1 << 0);
735
736 buf[1] = bjump;
737
738 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
739 forever ? "FE" : "END",
740 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
741 loop ? '1' : '0',
742 bjump);
743
744 return SZ_DMALPEND;
745}
746
747static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
748{
749 if (dry_run)
750 return SZ_DMAKILL;
751
752 buf[0] = CMD_DMAKILL;
753
754 return SZ_DMAKILL;
755}
756
757static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
758 enum dmamov_dst dst, u32 val)
759{
760 if (dry_run)
761 return SZ_DMAMOV;
762
763 buf[0] = CMD_DMAMOV;
764 buf[1] = dst;
765 *((u32 *)&buf[2]) = val;
766
767 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
768 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
769
770 return SZ_DMAMOV;
771}
772
773static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
774{
775 if (dry_run)
776 return SZ_DMANOP;
777
778 buf[0] = CMD_DMANOP;
779
780 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
781
782 return SZ_DMANOP;
783}
784
785static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
786{
787 if (dry_run)
788 return SZ_DMARMB;
789
790 buf[0] = CMD_DMARMB;
791
792 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
793
794 return SZ_DMARMB;
795}
796
797static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
798{
799 if (dry_run)
800 return SZ_DMASEV;
801
802 buf[0] = CMD_DMASEV;
803
804 ev &= 0x1f;
805 ev <<= 3;
806 buf[1] = ev;
807
808 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
809
810 return SZ_DMASEV;
811}
812
813static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
814{
815 if (dry_run)
816 return SZ_DMAST;
817
818 buf[0] = CMD_DMAST;
819
820 if (cond == SINGLE)
821 buf[0] |= (0 << 1) | (1 << 0);
822 else if (cond == BURST)
823 buf[0] |= (1 << 1) | (1 << 0);
824
825 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
826 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
827
828 return SZ_DMAST;
829}
830
831static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
832 enum pl330_cond cond, u8 peri)
833{
834 if (dry_run)
835 return SZ_DMASTP;
836
837 buf[0] = CMD_DMASTP;
838
839 if (cond == BURST)
840 buf[0] |= (1 << 1);
841
842 peri &= 0x1f;
843 peri <<= 3;
844 buf[1] = peri;
845
846 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
847 cond == SINGLE ? 'S' : 'B', peri >> 3);
848
849 return SZ_DMASTP;
850}
851
852static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
853{
854 if (dry_run)
855 return SZ_DMASTZ;
856
857 buf[0] = CMD_DMASTZ;
858
859 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
860
861 return SZ_DMASTZ;
862}
863
864static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
865 unsigned invalidate)
866{
867 if (dry_run)
868 return SZ_DMAWFE;
869
870 buf[0] = CMD_DMAWFE;
871
872 ev &= 0x1f;
873 ev <<= 3;
874 buf[1] = ev;
875
876 if (invalidate)
877 buf[1] |= (1 << 1);
878
879 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
880 ev >> 3, invalidate ? ", I" : "");
881
882 return SZ_DMAWFE;
883}
884
885static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
886 enum pl330_cond cond, u8 peri)
887{
888 if (dry_run)
889 return SZ_DMAWFP;
890
891 buf[0] = CMD_DMAWFP;
892
893 if (cond == SINGLE)
894 buf[0] |= (0 << 1) | (0 << 0);
895 else if (cond == BURST)
896 buf[0] |= (1 << 1) | (0 << 0);
897 else
898 buf[0] |= (0 << 1) | (1 << 0);
899
900 peri &= 0x1f;
901 peri <<= 3;
902 buf[1] = peri;
903
904 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
905 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
906
907 return SZ_DMAWFP;
908}
909
910static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
911{
912 if (dry_run)
913 return SZ_DMAWMB;
914
915 buf[0] = CMD_DMAWMB;
916
917 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
918
919 return SZ_DMAWMB;
920}
921
922struct _arg_GO {
923 u8 chan;
924 u32 addr;
925 unsigned ns;
926};
927
928static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
929 const struct _arg_GO *arg)
930{
931 u8 chan = arg->chan;
932 u32 addr = arg->addr;
933 unsigned ns = arg->ns;
934
935 if (dry_run)
936 return SZ_DMAGO;
937
938 buf[0] = CMD_DMAGO;
939 buf[0] |= (ns << 1);
940
941 buf[1] = chan & 0x7;
942
943 *((u32 *)&buf[2]) = addr;
944
945 return SZ_DMAGO;
946}
947
948#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
949
950/* Returns Time-Out */
951static bool _until_dmac_idle(struct pl330_thread *thrd)
952{
953 void __iomem *regs = thrd->dmac->pinfo->base;
954 unsigned long loops = msecs_to_loops(5);
955
956 do {
957 /* Until Manager is Idle */
958 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
959 break;
960
961 cpu_relax();
962 } while (--loops);
963
964 if (!loops)
965 return true;
966
967 return false;
968}
969
970static inline void _execute_DBGINSN(struct pl330_thread *thrd,
971 u8 insn[], bool as_manager)
972{
973 void __iomem *regs = thrd->dmac->pinfo->base;
974 u32 val;
975
976 val = (insn[0] << 16) | (insn[1] << 24);
977 if (!as_manager) {
978 val |= (1 << 0);
979 val |= (thrd->id << 8); /* Channel Number */
980 }
981 writel(val, regs + DBGINST0);
982
983 val = *((u32 *)&insn[2]);
984 writel(val, regs + DBGINST1);
985
986 /* If timed out due to halted state-machine */
987 if (_until_dmac_idle(thrd)) {
988 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
989 return;
990 }
991
992 /* Get going */
993 writel(0, regs + DBGCMD);
994}
995
996/*
997 * Mark a _pl330_req as free.
998 * We do it by writing DMAEND as the first instruction
999 * because no valid request is going to have DMAEND as
1000 * its first instruction to execute.
1001 */
1002static void mark_free(struct pl330_thread *thrd, int idx)
1003{
1004 struct _pl330_req *req = &thrd->req[idx];
1005
1006 _emit_END(0, req->mc_cpu);
1007 req->mc_len = 0;
1008
1009 thrd->req_running = -1;
1010}
1011
1012static inline u32 _state(struct pl330_thread *thrd)
1013{
1014 void __iomem *regs = thrd->dmac->pinfo->base;
1015 u32 val;
1016
1017 if (is_manager(thrd))
1018 val = readl(regs + DS) & 0xf;
1019 else
1020 val = readl(regs + CS(thrd->id)) & 0xf;
1021
1022 switch (val) {
1023 case DS_ST_STOP:
1024 return PL330_STATE_STOPPED;
1025 case DS_ST_EXEC:
1026 return PL330_STATE_EXECUTING;
1027 case DS_ST_CMISS:
1028 return PL330_STATE_CACHEMISS;
1029 case DS_ST_UPDTPC:
1030 return PL330_STATE_UPDTPC;
1031 case DS_ST_WFE:
1032 return PL330_STATE_WFE;
1033 case DS_ST_FAULT:
1034 return PL330_STATE_FAULTING;
1035 case DS_ST_ATBRR:
1036 if (is_manager(thrd))
1037 return PL330_STATE_INVALID;
1038 else
1039 return PL330_STATE_ATBARRIER;
1040 case DS_ST_QBUSY:
1041 if (is_manager(thrd))
1042 return PL330_STATE_INVALID;
1043 else
1044 return PL330_STATE_QUEUEBUSY;
1045 case DS_ST_WFP:
1046 if (is_manager(thrd))
1047 return PL330_STATE_INVALID;
1048 else
1049 return PL330_STATE_WFP;
1050 case DS_ST_KILL:
1051 if (is_manager(thrd))
1052 return PL330_STATE_INVALID;
1053 else
1054 return PL330_STATE_KILLING;
1055 case DS_ST_CMPLT:
1056 if (is_manager(thrd))
1057 return PL330_STATE_INVALID;
1058 else
1059 return PL330_STATE_COMPLETING;
1060 case DS_ST_FLTCMP:
1061 if (is_manager(thrd))
1062 return PL330_STATE_INVALID;
1063 else
1064 return PL330_STATE_FAULT_COMPLETING;
1065 default:
1066 return PL330_STATE_INVALID;
1067 }
1068}
1069
1070static void _stop(struct pl330_thread *thrd)
1071{
1072 void __iomem *regs = thrd->dmac->pinfo->base;
1073 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1074
1075 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1076 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1077
1078 /* Return if nothing needs to be done */
1079 if (_state(thrd) == PL330_STATE_COMPLETING
1080 || _state(thrd) == PL330_STATE_KILLING
1081 || _state(thrd) == PL330_STATE_STOPPED)
1082 return;
1083
1084 _emit_KILL(0, insn);
1085
1086 /* Stop generating interrupts for SEV */
1087 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1088
1089 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1090}
1091
1092/* Start doing req 'idx' of thread 'thrd' */
1093static bool _trigger(struct pl330_thread *thrd)
1094{
1095 void __iomem *regs = thrd->dmac->pinfo->base;
1096 struct _pl330_req *req;
1097 struct pl330_req *r;
1098 struct _arg_GO go;
1099 unsigned ns;
1100 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1101 int idx;
1102
1103 /* Return if already ACTIVE */
1104 if (_state(thrd) != PL330_STATE_STOPPED)
1105 return true;
1106
1107 idx = 1 - thrd->lstenq;
1108 if (!IS_FREE(&thrd->req[idx]))
1109 req = &thrd->req[idx];
1110 else {
1111 idx = thrd->lstenq;
1112 if (!IS_FREE(&thrd->req[idx]))
1113 req = &thrd->req[idx];
1114 else
1115 req = NULL;
1116 }
1117
1118 /* Return if no request */
1119 if (!req || !req->r)
1120 return true;
1121
1122 r = req->r;
1123
1124 if (r->cfg)
1125 ns = r->cfg->nonsecure ? 1 : 0;
1126 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1127 ns = 1;
1128 else
1129 ns = 0;
1130
1131 /* See 'Abort Sources' point-4 at Page 2-25 */
1132 if (_manager_ns(thrd) && !ns)
1133 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1134 __func__, __LINE__);
1135
1136 go.chan = thrd->id;
1137 go.addr = req->mc_bus;
1138 go.ns = ns;
1139 _emit_GO(0, insn, &go);
1140
1141 /* Set to generate interrupts for SEV */
1142 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1143
1144 /* Only manager can execute GO */
1145 _execute_DBGINSN(thrd, insn, true);
1146
1147 thrd->req_running = idx;
1148
1149 return true;
1150}
1151
1152static bool _start(struct pl330_thread *thrd)
1153{
1154 switch (_state(thrd)) {
1155 case PL330_STATE_FAULT_COMPLETING:
1156 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1157
1158 if (_state(thrd) == PL330_STATE_KILLING)
1159 UNTIL(thrd, PL330_STATE_STOPPED)
1160
1161 case PL330_STATE_FAULTING:
1162 _stop(thrd);
1163
1164 case PL330_STATE_KILLING:
1165 case PL330_STATE_COMPLETING:
1166 UNTIL(thrd, PL330_STATE_STOPPED)
1167
1168 case PL330_STATE_STOPPED:
1169 return _trigger(thrd);
1170
1171 case PL330_STATE_WFP:
1172 case PL330_STATE_QUEUEBUSY:
1173 case PL330_STATE_ATBARRIER:
1174 case PL330_STATE_UPDTPC:
1175 case PL330_STATE_CACHEMISS:
1176 case PL330_STATE_EXECUTING:
1177 return true;
1178
1179 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1180 default:
1181 return false;
1182 }
1183}
1184
1185static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1186 const struct _xfer_spec *pxs, int cyc)
1187{
1188 int off = 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001189 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001190
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001191 /* check lock-up free version */
1192 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1193 while (cyc--) {
1194 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1195 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1196 }
1197 } else {
1198 while (cyc--) {
1199 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1200 off += _emit_RMB(dry_run, &buf[off]);
1201 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1202 off += _emit_WMB(dry_run, &buf[off]);
1203 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001204 }
1205
1206 return off;
1207}
1208
1209static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1210 const struct _xfer_spec *pxs, int cyc)
1211{
1212 int off = 0;
1213
1214 while (cyc--) {
1215 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1216 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1217 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1218 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1219 }
1220
1221 return off;
1222}
1223
1224static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1225 const struct _xfer_spec *pxs, int cyc)
1226{
1227 int off = 0;
1228
1229 while (cyc--) {
1230 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1231 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1232 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1233 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1234 }
1235
1236 return off;
1237}
1238
1239static int _bursts(unsigned dry_run, u8 buf[],
1240 const struct _xfer_spec *pxs, int cyc)
1241{
1242 int off = 0;
1243
1244 switch (pxs->r->rqtype) {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001245 case DMA_MEM_TO_DEV:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001246 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1247 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001248 case DMA_DEV_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001249 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1250 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001251 case DMA_MEM_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001252 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1253 break;
1254 default:
1255 off += 0x40000000; /* Scare off the Client */
1256 break;
1257 }
1258
1259 return off;
1260}
1261
1262/* Returns bytes consumed and updates bursts */
1263static inline int _loop(unsigned dry_run, u8 buf[],
1264 unsigned long *bursts, const struct _xfer_spec *pxs)
1265{
1266 int cyc, cycmax, szlp, szlpend, szbrst, off;
1267 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1268 struct _arg_LPEND lpend;
1269
1270 /* Max iterations possible in DMALP is 256 */
1271 if (*bursts >= 256*256) {
1272 lcnt1 = 256;
1273 lcnt0 = 256;
1274 cyc = *bursts / lcnt1 / lcnt0;
1275 } else if (*bursts > 256) {
1276 lcnt1 = 256;
1277 lcnt0 = *bursts / lcnt1;
1278 cyc = 1;
1279 } else {
1280 lcnt1 = *bursts;
1281 lcnt0 = 0;
1282 cyc = 1;
1283 }
1284
1285 szlp = _emit_LP(1, buf, 0, 0);
1286 szbrst = _bursts(1, buf, pxs, 1);
1287
1288 lpend.cond = ALWAYS;
1289 lpend.forever = false;
1290 lpend.loop = 0;
1291 lpend.bjump = 0;
1292 szlpend = _emit_LPEND(1, buf, &lpend);
1293
1294 if (lcnt0) {
1295 szlp *= 2;
1296 szlpend *= 2;
1297 }
1298
1299 /*
1300 * Max bursts that we can unroll due to limit on the
1301 * size of backward jump that can be encoded in DMALPEND
1302 * which is 8-bits and hence 255
1303 */
1304 cycmax = (255 - (szlp + szlpend)) / szbrst;
1305
1306 cyc = (cycmax < cyc) ? cycmax : cyc;
1307
1308 off = 0;
1309
1310 if (lcnt0) {
1311 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1312 ljmp0 = off;
1313 }
1314
1315 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1316 ljmp1 = off;
1317
1318 off += _bursts(dry_run, &buf[off], pxs, cyc);
1319
1320 lpend.cond = ALWAYS;
1321 lpend.forever = false;
1322 lpend.loop = 1;
1323 lpend.bjump = off - ljmp1;
1324 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1325
1326 if (lcnt0) {
1327 lpend.cond = ALWAYS;
1328 lpend.forever = false;
1329 lpend.loop = 0;
1330 lpend.bjump = off - ljmp0;
1331 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1332 }
1333
1334 *bursts = lcnt1 * cyc;
1335 if (lcnt0)
1336 *bursts *= lcnt0;
1337
1338 return off;
1339}
1340
1341static inline int _setup_loops(unsigned dry_run, u8 buf[],
1342 const struct _xfer_spec *pxs)
1343{
1344 struct pl330_xfer *x = pxs->x;
1345 u32 ccr = pxs->ccr;
1346 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1347 int off = 0;
1348
1349 while (bursts) {
1350 c = bursts;
1351 off += _loop(dry_run, &buf[off], &c, pxs);
1352 bursts -= c;
1353 }
1354
1355 return off;
1356}
1357
1358static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1359 const struct _xfer_spec *pxs)
1360{
1361 struct pl330_xfer *x = pxs->x;
1362 int off = 0;
1363
1364 /* DMAMOV SAR, x->src_addr */
1365 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1366 /* DMAMOV DAR, x->dst_addr */
1367 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1368
1369 /* Setup Loop(s) */
1370 off += _setup_loops(dry_run, &buf[off], pxs);
1371
1372 return off;
1373}
1374
1375/*
1376 * A req is a sequence of one or more xfer units.
1377 * Returns the number of bytes taken to setup the MC for the req.
1378 */
1379static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1380 unsigned index, struct _xfer_spec *pxs)
1381{
1382 struct _pl330_req *req = &thrd->req[index];
1383 struct pl330_xfer *x;
1384 u8 *buf = req->mc_cpu;
1385 int off = 0;
1386
1387 PL330_DBGMC_START(req->mc_bus);
1388
1389 /* DMAMOV CCR, ccr */
1390 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1391
1392 x = pxs->r->x;
1393 do {
1394 /* Error if xfer length is not aligned at burst size */
1395 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1396 return -EINVAL;
1397
1398 pxs->x = x;
1399 off += _setup_xfer(dry_run, &buf[off], pxs);
1400
1401 x = x->next;
1402 } while (x);
1403
1404 /* DMASEV peripheral/event */
1405 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1406 /* DMAEND */
1407 off += _emit_END(dry_run, &buf[off]);
1408
1409 return off;
1410}
1411
1412static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1413{
1414 u32 ccr = 0;
1415
1416 if (rqc->src_inc)
1417 ccr |= CC_SRCINC;
1418
1419 if (rqc->dst_inc)
1420 ccr |= CC_DSTINC;
1421
1422 /* We set same protection levels for Src and DST for now */
1423 if (rqc->privileged)
1424 ccr |= CC_SRCPRI | CC_DSTPRI;
1425 if (rqc->nonsecure)
1426 ccr |= CC_SRCNS | CC_DSTNS;
1427 if (rqc->insnaccess)
1428 ccr |= CC_SRCIA | CC_DSTIA;
1429
1430 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1431 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1432
1433 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1434 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1435
1436 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1437 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1438
1439 ccr |= (rqc->swap << CC_SWAP_SHFT);
1440
1441 return ccr;
1442}
1443
1444static inline bool _is_valid(u32 ccr)
1445{
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02001446 enum pl330_cachectrl dcctl;
1447 enum pl330_cachectrl scctl;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001448
1449 dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1450 scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1451
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02001452 if (dcctl == INVALID1 || dcctl == INVALID2
1453 || scctl == INVALID1 || scctl == INVALID2)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001454 return false;
1455 else
1456 return true;
1457}
1458
1459/*
1460 * Submit a list of xfers after which the client wants notification.
1461 * Client is not notified after each xfer unit, just once after all
1462 * xfer units are done or some error occurs.
1463 */
1464static int pl330_submit_req(void *ch_id, struct pl330_req *r)
1465{
1466 struct pl330_thread *thrd = ch_id;
1467 struct pl330_dmac *pl330;
1468 struct pl330_info *pi;
1469 struct _xfer_spec xs;
1470 unsigned long flags;
1471 void __iomem *regs;
1472 unsigned idx;
1473 u32 ccr;
1474 int ret = 0;
1475
1476 /* No Req or Unacquired Channel or DMAC */
1477 if (!r || !thrd || thrd->free)
1478 return -EINVAL;
1479
1480 pl330 = thrd->dmac;
1481 pi = pl330->pinfo;
1482 regs = pi->base;
1483
1484 if (pl330->state == DYING
1485 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1486 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1487 __func__, __LINE__);
1488 return -EAGAIN;
1489 }
1490
1491 /* If request for non-existing peripheral */
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001492 if (r->rqtype != DMA_MEM_TO_MEM && r->peri >= pi->pcfg.num_peri) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001493 dev_info(thrd->dmac->pinfo->dev,
1494 "%s:%d Invalid peripheral(%u)!\n",
1495 __func__, __LINE__, r->peri);
1496 return -EINVAL;
1497 }
1498
1499 spin_lock_irqsave(&pl330->lock, flags);
1500
1501 if (_queue_full(thrd)) {
1502 ret = -EAGAIN;
1503 goto xfer_exit;
1504 }
1505
Boojin Kimb7d861d2011-12-26 18:49:52 +09001506
1507 /* Use last settings, if not provided */
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301508 if (r->cfg) {
1509 /* Prefer Secure Channel */
1510 if (!_manager_ns(thrd))
1511 r->cfg->nonsecure = 0;
1512 else
1513 r->cfg->nonsecure = 1;
1514
Boojin Kimb7d861d2011-12-26 18:49:52 +09001515 ccr = _prepare_ccr(r->cfg);
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301516 } else {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001517 ccr = readl(regs + CC(thrd->id));
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301518 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001519
1520 /* If this req doesn't have valid xfer settings */
1521 if (!_is_valid(ccr)) {
1522 ret = -EINVAL;
1523 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1524 __func__, __LINE__, ccr);
1525 goto xfer_exit;
1526 }
1527
1528 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1529
1530 xs.ccr = ccr;
1531 xs.r = r;
1532
1533 /* First dry run to check if req is acceptable */
1534 ret = _setup_req(1, thrd, idx, &xs);
1535 if (ret < 0)
1536 goto xfer_exit;
1537
1538 if (ret > pi->mcbufsz / 2) {
1539 dev_info(thrd->dmac->pinfo->dev,
1540 "%s:%d Trying increasing mcbufsz\n",
1541 __func__, __LINE__);
1542 ret = -ENOMEM;
1543 goto xfer_exit;
1544 }
1545
1546 /* Hook the request */
1547 thrd->lstenq = idx;
1548 thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs);
1549 thrd->req[idx].r = r;
1550
1551 ret = 0;
1552
1553xfer_exit:
1554 spin_unlock_irqrestore(&pl330->lock, flags);
1555
1556 return ret;
1557}
1558
1559static void pl330_dotask(unsigned long data)
1560{
1561 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1562 struct pl330_info *pi = pl330->pinfo;
1563 unsigned long flags;
1564 int i;
1565
1566 spin_lock_irqsave(&pl330->lock, flags);
1567
1568 /* The DMAC itself gone nuts */
1569 if (pl330->dmac_tbd.reset_dmac) {
1570 pl330->state = DYING;
1571 /* Reset the manager too */
1572 pl330->dmac_tbd.reset_mngr = true;
1573 /* Clear the reset flag */
1574 pl330->dmac_tbd.reset_dmac = false;
1575 }
1576
1577 if (pl330->dmac_tbd.reset_mngr) {
1578 _stop(pl330->manager);
1579 /* Reset all channels */
1580 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1581 /* Clear the reset flag */
1582 pl330->dmac_tbd.reset_mngr = false;
1583 }
1584
1585 for (i = 0; i < pi->pcfg.num_chan; i++) {
1586
1587 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1588 struct pl330_thread *thrd = &pl330->channels[i];
1589 void __iomem *regs = pi->base;
1590 enum pl330_op_err err;
1591
1592 _stop(thrd);
1593
1594 if (readl(regs + FSC) & (1 << thrd->id))
1595 err = PL330_ERR_FAIL;
1596 else
1597 err = PL330_ERR_ABORT;
1598
1599 spin_unlock_irqrestore(&pl330->lock, flags);
1600
1601 _callback(thrd->req[1 - thrd->lstenq].r, err);
1602 _callback(thrd->req[thrd->lstenq].r, err);
1603
1604 spin_lock_irqsave(&pl330->lock, flags);
1605
1606 thrd->req[0].r = NULL;
1607 thrd->req[1].r = NULL;
1608 mark_free(thrd, 0);
1609 mark_free(thrd, 1);
1610
1611 /* Clear the reset flag */
1612 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1613 }
1614 }
1615
1616 spin_unlock_irqrestore(&pl330->lock, flags);
1617
1618 return;
1619}
1620
1621/* Returns 1 if state was updated, 0 otherwise */
1622static int pl330_update(const struct pl330_info *pi)
1623{
Javi Merinofdec53d2012-06-13 15:07:00 +01001624 struct pl330_req *rqdone, *tmp;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001625 struct pl330_dmac *pl330;
1626 unsigned long flags;
1627 void __iomem *regs;
1628 u32 val;
1629 int id, ev, ret = 0;
1630
1631 if (!pi || !pi->pl330_data)
1632 return 0;
1633
1634 regs = pi->base;
1635 pl330 = pi->pl330_data;
1636
1637 spin_lock_irqsave(&pl330->lock, flags);
1638
1639 val = readl(regs + FSM) & 0x1;
1640 if (val)
1641 pl330->dmac_tbd.reset_mngr = true;
1642 else
1643 pl330->dmac_tbd.reset_mngr = false;
1644
1645 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1646 pl330->dmac_tbd.reset_chan |= val;
1647 if (val) {
1648 int i = 0;
1649 while (i < pi->pcfg.num_chan) {
1650 if (val & (1 << i)) {
1651 dev_info(pi->dev,
1652 "Reset Channel-%d\t CS-%x FTC-%x\n",
1653 i, readl(regs + CS(i)),
1654 readl(regs + FTC(i)));
1655 _stop(&pl330->channels[i]);
1656 }
1657 i++;
1658 }
1659 }
1660
1661 /* Check which event happened i.e, thread notified */
1662 val = readl(regs + ES);
1663 if (pi->pcfg.num_events < 32
1664 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1665 pl330->dmac_tbd.reset_dmac = true;
1666 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1667 ret = 1;
1668 goto updt_exit;
1669 }
1670
1671 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1672 if (val & (1 << ev)) { /* Event occurred */
1673 struct pl330_thread *thrd;
1674 u32 inten = readl(regs + INTEN);
1675 int active;
1676
1677 /* Clear the event */
1678 if (inten & (1 << ev))
1679 writel(1 << ev, regs + INTCLR);
1680
1681 ret = 1;
1682
1683 id = pl330->events[ev];
1684
1685 thrd = &pl330->channels[id];
1686
1687 active = thrd->req_running;
1688 if (active == -1) /* Aborted */
1689 continue;
1690
Javi Merinofdec53d2012-06-13 15:07:00 +01001691 /* Detach the req */
1692 rqdone = thrd->req[active].r;
1693 thrd->req[active].r = NULL;
1694
Boojin Kimb7d861d2011-12-26 18:49:52 +09001695 mark_free(thrd, active);
1696
1697 /* Get going again ASAP */
1698 _start(thrd);
1699
1700 /* For now, just make a list of callbacks to be done */
1701 list_add_tail(&rqdone->rqd, &pl330->req_done);
1702 }
1703 }
1704
1705 /* Now that we are in no hurry, do the callbacks */
Javi Merinofdec53d2012-06-13 15:07:00 +01001706 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1707 list_del(&rqdone->rqd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001708
1709 spin_unlock_irqrestore(&pl330->lock, flags);
Javi Merinofdec53d2012-06-13 15:07:00 +01001710 _callback(rqdone, PL330_ERR_NONE);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001711 spin_lock_irqsave(&pl330->lock, flags);
1712 }
1713
1714updt_exit:
1715 spin_unlock_irqrestore(&pl330->lock, flags);
1716
1717 if (pl330->dmac_tbd.reset_dmac
1718 || pl330->dmac_tbd.reset_mngr
1719 || pl330->dmac_tbd.reset_chan) {
1720 ret = 1;
1721 tasklet_schedule(&pl330->tasks);
1722 }
1723
1724 return ret;
1725}
1726
1727static int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op)
1728{
1729 struct pl330_thread *thrd = ch_id;
1730 struct pl330_dmac *pl330;
1731 unsigned long flags;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001732 int ret = 0, active;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001733
1734 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1735 return -EINVAL;
1736
1737 pl330 = thrd->dmac;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001738 active = thrd->req_running;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001739
1740 spin_lock_irqsave(&pl330->lock, flags);
1741
1742 switch (op) {
1743 case PL330_OP_FLUSH:
1744 /* Make sure the channel is stopped */
1745 _stop(thrd);
1746
1747 thrd->req[0].r = NULL;
1748 thrd->req[1].r = NULL;
1749 mark_free(thrd, 0);
1750 mark_free(thrd, 1);
1751 break;
1752
1753 case PL330_OP_ABORT:
1754 /* Make sure the channel is stopped */
1755 _stop(thrd);
1756
1757 /* ABORT is only for the active req */
1758 if (active == -1)
1759 break;
1760
1761 thrd->req[active].r = NULL;
1762 mark_free(thrd, active);
1763
1764 /* Start the next */
1765 case PL330_OP_START:
1766 if ((active == -1) && !_start(thrd))
1767 ret = -EIO;
1768 break;
1769
1770 default:
1771 ret = -EINVAL;
1772 }
1773
1774 spin_unlock_irqrestore(&pl330->lock, flags);
1775 return ret;
1776}
1777
Boojin Kimb7d861d2011-12-26 18:49:52 +09001778/* Reserve an event */
1779static inline int _alloc_event(struct pl330_thread *thrd)
1780{
1781 struct pl330_dmac *pl330 = thrd->dmac;
1782 struct pl330_info *pi = pl330->pinfo;
1783 int ev;
1784
1785 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1786 if (pl330->events[ev] == -1) {
1787 pl330->events[ev] = thrd->id;
1788 return ev;
1789 }
1790
1791 return -1;
1792}
1793
1794static bool _chan_ns(const struct pl330_info *pi, int i)
1795{
1796 return pi->pcfg.irq_ns & (1 << i);
1797}
1798
1799/* Upon success, returns IdentityToken for the
1800 * allocated channel, NULL otherwise.
1801 */
1802static void *pl330_request_channel(const struct pl330_info *pi)
1803{
1804 struct pl330_thread *thrd = NULL;
1805 struct pl330_dmac *pl330;
1806 unsigned long flags;
1807 int chans, i;
1808
1809 if (!pi || !pi->pl330_data)
1810 return NULL;
1811
1812 pl330 = pi->pl330_data;
1813
1814 if (pl330->state == DYING)
1815 return NULL;
1816
1817 chans = pi->pcfg.num_chan;
1818
1819 spin_lock_irqsave(&pl330->lock, flags);
1820
1821 for (i = 0; i < chans; i++) {
1822 thrd = &pl330->channels[i];
1823 if ((thrd->free) && (!_manager_ns(thrd) ||
1824 _chan_ns(pi, i))) {
1825 thrd->ev = _alloc_event(thrd);
1826 if (thrd->ev >= 0) {
1827 thrd->free = false;
1828 thrd->lstenq = 1;
1829 thrd->req[0].r = NULL;
1830 mark_free(thrd, 0);
1831 thrd->req[1].r = NULL;
1832 mark_free(thrd, 1);
1833 break;
1834 }
1835 }
1836 thrd = NULL;
1837 }
1838
1839 spin_unlock_irqrestore(&pl330->lock, flags);
1840
1841 return thrd;
1842}
1843
1844/* Release an event */
1845static inline void _free_event(struct pl330_thread *thrd, int ev)
1846{
1847 struct pl330_dmac *pl330 = thrd->dmac;
1848 struct pl330_info *pi = pl330->pinfo;
1849
1850 /* If the event is valid and was held by the thread */
1851 if (ev >= 0 && ev < pi->pcfg.num_events
1852 && pl330->events[ev] == thrd->id)
1853 pl330->events[ev] = -1;
1854}
1855
1856static void pl330_release_channel(void *ch_id)
1857{
1858 struct pl330_thread *thrd = ch_id;
1859 struct pl330_dmac *pl330;
1860 unsigned long flags;
1861
1862 if (!thrd || thrd->free)
1863 return;
1864
1865 _stop(thrd);
1866
1867 _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1868 _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1869
1870 pl330 = thrd->dmac;
1871
1872 spin_lock_irqsave(&pl330->lock, flags);
1873 _free_event(thrd, thrd->ev);
1874 thrd->free = true;
1875 spin_unlock_irqrestore(&pl330->lock, flags);
1876}
1877
1878/* Initialize the structure for PL330 configuration, that can be used
1879 * by the client driver the make best use of the DMAC
1880 */
1881static void read_dmac_config(struct pl330_info *pi)
1882{
1883 void __iomem *regs = pi->base;
1884 u32 val;
1885
1886 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1887 val &= CRD_DATA_WIDTH_MASK;
1888 pi->pcfg.data_bus_width = 8 * (1 << val);
1889
1890 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1891 val &= CRD_DATA_BUFF_MASK;
1892 pi->pcfg.data_buf_dep = val + 1;
1893
1894 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1895 val &= CR0_NUM_CHANS_MASK;
1896 val += 1;
1897 pi->pcfg.num_chan = val;
1898
1899 val = readl(regs + CR0);
1900 if (val & CR0_PERIPH_REQ_SET) {
1901 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1902 val += 1;
1903 pi->pcfg.num_peri = val;
1904 pi->pcfg.peri_ns = readl(regs + CR4);
1905 } else {
1906 pi->pcfg.num_peri = 0;
1907 }
1908
1909 val = readl(regs + CR0);
1910 if (val & CR0_BOOT_MAN_NS)
1911 pi->pcfg.mode |= DMAC_MODE_NS;
1912 else
1913 pi->pcfg.mode &= ~DMAC_MODE_NS;
1914
1915 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1916 val &= CR0_NUM_EVENTS_MASK;
1917 val += 1;
1918 pi->pcfg.num_events = val;
1919
1920 pi->pcfg.irq_ns = readl(regs + CR3);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001921}
1922
1923static inline void _reset_thread(struct pl330_thread *thrd)
1924{
1925 struct pl330_dmac *pl330 = thrd->dmac;
1926 struct pl330_info *pi = pl330->pinfo;
1927
1928 thrd->req[0].mc_cpu = pl330->mcode_cpu
1929 + (thrd->id * pi->mcbufsz);
1930 thrd->req[0].mc_bus = pl330->mcode_bus
1931 + (thrd->id * pi->mcbufsz);
1932 thrd->req[0].r = NULL;
1933 mark_free(thrd, 0);
1934
1935 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1936 + pi->mcbufsz / 2;
1937 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1938 + pi->mcbufsz / 2;
1939 thrd->req[1].r = NULL;
1940 mark_free(thrd, 1);
1941}
1942
1943static int dmac_alloc_threads(struct pl330_dmac *pl330)
1944{
1945 struct pl330_info *pi = pl330->pinfo;
1946 int chans = pi->pcfg.num_chan;
1947 struct pl330_thread *thrd;
1948 int i;
1949
1950 /* Allocate 1 Manager and 'chans' Channel threads */
1951 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
1952 GFP_KERNEL);
1953 if (!pl330->channels)
1954 return -ENOMEM;
1955
1956 /* Init Channel threads */
1957 for (i = 0; i < chans; i++) {
1958 thrd = &pl330->channels[i];
1959 thrd->id = i;
1960 thrd->dmac = pl330;
1961 _reset_thread(thrd);
1962 thrd->free = true;
1963 }
1964
1965 /* MANAGER is indexed at the end */
1966 thrd = &pl330->channels[chans];
1967 thrd->id = chans;
1968 thrd->dmac = pl330;
1969 thrd->free = false;
1970 pl330->manager = thrd;
1971
1972 return 0;
1973}
1974
1975static int dmac_alloc_resources(struct pl330_dmac *pl330)
1976{
1977 struct pl330_info *pi = pl330->pinfo;
1978 int chans = pi->pcfg.num_chan;
1979 int ret;
1980
1981 /*
1982 * Alloc MicroCode buffer for 'chans' Channel threads.
1983 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1984 */
1985 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
1986 chans * pi->mcbufsz,
1987 &pl330->mcode_bus, GFP_KERNEL);
1988 if (!pl330->mcode_cpu) {
1989 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
1990 __func__, __LINE__);
1991 return -ENOMEM;
1992 }
1993
1994 ret = dmac_alloc_threads(pl330);
1995 if (ret) {
1996 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
1997 __func__, __LINE__);
1998 dma_free_coherent(pi->dev,
1999 chans * pi->mcbufsz,
2000 pl330->mcode_cpu, pl330->mcode_bus);
2001 return ret;
2002 }
2003
2004 return 0;
2005}
2006
2007static int pl330_add(struct pl330_info *pi)
2008{
2009 struct pl330_dmac *pl330;
2010 void __iomem *regs;
2011 int i, ret;
2012
2013 if (!pi || !pi->dev)
2014 return -EINVAL;
2015
2016 /* If already added */
2017 if (pi->pl330_data)
2018 return -EINVAL;
2019
Boojin Kimb7d861d2011-12-26 18:49:52 +09002020 regs = pi->base;
2021
2022 /* Check if we can handle this DMAC */
Will Deacon09677172013-06-10 19:34:37 +01002023 if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
2024 dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002025 return -EINVAL;
2026 }
2027
2028 /* Read the configuration of the DMAC */
2029 read_dmac_config(pi);
2030
2031 if (pi->pcfg.num_events == 0) {
2032 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2033 __func__, __LINE__);
2034 return -EINVAL;
2035 }
2036
2037 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2038 if (!pl330) {
2039 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2040 __func__, __LINE__);
2041 return -ENOMEM;
2042 }
2043
2044 /* Assign the info structure and private data */
2045 pl330->pinfo = pi;
2046 pi->pl330_data = pl330;
2047
2048 spin_lock_init(&pl330->lock);
2049
2050 INIT_LIST_HEAD(&pl330->req_done);
2051
2052 /* Use default MC buffer size if not provided */
2053 if (!pi->mcbufsz)
2054 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2055
2056 /* Mark all events as free */
2057 for (i = 0; i < pi->pcfg.num_events; i++)
2058 pl330->events[i] = -1;
2059
2060 /* Allocate resources needed by the DMAC */
2061 ret = dmac_alloc_resources(pl330);
2062 if (ret) {
2063 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2064 kfree(pl330);
2065 return ret;
2066 }
2067
2068 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2069
2070 pl330->state = INIT;
2071
2072 return 0;
2073}
2074
2075static int dmac_free_threads(struct pl330_dmac *pl330)
2076{
2077 struct pl330_info *pi = pl330->pinfo;
2078 int chans = pi->pcfg.num_chan;
2079 struct pl330_thread *thrd;
2080 int i;
2081
2082 /* Release Channel threads */
2083 for (i = 0; i < chans; i++) {
2084 thrd = &pl330->channels[i];
2085 pl330_release_channel((void *)thrd);
2086 }
2087
2088 /* Free memory */
2089 kfree(pl330->channels);
2090
2091 return 0;
2092}
2093
2094static void dmac_free_resources(struct pl330_dmac *pl330)
2095{
2096 struct pl330_info *pi = pl330->pinfo;
2097 int chans = pi->pcfg.num_chan;
2098
2099 dmac_free_threads(pl330);
2100
2101 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2102 pl330->mcode_cpu, pl330->mcode_bus);
2103}
2104
2105static void pl330_del(struct pl330_info *pi)
2106{
2107 struct pl330_dmac *pl330;
2108
2109 if (!pi || !pi->pl330_data)
2110 return;
2111
2112 pl330 = pi->pl330_data;
2113
2114 pl330->state = UNINIT;
2115
2116 tasklet_kill(&pl330->tasks);
2117
2118 /* Free DMAC resources */
2119 dmac_free_resources(pl330);
2120
2121 kfree(pl330);
2122 pi->pl330_data = NULL;
2123}
2124
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002125/* forward declaration */
2126static struct amba_driver pl330_driver;
2127
Jassi Brarb3040e42010-05-23 20:28:19 -07002128static inline struct dma_pl330_chan *
2129to_pchan(struct dma_chan *ch)
2130{
2131 if (!ch)
2132 return NULL;
2133
2134 return container_of(ch, struct dma_pl330_chan, chan);
2135}
2136
2137static inline struct dma_pl330_desc *
2138to_desc(struct dma_async_tx_descriptor *tx)
2139{
2140 return container_of(tx, struct dma_pl330_desc, txd);
2141}
2142
Jassi Brarb3040e42010-05-23 20:28:19 -07002143static inline void fill_queue(struct dma_pl330_chan *pch)
2144{
2145 struct dma_pl330_desc *desc;
2146 int ret;
2147
2148 list_for_each_entry(desc, &pch->work_list, node) {
2149
2150 /* If already submitted */
2151 if (desc->status == BUSY)
Jassi Brar30fb9802013-02-13 16:13:14 +05302152 continue;
Jassi Brarb3040e42010-05-23 20:28:19 -07002153
2154 ret = pl330_submit_req(pch->pl330_chid,
2155 &desc->req);
2156 if (!ret) {
2157 desc->status = BUSY;
Jassi Brarb3040e42010-05-23 20:28:19 -07002158 } else if (ret == -EAGAIN) {
2159 /* QFull or DMAC Dying */
2160 break;
2161 } else {
2162 /* Unacceptable request */
2163 desc->status = DONE;
2164 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2165 __func__, __LINE__, desc->txd.cookie);
2166 tasklet_schedule(&pch->task);
2167 }
2168 }
2169}
2170
2171static void pl330_tasklet(unsigned long data)
2172{
2173 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2174 struct dma_pl330_desc *desc, *_dt;
2175 unsigned long flags;
Jassi Brarb3040e42010-05-23 20:28:19 -07002176
2177 spin_lock_irqsave(&pch->lock, flags);
2178
2179 /* Pick up ripe tomatoes */
2180 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2181 if (desc->status == DONE) {
Tushar Behera30c1dc02012-05-23 16:47:31 +05302182 if (!pch->cyclic)
Vinod Kouleab21582012-05-11 11:24:41 +05302183 dma_cookie_complete(&desc->txd);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002184 list_move_tail(&desc->node, &pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002185 }
2186
2187 /* Try to submit a req imm. next to the last completed cookie */
2188 fill_queue(pch);
2189
2190 /* Make sure the PL330 Channel thread is active */
2191 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
2192
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002193 while (!list_empty(&pch->completed_list)) {
2194 dma_async_tx_callback callback;
2195 void *callback_param;
Jassi Brarb3040e42010-05-23 20:28:19 -07002196
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002197 desc = list_first_entry(&pch->completed_list,
2198 struct dma_pl330_desc, node);
2199
2200 callback = desc->txd.callback;
2201 callback_param = desc->txd.callback_param;
2202
2203 if (pch->cyclic) {
2204 desc->status = PREP;
2205 list_move_tail(&desc->node, &pch->work_list);
2206 } else {
2207 desc->status = FREE;
2208 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2209 }
2210
Dan Williamsd38a8c62013-10-18 19:35:23 +02002211 dma_descriptor_unmap(&desc->txd);
2212
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002213 if (callback) {
2214 spin_unlock_irqrestore(&pch->lock, flags);
2215 callback(callback_param);
2216 spin_lock_irqsave(&pch->lock, flags);
2217 }
2218 }
2219 spin_unlock_irqrestore(&pch->lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002220}
2221
2222static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
2223{
2224 struct dma_pl330_desc *desc = token;
2225 struct dma_pl330_chan *pch = desc->pchan;
2226 unsigned long flags;
2227
2228 /* If desc aborted */
2229 if (!pch)
2230 return;
2231
2232 spin_lock_irqsave(&pch->lock, flags);
2233
2234 desc->status = DONE;
2235
2236 spin_unlock_irqrestore(&pch->lock, flags);
2237
2238 tasklet_schedule(&pch->task);
2239}
2240
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002241bool pl330_filter(struct dma_chan *chan, void *param)
2242{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002243 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002244
2245 if (chan->device->dev->driver != &pl330_driver.drv)
2246 return false;
2247
Thomas Abrahamcd072512011-10-24 11:43:11 +02002248 peri_id = chan->private;
Dan Carpenter2f986ec2013-11-08 12:51:16 +03002249 return *peri_id == (unsigned long)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002250}
2251EXPORT_SYMBOL(pl330_filter);
2252
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302253static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2254 struct of_dma *ofdma)
2255{
2256 int count = dma_spec->args_count;
2257 struct dma_pl330_dmac *pdmac = ofdma->of_dma_data;
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002258 unsigned int chan_id;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302259
2260 if (count != 1)
2261 return NULL;
2262
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002263 chan_id = dma_spec->args[0];
2264 if (chan_id >= pdmac->num_peripherals)
2265 return NULL;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302266
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002267 return dma_get_slave_channel(&pdmac->peripherals[chan_id].chan);
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302268}
2269
Jassi Brarb3040e42010-05-23 20:28:19 -07002270static int pl330_alloc_chan_resources(struct dma_chan *chan)
2271{
2272 struct dma_pl330_chan *pch = to_pchan(chan);
2273 struct dma_pl330_dmac *pdmac = pch->dmac;
2274 unsigned long flags;
2275
2276 spin_lock_irqsave(&pch->lock, flags);
2277
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002278 dma_cookie_init(chan);
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002279 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002280
2281 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
2282 if (!pch->pl330_chid) {
2283 spin_unlock_irqrestore(&pch->lock, flags);
Inderpal Singh02747882012-09-17 09:57:45 +05302284 return -ENOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002285 }
2286
2287 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2288
2289 spin_unlock_irqrestore(&pch->lock, flags);
2290
2291 return 1;
2292}
2293
2294static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2295{
2296 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002297 struct dma_pl330_desc *desc;
Jassi Brarb3040e42010-05-23 20:28:19 -07002298 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002299 struct dma_pl330_dmac *pdmac = pch->dmac;
2300 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002301 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002302
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002303 switch (cmd) {
2304 case DMA_TERMINATE_ALL:
2305 spin_lock_irqsave(&pch->lock, flags);
2306
2307 /* FLUSH the PL330 Channel thread */
2308 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
2309
2310 /* Mark all desc done */
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002311 list_for_each_entry(desc, &pch->submitted_list, node) {
2312 desc->status = FREE;
2313 dma_cookie_complete(&desc->txd);
2314 }
2315
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002316 list_for_each_entry(desc, &pch->work_list , node) {
2317 desc->status = FREE;
2318 dma_cookie_complete(&desc->txd);
Boojin Kimae43b882011-09-02 09:44:32 +09002319 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002320
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002321 list_for_each_entry(desc, &pch->completed_list , node) {
2322 desc->status = FREE;
2323 dma_cookie_complete(&desc->txd);
2324 }
2325
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002326 list_splice_tail_init(&pch->submitted_list, &pdmac->desc_pool);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002327 list_splice_tail_init(&pch->work_list, &pdmac->desc_pool);
2328 list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002329 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002330 break;
2331 case DMA_SLAVE_CONFIG:
2332 slave_config = (struct dma_slave_config *)arg;
2333
Vinod Kouldb8196d2011-10-13 22:34:23 +05302334 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002335 if (slave_config->dst_addr)
2336 pch->fifo_addr = slave_config->dst_addr;
2337 if (slave_config->dst_addr_width)
2338 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2339 if (slave_config->dst_maxburst)
2340 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302341 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002342 if (slave_config->src_addr)
2343 pch->fifo_addr = slave_config->src_addr;
2344 if (slave_config->src_addr_width)
2345 pch->burst_sz = __ffs(slave_config->src_addr_width);
2346 if (slave_config->src_maxburst)
2347 pch->burst_len = slave_config->src_maxburst;
2348 }
2349 break;
2350 default:
2351 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002352 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002353 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002354
2355 return 0;
2356}
2357
2358static void pl330_free_chan_resources(struct dma_chan *chan)
2359{
2360 struct dma_pl330_chan *pch = to_pchan(chan);
2361 unsigned long flags;
2362
Jassi Brarb3040e42010-05-23 20:28:19 -07002363 tasklet_kill(&pch->task);
2364
Bartlomiej Zolnierkiewiczda331ba2013-07-03 15:00:43 -07002365 spin_lock_irqsave(&pch->lock, flags);
2366
Jassi Brarb3040e42010-05-23 20:28:19 -07002367 pl330_release_channel(pch->pl330_chid);
2368 pch->pl330_chid = NULL;
2369
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002370 if (pch->cyclic)
2371 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2372
Jassi Brarb3040e42010-05-23 20:28:19 -07002373 spin_unlock_irqrestore(&pch->lock, flags);
2374}
2375
2376static enum dma_status
2377pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2378 struct dma_tx_state *txstate)
2379{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002380 return dma_cookie_status(chan, cookie, txstate);
Jassi Brarb3040e42010-05-23 20:28:19 -07002381}
2382
2383static void pl330_issue_pending(struct dma_chan *chan)
2384{
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002385 struct dma_pl330_chan *pch = to_pchan(chan);
2386 unsigned long flags;
2387
2388 spin_lock_irqsave(&pch->lock, flags);
2389 list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2390 spin_unlock_irqrestore(&pch->lock, flags);
2391
2392 pl330_tasklet((unsigned long)pch);
Jassi Brarb3040e42010-05-23 20:28:19 -07002393}
2394
2395/*
2396 * We returned the last one of the circular list of descriptor(s)
2397 * from prep_xxx, so the argument to submit corresponds to the last
2398 * descriptor of the list.
2399 */
2400static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2401{
2402 struct dma_pl330_desc *desc, *last = to_desc(tx);
2403 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2404 dma_cookie_t cookie;
2405 unsigned long flags;
2406
2407 spin_lock_irqsave(&pch->lock, flags);
2408
2409 /* Assign cookies to all nodes */
Jassi Brarb3040e42010-05-23 20:28:19 -07002410 while (!list_empty(&last->node)) {
2411 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002412 if (pch->cyclic) {
2413 desc->txd.callback = last->txd.callback;
2414 desc->txd.callback_param = last->txd.callback_param;
2415 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002416
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002417 dma_cookie_assign(&desc->txd);
Jassi Brarb3040e42010-05-23 20:28:19 -07002418
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002419 list_move_tail(&desc->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002420 }
2421
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002422 cookie = dma_cookie_assign(&last->txd);
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002423 list_add_tail(&last->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002424 spin_unlock_irqrestore(&pch->lock, flags);
2425
2426 return cookie;
2427}
2428
2429static inline void _init_desc(struct dma_pl330_desc *desc)
2430{
Jassi Brarb3040e42010-05-23 20:28:19 -07002431 desc->req.x = &desc->px;
2432 desc->req.token = desc;
2433 desc->rqcfg.swap = SWAP_NO;
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02002434 desc->rqcfg.scctl = CCTRL0;
2435 desc->rqcfg.dcctl = CCTRL0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002436 desc->req.cfg = &desc->rqcfg;
2437 desc->req.xfer_cb = dma_pl330_rqcb;
2438 desc->txd.tx_submit = pl330_tx_submit;
2439
2440 INIT_LIST_HEAD(&desc->node);
2441}
2442
2443/* Returns the number of descriptors added to the DMAC pool */
Sachin Kamat5a67ac52012-06-04 17:09:45 +05302444static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
Jassi Brarb3040e42010-05-23 20:28:19 -07002445{
2446 struct dma_pl330_desc *desc;
2447 unsigned long flags;
2448 int i;
2449
2450 if (!pdmac)
2451 return 0;
2452
Will Deacon0baf8f62013-12-02 18:01:30 +00002453 desc = kcalloc(count, sizeof(*desc), flg);
Jassi Brarb3040e42010-05-23 20:28:19 -07002454 if (!desc)
2455 return 0;
2456
2457 spin_lock_irqsave(&pdmac->pool_lock, flags);
2458
2459 for (i = 0; i < count; i++) {
2460 _init_desc(&desc[i]);
2461 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2462 }
2463
2464 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2465
2466 return count;
2467}
2468
2469static struct dma_pl330_desc *
2470pluck_desc(struct dma_pl330_dmac *pdmac)
2471{
2472 struct dma_pl330_desc *desc = NULL;
2473 unsigned long flags;
2474
2475 if (!pdmac)
2476 return NULL;
2477
2478 spin_lock_irqsave(&pdmac->pool_lock, flags);
2479
2480 if (!list_empty(&pdmac->desc_pool)) {
2481 desc = list_entry(pdmac->desc_pool.next,
2482 struct dma_pl330_desc, node);
2483
2484 list_del_init(&desc->node);
2485
2486 desc->status = PREP;
2487 desc->txd.callback = NULL;
2488 }
2489
2490 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2491
2492 return desc;
2493}
2494
2495static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2496{
2497 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002498 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002499 struct dma_pl330_desc *desc;
2500
2501 /* Pluck one desc from the pool of DMAC */
2502 desc = pluck_desc(pdmac);
2503
2504 /* If the DMAC pool is empty, alloc new */
2505 if (!desc) {
2506 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2507 return NULL;
2508
2509 /* Try again */
2510 desc = pluck_desc(pdmac);
2511 if (!desc) {
2512 dev_err(pch->dmac->pif.dev,
2513 "%s:%d ALERT!\n", __func__, __LINE__);
2514 return NULL;
2515 }
2516 }
2517
2518 /* Initialize the descriptor */
2519 desc->pchan = pch;
2520 desc->txd.cookie = 0;
2521 async_tx_ack(&desc->txd);
2522
Thomas Abrahamcd072512011-10-24 11:43:11 +02002523 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09002524 desc->rqcfg.pcfg = &pch->dmac->pif.pcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002525
2526 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2527
2528 return desc;
2529}
2530
2531static inline void fill_px(struct pl330_xfer *px,
2532 dma_addr_t dst, dma_addr_t src, size_t len)
2533{
2534 px->next = NULL;
2535 px->bytes = len;
2536 px->dst_addr = dst;
2537 px->src_addr = src;
2538}
2539
2540static struct dma_pl330_desc *
2541__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2542 dma_addr_t src, size_t len)
2543{
2544 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2545
2546 if (!desc) {
2547 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2548 __func__, __LINE__);
2549 return NULL;
2550 }
2551
2552 /*
2553 * Ideally we should lookout for reqs bigger than
2554 * those that can be programmed with 256 bytes of
2555 * MC buffer, but considering a req size is seldom
2556 * going to be word-unaligned and more than 200MB,
2557 * we take it easy.
2558 * Also, should the limit is reached we'd rather
2559 * have the platform increase MC buffer size than
2560 * complicating this API driver.
2561 */
2562 fill_px(&desc->px, dst, src, len);
2563
2564 return desc;
2565}
2566
2567/* Call after fixing burst size */
2568static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2569{
2570 struct dma_pl330_chan *pch = desc->pchan;
2571 struct pl330_info *pi = &pch->dmac->pif;
2572 int burst_len;
2573
2574 burst_len = pi->pcfg.data_bus_width / 8;
2575 burst_len *= pi->pcfg.data_buf_dep;
2576 burst_len >>= desc->rqcfg.brst_size;
2577
2578 /* src/dst_burst_len can't be more than 16 */
2579 if (burst_len > 16)
2580 burst_len = 16;
2581
2582 while (burst_len > 1) {
2583 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2584 break;
2585 burst_len--;
2586 }
2587
2588 return burst_len;
2589}
2590
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002591static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2592 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002593 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002594 unsigned long flags, void *context)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002595{
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002596 struct dma_pl330_desc *desc = NULL, *first = NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002597 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002598 struct dma_pl330_dmac *pdmac = pch->dmac;
2599 unsigned int i;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002600 dma_addr_t dst;
2601 dma_addr_t src;
2602
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002603 if (len % period_len != 0)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002604 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002605
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002606 if (!is_slave_direction(direction)) {
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002607 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2608 __func__, __LINE__);
2609 return NULL;
2610 }
2611
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002612 for (i = 0; i < len / period_len; i++) {
2613 desc = pl330_get_desc(pch);
2614 if (!desc) {
2615 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2616 __func__, __LINE__);
2617
2618 if (!first)
2619 return NULL;
2620
2621 spin_lock_irqsave(&pdmac->pool_lock, flags);
2622
2623 while (!list_empty(&first->node)) {
2624 desc = list_entry(first->node.next,
2625 struct dma_pl330_desc, node);
2626 list_move_tail(&desc->node, &pdmac->desc_pool);
2627 }
2628
2629 list_move_tail(&first->node, &pdmac->desc_pool);
2630
2631 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2632
2633 return NULL;
2634 }
2635
2636 switch (direction) {
2637 case DMA_MEM_TO_DEV:
2638 desc->rqcfg.src_inc = 1;
2639 desc->rqcfg.dst_inc = 0;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002640 src = dma_addr;
2641 dst = pch->fifo_addr;
2642 break;
2643 case DMA_DEV_TO_MEM:
2644 desc->rqcfg.src_inc = 0;
2645 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002646 src = pch->fifo_addr;
2647 dst = dma_addr;
2648 break;
2649 default:
2650 break;
2651 }
2652
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002653 desc->req.rqtype = direction;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002654 desc->rqcfg.brst_size = pch->burst_sz;
2655 desc->rqcfg.brst_len = 1;
2656 fill_px(&desc->px, dst, src, period_len);
2657
2658 if (!first)
2659 first = desc;
2660 else
2661 list_add_tail(&desc->node, &first->node);
2662
2663 dma_addr += period_len;
2664 }
2665
2666 if (!desc)
2667 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002668
2669 pch->cyclic = true;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002670 desc->txd.flags = flags;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002671
2672 return &desc->txd;
2673}
2674
Jassi Brarb3040e42010-05-23 20:28:19 -07002675static struct dma_async_tx_descriptor *
2676pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2677 dma_addr_t src, size_t len, unsigned long flags)
2678{
2679 struct dma_pl330_desc *desc;
2680 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002681 struct pl330_info *pi;
2682 int burst;
2683
Rob Herring4e0e6102011-07-25 16:05:04 -05002684 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002685 return NULL;
2686
Jassi Brarb3040e42010-05-23 20:28:19 -07002687 pi = &pch->dmac->pif;
2688
2689 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2690 if (!desc)
2691 return NULL;
2692
2693 desc->rqcfg.src_inc = 1;
2694 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002695 desc->req.rqtype = DMA_MEM_TO_MEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002696
2697 /* Select max possible burst size */
2698 burst = pi->pcfg.data_bus_width / 8;
2699
2700 while (burst > 1) {
2701 if (!(len % burst))
2702 break;
2703 burst /= 2;
2704 }
2705
2706 desc->rqcfg.brst_size = 0;
2707 while (burst != (1 << desc->rqcfg.brst_size))
2708 desc->rqcfg.brst_size++;
2709
2710 desc->rqcfg.brst_len = get_burst_len(desc, len);
2711
2712 desc->txd.flags = flags;
2713
2714 return &desc->txd;
2715}
2716
Chanho Park52a9d172013-08-09 20:11:33 +09002717static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac,
2718 struct dma_pl330_desc *first)
2719{
2720 unsigned long flags;
2721 struct dma_pl330_desc *desc;
2722
2723 if (!first)
2724 return;
2725
2726 spin_lock_irqsave(&pdmac->pool_lock, flags);
2727
2728 while (!list_empty(&first->node)) {
2729 desc = list_entry(first->node.next,
2730 struct dma_pl330_desc, node);
2731 list_move_tail(&desc->node, &pdmac->desc_pool);
2732 }
2733
2734 list_move_tail(&first->node, &pdmac->desc_pool);
2735
2736 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2737}
2738
Jassi Brarb3040e42010-05-23 20:28:19 -07002739static struct dma_async_tx_descriptor *
2740pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302741 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002742 unsigned long flg, void *context)
Jassi Brarb3040e42010-05-23 20:28:19 -07002743{
2744 struct dma_pl330_desc *first, *desc = NULL;
2745 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002746 struct scatterlist *sg;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002747 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002748 dma_addr_t addr;
2749
Thomas Abrahamcd072512011-10-24 11:43:11 +02002750 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002751 return NULL;
2752
Boojin Kim1b9bb712011-09-02 09:44:30 +09002753 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002754
2755 first = NULL;
2756
2757 for_each_sg(sgl, sg, sg_len, i) {
2758
2759 desc = pl330_get_desc(pch);
2760 if (!desc) {
2761 struct dma_pl330_dmac *pdmac = pch->dmac;
2762
2763 dev_err(pch->dmac->pif.dev,
2764 "%s:%d Unable to fetch desc\n",
2765 __func__, __LINE__);
Chanho Park52a9d172013-08-09 20:11:33 +09002766 __pl330_giveback_desc(pdmac, first);
Jassi Brarb3040e42010-05-23 20:28:19 -07002767
2768 return NULL;
2769 }
2770
2771 if (!first)
2772 first = desc;
2773 else
2774 list_add_tail(&desc->node, &first->node);
2775
Vinod Kouldb8196d2011-10-13 22:34:23 +05302776 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002777 desc->rqcfg.src_inc = 1;
2778 desc->rqcfg.dst_inc = 0;
2779 fill_px(&desc->px,
2780 addr, sg_dma_address(sg), sg_dma_len(sg));
2781 } else {
2782 desc->rqcfg.src_inc = 0;
2783 desc->rqcfg.dst_inc = 1;
2784 fill_px(&desc->px,
2785 sg_dma_address(sg), addr, sg_dma_len(sg));
2786 }
2787
Boojin Kim1b9bb712011-09-02 09:44:30 +09002788 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002789 desc->rqcfg.brst_len = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002790 desc->req.rqtype = direction;
Jassi Brarb3040e42010-05-23 20:28:19 -07002791 }
2792
2793 /* Return the last desc in the chain */
2794 desc->txd.flags = flg;
2795 return &desc->txd;
2796}
2797
2798static irqreturn_t pl330_irq_handler(int irq, void *data)
2799{
2800 if (pl330_update(data))
2801 return IRQ_HANDLED;
2802 else
2803 return IRQ_NONE;
2804}
2805
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002806#define PL330_DMA_BUSWIDTHS \
2807 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2808 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2809 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2810 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2811 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2812
2813static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2814 struct dma_slave_caps *caps)
2815{
2816 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2817 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2818 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2819 caps->cmd_pause = false;
2820 caps->cmd_terminate = true;
Lars-Peter Clausenbfb9bb42014-01-11 14:02:17 +01002821 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002822
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002823 return 0;
2824}
2825
Bill Pemberton463a1f82012-11-19 13:22:55 -05002826static int
Russell Kingaa25afa2011-02-19 15:55:00 +00002827pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002828{
2829 struct dma_pl330_platdata *pdat;
2830 struct dma_pl330_dmac *pdmac;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302831 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002832 struct pl330_info *pi;
2833 struct dma_device *pd;
2834 struct resource *res;
2835 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002836 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002837
Jingoo Hand4adcc02013-07-30 17:09:11 +09002838 pdat = dev_get_platdata(&adev->dev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002839
Russell King64113012013-06-27 10:29:32 +01002840 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2841 if (ret)
2842 return ret;
2843
Jassi Brarb3040e42010-05-23 20:28:19 -07002844 /* Allocate a new DMAC and its Channels */
Sachin Kamate4d43c12012-11-15 06:27:50 +00002845 pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002846 if (!pdmac) {
2847 dev_err(&adev->dev, "unable to allocate mem\n");
2848 return -ENOMEM;
2849 }
2850
2851 pi = &pdmac->pif;
2852 pi->dev = &adev->dev;
2853 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002854 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002855
2856 res = &adev->res;
Sachin Kamatbcc7fa92013-03-04 14:36:27 +05302857 pi->base = devm_ioremap_resource(&adev->dev, res);
2858 if (IS_ERR(pi->base))
2859 return PTR_ERR(pi->base);
Jassi Brarb3040e42010-05-23 20:28:19 -07002860
Boojin Kima2f52032011-09-02 09:44:29 +09002861 amba_set_drvdata(adev, pdmac);
2862
Dan Carpenter02808b42013-11-08 12:50:24 +03002863 for (i = 0; i < AMBA_NR_IRQS; i++) {
Michal Simeke98b3ca2013-09-30 08:50:48 +02002864 irq = adev->irq[i];
2865 if (irq) {
2866 ret = devm_request_irq(&adev->dev, irq,
2867 pl330_irq_handler, 0,
2868 dev_name(&adev->dev), pi);
2869 if (ret)
2870 return ret;
2871 } else {
2872 break;
2873 }
2874 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002875
Will Deacon09677172013-06-10 19:34:37 +01002876 pi->pcfg.periph_id = adev->periphid;
Jassi Brarb3040e42010-05-23 20:28:19 -07002877 ret = pl330_add(pi);
2878 if (ret)
Michal Simek173e8382013-09-04 16:40:17 +02002879 return ret;
Jassi Brarb3040e42010-05-23 20:28:19 -07002880
2881 INIT_LIST_HEAD(&pdmac->desc_pool);
2882 spin_lock_init(&pdmac->pool_lock);
2883
2884 /* Create a descriptor pool of default size */
2885 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2886 dev_warn(&adev->dev, "unable to allocate desc\n");
2887
2888 pd = &pdmac->ddma;
2889 INIT_LIST_HEAD(&pd->channels);
2890
2891 /* Initialize channel parameters */
Olof Johanssonc8473822012-04-08 16:26:19 -07002892 if (pdat)
2893 num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan);
2894 else
2895 num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan);
2896
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002897 pdmac->num_peripherals = num_chan;
2898
Rob Herring4e0e6102011-07-25 16:05:04 -05002899 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Sachin Kamat61c6e752012-09-17 15:20:23 +05302900 if (!pdmac->peripherals) {
2901 ret = -ENOMEM;
2902 dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n");
Sachin Kamate4d43c12012-11-15 06:27:50 +00002903 goto probe_err2;
Sachin Kamat61c6e752012-09-17 15:20:23 +05302904 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002905
Rob Herring4e0e6102011-07-25 16:05:04 -05002906 for (i = 0; i < num_chan; i++) {
2907 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002908 if (!adev->dev.of_node)
2909 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2910 else
2911 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002912
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002913 INIT_LIST_HEAD(&pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002914 INIT_LIST_HEAD(&pch->work_list);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002915 INIT_LIST_HEAD(&pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002916 spin_lock_init(&pch->lock);
2917 pch->pl330_chid = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002918 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07002919 pch->dmac = pdmac;
2920
2921 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002922 list_add_tail(&pch->chan.device_node, &pd->channels);
2923 }
2924
2925 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002926 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002927 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002928 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002929 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002930 if (pi->pcfg.num_peri) {
2931 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2932 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
Tushar Behera5557a412012-08-29 10:16:25 +05302933 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002934 }
2935 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002936
2937 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2938 pd->device_free_chan_resources = pl330_free_chan_resources;
2939 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002940 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07002941 pd->device_tx_status = pl330_tx_status;
2942 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2943 pd->device_control = pl330_control;
2944 pd->device_issue_pending = pl330_issue_pending;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002945 pd->device_slave_caps = pl330_dma_device_slave_caps;
Jassi Brarb3040e42010-05-23 20:28:19 -07002946
2947 ret = dma_async_device_register(pd);
2948 if (ret) {
2949 dev_err(&adev->dev, "unable to register DMAC\n");
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302950 goto probe_err3;
2951 }
2952
2953 if (adev->dev.of_node) {
2954 ret = of_dma_controller_register(adev->dev.of_node,
2955 of_dma_pl330_xlate, pdmac);
2956 if (ret) {
2957 dev_err(&adev->dev,
2958 "unable to register DMA to the generic DT DMA helpers\n");
2959 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002960 }
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01002961
2962 adev->dev.dma_parms = &pdmac->dma_parms;
2963
Vinod Kouldbaf6d82013-09-02 21:54:48 +05302964 /*
2965 * This is the limit for transfers with a buswidth of 1, larger
2966 * buswidths will have larger limits.
2967 */
2968 ret = dma_set_max_seg_size(&adev->dev, 1900800);
2969 if (ret)
2970 dev_err(&adev->dev, "unable to set the seg size\n");
2971
Jassi Brarb3040e42010-05-23 20:28:19 -07002972
Jassi Brarb3040e42010-05-23 20:28:19 -07002973 dev_info(&adev->dev,
2974 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
2975 dev_info(&adev->dev,
2976 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
2977 pi->pcfg.data_buf_dep,
2978 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
2979 pi->pcfg.num_peri, pi->pcfg.num_events);
2980
2981 return 0;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302982probe_err3:
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302983 /* Idle the DMAC */
2984 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
2985 chan.device_node) {
2986
2987 /* Remove the channel */
2988 list_del(&pch->chan.device_node);
2989
2990 /* Flush the channel */
2991 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2992 pl330_free_chan_resources(&pch->chan);
2993 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002994probe_err2:
Sachin Kamate4d43c12012-11-15 06:27:50 +00002995 pl330_del(pi);
Jassi Brarb3040e42010-05-23 20:28:19 -07002996
2997 return ret;
2998}
2999
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08003000static int pl330_remove(struct amba_device *adev)
Jassi Brarb3040e42010-05-23 20:28:19 -07003001{
3002 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
3003 struct dma_pl330_chan *pch, *_p;
3004 struct pl330_info *pi;
Jassi Brarb3040e42010-05-23 20:28:19 -07003005
3006 if (!pdmac)
3007 return 0;
3008
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303009 if (adev->dev.of_node)
3010 of_dma_controller_free(adev->dev.of_node);
Padmavathi Venna421da892013-02-14 09:10:07 +05303011
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303012 dma_async_device_unregister(&pdmac->ddma);
Jassi Brarb3040e42010-05-23 20:28:19 -07003013
3014 /* Idle the DMAC */
3015 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3016 chan.device_node) {
3017
3018 /* Remove the channel */
3019 list_del(&pch->chan.device_node);
3020
3021 /* Flush the channel */
3022 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3023 pl330_free_chan_resources(&pch->chan);
3024 }
3025
3026 pi = &pdmac->pif;
3027
3028 pl330_del(pi);
3029
Jassi Brarb3040e42010-05-23 20:28:19 -07003030 return 0;
3031}
3032
3033static struct amba_id pl330_ids[] = {
3034 {
3035 .id = 0x00041330,
3036 .mask = 0x000fffff,
3037 },
3038 { 0, 0 },
3039};
3040
Dave Martine8fa5162011-10-05 15:15:20 +01003041MODULE_DEVICE_TABLE(amba, pl330_ids);
3042
Jassi Brarb3040e42010-05-23 20:28:19 -07003043static struct amba_driver pl330_driver = {
3044 .drv = {
3045 .owner = THIS_MODULE,
3046 .name = "dma-pl330",
Jassi Brarb3040e42010-05-23 20:28:19 -07003047 },
3048 .id_table = pl330_ids,
3049 .probe = pl330_probe,
3050 .remove = pl330_remove,
3051};
3052
viresh kumar9e5ed092012-03-15 10:40:38 +01003053module_amba_driver(pl330_driver);
Jassi Brarb3040e42010-05-23 20:28:19 -07003054
3055MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3056MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3057MODULE_LICENSE("GPL");