Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[linux-2.6.git] / drivers / dma / pl330.c
blobdf8b10fd1726ed466d2b05e814a8feb6c711dfe3
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassi.brar@samsung.com>
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.
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dmaengine.h>
24 #include <linux/amba/bus.h>
25 #include <linux/amba/pl330.h>
26 #include <linux/scatterlist.h>
27 #include <linux/of.h>
28 #include <linux/of_dma.h>
29 #include <linux/err.h>
31 #include "dmaengine.h"
32 #define PL330_MAX_CHAN 8
33 #define PL330_MAX_IRQS 32
34 #define PL330_MAX_PERI 32
36 enum pl330_srccachectrl {
37 SCCTRL0, /* Noncacheable and nonbufferable */
38 SCCTRL1, /* Bufferable only */
39 SCCTRL2, /* Cacheable, but do not allocate */
40 SCCTRL3, /* Cacheable and bufferable, but do not allocate */
41 SINVALID1,
42 SINVALID2,
43 SCCTRL6, /* Cacheable write-through, allocate on reads only */
44 SCCTRL7, /* Cacheable write-back, allocate on reads only */
47 enum pl330_dstcachectrl {
48 DCCTRL0, /* Noncacheable and nonbufferable */
49 DCCTRL1, /* Bufferable only */
50 DCCTRL2, /* Cacheable, but do not allocate */
51 DCCTRL3, /* Cacheable and bufferable, but do not allocate */
52 DINVALID1, /* AWCACHE = 0x1000 */
53 DINVALID2,
54 DCCTRL6, /* Cacheable write-through, allocate on writes only */
55 DCCTRL7, /* Cacheable write-back, allocate on writes only */
58 enum pl330_byteswap {
59 SWAP_NO,
60 SWAP_2,
61 SWAP_4,
62 SWAP_8,
63 SWAP_16,
66 enum pl330_reqtype {
67 MEMTOMEM,
68 MEMTODEV,
69 DEVTOMEM,
70 DEVTODEV,
73 /* Register and Bit field Definitions */
74 #define DS 0x0
75 #define DS_ST_STOP 0x0
76 #define DS_ST_EXEC 0x1
77 #define DS_ST_CMISS 0x2
78 #define DS_ST_UPDTPC 0x3
79 #define DS_ST_WFE 0x4
80 #define DS_ST_ATBRR 0x5
81 #define DS_ST_QBUSY 0x6
82 #define DS_ST_WFP 0x7
83 #define DS_ST_KILL 0x8
84 #define DS_ST_CMPLT 0x9
85 #define DS_ST_FLTCMP 0xe
86 #define DS_ST_FAULT 0xf
88 #define DPC 0x4
89 #define INTEN 0x20
90 #define ES 0x24
91 #define INTSTATUS 0x28
92 #define INTCLR 0x2c
93 #define FSM 0x30
94 #define FSC 0x34
95 #define FTM 0x38
97 #define _FTC 0x40
98 #define FTC(n) (_FTC + (n)*0x4)
100 #define _CS 0x100
101 #define CS(n) (_CS + (n)*0x8)
102 #define CS_CNS (1 << 21)
104 #define _CPC 0x104
105 #define CPC(n) (_CPC + (n)*0x8)
107 #define _SA 0x400
108 #define SA(n) (_SA + (n)*0x20)
110 #define _DA 0x404
111 #define DA(n) (_DA + (n)*0x20)
113 #define _CC 0x408
114 #define CC(n) (_CC + (n)*0x20)
116 #define CC_SRCINC (1 << 0)
117 #define CC_DSTINC (1 << 14)
118 #define CC_SRCPRI (1 << 8)
119 #define CC_DSTPRI (1 << 22)
120 #define CC_SRCNS (1 << 9)
121 #define CC_DSTNS (1 << 23)
122 #define CC_SRCIA (1 << 10)
123 #define CC_DSTIA (1 << 24)
124 #define CC_SRCBRSTLEN_SHFT 4
125 #define CC_DSTBRSTLEN_SHFT 18
126 #define CC_SRCBRSTSIZE_SHFT 1
127 #define CC_DSTBRSTSIZE_SHFT 15
128 #define CC_SRCCCTRL_SHFT 11
129 #define CC_SRCCCTRL_MASK 0x7
130 #define CC_DSTCCTRL_SHFT 25
131 #define CC_DRCCCTRL_MASK 0x7
132 #define CC_SWAP_SHFT 28
134 #define _LC0 0x40c
135 #define LC0(n) (_LC0 + (n)*0x20)
137 #define _LC1 0x410
138 #define LC1(n) (_LC1 + (n)*0x20)
140 #define DBGSTATUS 0xd00
141 #define DBG_BUSY (1 << 0)
143 #define DBGCMD 0xd04
144 #define DBGINST0 0xd08
145 #define DBGINST1 0xd0c
147 #define CR0 0xe00
148 #define CR1 0xe04
149 #define CR2 0xe08
150 #define CR3 0xe0c
151 #define CR4 0xe10
152 #define CRD 0xe14
154 #define PERIPH_ID 0xfe0
155 #define PERIPH_REV_SHIFT 20
156 #define PERIPH_REV_MASK 0xf
157 #define PERIPH_REV_R0P0 0
158 #define PERIPH_REV_R1P0 1
159 #define PERIPH_REV_R1P1 2
161 #define CR0_PERIPH_REQ_SET (1 << 0)
162 #define CR0_BOOT_EN_SET (1 << 1)
163 #define CR0_BOOT_MAN_NS (1 << 2)
164 #define CR0_NUM_CHANS_SHIFT 4
165 #define CR0_NUM_CHANS_MASK 0x7
166 #define CR0_NUM_PERIPH_SHIFT 12
167 #define CR0_NUM_PERIPH_MASK 0x1f
168 #define CR0_NUM_EVENTS_SHIFT 17
169 #define CR0_NUM_EVENTS_MASK 0x1f
171 #define CR1_ICACHE_LEN_SHIFT 0
172 #define CR1_ICACHE_LEN_MASK 0x7
173 #define CR1_NUM_ICACHELINES_SHIFT 4
174 #define CR1_NUM_ICACHELINES_MASK 0xf
176 #define CRD_DATA_WIDTH_SHIFT 0
177 #define CRD_DATA_WIDTH_MASK 0x7
178 #define CRD_WR_CAP_SHIFT 4
179 #define CRD_WR_CAP_MASK 0x7
180 #define CRD_WR_Q_DEP_SHIFT 8
181 #define CRD_WR_Q_DEP_MASK 0xf
182 #define CRD_RD_CAP_SHIFT 12
183 #define CRD_RD_CAP_MASK 0x7
184 #define CRD_RD_Q_DEP_SHIFT 16
185 #define CRD_RD_Q_DEP_MASK 0xf
186 #define CRD_DATA_BUFF_SHIFT 20
187 #define CRD_DATA_BUFF_MASK 0x3ff
189 #define PART 0x330
190 #define DESIGNER 0x41
191 #define REVISION 0x0
192 #define INTEG_CFG 0x0
193 #define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
195 #define PL330_STATE_STOPPED (1 << 0)
196 #define PL330_STATE_EXECUTING (1 << 1)
197 #define PL330_STATE_WFE (1 << 2)
198 #define PL330_STATE_FAULTING (1 << 3)
199 #define PL330_STATE_COMPLETING (1 << 4)
200 #define PL330_STATE_WFP (1 << 5)
201 #define PL330_STATE_KILLING (1 << 6)
202 #define PL330_STATE_FAULT_COMPLETING (1 << 7)
203 #define PL330_STATE_CACHEMISS (1 << 8)
204 #define PL330_STATE_UPDTPC (1 << 9)
205 #define PL330_STATE_ATBARRIER (1 << 10)
206 #define PL330_STATE_QUEUEBUSY (1 << 11)
207 #define PL330_STATE_INVALID (1 << 15)
209 #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
210 | PL330_STATE_WFE | PL330_STATE_FAULTING)
212 #define CMD_DMAADDH 0x54
213 #define CMD_DMAEND 0x00
214 #define CMD_DMAFLUSHP 0x35
215 #define CMD_DMAGO 0xa0
216 #define CMD_DMALD 0x04
217 #define CMD_DMALDP 0x25
218 #define CMD_DMALP 0x20
219 #define CMD_DMALPEND 0x28
220 #define CMD_DMAKILL 0x01
221 #define CMD_DMAMOV 0xbc
222 #define CMD_DMANOP 0x18
223 #define CMD_DMARMB 0x12
224 #define CMD_DMASEV 0x34
225 #define CMD_DMAST 0x08
226 #define CMD_DMASTP 0x29
227 #define CMD_DMASTZ 0x0c
228 #define CMD_DMAWFE 0x36
229 #define CMD_DMAWFP 0x30
230 #define CMD_DMAWMB 0x13
232 #define SZ_DMAADDH 3
233 #define SZ_DMAEND 1
234 #define SZ_DMAFLUSHP 2
235 #define SZ_DMALD 1
236 #define SZ_DMALDP 2
237 #define SZ_DMALP 2
238 #define SZ_DMALPEND 2
239 #define SZ_DMAKILL 1
240 #define SZ_DMAMOV 6
241 #define SZ_DMANOP 1
242 #define SZ_DMARMB 1
243 #define SZ_DMASEV 2
244 #define SZ_DMAST 1
245 #define SZ_DMASTP 2
246 #define SZ_DMASTZ 1
247 #define SZ_DMAWFE 2
248 #define SZ_DMAWFP 2
249 #define SZ_DMAWMB 1
250 #define SZ_DMAGO 6
252 #define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
253 #define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
255 #define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
256 #define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
259 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
260 * at 1byte/burst for P<->M and M<->M respectively.
261 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
262 * should be enough for P<->M and M<->M respectively.
264 #define MCODE_BUFF_PER_REQ 256
266 /* If the _pl330_req is available to the client */
267 #define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
269 /* Use this _only_ to wait on transient states */
270 #define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
272 #ifdef PL330_DEBUG_MCGEN
273 static unsigned cmd_line;
274 #define PL330_DBGCMD_DUMP(off, x...) do { \
275 printk("%x:", cmd_line); \
276 printk(x); \
277 cmd_line += off; \
278 } while (0)
279 #define PL330_DBGMC_START(addr) (cmd_line = addr)
280 #else
281 #define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
282 #define PL330_DBGMC_START(addr) do {} while (0)
283 #endif
285 /* The number of default descriptors */
287 #define NR_DEFAULT_DESC 16
289 /* Populated by the PL330 core driver for DMA API driver's info */
290 struct pl330_config {
291 u32 periph_id;
292 #define DMAC_MODE_NS (1 << 0)
293 unsigned int mode;
294 unsigned int data_bus_width:10; /* In number of bits */
295 unsigned int data_buf_dep:10;
296 unsigned int num_chan:4;
297 unsigned int num_peri:6;
298 u32 peri_ns;
299 unsigned int num_events:6;
300 u32 irq_ns;
303 /* Handle to the DMAC provided to the PL330 core */
304 struct pl330_info {
305 /* Owning device */
306 struct device *dev;
307 /* Size of MicroCode buffers for each channel. */
308 unsigned mcbufsz;
309 /* ioremap'ed address of PL330 registers. */
310 void __iomem *base;
311 /* Client can freely use it. */
312 void *client_data;
313 /* PL330 core data, Client must not touch it. */
314 void *pl330_data;
315 /* Populated by the PL330 core driver during pl330_add */
316 struct pl330_config pcfg;
318 * If the DMAC has some reset mechanism, then the
319 * client may want to provide pointer to the method.
321 void (*dmac_reset)(struct pl330_info *pi);
325 * Request Configuration.
326 * The PL330 core does not modify this and uses the last
327 * working configuration if the request doesn't provide any.
329 * The Client may want to provide this info only for the
330 * first request and a request with new settings.
332 struct pl330_reqcfg {
333 /* Address Incrementing */
334 unsigned dst_inc:1;
335 unsigned src_inc:1;
338 * For now, the SRC & DST protection levels
339 * and burst size/length are assumed same.
341 bool nonsecure;
342 bool privileged;
343 bool insnaccess;
344 unsigned brst_len:5;
345 unsigned brst_size:3; /* in power of 2 */
347 enum pl330_dstcachectrl dcctl;
348 enum pl330_srccachectrl scctl;
349 enum pl330_byteswap swap;
350 struct pl330_config *pcfg;
354 * One cycle of DMAC operation.
355 * There may be more than one xfer in a request.
357 struct pl330_xfer {
358 u32 src_addr;
359 u32 dst_addr;
360 /* Size to xfer */
361 u32 bytes;
363 * Pointer to next xfer in the list.
364 * The last xfer in the req must point to NULL.
366 struct pl330_xfer *next;
369 /* The xfer callbacks are made with one of these arguments. */
370 enum pl330_op_err {
371 /* The all xfers in the request were success. */
372 PL330_ERR_NONE,
373 /* If req aborted due to global error. */
374 PL330_ERR_ABORT,
375 /* If req failed due to problem with Channel. */
376 PL330_ERR_FAIL,
379 /* A request defining Scatter-Gather List ending with NULL xfer. */
380 struct pl330_req {
381 enum pl330_reqtype rqtype;
382 /* Index of peripheral for the xfer. */
383 unsigned peri:5;
384 /* Unique token for this xfer, set by the client. */
385 void *token;
386 /* Callback to be called after xfer. */
387 void (*xfer_cb)(void *token, enum pl330_op_err err);
388 /* If NULL, req will be done at last set parameters. */
389 struct pl330_reqcfg *cfg;
390 /* Pointer to first xfer in the request. */
391 struct pl330_xfer *x;
392 /* Hook to attach to DMAC's list of reqs with due callback */
393 struct list_head rqd;
397 * To know the status of the channel and DMAC, the client
398 * provides a pointer to this structure. The PL330 core
399 * fills it with current information.
401 struct pl330_chanstatus {
403 * If the DMAC engine halted due to some error,
404 * the client should remove-add DMAC.
406 bool dmac_halted;
408 * If channel is halted due to some error,
409 * the client should ABORT/FLUSH and START the channel.
411 bool faulting;
412 /* Location of last load */
413 u32 src_addr;
414 /* Location of last store */
415 u32 dst_addr;
417 * Pointer to the currently active req, NULL if channel is
418 * inactive, even though the requests may be present.
420 struct pl330_req *top_req;
421 /* Pointer to req waiting second in the queue if any. */
422 struct pl330_req *wait_req;
425 enum pl330_chan_op {
426 /* Start the channel */
427 PL330_OP_START,
428 /* Abort the active xfer */
429 PL330_OP_ABORT,
430 /* Stop xfer and flush queue */
431 PL330_OP_FLUSH,
434 struct _xfer_spec {
435 u32 ccr;
436 struct pl330_req *r;
437 struct pl330_xfer *x;
440 enum dmamov_dst {
441 SAR = 0,
442 CCR,
443 DAR,
446 enum pl330_dst {
447 SRC = 0,
448 DST,
451 enum pl330_cond {
452 SINGLE,
453 BURST,
454 ALWAYS,
457 struct _pl330_req {
458 u32 mc_bus;
459 void *mc_cpu;
460 /* Number of bytes taken to setup MC for the req */
461 u32 mc_len;
462 struct pl330_req *r;
465 /* ToBeDone for tasklet */
466 struct _pl330_tbd {
467 bool reset_dmac;
468 bool reset_mngr;
469 u8 reset_chan;
472 /* A DMAC Thread */
473 struct pl330_thread {
474 u8 id;
475 int ev;
476 /* If the channel is not yet acquired by any client */
477 bool free;
478 /* Parent DMAC */
479 struct pl330_dmac *dmac;
480 /* Only two at a time */
481 struct _pl330_req req[2];
482 /* Index of the last enqueued request */
483 unsigned lstenq;
484 /* Index of the last submitted request or -1 if the DMA is stopped */
485 int req_running;
488 enum pl330_dmac_state {
489 UNINIT,
490 INIT,
491 DYING,
494 /* A DMAC */
495 struct pl330_dmac {
496 spinlock_t lock;
497 /* Holds list of reqs with due callbacks */
498 struct list_head req_done;
499 /* Pointer to platform specific stuff */
500 struct pl330_info *pinfo;
501 /* Maximum possible events/irqs */
502 int events[32];
503 /* BUS address of MicroCode buffer */
504 dma_addr_t mcode_bus;
505 /* CPU address of MicroCode buffer */
506 void *mcode_cpu;
507 /* List of all Channel threads */
508 struct pl330_thread *channels;
509 /* Pointer to the MANAGER thread */
510 struct pl330_thread *manager;
511 /* To handle bad news in interrupt */
512 struct tasklet_struct tasks;
513 struct _pl330_tbd dmac_tbd;
514 /* State of DMAC operation */
515 enum pl330_dmac_state state;
518 enum desc_status {
519 /* In the DMAC pool */
520 FREE,
522 * Allocated to some channel during prep_xxx
523 * Also may be sitting on the work_list.
525 PREP,
527 * Sitting on the work_list and already submitted
528 * to the PL330 core. Not more than two descriptors
529 * of a channel can be BUSY at any time.
531 BUSY,
533 * Sitting on the channel work_list but xfer done
534 * by PL330 core
536 DONE,
539 struct dma_pl330_chan {
540 /* Schedule desc completion */
541 struct tasklet_struct task;
543 /* DMA-Engine Channel */
544 struct dma_chan chan;
546 /* List of to be xfered descriptors */
547 struct list_head work_list;
548 /* List of completed descriptors */
549 struct list_head completed_list;
551 /* Pointer to the DMAC that manages this channel,
552 * NULL if the channel is available to be acquired.
553 * As the parent, this DMAC also provides descriptors
554 * to the channel.
556 struct dma_pl330_dmac *dmac;
558 /* To protect channel manipulation */
559 spinlock_t lock;
561 /* Token of a hardware channel thread of PL330 DMAC
562 * NULL if the channel is available to be acquired.
564 void *pl330_chid;
566 /* For D-to-M and M-to-D channels */
567 int burst_sz; /* the peripheral fifo width */
568 int burst_len; /* the number of burst */
569 dma_addr_t fifo_addr;
571 /* for cyclic capability */
572 bool cyclic;
575 struct dma_pl330_dmac {
576 struct pl330_info pif;
578 /* DMA-Engine Device */
579 struct dma_device ddma;
581 /* Pool of descriptors available for the DMAC's channels */
582 struct list_head desc_pool;
583 /* To protect desc_pool manipulation */
584 spinlock_t pool_lock;
586 /* Peripheral channels connected to this DMAC */
587 struct dma_pl330_chan *peripherals; /* keep at end */
590 struct dma_pl330_desc {
591 /* To attach to a queue as child */
592 struct list_head node;
594 /* Descriptor for the DMA Engine API */
595 struct dma_async_tx_descriptor txd;
597 /* Xfer for PL330 core */
598 struct pl330_xfer px;
600 struct pl330_reqcfg rqcfg;
601 struct pl330_req req;
603 enum desc_status status;
605 /* The channel which currently holds this desc */
606 struct dma_pl330_chan *pchan;
609 struct dma_pl330_filter_args {
610 struct dma_pl330_dmac *pdmac;
611 unsigned int chan_id;
614 static inline void _callback(struct pl330_req *r, enum pl330_op_err err)
616 if (r && r->xfer_cb)
617 r->xfer_cb(r->token, err);
620 static inline bool _queue_empty(struct pl330_thread *thrd)
622 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
623 ? true : false;
626 static inline bool _queue_full(struct pl330_thread *thrd)
628 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
629 ? false : true;
632 static inline bool is_manager(struct pl330_thread *thrd)
634 struct pl330_dmac *pl330 = thrd->dmac;
636 /* MANAGER is indexed at the end */
637 if (thrd->id == pl330->pinfo->pcfg.num_chan)
638 return true;
639 else
640 return false;
643 /* If manager of the thread is in Non-Secure mode */
644 static inline bool _manager_ns(struct pl330_thread *thrd)
646 struct pl330_dmac *pl330 = thrd->dmac;
648 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
651 static inline u32 get_revision(u32 periph_id)
653 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
656 static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
657 enum pl330_dst da, u16 val)
659 if (dry_run)
660 return SZ_DMAADDH;
662 buf[0] = CMD_DMAADDH;
663 buf[0] |= (da << 1);
664 *((u16 *)&buf[1]) = val;
666 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
667 da == 1 ? "DA" : "SA", val);
669 return SZ_DMAADDH;
672 static inline u32 _emit_END(unsigned dry_run, u8 buf[])
674 if (dry_run)
675 return SZ_DMAEND;
677 buf[0] = CMD_DMAEND;
679 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
681 return SZ_DMAEND;
684 static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
686 if (dry_run)
687 return SZ_DMAFLUSHP;
689 buf[0] = CMD_DMAFLUSHP;
691 peri &= 0x1f;
692 peri <<= 3;
693 buf[1] = peri;
695 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
697 return SZ_DMAFLUSHP;
700 static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
702 if (dry_run)
703 return SZ_DMALD;
705 buf[0] = CMD_DMALD;
707 if (cond == SINGLE)
708 buf[0] |= (0 << 1) | (1 << 0);
709 else if (cond == BURST)
710 buf[0] |= (1 << 1) | (1 << 0);
712 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
713 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
715 return SZ_DMALD;
718 static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
719 enum pl330_cond cond, u8 peri)
721 if (dry_run)
722 return SZ_DMALDP;
724 buf[0] = CMD_DMALDP;
726 if (cond == BURST)
727 buf[0] |= (1 << 1);
729 peri &= 0x1f;
730 peri <<= 3;
731 buf[1] = peri;
733 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
734 cond == SINGLE ? 'S' : 'B', peri >> 3);
736 return SZ_DMALDP;
739 static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
740 unsigned loop, u8 cnt)
742 if (dry_run)
743 return SZ_DMALP;
745 buf[0] = CMD_DMALP;
747 if (loop)
748 buf[0] |= (1 << 1);
750 cnt--; /* DMAC increments by 1 internally */
751 buf[1] = cnt;
753 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
755 return SZ_DMALP;
758 struct _arg_LPEND {
759 enum pl330_cond cond;
760 bool forever;
761 unsigned loop;
762 u8 bjump;
765 static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
766 const struct _arg_LPEND *arg)
768 enum pl330_cond cond = arg->cond;
769 bool forever = arg->forever;
770 unsigned loop = arg->loop;
771 u8 bjump = arg->bjump;
773 if (dry_run)
774 return SZ_DMALPEND;
776 buf[0] = CMD_DMALPEND;
778 if (loop)
779 buf[0] |= (1 << 2);
781 if (!forever)
782 buf[0] |= (1 << 4);
784 if (cond == SINGLE)
785 buf[0] |= (0 << 1) | (1 << 0);
786 else if (cond == BURST)
787 buf[0] |= (1 << 1) | (1 << 0);
789 buf[1] = bjump;
791 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
792 forever ? "FE" : "END",
793 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
794 loop ? '1' : '0',
795 bjump);
797 return SZ_DMALPEND;
800 static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
802 if (dry_run)
803 return SZ_DMAKILL;
805 buf[0] = CMD_DMAKILL;
807 return SZ_DMAKILL;
810 static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
811 enum dmamov_dst dst, u32 val)
813 if (dry_run)
814 return SZ_DMAMOV;
816 buf[0] = CMD_DMAMOV;
817 buf[1] = dst;
818 *((u32 *)&buf[2]) = val;
820 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
821 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
823 return SZ_DMAMOV;
826 static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
828 if (dry_run)
829 return SZ_DMANOP;
831 buf[0] = CMD_DMANOP;
833 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
835 return SZ_DMANOP;
838 static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
840 if (dry_run)
841 return SZ_DMARMB;
843 buf[0] = CMD_DMARMB;
845 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
847 return SZ_DMARMB;
850 static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
852 if (dry_run)
853 return SZ_DMASEV;
855 buf[0] = CMD_DMASEV;
857 ev &= 0x1f;
858 ev <<= 3;
859 buf[1] = ev;
861 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
863 return SZ_DMASEV;
866 static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
868 if (dry_run)
869 return SZ_DMAST;
871 buf[0] = CMD_DMAST;
873 if (cond == SINGLE)
874 buf[0] |= (0 << 1) | (1 << 0);
875 else if (cond == BURST)
876 buf[0] |= (1 << 1) | (1 << 0);
878 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
879 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
881 return SZ_DMAST;
884 static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
885 enum pl330_cond cond, u8 peri)
887 if (dry_run)
888 return SZ_DMASTP;
890 buf[0] = CMD_DMASTP;
892 if (cond == BURST)
893 buf[0] |= (1 << 1);
895 peri &= 0x1f;
896 peri <<= 3;
897 buf[1] = peri;
899 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
900 cond == SINGLE ? 'S' : 'B', peri >> 3);
902 return SZ_DMASTP;
905 static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
907 if (dry_run)
908 return SZ_DMASTZ;
910 buf[0] = CMD_DMASTZ;
912 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
914 return SZ_DMASTZ;
917 static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
918 unsigned invalidate)
920 if (dry_run)
921 return SZ_DMAWFE;
923 buf[0] = CMD_DMAWFE;
925 ev &= 0x1f;
926 ev <<= 3;
927 buf[1] = ev;
929 if (invalidate)
930 buf[1] |= (1 << 1);
932 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
933 ev >> 3, invalidate ? ", I" : "");
935 return SZ_DMAWFE;
938 static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
939 enum pl330_cond cond, u8 peri)
941 if (dry_run)
942 return SZ_DMAWFP;
944 buf[0] = CMD_DMAWFP;
946 if (cond == SINGLE)
947 buf[0] |= (0 << 1) | (0 << 0);
948 else if (cond == BURST)
949 buf[0] |= (1 << 1) | (0 << 0);
950 else
951 buf[0] |= (0 << 1) | (1 << 0);
953 peri &= 0x1f;
954 peri <<= 3;
955 buf[1] = peri;
957 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
958 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
960 return SZ_DMAWFP;
963 static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
965 if (dry_run)
966 return SZ_DMAWMB;
968 buf[0] = CMD_DMAWMB;
970 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
972 return SZ_DMAWMB;
975 struct _arg_GO {
976 u8 chan;
977 u32 addr;
978 unsigned ns;
981 static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
982 const struct _arg_GO *arg)
984 u8 chan = arg->chan;
985 u32 addr = arg->addr;
986 unsigned ns = arg->ns;
988 if (dry_run)
989 return SZ_DMAGO;
991 buf[0] = CMD_DMAGO;
992 buf[0] |= (ns << 1);
994 buf[1] = chan & 0x7;
996 *((u32 *)&buf[2]) = addr;
998 return SZ_DMAGO;
1001 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
1003 /* Returns Time-Out */
1004 static bool _until_dmac_idle(struct pl330_thread *thrd)
1006 void __iomem *regs = thrd->dmac->pinfo->base;
1007 unsigned long loops = msecs_to_loops(5);
1009 do {
1010 /* Until Manager is Idle */
1011 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
1012 break;
1014 cpu_relax();
1015 } while (--loops);
1017 if (!loops)
1018 return true;
1020 return false;
1023 static inline void _execute_DBGINSN(struct pl330_thread *thrd,
1024 u8 insn[], bool as_manager)
1026 void __iomem *regs = thrd->dmac->pinfo->base;
1027 u32 val;
1029 val = (insn[0] << 16) | (insn[1] << 24);
1030 if (!as_manager) {
1031 val |= (1 << 0);
1032 val |= (thrd->id << 8); /* Channel Number */
1034 writel(val, regs + DBGINST0);
1036 val = *((u32 *)&insn[2]);
1037 writel(val, regs + DBGINST1);
1039 /* If timed out due to halted state-machine */
1040 if (_until_dmac_idle(thrd)) {
1041 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
1042 return;
1045 /* Get going */
1046 writel(0, regs + DBGCMD);
1050 * Mark a _pl330_req as free.
1051 * We do it by writing DMAEND as the first instruction
1052 * because no valid request is going to have DMAEND as
1053 * its first instruction to execute.
1055 static void mark_free(struct pl330_thread *thrd, int idx)
1057 struct _pl330_req *req = &thrd->req[idx];
1059 _emit_END(0, req->mc_cpu);
1060 req->mc_len = 0;
1062 thrd->req_running = -1;
1065 static inline u32 _state(struct pl330_thread *thrd)
1067 void __iomem *regs = thrd->dmac->pinfo->base;
1068 u32 val;
1070 if (is_manager(thrd))
1071 val = readl(regs + DS) & 0xf;
1072 else
1073 val = readl(regs + CS(thrd->id)) & 0xf;
1075 switch (val) {
1076 case DS_ST_STOP:
1077 return PL330_STATE_STOPPED;
1078 case DS_ST_EXEC:
1079 return PL330_STATE_EXECUTING;
1080 case DS_ST_CMISS:
1081 return PL330_STATE_CACHEMISS;
1082 case DS_ST_UPDTPC:
1083 return PL330_STATE_UPDTPC;
1084 case DS_ST_WFE:
1085 return PL330_STATE_WFE;
1086 case DS_ST_FAULT:
1087 return PL330_STATE_FAULTING;
1088 case DS_ST_ATBRR:
1089 if (is_manager(thrd))
1090 return PL330_STATE_INVALID;
1091 else
1092 return PL330_STATE_ATBARRIER;
1093 case DS_ST_QBUSY:
1094 if (is_manager(thrd))
1095 return PL330_STATE_INVALID;
1096 else
1097 return PL330_STATE_QUEUEBUSY;
1098 case DS_ST_WFP:
1099 if (is_manager(thrd))
1100 return PL330_STATE_INVALID;
1101 else
1102 return PL330_STATE_WFP;
1103 case DS_ST_KILL:
1104 if (is_manager(thrd))
1105 return PL330_STATE_INVALID;
1106 else
1107 return PL330_STATE_KILLING;
1108 case DS_ST_CMPLT:
1109 if (is_manager(thrd))
1110 return PL330_STATE_INVALID;
1111 else
1112 return PL330_STATE_COMPLETING;
1113 case DS_ST_FLTCMP:
1114 if (is_manager(thrd))
1115 return PL330_STATE_INVALID;
1116 else
1117 return PL330_STATE_FAULT_COMPLETING;
1118 default:
1119 return PL330_STATE_INVALID;
1123 static void _stop(struct pl330_thread *thrd)
1125 void __iomem *regs = thrd->dmac->pinfo->base;
1126 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1128 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1129 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1131 /* Return if nothing needs to be done */
1132 if (_state(thrd) == PL330_STATE_COMPLETING
1133 || _state(thrd) == PL330_STATE_KILLING
1134 || _state(thrd) == PL330_STATE_STOPPED)
1135 return;
1137 _emit_KILL(0, insn);
1139 /* Stop generating interrupts for SEV */
1140 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1142 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1145 /* Start doing req 'idx' of thread 'thrd' */
1146 static bool _trigger(struct pl330_thread *thrd)
1148 void __iomem *regs = thrd->dmac->pinfo->base;
1149 struct _pl330_req *req;
1150 struct pl330_req *r;
1151 struct _arg_GO go;
1152 unsigned ns;
1153 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1154 int idx;
1156 /* Return if already ACTIVE */
1157 if (_state(thrd) != PL330_STATE_STOPPED)
1158 return true;
1160 idx = 1 - thrd->lstenq;
1161 if (!IS_FREE(&thrd->req[idx]))
1162 req = &thrd->req[idx];
1163 else {
1164 idx = thrd->lstenq;
1165 if (!IS_FREE(&thrd->req[idx]))
1166 req = &thrd->req[idx];
1167 else
1168 req = NULL;
1171 /* Return if no request */
1172 if (!req || !req->r)
1173 return true;
1175 r = req->r;
1177 if (r->cfg)
1178 ns = r->cfg->nonsecure ? 1 : 0;
1179 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1180 ns = 1;
1181 else
1182 ns = 0;
1184 /* See 'Abort Sources' point-4 at Page 2-25 */
1185 if (_manager_ns(thrd) && !ns)
1186 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1187 __func__, __LINE__);
1189 go.chan = thrd->id;
1190 go.addr = req->mc_bus;
1191 go.ns = ns;
1192 _emit_GO(0, insn, &go);
1194 /* Set to generate interrupts for SEV */
1195 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1197 /* Only manager can execute GO */
1198 _execute_DBGINSN(thrd, insn, true);
1200 thrd->req_running = idx;
1202 return true;
1205 static bool _start(struct pl330_thread *thrd)
1207 switch (_state(thrd)) {
1208 case PL330_STATE_FAULT_COMPLETING:
1209 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1211 if (_state(thrd) == PL330_STATE_KILLING)
1212 UNTIL(thrd, PL330_STATE_STOPPED)
1214 case PL330_STATE_FAULTING:
1215 _stop(thrd);
1217 case PL330_STATE_KILLING:
1218 case PL330_STATE_COMPLETING:
1219 UNTIL(thrd, PL330_STATE_STOPPED)
1221 case PL330_STATE_STOPPED:
1222 return _trigger(thrd);
1224 case PL330_STATE_WFP:
1225 case PL330_STATE_QUEUEBUSY:
1226 case PL330_STATE_ATBARRIER:
1227 case PL330_STATE_UPDTPC:
1228 case PL330_STATE_CACHEMISS:
1229 case PL330_STATE_EXECUTING:
1230 return true;
1232 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1233 default:
1234 return false;
1238 static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1239 const struct _xfer_spec *pxs, int cyc)
1241 int off = 0;
1242 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
1244 /* check lock-up free version */
1245 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1246 while (cyc--) {
1247 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1248 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1250 } else {
1251 while (cyc--) {
1252 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1253 off += _emit_RMB(dry_run, &buf[off]);
1254 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1255 off += _emit_WMB(dry_run, &buf[off]);
1259 return off;
1262 static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1263 const struct _xfer_spec *pxs, int cyc)
1265 int off = 0;
1267 while (cyc--) {
1268 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1269 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1270 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1271 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1274 return off;
1277 static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1278 const struct _xfer_spec *pxs, int cyc)
1280 int off = 0;
1282 while (cyc--) {
1283 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1284 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1285 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1286 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1289 return off;
1292 static int _bursts(unsigned dry_run, u8 buf[],
1293 const struct _xfer_spec *pxs, int cyc)
1295 int off = 0;
1297 switch (pxs->r->rqtype) {
1298 case MEMTODEV:
1299 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1300 break;
1301 case DEVTOMEM:
1302 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1303 break;
1304 case MEMTOMEM:
1305 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1306 break;
1307 default:
1308 off += 0x40000000; /* Scare off the Client */
1309 break;
1312 return off;
1315 /* Returns bytes consumed and updates bursts */
1316 static inline int _loop(unsigned dry_run, u8 buf[],
1317 unsigned long *bursts, const struct _xfer_spec *pxs)
1319 int cyc, cycmax, szlp, szlpend, szbrst, off;
1320 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1321 struct _arg_LPEND lpend;
1323 /* Max iterations possible in DMALP is 256 */
1324 if (*bursts >= 256*256) {
1325 lcnt1 = 256;
1326 lcnt0 = 256;
1327 cyc = *bursts / lcnt1 / lcnt0;
1328 } else if (*bursts > 256) {
1329 lcnt1 = 256;
1330 lcnt0 = *bursts / lcnt1;
1331 cyc = 1;
1332 } else {
1333 lcnt1 = *bursts;
1334 lcnt0 = 0;
1335 cyc = 1;
1338 szlp = _emit_LP(1, buf, 0, 0);
1339 szbrst = _bursts(1, buf, pxs, 1);
1341 lpend.cond = ALWAYS;
1342 lpend.forever = false;
1343 lpend.loop = 0;
1344 lpend.bjump = 0;
1345 szlpend = _emit_LPEND(1, buf, &lpend);
1347 if (lcnt0) {
1348 szlp *= 2;
1349 szlpend *= 2;
1353 * Max bursts that we can unroll due to limit on the
1354 * size of backward jump that can be encoded in DMALPEND
1355 * which is 8-bits and hence 255
1357 cycmax = (255 - (szlp + szlpend)) / szbrst;
1359 cyc = (cycmax < cyc) ? cycmax : cyc;
1361 off = 0;
1363 if (lcnt0) {
1364 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1365 ljmp0 = off;
1368 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1369 ljmp1 = off;
1371 off += _bursts(dry_run, &buf[off], pxs, cyc);
1373 lpend.cond = ALWAYS;
1374 lpend.forever = false;
1375 lpend.loop = 1;
1376 lpend.bjump = off - ljmp1;
1377 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1379 if (lcnt0) {
1380 lpend.cond = ALWAYS;
1381 lpend.forever = false;
1382 lpend.loop = 0;
1383 lpend.bjump = off - ljmp0;
1384 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1387 *bursts = lcnt1 * cyc;
1388 if (lcnt0)
1389 *bursts *= lcnt0;
1391 return off;
1394 static inline int _setup_loops(unsigned dry_run, u8 buf[],
1395 const struct _xfer_spec *pxs)
1397 struct pl330_xfer *x = pxs->x;
1398 u32 ccr = pxs->ccr;
1399 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1400 int off = 0;
1402 while (bursts) {
1403 c = bursts;
1404 off += _loop(dry_run, &buf[off], &c, pxs);
1405 bursts -= c;
1408 return off;
1411 static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1412 const struct _xfer_spec *pxs)
1414 struct pl330_xfer *x = pxs->x;
1415 int off = 0;
1417 /* DMAMOV SAR, x->src_addr */
1418 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1419 /* DMAMOV DAR, x->dst_addr */
1420 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1422 /* Setup Loop(s) */
1423 off += _setup_loops(dry_run, &buf[off], pxs);
1425 return off;
1429 * A req is a sequence of one or more xfer units.
1430 * Returns the number of bytes taken to setup the MC for the req.
1432 static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1433 unsigned index, struct _xfer_spec *pxs)
1435 struct _pl330_req *req = &thrd->req[index];
1436 struct pl330_xfer *x;
1437 u8 *buf = req->mc_cpu;
1438 int off = 0;
1440 PL330_DBGMC_START(req->mc_bus);
1442 /* DMAMOV CCR, ccr */
1443 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1445 x = pxs->r->x;
1446 do {
1447 /* Error if xfer length is not aligned at burst size */
1448 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1449 return -EINVAL;
1451 pxs->x = x;
1452 off += _setup_xfer(dry_run, &buf[off], pxs);
1454 x = x->next;
1455 } while (x);
1457 /* DMASEV peripheral/event */
1458 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1459 /* DMAEND */
1460 off += _emit_END(dry_run, &buf[off]);
1462 return off;
1465 static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1467 u32 ccr = 0;
1469 if (rqc->src_inc)
1470 ccr |= CC_SRCINC;
1472 if (rqc->dst_inc)
1473 ccr |= CC_DSTINC;
1475 /* We set same protection levels for Src and DST for now */
1476 if (rqc->privileged)
1477 ccr |= CC_SRCPRI | CC_DSTPRI;
1478 if (rqc->nonsecure)
1479 ccr |= CC_SRCNS | CC_DSTNS;
1480 if (rqc->insnaccess)
1481 ccr |= CC_SRCIA | CC_DSTIA;
1483 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1484 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1486 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1487 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1489 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1490 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1492 ccr |= (rqc->swap << CC_SWAP_SHFT);
1494 return ccr;
1497 static inline bool _is_valid(u32 ccr)
1499 enum pl330_dstcachectrl dcctl;
1500 enum pl330_srccachectrl scctl;
1502 dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1503 scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1505 if (dcctl == DINVALID1 || dcctl == DINVALID2
1506 || scctl == SINVALID1 || scctl == SINVALID2)
1507 return false;
1508 else
1509 return true;
1513 * Submit a list of xfers after which the client wants notification.
1514 * Client is not notified after each xfer unit, just once after all
1515 * xfer units are done or some error occurs.
1517 static int pl330_submit_req(void *ch_id, struct pl330_req *r)
1519 struct pl330_thread *thrd = ch_id;
1520 struct pl330_dmac *pl330;
1521 struct pl330_info *pi;
1522 struct _xfer_spec xs;
1523 unsigned long flags;
1524 void __iomem *regs;
1525 unsigned idx;
1526 u32 ccr;
1527 int ret = 0;
1529 /* No Req or Unacquired Channel or DMAC */
1530 if (!r || !thrd || thrd->free)
1531 return -EINVAL;
1533 pl330 = thrd->dmac;
1534 pi = pl330->pinfo;
1535 regs = pi->base;
1537 if (pl330->state == DYING
1538 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1539 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1540 __func__, __LINE__);
1541 return -EAGAIN;
1544 /* If request for non-existing peripheral */
1545 if (r->rqtype != MEMTOMEM && r->peri >= pi->pcfg.num_peri) {
1546 dev_info(thrd->dmac->pinfo->dev,
1547 "%s:%d Invalid peripheral(%u)!\n",
1548 __func__, __LINE__, r->peri);
1549 return -EINVAL;
1552 spin_lock_irqsave(&pl330->lock, flags);
1554 if (_queue_full(thrd)) {
1555 ret = -EAGAIN;
1556 goto xfer_exit;
1560 /* Use last settings, if not provided */
1561 if (r->cfg) {
1562 /* Prefer Secure Channel */
1563 if (!_manager_ns(thrd))
1564 r->cfg->nonsecure = 0;
1565 else
1566 r->cfg->nonsecure = 1;
1568 ccr = _prepare_ccr(r->cfg);
1569 } else {
1570 ccr = readl(regs + CC(thrd->id));
1573 /* If this req doesn't have valid xfer settings */
1574 if (!_is_valid(ccr)) {
1575 ret = -EINVAL;
1576 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1577 __func__, __LINE__, ccr);
1578 goto xfer_exit;
1581 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1583 xs.ccr = ccr;
1584 xs.r = r;
1586 /* First dry run to check if req is acceptable */
1587 ret = _setup_req(1, thrd, idx, &xs);
1588 if (ret < 0)
1589 goto xfer_exit;
1591 if (ret > pi->mcbufsz / 2) {
1592 dev_info(thrd->dmac->pinfo->dev,
1593 "%s:%d Trying increasing mcbufsz\n",
1594 __func__, __LINE__);
1595 ret = -ENOMEM;
1596 goto xfer_exit;
1599 /* Hook the request */
1600 thrd->lstenq = idx;
1601 thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs);
1602 thrd->req[idx].r = r;
1604 ret = 0;
1606 xfer_exit:
1607 spin_unlock_irqrestore(&pl330->lock, flags);
1609 return ret;
1612 static void pl330_dotask(unsigned long data)
1614 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1615 struct pl330_info *pi = pl330->pinfo;
1616 unsigned long flags;
1617 int i;
1619 spin_lock_irqsave(&pl330->lock, flags);
1621 /* The DMAC itself gone nuts */
1622 if (pl330->dmac_tbd.reset_dmac) {
1623 pl330->state = DYING;
1624 /* Reset the manager too */
1625 pl330->dmac_tbd.reset_mngr = true;
1626 /* Clear the reset flag */
1627 pl330->dmac_tbd.reset_dmac = false;
1630 if (pl330->dmac_tbd.reset_mngr) {
1631 _stop(pl330->manager);
1632 /* Reset all channels */
1633 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1634 /* Clear the reset flag */
1635 pl330->dmac_tbd.reset_mngr = false;
1638 for (i = 0; i < pi->pcfg.num_chan; i++) {
1640 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1641 struct pl330_thread *thrd = &pl330->channels[i];
1642 void __iomem *regs = pi->base;
1643 enum pl330_op_err err;
1645 _stop(thrd);
1647 if (readl(regs + FSC) & (1 << thrd->id))
1648 err = PL330_ERR_FAIL;
1649 else
1650 err = PL330_ERR_ABORT;
1652 spin_unlock_irqrestore(&pl330->lock, flags);
1654 _callback(thrd->req[1 - thrd->lstenq].r, err);
1655 _callback(thrd->req[thrd->lstenq].r, err);
1657 spin_lock_irqsave(&pl330->lock, flags);
1659 thrd->req[0].r = NULL;
1660 thrd->req[1].r = NULL;
1661 mark_free(thrd, 0);
1662 mark_free(thrd, 1);
1664 /* Clear the reset flag */
1665 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1669 spin_unlock_irqrestore(&pl330->lock, flags);
1671 return;
1674 /* Returns 1 if state was updated, 0 otherwise */
1675 static int pl330_update(const struct pl330_info *pi)
1677 struct pl330_req *rqdone, *tmp;
1678 struct pl330_dmac *pl330;
1679 unsigned long flags;
1680 void __iomem *regs;
1681 u32 val;
1682 int id, ev, ret = 0;
1684 if (!pi || !pi->pl330_data)
1685 return 0;
1687 regs = pi->base;
1688 pl330 = pi->pl330_data;
1690 spin_lock_irqsave(&pl330->lock, flags);
1692 val = readl(regs + FSM) & 0x1;
1693 if (val)
1694 pl330->dmac_tbd.reset_mngr = true;
1695 else
1696 pl330->dmac_tbd.reset_mngr = false;
1698 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1699 pl330->dmac_tbd.reset_chan |= val;
1700 if (val) {
1701 int i = 0;
1702 while (i < pi->pcfg.num_chan) {
1703 if (val & (1 << i)) {
1704 dev_info(pi->dev,
1705 "Reset Channel-%d\t CS-%x FTC-%x\n",
1706 i, readl(regs + CS(i)),
1707 readl(regs + FTC(i)));
1708 _stop(&pl330->channels[i]);
1710 i++;
1714 /* Check which event happened i.e, thread notified */
1715 val = readl(regs + ES);
1716 if (pi->pcfg.num_events < 32
1717 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1718 pl330->dmac_tbd.reset_dmac = true;
1719 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1720 ret = 1;
1721 goto updt_exit;
1724 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1725 if (val & (1 << ev)) { /* Event occurred */
1726 struct pl330_thread *thrd;
1727 u32 inten = readl(regs + INTEN);
1728 int active;
1730 /* Clear the event */
1731 if (inten & (1 << ev))
1732 writel(1 << ev, regs + INTCLR);
1734 ret = 1;
1736 id = pl330->events[ev];
1738 thrd = &pl330->channels[id];
1740 active = thrd->req_running;
1741 if (active == -1) /* Aborted */
1742 continue;
1744 /* Detach the req */
1745 rqdone = thrd->req[active].r;
1746 thrd->req[active].r = NULL;
1748 mark_free(thrd, active);
1750 /* Get going again ASAP */
1751 _start(thrd);
1753 /* For now, just make a list of callbacks to be done */
1754 list_add_tail(&rqdone->rqd, &pl330->req_done);
1758 /* Now that we are in no hurry, do the callbacks */
1759 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1760 list_del(&rqdone->rqd);
1762 spin_unlock_irqrestore(&pl330->lock, flags);
1763 _callback(rqdone, PL330_ERR_NONE);
1764 spin_lock_irqsave(&pl330->lock, flags);
1767 updt_exit:
1768 spin_unlock_irqrestore(&pl330->lock, flags);
1770 if (pl330->dmac_tbd.reset_dmac
1771 || pl330->dmac_tbd.reset_mngr
1772 || pl330->dmac_tbd.reset_chan) {
1773 ret = 1;
1774 tasklet_schedule(&pl330->tasks);
1777 return ret;
1780 static int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op)
1782 struct pl330_thread *thrd = ch_id;
1783 struct pl330_dmac *pl330;
1784 unsigned long flags;
1785 int ret = 0, active;
1787 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1788 return -EINVAL;
1790 pl330 = thrd->dmac;
1791 active = thrd->req_running;
1793 spin_lock_irqsave(&pl330->lock, flags);
1795 switch (op) {
1796 case PL330_OP_FLUSH:
1797 /* Make sure the channel is stopped */
1798 _stop(thrd);
1800 thrd->req[0].r = NULL;
1801 thrd->req[1].r = NULL;
1802 mark_free(thrd, 0);
1803 mark_free(thrd, 1);
1804 break;
1806 case PL330_OP_ABORT:
1807 /* Make sure the channel is stopped */
1808 _stop(thrd);
1810 /* ABORT is only for the active req */
1811 if (active == -1)
1812 break;
1814 thrd->req[active].r = NULL;
1815 mark_free(thrd, active);
1817 /* Start the next */
1818 case PL330_OP_START:
1819 if ((active == -1) && !_start(thrd))
1820 ret = -EIO;
1821 break;
1823 default:
1824 ret = -EINVAL;
1827 spin_unlock_irqrestore(&pl330->lock, flags);
1828 return ret;
1831 /* Reserve an event */
1832 static inline int _alloc_event(struct pl330_thread *thrd)
1834 struct pl330_dmac *pl330 = thrd->dmac;
1835 struct pl330_info *pi = pl330->pinfo;
1836 int ev;
1838 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1839 if (pl330->events[ev] == -1) {
1840 pl330->events[ev] = thrd->id;
1841 return ev;
1844 return -1;
1847 static bool _chan_ns(const struct pl330_info *pi, int i)
1849 return pi->pcfg.irq_ns & (1 << i);
1852 /* Upon success, returns IdentityToken for the
1853 * allocated channel, NULL otherwise.
1855 static void *pl330_request_channel(const struct pl330_info *pi)
1857 struct pl330_thread *thrd = NULL;
1858 struct pl330_dmac *pl330;
1859 unsigned long flags;
1860 int chans, i;
1862 if (!pi || !pi->pl330_data)
1863 return NULL;
1865 pl330 = pi->pl330_data;
1867 if (pl330->state == DYING)
1868 return NULL;
1870 chans = pi->pcfg.num_chan;
1872 spin_lock_irqsave(&pl330->lock, flags);
1874 for (i = 0; i < chans; i++) {
1875 thrd = &pl330->channels[i];
1876 if ((thrd->free) && (!_manager_ns(thrd) ||
1877 _chan_ns(pi, i))) {
1878 thrd->ev = _alloc_event(thrd);
1879 if (thrd->ev >= 0) {
1880 thrd->free = false;
1881 thrd->lstenq = 1;
1882 thrd->req[0].r = NULL;
1883 mark_free(thrd, 0);
1884 thrd->req[1].r = NULL;
1885 mark_free(thrd, 1);
1886 break;
1889 thrd = NULL;
1892 spin_unlock_irqrestore(&pl330->lock, flags);
1894 return thrd;
1897 /* Release an event */
1898 static inline void _free_event(struct pl330_thread *thrd, int ev)
1900 struct pl330_dmac *pl330 = thrd->dmac;
1901 struct pl330_info *pi = pl330->pinfo;
1903 /* If the event is valid and was held by the thread */
1904 if (ev >= 0 && ev < pi->pcfg.num_events
1905 && pl330->events[ev] == thrd->id)
1906 pl330->events[ev] = -1;
1909 static void pl330_release_channel(void *ch_id)
1911 struct pl330_thread *thrd = ch_id;
1912 struct pl330_dmac *pl330;
1913 unsigned long flags;
1915 if (!thrd || thrd->free)
1916 return;
1918 _stop(thrd);
1920 _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1921 _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1923 pl330 = thrd->dmac;
1925 spin_lock_irqsave(&pl330->lock, flags);
1926 _free_event(thrd, thrd->ev);
1927 thrd->free = true;
1928 spin_unlock_irqrestore(&pl330->lock, flags);
1931 /* Initialize the structure for PL330 configuration, that can be used
1932 * by the client driver the make best use of the DMAC
1934 static void read_dmac_config(struct pl330_info *pi)
1936 void __iomem *regs = pi->base;
1937 u32 val;
1939 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1940 val &= CRD_DATA_WIDTH_MASK;
1941 pi->pcfg.data_bus_width = 8 * (1 << val);
1943 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1944 val &= CRD_DATA_BUFF_MASK;
1945 pi->pcfg.data_buf_dep = val + 1;
1947 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1948 val &= CR0_NUM_CHANS_MASK;
1949 val += 1;
1950 pi->pcfg.num_chan = val;
1952 val = readl(regs + CR0);
1953 if (val & CR0_PERIPH_REQ_SET) {
1954 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1955 val += 1;
1956 pi->pcfg.num_peri = val;
1957 pi->pcfg.peri_ns = readl(regs + CR4);
1958 } else {
1959 pi->pcfg.num_peri = 0;
1962 val = readl(regs + CR0);
1963 if (val & CR0_BOOT_MAN_NS)
1964 pi->pcfg.mode |= DMAC_MODE_NS;
1965 else
1966 pi->pcfg.mode &= ~DMAC_MODE_NS;
1968 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1969 val &= CR0_NUM_EVENTS_MASK;
1970 val += 1;
1971 pi->pcfg.num_events = val;
1973 pi->pcfg.irq_ns = readl(regs + CR3);
1976 static inline void _reset_thread(struct pl330_thread *thrd)
1978 struct pl330_dmac *pl330 = thrd->dmac;
1979 struct pl330_info *pi = pl330->pinfo;
1981 thrd->req[0].mc_cpu = pl330->mcode_cpu
1982 + (thrd->id * pi->mcbufsz);
1983 thrd->req[0].mc_bus = pl330->mcode_bus
1984 + (thrd->id * pi->mcbufsz);
1985 thrd->req[0].r = NULL;
1986 mark_free(thrd, 0);
1988 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1989 + pi->mcbufsz / 2;
1990 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1991 + pi->mcbufsz / 2;
1992 thrd->req[1].r = NULL;
1993 mark_free(thrd, 1);
1996 static int dmac_alloc_threads(struct pl330_dmac *pl330)
1998 struct pl330_info *pi = pl330->pinfo;
1999 int chans = pi->pcfg.num_chan;
2000 struct pl330_thread *thrd;
2001 int i;
2003 /* Allocate 1 Manager and 'chans' Channel threads */
2004 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
2005 GFP_KERNEL);
2006 if (!pl330->channels)
2007 return -ENOMEM;
2009 /* Init Channel threads */
2010 for (i = 0; i < chans; i++) {
2011 thrd = &pl330->channels[i];
2012 thrd->id = i;
2013 thrd->dmac = pl330;
2014 _reset_thread(thrd);
2015 thrd->free = true;
2018 /* MANAGER is indexed at the end */
2019 thrd = &pl330->channels[chans];
2020 thrd->id = chans;
2021 thrd->dmac = pl330;
2022 thrd->free = false;
2023 pl330->manager = thrd;
2025 return 0;
2028 static int dmac_alloc_resources(struct pl330_dmac *pl330)
2030 struct pl330_info *pi = pl330->pinfo;
2031 int chans = pi->pcfg.num_chan;
2032 int ret;
2035 * Alloc MicroCode buffer for 'chans' Channel threads.
2036 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
2038 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
2039 chans * pi->mcbufsz,
2040 &pl330->mcode_bus, GFP_KERNEL);
2041 if (!pl330->mcode_cpu) {
2042 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2043 __func__, __LINE__);
2044 return -ENOMEM;
2047 ret = dmac_alloc_threads(pl330);
2048 if (ret) {
2049 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
2050 __func__, __LINE__);
2051 dma_free_coherent(pi->dev,
2052 chans * pi->mcbufsz,
2053 pl330->mcode_cpu, pl330->mcode_bus);
2054 return ret;
2057 return 0;
2060 static int pl330_add(struct pl330_info *pi)
2062 struct pl330_dmac *pl330;
2063 void __iomem *regs;
2064 int i, ret;
2066 if (!pi || !pi->dev)
2067 return -EINVAL;
2069 /* If already added */
2070 if (pi->pl330_data)
2071 return -EINVAL;
2074 * If the SoC can perform reset on the DMAC, then do it
2075 * before reading its configuration.
2077 if (pi->dmac_reset)
2078 pi->dmac_reset(pi);
2080 regs = pi->base;
2082 /* Check if we can handle this DMAC */
2083 if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
2084 dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id);
2085 return -EINVAL;
2088 /* Read the configuration of the DMAC */
2089 read_dmac_config(pi);
2091 if (pi->pcfg.num_events == 0) {
2092 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2093 __func__, __LINE__);
2094 return -EINVAL;
2097 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2098 if (!pl330) {
2099 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2100 __func__, __LINE__);
2101 return -ENOMEM;
2104 /* Assign the info structure and private data */
2105 pl330->pinfo = pi;
2106 pi->pl330_data = pl330;
2108 spin_lock_init(&pl330->lock);
2110 INIT_LIST_HEAD(&pl330->req_done);
2112 /* Use default MC buffer size if not provided */
2113 if (!pi->mcbufsz)
2114 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2116 /* Mark all events as free */
2117 for (i = 0; i < pi->pcfg.num_events; i++)
2118 pl330->events[i] = -1;
2120 /* Allocate resources needed by the DMAC */
2121 ret = dmac_alloc_resources(pl330);
2122 if (ret) {
2123 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2124 kfree(pl330);
2125 return ret;
2128 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2130 pl330->state = INIT;
2132 return 0;
2135 static int dmac_free_threads(struct pl330_dmac *pl330)
2137 struct pl330_info *pi = pl330->pinfo;
2138 int chans = pi->pcfg.num_chan;
2139 struct pl330_thread *thrd;
2140 int i;
2142 /* Release Channel threads */
2143 for (i = 0; i < chans; i++) {
2144 thrd = &pl330->channels[i];
2145 pl330_release_channel((void *)thrd);
2148 /* Free memory */
2149 kfree(pl330->channels);
2151 return 0;
2154 static void dmac_free_resources(struct pl330_dmac *pl330)
2156 struct pl330_info *pi = pl330->pinfo;
2157 int chans = pi->pcfg.num_chan;
2159 dmac_free_threads(pl330);
2161 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2162 pl330->mcode_cpu, pl330->mcode_bus);
2165 static void pl330_del(struct pl330_info *pi)
2167 struct pl330_dmac *pl330;
2169 if (!pi || !pi->pl330_data)
2170 return;
2172 pl330 = pi->pl330_data;
2174 pl330->state = UNINIT;
2176 tasklet_kill(&pl330->tasks);
2178 /* Free DMAC resources */
2179 dmac_free_resources(pl330);
2181 kfree(pl330);
2182 pi->pl330_data = NULL;
2185 /* forward declaration */
2186 static struct amba_driver pl330_driver;
2188 static inline struct dma_pl330_chan *
2189 to_pchan(struct dma_chan *ch)
2191 if (!ch)
2192 return NULL;
2194 return container_of(ch, struct dma_pl330_chan, chan);
2197 static inline struct dma_pl330_desc *
2198 to_desc(struct dma_async_tx_descriptor *tx)
2200 return container_of(tx, struct dma_pl330_desc, txd);
2203 static inline void fill_queue(struct dma_pl330_chan *pch)
2205 struct dma_pl330_desc *desc;
2206 int ret;
2208 list_for_each_entry(desc, &pch->work_list, node) {
2210 /* If already submitted */
2211 if (desc->status == BUSY)
2212 continue;
2214 ret = pl330_submit_req(pch->pl330_chid,
2215 &desc->req);
2216 if (!ret) {
2217 desc->status = BUSY;
2218 } else if (ret == -EAGAIN) {
2219 /* QFull or DMAC Dying */
2220 break;
2221 } else {
2222 /* Unacceptable request */
2223 desc->status = DONE;
2224 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2225 __func__, __LINE__, desc->txd.cookie);
2226 tasklet_schedule(&pch->task);
2231 static void pl330_tasklet(unsigned long data)
2233 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2234 struct dma_pl330_desc *desc, *_dt;
2235 unsigned long flags;
2237 spin_lock_irqsave(&pch->lock, flags);
2239 /* Pick up ripe tomatoes */
2240 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2241 if (desc->status == DONE) {
2242 if (!pch->cyclic)
2243 dma_cookie_complete(&desc->txd);
2244 list_move_tail(&desc->node, &pch->completed_list);
2247 /* Try to submit a req imm. next to the last completed cookie */
2248 fill_queue(pch);
2250 /* Make sure the PL330 Channel thread is active */
2251 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
2253 while (!list_empty(&pch->completed_list)) {
2254 dma_async_tx_callback callback;
2255 void *callback_param;
2257 desc = list_first_entry(&pch->completed_list,
2258 struct dma_pl330_desc, node);
2260 callback = desc->txd.callback;
2261 callback_param = desc->txd.callback_param;
2263 if (pch->cyclic) {
2264 desc->status = PREP;
2265 list_move_tail(&desc->node, &pch->work_list);
2266 } else {
2267 desc->status = FREE;
2268 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2271 if (callback) {
2272 spin_unlock_irqrestore(&pch->lock, flags);
2273 callback(callback_param);
2274 spin_lock_irqsave(&pch->lock, flags);
2277 spin_unlock_irqrestore(&pch->lock, flags);
2280 static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
2282 struct dma_pl330_desc *desc = token;
2283 struct dma_pl330_chan *pch = desc->pchan;
2284 unsigned long flags;
2286 /* If desc aborted */
2287 if (!pch)
2288 return;
2290 spin_lock_irqsave(&pch->lock, flags);
2292 desc->status = DONE;
2294 spin_unlock_irqrestore(&pch->lock, flags);
2296 tasklet_schedule(&pch->task);
2299 static bool pl330_dt_filter(struct dma_chan *chan, void *param)
2301 struct dma_pl330_filter_args *fargs = param;
2303 if (chan->device != &fargs->pdmac->ddma)
2304 return false;
2306 return (chan->chan_id == fargs->chan_id);
2309 bool pl330_filter(struct dma_chan *chan, void *param)
2311 u8 *peri_id;
2313 if (chan->device->dev->driver != &pl330_driver.drv)
2314 return false;
2316 peri_id = chan->private;
2317 return *peri_id == (unsigned)param;
2319 EXPORT_SYMBOL(pl330_filter);
2321 static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2322 struct of_dma *ofdma)
2324 int count = dma_spec->args_count;
2325 struct dma_pl330_dmac *pdmac = ofdma->of_dma_data;
2326 struct dma_pl330_filter_args fargs;
2327 dma_cap_mask_t cap;
2329 if (!pdmac)
2330 return NULL;
2332 if (count != 1)
2333 return NULL;
2335 fargs.pdmac = pdmac;
2336 fargs.chan_id = dma_spec->args[0];
2338 dma_cap_zero(cap);
2339 dma_cap_set(DMA_SLAVE, cap);
2340 dma_cap_set(DMA_CYCLIC, cap);
2342 return dma_request_channel(cap, pl330_dt_filter, &fargs);
2345 static int pl330_alloc_chan_resources(struct dma_chan *chan)
2347 struct dma_pl330_chan *pch = to_pchan(chan);
2348 struct dma_pl330_dmac *pdmac = pch->dmac;
2349 unsigned long flags;
2351 spin_lock_irqsave(&pch->lock, flags);
2353 dma_cookie_init(chan);
2354 pch->cyclic = false;
2356 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
2357 if (!pch->pl330_chid) {
2358 spin_unlock_irqrestore(&pch->lock, flags);
2359 return -ENOMEM;
2362 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2364 spin_unlock_irqrestore(&pch->lock, flags);
2366 return 1;
2369 static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2371 struct dma_pl330_chan *pch = to_pchan(chan);
2372 struct dma_pl330_desc *desc;
2373 unsigned long flags;
2374 struct dma_pl330_dmac *pdmac = pch->dmac;
2375 struct dma_slave_config *slave_config;
2376 LIST_HEAD(list);
2378 switch (cmd) {
2379 case DMA_TERMINATE_ALL:
2380 spin_lock_irqsave(&pch->lock, flags);
2382 /* FLUSH the PL330 Channel thread */
2383 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
2385 /* Mark all desc done */
2386 list_for_each_entry(desc, &pch->work_list , node) {
2387 desc->status = FREE;
2388 dma_cookie_complete(&desc->txd);
2391 list_for_each_entry(desc, &pch->completed_list , node) {
2392 desc->status = FREE;
2393 dma_cookie_complete(&desc->txd);
2396 list_splice_tail_init(&pch->work_list, &pdmac->desc_pool);
2397 list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool);
2398 spin_unlock_irqrestore(&pch->lock, flags);
2399 break;
2400 case DMA_SLAVE_CONFIG:
2401 slave_config = (struct dma_slave_config *)arg;
2403 if (slave_config->direction == DMA_MEM_TO_DEV) {
2404 if (slave_config->dst_addr)
2405 pch->fifo_addr = slave_config->dst_addr;
2406 if (slave_config->dst_addr_width)
2407 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2408 if (slave_config->dst_maxburst)
2409 pch->burst_len = slave_config->dst_maxburst;
2410 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
2411 if (slave_config->src_addr)
2412 pch->fifo_addr = slave_config->src_addr;
2413 if (slave_config->src_addr_width)
2414 pch->burst_sz = __ffs(slave_config->src_addr_width);
2415 if (slave_config->src_maxburst)
2416 pch->burst_len = slave_config->src_maxburst;
2418 break;
2419 default:
2420 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
2421 return -ENXIO;
2424 return 0;
2427 static void pl330_free_chan_resources(struct dma_chan *chan)
2429 struct dma_pl330_chan *pch = to_pchan(chan);
2430 unsigned long flags;
2432 tasklet_kill(&pch->task);
2434 spin_lock_irqsave(&pch->lock, flags);
2436 pl330_release_channel(pch->pl330_chid);
2437 pch->pl330_chid = NULL;
2439 if (pch->cyclic)
2440 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2442 spin_unlock_irqrestore(&pch->lock, flags);
2445 static enum dma_status
2446 pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2447 struct dma_tx_state *txstate)
2449 return dma_cookie_status(chan, cookie, txstate);
2452 static void pl330_issue_pending(struct dma_chan *chan)
2454 pl330_tasklet((unsigned long) to_pchan(chan));
2458 * We returned the last one of the circular list of descriptor(s)
2459 * from prep_xxx, so the argument to submit corresponds to the last
2460 * descriptor of the list.
2462 static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2464 struct dma_pl330_desc *desc, *last = to_desc(tx);
2465 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2466 dma_cookie_t cookie;
2467 unsigned long flags;
2469 spin_lock_irqsave(&pch->lock, flags);
2471 /* Assign cookies to all nodes */
2472 while (!list_empty(&last->node)) {
2473 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
2474 if (pch->cyclic) {
2475 desc->txd.callback = last->txd.callback;
2476 desc->txd.callback_param = last->txd.callback_param;
2479 dma_cookie_assign(&desc->txd);
2481 list_move_tail(&desc->node, &pch->work_list);
2484 cookie = dma_cookie_assign(&last->txd);
2485 list_add_tail(&last->node, &pch->work_list);
2486 spin_unlock_irqrestore(&pch->lock, flags);
2488 return cookie;
2491 static inline void _init_desc(struct dma_pl330_desc *desc)
2493 desc->pchan = NULL;
2494 desc->req.x = &desc->px;
2495 desc->req.token = desc;
2496 desc->rqcfg.swap = SWAP_NO;
2497 desc->rqcfg.privileged = 0;
2498 desc->rqcfg.insnaccess = 0;
2499 desc->rqcfg.scctl = SCCTRL0;
2500 desc->rqcfg.dcctl = DCCTRL0;
2501 desc->req.cfg = &desc->rqcfg;
2502 desc->req.xfer_cb = dma_pl330_rqcb;
2503 desc->txd.tx_submit = pl330_tx_submit;
2505 INIT_LIST_HEAD(&desc->node);
2508 /* Returns the number of descriptors added to the DMAC pool */
2509 static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
2511 struct dma_pl330_desc *desc;
2512 unsigned long flags;
2513 int i;
2515 if (!pdmac)
2516 return 0;
2518 desc = kmalloc(count * sizeof(*desc), flg);
2519 if (!desc)
2520 return 0;
2522 spin_lock_irqsave(&pdmac->pool_lock, flags);
2524 for (i = 0; i < count; i++) {
2525 _init_desc(&desc[i]);
2526 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2529 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2531 return count;
2534 static struct dma_pl330_desc *
2535 pluck_desc(struct dma_pl330_dmac *pdmac)
2537 struct dma_pl330_desc *desc = NULL;
2538 unsigned long flags;
2540 if (!pdmac)
2541 return NULL;
2543 spin_lock_irqsave(&pdmac->pool_lock, flags);
2545 if (!list_empty(&pdmac->desc_pool)) {
2546 desc = list_entry(pdmac->desc_pool.next,
2547 struct dma_pl330_desc, node);
2549 list_del_init(&desc->node);
2551 desc->status = PREP;
2552 desc->txd.callback = NULL;
2555 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2557 return desc;
2560 static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2562 struct dma_pl330_dmac *pdmac = pch->dmac;
2563 u8 *peri_id = pch->chan.private;
2564 struct dma_pl330_desc *desc;
2566 /* Pluck one desc from the pool of DMAC */
2567 desc = pluck_desc(pdmac);
2569 /* If the DMAC pool is empty, alloc new */
2570 if (!desc) {
2571 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2572 return NULL;
2574 /* Try again */
2575 desc = pluck_desc(pdmac);
2576 if (!desc) {
2577 dev_err(pch->dmac->pif.dev,
2578 "%s:%d ALERT!\n", __func__, __LINE__);
2579 return NULL;
2583 /* Initialize the descriptor */
2584 desc->pchan = pch;
2585 desc->txd.cookie = 0;
2586 async_tx_ack(&desc->txd);
2588 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
2589 desc->rqcfg.pcfg = &pch->dmac->pif.pcfg;
2591 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2593 return desc;
2596 static inline void fill_px(struct pl330_xfer *px,
2597 dma_addr_t dst, dma_addr_t src, size_t len)
2599 px->next = NULL;
2600 px->bytes = len;
2601 px->dst_addr = dst;
2602 px->src_addr = src;
2605 static struct dma_pl330_desc *
2606 __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2607 dma_addr_t src, size_t len)
2609 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2611 if (!desc) {
2612 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2613 __func__, __LINE__);
2614 return NULL;
2618 * Ideally we should lookout for reqs bigger than
2619 * those that can be programmed with 256 bytes of
2620 * MC buffer, but considering a req size is seldom
2621 * going to be word-unaligned and more than 200MB,
2622 * we take it easy.
2623 * Also, should the limit is reached we'd rather
2624 * have the platform increase MC buffer size than
2625 * complicating this API driver.
2627 fill_px(&desc->px, dst, src, len);
2629 return desc;
2632 /* Call after fixing burst size */
2633 static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2635 struct dma_pl330_chan *pch = desc->pchan;
2636 struct pl330_info *pi = &pch->dmac->pif;
2637 int burst_len;
2639 burst_len = pi->pcfg.data_bus_width / 8;
2640 burst_len *= pi->pcfg.data_buf_dep;
2641 burst_len >>= desc->rqcfg.brst_size;
2643 /* src/dst_burst_len can't be more than 16 */
2644 if (burst_len > 16)
2645 burst_len = 16;
2647 while (burst_len > 1) {
2648 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2649 break;
2650 burst_len--;
2653 return burst_len;
2656 static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2657 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
2658 size_t period_len, enum dma_transfer_direction direction,
2659 unsigned long flags, void *context)
2661 struct dma_pl330_desc *desc = NULL, *first = NULL;
2662 struct dma_pl330_chan *pch = to_pchan(chan);
2663 struct dma_pl330_dmac *pdmac = pch->dmac;
2664 unsigned int i;
2665 dma_addr_t dst;
2666 dma_addr_t src;
2668 if (len % period_len != 0)
2669 return NULL;
2671 if (!is_slave_direction(direction)) {
2672 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2673 __func__, __LINE__);
2674 return NULL;
2677 for (i = 0; i < len / period_len; i++) {
2678 desc = pl330_get_desc(pch);
2679 if (!desc) {
2680 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2681 __func__, __LINE__);
2683 if (!first)
2684 return NULL;
2686 spin_lock_irqsave(&pdmac->pool_lock, flags);
2688 while (!list_empty(&first->node)) {
2689 desc = list_entry(first->node.next,
2690 struct dma_pl330_desc, node);
2691 list_move_tail(&desc->node, &pdmac->desc_pool);
2694 list_move_tail(&first->node, &pdmac->desc_pool);
2696 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2698 return NULL;
2701 switch (direction) {
2702 case DMA_MEM_TO_DEV:
2703 desc->rqcfg.src_inc = 1;
2704 desc->rqcfg.dst_inc = 0;
2705 desc->req.rqtype = MEMTODEV;
2706 src = dma_addr;
2707 dst = pch->fifo_addr;
2708 break;
2709 case DMA_DEV_TO_MEM:
2710 desc->rqcfg.src_inc = 0;
2711 desc->rqcfg.dst_inc = 1;
2712 desc->req.rqtype = DEVTOMEM;
2713 src = pch->fifo_addr;
2714 dst = dma_addr;
2715 break;
2716 default:
2717 break;
2720 desc->rqcfg.brst_size = pch->burst_sz;
2721 desc->rqcfg.brst_len = 1;
2722 fill_px(&desc->px, dst, src, period_len);
2724 if (!first)
2725 first = desc;
2726 else
2727 list_add_tail(&desc->node, &first->node);
2729 dma_addr += period_len;
2732 if (!desc)
2733 return NULL;
2735 pch->cyclic = true;
2736 desc->txd.flags = flags;
2738 return &desc->txd;
2741 static struct dma_async_tx_descriptor *
2742 pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2743 dma_addr_t src, size_t len, unsigned long flags)
2745 struct dma_pl330_desc *desc;
2746 struct dma_pl330_chan *pch = to_pchan(chan);
2747 struct pl330_info *pi;
2748 int burst;
2750 if (unlikely(!pch || !len))
2751 return NULL;
2753 pi = &pch->dmac->pif;
2755 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2756 if (!desc)
2757 return NULL;
2759 desc->rqcfg.src_inc = 1;
2760 desc->rqcfg.dst_inc = 1;
2761 desc->req.rqtype = MEMTOMEM;
2763 /* Select max possible burst size */
2764 burst = pi->pcfg.data_bus_width / 8;
2766 while (burst > 1) {
2767 if (!(len % burst))
2768 break;
2769 burst /= 2;
2772 desc->rqcfg.brst_size = 0;
2773 while (burst != (1 << desc->rqcfg.brst_size))
2774 desc->rqcfg.brst_size++;
2776 desc->rqcfg.brst_len = get_burst_len(desc, len);
2778 desc->txd.flags = flags;
2780 return &desc->txd;
2783 static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac,
2784 struct dma_pl330_desc *first)
2786 unsigned long flags;
2787 struct dma_pl330_desc *desc;
2789 if (!first)
2790 return;
2792 spin_lock_irqsave(&pdmac->pool_lock, flags);
2794 while (!list_empty(&first->node)) {
2795 desc = list_entry(first->node.next,
2796 struct dma_pl330_desc, node);
2797 list_move_tail(&desc->node, &pdmac->desc_pool);
2800 list_move_tail(&first->node, &pdmac->desc_pool);
2802 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2805 static struct dma_async_tx_descriptor *
2806 pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2807 unsigned int sg_len, enum dma_transfer_direction direction,
2808 unsigned long flg, void *context)
2810 struct dma_pl330_desc *first, *desc = NULL;
2811 struct dma_pl330_chan *pch = to_pchan(chan);
2812 struct scatterlist *sg;
2813 int i;
2814 dma_addr_t addr;
2816 if (unlikely(!pch || !sgl || !sg_len))
2817 return NULL;
2819 addr = pch->fifo_addr;
2821 first = NULL;
2823 for_each_sg(sgl, sg, sg_len, i) {
2825 desc = pl330_get_desc(pch);
2826 if (!desc) {
2827 struct dma_pl330_dmac *pdmac = pch->dmac;
2829 dev_err(pch->dmac->pif.dev,
2830 "%s:%d Unable to fetch desc\n",
2831 __func__, __LINE__);
2832 __pl330_giveback_desc(pdmac, first);
2834 return NULL;
2837 if (!first)
2838 first = desc;
2839 else
2840 list_add_tail(&desc->node, &first->node);
2842 if (direction == DMA_MEM_TO_DEV) {
2843 desc->rqcfg.src_inc = 1;
2844 desc->rqcfg.dst_inc = 0;
2845 desc->req.rqtype = MEMTODEV;
2846 fill_px(&desc->px,
2847 addr, sg_dma_address(sg), sg_dma_len(sg));
2848 } else {
2849 desc->rqcfg.src_inc = 0;
2850 desc->rqcfg.dst_inc = 1;
2851 desc->req.rqtype = DEVTOMEM;
2852 fill_px(&desc->px,
2853 sg_dma_address(sg), addr, sg_dma_len(sg));
2856 desc->rqcfg.brst_size = pch->burst_sz;
2857 desc->rqcfg.brst_len = 1;
2860 /* Return the last desc in the chain */
2861 desc->txd.flags = flg;
2862 return &desc->txd;
2865 static irqreturn_t pl330_irq_handler(int irq, void *data)
2867 if (pl330_update(data))
2868 return IRQ_HANDLED;
2869 else
2870 return IRQ_NONE;
2873 #define PL330_DMA_BUSWIDTHS \
2874 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2875 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2876 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2877 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2878 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2880 static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2881 struct dma_slave_caps *caps)
2883 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2884 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2885 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2886 caps->cmd_pause = false;
2887 caps->cmd_terminate = true;
2889 return 0;
2892 static int
2893 pl330_probe(struct amba_device *adev, const struct amba_id *id)
2895 struct dma_pl330_platdata *pdat;
2896 struct dma_pl330_dmac *pdmac;
2897 struct dma_pl330_chan *pch, *_p;
2898 struct pl330_info *pi;
2899 struct dma_device *pd;
2900 struct resource *res;
2901 int i, ret, irq;
2902 int num_chan;
2904 pdat = dev_get_platdata(&adev->dev);
2906 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2907 if (ret)
2908 return ret;
2910 /* Allocate a new DMAC and its Channels */
2911 pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL);
2912 if (!pdmac) {
2913 dev_err(&adev->dev, "unable to allocate mem\n");
2914 return -ENOMEM;
2917 pi = &pdmac->pif;
2918 pi->dev = &adev->dev;
2919 pi->pl330_data = NULL;
2920 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
2922 res = &adev->res;
2923 pi->base = devm_ioremap_resource(&adev->dev, res);
2924 if (IS_ERR(pi->base))
2925 return PTR_ERR(pi->base);
2927 amba_set_drvdata(adev, pdmac);
2929 irq = adev->irq[0];
2930 ret = request_irq(irq, pl330_irq_handler, 0,
2931 dev_name(&adev->dev), pi);
2932 if (ret)
2933 return ret;
2935 pi->pcfg.periph_id = adev->periphid;
2936 ret = pl330_add(pi);
2937 if (ret)
2938 goto probe_err1;
2940 INIT_LIST_HEAD(&pdmac->desc_pool);
2941 spin_lock_init(&pdmac->pool_lock);
2943 /* Create a descriptor pool of default size */
2944 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2945 dev_warn(&adev->dev, "unable to allocate desc\n");
2947 pd = &pdmac->ddma;
2948 INIT_LIST_HEAD(&pd->channels);
2950 /* Initialize channel parameters */
2951 if (pdat)
2952 num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan);
2953 else
2954 num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan);
2956 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
2957 if (!pdmac->peripherals) {
2958 ret = -ENOMEM;
2959 dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n");
2960 goto probe_err2;
2963 for (i = 0; i < num_chan; i++) {
2964 pch = &pdmac->peripherals[i];
2965 if (!adev->dev.of_node)
2966 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2967 else
2968 pch->chan.private = adev->dev.of_node;
2970 INIT_LIST_HEAD(&pch->work_list);
2971 INIT_LIST_HEAD(&pch->completed_list);
2972 spin_lock_init(&pch->lock);
2973 pch->pl330_chid = NULL;
2974 pch->chan.device = pd;
2975 pch->dmac = pdmac;
2977 /* Add the channel to the DMAC list */
2978 list_add_tail(&pch->chan.device_node, &pd->channels);
2981 pd->dev = &adev->dev;
2982 if (pdat) {
2983 pd->cap_mask = pdat->cap_mask;
2984 } else {
2985 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
2986 if (pi->pcfg.num_peri) {
2987 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2988 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
2989 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
2993 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2994 pd->device_free_chan_resources = pl330_free_chan_resources;
2995 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
2996 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
2997 pd->device_tx_status = pl330_tx_status;
2998 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2999 pd->device_control = pl330_control;
3000 pd->device_issue_pending = pl330_issue_pending;
3001 pd->device_slave_caps = pl330_dma_device_slave_caps;
3003 ret = dma_async_device_register(pd);
3004 if (ret) {
3005 dev_err(&adev->dev, "unable to register DMAC\n");
3006 goto probe_err3;
3009 if (adev->dev.of_node) {
3010 ret = of_dma_controller_register(adev->dev.of_node,
3011 of_dma_pl330_xlate, pdmac);
3012 if (ret) {
3013 dev_err(&adev->dev,
3014 "unable to register DMA to the generic DT DMA helpers\n");
3018 * This is the limit for transfers with a buswidth of 1, larger
3019 * buswidths will have larger limits.
3021 ret = dma_set_max_seg_size(&adev->dev, 1900800);
3022 if (ret)
3023 dev_err(&adev->dev, "unable to set the seg size\n");
3026 dev_info(&adev->dev,
3027 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
3028 dev_info(&adev->dev,
3029 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
3030 pi->pcfg.data_buf_dep,
3031 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
3032 pi->pcfg.num_peri, pi->pcfg.num_events);
3034 return 0;
3035 probe_err3:
3036 amba_set_drvdata(adev, NULL);
3038 /* Idle the DMAC */
3039 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3040 chan.device_node) {
3042 /* Remove the channel */
3043 list_del(&pch->chan.device_node);
3045 /* Flush the channel */
3046 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3047 pl330_free_chan_resources(&pch->chan);
3049 probe_err2:
3050 pl330_del(pi);
3051 probe_err1:
3052 free_irq(irq, pi);
3054 return ret;
3057 static int pl330_remove(struct amba_device *adev)
3059 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
3060 struct dma_pl330_chan *pch, *_p;
3061 struct pl330_info *pi;
3062 int irq;
3064 if (!pdmac)
3065 return 0;
3067 if (adev->dev.of_node)
3068 of_dma_controller_free(adev->dev.of_node);
3070 dma_async_device_unregister(&pdmac->ddma);
3071 amba_set_drvdata(adev, NULL);
3073 /* Idle the DMAC */
3074 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3075 chan.device_node) {
3077 /* Remove the channel */
3078 list_del(&pch->chan.device_node);
3080 /* Flush the channel */
3081 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3082 pl330_free_chan_resources(&pch->chan);
3085 pi = &pdmac->pif;
3087 pl330_del(pi);
3089 irq = adev->irq[0];
3090 free_irq(irq, pi);
3092 return 0;
3095 static struct amba_id pl330_ids[] = {
3097 .id = 0x00041330,
3098 .mask = 0x000fffff,
3100 { 0, 0 },
3103 MODULE_DEVICE_TABLE(amba, pl330_ids);
3105 static struct amba_driver pl330_driver = {
3106 .drv = {
3107 .owner = THIS_MODULE,
3108 .name = "dma-pl330",
3110 .id_table = pl330_ids,
3111 .probe = pl330_probe,
3112 .remove = pl330_remove,
3115 module_amba_driver(pl330_driver);
3117 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3118 MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3119 MODULE_LICENSE("GPL");