blob: 8e018a221f19037fe90f3e0a61f9c1daff4f7d2f [file] [log] [blame]
Boojin Kimb7d861d2011-12-26 18:49:52 +09001/*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
Jassi Brarb3040e42010-05-23 20:28:19 -07004 *
5 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassi.brar@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Boojin Kimb7d861d2011-12-26 18:49:52 +090014#include <linux/kernel.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070015#include <linux/io.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/module.h>
Boojin Kimb7d861d2011-12-26 18:49:52 +090019#include <linux/string.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/dma-mapping.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070023#include <linux/dmaengine.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070024#include <linux/amba/bus.h>
25#include <linux/amba/pl330.h>
Boojin Kim1b9bb712011-09-02 09:44:30 +090026#include <linux/scatterlist.h>
Thomas Abraham93ed5542011-10-24 11:43:31 +020027#include <linux/of.h>
Padmavathi Vennaa80258f2013-02-14 09:10:06 +053028#include <linux/of_dma.h>
Sachin Kamatbcc7fa92013-03-04 14:36:27 +053029#include <linux/err.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070030
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031#include "dmaengine.h"
Boojin Kimb7d861d2011-12-26 18:49:52 +090032#define PL330_MAX_CHAN 8
33#define PL330_MAX_IRQS 32
34#define PL330_MAX_PERI 32
35
36enum pl330_srccachectrl {
37 SCCTRL0, /* Noncacheable and nonbufferable */
38 SCCTRL1, /* Bufferable only */
39 SCCTRL2, /* Cacheable, but do not allocate */
40 SCCTRL3, /* Cacheable and bufferable, but do not allocate */
41 SINVALID1,
42 SINVALID2,
43 SCCTRL6, /* Cacheable write-through, allocate on reads only */
44 SCCTRL7, /* Cacheable write-back, allocate on reads only */
45};
46
47enum pl330_dstcachectrl {
48 DCCTRL0, /* Noncacheable and nonbufferable */
49 DCCTRL1, /* Bufferable only */
50 DCCTRL2, /* Cacheable, but do not allocate */
51 DCCTRL3, /* Cacheable and bufferable, but do not allocate */
Linus Torvaldsef08e782012-03-29 15:34:57 -070052 DINVALID1, /* AWCACHE = 0x1000 */
Boojin Kimb7d861d2011-12-26 18:49:52 +090053 DINVALID2,
54 DCCTRL6, /* Cacheable write-through, allocate on writes only */
55 DCCTRL7, /* Cacheable write-back, allocate on writes only */
56};
57
58enum pl330_byteswap {
59 SWAP_NO,
60 SWAP_2,
61 SWAP_4,
62 SWAP_8,
63 SWAP_16,
64};
65
66enum pl330_reqtype {
67 MEMTOMEM,
68 MEMTODEV,
69 DEVTOMEM,
70 DEVTODEV,
71};
72
73/* Register and Bit field Definitions */
74#define DS 0x0
75#define DS_ST_STOP 0x0
76#define DS_ST_EXEC 0x1
77#define DS_ST_CMISS 0x2
78#define DS_ST_UPDTPC 0x3
79#define DS_ST_WFE 0x4
80#define DS_ST_ATBRR 0x5
81#define DS_ST_QBUSY 0x6
82#define DS_ST_WFP 0x7
83#define DS_ST_KILL 0x8
84#define DS_ST_CMPLT 0x9
85#define DS_ST_FLTCMP 0xe
86#define DS_ST_FAULT 0xf
87
88#define DPC 0x4
89#define INTEN 0x20
90#define ES 0x24
91#define INTSTATUS 0x28
92#define INTCLR 0x2c
93#define FSM 0x30
94#define FSC 0x34
95#define FTM 0x38
96
97#define _FTC 0x40
98#define FTC(n) (_FTC + (n)*0x4)
99
100#define _CS 0x100
101#define CS(n) (_CS + (n)*0x8)
102#define CS_CNS (1 << 21)
103
104#define _CPC 0x104
105#define CPC(n) (_CPC + (n)*0x8)
106
107#define _SA 0x400
108#define SA(n) (_SA + (n)*0x20)
109
110#define _DA 0x404
111#define DA(n) (_DA + (n)*0x20)
112
113#define _CC 0x408
114#define CC(n) (_CC + (n)*0x20)
115
116#define CC_SRCINC (1 << 0)
117#define CC_DSTINC (1 << 14)
118#define CC_SRCPRI (1 << 8)
119#define CC_DSTPRI (1 << 22)
120#define CC_SRCNS (1 << 9)
121#define CC_DSTNS (1 << 23)
122#define CC_SRCIA (1 << 10)
123#define CC_DSTIA (1 << 24)
124#define CC_SRCBRSTLEN_SHFT 4
125#define CC_DSTBRSTLEN_SHFT 18
126#define CC_SRCBRSTSIZE_SHFT 1
127#define CC_DSTBRSTSIZE_SHFT 15
128#define CC_SRCCCTRL_SHFT 11
129#define CC_SRCCCTRL_MASK 0x7
130#define CC_DSTCCTRL_SHFT 25
131#define CC_DRCCCTRL_MASK 0x7
132#define CC_SWAP_SHFT 28
133
134#define _LC0 0x40c
135#define LC0(n) (_LC0 + (n)*0x20)
136
137#define _LC1 0x410
138#define LC1(n) (_LC1 + (n)*0x20)
139
140#define DBGSTATUS 0xd00
141#define DBG_BUSY (1 << 0)
142
143#define DBGCMD 0xd04
144#define DBGINST0 0xd08
145#define DBGINST1 0xd0c
146
147#define CR0 0xe00
148#define CR1 0xe04
149#define CR2 0xe08
150#define CR3 0xe0c
151#define CR4 0xe10
152#define CRD 0xe14
153
154#define PERIPH_ID 0xfe0
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900155#define PERIPH_REV_SHIFT 20
156#define PERIPH_REV_MASK 0xf
157#define PERIPH_REV_R0P0 0
158#define PERIPH_REV_R1P0 1
159#define PERIPH_REV_R1P1 2
Boojin Kimb7d861d2011-12-26 18:49:52 +0900160
161#define CR0_PERIPH_REQ_SET (1 << 0)
162#define CR0_BOOT_EN_SET (1 << 1)
163#define CR0_BOOT_MAN_NS (1 << 2)
164#define CR0_NUM_CHANS_SHIFT 4
165#define CR0_NUM_CHANS_MASK 0x7
166#define CR0_NUM_PERIPH_SHIFT 12
167#define CR0_NUM_PERIPH_MASK 0x1f
168#define CR0_NUM_EVENTS_SHIFT 17
169#define CR0_NUM_EVENTS_MASK 0x1f
170
171#define CR1_ICACHE_LEN_SHIFT 0
172#define CR1_ICACHE_LEN_MASK 0x7
173#define CR1_NUM_ICACHELINES_SHIFT 4
174#define CR1_NUM_ICACHELINES_MASK 0xf
175
176#define CRD_DATA_WIDTH_SHIFT 0
177#define CRD_DATA_WIDTH_MASK 0x7
178#define CRD_WR_CAP_SHIFT 4
179#define CRD_WR_CAP_MASK 0x7
180#define CRD_WR_Q_DEP_SHIFT 8
181#define CRD_WR_Q_DEP_MASK 0xf
182#define CRD_RD_CAP_SHIFT 12
183#define CRD_RD_CAP_MASK 0x7
184#define CRD_RD_Q_DEP_SHIFT 16
185#define CRD_RD_Q_DEP_MASK 0xf
186#define CRD_DATA_BUFF_SHIFT 20
187#define CRD_DATA_BUFF_MASK 0x3ff
188
189#define PART 0x330
190#define DESIGNER 0x41
191#define REVISION 0x0
192#define INTEG_CFG 0x0
193#define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
194
Boojin Kimb7d861d2011-12-26 18:49:52 +0900195#define PL330_STATE_STOPPED (1 << 0)
196#define PL330_STATE_EXECUTING (1 << 1)
197#define PL330_STATE_WFE (1 << 2)
198#define PL330_STATE_FAULTING (1 << 3)
199#define PL330_STATE_COMPLETING (1 << 4)
200#define PL330_STATE_WFP (1 << 5)
201#define PL330_STATE_KILLING (1 << 6)
202#define PL330_STATE_FAULT_COMPLETING (1 << 7)
203#define PL330_STATE_CACHEMISS (1 << 8)
204#define PL330_STATE_UPDTPC (1 << 9)
205#define PL330_STATE_ATBARRIER (1 << 10)
206#define PL330_STATE_QUEUEBUSY (1 << 11)
207#define PL330_STATE_INVALID (1 << 15)
208
209#define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
210 | PL330_STATE_WFE | PL330_STATE_FAULTING)
211
212#define CMD_DMAADDH 0x54
213#define CMD_DMAEND 0x00
214#define CMD_DMAFLUSHP 0x35
215#define CMD_DMAGO 0xa0
216#define CMD_DMALD 0x04
217#define CMD_DMALDP 0x25
218#define CMD_DMALP 0x20
219#define CMD_DMALPEND 0x28
220#define CMD_DMAKILL 0x01
221#define CMD_DMAMOV 0xbc
222#define CMD_DMANOP 0x18
223#define CMD_DMARMB 0x12
224#define CMD_DMASEV 0x34
225#define CMD_DMAST 0x08
226#define CMD_DMASTP 0x29
227#define CMD_DMASTZ 0x0c
228#define CMD_DMAWFE 0x36
229#define CMD_DMAWFP 0x30
230#define CMD_DMAWMB 0x13
231
232#define SZ_DMAADDH 3
233#define SZ_DMAEND 1
234#define SZ_DMAFLUSHP 2
235#define SZ_DMALD 1
236#define SZ_DMALDP 2
237#define SZ_DMALP 2
238#define SZ_DMALPEND 2
239#define SZ_DMAKILL 1
240#define SZ_DMAMOV 6
241#define SZ_DMANOP 1
242#define SZ_DMARMB 1
243#define SZ_DMASEV 2
244#define SZ_DMAST 1
245#define SZ_DMASTP 2
246#define SZ_DMASTZ 1
247#define SZ_DMAWFE 2
248#define SZ_DMAWFP 2
249#define SZ_DMAWMB 1
250#define SZ_DMAGO 6
251
252#define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
253#define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
254
255#define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
256#define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
257
258/*
259 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
260 * at 1byte/burst for P<->M and M<->M respectively.
261 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
262 * should be enough for P<->M and M<->M respectively.
263 */
264#define MCODE_BUFF_PER_REQ 256
265
266/* If the _pl330_req is available to the client */
267#define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
268
269/* Use this _only_ to wait on transient states */
270#define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
271
272#ifdef PL330_DEBUG_MCGEN
273static unsigned cmd_line;
274#define PL330_DBGCMD_DUMP(off, x...) do { \
275 printk("%x:", cmd_line); \
276 printk(x); \
277 cmd_line += off; \
278 } while (0)
279#define PL330_DBGMC_START(addr) (cmd_line = addr)
280#else
281#define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
282#define PL330_DBGMC_START(addr) do {} while (0)
283#endif
284
285/* The number of default descriptors */
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +0000286
Jassi Brarb3040e42010-05-23 20:28:19 -0700287#define NR_DEFAULT_DESC 16
288
Boojin Kimb7d861d2011-12-26 18:49:52 +0900289/* Populated by the PL330 core driver for DMA API driver's info */
290struct pl330_config {
291 u32 periph_id;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900292#define DMAC_MODE_NS (1 << 0)
293 unsigned int mode;
294 unsigned int data_bus_width:10; /* In number of bits */
295 unsigned int data_buf_dep:10;
296 unsigned int num_chan:4;
297 unsigned int num_peri:6;
298 u32 peri_ns;
299 unsigned int num_events:6;
300 u32 irq_ns;
301};
302
303/* Handle to the DMAC provided to the PL330 core */
304struct pl330_info {
305 /* Owning device */
306 struct device *dev;
307 /* Size of MicroCode buffers for each channel. */
308 unsigned mcbufsz;
309 /* ioremap'ed address of PL330 registers. */
310 void __iomem *base;
311 /* Client can freely use it. */
312 void *client_data;
313 /* PL330 core data, Client must not touch it. */
314 void *pl330_data;
315 /* Populated by the PL330 core driver during pl330_add */
316 struct pl330_config pcfg;
317 /*
318 * If the DMAC has some reset mechanism, then the
319 * client may want to provide pointer to the method.
320 */
321 void (*dmac_reset)(struct pl330_info *pi);
322};
323
324/**
325 * Request Configuration.
326 * The PL330 core does not modify this and uses the last
327 * working configuration if the request doesn't provide any.
328 *
329 * The Client may want to provide this info only for the
330 * first request and a request with new settings.
331 */
332struct pl330_reqcfg {
333 /* Address Incrementing */
334 unsigned dst_inc:1;
335 unsigned src_inc:1;
336
337 /*
338 * For now, the SRC & DST protection levels
339 * and burst size/length are assumed same.
340 */
341 bool nonsecure;
342 bool privileged;
343 bool insnaccess;
344 unsigned brst_len:5;
345 unsigned brst_size:3; /* in power of 2 */
346
347 enum pl330_dstcachectrl dcctl;
348 enum pl330_srccachectrl scctl;
349 enum pl330_byteswap swap;
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900350 struct pl330_config *pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900351};
352
353/*
354 * One cycle of DMAC operation.
355 * There may be more than one xfer in a request.
356 */
357struct pl330_xfer {
358 u32 src_addr;
359 u32 dst_addr;
360 /* Size to xfer */
361 u32 bytes;
362 /*
363 * Pointer to next xfer in the list.
364 * The last xfer in the req must point to NULL.
365 */
366 struct pl330_xfer *next;
367};
368
369/* The xfer callbacks are made with one of these arguments. */
370enum pl330_op_err {
371 /* The all xfers in the request were success. */
372 PL330_ERR_NONE,
373 /* If req aborted due to global error. */
374 PL330_ERR_ABORT,
375 /* If req failed due to problem with Channel. */
376 PL330_ERR_FAIL,
377};
378
379/* A request defining Scatter-Gather List ending with NULL xfer. */
380struct pl330_req {
381 enum pl330_reqtype rqtype;
382 /* Index of peripheral for the xfer. */
383 unsigned peri:5;
384 /* Unique token for this xfer, set by the client. */
385 void *token;
386 /* Callback to be called after xfer. */
387 void (*xfer_cb)(void *token, enum pl330_op_err err);
388 /* If NULL, req will be done at last set parameters. */
389 struct pl330_reqcfg *cfg;
390 /* Pointer to first xfer in the request. */
391 struct pl330_xfer *x;
Javi Merinofdec53d2012-06-13 15:07:00 +0100392 /* Hook to attach to DMAC's list of reqs with due callback */
393 struct list_head rqd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900394};
395
396/*
397 * To know the status of the channel and DMAC, the client
398 * provides a pointer to this structure. The PL330 core
399 * fills it with current information.
400 */
401struct pl330_chanstatus {
402 /*
403 * If the DMAC engine halted due to some error,
404 * the client should remove-add DMAC.
405 */
406 bool dmac_halted;
407 /*
408 * If channel is halted due to some error,
409 * the client should ABORT/FLUSH and START the channel.
410 */
411 bool faulting;
412 /* Location of last load */
413 u32 src_addr;
414 /* Location of last store */
415 u32 dst_addr;
416 /*
417 * Pointer to the currently active req, NULL if channel is
418 * inactive, even though the requests may be present.
419 */
420 struct pl330_req *top_req;
421 /* Pointer to req waiting second in the queue if any. */
422 struct pl330_req *wait_req;
423};
424
425enum pl330_chan_op {
426 /* Start the channel */
427 PL330_OP_START,
428 /* Abort the active xfer */
429 PL330_OP_ABORT,
430 /* Stop xfer and flush queue */
431 PL330_OP_FLUSH,
432};
433
434struct _xfer_spec {
435 u32 ccr;
436 struct pl330_req *r;
437 struct pl330_xfer *x;
438};
439
440enum dmamov_dst {
441 SAR = 0,
442 CCR,
443 DAR,
444};
445
446enum pl330_dst {
447 SRC = 0,
448 DST,
449};
450
451enum pl330_cond {
452 SINGLE,
453 BURST,
454 ALWAYS,
455};
456
457struct _pl330_req {
458 u32 mc_bus;
459 void *mc_cpu;
460 /* Number of bytes taken to setup MC for the req */
461 u32 mc_len;
462 struct pl330_req *r;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900463};
464
465/* ToBeDone for tasklet */
466struct _pl330_tbd {
467 bool reset_dmac;
468 bool reset_mngr;
469 u8 reset_chan;
470};
471
472/* A DMAC Thread */
473struct pl330_thread {
474 u8 id;
475 int ev;
476 /* If the channel is not yet acquired by any client */
477 bool free;
478 /* Parent DMAC */
479 struct pl330_dmac *dmac;
480 /* Only two at a time */
481 struct _pl330_req req[2];
482 /* Index of the last enqueued request */
483 unsigned lstenq;
484 /* Index of the last submitted request or -1 if the DMA is stopped */
485 int req_running;
486};
487
488enum pl330_dmac_state {
489 UNINIT,
490 INIT,
491 DYING,
492};
493
494/* A DMAC */
495struct pl330_dmac {
496 spinlock_t lock;
497 /* Holds list of reqs with due callbacks */
498 struct list_head req_done;
499 /* Pointer to platform specific stuff */
500 struct pl330_info *pinfo;
501 /* Maximum possible events/irqs */
502 int events[32];
503 /* BUS address of MicroCode buffer */
Will Deaconfed8c452013-06-10 19:34:38 +0100504 dma_addr_t mcode_bus;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900505 /* CPU address of MicroCode buffer */
506 void *mcode_cpu;
507 /* List of all Channel threads */
508 struct pl330_thread *channels;
509 /* Pointer to the MANAGER thread */
510 struct pl330_thread *manager;
511 /* To handle bad news in interrupt */
512 struct tasklet_struct tasks;
513 struct _pl330_tbd dmac_tbd;
514 /* State of DMAC operation */
515 enum pl330_dmac_state state;
516};
517
Jassi Brarb3040e42010-05-23 20:28:19 -0700518enum desc_status {
519 /* In the DMAC pool */
520 FREE,
521 /*
Masanari Iidad73111c2012-08-04 23:37:53 +0900522 * Allocated to some channel during prep_xxx
Jassi Brarb3040e42010-05-23 20:28:19 -0700523 * Also may be sitting on the work_list.
524 */
525 PREP,
526 /*
527 * Sitting on the work_list and already submitted
528 * to the PL330 core. Not more than two descriptors
529 * of a channel can be BUSY at any time.
530 */
531 BUSY,
532 /*
533 * Sitting on the channel work_list but xfer done
534 * by PL330 core
535 */
536 DONE,
537};
538
539struct dma_pl330_chan {
540 /* Schedule desc completion */
541 struct tasklet_struct task;
542
543 /* DMA-Engine Channel */
544 struct dma_chan chan;
545
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +0100546 /* List of submitted descriptors */
547 struct list_head submitted_list;
548 /* List of issued descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700549 struct list_head work_list;
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +0200550 /* List of completed descriptors */
551 struct list_head completed_list;
Jassi Brarb3040e42010-05-23 20:28:19 -0700552
553 /* Pointer to the DMAC that manages this channel,
554 * NULL if the channel is available to be acquired.
555 * As the parent, this DMAC also provides descriptors
556 * to the channel.
557 */
558 struct dma_pl330_dmac *dmac;
559
560 /* To protect channel manipulation */
561 spinlock_t lock;
562
563 /* Token of a hardware channel thread of PL330 DMAC
564 * NULL if the channel is available to be acquired.
565 */
566 void *pl330_chid;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900567
568 /* For D-to-M and M-to-D channels */
569 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900570 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900571 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900572
573 /* for cyclic capability */
574 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700575};
576
577struct dma_pl330_dmac {
578 struct pl330_info pif;
579
580 /* DMA-Engine Device */
581 struct dma_device ddma;
582
Lars-Peter Clausenb714b842013-11-25 16:07:46 +0100583 /* Holds info about sg limitations */
584 struct device_dma_parameters dma_parms;
585
Jassi Brarb3040e42010-05-23 20:28:19 -0700586 /* Pool of descriptors available for the DMAC's channels */
587 struct list_head desc_pool;
588 /* To protect desc_pool manipulation */
589 spinlock_t pool_lock;
590
591 /* Peripheral channels connected to this DMAC */
Rob Herring4e0e6102011-07-25 16:05:04 -0500592 struct dma_pl330_chan *peripherals; /* keep at end */
Jassi Brarb3040e42010-05-23 20:28:19 -0700593};
594
595struct dma_pl330_desc {
596 /* To attach to a queue as child */
597 struct list_head node;
598
599 /* Descriptor for the DMA Engine API */
600 struct dma_async_tx_descriptor txd;
601
602 /* Xfer for PL330 core */
603 struct pl330_xfer px;
604
605 struct pl330_reqcfg rqcfg;
606 struct pl330_req req;
607
608 enum desc_status status;
609
610 /* The channel which currently holds this desc */
611 struct dma_pl330_chan *pchan;
612};
613
Padmavathi Venna34d19352013-02-14 09:10:05 +0530614struct dma_pl330_filter_args {
615 struct dma_pl330_dmac *pdmac;
616 unsigned int chan_id;
617};
618
Boojin Kimb7d861d2011-12-26 18:49:52 +0900619static inline void _callback(struct pl330_req *r, enum pl330_op_err err)
620{
621 if (r && r->xfer_cb)
622 r->xfer_cb(r->token, err);
623}
624
625static inline bool _queue_empty(struct pl330_thread *thrd)
626{
627 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
628 ? true : false;
629}
630
631static inline bool _queue_full(struct pl330_thread *thrd)
632{
633 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
634 ? false : true;
635}
636
637static inline bool is_manager(struct pl330_thread *thrd)
638{
639 struct pl330_dmac *pl330 = thrd->dmac;
640
641 /* MANAGER is indexed at the end */
642 if (thrd->id == pl330->pinfo->pcfg.num_chan)
643 return true;
644 else
645 return false;
646}
647
648/* If manager of the thread is in Non-Secure mode */
649static inline bool _manager_ns(struct pl330_thread *thrd)
650{
651 struct pl330_dmac *pl330 = thrd->dmac;
652
653 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
654}
655
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900656static inline u32 get_revision(u32 periph_id)
657{
658 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
659}
660
Boojin Kimb7d861d2011-12-26 18:49:52 +0900661static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
662 enum pl330_dst da, u16 val)
663{
664 if (dry_run)
665 return SZ_DMAADDH;
666
667 buf[0] = CMD_DMAADDH;
668 buf[0] |= (da << 1);
669 *((u16 *)&buf[1]) = val;
670
671 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
672 da == 1 ? "DA" : "SA", val);
673
674 return SZ_DMAADDH;
675}
676
677static inline u32 _emit_END(unsigned dry_run, u8 buf[])
678{
679 if (dry_run)
680 return SZ_DMAEND;
681
682 buf[0] = CMD_DMAEND;
683
684 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
685
686 return SZ_DMAEND;
687}
688
689static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
690{
691 if (dry_run)
692 return SZ_DMAFLUSHP;
693
694 buf[0] = CMD_DMAFLUSHP;
695
696 peri &= 0x1f;
697 peri <<= 3;
698 buf[1] = peri;
699
700 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
701
702 return SZ_DMAFLUSHP;
703}
704
705static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
706{
707 if (dry_run)
708 return SZ_DMALD;
709
710 buf[0] = CMD_DMALD;
711
712 if (cond == SINGLE)
713 buf[0] |= (0 << 1) | (1 << 0);
714 else if (cond == BURST)
715 buf[0] |= (1 << 1) | (1 << 0);
716
717 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
718 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
719
720 return SZ_DMALD;
721}
722
723static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
724 enum pl330_cond cond, u8 peri)
725{
726 if (dry_run)
727 return SZ_DMALDP;
728
729 buf[0] = CMD_DMALDP;
730
731 if (cond == BURST)
732 buf[0] |= (1 << 1);
733
734 peri &= 0x1f;
735 peri <<= 3;
736 buf[1] = peri;
737
738 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
739 cond == SINGLE ? 'S' : 'B', peri >> 3);
740
741 return SZ_DMALDP;
742}
743
744static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
745 unsigned loop, u8 cnt)
746{
747 if (dry_run)
748 return SZ_DMALP;
749
750 buf[0] = CMD_DMALP;
751
752 if (loop)
753 buf[0] |= (1 << 1);
754
755 cnt--; /* DMAC increments by 1 internally */
756 buf[1] = cnt;
757
758 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
759
760 return SZ_DMALP;
761}
762
763struct _arg_LPEND {
764 enum pl330_cond cond;
765 bool forever;
766 unsigned loop;
767 u8 bjump;
768};
769
770static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
771 const struct _arg_LPEND *arg)
772{
773 enum pl330_cond cond = arg->cond;
774 bool forever = arg->forever;
775 unsigned loop = arg->loop;
776 u8 bjump = arg->bjump;
777
778 if (dry_run)
779 return SZ_DMALPEND;
780
781 buf[0] = CMD_DMALPEND;
782
783 if (loop)
784 buf[0] |= (1 << 2);
785
786 if (!forever)
787 buf[0] |= (1 << 4);
788
789 if (cond == SINGLE)
790 buf[0] |= (0 << 1) | (1 << 0);
791 else if (cond == BURST)
792 buf[0] |= (1 << 1) | (1 << 0);
793
794 buf[1] = bjump;
795
796 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
797 forever ? "FE" : "END",
798 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
799 loop ? '1' : '0',
800 bjump);
801
802 return SZ_DMALPEND;
803}
804
805static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
806{
807 if (dry_run)
808 return SZ_DMAKILL;
809
810 buf[0] = CMD_DMAKILL;
811
812 return SZ_DMAKILL;
813}
814
815static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
816 enum dmamov_dst dst, u32 val)
817{
818 if (dry_run)
819 return SZ_DMAMOV;
820
821 buf[0] = CMD_DMAMOV;
822 buf[1] = dst;
823 *((u32 *)&buf[2]) = val;
824
825 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
826 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
827
828 return SZ_DMAMOV;
829}
830
831static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
832{
833 if (dry_run)
834 return SZ_DMANOP;
835
836 buf[0] = CMD_DMANOP;
837
838 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
839
840 return SZ_DMANOP;
841}
842
843static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
844{
845 if (dry_run)
846 return SZ_DMARMB;
847
848 buf[0] = CMD_DMARMB;
849
850 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
851
852 return SZ_DMARMB;
853}
854
855static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
856{
857 if (dry_run)
858 return SZ_DMASEV;
859
860 buf[0] = CMD_DMASEV;
861
862 ev &= 0x1f;
863 ev <<= 3;
864 buf[1] = ev;
865
866 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
867
868 return SZ_DMASEV;
869}
870
871static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
872{
873 if (dry_run)
874 return SZ_DMAST;
875
876 buf[0] = CMD_DMAST;
877
878 if (cond == SINGLE)
879 buf[0] |= (0 << 1) | (1 << 0);
880 else if (cond == BURST)
881 buf[0] |= (1 << 1) | (1 << 0);
882
883 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
884 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
885
886 return SZ_DMAST;
887}
888
889static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
890 enum pl330_cond cond, u8 peri)
891{
892 if (dry_run)
893 return SZ_DMASTP;
894
895 buf[0] = CMD_DMASTP;
896
897 if (cond == BURST)
898 buf[0] |= (1 << 1);
899
900 peri &= 0x1f;
901 peri <<= 3;
902 buf[1] = peri;
903
904 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
905 cond == SINGLE ? 'S' : 'B', peri >> 3);
906
907 return SZ_DMASTP;
908}
909
910static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
911{
912 if (dry_run)
913 return SZ_DMASTZ;
914
915 buf[0] = CMD_DMASTZ;
916
917 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
918
919 return SZ_DMASTZ;
920}
921
922static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
923 unsigned invalidate)
924{
925 if (dry_run)
926 return SZ_DMAWFE;
927
928 buf[0] = CMD_DMAWFE;
929
930 ev &= 0x1f;
931 ev <<= 3;
932 buf[1] = ev;
933
934 if (invalidate)
935 buf[1] |= (1 << 1);
936
937 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
938 ev >> 3, invalidate ? ", I" : "");
939
940 return SZ_DMAWFE;
941}
942
943static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
944 enum pl330_cond cond, u8 peri)
945{
946 if (dry_run)
947 return SZ_DMAWFP;
948
949 buf[0] = CMD_DMAWFP;
950
951 if (cond == SINGLE)
952 buf[0] |= (0 << 1) | (0 << 0);
953 else if (cond == BURST)
954 buf[0] |= (1 << 1) | (0 << 0);
955 else
956 buf[0] |= (0 << 1) | (1 << 0);
957
958 peri &= 0x1f;
959 peri <<= 3;
960 buf[1] = peri;
961
962 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
963 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
964
965 return SZ_DMAWFP;
966}
967
968static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
969{
970 if (dry_run)
971 return SZ_DMAWMB;
972
973 buf[0] = CMD_DMAWMB;
974
975 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
976
977 return SZ_DMAWMB;
978}
979
980struct _arg_GO {
981 u8 chan;
982 u32 addr;
983 unsigned ns;
984};
985
986static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
987 const struct _arg_GO *arg)
988{
989 u8 chan = arg->chan;
990 u32 addr = arg->addr;
991 unsigned ns = arg->ns;
992
993 if (dry_run)
994 return SZ_DMAGO;
995
996 buf[0] = CMD_DMAGO;
997 buf[0] |= (ns << 1);
998
999 buf[1] = chan & 0x7;
1000
1001 *((u32 *)&buf[2]) = addr;
1002
1003 return SZ_DMAGO;
1004}
1005
1006#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
1007
1008/* Returns Time-Out */
1009static bool _until_dmac_idle(struct pl330_thread *thrd)
1010{
1011 void __iomem *regs = thrd->dmac->pinfo->base;
1012 unsigned long loops = msecs_to_loops(5);
1013
1014 do {
1015 /* Until Manager is Idle */
1016 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
1017 break;
1018
1019 cpu_relax();
1020 } while (--loops);
1021
1022 if (!loops)
1023 return true;
1024
1025 return false;
1026}
1027
1028static inline void _execute_DBGINSN(struct pl330_thread *thrd,
1029 u8 insn[], bool as_manager)
1030{
1031 void __iomem *regs = thrd->dmac->pinfo->base;
1032 u32 val;
1033
1034 val = (insn[0] << 16) | (insn[1] << 24);
1035 if (!as_manager) {
1036 val |= (1 << 0);
1037 val |= (thrd->id << 8); /* Channel Number */
1038 }
1039 writel(val, regs + DBGINST0);
1040
1041 val = *((u32 *)&insn[2]);
1042 writel(val, regs + DBGINST1);
1043
1044 /* If timed out due to halted state-machine */
1045 if (_until_dmac_idle(thrd)) {
1046 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
1047 return;
1048 }
1049
1050 /* Get going */
1051 writel(0, regs + DBGCMD);
1052}
1053
1054/*
1055 * Mark a _pl330_req as free.
1056 * We do it by writing DMAEND as the first instruction
1057 * because no valid request is going to have DMAEND as
1058 * its first instruction to execute.
1059 */
1060static void mark_free(struct pl330_thread *thrd, int idx)
1061{
1062 struct _pl330_req *req = &thrd->req[idx];
1063
1064 _emit_END(0, req->mc_cpu);
1065 req->mc_len = 0;
1066
1067 thrd->req_running = -1;
1068}
1069
1070static inline u32 _state(struct pl330_thread *thrd)
1071{
1072 void __iomem *regs = thrd->dmac->pinfo->base;
1073 u32 val;
1074
1075 if (is_manager(thrd))
1076 val = readl(regs + DS) & 0xf;
1077 else
1078 val = readl(regs + CS(thrd->id)) & 0xf;
1079
1080 switch (val) {
1081 case DS_ST_STOP:
1082 return PL330_STATE_STOPPED;
1083 case DS_ST_EXEC:
1084 return PL330_STATE_EXECUTING;
1085 case DS_ST_CMISS:
1086 return PL330_STATE_CACHEMISS;
1087 case DS_ST_UPDTPC:
1088 return PL330_STATE_UPDTPC;
1089 case DS_ST_WFE:
1090 return PL330_STATE_WFE;
1091 case DS_ST_FAULT:
1092 return PL330_STATE_FAULTING;
1093 case DS_ST_ATBRR:
1094 if (is_manager(thrd))
1095 return PL330_STATE_INVALID;
1096 else
1097 return PL330_STATE_ATBARRIER;
1098 case DS_ST_QBUSY:
1099 if (is_manager(thrd))
1100 return PL330_STATE_INVALID;
1101 else
1102 return PL330_STATE_QUEUEBUSY;
1103 case DS_ST_WFP:
1104 if (is_manager(thrd))
1105 return PL330_STATE_INVALID;
1106 else
1107 return PL330_STATE_WFP;
1108 case DS_ST_KILL:
1109 if (is_manager(thrd))
1110 return PL330_STATE_INVALID;
1111 else
1112 return PL330_STATE_KILLING;
1113 case DS_ST_CMPLT:
1114 if (is_manager(thrd))
1115 return PL330_STATE_INVALID;
1116 else
1117 return PL330_STATE_COMPLETING;
1118 case DS_ST_FLTCMP:
1119 if (is_manager(thrd))
1120 return PL330_STATE_INVALID;
1121 else
1122 return PL330_STATE_FAULT_COMPLETING;
1123 default:
1124 return PL330_STATE_INVALID;
1125 }
1126}
1127
1128static void _stop(struct pl330_thread *thrd)
1129{
1130 void __iomem *regs = thrd->dmac->pinfo->base;
1131 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1132
1133 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1134 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1135
1136 /* Return if nothing needs to be done */
1137 if (_state(thrd) == PL330_STATE_COMPLETING
1138 || _state(thrd) == PL330_STATE_KILLING
1139 || _state(thrd) == PL330_STATE_STOPPED)
1140 return;
1141
1142 _emit_KILL(0, insn);
1143
1144 /* Stop generating interrupts for SEV */
1145 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1146
1147 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1148}
1149
1150/* Start doing req 'idx' of thread 'thrd' */
1151static bool _trigger(struct pl330_thread *thrd)
1152{
1153 void __iomem *regs = thrd->dmac->pinfo->base;
1154 struct _pl330_req *req;
1155 struct pl330_req *r;
1156 struct _arg_GO go;
1157 unsigned ns;
1158 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1159 int idx;
1160
1161 /* Return if already ACTIVE */
1162 if (_state(thrd) != PL330_STATE_STOPPED)
1163 return true;
1164
1165 idx = 1 - thrd->lstenq;
1166 if (!IS_FREE(&thrd->req[idx]))
1167 req = &thrd->req[idx];
1168 else {
1169 idx = thrd->lstenq;
1170 if (!IS_FREE(&thrd->req[idx]))
1171 req = &thrd->req[idx];
1172 else
1173 req = NULL;
1174 }
1175
1176 /* Return if no request */
1177 if (!req || !req->r)
1178 return true;
1179
1180 r = req->r;
1181
1182 if (r->cfg)
1183 ns = r->cfg->nonsecure ? 1 : 0;
1184 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1185 ns = 1;
1186 else
1187 ns = 0;
1188
1189 /* See 'Abort Sources' point-4 at Page 2-25 */
1190 if (_manager_ns(thrd) && !ns)
1191 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1192 __func__, __LINE__);
1193
1194 go.chan = thrd->id;
1195 go.addr = req->mc_bus;
1196 go.ns = ns;
1197 _emit_GO(0, insn, &go);
1198
1199 /* Set to generate interrupts for SEV */
1200 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1201
1202 /* Only manager can execute GO */
1203 _execute_DBGINSN(thrd, insn, true);
1204
1205 thrd->req_running = idx;
1206
1207 return true;
1208}
1209
1210static bool _start(struct pl330_thread *thrd)
1211{
1212 switch (_state(thrd)) {
1213 case PL330_STATE_FAULT_COMPLETING:
1214 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1215
1216 if (_state(thrd) == PL330_STATE_KILLING)
1217 UNTIL(thrd, PL330_STATE_STOPPED)
1218
1219 case PL330_STATE_FAULTING:
1220 _stop(thrd);
1221
1222 case PL330_STATE_KILLING:
1223 case PL330_STATE_COMPLETING:
1224 UNTIL(thrd, PL330_STATE_STOPPED)
1225
1226 case PL330_STATE_STOPPED:
1227 return _trigger(thrd);
1228
1229 case PL330_STATE_WFP:
1230 case PL330_STATE_QUEUEBUSY:
1231 case PL330_STATE_ATBARRIER:
1232 case PL330_STATE_UPDTPC:
1233 case PL330_STATE_CACHEMISS:
1234 case PL330_STATE_EXECUTING:
1235 return true;
1236
1237 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1238 default:
1239 return false;
1240 }
1241}
1242
1243static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1244 const struct _xfer_spec *pxs, int cyc)
1245{
1246 int off = 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001247 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001248
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001249 /* check lock-up free version */
1250 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1251 while (cyc--) {
1252 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1253 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1254 }
1255 } else {
1256 while (cyc--) {
1257 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1258 off += _emit_RMB(dry_run, &buf[off]);
1259 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1260 off += _emit_WMB(dry_run, &buf[off]);
1261 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001262 }
1263
1264 return off;
1265}
1266
1267static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1268 const struct _xfer_spec *pxs, int cyc)
1269{
1270 int off = 0;
1271
1272 while (cyc--) {
1273 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1274 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1275 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1276 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1277 }
1278
1279 return off;
1280}
1281
1282static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1283 const struct _xfer_spec *pxs, int cyc)
1284{
1285 int off = 0;
1286
1287 while (cyc--) {
1288 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1289 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1290 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1291 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1292 }
1293
1294 return off;
1295}
1296
1297static int _bursts(unsigned dry_run, u8 buf[],
1298 const struct _xfer_spec *pxs, int cyc)
1299{
1300 int off = 0;
1301
1302 switch (pxs->r->rqtype) {
1303 case MEMTODEV:
1304 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1305 break;
1306 case DEVTOMEM:
1307 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1308 break;
1309 case MEMTOMEM:
1310 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1311 break;
1312 default:
1313 off += 0x40000000; /* Scare off the Client */
1314 break;
1315 }
1316
1317 return off;
1318}
1319
1320/* Returns bytes consumed and updates bursts */
1321static inline int _loop(unsigned dry_run, u8 buf[],
1322 unsigned long *bursts, const struct _xfer_spec *pxs)
1323{
1324 int cyc, cycmax, szlp, szlpend, szbrst, off;
1325 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1326 struct _arg_LPEND lpend;
1327
1328 /* Max iterations possible in DMALP is 256 */
1329 if (*bursts >= 256*256) {
1330 lcnt1 = 256;
1331 lcnt0 = 256;
1332 cyc = *bursts / lcnt1 / lcnt0;
1333 } else if (*bursts > 256) {
1334 lcnt1 = 256;
1335 lcnt0 = *bursts / lcnt1;
1336 cyc = 1;
1337 } else {
1338 lcnt1 = *bursts;
1339 lcnt0 = 0;
1340 cyc = 1;
1341 }
1342
1343 szlp = _emit_LP(1, buf, 0, 0);
1344 szbrst = _bursts(1, buf, pxs, 1);
1345
1346 lpend.cond = ALWAYS;
1347 lpend.forever = false;
1348 lpend.loop = 0;
1349 lpend.bjump = 0;
1350 szlpend = _emit_LPEND(1, buf, &lpend);
1351
1352 if (lcnt0) {
1353 szlp *= 2;
1354 szlpend *= 2;
1355 }
1356
1357 /*
1358 * Max bursts that we can unroll due to limit on the
1359 * size of backward jump that can be encoded in DMALPEND
1360 * which is 8-bits and hence 255
1361 */
1362 cycmax = (255 - (szlp + szlpend)) / szbrst;
1363
1364 cyc = (cycmax < cyc) ? cycmax : cyc;
1365
1366 off = 0;
1367
1368 if (lcnt0) {
1369 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1370 ljmp0 = off;
1371 }
1372
1373 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1374 ljmp1 = off;
1375
1376 off += _bursts(dry_run, &buf[off], pxs, cyc);
1377
1378 lpend.cond = ALWAYS;
1379 lpend.forever = false;
1380 lpend.loop = 1;
1381 lpend.bjump = off - ljmp1;
1382 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1383
1384 if (lcnt0) {
1385 lpend.cond = ALWAYS;
1386 lpend.forever = false;
1387 lpend.loop = 0;
1388 lpend.bjump = off - ljmp0;
1389 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1390 }
1391
1392 *bursts = lcnt1 * cyc;
1393 if (lcnt0)
1394 *bursts *= lcnt0;
1395
1396 return off;
1397}
1398
1399static inline int _setup_loops(unsigned dry_run, u8 buf[],
1400 const struct _xfer_spec *pxs)
1401{
1402 struct pl330_xfer *x = pxs->x;
1403 u32 ccr = pxs->ccr;
1404 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1405 int off = 0;
1406
1407 while (bursts) {
1408 c = bursts;
1409 off += _loop(dry_run, &buf[off], &c, pxs);
1410 bursts -= c;
1411 }
1412
1413 return off;
1414}
1415
1416static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1417 const struct _xfer_spec *pxs)
1418{
1419 struct pl330_xfer *x = pxs->x;
1420 int off = 0;
1421
1422 /* DMAMOV SAR, x->src_addr */
1423 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1424 /* DMAMOV DAR, x->dst_addr */
1425 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1426
1427 /* Setup Loop(s) */
1428 off += _setup_loops(dry_run, &buf[off], pxs);
1429
1430 return off;
1431}
1432
1433/*
1434 * A req is a sequence of one or more xfer units.
1435 * Returns the number of bytes taken to setup the MC for the req.
1436 */
1437static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1438 unsigned index, struct _xfer_spec *pxs)
1439{
1440 struct _pl330_req *req = &thrd->req[index];
1441 struct pl330_xfer *x;
1442 u8 *buf = req->mc_cpu;
1443 int off = 0;
1444
1445 PL330_DBGMC_START(req->mc_bus);
1446
1447 /* DMAMOV CCR, ccr */
1448 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1449
1450 x = pxs->r->x;
1451 do {
1452 /* Error if xfer length is not aligned at burst size */
1453 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1454 return -EINVAL;
1455
1456 pxs->x = x;
1457 off += _setup_xfer(dry_run, &buf[off], pxs);
1458
1459 x = x->next;
1460 } while (x);
1461
1462 /* DMASEV peripheral/event */
1463 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1464 /* DMAEND */
1465 off += _emit_END(dry_run, &buf[off]);
1466
1467 return off;
1468}
1469
1470static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1471{
1472 u32 ccr = 0;
1473
1474 if (rqc->src_inc)
1475 ccr |= CC_SRCINC;
1476
1477 if (rqc->dst_inc)
1478 ccr |= CC_DSTINC;
1479
1480 /* We set same protection levels for Src and DST for now */
1481 if (rqc->privileged)
1482 ccr |= CC_SRCPRI | CC_DSTPRI;
1483 if (rqc->nonsecure)
1484 ccr |= CC_SRCNS | CC_DSTNS;
1485 if (rqc->insnaccess)
1486 ccr |= CC_SRCIA | CC_DSTIA;
1487
1488 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1489 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1490
1491 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1492 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1493
1494 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1495 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1496
1497 ccr |= (rqc->swap << CC_SWAP_SHFT);
1498
1499 return ccr;
1500}
1501
1502static inline bool _is_valid(u32 ccr)
1503{
1504 enum pl330_dstcachectrl dcctl;
1505 enum pl330_srccachectrl scctl;
1506
1507 dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1508 scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1509
1510 if (dcctl == DINVALID1 || dcctl == DINVALID2
1511 || scctl == SINVALID1 || scctl == SINVALID2)
1512 return false;
1513 else
1514 return true;
1515}
1516
1517/*
1518 * Submit a list of xfers after which the client wants notification.
1519 * Client is not notified after each xfer unit, just once after all
1520 * xfer units are done or some error occurs.
1521 */
1522static int pl330_submit_req(void *ch_id, struct pl330_req *r)
1523{
1524 struct pl330_thread *thrd = ch_id;
1525 struct pl330_dmac *pl330;
1526 struct pl330_info *pi;
1527 struct _xfer_spec xs;
1528 unsigned long flags;
1529 void __iomem *regs;
1530 unsigned idx;
1531 u32 ccr;
1532 int ret = 0;
1533
1534 /* No Req or Unacquired Channel or DMAC */
1535 if (!r || !thrd || thrd->free)
1536 return -EINVAL;
1537
1538 pl330 = thrd->dmac;
1539 pi = pl330->pinfo;
1540 regs = pi->base;
1541
1542 if (pl330->state == DYING
1543 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1544 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1545 __func__, __LINE__);
1546 return -EAGAIN;
1547 }
1548
1549 /* If request for non-existing peripheral */
1550 if (r->rqtype != MEMTOMEM && r->peri >= pi->pcfg.num_peri) {
1551 dev_info(thrd->dmac->pinfo->dev,
1552 "%s:%d Invalid peripheral(%u)!\n",
1553 __func__, __LINE__, r->peri);
1554 return -EINVAL;
1555 }
1556
1557 spin_lock_irqsave(&pl330->lock, flags);
1558
1559 if (_queue_full(thrd)) {
1560 ret = -EAGAIN;
1561 goto xfer_exit;
1562 }
1563
Boojin Kimb7d861d2011-12-26 18:49:52 +09001564
1565 /* Use last settings, if not provided */
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301566 if (r->cfg) {
1567 /* Prefer Secure Channel */
1568 if (!_manager_ns(thrd))
1569 r->cfg->nonsecure = 0;
1570 else
1571 r->cfg->nonsecure = 1;
1572
Boojin Kimb7d861d2011-12-26 18:49:52 +09001573 ccr = _prepare_ccr(r->cfg);
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301574 } else {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001575 ccr = readl(regs + CC(thrd->id));
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301576 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001577
1578 /* If this req doesn't have valid xfer settings */
1579 if (!_is_valid(ccr)) {
1580 ret = -EINVAL;
1581 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1582 __func__, __LINE__, ccr);
1583 goto xfer_exit;
1584 }
1585
1586 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1587
1588 xs.ccr = ccr;
1589 xs.r = r;
1590
1591 /* First dry run to check if req is acceptable */
1592 ret = _setup_req(1, thrd, idx, &xs);
1593 if (ret < 0)
1594 goto xfer_exit;
1595
1596 if (ret > pi->mcbufsz / 2) {
1597 dev_info(thrd->dmac->pinfo->dev,
1598 "%s:%d Trying increasing mcbufsz\n",
1599 __func__, __LINE__);
1600 ret = -ENOMEM;
1601 goto xfer_exit;
1602 }
1603
1604 /* Hook the request */
1605 thrd->lstenq = idx;
1606 thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs);
1607 thrd->req[idx].r = r;
1608
1609 ret = 0;
1610
1611xfer_exit:
1612 spin_unlock_irqrestore(&pl330->lock, flags);
1613
1614 return ret;
1615}
1616
1617static void pl330_dotask(unsigned long data)
1618{
1619 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1620 struct pl330_info *pi = pl330->pinfo;
1621 unsigned long flags;
1622 int i;
1623
1624 spin_lock_irqsave(&pl330->lock, flags);
1625
1626 /* The DMAC itself gone nuts */
1627 if (pl330->dmac_tbd.reset_dmac) {
1628 pl330->state = DYING;
1629 /* Reset the manager too */
1630 pl330->dmac_tbd.reset_mngr = true;
1631 /* Clear the reset flag */
1632 pl330->dmac_tbd.reset_dmac = false;
1633 }
1634
1635 if (pl330->dmac_tbd.reset_mngr) {
1636 _stop(pl330->manager);
1637 /* Reset all channels */
1638 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1639 /* Clear the reset flag */
1640 pl330->dmac_tbd.reset_mngr = false;
1641 }
1642
1643 for (i = 0; i < pi->pcfg.num_chan; i++) {
1644
1645 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1646 struct pl330_thread *thrd = &pl330->channels[i];
1647 void __iomem *regs = pi->base;
1648 enum pl330_op_err err;
1649
1650 _stop(thrd);
1651
1652 if (readl(regs + FSC) & (1 << thrd->id))
1653 err = PL330_ERR_FAIL;
1654 else
1655 err = PL330_ERR_ABORT;
1656
1657 spin_unlock_irqrestore(&pl330->lock, flags);
1658
1659 _callback(thrd->req[1 - thrd->lstenq].r, err);
1660 _callback(thrd->req[thrd->lstenq].r, err);
1661
1662 spin_lock_irqsave(&pl330->lock, flags);
1663
1664 thrd->req[0].r = NULL;
1665 thrd->req[1].r = NULL;
1666 mark_free(thrd, 0);
1667 mark_free(thrd, 1);
1668
1669 /* Clear the reset flag */
1670 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1671 }
1672 }
1673
1674 spin_unlock_irqrestore(&pl330->lock, flags);
1675
1676 return;
1677}
1678
1679/* Returns 1 if state was updated, 0 otherwise */
1680static int pl330_update(const struct pl330_info *pi)
1681{
Javi Merinofdec53d2012-06-13 15:07:00 +01001682 struct pl330_req *rqdone, *tmp;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001683 struct pl330_dmac *pl330;
1684 unsigned long flags;
1685 void __iomem *regs;
1686 u32 val;
1687 int id, ev, ret = 0;
1688
1689 if (!pi || !pi->pl330_data)
1690 return 0;
1691
1692 regs = pi->base;
1693 pl330 = pi->pl330_data;
1694
1695 spin_lock_irqsave(&pl330->lock, flags);
1696
1697 val = readl(regs + FSM) & 0x1;
1698 if (val)
1699 pl330->dmac_tbd.reset_mngr = true;
1700 else
1701 pl330->dmac_tbd.reset_mngr = false;
1702
1703 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1704 pl330->dmac_tbd.reset_chan |= val;
1705 if (val) {
1706 int i = 0;
1707 while (i < pi->pcfg.num_chan) {
1708 if (val & (1 << i)) {
1709 dev_info(pi->dev,
1710 "Reset Channel-%d\t CS-%x FTC-%x\n",
1711 i, readl(regs + CS(i)),
1712 readl(regs + FTC(i)));
1713 _stop(&pl330->channels[i]);
1714 }
1715 i++;
1716 }
1717 }
1718
1719 /* Check which event happened i.e, thread notified */
1720 val = readl(regs + ES);
1721 if (pi->pcfg.num_events < 32
1722 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1723 pl330->dmac_tbd.reset_dmac = true;
1724 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1725 ret = 1;
1726 goto updt_exit;
1727 }
1728
1729 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1730 if (val & (1 << ev)) { /* Event occurred */
1731 struct pl330_thread *thrd;
1732 u32 inten = readl(regs + INTEN);
1733 int active;
1734
1735 /* Clear the event */
1736 if (inten & (1 << ev))
1737 writel(1 << ev, regs + INTCLR);
1738
1739 ret = 1;
1740
1741 id = pl330->events[ev];
1742
1743 thrd = &pl330->channels[id];
1744
1745 active = thrd->req_running;
1746 if (active == -1) /* Aborted */
1747 continue;
1748
Javi Merinofdec53d2012-06-13 15:07:00 +01001749 /* Detach the req */
1750 rqdone = thrd->req[active].r;
1751 thrd->req[active].r = NULL;
1752
Boojin Kimb7d861d2011-12-26 18:49:52 +09001753 mark_free(thrd, active);
1754
1755 /* Get going again ASAP */
1756 _start(thrd);
1757
1758 /* For now, just make a list of callbacks to be done */
1759 list_add_tail(&rqdone->rqd, &pl330->req_done);
1760 }
1761 }
1762
1763 /* Now that we are in no hurry, do the callbacks */
Javi Merinofdec53d2012-06-13 15:07:00 +01001764 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1765 list_del(&rqdone->rqd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001766
1767 spin_unlock_irqrestore(&pl330->lock, flags);
Javi Merinofdec53d2012-06-13 15:07:00 +01001768 _callback(rqdone, PL330_ERR_NONE);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001769 spin_lock_irqsave(&pl330->lock, flags);
1770 }
1771
1772updt_exit:
1773 spin_unlock_irqrestore(&pl330->lock, flags);
1774
1775 if (pl330->dmac_tbd.reset_dmac
1776 || pl330->dmac_tbd.reset_mngr
1777 || pl330->dmac_tbd.reset_chan) {
1778 ret = 1;
1779 tasklet_schedule(&pl330->tasks);
1780 }
1781
1782 return ret;
1783}
1784
1785static int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op)
1786{
1787 struct pl330_thread *thrd = ch_id;
1788 struct pl330_dmac *pl330;
1789 unsigned long flags;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001790 int ret = 0, active;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001791
1792 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1793 return -EINVAL;
1794
1795 pl330 = thrd->dmac;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001796 active = thrd->req_running;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001797
1798 spin_lock_irqsave(&pl330->lock, flags);
1799
1800 switch (op) {
1801 case PL330_OP_FLUSH:
1802 /* Make sure the channel is stopped */
1803 _stop(thrd);
1804
1805 thrd->req[0].r = NULL;
1806 thrd->req[1].r = NULL;
1807 mark_free(thrd, 0);
1808 mark_free(thrd, 1);
1809 break;
1810
1811 case PL330_OP_ABORT:
1812 /* Make sure the channel is stopped */
1813 _stop(thrd);
1814
1815 /* ABORT is only for the active req */
1816 if (active == -1)
1817 break;
1818
1819 thrd->req[active].r = NULL;
1820 mark_free(thrd, active);
1821
1822 /* Start the next */
1823 case PL330_OP_START:
1824 if ((active == -1) && !_start(thrd))
1825 ret = -EIO;
1826 break;
1827
1828 default:
1829 ret = -EINVAL;
1830 }
1831
1832 spin_unlock_irqrestore(&pl330->lock, flags);
1833 return ret;
1834}
1835
Boojin Kimb7d861d2011-12-26 18:49:52 +09001836/* Reserve an event */
1837static inline int _alloc_event(struct pl330_thread *thrd)
1838{
1839 struct pl330_dmac *pl330 = thrd->dmac;
1840 struct pl330_info *pi = pl330->pinfo;
1841 int ev;
1842
1843 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1844 if (pl330->events[ev] == -1) {
1845 pl330->events[ev] = thrd->id;
1846 return ev;
1847 }
1848
1849 return -1;
1850}
1851
1852static bool _chan_ns(const struct pl330_info *pi, int i)
1853{
1854 return pi->pcfg.irq_ns & (1 << i);
1855}
1856
1857/* Upon success, returns IdentityToken for the
1858 * allocated channel, NULL otherwise.
1859 */
1860static void *pl330_request_channel(const struct pl330_info *pi)
1861{
1862 struct pl330_thread *thrd = NULL;
1863 struct pl330_dmac *pl330;
1864 unsigned long flags;
1865 int chans, i;
1866
1867 if (!pi || !pi->pl330_data)
1868 return NULL;
1869
1870 pl330 = pi->pl330_data;
1871
1872 if (pl330->state == DYING)
1873 return NULL;
1874
1875 chans = pi->pcfg.num_chan;
1876
1877 spin_lock_irqsave(&pl330->lock, flags);
1878
1879 for (i = 0; i < chans; i++) {
1880 thrd = &pl330->channels[i];
1881 if ((thrd->free) && (!_manager_ns(thrd) ||
1882 _chan_ns(pi, i))) {
1883 thrd->ev = _alloc_event(thrd);
1884 if (thrd->ev >= 0) {
1885 thrd->free = false;
1886 thrd->lstenq = 1;
1887 thrd->req[0].r = NULL;
1888 mark_free(thrd, 0);
1889 thrd->req[1].r = NULL;
1890 mark_free(thrd, 1);
1891 break;
1892 }
1893 }
1894 thrd = NULL;
1895 }
1896
1897 spin_unlock_irqrestore(&pl330->lock, flags);
1898
1899 return thrd;
1900}
1901
1902/* Release an event */
1903static inline void _free_event(struct pl330_thread *thrd, int ev)
1904{
1905 struct pl330_dmac *pl330 = thrd->dmac;
1906 struct pl330_info *pi = pl330->pinfo;
1907
1908 /* If the event is valid and was held by the thread */
1909 if (ev >= 0 && ev < pi->pcfg.num_events
1910 && pl330->events[ev] == thrd->id)
1911 pl330->events[ev] = -1;
1912}
1913
1914static void pl330_release_channel(void *ch_id)
1915{
1916 struct pl330_thread *thrd = ch_id;
1917 struct pl330_dmac *pl330;
1918 unsigned long flags;
1919
1920 if (!thrd || thrd->free)
1921 return;
1922
1923 _stop(thrd);
1924
1925 _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1926 _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1927
1928 pl330 = thrd->dmac;
1929
1930 spin_lock_irqsave(&pl330->lock, flags);
1931 _free_event(thrd, thrd->ev);
1932 thrd->free = true;
1933 spin_unlock_irqrestore(&pl330->lock, flags);
1934}
1935
1936/* Initialize the structure for PL330 configuration, that can be used
1937 * by the client driver the make best use of the DMAC
1938 */
1939static void read_dmac_config(struct pl330_info *pi)
1940{
1941 void __iomem *regs = pi->base;
1942 u32 val;
1943
1944 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1945 val &= CRD_DATA_WIDTH_MASK;
1946 pi->pcfg.data_bus_width = 8 * (1 << val);
1947
1948 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1949 val &= CRD_DATA_BUFF_MASK;
1950 pi->pcfg.data_buf_dep = val + 1;
1951
1952 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1953 val &= CR0_NUM_CHANS_MASK;
1954 val += 1;
1955 pi->pcfg.num_chan = val;
1956
1957 val = readl(regs + CR0);
1958 if (val & CR0_PERIPH_REQ_SET) {
1959 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1960 val += 1;
1961 pi->pcfg.num_peri = val;
1962 pi->pcfg.peri_ns = readl(regs + CR4);
1963 } else {
1964 pi->pcfg.num_peri = 0;
1965 }
1966
1967 val = readl(regs + CR0);
1968 if (val & CR0_BOOT_MAN_NS)
1969 pi->pcfg.mode |= DMAC_MODE_NS;
1970 else
1971 pi->pcfg.mode &= ~DMAC_MODE_NS;
1972
1973 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1974 val &= CR0_NUM_EVENTS_MASK;
1975 val += 1;
1976 pi->pcfg.num_events = val;
1977
1978 pi->pcfg.irq_ns = readl(regs + CR3);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001979}
1980
1981static inline void _reset_thread(struct pl330_thread *thrd)
1982{
1983 struct pl330_dmac *pl330 = thrd->dmac;
1984 struct pl330_info *pi = pl330->pinfo;
1985
1986 thrd->req[0].mc_cpu = pl330->mcode_cpu
1987 + (thrd->id * pi->mcbufsz);
1988 thrd->req[0].mc_bus = pl330->mcode_bus
1989 + (thrd->id * pi->mcbufsz);
1990 thrd->req[0].r = NULL;
1991 mark_free(thrd, 0);
1992
1993 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1994 + pi->mcbufsz / 2;
1995 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1996 + pi->mcbufsz / 2;
1997 thrd->req[1].r = NULL;
1998 mark_free(thrd, 1);
1999}
2000
2001static int dmac_alloc_threads(struct pl330_dmac *pl330)
2002{
2003 struct pl330_info *pi = pl330->pinfo;
2004 int chans = pi->pcfg.num_chan;
2005 struct pl330_thread *thrd;
2006 int i;
2007
2008 /* Allocate 1 Manager and 'chans' Channel threads */
2009 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
2010 GFP_KERNEL);
2011 if (!pl330->channels)
2012 return -ENOMEM;
2013
2014 /* Init Channel threads */
2015 for (i = 0; i < chans; i++) {
2016 thrd = &pl330->channels[i];
2017 thrd->id = i;
2018 thrd->dmac = pl330;
2019 _reset_thread(thrd);
2020 thrd->free = true;
2021 }
2022
2023 /* MANAGER is indexed at the end */
2024 thrd = &pl330->channels[chans];
2025 thrd->id = chans;
2026 thrd->dmac = pl330;
2027 thrd->free = false;
2028 pl330->manager = thrd;
2029
2030 return 0;
2031}
2032
2033static int dmac_alloc_resources(struct pl330_dmac *pl330)
2034{
2035 struct pl330_info *pi = pl330->pinfo;
2036 int chans = pi->pcfg.num_chan;
2037 int ret;
2038
2039 /*
2040 * Alloc MicroCode buffer for 'chans' Channel threads.
2041 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
2042 */
2043 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
2044 chans * pi->mcbufsz,
2045 &pl330->mcode_bus, GFP_KERNEL);
2046 if (!pl330->mcode_cpu) {
2047 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2048 __func__, __LINE__);
2049 return -ENOMEM;
2050 }
2051
2052 ret = dmac_alloc_threads(pl330);
2053 if (ret) {
2054 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
2055 __func__, __LINE__);
2056 dma_free_coherent(pi->dev,
2057 chans * pi->mcbufsz,
2058 pl330->mcode_cpu, pl330->mcode_bus);
2059 return ret;
2060 }
2061
2062 return 0;
2063}
2064
2065static int pl330_add(struct pl330_info *pi)
2066{
2067 struct pl330_dmac *pl330;
2068 void __iomem *regs;
2069 int i, ret;
2070
2071 if (!pi || !pi->dev)
2072 return -EINVAL;
2073
2074 /* If already added */
2075 if (pi->pl330_data)
2076 return -EINVAL;
2077
2078 /*
2079 * If the SoC can perform reset on the DMAC, then do it
2080 * before reading its configuration.
2081 */
2082 if (pi->dmac_reset)
2083 pi->dmac_reset(pi);
2084
2085 regs = pi->base;
2086
2087 /* Check if we can handle this DMAC */
Will Deacon09677172013-06-10 19:34:37 +01002088 if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
2089 dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002090 return -EINVAL;
2091 }
2092
2093 /* Read the configuration of the DMAC */
2094 read_dmac_config(pi);
2095
2096 if (pi->pcfg.num_events == 0) {
2097 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2098 __func__, __LINE__);
2099 return -EINVAL;
2100 }
2101
2102 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2103 if (!pl330) {
2104 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2105 __func__, __LINE__);
2106 return -ENOMEM;
2107 }
2108
2109 /* Assign the info structure and private data */
2110 pl330->pinfo = pi;
2111 pi->pl330_data = pl330;
2112
2113 spin_lock_init(&pl330->lock);
2114
2115 INIT_LIST_HEAD(&pl330->req_done);
2116
2117 /* Use default MC buffer size if not provided */
2118 if (!pi->mcbufsz)
2119 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2120
2121 /* Mark all events as free */
2122 for (i = 0; i < pi->pcfg.num_events; i++)
2123 pl330->events[i] = -1;
2124
2125 /* Allocate resources needed by the DMAC */
2126 ret = dmac_alloc_resources(pl330);
2127 if (ret) {
2128 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2129 kfree(pl330);
2130 return ret;
2131 }
2132
2133 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2134
2135 pl330->state = INIT;
2136
2137 return 0;
2138}
2139
2140static int dmac_free_threads(struct pl330_dmac *pl330)
2141{
2142 struct pl330_info *pi = pl330->pinfo;
2143 int chans = pi->pcfg.num_chan;
2144 struct pl330_thread *thrd;
2145 int i;
2146
2147 /* Release Channel threads */
2148 for (i = 0; i < chans; i++) {
2149 thrd = &pl330->channels[i];
2150 pl330_release_channel((void *)thrd);
2151 }
2152
2153 /* Free memory */
2154 kfree(pl330->channels);
2155
2156 return 0;
2157}
2158
2159static void dmac_free_resources(struct pl330_dmac *pl330)
2160{
2161 struct pl330_info *pi = pl330->pinfo;
2162 int chans = pi->pcfg.num_chan;
2163
2164 dmac_free_threads(pl330);
2165
2166 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2167 pl330->mcode_cpu, pl330->mcode_bus);
2168}
2169
2170static void pl330_del(struct pl330_info *pi)
2171{
2172 struct pl330_dmac *pl330;
2173
2174 if (!pi || !pi->pl330_data)
2175 return;
2176
2177 pl330 = pi->pl330_data;
2178
2179 pl330->state = UNINIT;
2180
2181 tasklet_kill(&pl330->tasks);
2182
2183 /* Free DMAC resources */
2184 dmac_free_resources(pl330);
2185
2186 kfree(pl330);
2187 pi->pl330_data = NULL;
2188}
2189
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002190/* forward declaration */
2191static struct amba_driver pl330_driver;
2192
Jassi Brarb3040e42010-05-23 20:28:19 -07002193static inline struct dma_pl330_chan *
2194to_pchan(struct dma_chan *ch)
2195{
2196 if (!ch)
2197 return NULL;
2198
2199 return container_of(ch, struct dma_pl330_chan, chan);
2200}
2201
2202static inline struct dma_pl330_desc *
2203to_desc(struct dma_async_tx_descriptor *tx)
2204{
2205 return container_of(tx, struct dma_pl330_desc, txd);
2206}
2207
Jassi Brarb3040e42010-05-23 20:28:19 -07002208static inline void fill_queue(struct dma_pl330_chan *pch)
2209{
2210 struct dma_pl330_desc *desc;
2211 int ret;
2212
2213 list_for_each_entry(desc, &pch->work_list, node) {
2214
2215 /* If already submitted */
2216 if (desc->status == BUSY)
Jassi Brar30fb9802013-02-13 16:13:14 +05302217 continue;
Jassi Brarb3040e42010-05-23 20:28:19 -07002218
2219 ret = pl330_submit_req(pch->pl330_chid,
2220 &desc->req);
2221 if (!ret) {
2222 desc->status = BUSY;
Jassi Brarb3040e42010-05-23 20:28:19 -07002223 } else if (ret == -EAGAIN) {
2224 /* QFull or DMAC Dying */
2225 break;
2226 } else {
2227 /* Unacceptable request */
2228 desc->status = DONE;
2229 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2230 __func__, __LINE__, desc->txd.cookie);
2231 tasklet_schedule(&pch->task);
2232 }
2233 }
2234}
2235
2236static void pl330_tasklet(unsigned long data)
2237{
2238 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2239 struct dma_pl330_desc *desc, *_dt;
2240 unsigned long flags;
Jassi Brarb3040e42010-05-23 20:28:19 -07002241
2242 spin_lock_irqsave(&pch->lock, flags);
2243
2244 /* Pick up ripe tomatoes */
2245 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2246 if (desc->status == DONE) {
Tushar Behera30c1dc02012-05-23 16:47:31 +05302247 if (!pch->cyclic)
Vinod Kouleab21582012-05-11 11:24:41 +05302248 dma_cookie_complete(&desc->txd);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002249 list_move_tail(&desc->node, &pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002250 }
2251
2252 /* Try to submit a req imm. next to the last completed cookie */
2253 fill_queue(pch);
2254
2255 /* Make sure the PL330 Channel thread is active */
2256 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
2257
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002258 while (!list_empty(&pch->completed_list)) {
2259 dma_async_tx_callback callback;
2260 void *callback_param;
Jassi Brarb3040e42010-05-23 20:28:19 -07002261
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002262 desc = list_first_entry(&pch->completed_list,
2263 struct dma_pl330_desc, node);
2264
2265 callback = desc->txd.callback;
2266 callback_param = desc->txd.callback_param;
2267
2268 if (pch->cyclic) {
2269 desc->status = PREP;
2270 list_move_tail(&desc->node, &pch->work_list);
2271 } else {
2272 desc->status = FREE;
2273 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2274 }
2275
Dan Williamsd38a8c62013-10-18 19:35:23 +02002276 dma_descriptor_unmap(&desc->txd);
2277
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002278 if (callback) {
2279 spin_unlock_irqrestore(&pch->lock, flags);
2280 callback(callback_param);
2281 spin_lock_irqsave(&pch->lock, flags);
2282 }
2283 }
2284 spin_unlock_irqrestore(&pch->lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002285}
2286
2287static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
2288{
2289 struct dma_pl330_desc *desc = token;
2290 struct dma_pl330_chan *pch = desc->pchan;
2291 unsigned long flags;
2292
2293 /* If desc aborted */
2294 if (!pch)
2295 return;
2296
2297 spin_lock_irqsave(&pch->lock, flags);
2298
2299 desc->status = DONE;
2300
2301 spin_unlock_irqrestore(&pch->lock, flags);
2302
2303 tasklet_schedule(&pch->task);
2304}
2305
Padmavathi Venna34d19352013-02-14 09:10:05 +05302306static bool pl330_dt_filter(struct dma_chan *chan, void *param)
2307{
2308 struct dma_pl330_filter_args *fargs = param;
2309
2310 if (chan->device != &fargs->pdmac->ddma)
2311 return false;
2312
2313 return (chan->chan_id == fargs->chan_id);
2314}
2315
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002316bool pl330_filter(struct dma_chan *chan, void *param)
2317{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002318 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002319
2320 if (chan->device->dev->driver != &pl330_driver.drv)
2321 return false;
2322
Thomas Abrahamcd072512011-10-24 11:43:11 +02002323 peri_id = chan->private;
Dan Carpenter2f986ec2013-11-08 12:51:16 +03002324 return *peri_id == (unsigned long)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002325}
2326EXPORT_SYMBOL(pl330_filter);
2327
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302328static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2329 struct of_dma *ofdma)
2330{
2331 int count = dma_spec->args_count;
2332 struct dma_pl330_dmac *pdmac = ofdma->of_dma_data;
2333 struct dma_pl330_filter_args fargs;
2334 dma_cap_mask_t cap;
2335
2336 if (!pdmac)
2337 return NULL;
2338
2339 if (count != 1)
2340 return NULL;
2341
2342 fargs.pdmac = pdmac;
2343 fargs.chan_id = dma_spec->args[0];
2344
2345 dma_cap_zero(cap);
2346 dma_cap_set(DMA_SLAVE, cap);
2347 dma_cap_set(DMA_CYCLIC, cap);
2348
2349 return dma_request_channel(cap, pl330_dt_filter, &fargs);
2350}
2351
Jassi Brarb3040e42010-05-23 20:28:19 -07002352static int pl330_alloc_chan_resources(struct dma_chan *chan)
2353{
2354 struct dma_pl330_chan *pch = to_pchan(chan);
2355 struct dma_pl330_dmac *pdmac = pch->dmac;
2356 unsigned long flags;
2357
2358 spin_lock_irqsave(&pch->lock, flags);
2359
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002360 dma_cookie_init(chan);
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002361 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002362
2363 pch->pl330_chid = pl330_request_channel(&pdmac->pif);
2364 if (!pch->pl330_chid) {
2365 spin_unlock_irqrestore(&pch->lock, flags);
Inderpal Singh02747882012-09-17 09:57:45 +05302366 return -ENOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002367 }
2368
2369 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2370
2371 spin_unlock_irqrestore(&pch->lock, flags);
2372
2373 return 1;
2374}
2375
2376static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2377{
2378 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002379 struct dma_pl330_desc *desc;
Jassi Brarb3040e42010-05-23 20:28:19 -07002380 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002381 struct dma_pl330_dmac *pdmac = pch->dmac;
2382 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002383 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002384
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002385 switch (cmd) {
2386 case DMA_TERMINATE_ALL:
2387 spin_lock_irqsave(&pch->lock, flags);
2388
2389 /* FLUSH the PL330 Channel thread */
2390 pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
2391
2392 /* Mark all desc done */
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002393 list_for_each_entry(desc, &pch->submitted_list, node) {
2394 desc->status = FREE;
2395 dma_cookie_complete(&desc->txd);
2396 }
2397
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002398 list_for_each_entry(desc, &pch->work_list , node) {
2399 desc->status = FREE;
2400 dma_cookie_complete(&desc->txd);
Boojin Kimae43b882011-09-02 09:44:32 +09002401 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002402
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002403 list_for_each_entry(desc, &pch->completed_list , node) {
2404 desc->status = FREE;
2405 dma_cookie_complete(&desc->txd);
2406 }
2407
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002408 list_splice_tail_init(&pch->submitted_list, &pdmac->desc_pool);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002409 list_splice_tail_init(&pch->work_list, &pdmac->desc_pool);
2410 list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002411 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002412 break;
2413 case DMA_SLAVE_CONFIG:
2414 slave_config = (struct dma_slave_config *)arg;
2415
Vinod Kouldb8196d2011-10-13 22:34:23 +05302416 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002417 if (slave_config->dst_addr)
2418 pch->fifo_addr = slave_config->dst_addr;
2419 if (slave_config->dst_addr_width)
2420 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2421 if (slave_config->dst_maxburst)
2422 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302423 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002424 if (slave_config->src_addr)
2425 pch->fifo_addr = slave_config->src_addr;
2426 if (slave_config->src_addr_width)
2427 pch->burst_sz = __ffs(slave_config->src_addr_width);
2428 if (slave_config->src_maxburst)
2429 pch->burst_len = slave_config->src_maxburst;
2430 }
2431 break;
2432 default:
2433 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002434 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002435 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002436
2437 return 0;
2438}
2439
2440static void pl330_free_chan_resources(struct dma_chan *chan)
2441{
2442 struct dma_pl330_chan *pch = to_pchan(chan);
2443 unsigned long flags;
2444
Jassi Brarb3040e42010-05-23 20:28:19 -07002445 tasklet_kill(&pch->task);
2446
Bartlomiej Zolnierkiewiczda331ba2013-07-03 15:00:43 -07002447 spin_lock_irqsave(&pch->lock, flags);
2448
Jassi Brarb3040e42010-05-23 20:28:19 -07002449 pl330_release_channel(pch->pl330_chid);
2450 pch->pl330_chid = NULL;
2451
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002452 if (pch->cyclic)
2453 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2454
Jassi Brarb3040e42010-05-23 20:28:19 -07002455 spin_unlock_irqrestore(&pch->lock, flags);
2456}
2457
2458static enum dma_status
2459pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2460 struct dma_tx_state *txstate)
2461{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002462 return dma_cookie_status(chan, cookie, txstate);
Jassi Brarb3040e42010-05-23 20:28:19 -07002463}
2464
2465static void pl330_issue_pending(struct dma_chan *chan)
2466{
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002467 struct dma_pl330_chan *pch = to_pchan(chan);
2468 unsigned long flags;
2469
2470 spin_lock_irqsave(&pch->lock, flags);
2471 list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2472 spin_unlock_irqrestore(&pch->lock, flags);
2473
2474 pl330_tasklet((unsigned long)pch);
Jassi Brarb3040e42010-05-23 20:28:19 -07002475}
2476
2477/*
2478 * We returned the last one of the circular list of descriptor(s)
2479 * from prep_xxx, so the argument to submit corresponds to the last
2480 * descriptor of the list.
2481 */
2482static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2483{
2484 struct dma_pl330_desc *desc, *last = to_desc(tx);
2485 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2486 dma_cookie_t cookie;
2487 unsigned long flags;
2488
2489 spin_lock_irqsave(&pch->lock, flags);
2490
2491 /* Assign cookies to all nodes */
Jassi Brarb3040e42010-05-23 20:28:19 -07002492 while (!list_empty(&last->node)) {
2493 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002494 if (pch->cyclic) {
2495 desc->txd.callback = last->txd.callback;
2496 desc->txd.callback_param = last->txd.callback_param;
2497 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002498
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002499 dma_cookie_assign(&desc->txd);
Jassi Brarb3040e42010-05-23 20:28:19 -07002500
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002501 list_move_tail(&desc->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002502 }
2503
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002504 cookie = dma_cookie_assign(&last->txd);
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002505 list_add_tail(&last->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002506 spin_unlock_irqrestore(&pch->lock, flags);
2507
2508 return cookie;
2509}
2510
2511static inline void _init_desc(struct dma_pl330_desc *desc)
2512{
2513 desc->pchan = NULL;
2514 desc->req.x = &desc->px;
2515 desc->req.token = desc;
2516 desc->rqcfg.swap = SWAP_NO;
2517 desc->rqcfg.privileged = 0;
2518 desc->rqcfg.insnaccess = 0;
2519 desc->rqcfg.scctl = SCCTRL0;
2520 desc->rqcfg.dcctl = DCCTRL0;
2521 desc->req.cfg = &desc->rqcfg;
2522 desc->req.xfer_cb = dma_pl330_rqcb;
2523 desc->txd.tx_submit = pl330_tx_submit;
2524
2525 INIT_LIST_HEAD(&desc->node);
2526}
2527
2528/* Returns the number of descriptors added to the DMAC pool */
Sachin Kamat5a67ac52012-06-04 17:09:45 +05302529static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
Jassi Brarb3040e42010-05-23 20:28:19 -07002530{
2531 struct dma_pl330_desc *desc;
2532 unsigned long flags;
2533 int i;
2534
2535 if (!pdmac)
2536 return 0;
2537
2538 desc = kmalloc(count * sizeof(*desc), flg);
2539 if (!desc)
2540 return 0;
2541
2542 spin_lock_irqsave(&pdmac->pool_lock, flags);
2543
2544 for (i = 0; i < count; i++) {
2545 _init_desc(&desc[i]);
2546 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2547 }
2548
2549 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2550
2551 return count;
2552}
2553
2554static struct dma_pl330_desc *
2555pluck_desc(struct dma_pl330_dmac *pdmac)
2556{
2557 struct dma_pl330_desc *desc = NULL;
2558 unsigned long flags;
2559
2560 if (!pdmac)
2561 return NULL;
2562
2563 spin_lock_irqsave(&pdmac->pool_lock, flags);
2564
2565 if (!list_empty(&pdmac->desc_pool)) {
2566 desc = list_entry(pdmac->desc_pool.next,
2567 struct dma_pl330_desc, node);
2568
2569 list_del_init(&desc->node);
2570
2571 desc->status = PREP;
2572 desc->txd.callback = NULL;
2573 }
2574
2575 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2576
2577 return desc;
2578}
2579
2580static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2581{
2582 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002583 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002584 struct dma_pl330_desc *desc;
2585
2586 /* Pluck one desc from the pool of DMAC */
2587 desc = pluck_desc(pdmac);
2588
2589 /* If the DMAC pool is empty, alloc new */
2590 if (!desc) {
2591 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2592 return NULL;
2593
2594 /* Try again */
2595 desc = pluck_desc(pdmac);
2596 if (!desc) {
2597 dev_err(pch->dmac->pif.dev,
2598 "%s:%d ALERT!\n", __func__, __LINE__);
2599 return NULL;
2600 }
2601 }
2602
2603 /* Initialize the descriptor */
2604 desc->pchan = pch;
2605 desc->txd.cookie = 0;
2606 async_tx_ack(&desc->txd);
2607
Thomas Abrahamcd072512011-10-24 11:43:11 +02002608 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09002609 desc->rqcfg.pcfg = &pch->dmac->pif.pcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002610
2611 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2612
2613 return desc;
2614}
2615
2616static inline void fill_px(struct pl330_xfer *px,
2617 dma_addr_t dst, dma_addr_t src, size_t len)
2618{
2619 px->next = NULL;
2620 px->bytes = len;
2621 px->dst_addr = dst;
2622 px->src_addr = src;
2623}
2624
2625static struct dma_pl330_desc *
2626__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2627 dma_addr_t src, size_t len)
2628{
2629 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2630
2631 if (!desc) {
2632 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2633 __func__, __LINE__);
2634 return NULL;
2635 }
2636
2637 /*
2638 * Ideally we should lookout for reqs bigger than
2639 * those that can be programmed with 256 bytes of
2640 * MC buffer, but considering a req size is seldom
2641 * going to be word-unaligned and more than 200MB,
2642 * we take it easy.
2643 * Also, should the limit is reached we'd rather
2644 * have the platform increase MC buffer size than
2645 * complicating this API driver.
2646 */
2647 fill_px(&desc->px, dst, src, len);
2648
2649 return desc;
2650}
2651
2652/* Call after fixing burst size */
2653static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2654{
2655 struct dma_pl330_chan *pch = desc->pchan;
2656 struct pl330_info *pi = &pch->dmac->pif;
2657 int burst_len;
2658
2659 burst_len = pi->pcfg.data_bus_width / 8;
2660 burst_len *= pi->pcfg.data_buf_dep;
2661 burst_len >>= desc->rqcfg.brst_size;
2662
2663 /* src/dst_burst_len can't be more than 16 */
2664 if (burst_len > 16)
2665 burst_len = 16;
2666
2667 while (burst_len > 1) {
2668 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2669 break;
2670 burst_len--;
2671 }
2672
2673 return burst_len;
2674}
2675
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002676static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2677 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002678 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002679 unsigned long flags, void *context)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002680{
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002681 struct dma_pl330_desc *desc = NULL, *first = NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002682 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002683 struct dma_pl330_dmac *pdmac = pch->dmac;
2684 unsigned int i;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002685 dma_addr_t dst;
2686 dma_addr_t src;
2687
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002688 if (len % period_len != 0)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002689 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002690
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002691 if (!is_slave_direction(direction)) {
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002692 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2693 __func__, __LINE__);
2694 return NULL;
2695 }
2696
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002697 for (i = 0; i < len / period_len; i++) {
2698 desc = pl330_get_desc(pch);
2699 if (!desc) {
2700 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2701 __func__, __LINE__);
2702
2703 if (!first)
2704 return NULL;
2705
2706 spin_lock_irqsave(&pdmac->pool_lock, flags);
2707
2708 while (!list_empty(&first->node)) {
2709 desc = list_entry(first->node.next,
2710 struct dma_pl330_desc, node);
2711 list_move_tail(&desc->node, &pdmac->desc_pool);
2712 }
2713
2714 list_move_tail(&first->node, &pdmac->desc_pool);
2715
2716 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2717
2718 return NULL;
2719 }
2720
2721 switch (direction) {
2722 case DMA_MEM_TO_DEV:
2723 desc->rqcfg.src_inc = 1;
2724 desc->rqcfg.dst_inc = 0;
2725 desc->req.rqtype = MEMTODEV;
2726 src = dma_addr;
2727 dst = pch->fifo_addr;
2728 break;
2729 case DMA_DEV_TO_MEM:
2730 desc->rqcfg.src_inc = 0;
2731 desc->rqcfg.dst_inc = 1;
2732 desc->req.rqtype = DEVTOMEM;
2733 src = pch->fifo_addr;
2734 dst = dma_addr;
2735 break;
2736 default:
2737 break;
2738 }
2739
2740 desc->rqcfg.brst_size = pch->burst_sz;
2741 desc->rqcfg.brst_len = 1;
2742 fill_px(&desc->px, dst, src, period_len);
2743
2744 if (!first)
2745 first = desc;
2746 else
2747 list_add_tail(&desc->node, &first->node);
2748
2749 dma_addr += period_len;
2750 }
2751
2752 if (!desc)
2753 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002754
2755 pch->cyclic = true;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002756 desc->txd.flags = flags;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002757
2758 return &desc->txd;
2759}
2760
Jassi Brarb3040e42010-05-23 20:28:19 -07002761static struct dma_async_tx_descriptor *
2762pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2763 dma_addr_t src, size_t len, unsigned long flags)
2764{
2765 struct dma_pl330_desc *desc;
2766 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002767 struct pl330_info *pi;
2768 int burst;
2769
Rob Herring4e0e6102011-07-25 16:05:04 -05002770 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002771 return NULL;
2772
Jassi Brarb3040e42010-05-23 20:28:19 -07002773 pi = &pch->dmac->pif;
2774
2775 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2776 if (!desc)
2777 return NULL;
2778
2779 desc->rqcfg.src_inc = 1;
2780 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002781 desc->req.rqtype = MEMTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002782
2783 /* Select max possible burst size */
2784 burst = pi->pcfg.data_bus_width / 8;
2785
2786 while (burst > 1) {
2787 if (!(len % burst))
2788 break;
2789 burst /= 2;
2790 }
2791
2792 desc->rqcfg.brst_size = 0;
2793 while (burst != (1 << desc->rqcfg.brst_size))
2794 desc->rqcfg.brst_size++;
2795
2796 desc->rqcfg.brst_len = get_burst_len(desc, len);
2797
2798 desc->txd.flags = flags;
2799
2800 return &desc->txd;
2801}
2802
Chanho Park52a9d172013-08-09 20:11:33 +09002803static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac,
2804 struct dma_pl330_desc *first)
2805{
2806 unsigned long flags;
2807 struct dma_pl330_desc *desc;
2808
2809 if (!first)
2810 return;
2811
2812 spin_lock_irqsave(&pdmac->pool_lock, flags);
2813
2814 while (!list_empty(&first->node)) {
2815 desc = list_entry(first->node.next,
2816 struct dma_pl330_desc, node);
2817 list_move_tail(&desc->node, &pdmac->desc_pool);
2818 }
2819
2820 list_move_tail(&first->node, &pdmac->desc_pool);
2821
2822 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2823}
2824
Jassi Brarb3040e42010-05-23 20:28:19 -07002825static struct dma_async_tx_descriptor *
2826pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302827 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002828 unsigned long flg, void *context)
Jassi Brarb3040e42010-05-23 20:28:19 -07002829{
2830 struct dma_pl330_desc *first, *desc = NULL;
2831 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002832 struct scatterlist *sg;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002833 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002834 dma_addr_t addr;
2835
Thomas Abrahamcd072512011-10-24 11:43:11 +02002836 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002837 return NULL;
2838
Boojin Kim1b9bb712011-09-02 09:44:30 +09002839 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002840
2841 first = NULL;
2842
2843 for_each_sg(sgl, sg, sg_len, i) {
2844
2845 desc = pl330_get_desc(pch);
2846 if (!desc) {
2847 struct dma_pl330_dmac *pdmac = pch->dmac;
2848
2849 dev_err(pch->dmac->pif.dev,
2850 "%s:%d Unable to fetch desc\n",
2851 __func__, __LINE__);
Chanho Park52a9d172013-08-09 20:11:33 +09002852 __pl330_giveback_desc(pdmac, first);
Jassi Brarb3040e42010-05-23 20:28:19 -07002853
2854 return NULL;
2855 }
2856
2857 if (!first)
2858 first = desc;
2859 else
2860 list_add_tail(&desc->node, &first->node);
2861
Vinod Kouldb8196d2011-10-13 22:34:23 +05302862 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002863 desc->rqcfg.src_inc = 1;
2864 desc->rqcfg.dst_inc = 0;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002865 desc->req.rqtype = MEMTODEV;
Jassi Brarb3040e42010-05-23 20:28:19 -07002866 fill_px(&desc->px,
2867 addr, sg_dma_address(sg), sg_dma_len(sg));
2868 } else {
2869 desc->rqcfg.src_inc = 0;
2870 desc->rqcfg.dst_inc = 1;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002871 desc->req.rqtype = DEVTOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002872 fill_px(&desc->px,
2873 sg_dma_address(sg), addr, sg_dma_len(sg));
2874 }
2875
Boojin Kim1b9bb712011-09-02 09:44:30 +09002876 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002877 desc->rqcfg.brst_len = 1;
2878 }
2879
2880 /* Return the last desc in the chain */
2881 desc->txd.flags = flg;
2882 return &desc->txd;
2883}
2884
2885static irqreturn_t pl330_irq_handler(int irq, void *data)
2886{
2887 if (pl330_update(data))
2888 return IRQ_HANDLED;
2889 else
2890 return IRQ_NONE;
2891}
2892
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002893#define PL330_DMA_BUSWIDTHS \
2894 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2895 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2896 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2897 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2898 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2899
2900static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2901 struct dma_slave_caps *caps)
2902{
2903 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2904 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2905 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2906 caps->cmd_pause = false;
2907 caps->cmd_terminate = true;
2908
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002909 return 0;
2910}
2911
Bill Pemberton463a1f82012-11-19 13:22:55 -05002912static int
Russell Kingaa25afa2011-02-19 15:55:00 +00002913pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002914{
2915 struct dma_pl330_platdata *pdat;
2916 struct dma_pl330_dmac *pdmac;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302917 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002918 struct pl330_info *pi;
2919 struct dma_device *pd;
2920 struct resource *res;
2921 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002922 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002923
Jingoo Hand4adcc02013-07-30 17:09:11 +09002924 pdat = dev_get_platdata(&adev->dev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002925
Russell King64113012013-06-27 10:29:32 +01002926 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2927 if (ret)
2928 return ret;
2929
Jassi Brarb3040e42010-05-23 20:28:19 -07002930 /* Allocate a new DMAC and its Channels */
Sachin Kamate4d43c12012-11-15 06:27:50 +00002931 pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002932 if (!pdmac) {
2933 dev_err(&adev->dev, "unable to allocate mem\n");
2934 return -ENOMEM;
2935 }
2936
2937 pi = &pdmac->pif;
2938 pi->dev = &adev->dev;
2939 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002940 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002941
2942 res = &adev->res;
Sachin Kamatbcc7fa92013-03-04 14:36:27 +05302943 pi->base = devm_ioremap_resource(&adev->dev, res);
2944 if (IS_ERR(pi->base))
2945 return PTR_ERR(pi->base);
Jassi Brarb3040e42010-05-23 20:28:19 -07002946
Boojin Kima2f52032011-09-02 09:44:29 +09002947 amba_set_drvdata(adev, pdmac);
2948
Dan Carpenter02808b42013-11-08 12:50:24 +03002949 for (i = 0; i < AMBA_NR_IRQS; i++) {
Michal Simeke98b3ca2013-09-30 08:50:48 +02002950 irq = adev->irq[i];
2951 if (irq) {
2952 ret = devm_request_irq(&adev->dev, irq,
2953 pl330_irq_handler, 0,
2954 dev_name(&adev->dev), pi);
2955 if (ret)
2956 return ret;
2957 } else {
2958 break;
2959 }
2960 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002961
Will Deacon09677172013-06-10 19:34:37 +01002962 pi->pcfg.periph_id = adev->periphid;
Jassi Brarb3040e42010-05-23 20:28:19 -07002963 ret = pl330_add(pi);
2964 if (ret)
Michal Simek173e8382013-09-04 16:40:17 +02002965 return ret;
Jassi Brarb3040e42010-05-23 20:28:19 -07002966
2967 INIT_LIST_HEAD(&pdmac->desc_pool);
2968 spin_lock_init(&pdmac->pool_lock);
2969
2970 /* Create a descriptor pool of default size */
2971 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2972 dev_warn(&adev->dev, "unable to allocate desc\n");
2973
2974 pd = &pdmac->ddma;
2975 INIT_LIST_HEAD(&pd->channels);
2976
2977 /* Initialize channel parameters */
Olof Johanssonc8473822012-04-08 16:26:19 -07002978 if (pdat)
2979 num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan);
2980 else
2981 num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan);
2982
Rob Herring4e0e6102011-07-25 16:05:04 -05002983 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Sachin Kamat61c6e752012-09-17 15:20:23 +05302984 if (!pdmac->peripherals) {
2985 ret = -ENOMEM;
2986 dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n");
Sachin Kamate4d43c12012-11-15 06:27:50 +00002987 goto probe_err2;
Sachin Kamat61c6e752012-09-17 15:20:23 +05302988 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002989
Rob Herring4e0e6102011-07-25 16:05:04 -05002990 for (i = 0; i < num_chan; i++) {
2991 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002992 if (!adev->dev.of_node)
2993 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2994 else
2995 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002996
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002997 INIT_LIST_HEAD(&pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002998 INIT_LIST_HEAD(&pch->work_list);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002999 INIT_LIST_HEAD(&pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07003000 spin_lock_init(&pch->lock);
3001 pch->pl330_chid = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07003002 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07003003 pch->dmac = pdmac;
3004
3005 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07003006 list_add_tail(&pch->chan.device_node, &pd->channels);
3007 }
3008
3009 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02003010 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02003011 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02003012 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02003013 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02003014 if (pi->pcfg.num_peri) {
3015 dma_cap_set(DMA_SLAVE, pd->cap_mask);
3016 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
Tushar Behera5557a412012-08-29 10:16:25 +05303017 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02003018 }
3019 }
Jassi Brarb3040e42010-05-23 20:28:19 -07003020
3021 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
3022 pd->device_free_chan_resources = pl330_free_chan_resources;
3023 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09003024 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07003025 pd->device_tx_status = pl330_tx_status;
3026 pd->device_prep_slave_sg = pl330_prep_slave_sg;
3027 pd->device_control = pl330_control;
3028 pd->device_issue_pending = pl330_issue_pending;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02003029 pd->device_slave_caps = pl330_dma_device_slave_caps;
Jassi Brarb3040e42010-05-23 20:28:19 -07003030
3031 ret = dma_async_device_register(pd);
3032 if (ret) {
3033 dev_err(&adev->dev, "unable to register DMAC\n");
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303034 goto probe_err3;
3035 }
3036
3037 if (adev->dev.of_node) {
3038 ret = of_dma_controller_register(adev->dev.of_node,
3039 of_dma_pl330_xlate, pdmac);
3040 if (ret) {
3041 dev_err(&adev->dev,
3042 "unable to register DMA to the generic DT DMA helpers\n");
3043 }
Jassi Brarb3040e42010-05-23 20:28:19 -07003044 }
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01003045
3046 adev->dev.dma_parms = &pdmac->dma_parms;
3047
Vinod Kouldbaf6d82013-09-02 21:54:48 +05303048 /*
3049 * This is the limit for transfers with a buswidth of 1, larger
3050 * buswidths will have larger limits.
3051 */
3052 ret = dma_set_max_seg_size(&adev->dev, 1900800);
3053 if (ret)
3054 dev_err(&adev->dev, "unable to set the seg size\n");
3055
Jassi Brarb3040e42010-05-23 20:28:19 -07003056
Jassi Brarb3040e42010-05-23 20:28:19 -07003057 dev_info(&adev->dev,
3058 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
3059 dev_info(&adev->dev,
3060 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
3061 pi->pcfg.data_buf_dep,
3062 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
3063 pi->pcfg.num_peri, pi->pcfg.num_events);
3064
3065 return 0;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303066probe_err3:
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303067 /* Idle the DMAC */
3068 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3069 chan.device_node) {
3070
3071 /* Remove the channel */
3072 list_del(&pch->chan.device_node);
3073
3074 /* Flush the channel */
3075 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3076 pl330_free_chan_resources(&pch->chan);
3077 }
Jassi Brarb3040e42010-05-23 20:28:19 -07003078probe_err2:
Sachin Kamate4d43c12012-11-15 06:27:50 +00003079 pl330_del(pi);
Jassi Brarb3040e42010-05-23 20:28:19 -07003080
3081 return ret;
3082}
3083
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08003084static int pl330_remove(struct amba_device *adev)
Jassi Brarb3040e42010-05-23 20:28:19 -07003085{
3086 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
3087 struct dma_pl330_chan *pch, *_p;
3088 struct pl330_info *pi;
Jassi Brarb3040e42010-05-23 20:28:19 -07003089
3090 if (!pdmac)
3091 return 0;
3092
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303093 if (adev->dev.of_node)
3094 of_dma_controller_free(adev->dev.of_node);
Padmavathi Venna421da892013-02-14 09:10:07 +05303095
Padmavathi Venna0b94c572013-03-05 14:55:31 +05303096 dma_async_device_unregister(&pdmac->ddma);
Jassi Brarb3040e42010-05-23 20:28:19 -07003097
3098 /* Idle the DMAC */
3099 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
3100 chan.device_node) {
3101
3102 /* Remove the channel */
3103 list_del(&pch->chan.device_node);
3104
3105 /* Flush the channel */
3106 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
3107 pl330_free_chan_resources(&pch->chan);
3108 }
3109
3110 pi = &pdmac->pif;
3111
3112 pl330_del(pi);
3113
Jassi Brarb3040e42010-05-23 20:28:19 -07003114 return 0;
3115}
3116
3117static struct amba_id pl330_ids[] = {
3118 {
3119 .id = 0x00041330,
3120 .mask = 0x000fffff,
3121 },
3122 { 0, 0 },
3123};
3124
Dave Martine8fa5162011-10-05 15:15:20 +01003125MODULE_DEVICE_TABLE(amba, pl330_ids);
3126
Jassi Brarb3040e42010-05-23 20:28:19 -07003127static struct amba_driver pl330_driver = {
3128 .drv = {
3129 .owner = THIS_MODULE,
3130 .name = "dma-pl330",
Jassi Brarb3040e42010-05-23 20:28:19 -07003131 },
3132 .id_table = pl330_ids,
3133 .probe = pl330_probe,
3134 .remove = pl330_remove,
3135};
3136
viresh kumar9e5ed092012-03-15 10:40:38 +01003137module_amba_driver(pl330_driver);
Jassi Brarb3040e42010-05-23 20:28:19 -07003138
3139MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3140MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3141MODULE_LICENSE("GPL");