blob: 2e351f40fc49617bfe4c47588b8776d24122c82e [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>
24#include <linux/interrupt.h>
25#include <linux/amba/bus.h>
26#include <linux/amba/pl330.h>
Boojin Kima2f52032011-09-02 09:44:29 +090027#include <linux/pm_runtime.h>
Boojin Kim1b9bb712011-09-02 09:44:30 +090028#include <linux/scatterlist.h>
Thomas Abraham93ed5542011-10-24 11:43:31 +020029#include <linux/of.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070030
Boojin Kimb7d861d2011-12-26 18:49:52 +090031#define PL330_MAX_CHAN 8
32#define PL330_MAX_IRQS 32
33#define PL330_MAX_PERI 32
34
35enum pl330_srccachectrl {
36 SCCTRL0, /* Noncacheable and nonbufferable */
37 SCCTRL1, /* Bufferable only */
38 SCCTRL2, /* Cacheable, but do not allocate */
39 SCCTRL3, /* Cacheable and bufferable, but do not allocate */
40 SINVALID1,
41 SINVALID2,
42 SCCTRL6, /* Cacheable write-through, allocate on reads only */
43 SCCTRL7, /* Cacheable write-back, allocate on reads only */
44};
45
46enum pl330_dstcachectrl {
47 DCCTRL0, /* Noncacheable and nonbufferable */
48 DCCTRL1, /* Bufferable only */
49 DCCTRL2, /* Cacheable, but do not allocate */
50 DCCTRL3, /* Cacheable and bufferable, but do not allocate */
51 DINVALID1 = 8,
52 DINVALID2,
53 DCCTRL6, /* Cacheable write-through, allocate on writes only */
54 DCCTRL7, /* Cacheable write-back, allocate on writes only */
55};
56
57enum pl330_byteswap {
58 SWAP_NO,
59 SWAP_2,
60 SWAP_4,
61 SWAP_8,
62 SWAP_16,
63};
64
65enum pl330_reqtype {
66 MEMTOMEM,
67 MEMTODEV,
68 DEVTOMEM,
69 DEVTODEV,
70};
71
72/* Register and Bit field Definitions */
73#define DS 0x0
74#define DS_ST_STOP 0x0
75#define DS_ST_EXEC 0x1
76#define DS_ST_CMISS 0x2
77#define DS_ST_UPDTPC 0x3
78#define DS_ST_WFE 0x4
79#define DS_ST_ATBRR 0x5
80#define DS_ST_QBUSY 0x6
81#define DS_ST_WFP 0x7
82#define DS_ST_KILL 0x8
83#define DS_ST_CMPLT 0x9
84#define DS_ST_FLTCMP 0xe
85#define DS_ST_FAULT 0xf
86
87#define DPC 0x4
88#define INTEN 0x20
89#define ES 0x24
90#define INTSTATUS 0x28
91#define INTCLR 0x2c
92#define FSM 0x30
93#define FSC 0x34
94#define FTM 0x38
95
96#define _FTC 0x40
97#define FTC(n) (_FTC + (n)*0x4)
98
99#define _CS 0x100
100#define CS(n) (_CS + (n)*0x8)
101#define CS_CNS (1 << 21)
102
103#define _CPC 0x104
104#define CPC(n) (_CPC + (n)*0x8)
105
106#define _SA 0x400
107#define SA(n) (_SA + (n)*0x20)
108
109#define _DA 0x404
110#define DA(n) (_DA + (n)*0x20)
111
112#define _CC 0x408
113#define CC(n) (_CC + (n)*0x20)
114
115#define CC_SRCINC (1 << 0)
116#define CC_DSTINC (1 << 14)
117#define CC_SRCPRI (1 << 8)
118#define CC_DSTPRI (1 << 22)
119#define CC_SRCNS (1 << 9)
120#define CC_DSTNS (1 << 23)
121#define CC_SRCIA (1 << 10)
122#define CC_DSTIA (1 << 24)
123#define CC_SRCBRSTLEN_SHFT 4
124#define CC_DSTBRSTLEN_SHFT 18
125#define CC_SRCBRSTSIZE_SHFT 1
126#define CC_DSTBRSTSIZE_SHFT 15
127#define CC_SRCCCTRL_SHFT 11
128#define CC_SRCCCTRL_MASK 0x7
129#define CC_DSTCCTRL_SHFT 25
130#define CC_DRCCCTRL_MASK 0x7
131#define CC_SWAP_SHFT 28
132
133#define _LC0 0x40c
134#define LC0(n) (_LC0 + (n)*0x20)
135
136#define _LC1 0x410
137#define LC1(n) (_LC1 + (n)*0x20)
138
139#define DBGSTATUS 0xd00
140#define DBG_BUSY (1 << 0)
141
142#define DBGCMD 0xd04
143#define DBGINST0 0xd08
144#define DBGINST1 0xd0c
145
146#define CR0 0xe00
147#define CR1 0xe04
148#define CR2 0xe08
149#define CR3 0xe0c
150#define CR4 0xe10
151#define CRD 0xe14
152
153#define PERIPH_ID 0xfe0
154#define PCELL_ID 0xff0
155
156#define CR0_PERIPH_REQ_SET (1 << 0)
157#define CR0_BOOT_EN_SET (1 << 1)
158#define CR0_BOOT_MAN_NS (1 << 2)
159#define CR0_NUM_CHANS_SHIFT 4
160#define CR0_NUM_CHANS_MASK 0x7
161#define CR0_NUM_PERIPH_SHIFT 12
162#define CR0_NUM_PERIPH_MASK 0x1f
163#define CR0_NUM_EVENTS_SHIFT 17
164#define CR0_NUM_EVENTS_MASK 0x1f
165
166#define CR1_ICACHE_LEN_SHIFT 0
167#define CR1_ICACHE_LEN_MASK 0x7
168#define CR1_NUM_ICACHELINES_SHIFT 4
169#define CR1_NUM_ICACHELINES_MASK 0xf
170
171#define CRD_DATA_WIDTH_SHIFT 0
172#define CRD_DATA_WIDTH_MASK 0x7
173#define CRD_WR_CAP_SHIFT 4
174#define CRD_WR_CAP_MASK 0x7
175#define CRD_WR_Q_DEP_SHIFT 8
176#define CRD_WR_Q_DEP_MASK 0xf
177#define CRD_RD_CAP_SHIFT 12
178#define CRD_RD_CAP_MASK 0x7
179#define CRD_RD_Q_DEP_SHIFT 16
180#define CRD_RD_Q_DEP_MASK 0xf
181#define CRD_DATA_BUFF_SHIFT 20
182#define CRD_DATA_BUFF_MASK 0x3ff
183
184#define PART 0x330
185#define DESIGNER 0x41
186#define REVISION 0x0
187#define INTEG_CFG 0x0
188#define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
189
190#define PCELL_ID_VAL 0xb105f00d
191
192#define PL330_STATE_STOPPED (1 << 0)
193#define PL330_STATE_EXECUTING (1 << 1)
194#define PL330_STATE_WFE (1 << 2)
195#define PL330_STATE_FAULTING (1 << 3)
196#define PL330_STATE_COMPLETING (1 << 4)
197#define PL330_STATE_WFP (1 << 5)
198#define PL330_STATE_KILLING (1 << 6)
199#define PL330_STATE_FAULT_COMPLETING (1 << 7)
200#define PL330_STATE_CACHEMISS (1 << 8)
201#define PL330_STATE_UPDTPC (1 << 9)
202#define PL330_STATE_ATBARRIER (1 << 10)
203#define PL330_STATE_QUEUEBUSY (1 << 11)
204#define PL330_STATE_INVALID (1 << 15)
205
206#define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
207 | PL330_STATE_WFE | PL330_STATE_FAULTING)
208
209#define CMD_DMAADDH 0x54
210#define CMD_DMAEND 0x00
211#define CMD_DMAFLUSHP 0x35
212#define CMD_DMAGO 0xa0
213#define CMD_DMALD 0x04
214#define CMD_DMALDP 0x25
215#define CMD_DMALP 0x20
216#define CMD_DMALPEND 0x28
217#define CMD_DMAKILL 0x01
218#define CMD_DMAMOV 0xbc
219#define CMD_DMANOP 0x18
220#define CMD_DMARMB 0x12
221#define CMD_DMASEV 0x34
222#define CMD_DMAST 0x08
223#define CMD_DMASTP 0x29
224#define CMD_DMASTZ 0x0c
225#define CMD_DMAWFE 0x36
226#define CMD_DMAWFP 0x30
227#define CMD_DMAWMB 0x13
228
229#define SZ_DMAADDH 3
230#define SZ_DMAEND 1
231#define SZ_DMAFLUSHP 2
232#define SZ_DMALD 1
233#define SZ_DMALDP 2
234#define SZ_DMALP 2
235#define SZ_DMALPEND 2
236#define SZ_DMAKILL 1
237#define SZ_DMAMOV 6
238#define SZ_DMANOP 1
239#define SZ_DMARMB 1
240#define SZ_DMASEV 2
241#define SZ_DMAST 1
242#define SZ_DMASTP 2
243#define SZ_DMASTZ 1
244#define SZ_DMAWFE 2
245#define SZ_DMAWFP 2
246#define SZ_DMAWMB 1
247#define SZ_DMAGO 6
248
249#define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
250#define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
251
252#define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
253#define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
254
255/*
256 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
257 * at 1byte/burst for P<->M and M<->M respectively.
258 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
259 * should be enough for P<->M and M<->M respectively.
260 */
261#define MCODE_BUFF_PER_REQ 256
262
263/* If the _pl330_req is available to the client */
264#define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
265
266/* Use this _only_ to wait on transient states */
267#define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
268
269#ifdef PL330_DEBUG_MCGEN
270static unsigned cmd_line;
271#define PL330_DBGCMD_DUMP(off, x...) do { \
272 printk("%x:", cmd_line); \
273 printk(x); \
274 cmd_line += off; \
275 } while (0)
276#define PL330_DBGMC_START(addr) (cmd_line = addr)
277#else
278#define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
279#define PL330_DBGMC_START(addr) do {} while (0)
280#endif
281
282/* The number of default descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700283#define NR_DEFAULT_DESC 16
284
Boojin Kimb7d861d2011-12-26 18:49:52 +0900285/* Populated by the PL330 core driver for DMA API driver's info */
286struct pl330_config {
287 u32 periph_id;
288 u32 pcell_id;
289#define DMAC_MODE_NS (1 << 0)
290 unsigned int mode;
291 unsigned int data_bus_width:10; /* In number of bits */
292 unsigned int data_buf_dep:10;
293 unsigned int num_chan:4;
294 unsigned int num_peri:6;
295 u32 peri_ns;
296 unsigned int num_events:6;
297 u32 irq_ns;
298};
299
300/* Handle to the DMAC provided to the PL330 core */
301struct pl330_info {
302 /* Owning device */
303 struct device *dev;
304 /* Size of MicroCode buffers for each channel. */
305 unsigned mcbufsz;
306 /* ioremap'ed address of PL330 registers. */
307 void __iomem *base;
308 /* Client can freely use it. */
309 void *client_data;
310 /* PL330 core data, Client must not touch it. */
311 void *pl330_data;
312 /* Populated by the PL330 core driver during pl330_add */
313 struct pl330_config pcfg;
314 /*
315 * If the DMAC has some reset mechanism, then the
316 * client may want to provide pointer to the method.
317 */
318 void (*dmac_reset)(struct pl330_info *pi);
319};
320
321/**
322 * Request Configuration.
323 * The PL330 core does not modify this and uses the last
324 * working configuration if the request doesn't provide any.
325 *
326 * The Client may want to provide this info only for the
327 * first request and a request with new settings.
328 */
329struct pl330_reqcfg {
330 /* Address Incrementing */
331 unsigned dst_inc:1;
332 unsigned src_inc:1;
333
334 /*
335 * For now, the SRC & DST protection levels
336 * and burst size/length are assumed same.
337 */
338 bool nonsecure;
339 bool privileged;
340 bool insnaccess;
341 unsigned brst_len:5;
342 unsigned brst_size:3; /* in power of 2 */
343
344 enum pl330_dstcachectrl dcctl;
345 enum pl330_srccachectrl scctl;
346 enum pl330_byteswap swap;
347};
348
349/*
350 * One cycle of DMAC operation.
351 * There may be more than one xfer in a request.
352 */
353struct pl330_xfer {
354 u32 src_addr;
355 u32 dst_addr;
356 /* Size to xfer */
357 u32 bytes;
358 /*
359 * Pointer to next xfer in the list.
360 * The last xfer in the req must point to NULL.
361 */
362 struct pl330_xfer *next;
363};
364
365/* The xfer callbacks are made with one of these arguments. */
366enum pl330_op_err {
367 /* The all xfers in the request were success. */
368 PL330_ERR_NONE,
369 /* If req aborted due to global error. */
370 PL330_ERR_ABORT,
371 /* If req failed due to problem with Channel. */
372 PL330_ERR_FAIL,
373};
374
375/* A request defining Scatter-Gather List ending with NULL xfer. */
376struct pl330_req {
377 enum pl330_reqtype rqtype;
378 /* Index of peripheral for the xfer. */
379 unsigned peri:5;
380 /* Unique token for this xfer, set by the client. */
381 void *token;
382 /* Callback to be called after xfer. */
383 void (*xfer_cb)(void *token, enum pl330_op_err err);
384 /* If NULL, req will be done at last set parameters. */
385 struct pl330_reqcfg *cfg;
386 /* Pointer to first xfer in the request. */
387 struct pl330_xfer *x;
388};
389
390/*
391 * To know the status of the channel and DMAC, the client
392 * provides a pointer to this structure. The PL330 core
393 * fills it with current information.
394 */
395struct pl330_chanstatus {
396 /*
397 * If the DMAC engine halted due to some error,
398 * the client should remove-add DMAC.
399 */
400 bool dmac_halted;
401 /*
402 * If channel is halted due to some error,
403 * the client should ABORT/FLUSH and START the channel.
404 */
405 bool faulting;
406 /* Location of last load */
407 u32 src_addr;
408 /* Location of last store */
409 u32 dst_addr;
410 /*
411 * Pointer to the currently active req, NULL if channel is
412 * inactive, even though the requests may be present.
413 */
414 struct pl330_req *top_req;
415 /* Pointer to req waiting second in the queue if any. */
416 struct pl330_req *wait_req;
417};
418
419enum pl330_chan_op {
420 /* Start the channel */
421 PL330_OP_START,
422 /* Abort the active xfer */
423 PL330_OP_ABORT,
424 /* Stop xfer and flush queue */
425 PL330_OP_FLUSH,
426};
427
428struct _xfer_spec {
429 u32 ccr;
430 struct pl330_req *r;
431 struct pl330_xfer *x;
432};
433
434enum dmamov_dst {
435 SAR = 0,
436 CCR,
437 DAR,
438};
439
440enum pl330_dst {
441 SRC = 0,
442 DST,
443};
444
445enum pl330_cond {
446 SINGLE,
447 BURST,
448 ALWAYS,
449};
450
451struct _pl330_req {
452 u32 mc_bus;
453 void *mc_cpu;
454 /* Number of bytes taken to setup MC for the req */
455 u32 mc_len;
456 struct pl330_req *r;
457 /* Hook to attach to DMAC's list of reqs with due callback */
458 struct list_head rqd;
459};
460
461/* ToBeDone for tasklet */
462struct _pl330_tbd {
463 bool reset_dmac;
464 bool reset_mngr;
465 u8 reset_chan;
466};
467
468/* A DMAC Thread */
469struct pl330_thread {
470 u8 id;
471 int ev;
472 /* If the channel is not yet acquired by any client */
473 bool free;
474 /* Parent DMAC */
475 struct pl330_dmac *dmac;
476 /* Only two at a time */
477 struct _pl330_req req[2];
478 /* Index of the last enqueued request */
479 unsigned lstenq;
480 /* Index of the last submitted request or -1 if the DMA is stopped */
481 int req_running;
482};
483
484enum pl330_dmac_state {
485 UNINIT,
486 INIT,
487 DYING,
488};
489
490/* A DMAC */
491struct pl330_dmac {
492 spinlock_t lock;
493 /* Holds list of reqs with due callbacks */
494 struct list_head req_done;
495 /* Pointer to platform specific stuff */
496 struct pl330_info *pinfo;
497 /* Maximum possible events/irqs */
498 int events[32];
499 /* BUS address of MicroCode buffer */
500 u32 mcode_bus;
501 /* CPU address of MicroCode buffer */
502 void *mcode_cpu;
503 /* List of all Channel threads */
504 struct pl330_thread *channels;
505 /* Pointer to the MANAGER thread */
506 struct pl330_thread *manager;
507 /* To handle bad news in interrupt */
508 struct tasklet_struct tasks;
509 struct _pl330_tbd dmac_tbd;
510 /* State of DMAC operation */
511 enum pl330_dmac_state state;
512};
513
Jassi Brarb3040e42010-05-23 20:28:19 -0700514enum desc_status {
515 /* In the DMAC pool */
516 FREE,
517 /*
518 * Allocted to some channel during prep_xxx
519 * Also may be sitting on the work_list.
520 */
521 PREP,
522 /*
523 * Sitting on the work_list and already submitted
524 * to the PL330 core. Not more than two descriptors
525 * of a channel can be BUSY at any time.
526 */
527 BUSY,
528 /*
529 * Sitting on the channel work_list but xfer done
530 * by PL330 core
531 */
532 DONE,
533};
534
535struct dma_pl330_chan {
536 /* Schedule desc completion */
537 struct tasklet_struct task;
538
539 /* DMA-Engine Channel */
540 struct dma_chan chan;
541
542 /* Last completed cookie */
543 dma_cookie_t completed;
544
545 /* List of to be xfered descriptors */
546 struct list_head work_list;
547
548 /* Pointer to the DMAC that manages this channel,
549 * NULL if the channel is available to be acquired.
550 * As the parent, this DMAC also provides descriptors
551 * to the channel.
552 */
553 struct dma_pl330_dmac *dmac;
554
555 /* To protect channel manipulation */
556 spinlock_t lock;
557
558 /* Token of a hardware channel thread of PL330 DMAC
559 * NULL if the channel is available to be acquired.
560 */
561 void *pl330_chid;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900562
563 /* For D-to-M and M-to-D channels */
564 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900565 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900566 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900567
568 /* for cyclic capability */
569 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700570};
571
572struct dma_pl330_dmac {
573 struct pl330_info pif;
574
575 /* DMA-Engine Device */
576 struct dma_device ddma;
577
578 /* Pool of descriptors available for the DMAC's channels */
579 struct list_head desc_pool;
580 /* To protect desc_pool manipulation */
581 spinlock_t pool_lock;
582
583 /* Peripheral channels connected to this DMAC */
Rob Herring4e0e6102011-07-25 16:05:04 -0500584 struct dma_pl330_chan *peripherals; /* keep at end */
Boojin Kima2f52032011-09-02 09:44:29 +0900585
586 struct clk *clk;
Jassi Brarb3040e42010-05-23 20:28:19 -0700587};
588
589struct dma_pl330_desc {
590 /* To attach to a queue as child */
591 struct list_head node;
592
593 /* Descriptor for the DMA Engine API */
594 struct dma_async_tx_descriptor txd;
595
596 /* Xfer for PL330 core */
597 struct pl330_xfer px;
598
599 struct pl330_reqcfg rqcfg;
600 struct pl330_req req;
601
602 enum desc_status status;
603
604 /* The channel which currently holds this desc */
605 struct dma_pl330_chan *pchan;
606};
607
Boojin Kimb7d861d2011-12-26 18:49:52 +0900608static inline void _callback(struct pl330_req *r, enum pl330_op_err err)
609{
610 if (r && r->xfer_cb)
611 r->xfer_cb(r->token, err);
612}
613
614static inline bool _queue_empty(struct pl330_thread *thrd)
615{
616 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
617 ? true : false;
618}
619
620static inline bool _queue_full(struct pl330_thread *thrd)
621{
622 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
623 ? false : true;
624}
625
626static inline bool is_manager(struct pl330_thread *thrd)
627{
628 struct pl330_dmac *pl330 = thrd->dmac;
629
630 /* MANAGER is indexed at the end */
631 if (thrd->id == pl330->pinfo->pcfg.num_chan)
632 return true;
633 else
634 return false;
635}
636
637/* If manager of the thread is in Non-Secure mode */
638static inline bool _manager_ns(struct pl330_thread *thrd)
639{
640 struct pl330_dmac *pl330 = thrd->dmac;
641
642 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
643}
644
645static inline u32 get_id(struct pl330_info *pi, u32 off)
646{
647 void __iomem *regs = pi->base;
648 u32 id = 0;
649
650 id |= (readb(regs + off + 0x0) << 0);
651 id |= (readb(regs + off + 0x4) << 8);
652 id |= (readb(regs + off + 0x8) << 16);
653 id |= (readb(regs + off + 0xc) << 24);
654
655 return id;
656}
657
658static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
659 enum pl330_dst da, u16 val)
660{
661 if (dry_run)
662 return SZ_DMAADDH;
663
664 buf[0] = CMD_DMAADDH;
665 buf[0] |= (da << 1);
666 *((u16 *)&buf[1]) = val;
667
668 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
669 da == 1 ? "DA" : "SA", val);
670
671 return SZ_DMAADDH;
672}
673
674static inline u32 _emit_END(unsigned dry_run, u8 buf[])
675{
676 if (dry_run)
677 return SZ_DMAEND;
678
679 buf[0] = CMD_DMAEND;
680
681 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
682
683 return SZ_DMAEND;
684}
685
686static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
687{
688 if (dry_run)
689 return SZ_DMAFLUSHP;
690
691 buf[0] = CMD_DMAFLUSHP;
692
693 peri &= 0x1f;
694 peri <<= 3;
695 buf[1] = peri;
696
697 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
698
699 return SZ_DMAFLUSHP;
700}
701
702static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
703{
704 if (dry_run)
705 return SZ_DMALD;
706
707 buf[0] = CMD_DMALD;
708
709 if (cond == SINGLE)
710 buf[0] |= (0 << 1) | (1 << 0);
711 else if (cond == BURST)
712 buf[0] |= (1 << 1) | (1 << 0);
713
714 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
715 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
716
717 return SZ_DMALD;
718}
719
720static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
721 enum pl330_cond cond, u8 peri)
722{
723 if (dry_run)
724 return SZ_DMALDP;
725
726 buf[0] = CMD_DMALDP;
727
728 if (cond == BURST)
729 buf[0] |= (1 << 1);
730
731 peri &= 0x1f;
732 peri <<= 3;
733 buf[1] = peri;
734
735 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
736 cond == SINGLE ? 'S' : 'B', peri >> 3);
737
738 return SZ_DMALDP;
739}
740
741static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
742 unsigned loop, u8 cnt)
743{
744 if (dry_run)
745 return SZ_DMALP;
746
747 buf[0] = CMD_DMALP;
748
749 if (loop)
750 buf[0] |= (1 << 1);
751
752 cnt--; /* DMAC increments by 1 internally */
753 buf[1] = cnt;
754
755 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
756
757 return SZ_DMALP;
758}
759
760struct _arg_LPEND {
761 enum pl330_cond cond;
762 bool forever;
763 unsigned loop;
764 u8 bjump;
765};
766
767static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
768 const struct _arg_LPEND *arg)
769{
770 enum pl330_cond cond = arg->cond;
771 bool forever = arg->forever;
772 unsigned loop = arg->loop;
773 u8 bjump = arg->bjump;
774
775 if (dry_run)
776 return SZ_DMALPEND;
777
778 buf[0] = CMD_DMALPEND;
779
780 if (loop)
781 buf[0] |= (1 << 2);
782
783 if (!forever)
784 buf[0] |= (1 << 4);
785
786 if (cond == SINGLE)
787 buf[0] |= (0 << 1) | (1 << 0);
788 else if (cond == BURST)
789 buf[0] |= (1 << 1) | (1 << 0);
790
791 buf[1] = bjump;
792
793 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
794 forever ? "FE" : "END",
795 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
796 loop ? '1' : '0',
797 bjump);
798
799 return SZ_DMALPEND;
800}
801
802static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
803{
804 if (dry_run)
805 return SZ_DMAKILL;
806
807 buf[0] = CMD_DMAKILL;
808
809 return SZ_DMAKILL;
810}
811
812static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
813 enum dmamov_dst dst, u32 val)
814{
815 if (dry_run)
816 return SZ_DMAMOV;
817
818 buf[0] = CMD_DMAMOV;
819 buf[1] = dst;
820 *((u32 *)&buf[2]) = val;
821
822 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
823 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
824
825 return SZ_DMAMOV;
826}
827
828static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
829{
830 if (dry_run)
831 return SZ_DMANOP;
832
833 buf[0] = CMD_DMANOP;
834
835 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
836
837 return SZ_DMANOP;
838}
839
840static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
841{
842 if (dry_run)
843 return SZ_DMARMB;
844
845 buf[0] = CMD_DMARMB;
846
847 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
848
849 return SZ_DMARMB;
850}
851
852static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
853{
854 if (dry_run)
855 return SZ_DMASEV;
856
857 buf[0] = CMD_DMASEV;
858
859 ev &= 0x1f;
860 ev <<= 3;
861 buf[1] = ev;
862
863 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
864
865 return SZ_DMASEV;
866}
867
868static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
869{
870 if (dry_run)
871 return SZ_DMAST;
872
873 buf[0] = CMD_DMAST;
874
875 if (cond == SINGLE)
876 buf[0] |= (0 << 1) | (1 << 0);
877 else if (cond == BURST)
878 buf[0] |= (1 << 1) | (1 << 0);
879
880 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
881 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
882
883 return SZ_DMAST;
884}
885
886static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
887 enum pl330_cond cond, u8 peri)
888{
889 if (dry_run)
890 return SZ_DMASTP;
891
892 buf[0] = CMD_DMASTP;
893
894 if (cond == BURST)
895 buf[0] |= (1 << 1);
896
897 peri &= 0x1f;
898 peri <<= 3;
899 buf[1] = peri;
900
901 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
902 cond == SINGLE ? 'S' : 'B', peri >> 3);
903
904 return SZ_DMASTP;
905}
906
907static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
908{
909 if (dry_run)
910 return SZ_DMASTZ;
911
912 buf[0] = CMD_DMASTZ;
913
914 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
915
916 return SZ_DMASTZ;
917}
918
919static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
920 unsigned invalidate)
921{
922 if (dry_run)
923 return SZ_DMAWFE;
924
925 buf[0] = CMD_DMAWFE;
926
927 ev &= 0x1f;
928 ev <<= 3;
929 buf[1] = ev;
930
931 if (invalidate)
932 buf[1] |= (1 << 1);
933
934 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
935 ev >> 3, invalidate ? ", I" : "");
936
937 return SZ_DMAWFE;
938}
939
940static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
941 enum pl330_cond cond, u8 peri)
942{
943 if (dry_run)
944 return SZ_DMAWFP;
945
946 buf[0] = CMD_DMAWFP;
947
948 if (cond == SINGLE)
949 buf[0] |= (0 << 1) | (0 << 0);
950 else if (cond == BURST)
951 buf[0] |= (1 << 1) | (0 << 0);
952 else
953 buf[0] |= (0 << 1) | (1 << 0);
954
955 peri &= 0x1f;
956 peri <<= 3;
957 buf[1] = peri;
958
959 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
960 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
961
962 return SZ_DMAWFP;
963}
964
965static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
966{
967 if (dry_run)
968 return SZ_DMAWMB;
969
970 buf[0] = CMD_DMAWMB;
971
972 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
973
974 return SZ_DMAWMB;
975}
976
977struct _arg_GO {
978 u8 chan;
979 u32 addr;
980 unsigned ns;
981};
982
983static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
984 const struct _arg_GO *arg)
985{
986 u8 chan = arg->chan;
987 u32 addr = arg->addr;
988 unsigned ns = arg->ns;
989
990 if (dry_run)
991 return SZ_DMAGO;
992
993 buf[0] = CMD_DMAGO;
994 buf[0] |= (ns << 1);
995
996 buf[1] = chan & 0x7;
997
998 *((u32 *)&buf[2]) = addr;
999
1000 return SZ_DMAGO;
1001}
1002
1003#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
1004
1005/* Returns Time-Out */
1006static bool _until_dmac_idle(struct pl330_thread *thrd)
1007{
1008 void __iomem *regs = thrd->dmac->pinfo->base;
1009 unsigned long loops = msecs_to_loops(5);
1010
1011 do {
1012 /* Until Manager is Idle */
1013 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
1014 break;
1015
1016 cpu_relax();
1017 } while (--loops);
1018
1019 if (!loops)
1020 return true;
1021
1022 return false;
1023}
1024
1025static inline void _execute_DBGINSN(struct pl330_thread *thrd,
1026 u8 insn[], bool as_manager)
1027{
1028 void __iomem *regs = thrd->dmac->pinfo->base;
1029 u32 val;
1030
1031 val = (insn[0] << 16) | (insn[1] << 24);
1032 if (!as_manager) {
1033 val |= (1 << 0);
1034 val |= (thrd->id << 8); /* Channel Number */
1035 }
1036 writel(val, regs + DBGINST0);
1037
1038 val = *((u32 *)&insn[2]);
1039 writel(val, regs + DBGINST1);
1040
1041 /* If timed out due to halted state-machine */
1042 if (_until_dmac_idle(thrd)) {
1043 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
1044 return;
1045 }
1046
1047 /* Get going */
1048 writel(0, regs + DBGCMD);
1049}
1050
1051/*
1052 * Mark a _pl330_req as free.
1053 * We do it by writing DMAEND as the first instruction
1054 * because no valid request is going to have DMAEND as
1055 * its first instruction to execute.
1056 */
1057static void mark_free(struct pl330_thread *thrd, int idx)
1058{
1059 struct _pl330_req *req = &thrd->req[idx];
1060
1061 _emit_END(0, req->mc_cpu);
1062 req->mc_len = 0;
1063
1064 thrd->req_running = -1;
1065}
1066
1067static inline u32 _state(struct pl330_thread *thrd)
1068{
1069 void __iomem *regs = thrd->dmac->pinfo->base;
1070 u32 val;
1071
1072 if (is_manager(thrd))
1073 val = readl(regs + DS) & 0xf;
1074 else
1075 val = readl(regs + CS(thrd->id)) & 0xf;
1076
1077 switch (val) {
1078 case DS_ST_STOP:
1079 return PL330_STATE_STOPPED;
1080 case DS_ST_EXEC:
1081 return PL330_STATE_EXECUTING;
1082 case DS_ST_CMISS:
1083 return PL330_STATE_CACHEMISS;
1084 case DS_ST_UPDTPC:
1085 return PL330_STATE_UPDTPC;
1086 case DS_ST_WFE:
1087 return PL330_STATE_WFE;
1088 case DS_ST_FAULT:
1089 return PL330_STATE_FAULTING;
1090 case DS_ST_ATBRR:
1091 if (is_manager(thrd))
1092 return PL330_STATE_INVALID;
1093 else
1094 return PL330_STATE_ATBARRIER;
1095 case DS_ST_QBUSY:
1096 if (is_manager(thrd))
1097 return PL330_STATE_INVALID;
1098 else
1099 return PL330_STATE_QUEUEBUSY;
1100 case DS_ST_WFP:
1101 if (is_manager(thrd))
1102 return PL330_STATE_INVALID;
1103 else
1104 return PL330_STATE_WFP;
1105 case DS_ST_KILL:
1106 if (is_manager(thrd))
1107 return PL330_STATE_INVALID;
1108 else
1109 return PL330_STATE_KILLING;
1110 case DS_ST_CMPLT:
1111 if (is_manager(thrd))
1112 return PL330_STATE_INVALID;
1113 else
1114 return PL330_STATE_COMPLETING;
1115 case DS_ST_FLTCMP:
1116 if (is_manager(thrd))
1117 return PL330_STATE_INVALID;
1118 else
1119 return PL330_STATE_FAULT_COMPLETING;
1120 default:
1121 return PL330_STATE_INVALID;
1122 }
1123}
1124
1125static void _stop(struct pl330_thread *thrd)
1126{
1127 void __iomem *regs = thrd->dmac->pinfo->base;
1128 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1129
1130 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1131 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1132
1133 /* Return if nothing needs to be done */
1134 if (_state(thrd) == PL330_STATE_COMPLETING
1135 || _state(thrd) == PL330_STATE_KILLING
1136 || _state(thrd) == PL330_STATE_STOPPED)
1137 return;
1138
1139 _emit_KILL(0, insn);
1140
1141 /* Stop generating interrupts for SEV */
1142 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1143
1144 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1145}
1146
1147/* Start doing req 'idx' of thread 'thrd' */
1148static bool _trigger(struct pl330_thread *thrd)
1149{
1150 void __iomem *regs = thrd->dmac->pinfo->base;
1151 struct _pl330_req *req;
1152 struct pl330_req *r;
1153 struct _arg_GO go;
1154 unsigned ns;
1155 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1156 int idx;
1157
1158 /* Return if already ACTIVE */
1159 if (_state(thrd) != PL330_STATE_STOPPED)
1160 return true;
1161
1162 idx = 1 - thrd->lstenq;
1163 if (!IS_FREE(&thrd->req[idx]))
1164 req = &thrd->req[idx];
1165 else {
1166 idx = thrd->lstenq;
1167 if (!IS_FREE(&thrd->req[idx]))
1168 req = &thrd->req[idx];
1169 else
1170 req = NULL;
1171 }
1172
1173 /* Return if no request */
1174 if (!req || !req->r)
1175 return true;
1176
1177 r = req->r;
1178
1179 if (r->cfg)
1180 ns = r->cfg->nonsecure ? 1 : 0;
1181 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1182 ns = 1;
1183 else
1184 ns = 0;
1185
1186 /* See 'Abort Sources' point-4 at Page 2-25 */
1187 if (_manager_ns(thrd) && !ns)
1188 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1189 __func__, __LINE__);
1190
1191 go.chan = thrd->id;
1192 go.addr = req->mc_bus;
1193 go.ns = ns;
1194 _emit_GO(0, insn, &go);
1195
1196 /* Set to generate interrupts for SEV */
1197 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1198
1199 /* Only manager can execute GO */
1200 _execute_DBGINSN(thrd, insn, true);
1201
1202 thrd->req_running = idx;
1203
1204 return true;
1205}
1206
1207static bool _start(struct pl330_thread *thrd)
1208{
1209 switch (_state(thrd)) {
1210 case PL330_STATE_FAULT_COMPLETING:
1211 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1212
1213 if (_state(thrd) == PL330_STATE_KILLING)
1214 UNTIL(thrd, PL330_STATE_STOPPED)
1215
1216 case PL330_STATE_FAULTING:
1217 _stop(thrd);
1218
1219 case PL330_STATE_KILLING:
1220 case PL330_STATE_COMPLETING:
1221 UNTIL(thrd, PL330_STATE_STOPPED)
1222
1223 case PL330_STATE_STOPPED:
1224 return _trigger(thrd);
1225
1226 case PL330_STATE_WFP:
1227 case PL330_STATE_QUEUEBUSY:
1228 case PL330_STATE_ATBARRIER:
1229 case PL330_STATE_UPDTPC:
1230 case PL330_STATE_CACHEMISS:
1231 case PL330_STATE_EXECUTING:
1232 return true;
1233
1234 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1235 default:
1236 return false;
1237 }
1238}
1239
1240static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1241 const struct _xfer_spec *pxs, int cyc)
1242{
1243 int off = 0;
1244
1245 while (cyc--) {
1246 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1247 off += _emit_RMB(dry_run, &buf[off]);
1248 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1249 off += _emit_WMB(dry_run, &buf[off]);
1250 }
1251
1252 return off;
1253}
1254
1255static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1256 const struct _xfer_spec *pxs, int cyc)
1257{
1258 int off = 0;
1259
1260 while (cyc--) {
1261 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1262 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1263 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1264 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1265 }
1266
1267 return off;
1268}
1269
1270static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1271 const struct _xfer_spec *pxs, int cyc)
1272{
1273 int off = 0;
1274
1275 while (cyc--) {
1276 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1277 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1278 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1279 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1280 }
1281
1282 return off;
1283}
1284
1285static int _bursts(unsigned dry_run, u8 buf[],
1286 const struct _xfer_spec *pxs, int cyc)
1287{
1288 int off = 0;
1289
1290 switch (pxs->r->rqtype) {
1291 case MEMTODEV:
1292 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1293 break;
1294 case DEVTOMEM:
1295 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1296 break;
1297 case MEMTOMEM:
1298 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1299 break;
1300 default:
1301 off += 0x40000000; /* Scare off the Client */
1302 break;
1303 }
1304
1305 return off;
1306}
1307
1308/* Returns bytes consumed and updates bursts */
1309static inline int _loop(unsigned dry_run, u8 buf[],
1310 unsigned long *bursts, const struct _xfer_spec *pxs)
1311{
1312 int cyc, cycmax, szlp, szlpend, szbrst, off;
1313 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1314 struct _arg_LPEND lpend;
1315
1316 /* Max iterations possible in DMALP is 256 */
1317 if (*bursts >= 256*256) {
1318 lcnt1 = 256;
1319 lcnt0 = 256;
1320 cyc = *bursts / lcnt1 / lcnt0;
1321 } else if (*bursts > 256) {
1322 lcnt1 = 256;
1323 lcnt0 = *bursts / lcnt1;
1324 cyc = 1;
1325 } else {
1326 lcnt1 = *bursts;
1327 lcnt0 = 0;
1328 cyc = 1;
1329 }
1330
1331 szlp = _emit_LP(1, buf, 0, 0);
1332 szbrst = _bursts(1, buf, pxs, 1);
1333
1334 lpend.cond = ALWAYS;
1335 lpend.forever = false;
1336 lpend.loop = 0;
1337 lpend.bjump = 0;
1338 szlpend = _emit_LPEND(1, buf, &lpend);
1339
1340 if (lcnt0) {
1341 szlp *= 2;
1342 szlpend *= 2;
1343 }
1344
1345 /*
1346 * Max bursts that we can unroll due to limit on the
1347 * size of backward jump that can be encoded in DMALPEND
1348 * which is 8-bits and hence 255
1349 */
1350 cycmax = (255 - (szlp + szlpend)) / szbrst;
1351
1352 cyc = (cycmax < cyc) ? cycmax : cyc;
1353
1354 off = 0;
1355
1356 if (lcnt0) {
1357 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1358 ljmp0 = off;
1359 }
1360
1361 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1362 ljmp1 = off;
1363
1364 off += _bursts(dry_run, &buf[off], pxs, cyc);
1365
1366 lpend.cond = ALWAYS;
1367 lpend.forever = false;
1368 lpend.loop = 1;
1369 lpend.bjump = off - ljmp1;
1370 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1371
1372 if (lcnt0) {
1373 lpend.cond = ALWAYS;
1374 lpend.forever = false;
1375 lpend.loop = 0;
1376 lpend.bjump = off - ljmp0;
1377 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1378 }
1379
1380 *bursts = lcnt1 * cyc;
1381 if (lcnt0)
1382 *bursts *= lcnt0;
1383
1384 return off;
1385}
1386
1387static inline int _setup_loops(unsigned dry_run, u8 buf[],
1388 const struct _xfer_spec *pxs)
1389{
1390 struct pl330_xfer *x = pxs->x;
1391 u32 ccr = pxs->ccr;
1392 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1393 int off = 0;
1394
1395 while (bursts) {
1396 c = bursts;
1397 off += _loop(dry_run, &buf[off], &c, pxs);
1398 bursts -= c;
1399 }
1400
1401 return off;
1402}
1403
1404static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1405 const struct _xfer_spec *pxs)
1406{
1407 struct pl330_xfer *x = pxs->x;
1408 int off = 0;
1409
1410 /* DMAMOV SAR, x->src_addr */
1411 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1412 /* DMAMOV DAR, x->dst_addr */
1413 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1414
1415 /* Setup Loop(s) */
1416 off += _setup_loops(dry_run, &buf[off], pxs);
1417
1418 return off;
1419}
1420
1421/*
1422 * A req is a sequence of one or more xfer units.
1423 * Returns the number of bytes taken to setup the MC for the req.
1424 */
1425static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1426 unsigned index, struct _xfer_spec *pxs)
1427{
1428 struct _pl330_req *req = &thrd->req[index];
1429 struct pl330_xfer *x;
1430 u8 *buf = req->mc_cpu;
1431 int off = 0;
1432
1433 PL330_DBGMC_START(req->mc_bus);
1434
1435 /* DMAMOV CCR, ccr */
1436 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1437
1438 x = pxs->r->x;
1439 do {
1440 /* Error if xfer length is not aligned at burst size */
1441 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1442 return -EINVAL;
1443
1444 pxs->x = x;
1445 off += _setup_xfer(dry_run, &buf[off], pxs);
1446
1447 x = x->next;
1448 } while (x);
1449
1450 /* DMASEV peripheral/event */
1451 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1452 /* DMAEND */
1453 off += _emit_END(dry_run, &buf[off]);
1454
1455 return off;
1456}
1457
1458static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1459{
1460 u32 ccr = 0;
1461
1462 if (rqc->src_inc)
1463 ccr |= CC_SRCINC;
1464
1465 if (rqc->dst_inc)
1466 ccr |= CC_DSTINC;
1467
1468 /* We set same protection levels for Src and DST for now */
1469 if (rqc->privileged)
1470 ccr |= CC_SRCPRI | CC_DSTPRI;
1471 if (rqc->nonsecure)
1472 ccr |= CC_SRCNS | CC_DSTNS;
1473 if (rqc->insnaccess)
1474 ccr |= CC_SRCIA | CC_DSTIA;
1475
1476 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1477 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1478
1479 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1480 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1481
1482 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1483 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1484
1485 ccr |= (rqc->swap << CC_SWAP_SHFT);
1486
1487 return ccr;
1488}
1489
1490static inline bool _is_valid(u32 ccr)
1491{
1492 enum pl330_dstcachectrl dcctl;
1493 enum pl330_srccachectrl scctl;
1494
1495 dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1496 scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1497
1498 if (dcctl == DINVALID1 || dcctl == DINVALID2
1499 || scctl == SINVALID1 || scctl == SINVALID2)
1500 return false;
1501 else
1502 return true;
1503}
1504
1505/*
1506 * Submit a list of xfers after which the client wants notification.
1507 * Client is not notified after each xfer unit, just once after all
1508 * xfer units are done or some error occurs.
1509 */
1510static int pl330_submit_req(void *ch_id, struct pl330_req *r)
1511{
1512 struct pl330_thread *thrd = ch_id;
1513 struct pl330_dmac *pl330;
1514 struct pl330_info *pi;
1515 struct _xfer_spec xs;
1516 unsigned long flags;
1517 void __iomem *regs;
1518 unsigned idx;
1519 u32 ccr;
1520 int ret = 0;
1521
1522 /* No Req or Unacquired Channel or DMAC */
1523 if (!r || !thrd || thrd->free)
1524 return -EINVAL;
1525
1526 pl330 = thrd->dmac;
1527 pi = pl330->pinfo;
1528 regs = pi->base;
1529
1530 if (pl330->state == DYING
1531 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1532 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1533 __func__, __LINE__);
1534 return -EAGAIN;
1535 }
1536
1537 /* If request for non-existing peripheral */
1538 if (r->rqtype != MEMTOMEM && r->peri >= pi->pcfg.num_peri) {
1539 dev_info(thrd->dmac->pinfo->dev,
1540 "%s:%d Invalid peripheral(%u)!\n",
1541 __func__, __LINE__, r->peri);
1542 return -EINVAL;
1543 }
1544
1545 spin_lock_irqsave(&pl330->lock, flags);
1546
1547 if (_queue_full(thrd)) {
1548 ret = -EAGAIN;
1549 goto xfer_exit;
1550 }
1551
1552 /* Prefer Secure Channel */
1553 if (!_manager_ns(thrd))
1554 r->cfg->nonsecure = 0;
1555 else
1556 r->cfg->nonsecure = 1;
1557
1558 /* Use last settings, if not provided */
1559 if (r->cfg)
1560 ccr = _prepare_ccr(r->cfg);
1561 else
1562 ccr = readl(regs + CC(thrd->id));
1563
1564 /* If this req doesn't have valid xfer settings */
1565 if (!_is_valid(ccr)) {
1566 ret = -EINVAL;
1567 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1568 __func__, __LINE__, ccr);
1569 goto xfer_exit;
1570 }
1571
1572 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1573
1574 xs.ccr = ccr;
1575 xs.r = r;
1576
1577 /* First dry run to check if req is acceptable */
1578 ret = _setup_req(1, thrd, idx, &xs);
1579 if (ret < 0)
1580 goto xfer_exit;
1581
1582 if (ret > pi->mcbufsz / 2) {
1583 dev_info(thrd->dmac->pinfo->dev,
1584 "%s:%d Trying increasing mcbufsz\n",
1585 __func__, __LINE__);
1586 ret = -ENOMEM;
1587 goto xfer_exit;
1588 }
1589
1590 /* Hook the request */
1591 thrd->lstenq = idx;
1592 thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs);
1593 thrd->req[idx].r = r;
1594
1595 ret = 0;
1596
1597xfer_exit:
1598 spin_unlock_irqrestore(&pl330->lock, flags);
1599
1600 return ret;
1601}
1602
1603static void pl330_dotask(unsigned long data)
1604{
1605 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1606 struct pl330_info *pi = pl330->pinfo;
1607 unsigned long flags;
1608 int i;
1609
1610 spin_lock_irqsave(&pl330->lock, flags);
1611
1612 /* The DMAC itself gone nuts */
1613 if (pl330->dmac_tbd.reset_dmac) {
1614 pl330->state = DYING;
1615 /* Reset the manager too */
1616 pl330->dmac_tbd.reset_mngr = true;
1617 /* Clear the reset flag */
1618 pl330->dmac_tbd.reset_dmac = false;
1619 }
1620
1621 if (pl330->dmac_tbd.reset_mngr) {
1622 _stop(pl330->manager);
1623 /* Reset all channels */
1624 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1625 /* Clear the reset flag */
1626 pl330->dmac_tbd.reset_mngr = false;
1627 }
1628
1629 for (i = 0; i < pi->pcfg.num_chan; i++) {
1630
1631 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1632 struct pl330_thread *thrd = &pl330->channels[i];
1633 void __iomem *regs = pi->base;
1634 enum pl330_op_err err;
1635
1636 _stop(thrd);
1637
1638 if (readl(regs + FSC) & (1 << thrd->id))
1639 err = PL330_ERR_FAIL;
1640 else
1641 err = PL330_ERR_ABORT;
1642
1643 spin_unlock_irqrestore(&pl330->lock, flags);
1644
1645 _callback(thrd->req[1 - thrd->lstenq].r, err);
1646 _callback(thrd->req[thrd->lstenq].r, err);
1647
1648 spin_lock_irqsave(&pl330->lock, flags);
1649
1650 thrd->req[0].r = NULL;
1651 thrd->req[1].r = NULL;
1652 mark_free(thrd, 0);
1653 mark_free(thrd, 1);
1654
1655 /* Clear the reset flag */
1656 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1657 }
1658 }
1659
1660 spin_unlock_irqrestore(&pl330->lock, flags);
1661
1662 return;
1663}
1664
1665/* Returns 1 if state was updated, 0 otherwise */
1666static int pl330_update(const struct pl330_info *pi)
1667{
1668 struct _pl330_req *rqdone;
1669 struct pl330_dmac *pl330;
1670 unsigned long flags;
1671 void __iomem *regs;
1672 u32 val;
1673 int id, ev, ret = 0;
1674
1675 if (!pi || !pi->pl330_data)
1676 return 0;
1677
1678 regs = pi->base;
1679 pl330 = pi->pl330_data;
1680
1681 spin_lock_irqsave(&pl330->lock, flags);
1682
1683 val = readl(regs + FSM) & 0x1;
1684 if (val)
1685 pl330->dmac_tbd.reset_mngr = true;
1686 else
1687 pl330->dmac_tbd.reset_mngr = false;
1688
1689 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1690 pl330->dmac_tbd.reset_chan |= val;
1691 if (val) {
1692 int i = 0;
1693 while (i < pi->pcfg.num_chan) {
1694 if (val & (1 << i)) {
1695 dev_info(pi->dev,
1696 "Reset Channel-%d\t CS-%x FTC-%x\n",
1697 i, readl(regs + CS(i)),
1698 readl(regs + FTC(i)));
1699 _stop(&pl330->channels[i]);
1700 }
1701 i++;
1702 }
1703 }
1704
1705 /* Check which event happened i.e, thread notified */
1706 val = readl(regs + ES);
1707 if (pi->pcfg.num_events < 32
1708 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1709 pl330->dmac_tbd.reset_dmac = true;
1710 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1711 ret = 1;
1712 goto updt_exit;
1713 }
1714
1715 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1716 if (val & (1 << ev)) { /* Event occurred */
1717 struct pl330_thread *thrd;
1718 u32 inten = readl(regs + INTEN);
1719 int active;
1720
1721 /* Clear the event */
1722 if (inten & (1 << ev))
1723 writel(1 << ev, regs + INTCLR);
1724
1725 ret = 1;
1726
1727 id = pl330->events[ev];
1728
1729 thrd = &pl330->channels[id];
1730
1731 active = thrd->req_running;
1732 if (active == -1) /* Aborted */
1733 continue;
1734
1735 rqdone = &thrd->req[active];
1736 mark_free(thrd, active);
1737
1738 /* Get going again ASAP */
1739 _start(thrd);
1740
1741 /* For now, just make a list of callbacks to be done */
1742 list_add_tail(&rqdone->rqd, &pl330->req_done);
1743 }
1744 }
1745
1746 /* Now that we are in no hurry, do the callbacks */
1747 while (!list_empty(&pl330->req_done)) {
1748 struct pl330_req *r;
1749
1750 rqdone = container_of(pl330->req_done.next,
1751 struct _pl330_req, rqd);
1752
1753 list_del_init(&rqdone->rqd);
1754
1755 /* Detach the req */
1756 r = rqdone->r;
1757 rqdone->r = NULL;
1758
1759 spin_unlock_irqrestore(&pl330->lock, flags);
1760 _callback(r, PL330_ERR_NONE);
1761 spin_lock_irqsave(&pl330->lock, flags);
1762 }
1763
1764updt_exit:
1765 spin_unlock_irqrestore(&pl330->lock, flags);
1766
1767 if (pl330->dmac_tbd.reset_dmac
1768 || pl330->dmac_tbd.reset_mngr
1769 || pl330->dmac_tbd.reset_chan) {
1770 ret = 1;
1771 tasklet_schedule(&pl330->tasks);
1772 }
1773
1774 return ret;
1775}
1776
1777static int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op)
1778{
1779 struct pl330_thread *thrd = ch_id;
1780 struct pl330_dmac *pl330;
1781 unsigned long flags;
1782 int ret = 0, active = thrd->req_running;
1783
1784 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1785 return -EINVAL;
1786
1787 pl330 = thrd->dmac;
1788
1789 spin_lock_irqsave(&pl330->lock, flags);
1790
1791 switch (op) {
1792 case PL330_OP_FLUSH:
1793 /* Make sure the channel is stopped */
1794 _stop(thrd);
1795
1796 thrd->req[0].r = NULL;
1797 thrd->req[1].r = NULL;
1798 mark_free(thrd, 0);
1799 mark_free(thrd, 1);
1800 break;
1801
1802 case PL330_OP_ABORT:
1803 /* Make sure the channel is stopped */
1804 _stop(thrd);
1805
1806 /* ABORT is only for the active req */
1807 if (active == -1)
1808 break;
1809
1810 thrd->req[active].r = NULL;
1811 mark_free(thrd, active);
1812
1813 /* Start the next */
1814 case PL330_OP_START:
1815 if ((active == -1) && !_start(thrd))
1816 ret = -EIO;
1817 break;
1818
1819 default:
1820 ret = -EINVAL;
1821 }
1822
1823 spin_unlock_irqrestore(&pl330->lock, flags);
1824 return ret;
1825}
1826
1827static int pl330_chan_status(void *ch_id, struct pl330_chanstatus *pstatus)
1828{
1829 struct pl330_thread *thrd = ch_id;
1830 struct pl330_dmac *pl330;
1831 struct pl330_info *pi;
1832 void __iomem *regs;
1833 int active;
1834 u32 val;
1835
1836 if (!pstatus || !thrd || thrd->free)
1837 return -EINVAL;
1838
1839 pl330 = thrd->dmac;
1840 pi = pl330->pinfo;
1841 regs = pi->base;
1842
1843 /* The client should remove the DMAC and add again */
1844 if (pl330->state == DYING)
1845 pstatus->dmac_halted = true;
1846 else
1847 pstatus->dmac_halted = false;
1848
1849 val = readl(regs + FSC);
1850 if (val & (1 << thrd->id))
1851 pstatus->faulting = true;
1852 else
1853 pstatus->faulting = false;
1854
1855 active = thrd->req_running;
1856
1857 if (active == -1) {
1858 /* Indicate that the thread is not running */
1859 pstatus->top_req = NULL;
1860 pstatus->wait_req = NULL;
1861 } else {
1862 pstatus->top_req = thrd->req[active].r;
1863 pstatus->wait_req = !IS_FREE(&thrd->req[1 - active])
1864 ? thrd->req[1 - active].r : NULL;
1865 }
1866
1867 pstatus->src_addr = readl(regs + SA(thrd->id));
1868 pstatus->dst_addr = readl(regs + DA(thrd->id));
1869
1870 return 0;
1871}
1872
1873/* Reserve an event */
1874static inline int _alloc_event(struct pl330_thread *thrd)
1875{
1876 struct pl330_dmac *pl330 = thrd->dmac;
1877 struct pl330_info *pi = pl330->pinfo;
1878 int ev;
1879
1880 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1881 if (pl330->events[ev] == -1) {
1882 pl330->events[ev] = thrd->id;
1883 return ev;
1884 }
1885
1886 return -1;
1887}
1888
1889static bool _chan_ns(const struct pl330_info *pi, int i)
1890{
1891 return pi->pcfg.irq_ns & (1 << i);
1892}
1893
1894/* Upon success, returns IdentityToken for the
1895 * allocated channel, NULL otherwise.
1896 */
1897static void *pl330_request_channel(const struct pl330_info *pi)
1898{
1899 struct pl330_thread *thrd = NULL;
1900 struct pl330_dmac *pl330;
1901 unsigned long flags;
1902 int chans, i;
1903
1904 if (!pi || !pi->pl330_data)
1905 return NULL;
1906
1907 pl330 = pi->pl330_data;
1908
1909 if (pl330->state == DYING)
1910 return NULL;
1911
1912 chans = pi->pcfg.num_chan;
1913
1914 spin_lock_irqsave(&pl330->lock, flags);
1915
1916 for (i = 0; i < chans; i++) {
1917 thrd = &pl330->channels[i];
1918 if ((thrd->free) && (!_manager_ns(thrd) ||
1919 _chan_ns(pi, i))) {
1920 thrd->ev = _alloc_event(thrd);
1921 if (thrd->ev >= 0) {
1922 thrd->free = false;
1923 thrd->lstenq = 1;
1924 thrd->req[0].r = NULL;
1925 mark_free(thrd, 0);
1926 thrd->req[1].r = NULL;
1927 mark_free(thrd, 1);
1928 break;
1929 }
1930 }
1931 thrd = NULL;
1932 }
1933
1934 spin_unlock_irqrestore(&pl330->lock, flags);
1935
1936 return thrd;
1937}
1938
1939/* Release an event */
1940static inline void _free_event(struct pl330_thread *thrd, int ev)
1941{
1942 struct pl330_dmac *pl330 = thrd->dmac;
1943 struct pl330_info *pi = pl330->pinfo;
1944
1945 /* If the event is valid and was held by the thread */
1946 if (ev >= 0 && ev < pi->pcfg.num_events
1947 && pl330->events[ev] == thrd->id)
1948 pl330->events[ev] = -1;
1949}
1950
1951static void pl330_release_channel(void *ch_id)
1952{
1953 struct pl330_thread *thrd = ch_id;
1954 struct pl330_dmac *pl330;
1955 unsigned long flags;
1956
1957 if (!thrd || thrd->free)
1958 return;
1959
1960 _stop(thrd);
1961
1962 _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1963 _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1964
1965 pl330 = thrd->dmac;
1966
1967 spin_lock_irqsave(&pl330->lock, flags);
1968 _free_event(thrd, thrd->ev);
1969 thrd->free = true;
1970 spin_unlock_irqrestore(&pl330->lock, flags);
1971}
1972
1973/* Initialize the structure for PL330 configuration, that can be used
1974 * by the client driver the make best use of the DMAC
1975 */
1976static void read_dmac_config(struct pl330_info *pi)
1977{
1978 void __iomem *regs = pi->base;
1979 u32 val;
1980
1981 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1982 val &= CRD_DATA_WIDTH_MASK;
1983 pi->pcfg.data_bus_width = 8 * (1 << val);
1984
1985 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1986 val &= CRD_DATA_BUFF_MASK;
1987 pi->pcfg.data_buf_dep = val + 1;
1988
1989 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1990 val &= CR0_NUM_CHANS_MASK;
1991 val += 1;
1992 pi->pcfg.num_chan = val;
1993
1994 val = readl(regs + CR0);
1995 if (val & CR0_PERIPH_REQ_SET) {
1996 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1997 val += 1;
1998 pi->pcfg.num_peri = val;
1999 pi->pcfg.peri_ns = readl(regs + CR4);
2000 } else {
2001 pi->pcfg.num_peri = 0;
2002 }
2003
2004 val = readl(regs + CR0);
2005 if (val & CR0_BOOT_MAN_NS)
2006 pi->pcfg.mode |= DMAC_MODE_NS;
2007 else
2008 pi->pcfg.mode &= ~DMAC_MODE_NS;
2009
2010 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
2011 val &= CR0_NUM_EVENTS_MASK;
2012 val += 1;
2013 pi->pcfg.num_events = val;
2014
2015 pi->pcfg.irq_ns = readl(regs + CR3);
2016
2017 pi->pcfg.periph_id = get_id(pi, PERIPH_ID);
2018 pi->pcfg.pcell_id = get_id(pi, PCELL_ID);
2019}
2020
2021static inline void _reset_thread(struct pl330_thread *thrd)
2022{
2023 struct pl330_dmac *pl330 = thrd->dmac;
2024 struct pl330_info *pi = pl330->pinfo;
2025
2026 thrd->req[0].mc_cpu = pl330->mcode_cpu
2027 + (thrd->id * pi->mcbufsz);
2028 thrd->req[0].mc_bus = pl330->mcode_bus
2029 + (thrd->id * pi->mcbufsz);
2030 thrd->req[0].r = NULL;
2031 mark_free(thrd, 0);
2032
2033 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
2034 + pi->mcbufsz / 2;
2035 thrd->req[1].mc_bus = thrd->req[0].mc_bus
2036 + pi->mcbufsz / 2;
2037 thrd->req[1].r = NULL;
2038 mark_free(thrd, 1);
2039}
2040
2041static int dmac_alloc_threads(struct pl330_dmac *pl330)
2042{
2043 struct pl330_info *pi = pl330->pinfo;
2044 int chans = pi->pcfg.num_chan;
2045 struct pl330_thread *thrd;
2046 int i;
2047
2048 /* Allocate 1 Manager and 'chans' Channel threads */
2049 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
2050 GFP_KERNEL);
2051 if (!pl330->channels)
2052 return -ENOMEM;
2053
2054 /* Init Channel threads */
2055 for (i = 0; i < chans; i++) {
2056 thrd = &pl330->channels[i];
2057 thrd->id = i;
2058 thrd->dmac = pl330;
2059 _reset_thread(thrd);
2060 thrd->free = true;
2061 }
2062
2063 /* MANAGER is indexed at the end */
2064 thrd = &pl330->channels[chans];
2065 thrd->id = chans;
2066 thrd->dmac = pl330;
2067 thrd->free = false;
2068 pl330->manager = thrd;
2069
2070 return 0;
2071}
2072
2073static int dmac_alloc_resources(struct pl330_dmac *pl330)
2074{
2075 struct pl330_info *pi = pl330->pinfo;
2076 int chans = pi->pcfg.num_chan;
2077 int ret;
2078
2079 /*
2080 * Alloc MicroCode buffer for 'chans' Channel threads.
2081 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
2082 */
2083 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
2084 chans * pi->mcbufsz,
2085 &pl330->mcode_bus, GFP_KERNEL);
2086 if (!pl330->mcode_cpu) {
2087 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2088 __func__, __LINE__);
2089 return -ENOMEM;
2090 }
2091
2092 ret = dmac_alloc_threads(pl330);
2093 if (ret) {
2094 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
2095 __func__, __LINE__);
2096 dma_free_coherent(pi->dev,
2097 chans * pi->mcbufsz,
2098 pl330->mcode_cpu, pl330->mcode_bus);
2099 return ret;
2100 }
2101
2102 return 0;
2103}
2104
2105static int pl330_add(struct pl330_info *pi)
2106{
2107 struct pl330_dmac *pl330;
2108 void __iomem *regs;
2109 int i, ret;
2110
2111 if (!pi || !pi->dev)
2112 return -EINVAL;
2113
2114 /* If already added */
2115 if (pi->pl330_data)
2116 return -EINVAL;
2117
2118 /*
2119 * If the SoC can perform reset on the DMAC, then do it
2120 * before reading its configuration.
2121 */
2122 if (pi->dmac_reset)
2123 pi->dmac_reset(pi);
2124
2125 regs = pi->base;
2126
2127 /* Check if we can handle this DMAC */
2128 if ((get_id(pi, PERIPH_ID) & 0xfffff) != PERIPH_ID_VAL
2129 || get_id(pi, PCELL_ID) != PCELL_ID_VAL) {
2130 dev_err(pi->dev, "PERIPH_ID 0x%x, PCELL_ID 0x%x !\n",
2131 get_id(pi, PERIPH_ID), get_id(pi, PCELL_ID));
2132 return -EINVAL;
2133 }
2134
2135 /* Read the configuration of the DMAC */
2136 read_dmac_config(pi);
2137
2138 if (pi->pcfg.num_events == 0) {
2139 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2140 __func__, __LINE__);
2141 return -EINVAL;
2142 }
2143
2144 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2145 if (!pl330) {
2146 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2147 __func__, __LINE__);
2148 return -ENOMEM;
2149 }
2150
2151 /* Assign the info structure and private data */
2152 pl330->pinfo = pi;
2153 pi->pl330_data = pl330;
2154
2155 spin_lock_init(&pl330->lock);
2156
2157 INIT_LIST_HEAD(&pl330->req_done);
2158
2159 /* Use default MC buffer size if not provided */
2160 if (!pi->mcbufsz)
2161 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2162
2163 /* Mark all events as free */
2164 for (i = 0; i < pi->pcfg.num_events; i++)
2165 pl330->events[i] = -1;
2166
2167 /* Allocate resources needed by the DMAC */
2168 ret = dmac_alloc_resources(pl330);
2169 if (ret) {
2170 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2171 kfree(pl330);
2172 return ret;
2173 }
2174
2175 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2176
2177 pl330->state = INIT;
2178
2179 return 0;
2180}
2181
2182static int dmac_free_threads(struct pl330_dmac *pl330)
2183{
2184 struct pl330_info *pi = pl330->pinfo;
2185 int chans = pi->pcfg.num_chan;
2186 struct pl330_thread *thrd;
2187 int i;
2188
2189 /* Release Channel threads */
2190 for (i = 0; i < chans; i++) {
2191 thrd = &pl330->channels[i];
2192 pl330_release_channel((void *)thrd);
2193 }
2194
2195 /* Free memory */
2196 kfree(pl330->channels);
2197
2198 return 0;
2199}
2200
2201static void dmac_free_resources(struct pl330_dmac *pl330)
2202{
2203 struct pl330_info *pi = pl330->pinfo;
2204 int chans = pi->pcfg.num_chan;
2205
2206 dmac_free_threads(pl330);
2207
2208 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2209 pl330->mcode_cpu, pl330->mcode_bus);
2210}
2211
2212static void pl330_del(struct pl330_info *pi)
2213{
2214 struct pl330_dmac *pl330;
2215
2216 if (!pi || !pi->pl330_data)
2217 return;
2218
2219 pl330 = pi->pl330_data;
2220
2221 pl330->state = UNINIT;
2222
2223 tasklet_kill(&pl330->tasks);
2224
2225 /* Free DMAC resources */
2226 dmac_free_resources(pl330);
2227
2228 kfree(pl330);
2229 pi->pl330_data = NULL;
2230}
2231
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002232/* forward declaration */
2233static struct amba_driver pl330_driver;
2234
Jassi Brarb3040e42010-05-23 20:28:19 -07002235static inline struct dma_pl330_chan *
2236to_pchan(struct dma_chan *ch)
2237{
2238 if (!ch)
2239 return NULL;
2240
2241 return container_of(ch, struct dma_pl330_chan, chan);
2242}
2243
2244static inline struct dma_pl330_desc *
2245to_desc(struct dma_async_tx_descriptor *tx)
2246{
2247 return container_of(tx, struct dma_pl330_desc, txd);
2248}
2249
2250static inline void free_desc_list(struct list_head *list)
2251{
2252 struct dma_pl330_dmac *pdmac;
2253 struct dma_pl330_desc *desc;
2254 struct dma_pl330_chan *pch;
2255 unsigned long flags;
2256
2257 if (list_empty(list))
2258 return;
2259
2260 /* Finish off the work list */
2261 list_for_each_entry(desc, list, node) {
2262 dma_async_tx_callback callback;
2263 void *param;
2264
2265 /* All desc in a list belong to same channel */
2266 pch = desc->pchan;
2267 callback = desc->txd.callback;
2268 param = desc->txd.callback_param;
2269
2270 if (callback)
2271 callback(param);
2272
2273 desc->pchan = NULL;
2274 }
2275
2276 pdmac = pch->dmac;
2277
2278 spin_lock_irqsave(&pdmac->pool_lock, flags);
2279 list_splice_tail_init(list, &pdmac->desc_pool);
2280 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2281}
2282
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002283static inline void handle_cyclic_desc_list(struct list_head *list)
2284{
2285 struct dma_pl330_desc *desc;
2286 struct dma_pl330_chan *pch;
2287 unsigned long flags;
2288
2289 if (list_empty(list))
2290 return;
2291
2292 list_for_each_entry(desc, list, node) {
2293 dma_async_tx_callback callback;
2294
2295 /* Change status to reload it */
2296 desc->status = PREP;
2297 pch = desc->pchan;
2298 callback = desc->txd.callback;
2299 if (callback)
2300 callback(desc->txd.callback_param);
2301 }
2302
2303 spin_lock_irqsave(&pch->lock, flags);
2304 list_splice_tail_init(list, &pch->work_list);
2305 spin_unlock_irqrestore(&pch->lock, flags);
2306}
2307
Jassi Brarb3040e42010-05-23 20:28:19 -07002308static inline void fill_queue(struct dma_pl330_chan *pch)
2309{
2310 struct dma_pl330_desc *desc;
2311 int ret;
2312
2313 list_for_each_entry(desc, &pch->work_list, node) {
2314
2315 /* If already submitted */
2316 if (desc->status == BUSY)
2317 break;
2318
2319 ret = pl330_submit_req(pch->pl330_chid,
2320 &desc->req);
2321 if (!ret) {
2322 desc->status = BUSY;
2323 break;
2324 } else if (ret == -EAGAIN) {
2325 /* QFull or DMAC Dying */
2326 break;
2327 } else {
2328 /* Unacceptable request */
2329 desc->status = DONE;
2330 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2331 __func__, __LINE__, desc->txd.cookie);
2332 tasklet_schedule(&pch->task);
2333 }
2334 }
2335}
2336
2337static void pl330_tasklet(unsigned long data)
2338{
2339 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2340 struct dma_pl330_desc *desc, *_dt;
2341 unsigned long flags;
2342 LIST_HEAD(list);
2343
2344 spin_lock_irqsave(&pch->lock, flags);
2345
2346 /* Pick up ripe tomatoes */
2347 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2348 if (desc->status == DONE) {
2349 pch->completed = desc->txd.cookie;
2350 list_move_tail(&desc->node, &list);
2351 }
2352
2353 /* Try to submit a req imm. next to the last completed cookie */
2354 fill_queue(pch);
2355
2356 /* Make sure the PL330 Channel thread is active */
2357 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
2358
2359 spin_unlock_irqrestore(&pch->lock, flags);
2360
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002361 if (pch->cyclic)
2362 handle_cyclic_desc_list(&list);
2363 else
2364 free_desc_list(&list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002365}
2366
2367static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
2368{
2369 struct dma_pl330_desc *desc = token;
2370 struct dma_pl330_chan *pch = desc->pchan;
2371 unsigned long flags;
2372
2373 /* If desc aborted */
2374 if (!pch)
2375 return;
2376
2377 spin_lock_irqsave(&pch->lock, flags);
2378
2379 desc->status = DONE;
2380
2381 spin_unlock_irqrestore(&pch->lock, flags);
2382
2383 tasklet_schedule(&pch->task);
2384}
2385
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002386bool pl330_filter(struct dma_chan *chan, void *param)
2387{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002388 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002389
2390 if (chan->device->dev->driver != &pl330_driver.drv)
2391 return false;
2392
Thomas Abraham93ed5542011-10-24 11:43:31 +02002393#ifdef CONFIG_OF
2394 if (chan->device->dev->of_node) {
2395 const __be32 *prop_value;
2396 phandle phandle;
2397 struct device_node *node;
2398
2399 prop_value = ((struct property *)param)->value;
2400 phandle = be32_to_cpup(prop_value++);
2401 node = of_find_node_by_phandle(phandle);
2402 return ((chan->private == node) &&
2403 (chan->chan_id == be32_to_cpup(prop_value)));
2404 }
2405#endif
2406
Thomas Abrahamcd072512011-10-24 11:43:11 +02002407 peri_id = chan->private;
2408 return *peri_id == (unsigned)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002409}
2410EXPORT_SYMBOL(pl330_filter);
2411
Jassi Brarb3040e42010-05-23 20:28:19 -07002412static int pl330_alloc_chan_resources(struct dma_chan *chan)
2413{
2414 struct dma_pl330_chan *pch = to_pchan(chan);
2415 struct dma_pl330_dmac *pdmac = pch->dmac;
2416 unsigned long flags;
2417
2418 spin_lock_irqsave(&pch->lock, flags);
2419
2420 pch->completed = chan->cookie = 1;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002421 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002422
2423 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
2424 if (!pch->pl330_chid) {
2425 spin_unlock_irqrestore(&pch->lock, flags);
2426 return 0;
2427 }
2428
2429 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2430
2431 spin_unlock_irqrestore(&pch->lock, flags);
2432
2433 return 1;
2434}
2435
2436static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2437{
2438 struct dma_pl330_chan *pch = to_pchan(chan);
Boojin Kimae43b882011-09-02 09:44:32 +09002439 struct dma_pl330_desc *desc, *_dt;
Jassi Brarb3040e42010-05-23 20:28:19 -07002440 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002441 struct dma_pl330_dmac *pdmac = pch->dmac;
2442 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002443 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002444
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002445 switch (cmd) {
2446 case DMA_TERMINATE_ALL:
2447 spin_lock_irqsave(&pch->lock, flags);
2448
2449 /* FLUSH the PL330 Channel thread */
2450 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
2451
2452 /* Mark all desc done */
Boojin Kimae43b882011-09-02 09:44:32 +09002453 list_for_each_entry_safe(desc, _dt, &pch->work_list , node) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002454 desc->status = DONE;
Boojin Kimae43b882011-09-02 09:44:32 +09002455 pch->completed = desc->txd.cookie;
2456 list_move_tail(&desc->node, &list);
2457 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002458
Boojin Kimae43b882011-09-02 09:44:32 +09002459 list_splice_tail_init(&list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002460 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002461 break;
2462 case DMA_SLAVE_CONFIG:
2463 slave_config = (struct dma_slave_config *)arg;
2464
Vinod Kouldb8196d2011-10-13 22:34:23 +05302465 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002466 if (slave_config->dst_addr)
2467 pch->fifo_addr = slave_config->dst_addr;
2468 if (slave_config->dst_addr_width)
2469 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2470 if (slave_config->dst_maxburst)
2471 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302472 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002473 if (slave_config->src_addr)
2474 pch->fifo_addr = slave_config->src_addr;
2475 if (slave_config->src_addr_width)
2476 pch->burst_sz = __ffs(slave_config->src_addr_width);
2477 if (slave_config->src_maxburst)
2478 pch->burst_len = slave_config->src_maxburst;
2479 }
2480 break;
2481 default:
2482 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002483 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002484 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002485
2486 return 0;
2487}
2488
2489static void pl330_free_chan_resources(struct dma_chan *chan)
2490{
2491 struct dma_pl330_chan *pch = to_pchan(chan);
2492 unsigned long flags;
2493
2494 spin_lock_irqsave(&pch->lock, flags);
2495
2496 tasklet_kill(&pch->task);
2497
2498 pl330_release_channel(pch->pl330_chid);
2499 pch->pl330_chid = NULL;
2500
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002501 if (pch->cyclic)
2502 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2503
Jassi Brarb3040e42010-05-23 20:28:19 -07002504 spin_unlock_irqrestore(&pch->lock, flags);
2505}
2506
2507static enum dma_status
2508pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2509 struct dma_tx_state *txstate)
2510{
2511 struct dma_pl330_chan *pch = to_pchan(chan);
2512 dma_cookie_t last_done, last_used;
2513 int ret;
2514
2515 last_done = pch->completed;
2516 last_used = chan->cookie;
2517
2518 ret = dma_async_is_complete(cookie, last_done, last_used);
2519
2520 dma_set_tx_state(txstate, last_done, last_used, 0);
2521
2522 return ret;
2523}
2524
2525static void pl330_issue_pending(struct dma_chan *chan)
2526{
2527 pl330_tasklet((unsigned long) to_pchan(chan));
2528}
2529
2530/*
2531 * We returned the last one of the circular list of descriptor(s)
2532 * from prep_xxx, so the argument to submit corresponds to the last
2533 * descriptor of the list.
2534 */
2535static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2536{
2537 struct dma_pl330_desc *desc, *last = to_desc(tx);
2538 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2539 dma_cookie_t cookie;
2540 unsigned long flags;
2541
2542 spin_lock_irqsave(&pch->lock, flags);
2543
2544 /* Assign cookies to all nodes */
2545 cookie = tx->chan->cookie;
2546
2547 while (!list_empty(&last->node)) {
2548 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
2549
2550 if (++cookie < 0)
2551 cookie = 1;
2552 desc->txd.cookie = cookie;
2553
2554 list_move_tail(&desc->node, &pch->work_list);
2555 }
2556
2557 if (++cookie < 0)
2558 cookie = 1;
2559 last->txd.cookie = cookie;
2560
2561 list_add_tail(&last->node, &pch->work_list);
2562
2563 tx->chan->cookie = cookie;
2564
2565 spin_unlock_irqrestore(&pch->lock, flags);
2566
2567 return cookie;
2568}
2569
2570static inline void _init_desc(struct dma_pl330_desc *desc)
2571{
2572 desc->pchan = NULL;
2573 desc->req.x = &desc->px;
2574 desc->req.token = desc;
2575 desc->rqcfg.swap = SWAP_NO;
2576 desc->rqcfg.privileged = 0;
2577 desc->rqcfg.insnaccess = 0;
2578 desc->rqcfg.scctl = SCCTRL0;
2579 desc->rqcfg.dcctl = DCCTRL0;
2580 desc->req.cfg = &desc->rqcfg;
2581 desc->req.xfer_cb = dma_pl330_rqcb;
2582 desc->txd.tx_submit = pl330_tx_submit;
2583
2584 INIT_LIST_HEAD(&desc->node);
2585}
2586
2587/* Returns the number of descriptors added to the DMAC pool */
2588int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
2589{
2590 struct dma_pl330_desc *desc;
2591 unsigned long flags;
2592 int i;
2593
2594 if (!pdmac)
2595 return 0;
2596
2597 desc = kmalloc(count * sizeof(*desc), flg);
2598 if (!desc)
2599 return 0;
2600
2601 spin_lock_irqsave(&pdmac->pool_lock, flags);
2602
2603 for (i = 0; i < count; i++) {
2604 _init_desc(&desc[i]);
2605 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2606 }
2607
2608 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2609
2610 return count;
2611}
2612
2613static struct dma_pl330_desc *
2614pluck_desc(struct dma_pl330_dmac *pdmac)
2615{
2616 struct dma_pl330_desc *desc = NULL;
2617 unsigned long flags;
2618
2619 if (!pdmac)
2620 return NULL;
2621
2622 spin_lock_irqsave(&pdmac->pool_lock, flags);
2623
2624 if (!list_empty(&pdmac->desc_pool)) {
2625 desc = list_entry(pdmac->desc_pool.next,
2626 struct dma_pl330_desc, node);
2627
2628 list_del_init(&desc->node);
2629
2630 desc->status = PREP;
2631 desc->txd.callback = NULL;
2632 }
2633
2634 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2635
2636 return desc;
2637}
2638
2639static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2640{
2641 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002642 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002643 struct dma_pl330_desc *desc;
2644
2645 /* Pluck one desc from the pool of DMAC */
2646 desc = pluck_desc(pdmac);
2647
2648 /* If the DMAC pool is empty, alloc new */
2649 if (!desc) {
2650 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2651 return NULL;
2652
2653 /* Try again */
2654 desc = pluck_desc(pdmac);
2655 if (!desc) {
2656 dev_err(pch->dmac->pif.dev,
2657 "%s:%d ALERT!\n", __func__, __LINE__);
2658 return NULL;
2659 }
2660 }
2661
2662 /* Initialize the descriptor */
2663 desc->pchan = pch;
2664 desc->txd.cookie = 0;
2665 async_tx_ack(&desc->txd);
2666
Thomas Abrahamcd072512011-10-24 11:43:11 +02002667 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002668
2669 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2670
2671 return desc;
2672}
2673
2674static inline void fill_px(struct pl330_xfer *px,
2675 dma_addr_t dst, dma_addr_t src, size_t len)
2676{
2677 px->next = NULL;
2678 px->bytes = len;
2679 px->dst_addr = dst;
2680 px->src_addr = src;
2681}
2682
2683static struct dma_pl330_desc *
2684__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2685 dma_addr_t src, size_t len)
2686{
2687 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2688
2689 if (!desc) {
2690 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2691 __func__, __LINE__);
2692 return NULL;
2693 }
2694
2695 /*
2696 * Ideally we should lookout for reqs bigger than
2697 * those that can be programmed with 256 bytes of
2698 * MC buffer, but considering a req size is seldom
2699 * going to be word-unaligned and more than 200MB,
2700 * we take it easy.
2701 * Also, should the limit is reached we'd rather
2702 * have the platform increase MC buffer size than
2703 * complicating this API driver.
2704 */
2705 fill_px(&desc->px, dst, src, len);
2706
2707 return desc;
2708}
2709
2710/* Call after fixing burst size */
2711static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2712{
2713 struct dma_pl330_chan *pch = desc->pchan;
2714 struct pl330_info *pi = &pch->dmac->pif;
2715 int burst_len;
2716
2717 burst_len = pi->pcfg.data_bus_width / 8;
2718 burst_len *= pi->pcfg.data_buf_dep;
2719 burst_len >>= desc->rqcfg.brst_size;
2720
2721 /* src/dst_burst_len can't be more than 16 */
2722 if (burst_len > 16)
2723 burst_len = 16;
2724
2725 while (burst_len > 1) {
2726 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2727 break;
2728 burst_len--;
2729 }
2730
2731 return burst_len;
2732}
2733
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002734static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2735 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302736 size_t period_len, enum dma_transfer_direction direction)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002737{
2738 struct dma_pl330_desc *desc;
2739 struct dma_pl330_chan *pch = to_pchan(chan);
2740 dma_addr_t dst;
2741 dma_addr_t src;
2742
2743 desc = pl330_get_desc(pch);
2744 if (!desc) {
2745 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2746 __func__, __LINE__);
2747 return NULL;
2748 }
2749
2750 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05302751 case DMA_MEM_TO_DEV:
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002752 desc->rqcfg.src_inc = 1;
2753 desc->rqcfg.dst_inc = 0;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002754 desc->req.rqtype = MEMTODEV;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002755 src = dma_addr;
2756 dst = pch->fifo_addr;
2757 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302758 case DMA_DEV_TO_MEM:
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002759 desc->rqcfg.src_inc = 0;
2760 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002761 desc->req.rqtype = DEVTOMEM;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002762 src = pch->fifo_addr;
2763 dst = dma_addr;
2764 break;
2765 default:
2766 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2767 __func__, __LINE__);
2768 return NULL;
2769 }
2770
2771 desc->rqcfg.brst_size = pch->burst_sz;
2772 desc->rqcfg.brst_len = 1;
2773
2774 pch->cyclic = true;
2775
2776 fill_px(&desc->px, dst, src, period_len);
2777
2778 return &desc->txd;
2779}
2780
Jassi Brarb3040e42010-05-23 20:28:19 -07002781static struct dma_async_tx_descriptor *
2782pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2783 dma_addr_t src, size_t len, unsigned long flags)
2784{
2785 struct dma_pl330_desc *desc;
2786 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002787 struct pl330_info *pi;
2788 int burst;
2789
Rob Herring4e0e6102011-07-25 16:05:04 -05002790 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002791 return NULL;
2792
Jassi Brarb3040e42010-05-23 20:28:19 -07002793 pi = &pch->dmac->pif;
2794
2795 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2796 if (!desc)
2797 return NULL;
2798
2799 desc->rqcfg.src_inc = 1;
2800 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002801 desc->req.rqtype = MEMTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002802
2803 /* Select max possible burst size */
2804 burst = pi->pcfg.data_bus_width / 8;
2805
2806 while (burst > 1) {
2807 if (!(len % burst))
2808 break;
2809 burst /= 2;
2810 }
2811
2812 desc->rqcfg.brst_size = 0;
2813 while (burst != (1 << desc->rqcfg.brst_size))
2814 desc->rqcfg.brst_size++;
2815
2816 desc->rqcfg.brst_len = get_burst_len(desc, len);
2817
2818 desc->txd.flags = flags;
2819
2820 return &desc->txd;
2821}
2822
2823static struct dma_async_tx_descriptor *
2824pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302825 unsigned int sg_len, enum dma_transfer_direction direction,
Jassi Brarb3040e42010-05-23 20:28:19 -07002826 unsigned long flg)
2827{
2828 struct dma_pl330_desc *first, *desc = NULL;
2829 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002830 struct scatterlist *sg;
2831 unsigned long flags;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002832 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002833 dma_addr_t addr;
2834
Thomas Abrahamcd072512011-10-24 11:43:11 +02002835 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002836 return NULL;
2837
Boojin Kim1b9bb712011-09-02 09:44:30 +09002838 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002839
2840 first = NULL;
2841
2842 for_each_sg(sgl, sg, sg_len, i) {
2843
2844 desc = pl330_get_desc(pch);
2845 if (!desc) {
2846 struct dma_pl330_dmac *pdmac = pch->dmac;
2847
2848 dev_err(pch->dmac->pif.dev,
2849 "%s:%d Unable to fetch desc\n",
2850 __func__, __LINE__);
2851 if (!first)
2852 return NULL;
2853
2854 spin_lock_irqsave(&pdmac->pool_lock, flags);
2855
2856 while (!list_empty(&first->node)) {
2857 desc = list_entry(first->node.next,
2858 struct dma_pl330_desc, node);
2859 list_move_tail(&desc->node, &pdmac->desc_pool);
2860 }
2861
2862 list_move_tail(&first->node, &pdmac->desc_pool);
2863
2864 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2865
2866 return NULL;
2867 }
2868
2869 if (!first)
2870 first = desc;
2871 else
2872 list_add_tail(&desc->node, &first->node);
2873
Vinod Kouldb8196d2011-10-13 22:34:23 +05302874 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002875 desc->rqcfg.src_inc = 1;
2876 desc->rqcfg.dst_inc = 0;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002877 desc->req.rqtype = MEMTODEV;
Jassi Brarb3040e42010-05-23 20:28:19 -07002878 fill_px(&desc->px,
2879 addr, sg_dma_address(sg), sg_dma_len(sg));
2880 } else {
2881 desc->rqcfg.src_inc = 0;
2882 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002883 desc->req.rqtype = DEVTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002884 fill_px(&desc->px,
2885 sg_dma_address(sg), addr, sg_dma_len(sg));
2886 }
2887
Boojin Kim1b9bb712011-09-02 09:44:30 +09002888 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002889 desc->rqcfg.brst_len = 1;
2890 }
2891
2892 /* Return the last desc in the chain */
2893 desc->txd.flags = flg;
2894 return &desc->txd;
2895}
2896
2897static irqreturn_t pl330_irq_handler(int irq, void *data)
2898{
2899 if (pl330_update(data))
2900 return IRQ_HANDLED;
2901 else
2902 return IRQ_NONE;
2903}
2904
2905static int __devinit
Russell Kingaa25afa2011-02-19 15:55:00 +00002906pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002907{
2908 struct dma_pl330_platdata *pdat;
2909 struct dma_pl330_dmac *pdmac;
2910 struct dma_pl330_chan *pch;
2911 struct pl330_info *pi;
2912 struct dma_device *pd;
2913 struct resource *res;
2914 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002915 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002916
2917 pdat = adev->dev.platform_data;
2918
Jassi Brarb3040e42010-05-23 20:28:19 -07002919 /* Allocate a new DMAC and its Channels */
Rob Herring4e0e6102011-07-25 16:05:04 -05002920 pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002921 if (!pdmac) {
2922 dev_err(&adev->dev, "unable to allocate mem\n");
2923 return -ENOMEM;
2924 }
2925
2926 pi = &pdmac->pif;
2927 pi->dev = &adev->dev;
2928 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002929 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002930
2931 res = &adev->res;
2932 request_mem_region(res->start, resource_size(res), "dma-pl330");
2933
2934 pi->base = ioremap(res->start, resource_size(res));
2935 if (!pi->base) {
2936 ret = -ENXIO;
2937 goto probe_err1;
2938 }
2939
Boojin Kima2f52032011-09-02 09:44:29 +09002940 pdmac->clk = clk_get(&adev->dev, "dma");
2941 if (IS_ERR(pdmac->clk)) {
2942 dev_err(&adev->dev, "Cannot get operation clock.\n");
2943 ret = -EINVAL;
Julia Lawall7bec78e2012-01-12 10:55:06 +01002944 goto probe_err2;
Boojin Kima2f52032011-09-02 09:44:29 +09002945 }
2946
2947 amba_set_drvdata(adev, pdmac);
2948
Tushar Behera3506c0d2011-12-06 16:15:54 +05302949#ifndef CONFIG_PM_RUNTIME
Boojin Kima2f52032011-09-02 09:44:29 +09002950 /* enable dma clk */
2951 clk_enable(pdmac->clk);
2952#endif
2953
Jassi Brarb3040e42010-05-23 20:28:19 -07002954 irq = adev->irq[0];
2955 ret = request_irq(irq, pl330_irq_handler, 0,
2956 dev_name(&adev->dev), pi);
2957 if (ret)
Julia Lawall7bec78e2012-01-12 10:55:06 +01002958 goto probe_err3;
Jassi Brarb3040e42010-05-23 20:28:19 -07002959
2960 ret = pl330_add(pi);
2961 if (ret)
Julia Lawall7bec78e2012-01-12 10:55:06 +01002962 goto probe_err4;
Jassi Brarb3040e42010-05-23 20:28:19 -07002963
2964 INIT_LIST_HEAD(&pdmac->desc_pool);
2965 spin_lock_init(&pdmac->pool_lock);
2966
2967 /* Create a descriptor pool of default size */
2968 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2969 dev_warn(&adev->dev, "unable to allocate desc\n");
2970
2971 pd = &pdmac->ddma;
2972 INIT_LIST_HEAD(&pd->channels);
2973
2974 /* Initialize channel parameters */
Thomas Abraham93ed5542011-10-24 11:43:31 +02002975 num_chan = max(pdat ? pdat->nr_valid_peri : (u8)pi->pcfg.num_peri,
2976 (u8)pi->pcfg.num_chan);
Rob Herring4e0e6102011-07-25 16:05:04 -05002977 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002978
Rob Herring4e0e6102011-07-25 16:05:04 -05002979 for (i = 0; i < num_chan; i++) {
2980 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002981 if (!adev->dev.of_node)
2982 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2983 else
2984 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002985
2986 INIT_LIST_HEAD(&pch->work_list);
2987 spin_lock_init(&pch->lock);
2988 pch->pl330_chid = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002989 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07002990 pch->dmac = pdmac;
2991
2992 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002993 list_add_tail(&pch->chan.device_node, &pd->channels);
2994 }
2995
2996 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002997 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002998 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002999 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02003000 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02003001 if (pi->pcfg.num_peri) {
3002 dma_cap_set(DMA_SLAVE, pd->cap_mask);
3003 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
3004 }
3005 }
Jassi Brarb3040e42010-05-23 20:28:19 -07003006
3007 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
3008 pd->device_free_chan_resources = pl330_free_chan_resources;
3009 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09003010 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07003011 pd->device_tx_status = pl330_tx_status;
3012 pd->device_prep_slave_sg = pl330_prep_slave_sg;
3013 pd->device_control = pl330_control;
3014 pd->device_issue_pending = pl330_issue_pending;
3015
3016 ret = dma_async_device_register(pd);
3017 if (ret) {
3018 dev_err(&adev->dev, "unable to register DMAC\n");
Julia Lawall7bec78e2012-01-12 10:55:06 +01003019 goto probe_err5;
Jassi Brarb3040e42010-05-23 20:28:19 -07003020 }
3021
Jassi Brarb3040e42010-05-23 20:28:19 -07003022 dev_info(&adev->dev,
3023 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
3024 dev_info(&adev->dev,
3025 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
3026 pi->pcfg.data_buf_dep,
3027 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
3028 pi->pcfg.num_peri, pi->pcfg.num_events);
3029
3030 return 0;
3031
Julia Lawall7bec78e2012-01-12 10:55:06 +01003032probe_err5:
Jassi Brarb3040e42010-05-23 20:28:19 -07003033 pl330_del(pi);
Julia Lawall7bec78e2012-01-12 10:55:06 +01003034probe_err4:
Jassi Brarb3040e42010-05-23 20:28:19 -07003035 free_irq(irq, pi);
Julia Lawall7bec78e2012-01-12 10:55:06 +01003036probe_err3:
3037#ifndef CONFIG_PM_RUNTIME
3038 clk_disable(pdmac->clk);
3039#endif
3040 clk_put(pdmac->clk);
Jassi Brarb3040e42010-05-23 20:28:19 -07003041probe_err2:
3042 iounmap(pi->base);
3043probe_err1:
3044 release_mem_region(res->start, resource_size(res));
3045 kfree(pdmac);
3046
3047 return ret;
3048}
3049
3050static int __devexit pl330_remove(struct amba_device *adev)
3051{
3052 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
3053 struct dma_pl330_chan *pch, *_p;
3054 struct pl330_info *pi;
3055 struct resource *res;
3056 int irq;
3057
3058 if (!pdmac)
3059 return 0;
3060
3061 amba_set_drvdata(adev, NULL);
3062
3063 /* Idle the DMAC */
3064 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3065 chan.device_node) {
3066
3067 /* Remove the channel */
3068 list_del(&pch->chan.device_node);
3069
3070 /* Flush the channel */
3071 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3072 pl330_free_chan_resources(&pch->chan);
3073 }
3074
3075 pi = &pdmac->pif;
3076
3077 pl330_del(pi);
3078
3079 irq = adev->irq[0];
3080 free_irq(irq, pi);
3081
3082 iounmap(pi->base);
3083
3084 res = &adev->res;
3085 release_mem_region(res->start, resource_size(res));
3086
Tushar Behera3506c0d2011-12-06 16:15:54 +05303087#ifndef CONFIG_PM_RUNTIME
Boojin Kima2f52032011-09-02 09:44:29 +09003088 clk_disable(pdmac->clk);
3089#endif
3090
Jassi Brarb3040e42010-05-23 20:28:19 -07003091 kfree(pdmac);
3092
3093 return 0;
3094}
3095
3096static struct amba_id pl330_ids[] = {
3097 {
3098 .id = 0x00041330,
3099 .mask = 0x000fffff,
3100 },
3101 { 0, 0 },
3102};
3103
Dave Martine8fa5162011-10-05 15:15:20 +01003104MODULE_DEVICE_TABLE(amba, pl330_ids);
3105
Boojin Kima2f52032011-09-02 09:44:29 +09003106#ifdef CONFIG_PM_RUNTIME
3107static int pl330_runtime_suspend(struct device *dev)
3108{
3109 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
3110
3111 if (!pdmac) {
3112 dev_err(dev, "failed to get dmac\n");
3113 return -ENODEV;
3114 }
3115
3116 clk_disable(pdmac->clk);
3117
3118 return 0;
3119}
3120
3121static int pl330_runtime_resume(struct device *dev)
3122{
3123 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
3124
3125 if (!pdmac) {
3126 dev_err(dev, "failed to get dmac\n");
3127 return -ENODEV;
3128 }
3129
3130 clk_enable(pdmac->clk);
3131
3132 return 0;
3133}
3134#else
3135#define pl330_runtime_suspend NULL
3136#define pl330_runtime_resume NULL
3137#endif /* CONFIG_PM_RUNTIME */
3138
3139static const struct dev_pm_ops pl330_pm_ops = {
3140 .runtime_suspend = pl330_runtime_suspend,
3141 .runtime_resume = pl330_runtime_resume,
3142};
3143
Jassi Brarb3040e42010-05-23 20:28:19 -07003144static struct amba_driver pl330_driver = {
3145 .drv = {
3146 .owner = THIS_MODULE,
3147 .name = "dma-pl330",
Boojin Kima2f52032011-09-02 09:44:29 +09003148 .pm = &pl330_pm_ops,
Jassi Brarb3040e42010-05-23 20:28:19 -07003149 },
3150 .id_table = pl330_ids,
3151 .probe = pl330_probe,
3152 .remove = pl330_remove,
3153};
3154
3155static int __init pl330_init(void)
3156{
3157 return amba_driver_register(&pl330_driver);
3158}
3159module_init(pl330_init);
3160
3161static void __exit pl330_exit(void)
3162{
3163 amba_driver_unregister(&pl330_driver);
3164 return;
3165}
3166module_exit(pl330_exit);
3167
3168MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3169MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3170MODULE_LICENSE("GPL");