blob: dd74c2478c6d1d4dfcc33ff69ab240d9c75ca422 [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
Boojin Kimb7d861d2011-12-26 18:49:52 +09001827/* Reserve an event */
1828static inline int _alloc_event(struct pl330_thread *thrd)
1829{
1830 struct pl330_dmac *pl330 = thrd->dmac;
1831 struct pl330_info *pi = pl330->pinfo;
1832 int ev;
1833
1834 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1835 if (pl330->events[ev] == -1) {
1836 pl330->events[ev] = thrd->id;
1837 return ev;
1838 }
1839
1840 return -1;
1841}
1842
1843static bool _chan_ns(const struct pl330_info *pi, int i)
1844{
1845 return pi->pcfg.irq_ns & (1 << i);
1846}
1847
1848/* Upon success, returns IdentityToken for the
1849 * allocated channel, NULL otherwise.
1850 */
1851static void *pl330_request_channel(const struct pl330_info *pi)
1852{
1853 struct pl330_thread *thrd = NULL;
1854 struct pl330_dmac *pl330;
1855 unsigned long flags;
1856 int chans, i;
1857
1858 if (!pi || !pi->pl330_data)
1859 return NULL;
1860
1861 pl330 = pi->pl330_data;
1862
1863 if (pl330->state == DYING)
1864 return NULL;
1865
1866 chans = pi->pcfg.num_chan;
1867
1868 spin_lock_irqsave(&pl330->lock, flags);
1869
1870 for (i = 0; i < chans; i++) {
1871 thrd = &pl330->channels[i];
1872 if ((thrd->free) && (!_manager_ns(thrd) ||
1873 _chan_ns(pi, i))) {
1874 thrd->ev = _alloc_event(thrd);
1875 if (thrd->ev >= 0) {
1876 thrd->free = false;
1877 thrd->lstenq = 1;
1878 thrd->req[0].r = NULL;
1879 mark_free(thrd, 0);
1880 thrd->req[1].r = NULL;
1881 mark_free(thrd, 1);
1882 break;
1883 }
1884 }
1885 thrd = NULL;
1886 }
1887
1888 spin_unlock_irqrestore(&pl330->lock, flags);
1889
1890 return thrd;
1891}
1892
1893/* Release an event */
1894static inline void _free_event(struct pl330_thread *thrd, int ev)
1895{
1896 struct pl330_dmac *pl330 = thrd->dmac;
1897 struct pl330_info *pi = pl330->pinfo;
1898
1899 /* If the event is valid and was held by the thread */
1900 if (ev >= 0 && ev < pi->pcfg.num_events
1901 && pl330->events[ev] == thrd->id)
1902 pl330->events[ev] = -1;
1903}
1904
1905static void pl330_release_channel(void *ch_id)
1906{
1907 struct pl330_thread *thrd = ch_id;
1908 struct pl330_dmac *pl330;
1909 unsigned long flags;
1910
1911 if (!thrd || thrd->free)
1912 return;
1913
1914 _stop(thrd);
1915
1916 _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1917 _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1918
1919 pl330 = thrd->dmac;
1920
1921 spin_lock_irqsave(&pl330->lock, flags);
1922 _free_event(thrd, thrd->ev);
1923 thrd->free = true;
1924 spin_unlock_irqrestore(&pl330->lock, flags);
1925}
1926
1927/* Initialize the structure for PL330 configuration, that can be used
1928 * by the client driver the make best use of the DMAC
1929 */
1930static void read_dmac_config(struct pl330_info *pi)
1931{
1932 void __iomem *regs = pi->base;
1933 u32 val;
1934
1935 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1936 val &= CRD_DATA_WIDTH_MASK;
1937 pi->pcfg.data_bus_width = 8 * (1 << val);
1938
1939 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1940 val &= CRD_DATA_BUFF_MASK;
1941 pi->pcfg.data_buf_dep = val + 1;
1942
1943 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1944 val &= CR0_NUM_CHANS_MASK;
1945 val += 1;
1946 pi->pcfg.num_chan = val;
1947
1948 val = readl(regs + CR0);
1949 if (val & CR0_PERIPH_REQ_SET) {
1950 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1951 val += 1;
1952 pi->pcfg.num_peri = val;
1953 pi->pcfg.peri_ns = readl(regs + CR4);
1954 } else {
1955 pi->pcfg.num_peri = 0;
1956 }
1957
1958 val = readl(regs + CR0);
1959 if (val & CR0_BOOT_MAN_NS)
1960 pi->pcfg.mode |= DMAC_MODE_NS;
1961 else
1962 pi->pcfg.mode &= ~DMAC_MODE_NS;
1963
1964 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1965 val &= CR0_NUM_EVENTS_MASK;
1966 val += 1;
1967 pi->pcfg.num_events = val;
1968
1969 pi->pcfg.irq_ns = readl(regs + CR3);
1970
1971 pi->pcfg.periph_id = get_id(pi, PERIPH_ID);
1972 pi->pcfg.pcell_id = get_id(pi, PCELL_ID);
1973}
1974
1975static inline void _reset_thread(struct pl330_thread *thrd)
1976{
1977 struct pl330_dmac *pl330 = thrd->dmac;
1978 struct pl330_info *pi = pl330->pinfo;
1979
1980 thrd->req[0].mc_cpu = pl330->mcode_cpu
1981 + (thrd->id * pi->mcbufsz);
1982 thrd->req[0].mc_bus = pl330->mcode_bus
1983 + (thrd->id * pi->mcbufsz);
1984 thrd->req[0].r = NULL;
1985 mark_free(thrd, 0);
1986
1987 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1988 + pi->mcbufsz / 2;
1989 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1990 + pi->mcbufsz / 2;
1991 thrd->req[1].r = NULL;
1992 mark_free(thrd, 1);
1993}
1994
1995static int dmac_alloc_threads(struct pl330_dmac *pl330)
1996{
1997 struct pl330_info *pi = pl330->pinfo;
1998 int chans = pi->pcfg.num_chan;
1999 struct pl330_thread *thrd;
2000 int i;
2001
2002 /* Allocate 1 Manager and 'chans' Channel threads */
2003 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
2004 GFP_KERNEL);
2005 if (!pl330->channels)
2006 return -ENOMEM;
2007
2008 /* Init Channel threads */
2009 for (i = 0; i < chans; i++) {
2010 thrd = &pl330->channels[i];
2011 thrd->id = i;
2012 thrd->dmac = pl330;
2013 _reset_thread(thrd);
2014 thrd->free = true;
2015 }
2016
2017 /* MANAGER is indexed at the end */
2018 thrd = &pl330->channels[chans];
2019 thrd->id = chans;
2020 thrd->dmac = pl330;
2021 thrd->free = false;
2022 pl330->manager = thrd;
2023
2024 return 0;
2025}
2026
2027static int dmac_alloc_resources(struct pl330_dmac *pl330)
2028{
2029 struct pl330_info *pi = pl330->pinfo;
2030 int chans = pi->pcfg.num_chan;
2031 int ret;
2032
2033 /*
2034 * Alloc MicroCode buffer for 'chans' Channel threads.
2035 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
2036 */
2037 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
2038 chans * pi->mcbufsz,
2039 &pl330->mcode_bus, GFP_KERNEL);
2040 if (!pl330->mcode_cpu) {
2041 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2042 __func__, __LINE__);
2043 return -ENOMEM;
2044 }
2045
2046 ret = dmac_alloc_threads(pl330);
2047 if (ret) {
2048 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
2049 __func__, __LINE__);
2050 dma_free_coherent(pi->dev,
2051 chans * pi->mcbufsz,
2052 pl330->mcode_cpu, pl330->mcode_bus);
2053 return ret;
2054 }
2055
2056 return 0;
2057}
2058
2059static int pl330_add(struct pl330_info *pi)
2060{
2061 struct pl330_dmac *pl330;
2062 void __iomem *regs;
2063 int i, ret;
2064
2065 if (!pi || !pi->dev)
2066 return -EINVAL;
2067
2068 /* If already added */
2069 if (pi->pl330_data)
2070 return -EINVAL;
2071
2072 /*
2073 * If the SoC can perform reset on the DMAC, then do it
2074 * before reading its configuration.
2075 */
2076 if (pi->dmac_reset)
2077 pi->dmac_reset(pi);
2078
2079 regs = pi->base;
2080
2081 /* Check if we can handle this DMAC */
2082 if ((get_id(pi, PERIPH_ID) & 0xfffff) != PERIPH_ID_VAL
2083 || get_id(pi, PCELL_ID) != PCELL_ID_VAL) {
2084 dev_err(pi->dev, "PERIPH_ID 0x%x, PCELL_ID 0x%x !\n",
2085 get_id(pi, PERIPH_ID), get_id(pi, PCELL_ID));
2086 return -EINVAL;
2087 }
2088
2089 /* Read the configuration of the DMAC */
2090 read_dmac_config(pi);
2091
2092 if (pi->pcfg.num_events == 0) {
2093 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2094 __func__, __LINE__);
2095 return -EINVAL;
2096 }
2097
2098 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2099 if (!pl330) {
2100 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2101 __func__, __LINE__);
2102 return -ENOMEM;
2103 }
2104
2105 /* Assign the info structure and private data */
2106 pl330->pinfo = pi;
2107 pi->pl330_data = pl330;
2108
2109 spin_lock_init(&pl330->lock);
2110
2111 INIT_LIST_HEAD(&pl330->req_done);
2112
2113 /* Use default MC buffer size if not provided */
2114 if (!pi->mcbufsz)
2115 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2116
2117 /* Mark all events as free */
2118 for (i = 0; i < pi->pcfg.num_events; i++)
2119 pl330->events[i] = -1;
2120
2121 /* Allocate resources needed by the DMAC */
2122 ret = dmac_alloc_resources(pl330);
2123 if (ret) {
2124 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2125 kfree(pl330);
2126 return ret;
2127 }
2128
2129 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2130
2131 pl330->state = INIT;
2132
2133 return 0;
2134}
2135
2136static int dmac_free_threads(struct pl330_dmac *pl330)
2137{
2138 struct pl330_info *pi = pl330->pinfo;
2139 int chans = pi->pcfg.num_chan;
2140 struct pl330_thread *thrd;
2141 int i;
2142
2143 /* Release Channel threads */
2144 for (i = 0; i < chans; i++) {
2145 thrd = &pl330->channels[i];
2146 pl330_release_channel((void *)thrd);
2147 }
2148
2149 /* Free memory */
2150 kfree(pl330->channels);
2151
2152 return 0;
2153}
2154
2155static void dmac_free_resources(struct pl330_dmac *pl330)
2156{
2157 struct pl330_info *pi = pl330->pinfo;
2158 int chans = pi->pcfg.num_chan;
2159
2160 dmac_free_threads(pl330);
2161
2162 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2163 pl330->mcode_cpu, pl330->mcode_bus);
2164}
2165
2166static void pl330_del(struct pl330_info *pi)
2167{
2168 struct pl330_dmac *pl330;
2169
2170 if (!pi || !pi->pl330_data)
2171 return;
2172
2173 pl330 = pi->pl330_data;
2174
2175 pl330->state = UNINIT;
2176
2177 tasklet_kill(&pl330->tasks);
2178
2179 /* Free DMAC resources */
2180 dmac_free_resources(pl330);
2181
2182 kfree(pl330);
2183 pi->pl330_data = NULL;
2184}
2185
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002186/* forward declaration */
2187static struct amba_driver pl330_driver;
2188
Jassi Brarb3040e42010-05-23 20:28:19 -07002189static inline struct dma_pl330_chan *
2190to_pchan(struct dma_chan *ch)
2191{
2192 if (!ch)
2193 return NULL;
2194
2195 return container_of(ch, struct dma_pl330_chan, chan);
2196}
2197
2198static inline struct dma_pl330_desc *
2199to_desc(struct dma_async_tx_descriptor *tx)
2200{
2201 return container_of(tx, struct dma_pl330_desc, txd);
2202}
2203
2204static inline void free_desc_list(struct list_head *list)
2205{
2206 struct dma_pl330_dmac *pdmac;
2207 struct dma_pl330_desc *desc;
2208 struct dma_pl330_chan *pch;
2209 unsigned long flags;
2210
2211 if (list_empty(list))
2212 return;
2213
2214 /* Finish off the work list */
2215 list_for_each_entry(desc, list, node) {
2216 dma_async_tx_callback callback;
2217 void *param;
2218
2219 /* All desc in a list belong to same channel */
2220 pch = desc->pchan;
2221 callback = desc->txd.callback;
2222 param = desc->txd.callback_param;
2223
2224 if (callback)
2225 callback(param);
2226
2227 desc->pchan = NULL;
2228 }
2229
2230 pdmac = pch->dmac;
2231
2232 spin_lock_irqsave(&pdmac->pool_lock, flags);
2233 list_splice_tail_init(list, &pdmac->desc_pool);
2234 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2235}
2236
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002237static inline void handle_cyclic_desc_list(struct list_head *list)
2238{
2239 struct dma_pl330_desc *desc;
2240 struct dma_pl330_chan *pch;
2241 unsigned long flags;
2242
2243 if (list_empty(list))
2244 return;
2245
2246 list_for_each_entry(desc, list, node) {
2247 dma_async_tx_callback callback;
2248
2249 /* Change status to reload it */
2250 desc->status = PREP;
2251 pch = desc->pchan;
2252 callback = desc->txd.callback;
2253 if (callback)
2254 callback(desc->txd.callback_param);
2255 }
2256
2257 spin_lock_irqsave(&pch->lock, flags);
2258 list_splice_tail_init(list, &pch->work_list);
2259 spin_unlock_irqrestore(&pch->lock, flags);
2260}
2261
Jassi Brarb3040e42010-05-23 20:28:19 -07002262static inline void fill_queue(struct dma_pl330_chan *pch)
2263{
2264 struct dma_pl330_desc *desc;
2265 int ret;
2266
2267 list_for_each_entry(desc, &pch->work_list, node) {
2268
2269 /* If already submitted */
2270 if (desc->status == BUSY)
2271 break;
2272
2273 ret = pl330_submit_req(pch->pl330_chid,
2274 &desc->req);
2275 if (!ret) {
2276 desc->status = BUSY;
2277 break;
2278 } else if (ret == -EAGAIN) {
2279 /* QFull or DMAC Dying */
2280 break;
2281 } else {
2282 /* Unacceptable request */
2283 desc->status = DONE;
2284 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2285 __func__, __LINE__, desc->txd.cookie);
2286 tasklet_schedule(&pch->task);
2287 }
2288 }
2289}
2290
2291static void pl330_tasklet(unsigned long data)
2292{
2293 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2294 struct dma_pl330_desc *desc, *_dt;
2295 unsigned long flags;
2296 LIST_HEAD(list);
2297
2298 spin_lock_irqsave(&pch->lock, flags);
2299
2300 /* Pick up ripe tomatoes */
2301 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2302 if (desc->status == DONE) {
2303 pch->completed = desc->txd.cookie;
2304 list_move_tail(&desc->node, &list);
2305 }
2306
2307 /* Try to submit a req imm. next to the last completed cookie */
2308 fill_queue(pch);
2309
2310 /* Make sure the PL330 Channel thread is active */
2311 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
2312
2313 spin_unlock_irqrestore(&pch->lock, flags);
2314
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002315 if (pch->cyclic)
2316 handle_cyclic_desc_list(&list);
2317 else
2318 free_desc_list(&list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002319}
2320
2321static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
2322{
2323 struct dma_pl330_desc *desc = token;
2324 struct dma_pl330_chan *pch = desc->pchan;
2325 unsigned long flags;
2326
2327 /* If desc aborted */
2328 if (!pch)
2329 return;
2330
2331 spin_lock_irqsave(&pch->lock, flags);
2332
2333 desc->status = DONE;
2334
2335 spin_unlock_irqrestore(&pch->lock, flags);
2336
2337 tasklet_schedule(&pch->task);
2338}
2339
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002340bool pl330_filter(struct dma_chan *chan, void *param)
2341{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002342 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002343
2344 if (chan->device->dev->driver != &pl330_driver.drv)
2345 return false;
2346
Thomas Abraham93ed5542011-10-24 11:43:31 +02002347#ifdef CONFIG_OF
2348 if (chan->device->dev->of_node) {
2349 const __be32 *prop_value;
2350 phandle phandle;
2351 struct device_node *node;
2352
2353 prop_value = ((struct property *)param)->value;
2354 phandle = be32_to_cpup(prop_value++);
2355 node = of_find_node_by_phandle(phandle);
2356 return ((chan->private == node) &&
2357 (chan->chan_id == be32_to_cpup(prop_value)));
2358 }
2359#endif
2360
Thomas Abrahamcd072512011-10-24 11:43:11 +02002361 peri_id = chan->private;
2362 return *peri_id == (unsigned)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002363}
2364EXPORT_SYMBOL(pl330_filter);
2365
Jassi Brarb3040e42010-05-23 20:28:19 -07002366static int pl330_alloc_chan_resources(struct dma_chan *chan)
2367{
2368 struct dma_pl330_chan *pch = to_pchan(chan);
2369 struct dma_pl330_dmac *pdmac = pch->dmac;
2370 unsigned long flags;
2371
2372 spin_lock_irqsave(&pch->lock, flags);
2373
2374 pch->completed = chan->cookie = 1;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002375 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002376
2377 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
2378 if (!pch->pl330_chid) {
2379 spin_unlock_irqrestore(&pch->lock, flags);
2380 return 0;
2381 }
2382
2383 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2384
2385 spin_unlock_irqrestore(&pch->lock, flags);
2386
2387 return 1;
2388}
2389
2390static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2391{
2392 struct dma_pl330_chan *pch = to_pchan(chan);
Boojin Kimae43b882011-09-02 09:44:32 +09002393 struct dma_pl330_desc *desc, *_dt;
Jassi Brarb3040e42010-05-23 20:28:19 -07002394 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002395 struct dma_pl330_dmac *pdmac = pch->dmac;
2396 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002397 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002398
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002399 switch (cmd) {
2400 case DMA_TERMINATE_ALL:
2401 spin_lock_irqsave(&pch->lock, flags);
2402
2403 /* FLUSH the PL330 Channel thread */
2404 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
2405
2406 /* Mark all desc done */
Boojin Kimae43b882011-09-02 09:44:32 +09002407 list_for_each_entry_safe(desc, _dt, &pch->work_list , node) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002408 desc->status = DONE;
Boojin Kimae43b882011-09-02 09:44:32 +09002409 pch->completed = desc->txd.cookie;
2410 list_move_tail(&desc->node, &list);
2411 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002412
Boojin Kimae43b882011-09-02 09:44:32 +09002413 list_splice_tail_init(&list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002414 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002415 break;
2416 case DMA_SLAVE_CONFIG:
2417 slave_config = (struct dma_slave_config *)arg;
2418
Vinod Kouldb8196d2011-10-13 22:34:23 +05302419 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002420 if (slave_config->dst_addr)
2421 pch->fifo_addr = slave_config->dst_addr;
2422 if (slave_config->dst_addr_width)
2423 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2424 if (slave_config->dst_maxburst)
2425 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302426 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002427 if (slave_config->src_addr)
2428 pch->fifo_addr = slave_config->src_addr;
2429 if (slave_config->src_addr_width)
2430 pch->burst_sz = __ffs(slave_config->src_addr_width);
2431 if (slave_config->src_maxburst)
2432 pch->burst_len = slave_config->src_maxburst;
2433 }
2434 break;
2435 default:
2436 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002437 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002438 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002439
2440 return 0;
2441}
2442
2443static void pl330_free_chan_resources(struct dma_chan *chan)
2444{
2445 struct dma_pl330_chan *pch = to_pchan(chan);
2446 unsigned long flags;
2447
2448 spin_lock_irqsave(&pch->lock, flags);
2449
2450 tasklet_kill(&pch->task);
2451
2452 pl330_release_channel(pch->pl330_chid);
2453 pch->pl330_chid = NULL;
2454
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002455 if (pch->cyclic)
2456 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2457
Jassi Brarb3040e42010-05-23 20:28:19 -07002458 spin_unlock_irqrestore(&pch->lock, flags);
2459}
2460
2461static enum dma_status
2462pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2463 struct dma_tx_state *txstate)
2464{
2465 struct dma_pl330_chan *pch = to_pchan(chan);
2466 dma_cookie_t last_done, last_used;
2467 int ret;
2468
2469 last_done = pch->completed;
2470 last_used = chan->cookie;
2471
2472 ret = dma_async_is_complete(cookie, last_done, last_used);
2473
2474 dma_set_tx_state(txstate, last_done, last_used, 0);
2475
2476 return ret;
2477}
2478
2479static void pl330_issue_pending(struct dma_chan *chan)
2480{
2481 pl330_tasklet((unsigned long) to_pchan(chan));
2482}
2483
2484/*
2485 * We returned the last one of the circular list of descriptor(s)
2486 * from prep_xxx, so the argument to submit corresponds to the last
2487 * descriptor of the list.
2488 */
2489static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2490{
2491 struct dma_pl330_desc *desc, *last = to_desc(tx);
2492 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2493 dma_cookie_t cookie;
2494 unsigned long flags;
2495
2496 spin_lock_irqsave(&pch->lock, flags);
2497
2498 /* Assign cookies to all nodes */
2499 cookie = tx->chan->cookie;
2500
2501 while (!list_empty(&last->node)) {
2502 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
2503
2504 if (++cookie < 0)
2505 cookie = 1;
2506 desc->txd.cookie = cookie;
2507
2508 list_move_tail(&desc->node, &pch->work_list);
2509 }
2510
2511 if (++cookie < 0)
2512 cookie = 1;
2513 last->txd.cookie = cookie;
2514
2515 list_add_tail(&last->node, &pch->work_list);
2516
2517 tx->chan->cookie = cookie;
2518
2519 spin_unlock_irqrestore(&pch->lock, flags);
2520
2521 return cookie;
2522}
2523
2524static inline void _init_desc(struct dma_pl330_desc *desc)
2525{
2526 desc->pchan = NULL;
2527 desc->req.x = &desc->px;
2528 desc->req.token = desc;
2529 desc->rqcfg.swap = SWAP_NO;
2530 desc->rqcfg.privileged = 0;
2531 desc->rqcfg.insnaccess = 0;
2532 desc->rqcfg.scctl = SCCTRL0;
2533 desc->rqcfg.dcctl = DCCTRL0;
2534 desc->req.cfg = &desc->rqcfg;
2535 desc->req.xfer_cb = dma_pl330_rqcb;
2536 desc->txd.tx_submit = pl330_tx_submit;
2537
2538 INIT_LIST_HEAD(&desc->node);
2539}
2540
2541/* Returns the number of descriptors added to the DMAC pool */
2542int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
2543{
2544 struct dma_pl330_desc *desc;
2545 unsigned long flags;
2546 int i;
2547
2548 if (!pdmac)
2549 return 0;
2550
2551 desc = kmalloc(count * sizeof(*desc), flg);
2552 if (!desc)
2553 return 0;
2554
2555 spin_lock_irqsave(&pdmac->pool_lock, flags);
2556
2557 for (i = 0; i < count; i++) {
2558 _init_desc(&desc[i]);
2559 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2560 }
2561
2562 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2563
2564 return count;
2565}
2566
2567static struct dma_pl330_desc *
2568pluck_desc(struct dma_pl330_dmac *pdmac)
2569{
2570 struct dma_pl330_desc *desc = NULL;
2571 unsigned long flags;
2572
2573 if (!pdmac)
2574 return NULL;
2575
2576 spin_lock_irqsave(&pdmac->pool_lock, flags);
2577
2578 if (!list_empty(&pdmac->desc_pool)) {
2579 desc = list_entry(pdmac->desc_pool.next,
2580 struct dma_pl330_desc, node);
2581
2582 list_del_init(&desc->node);
2583
2584 desc->status = PREP;
2585 desc->txd.callback = NULL;
2586 }
2587
2588 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2589
2590 return desc;
2591}
2592
2593static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2594{
2595 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002596 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002597 struct dma_pl330_desc *desc;
2598
2599 /* Pluck one desc from the pool of DMAC */
2600 desc = pluck_desc(pdmac);
2601
2602 /* If the DMAC pool is empty, alloc new */
2603 if (!desc) {
2604 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2605 return NULL;
2606
2607 /* Try again */
2608 desc = pluck_desc(pdmac);
2609 if (!desc) {
2610 dev_err(pch->dmac->pif.dev,
2611 "%s:%d ALERT!\n", __func__, __LINE__);
2612 return NULL;
2613 }
2614 }
2615
2616 /* Initialize the descriptor */
2617 desc->pchan = pch;
2618 desc->txd.cookie = 0;
2619 async_tx_ack(&desc->txd);
2620
Thomas Abrahamcd072512011-10-24 11:43:11 +02002621 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002622
2623 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2624
2625 return desc;
2626}
2627
2628static inline void fill_px(struct pl330_xfer *px,
2629 dma_addr_t dst, dma_addr_t src, size_t len)
2630{
2631 px->next = NULL;
2632 px->bytes = len;
2633 px->dst_addr = dst;
2634 px->src_addr = src;
2635}
2636
2637static struct dma_pl330_desc *
2638__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2639 dma_addr_t src, size_t len)
2640{
2641 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2642
2643 if (!desc) {
2644 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2645 __func__, __LINE__);
2646 return NULL;
2647 }
2648
2649 /*
2650 * Ideally we should lookout for reqs bigger than
2651 * those that can be programmed with 256 bytes of
2652 * MC buffer, but considering a req size is seldom
2653 * going to be word-unaligned and more than 200MB,
2654 * we take it easy.
2655 * Also, should the limit is reached we'd rather
2656 * have the platform increase MC buffer size than
2657 * complicating this API driver.
2658 */
2659 fill_px(&desc->px, dst, src, len);
2660
2661 return desc;
2662}
2663
2664/* Call after fixing burst size */
2665static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2666{
2667 struct dma_pl330_chan *pch = desc->pchan;
2668 struct pl330_info *pi = &pch->dmac->pif;
2669 int burst_len;
2670
2671 burst_len = pi->pcfg.data_bus_width / 8;
2672 burst_len *= pi->pcfg.data_buf_dep;
2673 burst_len >>= desc->rqcfg.brst_size;
2674
2675 /* src/dst_burst_len can't be more than 16 */
2676 if (burst_len > 16)
2677 burst_len = 16;
2678
2679 while (burst_len > 1) {
2680 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2681 break;
2682 burst_len--;
2683 }
2684
2685 return burst_len;
2686}
2687
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002688static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2689 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302690 size_t period_len, enum dma_transfer_direction direction)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002691{
2692 struct dma_pl330_desc *desc;
2693 struct dma_pl330_chan *pch = to_pchan(chan);
2694 dma_addr_t dst;
2695 dma_addr_t src;
2696
2697 desc = pl330_get_desc(pch);
2698 if (!desc) {
2699 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2700 __func__, __LINE__);
2701 return NULL;
2702 }
2703
2704 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05302705 case DMA_MEM_TO_DEV:
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002706 desc->rqcfg.src_inc = 1;
2707 desc->rqcfg.dst_inc = 0;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002708 desc->req.rqtype = MEMTODEV;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002709 src = dma_addr;
2710 dst = pch->fifo_addr;
2711 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302712 case DMA_DEV_TO_MEM:
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002713 desc->rqcfg.src_inc = 0;
2714 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002715 desc->req.rqtype = DEVTOMEM;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002716 src = pch->fifo_addr;
2717 dst = dma_addr;
2718 break;
2719 default:
2720 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2721 __func__, __LINE__);
2722 return NULL;
2723 }
2724
2725 desc->rqcfg.brst_size = pch->burst_sz;
2726 desc->rqcfg.brst_len = 1;
2727
2728 pch->cyclic = true;
2729
2730 fill_px(&desc->px, dst, src, period_len);
2731
2732 return &desc->txd;
2733}
2734
Jassi Brarb3040e42010-05-23 20:28:19 -07002735static struct dma_async_tx_descriptor *
2736pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2737 dma_addr_t src, size_t len, unsigned long flags)
2738{
2739 struct dma_pl330_desc *desc;
2740 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002741 struct pl330_info *pi;
2742 int burst;
2743
Rob Herring4e0e6102011-07-25 16:05:04 -05002744 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002745 return NULL;
2746
Jassi Brarb3040e42010-05-23 20:28:19 -07002747 pi = &pch->dmac->pif;
2748
2749 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2750 if (!desc)
2751 return NULL;
2752
2753 desc->rqcfg.src_inc = 1;
2754 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002755 desc->req.rqtype = MEMTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002756
2757 /* Select max possible burst size */
2758 burst = pi->pcfg.data_bus_width / 8;
2759
2760 while (burst > 1) {
2761 if (!(len % burst))
2762 break;
2763 burst /= 2;
2764 }
2765
2766 desc->rqcfg.brst_size = 0;
2767 while (burst != (1 << desc->rqcfg.brst_size))
2768 desc->rqcfg.brst_size++;
2769
2770 desc->rqcfg.brst_len = get_burst_len(desc, len);
2771
2772 desc->txd.flags = flags;
2773
2774 return &desc->txd;
2775}
2776
2777static struct dma_async_tx_descriptor *
2778pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302779 unsigned int sg_len, enum dma_transfer_direction direction,
Jassi Brarb3040e42010-05-23 20:28:19 -07002780 unsigned long flg)
2781{
2782 struct dma_pl330_desc *first, *desc = NULL;
2783 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002784 struct scatterlist *sg;
2785 unsigned long flags;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002786 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002787 dma_addr_t addr;
2788
Thomas Abrahamcd072512011-10-24 11:43:11 +02002789 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002790 return NULL;
2791
Boojin Kim1b9bb712011-09-02 09:44:30 +09002792 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002793
2794 first = NULL;
2795
2796 for_each_sg(sgl, sg, sg_len, i) {
2797
2798 desc = pl330_get_desc(pch);
2799 if (!desc) {
2800 struct dma_pl330_dmac *pdmac = pch->dmac;
2801
2802 dev_err(pch->dmac->pif.dev,
2803 "%s:%d Unable to fetch desc\n",
2804 __func__, __LINE__);
2805 if (!first)
2806 return NULL;
2807
2808 spin_lock_irqsave(&pdmac->pool_lock, flags);
2809
2810 while (!list_empty(&first->node)) {
2811 desc = list_entry(first->node.next,
2812 struct dma_pl330_desc, node);
2813 list_move_tail(&desc->node, &pdmac->desc_pool);
2814 }
2815
2816 list_move_tail(&first->node, &pdmac->desc_pool);
2817
2818 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2819
2820 return NULL;
2821 }
2822
2823 if (!first)
2824 first = desc;
2825 else
2826 list_add_tail(&desc->node, &first->node);
2827
Vinod Kouldb8196d2011-10-13 22:34:23 +05302828 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002829 desc->rqcfg.src_inc = 1;
2830 desc->rqcfg.dst_inc = 0;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002831 desc->req.rqtype = MEMTODEV;
Jassi Brarb3040e42010-05-23 20:28:19 -07002832 fill_px(&desc->px,
2833 addr, sg_dma_address(sg), sg_dma_len(sg));
2834 } else {
2835 desc->rqcfg.src_inc = 0;
2836 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002837 desc->req.rqtype = DEVTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002838 fill_px(&desc->px,
2839 sg_dma_address(sg), addr, sg_dma_len(sg));
2840 }
2841
Boojin Kim1b9bb712011-09-02 09:44:30 +09002842 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002843 desc->rqcfg.brst_len = 1;
2844 }
2845
2846 /* Return the last desc in the chain */
2847 desc->txd.flags = flg;
2848 return &desc->txd;
2849}
2850
2851static irqreturn_t pl330_irq_handler(int irq, void *data)
2852{
2853 if (pl330_update(data))
2854 return IRQ_HANDLED;
2855 else
2856 return IRQ_NONE;
2857}
2858
2859static int __devinit
Russell Kingaa25afa2011-02-19 15:55:00 +00002860pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002861{
2862 struct dma_pl330_platdata *pdat;
2863 struct dma_pl330_dmac *pdmac;
2864 struct dma_pl330_chan *pch;
2865 struct pl330_info *pi;
2866 struct dma_device *pd;
2867 struct resource *res;
2868 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002869 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002870
2871 pdat = adev->dev.platform_data;
2872
Jassi Brarb3040e42010-05-23 20:28:19 -07002873 /* Allocate a new DMAC and its Channels */
Rob Herring4e0e6102011-07-25 16:05:04 -05002874 pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002875 if (!pdmac) {
2876 dev_err(&adev->dev, "unable to allocate mem\n");
2877 return -ENOMEM;
2878 }
2879
2880 pi = &pdmac->pif;
2881 pi->dev = &adev->dev;
2882 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002883 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002884
2885 res = &adev->res;
2886 request_mem_region(res->start, resource_size(res), "dma-pl330");
2887
2888 pi->base = ioremap(res->start, resource_size(res));
2889 if (!pi->base) {
2890 ret = -ENXIO;
2891 goto probe_err1;
2892 }
2893
Boojin Kima2f52032011-09-02 09:44:29 +09002894 pdmac->clk = clk_get(&adev->dev, "dma");
2895 if (IS_ERR(pdmac->clk)) {
2896 dev_err(&adev->dev, "Cannot get operation clock.\n");
2897 ret = -EINVAL;
Julia Lawall7bec78e2012-01-12 10:55:06 +01002898 goto probe_err2;
Boojin Kima2f52032011-09-02 09:44:29 +09002899 }
2900
2901 amba_set_drvdata(adev, pdmac);
2902
Tushar Behera3506c0d2011-12-06 16:15:54 +05302903#ifndef CONFIG_PM_RUNTIME
Boojin Kima2f52032011-09-02 09:44:29 +09002904 /* enable dma clk */
2905 clk_enable(pdmac->clk);
2906#endif
2907
Jassi Brarb3040e42010-05-23 20:28:19 -07002908 irq = adev->irq[0];
2909 ret = request_irq(irq, pl330_irq_handler, 0,
2910 dev_name(&adev->dev), pi);
2911 if (ret)
Julia Lawall7bec78e2012-01-12 10:55:06 +01002912 goto probe_err3;
Jassi Brarb3040e42010-05-23 20:28:19 -07002913
2914 ret = pl330_add(pi);
2915 if (ret)
Julia Lawall7bec78e2012-01-12 10:55:06 +01002916 goto probe_err4;
Jassi Brarb3040e42010-05-23 20:28:19 -07002917
2918 INIT_LIST_HEAD(&pdmac->desc_pool);
2919 spin_lock_init(&pdmac->pool_lock);
2920
2921 /* Create a descriptor pool of default size */
2922 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2923 dev_warn(&adev->dev, "unable to allocate desc\n");
2924
2925 pd = &pdmac->ddma;
2926 INIT_LIST_HEAD(&pd->channels);
2927
2928 /* Initialize channel parameters */
Thomas Abraham93ed5542011-10-24 11:43:31 +02002929 num_chan = max(pdat ? pdat->nr_valid_peri : (u8)pi->pcfg.num_peri,
2930 (u8)pi->pcfg.num_chan);
Rob Herring4e0e6102011-07-25 16:05:04 -05002931 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002932
Rob Herring4e0e6102011-07-25 16:05:04 -05002933 for (i = 0; i < num_chan; i++) {
2934 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002935 if (!adev->dev.of_node)
2936 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2937 else
2938 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002939
2940 INIT_LIST_HEAD(&pch->work_list);
2941 spin_lock_init(&pch->lock);
2942 pch->pl330_chid = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002943 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07002944 pch->dmac = pdmac;
2945
2946 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002947 list_add_tail(&pch->chan.device_node, &pd->channels);
2948 }
2949
2950 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002951 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002952 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002953 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002954 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002955 if (pi->pcfg.num_peri) {
2956 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2957 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
2958 }
2959 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002960
2961 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2962 pd->device_free_chan_resources = pl330_free_chan_resources;
2963 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002964 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07002965 pd->device_tx_status = pl330_tx_status;
2966 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2967 pd->device_control = pl330_control;
2968 pd->device_issue_pending = pl330_issue_pending;
2969
2970 ret = dma_async_device_register(pd);
2971 if (ret) {
2972 dev_err(&adev->dev, "unable to register DMAC\n");
Julia Lawall7bec78e2012-01-12 10:55:06 +01002973 goto probe_err5;
Jassi Brarb3040e42010-05-23 20:28:19 -07002974 }
2975
Jassi Brarb3040e42010-05-23 20:28:19 -07002976 dev_info(&adev->dev,
2977 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
2978 dev_info(&adev->dev,
2979 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
2980 pi->pcfg.data_buf_dep,
2981 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
2982 pi->pcfg.num_peri, pi->pcfg.num_events);
2983
2984 return 0;
2985
Julia Lawall7bec78e2012-01-12 10:55:06 +01002986probe_err5:
Jassi Brarb3040e42010-05-23 20:28:19 -07002987 pl330_del(pi);
Julia Lawall7bec78e2012-01-12 10:55:06 +01002988probe_err4:
Jassi Brarb3040e42010-05-23 20:28:19 -07002989 free_irq(irq, pi);
Julia Lawall7bec78e2012-01-12 10:55:06 +01002990probe_err3:
2991#ifndef CONFIG_PM_RUNTIME
2992 clk_disable(pdmac->clk);
2993#endif
2994 clk_put(pdmac->clk);
Jassi Brarb3040e42010-05-23 20:28:19 -07002995probe_err2:
2996 iounmap(pi->base);
2997probe_err1:
2998 release_mem_region(res->start, resource_size(res));
2999 kfree(pdmac);
3000
3001 return ret;
3002}
3003
3004static int __devexit pl330_remove(struct amba_device *adev)
3005{
3006 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
3007 struct dma_pl330_chan *pch, *_p;
3008 struct pl330_info *pi;
3009 struct resource *res;
3010 int irq;
3011
3012 if (!pdmac)
3013 return 0;
3014
3015 amba_set_drvdata(adev, NULL);
3016
3017 /* Idle the DMAC */
3018 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3019 chan.device_node) {
3020
3021 /* Remove the channel */
3022 list_del(&pch->chan.device_node);
3023
3024 /* Flush the channel */
3025 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3026 pl330_free_chan_resources(&pch->chan);
3027 }
3028
3029 pi = &pdmac->pif;
3030
3031 pl330_del(pi);
3032
3033 irq = adev->irq[0];
3034 free_irq(irq, pi);
3035
3036 iounmap(pi->base);
3037
3038 res = &adev->res;
3039 release_mem_region(res->start, resource_size(res));
3040
Tushar Behera3506c0d2011-12-06 16:15:54 +05303041#ifndef CONFIG_PM_RUNTIME
Boojin Kima2f52032011-09-02 09:44:29 +09003042 clk_disable(pdmac->clk);
3043#endif
3044
Jassi Brarb3040e42010-05-23 20:28:19 -07003045 kfree(pdmac);
3046
3047 return 0;
3048}
3049
3050static struct amba_id pl330_ids[] = {
3051 {
3052 .id = 0x00041330,
3053 .mask = 0x000fffff,
3054 },
3055 { 0, 0 },
3056};
3057
Dave Martine8fa5162011-10-05 15:15:20 +01003058MODULE_DEVICE_TABLE(amba, pl330_ids);
3059
Boojin Kima2f52032011-09-02 09:44:29 +09003060#ifdef CONFIG_PM_RUNTIME
3061static int pl330_runtime_suspend(struct device *dev)
3062{
3063 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
3064
3065 if (!pdmac) {
3066 dev_err(dev, "failed to get dmac\n");
3067 return -ENODEV;
3068 }
3069
3070 clk_disable(pdmac->clk);
3071
3072 return 0;
3073}
3074
3075static int pl330_runtime_resume(struct device *dev)
3076{
3077 struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
3078
3079 if (!pdmac) {
3080 dev_err(dev, "failed to get dmac\n");
3081 return -ENODEV;
3082 }
3083
3084 clk_enable(pdmac->clk);
3085
3086 return 0;
3087}
3088#else
3089#define pl330_runtime_suspend NULL
3090#define pl330_runtime_resume NULL
3091#endif /* CONFIG_PM_RUNTIME */
3092
3093static const struct dev_pm_ops pl330_pm_ops = {
3094 .runtime_suspend = pl330_runtime_suspend,
3095 .runtime_resume = pl330_runtime_resume,
3096};
3097
Jassi Brarb3040e42010-05-23 20:28:19 -07003098static struct amba_driver pl330_driver = {
3099 .drv = {
3100 .owner = THIS_MODULE,
3101 .name = "dma-pl330",
Boojin Kima2f52032011-09-02 09:44:29 +09003102 .pm = &pl330_pm_ops,
Jassi Brarb3040e42010-05-23 20:28:19 -07003103 },
3104 .id_table = pl330_ids,
3105 .probe = pl330_probe,
3106 .remove = pl330_remove,
3107};
3108
3109static int __init pl330_init(void)
3110{
3111 return amba_driver_register(&pl330_driver);
3112}
3113module_init(pl330_init);
3114
3115static void __exit pl330_exit(void)
3116{
3117 amba_driver_unregister(&pl330_driver);
3118 return;
3119}
3120module_exit(pl330_exit);
3121
3122MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3123MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3124MODULE_LICENSE("GPL");