blob: f0e9f47e7dc65908c143402e2cd3bfdf87d8b7cb [file] [log] [blame]
Boojin Kimb7d861d2011-12-26 18:49:52 +09001/*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
Jassi Brarb3040e42010-05-23 20:28:19 -07004 *
5 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassi.brar@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Boojin Kimb7d861d2011-12-26 18:49:52 +090014#include <linux/kernel.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070015#include <linux/io.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/module.h>
Boojin Kimb7d861d2011-12-26 18:49:52 +090019#include <linux/string.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/dma-mapping.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070023#include <linux/dmaengine.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070024#include <linux/amba/bus.h>
25#include <linux/amba/pl330.h>
Boojin Kim1b9bb712011-09-02 09:44:30 +090026#include <linux/scatterlist.h>
Thomas Abraham93ed5542011-10-24 11:43:31 +020027#include <linux/of.h>
Padmavathi Vennaa80258f2013-02-14 09:10:06 +053028#include <linux/of_dma.h>
Sachin Kamatbcc7fa92013-03-04 14:36:27 +053029#include <linux/err.h>
Jassi Brarb3040e42010-05-23 20:28:19 -070030
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031#include "dmaengine.h"
Boojin Kimb7d861d2011-12-26 18:49:52 +090032#define PL330_MAX_CHAN 8
33#define PL330_MAX_IRQS 32
34#define PL330_MAX_PERI 32
35
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +020036enum pl330_cachectrl {
37 CCTRL0, /* Noncacheable and nonbufferable */
38 CCTRL1, /* Bufferable only */
39 CCTRL2, /* Cacheable, but do not allocate */
40 CCTRL3, /* Cacheable and bufferable, but do not allocate */
41 INVALID1, /* AWCACHE = 0x1000 */
42 INVALID2,
43 CCTRL6, /* Cacheable write-through, allocate on writes only */
44 CCTRL7, /* Cacheable write-back, allocate on writes only */
Boojin Kimb7d861d2011-12-26 18:49:52 +090045};
46
47enum pl330_byteswap {
48 SWAP_NO,
49 SWAP_2,
50 SWAP_4,
51 SWAP_8,
52 SWAP_16,
53};
54
Boojin Kimb7d861d2011-12-26 18:49:52 +090055/* Register and Bit field Definitions */
56#define DS 0x0
57#define DS_ST_STOP 0x0
58#define DS_ST_EXEC 0x1
59#define DS_ST_CMISS 0x2
60#define DS_ST_UPDTPC 0x3
61#define DS_ST_WFE 0x4
62#define DS_ST_ATBRR 0x5
63#define DS_ST_QBUSY 0x6
64#define DS_ST_WFP 0x7
65#define DS_ST_KILL 0x8
66#define DS_ST_CMPLT 0x9
67#define DS_ST_FLTCMP 0xe
68#define DS_ST_FAULT 0xf
69
70#define DPC 0x4
71#define INTEN 0x20
72#define ES 0x24
73#define INTSTATUS 0x28
74#define INTCLR 0x2c
75#define FSM 0x30
76#define FSC 0x34
77#define FTM 0x38
78
79#define _FTC 0x40
80#define FTC(n) (_FTC + (n)*0x4)
81
82#define _CS 0x100
83#define CS(n) (_CS + (n)*0x8)
84#define CS_CNS (1 << 21)
85
86#define _CPC 0x104
87#define CPC(n) (_CPC + (n)*0x8)
88
89#define _SA 0x400
90#define SA(n) (_SA + (n)*0x20)
91
92#define _DA 0x404
93#define DA(n) (_DA + (n)*0x20)
94
95#define _CC 0x408
96#define CC(n) (_CC + (n)*0x20)
97
98#define CC_SRCINC (1 << 0)
99#define CC_DSTINC (1 << 14)
100#define CC_SRCPRI (1 << 8)
101#define CC_DSTPRI (1 << 22)
102#define CC_SRCNS (1 << 9)
103#define CC_DSTNS (1 << 23)
104#define CC_SRCIA (1 << 10)
105#define CC_DSTIA (1 << 24)
106#define CC_SRCBRSTLEN_SHFT 4
107#define CC_DSTBRSTLEN_SHFT 18
108#define CC_SRCBRSTSIZE_SHFT 1
109#define CC_DSTBRSTSIZE_SHFT 15
110#define CC_SRCCCTRL_SHFT 11
111#define CC_SRCCCTRL_MASK 0x7
112#define CC_DSTCCTRL_SHFT 25
113#define CC_DRCCCTRL_MASK 0x7
114#define CC_SWAP_SHFT 28
115
116#define _LC0 0x40c
117#define LC0(n) (_LC0 + (n)*0x20)
118
119#define _LC1 0x410
120#define LC1(n) (_LC1 + (n)*0x20)
121
122#define DBGSTATUS 0xd00
123#define DBG_BUSY (1 << 0)
124
125#define DBGCMD 0xd04
126#define DBGINST0 0xd08
127#define DBGINST1 0xd0c
128
129#define CR0 0xe00
130#define CR1 0xe04
131#define CR2 0xe08
132#define CR3 0xe0c
133#define CR4 0xe10
134#define CRD 0xe14
135
136#define PERIPH_ID 0xfe0
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900137#define PERIPH_REV_SHIFT 20
138#define PERIPH_REV_MASK 0xf
139#define PERIPH_REV_R0P0 0
140#define PERIPH_REV_R1P0 1
141#define PERIPH_REV_R1P1 2
Boojin Kimb7d861d2011-12-26 18:49:52 +0900142
143#define CR0_PERIPH_REQ_SET (1 << 0)
144#define CR0_BOOT_EN_SET (1 << 1)
145#define CR0_BOOT_MAN_NS (1 << 2)
146#define CR0_NUM_CHANS_SHIFT 4
147#define CR0_NUM_CHANS_MASK 0x7
148#define CR0_NUM_PERIPH_SHIFT 12
149#define CR0_NUM_PERIPH_MASK 0x1f
150#define CR0_NUM_EVENTS_SHIFT 17
151#define CR0_NUM_EVENTS_MASK 0x1f
152
153#define CR1_ICACHE_LEN_SHIFT 0
154#define CR1_ICACHE_LEN_MASK 0x7
155#define CR1_NUM_ICACHELINES_SHIFT 4
156#define CR1_NUM_ICACHELINES_MASK 0xf
157
158#define CRD_DATA_WIDTH_SHIFT 0
159#define CRD_DATA_WIDTH_MASK 0x7
160#define CRD_WR_CAP_SHIFT 4
161#define CRD_WR_CAP_MASK 0x7
162#define CRD_WR_Q_DEP_SHIFT 8
163#define CRD_WR_Q_DEP_MASK 0xf
164#define CRD_RD_CAP_SHIFT 12
165#define CRD_RD_CAP_MASK 0x7
166#define CRD_RD_Q_DEP_SHIFT 16
167#define CRD_RD_Q_DEP_MASK 0xf
168#define CRD_DATA_BUFF_SHIFT 20
169#define CRD_DATA_BUFF_MASK 0x3ff
170
171#define PART 0x330
172#define DESIGNER 0x41
173#define REVISION 0x0
174#define INTEG_CFG 0x0
175#define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
176
Boojin Kimb7d861d2011-12-26 18:49:52 +0900177#define PL330_STATE_STOPPED (1 << 0)
178#define PL330_STATE_EXECUTING (1 << 1)
179#define PL330_STATE_WFE (1 << 2)
180#define PL330_STATE_FAULTING (1 << 3)
181#define PL330_STATE_COMPLETING (1 << 4)
182#define PL330_STATE_WFP (1 << 5)
183#define PL330_STATE_KILLING (1 << 6)
184#define PL330_STATE_FAULT_COMPLETING (1 << 7)
185#define PL330_STATE_CACHEMISS (1 << 8)
186#define PL330_STATE_UPDTPC (1 << 9)
187#define PL330_STATE_ATBARRIER (1 << 10)
188#define PL330_STATE_QUEUEBUSY (1 << 11)
189#define PL330_STATE_INVALID (1 << 15)
190
191#define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
192 | PL330_STATE_WFE | PL330_STATE_FAULTING)
193
194#define CMD_DMAADDH 0x54
195#define CMD_DMAEND 0x00
196#define CMD_DMAFLUSHP 0x35
197#define CMD_DMAGO 0xa0
198#define CMD_DMALD 0x04
199#define CMD_DMALDP 0x25
200#define CMD_DMALP 0x20
201#define CMD_DMALPEND 0x28
202#define CMD_DMAKILL 0x01
203#define CMD_DMAMOV 0xbc
204#define CMD_DMANOP 0x18
205#define CMD_DMARMB 0x12
206#define CMD_DMASEV 0x34
207#define CMD_DMAST 0x08
208#define CMD_DMASTP 0x29
209#define CMD_DMASTZ 0x0c
210#define CMD_DMAWFE 0x36
211#define CMD_DMAWFP 0x30
212#define CMD_DMAWMB 0x13
213
214#define SZ_DMAADDH 3
215#define SZ_DMAEND 1
216#define SZ_DMAFLUSHP 2
217#define SZ_DMALD 1
218#define SZ_DMALDP 2
219#define SZ_DMALP 2
220#define SZ_DMALPEND 2
221#define SZ_DMAKILL 1
222#define SZ_DMAMOV 6
223#define SZ_DMANOP 1
224#define SZ_DMARMB 1
225#define SZ_DMASEV 2
226#define SZ_DMAST 1
227#define SZ_DMASTP 2
228#define SZ_DMASTZ 1
229#define SZ_DMAWFE 2
230#define SZ_DMAWFP 2
231#define SZ_DMAWMB 1
232#define SZ_DMAGO 6
233
234#define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
235#define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
236
237#define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
238#define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
239
240/*
241 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
242 * at 1byte/burst for P<->M and M<->M respectively.
243 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
244 * should be enough for P<->M and M<->M respectively.
245 */
246#define MCODE_BUFF_PER_REQ 256
247
248/* If the _pl330_req is available to the client */
249#define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
250
251/* Use this _only_ to wait on transient states */
252#define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
253
254#ifdef PL330_DEBUG_MCGEN
255static unsigned cmd_line;
256#define PL330_DBGCMD_DUMP(off, x...) do { \
257 printk("%x:", cmd_line); \
258 printk(x); \
259 cmd_line += off; \
260 } while (0)
261#define PL330_DBGMC_START(addr) (cmd_line = addr)
262#else
263#define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
264#define PL330_DBGMC_START(addr) do {} while (0)
265#endif
266
267/* The number of default descriptors */
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +0000268
Jassi Brarb3040e42010-05-23 20:28:19 -0700269#define NR_DEFAULT_DESC 16
270
Boojin Kimb7d861d2011-12-26 18:49:52 +0900271/* Populated by the PL330 core driver for DMA API driver's info */
272struct pl330_config {
273 u32 periph_id;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900274#define DMAC_MODE_NS (1 << 0)
275 unsigned int mode;
276 unsigned int data_bus_width:10; /* In number of bits */
277 unsigned int data_buf_dep:10;
278 unsigned int num_chan:4;
279 unsigned int num_peri:6;
280 u32 peri_ns;
281 unsigned int num_events:6;
282 u32 irq_ns;
283};
284
285/* Handle to the DMAC provided to the PL330 core */
286struct pl330_info {
287 /* Owning device */
288 struct device *dev;
289 /* Size of MicroCode buffers for each channel. */
290 unsigned mcbufsz;
291 /* ioremap'ed address of PL330 registers. */
292 void __iomem *base;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900293 /* PL330 core data, Client must not touch it. */
294 void *pl330_data;
295 /* Populated by the PL330 core driver during pl330_add */
296 struct pl330_config pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900297};
298
299/**
300 * Request Configuration.
301 * The PL330 core does not modify this and uses the last
302 * working configuration if the request doesn't provide any.
303 *
304 * The Client may want to provide this info only for the
305 * first request and a request with new settings.
306 */
307struct pl330_reqcfg {
308 /* Address Incrementing */
309 unsigned dst_inc:1;
310 unsigned src_inc:1;
311
312 /*
313 * For now, the SRC & DST protection levels
314 * and burst size/length are assumed same.
315 */
316 bool nonsecure;
317 bool privileged;
318 bool insnaccess;
319 unsigned brst_len:5;
320 unsigned brst_size:3; /* in power of 2 */
321
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +0200322 enum pl330_cachectrl dcctl;
323 enum pl330_cachectrl scctl;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900324 enum pl330_byteswap swap;
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900325 struct pl330_config *pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900326};
327
328/*
329 * One cycle of DMAC operation.
330 * There may be more than one xfer in a request.
331 */
332struct pl330_xfer {
333 u32 src_addr;
334 u32 dst_addr;
335 /* Size to xfer */
336 u32 bytes;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900337};
338
339/* The xfer callbacks are made with one of these arguments. */
340enum pl330_op_err {
341 /* The all xfers in the request were success. */
342 PL330_ERR_NONE,
343 /* If req aborted due to global error. */
344 PL330_ERR_ABORT,
345 /* If req failed due to problem with Channel. */
346 PL330_ERR_FAIL,
347};
348
349/* A request defining Scatter-Gather List ending with NULL xfer. */
350struct pl330_req {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +0200351 enum dma_transfer_direction rqtype;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900352 /* Index of peripheral for the xfer. */
353 unsigned peri:5;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900354 /* If NULL, req will be done at last set parameters. */
355 struct pl330_reqcfg *cfg;
356 /* Pointer to first xfer in the request. */
357 struct pl330_xfer *x;
Javi Merinofdec53d2012-06-13 15:07:00 +0100358 /* Hook to attach to DMAC's list of reqs with due callback */
359 struct list_head rqd;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900360};
361
Boojin Kimb7d861d2011-12-26 18:49:52 +0900362enum pl330_chan_op {
363 /* Start the channel */
364 PL330_OP_START,
365 /* Abort the active xfer */
366 PL330_OP_ABORT,
367 /* Stop xfer and flush queue */
368 PL330_OP_FLUSH,
369};
370
371struct _xfer_spec {
372 u32 ccr;
373 struct pl330_req *r;
374 struct pl330_xfer *x;
375};
376
377enum dmamov_dst {
378 SAR = 0,
379 CCR,
380 DAR,
381};
382
383enum pl330_dst {
384 SRC = 0,
385 DST,
386};
387
388enum pl330_cond {
389 SINGLE,
390 BURST,
391 ALWAYS,
392};
393
394struct _pl330_req {
395 u32 mc_bus;
396 void *mc_cpu;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900397 struct pl330_req *r;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900398};
399
400/* ToBeDone for tasklet */
401struct _pl330_tbd {
402 bool reset_dmac;
403 bool reset_mngr;
404 u8 reset_chan;
405};
406
407/* A DMAC Thread */
408struct pl330_thread {
409 u8 id;
410 int ev;
411 /* If the channel is not yet acquired by any client */
412 bool free;
413 /* Parent DMAC */
414 struct pl330_dmac *dmac;
415 /* Only two at a time */
416 struct _pl330_req req[2];
417 /* Index of the last enqueued request */
418 unsigned lstenq;
419 /* Index of the last submitted request or -1 if the DMA is stopped */
420 int req_running;
421};
422
423enum pl330_dmac_state {
424 UNINIT,
425 INIT,
426 DYING,
427};
428
429/* A DMAC */
430struct pl330_dmac {
431 spinlock_t lock;
432 /* Holds list of reqs with due callbacks */
433 struct list_head req_done;
434 /* Pointer to platform specific stuff */
435 struct pl330_info *pinfo;
436 /* Maximum possible events/irqs */
437 int events[32];
438 /* BUS address of MicroCode buffer */
Will Deaconfed8c452013-06-10 19:34:38 +0100439 dma_addr_t mcode_bus;
Boojin Kimb7d861d2011-12-26 18:49:52 +0900440 /* CPU address of MicroCode buffer */
441 void *mcode_cpu;
442 /* List of all Channel threads */
443 struct pl330_thread *channels;
444 /* Pointer to the MANAGER thread */
445 struct pl330_thread *manager;
446 /* To handle bad news in interrupt */
447 struct tasklet_struct tasks;
448 struct _pl330_tbd dmac_tbd;
449 /* State of DMAC operation */
450 enum pl330_dmac_state state;
451};
452
Jassi Brarb3040e42010-05-23 20:28:19 -0700453enum desc_status {
454 /* In the DMAC pool */
455 FREE,
456 /*
Masanari Iidad73111c2012-08-04 23:37:53 +0900457 * Allocated to some channel during prep_xxx
Jassi Brarb3040e42010-05-23 20:28:19 -0700458 * Also may be sitting on the work_list.
459 */
460 PREP,
461 /*
462 * Sitting on the work_list and already submitted
463 * to the PL330 core. Not more than two descriptors
464 * of a channel can be BUSY at any time.
465 */
466 BUSY,
467 /*
468 * Sitting on the channel work_list but xfer done
469 * by PL330 core
470 */
471 DONE,
472};
473
474struct dma_pl330_chan {
475 /* Schedule desc completion */
476 struct tasklet_struct task;
477
478 /* DMA-Engine Channel */
479 struct dma_chan chan;
480
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +0100481 /* List of submitted descriptors */
482 struct list_head submitted_list;
483 /* List of issued descriptors */
Jassi Brarb3040e42010-05-23 20:28:19 -0700484 struct list_head work_list;
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +0200485 /* List of completed descriptors */
486 struct list_head completed_list;
Jassi Brarb3040e42010-05-23 20:28:19 -0700487
488 /* Pointer to the DMAC that manages this channel,
489 * NULL if the channel is available to be acquired.
490 * As the parent, this DMAC also provides descriptors
491 * to the channel.
492 */
493 struct dma_pl330_dmac *dmac;
494
495 /* To protect channel manipulation */
496 spinlock_t lock;
497
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200498 /*
499 * Hardware channel thread of PL330 DMAC. NULL if the channel is
500 * available.
Jassi Brarb3040e42010-05-23 20:28:19 -0700501 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +0200502 struct pl330_thread *thread;
Boojin Kim1b9bb712011-09-02 09:44:30 +0900503
504 /* For D-to-M and M-to-D channels */
505 int burst_sz; /* the peripheral fifo width */
Boojin Kim1d0c1d62011-09-02 09:44:31 +0900506 int burst_len; /* the number of burst */
Boojin Kim1b9bb712011-09-02 09:44:30 +0900507 dma_addr_t fifo_addr;
Boojin Kim42bc9cf2011-09-02 09:44:33 +0900508
509 /* for cyclic capability */
510 bool cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -0700511};
512
513struct dma_pl330_dmac {
514 struct pl330_info pif;
515
516 /* DMA-Engine Device */
517 struct dma_device ddma;
518
Lars-Peter Clausenb714b842013-11-25 16:07:46 +0100519 /* Holds info about sg limitations */
520 struct device_dma_parameters dma_parms;
521
Jassi Brarb3040e42010-05-23 20:28:19 -0700522 /* Pool of descriptors available for the DMAC's channels */
523 struct list_head desc_pool;
524 /* To protect desc_pool manipulation */
525 spinlock_t pool_lock;
526
527 /* Peripheral channels connected to this DMAC */
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +0100528 unsigned int num_peripherals;
Rob Herring4e0e6102011-07-25 16:05:04 -0500529 struct dma_pl330_chan *peripherals; /* keep at end */
Jassi Brarb3040e42010-05-23 20:28:19 -0700530};
531
532struct dma_pl330_desc {
533 /* To attach to a queue as child */
534 struct list_head node;
535
536 /* Descriptor for the DMA Engine API */
537 struct dma_async_tx_descriptor txd;
538
539 /* Xfer for PL330 core */
540 struct pl330_xfer px;
541
542 struct pl330_reqcfg rqcfg;
543 struct pl330_req req;
544
545 enum desc_status status;
546
547 /* The channel which currently holds this desc */
548 struct dma_pl330_chan *pchan;
549};
550
Boojin Kimb7d861d2011-12-26 18:49:52 +0900551static inline bool _queue_empty(struct pl330_thread *thrd)
552{
553 return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
554 ? true : false;
555}
556
557static inline bool _queue_full(struct pl330_thread *thrd)
558{
559 return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
560 ? false : true;
561}
562
563static inline bool is_manager(struct pl330_thread *thrd)
564{
565 struct pl330_dmac *pl330 = thrd->dmac;
566
567 /* MANAGER is indexed at the end */
568 if (thrd->id == pl330->pinfo->pcfg.num_chan)
569 return true;
570 else
571 return false;
572}
573
574/* If manager of the thread is in Non-Secure mode */
575static inline bool _manager_ns(struct pl330_thread *thrd)
576{
577 struct pl330_dmac *pl330 = thrd->dmac;
578
579 return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
580}
581
Boojin Kim3ecf51a2011-12-26 18:55:47 +0900582static inline u32 get_revision(u32 periph_id)
583{
584 return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
585}
586
Boojin Kimb7d861d2011-12-26 18:49:52 +0900587static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
588 enum pl330_dst da, u16 val)
589{
590 if (dry_run)
591 return SZ_DMAADDH;
592
593 buf[0] = CMD_DMAADDH;
594 buf[0] |= (da << 1);
595 *((u16 *)&buf[1]) = val;
596
597 PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
598 da == 1 ? "DA" : "SA", val);
599
600 return SZ_DMAADDH;
601}
602
603static inline u32 _emit_END(unsigned dry_run, u8 buf[])
604{
605 if (dry_run)
606 return SZ_DMAEND;
607
608 buf[0] = CMD_DMAEND;
609
610 PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
611
612 return SZ_DMAEND;
613}
614
615static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
616{
617 if (dry_run)
618 return SZ_DMAFLUSHP;
619
620 buf[0] = CMD_DMAFLUSHP;
621
622 peri &= 0x1f;
623 peri <<= 3;
624 buf[1] = peri;
625
626 PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
627
628 return SZ_DMAFLUSHP;
629}
630
631static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
632{
633 if (dry_run)
634 return SZ_DMALD;
635
636 buf[0] = CMD_DMALD;
637
638 if (cond == SINGLE)
639 buf[0] |= (0 << 1) | (1 << 0);
640 else if (cond == BURST)
641 buf[0] |= (1 << 1) | (1 << 0);
642
643 PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
644 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
645
646 return SZ_DMALD;
647}
648
649static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
650 enum pl330_cond cond, u8 peri)
651{
652 if (dry_run)
653 return SZ_DMALDP;
654
655 buf[0] = CMD_DMALDP;
656
657 if (cond == BURST)
658 buf[0] |= (1 << 1);
659
660 peri &= 0x1f;
661 peri <<= 3;
662 buf[1] = peri;
663
664 PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
665 cond == SINGLE ? 'S' : 'B', peri >> 3);
666
667 return SZ_DMALDP;
668}
669
670static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
671 unsigned loop, u8 cnt)
672{
673 if (dry_run)
674 return SZ_DMALP;
675
676 buf[0] = CMD_DMALP;
677
678 if (loop)
679 buf[0] |= (1 << 1);
680
681 cnt--; /* DMAC increments by 1 internally */
682 buf[1] = cnt;
683
684 PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
685
686 return SZ_DMALP;
687}
688
689struct _arg_LPEND {
690 enum pl330_cond cond;
691 bool forever;
692 unsigned loop;
693 u8 bjump;
694};
695
696static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
697 const struct _arg_LPEND *arg)
698{
699 enum pl330_cond cond = arg->cond;
700 bool forever = arg->forever;
701 unsigned loop = arg->loop;
702 u8 bjump = arg->bjump;
703
704 if (dry_run)
705 return SZ_DMALPEND;
706
707 buf[0] = CMD_DMALPEND;
708
709 if (loop)
710 buf[0] |= (1 << 2);
711
712 if (!forever)
713 buf[0] |= (1 << 4);
714
715 if (cond == SINGLE)
716 buf[0] |= (0 << 1) | (1 << 0);
717 else if (cond == BURST)
718 buf[0] |= (1 << 1) | (1 << 0);
719
720 buf[1] = bjump;
721
722 PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
723 forever ? "FE" : "END",
724 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
725 loop ? '1' : '0',
726 bjump);
727
728 return SZ_DMALPEND;
729}
730
731static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
732{
733 if (dry_run)
734 return SZ_DMAKILL;
735
736 buf[0] = CMD_DMAKILL;
737
738 return SZ_DMAKILL;
739}
740
741static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
742 enum dmamov_dst dst, u32 val)
743{
744 if (dry_run)
745 return SZ_DMAMOV;
746
747 buf[0] = CMD_DMAMOV;
748 buf[1] = dst;
749 *((u32 *)&buf[2]) = val;
750
751 PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
752 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
753
754 return SZ_DMAMOV;
755}
756
757static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
758{
759 if (dry_run)
760 return SZ_DMANOP;
761
762 buf[0] = CMD_DMANOP;
763
764 PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
765
766 return SZ_DMANOP;
767}
768
769static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
770{
771 if (dry_run)
772 return SZ_DMARMB;
773
774 buf[0] = CMD_DMARMB;
775
776 PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
777
778 return SZ_DMARMB;
779}
780
781static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
782{
783 if (dry_run)
784 return SZ_DMASEV;
785
786 buf[0] = CMD_DMASEV;
787
788 ev &= 0x1f;
789 ev <<= 3;
790 buf[1] = ev;
791
792 PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
793
794 return SZ_DMASEV;
795}
796
797static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
798{
799 if (dry_run)
800 return SZ_DMAST;
801
802 buf[0] = CMD_DMAST;
803
804 if (cond == SINGLE)
805 buf[0] |= (0 << 1) | (1 << 0);
806 else if (cond == BURST)
807 buf[0] |= (1 << 1) | (1 << 0);
808
809 PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
810 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
811
812 return SZ_DMAST;
813}
814
815static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
816 enum pl330_cond cond, u8 peri)
817{
818 if (dry_run)
819 return SZ_DMASTP;
820
821 buf[0] = CMD_DMASTP;
822
823 if (cond == BURST)
824 buf[0] |= (1 << 1);
825
826 peri &= 0x1f;
827 peri <<= 3;
828 buf[1] = peri;
829
830 PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
831 cond == SINGLE ? 'S' : 'B', peri >> 3);
832
833 return SZ_DMASTP;
834}
835
836static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
837{
838 if (dry_run)
839 return SZ_DMASTZ;
840
841 buf[0] = CMD_DMASTZ;
842
843 PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
844
845 return SZ_DMASTZ;
846}
847
848static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
849 unsigned invalidate)
850{
851 if (dry_run)
852 return SZ_DMAWFE;
853
854 buf[0] = CMD_DMAWFE;
855
856 ev &= 0x1f;
857 ev <<= 3;
858 buf[1] = ev;
859
860 if (invalidate)
861 buf[1] |= (1 << 1);
862
863 PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
864 ev >> 3, invalidate ? ", I" : "");
865
866 return SZ_DMAWFE;
867}
868
869static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
870 enum pl330_cond cond, u8 peri)
871{
872 if (dry_run)
873 return SZ_DMAWFP;
874
875 buf[0] = CMD_DMAWFP;
876
877 if (cond == SINGLE)
878 buf[0] |= (0 << 1) | (0 << 0);
879 else if (cond == BURST)
880 buf[0] |= (1 << 1) | (0 << 0);
881 else
882 buf[0] |= (0 << 1) | (1 << 0);
883
884 peri &= 0x1f;
885 peri <<= 3;
886 buf[1] = peri;
887
888 PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
889 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
890
891 return SZ_DMAWFP;
892}
893
894static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
895{
896 if (dry_run)
897 return SZ_DMAWMB;
898
899 buf[0] = CMD_DMAWMB;
900
901 PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
902
903 return SZ_DMAWMB;
904}
905
906struct _arg_GO {
907 u8 chan;
908 u32 addr;
909 unsigned ns;
910};
911
912static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
913 const struct _arg_GO *arg)
914{
915 u8 chan = arg->chan;
916 u32 addr = arg->addr;
917 unsigned ns = arg->ns;
918
919 if (dry_run)
920 return SZ_DMAGO;
921
922 buf[0] = CMD_DMAGO;
923 buf[0] |= (ns << 1);
924
925 buf[1] = chan & 0x7;
926
927 *((u32 *)&buf[2]) = addr;
928
929 return SZ_DMAGO;
930}
931
932#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
933
934/* Returns Time-Out */
935static bool _until_dmac_idle(struct pl330_thread *thrd)
936{
937 void __iomem *regs = thrd->dmac->pinfo->base;
938 unsigned long loops = msecs_to_loops(5);
939
940 do {
941 /* Until Manager is Idle */
942 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
943 break;
944
945 cpu_relax();
946 } while (--loops);
947
948 if (!loops)
949 return true;
950
951 return false;
952}
953
954static inline void _execute_DBGINSN(struct pl330_thread *thrd,
955 u8 insn[], bool as_manager)
956{
957 void __iomem *regs = thrd->dmac->pinfo->base;
958 u32 val;
959
960 val = (insn[0] << 16) | (insn[1] << 24);
961 if (!as_manager) {
962 val |= (1 << 0);
963 val |= (thrd->id << 8); /* Channel Number */
964 }
965 writel(val, regs + DBGINST0);
966
967 val = *((u32 *)&insn[2]);
968 writel(val, regs + DBGINST1);
969
970 /* If timed out due to halted state-machine */
971 if (_until_dmac_idle(thrd)) {
972 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
973 return;
974 }
975
976 /* Get going */
977 writel(0, regs + DBGCMD);
978}
979
980/*
981 * Mark a _pl330_req as free.
982 * We do it by writing DMAEND as the first instruction
983 * because no valid request is going to have DMAEND as
984 * its first instruction to execute.
985 */
986static void mark_free(struct pl330_thread *thrd, int idx)
987{
988 struct _pl330_req *req = &thrd->req[idx];
989
990 _emit_END(0, req->mc_cpu);
Boojin Kimb7d861d2011-12-26 18:49:52 +0900991
992 thrd->req_running = -1;
993}
994
995static inline u32 _state(struct pl330_thread *thrd)
996{
997 void __iomem *regs = thrd->dmac->pinfo->base;
998 u32 val;
999
1000 if (is_manager(thrd))
1001 val = readl(regs + DS) & 0xf;
1002 else
1003 val = readl(regs + CS(thrd->id)) & 0xf;
1004
1005 switch (val) {
1006 case DS_ST_STOP:
1007 return PL330_STATE_STOPPED;
1008 case DS_ST_EXEC:
1009 return PL330_STATE_EXECUTING;
1010 case DS_ST_CMISS:
1011 return PL330_STATE_CACHEMISS;
1012 case DS_ST_UPDTPC:
1013 return PL330_STATE_UPDTPC;
1014 case DS_ST_WFE:
1015 return PL330_STATE_WFE;
1016 case DS_ST_FAULT:
1017 return PL330_STATE_FAULTING;
1018 case DS_ST_ATBRR:
1019 if (is_manager(thrd))
1020 return PL330_STATE_INVALID;
1021 else
1022 return PL330_STATE_ATBARRIER;
1023 case DS_ST_QBUSY:
1024 if (is_manager(thrd))
1025 return PL330_STATE_INVALID;
1026 else
1027 return PL330_STATE_QUEUEBUSY;
1028 case DS_ST_WFP:
1029 if (is_manager(thrd))
1030 return PL330_STATE_INVALID;
1031 else
1032 return PL330_STATE_WFP;
1033 case DS_ST_KILL:
1034 if (is_manager(thrd))
1035 return PL330_STATE_INVALID;
1036 else
1037 return PL330_STATE_KILLING;
1038 case DS_ST_CMPLT:
1039 if (is_manager(thrd))
1040 return PL330_STATE_INVALID;
1041 else
1042 return PL330_STATE_COMPLETING;
1043 case DS_ST_FLTCMP:
1044 if (is_manager(thrd))
1045 return PL330_STATE_INVALID;
1046 else
1047 return PL330_STATE_FAULT_COMPLETING;
1048 default:
1049 return PL330_STATE_INVALID;
1050 }
1051}
1052
1053static void _stop(struct pl330_thread *thrd)
1054{
1055 void __iomem *regs = thrd->dmac->pinfo->base;
1056 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1057
1058 if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
1059 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1060
1061 /* Return if nothing needs to be done */
1062 if (_state(thrd) == PL330_STATE_COMPLETING
1063 || _state(thrd) == PL330_STATE_KILLING
1064 || _state(thrd) == PL330_STATE_STOPPED)
1065 return;
1066
1067 _emit_KILL(0, insn);
1068
1069 /* Stop generating interrupts for SEV */
1070 writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
1071
1072 _execute_DBGINSN(thrd, insn, is_manager(thrd));
1073}
1074
1075/* Start doing req 'idx' of thread 'thrd' */
1076static bool _trigger(struct pl330_thread *thrd)
1077{
1078 void __iomem *regs = thrd->dmac->pinfo->base;
1079 struct _pl330_req *req;
1080 struct pl330_req *r;
1081 struct _arg_GO go;
1082 unsigned ns;
1083 u8 insn[6] = {0, 0, 0, 0, 0, 0};
1084 int idx;
1085
1086 /* Return if already ACTIVE */
1087 if (_state(thrd) != PL330_STATE_STOPPED)
1088 return true;
1089
1090 idx = 1 - thrd->lstenq;
1091 if (!IS_FREE(&thrd->req[idx]))
1092 req = &thrd->req[idx];
1093 else {
1094 idx = thrd->lstenq;
1095 if (!IS_FREE(&thrd->req[idx]))
1096 req = &thrd->req[idx];
1097 else
1098 req = NULL;
1099 }
1100
1101 /* Return if no request */
1102 if (!req || !req->r)
1103 return true;
1104
1105 r = req->r;
1106
1107 if (r->cfg)
1108 ns = r->cfg->nonsecure ? 1 : 0;
1109 else if (readl(regs + CS(thrd->id)) & CS_CNS)
1110 ns = 1;
1111 else
1112 ns = 0;
1113
1114 /* See 'Abort Sources' point-4 at Page 2-25 */
1115 if (_manager_ns(thrd) && !ns)
1116 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
1117 __func__, __LINE__);
1118
1119 go.chan = thrd->id;
1120 go.addr = req->mc_bus;
1121 go.ns = ns;
1122 _emit_GO(0, insn, &go);
1123
1124 /* Set to generate interrupts for SEV */
1125 writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1126
1127 /* Only manager can execute GO */
1128 _execute_DBGINSN(thrd, insn, true);
1129
1130 thrd->req_running = idx;
1131
1132 return true;
1133}
1134
1135static bool _start(struct pl330_thread *thrd)
1136{
1137 switch (_state(thrd)) {
1138 case PL330_STATE_FAULT_COMPLETING:
1139 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1140
1141 if (_state(thrd) == PL330_STATE_KILLING)
1142 UNTIL(thrd, PL330_STATE_STOPPED)
1143
1144 case PL330_STATE_FAULTING:
1145 _stop(thrd);
1146
1147 case PL330_STATE_KILLING:
1148 case PL330_STATE_COMPLETING:
1149 UNTIL(thrd, PL330_STATE_STOPPED)
1150
1151 case PL330_STATE_STOPPED:
1152 return _trigger(thrd);
1153
1154 case PL330_STATE_WFP:
1155 case PL330_STATE_QUEUEBUSY:
1156 case PL330_STATE_ATBARRIER:
1157 case PL330_STATE_UPDTPC:
1158 case PL330_STATE_CACHEMISS:
1159 case PL330_STATE_EXECUTING:
1160 return true;
1161
1162 case PL330_STATE_WFE: /* For RESUME, nothing yet */
1163 default:
1164 return false;
1165 }
1166}
1167
1168static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1169 const struct _xfer_spec *pxs, int cyc)
1170{
1171 int off = 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001172 struct pl330_config *pcfg = pxs->r->cfg->pcfg;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001173
Boojin Kim3ecf51a2011-12-26 18:55:47 +09001174 /* check lock-up free version */
1175 if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1176 while (cyc--) {
1177 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1178 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1179 }
1180 } else {
1181 while (cyc--) {
1182 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1183 off += _emit_RMB(dry_run, &buf[off]);
1184 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1185 off += _emit_WMB(dry_run, &buf[off]);
1186 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001187 }
1188
1189 return off;
1190}
1191
1192static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
1193 const struct _xfer_spec *pxs, int cyc)
1194{
1195 int off = 0;
1196
1197 while (cyc--) {
1198 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1199 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1200 off += _emit_ST(dry_run, &buf[off], ALWAYS);
1201 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1202 }
1203
1204 return off;
1205}
1206
1207static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1208 const struct _xfer_spec *pxs, int cyc)
1209{
1210 int off = 0;
1211
1212 while (cyc--) {
1213 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1214 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1215 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1216 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1217 }
1218
1219 return off;
1220}
1221
1222static int _bursts(unsigned dry_run, u8 buf[],
1223 const struct _xfer_spec *pxs, int cyc)
1224{
1225 int off = 0;
1226
1227 switch (pxs->r->rqtype) {
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001228 case DMA_MEM_TO_DEV:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001229 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1230 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001231 case DMA_DEV_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001232 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1233 break;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001234 case DMA_MEM_TO_MEM:
Boojin Kimb7d861d2011-12-26 18:49:52 +09001235 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1236 break;
1237 default:
1238 off += 0x40000000; /* Scare off the Client */
1239 break;
1240 }
1241
1242 return off;
1243}
1244
1245/* Returns bytes consumed and updates bursts */
1246static inline int _loop(unsigned dry_run, u8 buf[],
1247 unsigned long *bursts, const struct _xfer_spec *pxs)
1248{
1249 int cyc, cycmax, szlp, szlpend, szbrst, off;
1250 unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1251 struct _arg_LPEND lpend;
1252
1253 /* Max iterations possible in DMALP is 256 */
1254 if (*bursts >= 256*256) {
1255 lcnt1 = 256;
1256 lcnt0 = 256;
1257 cyc = *bursts / lcnt1 / lcnt0;
1258 } else if (*bursts > 256) {
1259 lcnt1 = 256;
1260 lcnt0 = *bursts / lcnt1;
1261 cyc = 1;
1262 } else {
1263 lcnt1 = *bursts;
1264 lcnt0 = 0;
1265 cyc = 1;
1266 }
1267
1268 szlp = _emit_LP(1, buf, 0, 0);
1269 szbrst = _bursts(1, buf, pxs, 1);
1270
1271 lpend.cond = ALWAYS;
1272 lpend.forever = false;
1273 lpend.loop = 0;
1274 lpend.bjump = 0;
1275 szlpend = _emit_LPEND(1, buf, &lpend);
1276
1277 if (lcnt0) {
1278 szlp *= 2;
1279 szlpend *= 2;
1280 }
1281
1282 /*
1283 * Max bursts that we can unroll due to limit on the
1284 * size of backward jump that can be encoded in DMALPEND
1285 * which is 8-bits and hence 255
1286 */
1287 cycmax = (255 - (szlp + szlpend)) / szbrst;
1288
1289 cyc = (cycmax < cyc) ? cycmax : cyc;
1290
1291 off = 0;
1292
1293 if (lcnt0) {
1294 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1295 ljmp0 = off;
1296 }
1297
1298 off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1299 ljmp1 = off;
1300
1301 off += _bursts(dry_run, &buf[off], pxs, cyc);
1302
1303 lpend.cond = ALWAYS;
1304 lpend.forever = false;
1305 lpend.loop = 1;
1306 lpend.bjump = off - ljmp1;
1307 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1308
1309 if (lcnt0) {
1310 lpend.cond = ALWAYS;
1311 lpend.forever = false;
1312 lpend.loop = 0;
1313 lpend.bjump = off - ljmp0;
1314 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1315 }
1316
1317 *bursts = lcnt1 * cyc;
1318 if (lcnt0)
1319 *bursts *= lcnt0;
1320
1321 return off;
1322}
1323
1324static inline int _setup_loops(unsigned dry_run, u8 buf[],
1325 const struct _xfer_spec *pxs)
1326{
1327 struct pl330_xfer *x = pxs->x;
1328 u32 ccr = pxs->ccr;
1329 unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1330 int off = 0;
1331
1332 while (bursts) {
1333 c = bursts;
1334 off += _loop(dry_run, &buf[off], &c, pxs);
1335 bursts -= c;
1336 }
1337
1338 return off;
1339}
1340
1341static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1342 const struct _xfer_spec *pxs)
1343{
1344 struct pl330_xfer *x = pxs->x;
1345 int off = 0;
1346
1347 /* DMAMOV SAR, x->src_addr */
1348 off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1349 /* DMAMOV DAR, x->dst_addr */
1350 off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1351
1352 /* Setup Loop(s) */
1353 off += _setup_loops(dry_run, &buf[off], pxs);
1354
1355 return off;
1356}
1357
1358/*
1359 * A req is a sequence of one or more xfer units.
1360 * Returns the number of bytes taken to setup the MC for the req.
1361 */
1362static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1363 unsigned index, struct _xfer_spec *pxs)
1364{
1365 struct _pl330_req *req = &thrd->req[index];
1366 struct pl330_xfer *x;
1367 u8 *buf = req->mc_cpu;
1368 int off = 0;
1369
1370 PL330_DBGMC_START(req->mc_bus);
1371
1372 /* DMAMOV CCR, ccr */
1373 off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1374
1375 x = pxs->r->x;
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001376 /* Error if xfer length is not aligned at burst size */
1377 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1378 return -EINVAL;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001379
Lars-Peter Clausend5cef122014-07-06 20:32:23 +02001380 pxs->x = x;
1381 off += _setup_xfer(dry_run, &buf[off], pxs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001382
1383 /* DMASEV peripheral/event */
1384 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1385 /* DMAEND */
1386 off += _emit_END(dry_run, &buf[off]);
1387
1388 return off;
1389}
1390
1391static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1392{
1393 u32 ccr = 0;
1394
1395 if (rqc->src_inc)
1396 ccr |= CC_SRCINC;
1397
1398 if (rqc->dst_inc)
1399 ccr |= CC_DSTINC;
1400
1401 /* We set same protection levels for Src and DST for now */
1402 if (rqc->privileged)
1403 ccr |= CC_SRCPRI | CC_DSTPRI;
1404 if (rqc->nonsecure)
1405 ccr |= CC_SRCNS | CC_DSTNS;
1406 if (rqc->insnaccess)
1407 ccr |= CC_SRCIA | CC_DSTIA;
1408
1409 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1410 ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1411
1412 ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1413 ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1414
1415 ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1416 ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1417
1418 ccr |= (rqc->swap << CC_SWAP_SHFT);
1419
1420 return ccr;
1421}
1422
1423static inline bool _is_valid(u32 ccr)
1424{
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02001425 enum pl330_cachectrl dcctl;
1426 enum pl330_cachectrl scctl;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001427
1428 dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1429 scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1430
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02001431 if (dcctl == INVALID1 || dcctl == INVALID2
1432 || scctl == INVALID1 || scctl == INVALID2)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001433 return false;
1434 else
1435 return true;
1436}
1437
1438/*
1439 * Submit a list of xfers after which the client wants notification.
1440 * Client is not notified after each xfer unit, just once after all
1441 * xfer units are done or some error occurs.
1442 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001443static int pl330_submit_req(struct pl330_thread *thrd, struct pl330_req *r)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001444{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001445 struct pl330_dmac *pl330;
1446 struct pl330_info *pi;
1447 struct _xfer_spec xs;
1448 unsigned long flags;
1449 void __iomem *regs;
1450 unsigned idx;
1451 u32 ccr;
1452 int ret = 0;
1453
1454 /* No Req or Unacquired Channel or DMAC */
1455 if (!r || !thrd || thrd->free)
1456 return -EINVAL;
1457
1458 pl330 = thrd->dmac;
1459 pi = pl330->pinfo;
1460 regs = pi->base;
1461
1462 if (pl330->state == DYING
1463 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1464 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1465 __func__, __LINE__);
1466 return -EAGAIN;
1467 }
1468
1469 /* If request for non-existing peripheral */
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02001470 if (r->rqtype != DMA_MEM_TO_MEM && r->peri >= pi->pcfg.num_peri) {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001471 dev_info(thrd->dmac->pinfo->dev,
1472 "%s:%d Invalid peripheral(%u)!\n",
1473 __func__, __LINE__, r->peri);
1474 return -EINVAL;
1475 }
1476
1477 spin_lock_irqsave(&pl330->lock, flags);
1478
1479 if (_queue_full(thrd)) {
1480 ret = -EAGAIN;
1481 goto xfer_exit;
1482 }
1483
Boojin Kimb7d861d2011-12-26 18:49:52 +09001484
1485 /* Use last settings, if not provided */
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301486 if (r->cfg) {
1487 /* Prefer Secure Channel */
1488 if (!_manager_ns(thrd))
1489 r->cfg->nonsecure = 0;
1490 else
1491 r->cfg->nonsecure = 1;
1492
Boojin Kimb7d861d2011-12-26 18:49:52 +09001493 ccr = _prepare_ccr(r->cfg);
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301494 } else {
Boojin Kimb7d861d2011-12-26 18:49:52 +09001495 ccr = readl(regs + CC(thrd->id));
Sachin Kamat2e2c6822012-09-17 15:20:22 +05301496 }
Boojin Kimb7d861d2011-12-26 18:49:52 +09001497
1498 /* If this req doesn't have valid xfer settings */
1499 if (!_is_valid(ccr)) {
1500 ret = -EINVAL;
1501 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1502 __func__, __LINE__, ccr);
1503 goto xfer_exit;
1504 }
1505
1506 idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1507
1508 xs.ccr = ccr;
1509 xs.r = r;
1510
1511 /* First dry run to check if req is acceptable */
1512 ret = _setup_req(1, thrd, idx, &xs);
1513 if (ret < 0)
1514 goto xfer_exit;
1515
1516 if (ret > pi->mcbufsz / 2) {
1517 dev_info(thrd->dmac->pinfo->dev,
1518 "%s:%d Trying increasing mcbufsz\n",
1519 __func__, __LINE__);
1520 ret = -ENOMEM;
1521 goto xfer_exit;
1522 }
1523
1524 /* Hook the request */
1525 thrd->lstenq = idx;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001526 thrd->req[idx].r = r;
Lars-Peter Clausenbe025322014-07-06 20:32:24 +02001527 _setup_req(0, thrd, idx, &xs);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001528
1529 ret = 0;
1530
1531xfer_exit:
1532 spin_unlock_irqrestore(&pl330->lock, flags);
1533
1534 return ret;
1535}
1536
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001537static void dma_pl330_rqcb(struct pl330_req *req, enum pl330_op_err err)
1538{
1539 struct dma_pl330_desc *desc = container_of(req, struct dma_pl330_desc, req);
1540 struct dma_pl330_chan *pch = desc->pchan;
1541 unsigned long flags;
1542
1543 /* If desc aborted */
1544 if (!pch)
1545 return;
1546
1547 spin_lock_irqsave(&pch->lock, flags);
1548
1549 desc->status = DONE;
1550
1551 spin_unlock_irqrestore(&pch->lock, flags);
1552
1553 tasklet_schedule(&pch->task);
1554}
1555
Boojin Kimb7d861d2011-12-26 18:49:52 +09001556static void pl330_dotask(unsigned long data)
1557{
1558 struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1559 struct pl330_info *pi = pl330->pinfo;
1560 unsigned long flags;
1561 int i;
1562
1563 spin_lock_irqsave(&pl330->lock, flags);
1564
1565 /* The DMAC itself gone nuts */
1566 if (pl330->dmac_tbd.reset_dmac) {
1567 pl330->state = DYING;
1568 /* Reset the manager too */
1569 pl330->dmac_tbd.reset_mngr = true;
1570 /* Clear the reset flag */
1571 pl330->dmac_tbd.reset_dmac = false;
1572 }
1573
1574 if (pl330->dmac_tbd.reset_mngr) {
1575 _stop(pl330->manager);
1576 /* Reset all channels */
1577 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1578 /* Clear the reset flag */
1579 pl330->dmac_tbd.reset_mngr = false;
1580 }
1581
1582 for (i = 0; i < pi->pcfg.num_chan; i++) {
1583
1584 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1585 struct pl330_thread *thrd = &pl330->channels[i];
1586 void __iomem *regs = pi->base;
1587 enum pl330_op_err err;
1588
1589 _stop(thrd);
1590
1591 if (readl(regs + FSC) & (1 << thrd->id))
1592 err = PL330_ERR_FAIL;
1593 else
1594 err = PL330_ERR_ABORT;
1595
1596 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001597 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, err);
1598 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, err);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001599 spin_lock_irqsave(&pl330->lock, flags);
1600
1601 thrd->req[0].r = NULL;
1602 thrd->req[1].r = NULL;
1603 mark_free(thrd, 0);
1604 mark_free(thrd, 1);
1605
1606 /* Clear the reset flag */
1607 pl330->dmac_tbd.reset_chan &= ~(1 << i);
1608 }
1609 }
1610
1611 spin_unlock_irqrestore(&pl330->lock, flags);
1612
1613 return;
1614}
1615
1616/* Returns 1 if state was updated, 0 otherwise */
1617static int pl330_update(const struct pl330_info *pi)
1618{
Javi Merinofdec53d2012-06-13 15:07:00 +01001619 struct pl330_req *rqdone, *tmp;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001620 struct pl330_dmac *pl330;
1621 unsigned long flags;
1622 void __iomem *regs;
1623 u32 val;
1624 int id, ev, ret = 0;
1625
1626 if (!pi || !pi->pl330_data)
1627 return 0;
1628
1629 regs = pi->base;
1630 pl330 = pi->pl330_data;
1631
1632 spin_lock_irqsave(&pl330->lock, flags);
1633
1634 val = readl(regs + FSM) & 0x1;
1635 if (val)
1636 pl330->dmac_tbd.reset_mngr = true;
1637 else
1638 pl330->dmac_tbd.reset_mngr = false;
1639
1640 val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1641 pl330->dmac_tbd.reset_chan |= val;
1642 if (val) {
1643 int i = 0;
1644 while (i < pi->pcfg.num_chan) {
1645 if (val & (1 << i)) {
1646 dev_info(pi->dev,
1647 "Reset Channel-%d\t CS-%x FTC-%x\n",
1648 i, readl(regs + CS(i)),
1649 readl(regs + FTC(i)));
1650 _stop(&pl330->channels[i]);
1651 }
1652 i++;
1653 }
1654 }
1655
1656 /* Check which event happened i.e, thread notified */
1657 val = readl(regs + ES);
1658 if (pi->pcfg.num_events < 32
1659 && val & ~((1 << pi->pcfg.num_events) - 1)) {
1660 pl330->dmac_tbd.reset_dmac = true;
1661 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1662 ret = 1;
1663 goto updt_exit;
1664 }
1665
1666 for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1667 if (val & (1 << ev)) { /* Event occurred */
1668 struct pl330_thread *thrd;
1669 u32 inten = readl(regs + INTEN);
1670 int active;
1671
1672 /* Clear the event */
1673 if (inten & (1 << ev))
1674 writel(1 << ev, regs + INTCLR);
1675
1676 ret = 1;
1677
1678 id = pl330->events[ev];
1679
1680 thrd = &pl330->channels[id];
1681
1682 active = thrd->req_running;
1683 if (active == -1) /* Aborted */
1684 continue;
1685
Javi Merinofdec53d2012-06-13 15:07:00 +01001686 /* Detach the req */
1687 rqdone = thrd->req[active].r;
1688 thrd->req[active].r = NULL;
1689
Boojin Kimb7d861d2011-12-26 18:49:52 +09001690 mark_free(thrd, active);
1691
1692 /* Get going again ASAP */
1693 _start(thrd);
1694
1695 /* For now, just make a list of callbacks to be done */
1696 list_add_tail(&rqdone->rqd, &pl330->req_done);
1697 }
1698 }
1699
1700 /* Now that we are in no hurry, do the callbacks */
Javi Merinofdec53d2012-06-13 15:07:00 +01001701 list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) {
1702 list_del(&rqdone->rqd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001703
1704 spin_unlock_irqrestore(&pl330->lock, flags);
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001705 dma_pl330_rqcb(rqdone, PL330_ERR_NONE);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001706 spin_lock_irqsave(&pl330->lock, flags);
1707 }
1708
1709updt_exit:
1710 spin_unlock_irqrestore(&pl330->lock, flags);
1711
1712 if (pl330->dmac_tbd.reset_dmac
1713 || pl330->dmac_tbd.reset_mngr
1714 || pl330->dmac_tbd.reset_chan) {
1715 ret = 1;
1716 tasklet_schedule(&pl330->tasks);
1717 }
1718
1719 return ret;
1720}
1721
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001722static int pl330_chan_ctrl(struct pl330_thread *thrd, enum pl330_chan_op op)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001723{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001724 struct pl330_dmac *pl330;
1725 unsigned long flags;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001726 int ret = 0, active;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001727
1728 if (!thrd || thrd->free || thrd->dmac->state == DYING)
1729 return -EINVAL;
1730
1731 pl330 = thrd->dmac;
Linus Torvaldsef08e782012-03-29 15:34:57 -07001732 active = thrd->req_running;
Boojin Kimb7d861d2011-12-26 18:49:52 +09001733
1734 spin_lock_irqsave(&pl330->lock, flags);
1735
1736 switch (op) {
1737 case PL330_OP_FLUSH:
1738 /* Make sure the channel is stopped */
1739 _stop(thrd);
1740
1741 thrd->req[0].r = NULL;
1742 thrd->req[1].r = NULL;
1743 mark_free(thrd, 0);
1744 mark_free(thrd, 1);
1745 break;
1746
1747 case PL330_OP_ABORT:
1748 /* Make sure the channel is stopped */
1749 _stop(thrd);
1750
1751 /* ABORT is only for the active req */
1752 if (active == -1)
1753 break;
1754
1755 thrd->req[active].r = NULL;
1756 mark_free(thrd, active);
1757
1758 /* Start the next */
1759 case PL330_OP_START:
1760 if ((active == -1) && !_start(thrd))
1761 ret = -EIO;
1762 break;
1763
1764 default:
1765 ret = -EINVAL;
1766 }
1767
1768 spin_unlock_irqrestore(&pl330->lock, flags);
1769 return ret;
1770}
1771
Boojin Kimb7d861d2011-12-26 18:49:52 +09001772/* Reserve an event */
1773static inline int _alloc_event(struct pl330_thread *thrd)
1774{
1775 struct pl330_dmac *pl330 = thrd->dmac;
1776 struct pl330_info *pi = pl330->pinfo;
1777 int ev;
1778
1779 for (ev = 0; ev < pi->pcfg.num_events; ev++)
1780 if (pl330->events[ev] == -1) {
1781 pl330->events[ev] = thrd->id;
1782 return ev;
1783 }
1784
1785 return -1;
1786}
1787
1788static bool _chan_ns(const struct pl330_info *pi, int i)
1789{
1790 return pi->pcfg.irq_ns & (1 << i);
1791}
1792
1793/* Upon success, returns IdentityToken for the
1794 * allocated channel, NULL otherwise.
1795 */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001796static struct pl330_thread *pl330_request_channel(const struct pl330_info *pi)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001797{
1798 struct pl330_thread *thrd = NULL;
1799 struct pl330_dmac *pl330;
1800 unsigned long flags;
1801 int chans, i;
1802
1803 if (!pi || !pi->pl330_data)
1804 return NULL;
1805
1806 pl330 = pi->pl330_data;
1807
1808 if (pl330->state == DYING)
1809 return NULL;
1810
1811 chans = pi->pcfg.num_chan;
1812
1813 spin_lock_irqsave(&pl330->lock, flags);
1814
1815 for (i = 0; i < chans; i++) {
1816 thrd = &pl330->channels[i];
1817 if ((thrd->free) && (!_manager_ns(thrd) ||
1818 _chan_ns(pi, i))) {
1819 thrd->ev = _alloc_event(thrd);
1820 if (thrd->ev >= 0) {
1821 thrd->free = false;
1822 thrd->lstenq = 1;
1823 thrd->req[0].r = NULL;
1824 mark_free(thrd, 0);
1825 thrd->req[1].r = NULL;
1826 mark_free(thrd, 1);
1827 break;
1828 }
1829 }
1830 thrd = NULL;
1831 }
1832
1833 spin_unlock_irqrestore(&pl330->lock, flags);
1834
1835 return thrd;
1836}
1837
1838/* Release an event */
1839static inline void _free_event(struct pl330_thread *thrd, int ev)
1840{
1841 struct pl330_dmac *pl330 = thrd->dmac;
1842 struct pl330_info *pi = pl330->pinfo;
1843
1844 /* If the event is valid and was held by the thread */
1845 if (ev >= 0 && ev < pi->pcfg.num_events
1846 && pl330->events[ev] == thrd->id)
1847 pl330->events[ev] = -1;
1848}
1849
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02001850static void pl330_release_channel(struct pl330_thread *thrd)
Boojin Kimb7d861d2011-12-26 18:49:52 +09001851{
Boojin Kimb7d861d2011-12-26 18:49:52 +09001852 struct pl330_dmac *pl330;
1853 unsigned long flags;
1854
1855 if (!thrd || thrd->free)
1856 return;
1857
1858 _stop(thrd);
1859
Lars-Peter Clausen6079d382014-07-06 20:32:25 +02001860 dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1861 dma_pl330_rqcb(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001862
1863 pl330 = thrd->dmac;
1864
1865 spin_lock_irqsave(&pl330->lock, flags);
1866 _free_event(thrd, thrd->ev);
1867 thrd->free = true;
1868 spin_unlock_irqrestore(&pl330->lock, flags);
1869}
1870
1871/* Initialize the structure for PL330 configuration, that can be used
1872 * by the client driver the make best use of the DMAC
1873 */
1874static void read_dmac_config(struct pl330_info *pi)
1875{
1876 void __iomem *regs = pi->base;
1877 u32 val;
1878
1879 val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1880 val &= CRD_DATA_WIDTH_MASK;
1881 pi->pcfg.data_bus_width = 8 * (1 << val);
1882
1883 val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1884 val &= CRD_DATA_BUFF_MASK;
1885 pi->pcfg.data_buf_dep = val + 1;
1886
1887 val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1888 val &= CR0_NUM_CHANS_MASK;
1889 val += 1;
1890 pi->pcfg.num_chan = val;
1891
1892 val = readl(regs + CR0);
1893 if (val & CR0_PERIPH_REQ_SET) {
1894 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1895 val += 1;
1896 pi->pcfg.num_peri = val;
1897 pi->pcfg.peri_ns = readl(regs + CR4);
1898 } else {
1899 pi->pcfg.num_peri = 0;
1900 }
1901
1902 val = readl(regs + CR0);
1903 if (val & CR0_BOOT_MAN_NS)
1904 pi->pcfg.mode |= DMAC_MODE_NS;
1905 else
1906 pi->pcfg.mode &= ~DMAC_MODE_NS;
1907
1908 val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1909 val &= CR0_NUM_EVENTS_MASK;
1910 val += 1;
1911 pi->pcfg.num_events = val;
1912
1913 pi->pcfg.irq_ns = readl(regs + CR3);
Boojin Kimb7d861d2011-12-26 18:49:52 +09001914}
1915
1916static inline void _reset_thread(struct pl330_thread *thrd)
1917{
1918 struct pl330_dmac *pl330 = thrd->dmac;
1919 struct pl330_info *pi = pl330->pinfo;
1920
1921 thrd->req[0].mc_cpu = pl330->mcode_cpu
1922 + (thrd->id * pi->mcbufsz);
1923 thrd->req[0].mc_bus = pl330->mcode_bus
1924 + (thrd->id * pi->mcbufsz);
1925 thrd->req[0].r = NULL;
1926 mark_free(thrd, 0);
1927
1928 thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1929 + pi->mcbufsz / 2;
1930 thrd->req[1].mc_bus = thrd->req[0].mc_bus
1931 + pi->mcbufsz / 2;
1932 thrd->req[1].r = NULL;
1933 mark_free(thrd, 1);
1934}
1935
1936static int dmac_alloc_threads(struct pl330_dmac *pl330)
1937{
1938 struct pl330_info *pi = pl330->pinfo;
1939 int chans = pi->pcfg.num_chan;
1940 struct pl330_thread *thrd;
1941 int i;
1942
1943 /* Allocate 1 Manager and 'chans' Channel threads */
1944 pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
1945 GFP_KERNEL);
1946 if (!pl330->channels)
1947 return -ENOMEM;
1948
1949 /* Init Channel threads */
1950 for (i = 0; i < chans; i++) {
1951 thrd = &pl330->channels[i];
1952 thrd->id = i;
1953 thrd->dmac = pl330;
1954 _reset_thread(thrd);
1955 thrd->free = true;
1956 }
1957
1958 /* MANAGER is indexed at the end */
1959 thrd = &pl330->channels[chans];
1960 thrd->id = chans;
1961 thrd->dmac = pl330;
1962 thrd->free = false;
1963 pl330->manager = thrd;
1964
1965 return 0;
1966}
1967
1968static int dmac_alloc_resources(struct pl330_dmac *pl330)
1969{
1970 struct pl330_info *pi = pl330->pinfo;
1971 int chans = pi->pcfg.num_chan;
1972 int ret;
1973
1974 /*
1975 * Alloc MicroCode buffer for 'chans' Channel threads.
1976 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1977 */
1978 pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
1979 chans * pi->mcbufsz,
1980 &pl330->mcode_bus, GFP_KERNEL);
1981 if (!pl330->mcode_cpu) {
1982 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
1983 __func__, __LINE__);
1984 return -ENOMEM;
1985 }
1986
1987 ret = dmac_alloc_threads(pl330);
1988 if (ret) {
1989 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
1990 __func__, __LINE__);
1991 dma_free_coherent(pi->dev,
1992 chans * pi->mcbufsz,
1993 pl330->mcode_cpu, pl330->mcode_bus);
1994 return ret;
1995 }
1996
1997 return 0;
1998}
1999
2000static int pl330_add(struct pl330_info *pi)
2001{
2002 struct pl330_dmac *pl330;
2003 void __iomem *regs;
2004 int i, ret;
2005
2006 if (!pi || !pi->dev)
2007 return -EINVAL;
2008
2009 /* If already added */
2010 if (pi->pl330_data)
2011 return -EINVAL;
2012
Boojin Kimb7d861d2011-12-26 18:49:52 +09002013 regs = pi->base;
2014
2015 /* Check if we can handle this DMAC */
Will Deacon09677172013-06-10 19:34:37 +01002016 if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
2017 dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002018 return -EINVAL;
2019 }
2020
2021 /* Read the configuration of the DMAC */
2022 read_dmac_config(pi);
2023
2024 if (pi->pcfg.num_events == 0) {
2025 dev_err(pi->dev, "%s:%d Can't work without events!\n",
2026 __func__, __LINE__);
2027 return -EINVAL;
2028 }
2029
2030 pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
2031 if (!pl330) {
2032 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
2033 __func__, __LINE__);
2034 return -ENOMEM;
2035 }
2036
2037 /* Assign the info structure and private data */
2038 pl330->pinfo = pi;
2039 pi->pl330_data = pl330;
2040
2041 spin_lock_init(&pl330->lock);
2042
2043 INIT_LIST_HEAD(&pl330->req_done);
2044
2045 /* Use default MC buffer size if not provided */
2046 if (!pi->mcbufsz)
2047 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
2048
2049 /* Mark all events as free */
2050 for (i = 0; i < pi->pcfg.num_events; i++)
2051 pl330->events[i] = -1;
2052
2053 /* Allocate resources needed by the DMAC */
2054 ret = dmac_alloc_resources(pl330);
2055 if (ret) {
2056 dev_err(pi->dev, "Unable to create channels for DMAC\n");
2057 kfree(pl330);
2058 return ret;
2059 }
2060
2061 tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
2062
2063 pl330->state = INIT;
2064
2065 return 0;
2066}
2067
2068static int dmac_free_threads(struct pl330_dmac *pl330)
2069{
2070 struct pl330_info *pi = pl330->pinfo;
2071 int chans = pi->pcfg.num_chan;
2072 struct pl330_thread *thrd;
2073 int i;
2074
2075 /* Release Channel threads */
2076 for (i = 0; i < chans; i++) {
2077 thrd = &pl330->channels[i];
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002078 pl330_release_channel(thrd);
Boojin Kimb7d861d2011-12-26 18:49:52 +09002079 }
2080
2081 /* Free memory */
2082 kfree(pl330->channels);
2083
2084 return 0;
2085}
2086
2087static void dmac_free_resources(struct pl330_dmac *pl330)
2088{
2089 struct pl330_info *pi = pl330->pinfo;
2090 int chans = pi->pcfg.num_chan;
2091
2092 dmac_free_threads(pl330);
2093
2094 dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2095 pl330->mcode_cpu, pl330->mcode_bus);
2096}
2097
2098static void pl330_del(struct pl330_info *pi)
2099{
2100 struct pl330_dmac *pl330;
2101
2102 if (!pi || !pi->pl330_data)
2103 return;
2104
2105 pl330 = pi->pl330_data;
2106
2107 pl330->state = UNINIT;
2108
2109 tasklet_kill(&pl330->tasks);
2110
2111 /* Free DMAC resources */
2112 dmac_free_resources(pl330);
2113
2114 kfree(pl330);
2115 pi->pl330_data = NULL;
2116}
2117
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002118/* forward declaration */
2119static struct amba_driver pl330_driver;
2120
Jassi Brarb3040e42010-05-23 20:28:19 -07002121static inline struct dma_pl330_chan *
2122to_pchan(struct dma_chan *ch)
2123{
2124 if (!ch)
2125 return NULL;
2126
2127 return container_of(ch, struct dma_pl330_chan, chan);
2128}
2129
2130static inline struct dma_pl330_desc *
2131to_desc(struct dma_async_tx_descriptor *tx)
2132{
2133 return container_of(tx, struct dma_pl330_desc, txd);
2134}
2135
Jassi Brarb3040e42010-05-23 20:28:19 -07002136static inline void fill_queue(struct dma_pl330_chan *pch)
2137{
2138 struct dma_pl330_desc *desc;
2139 int ret;
2140
2141 list_for_each_entry(desc, &pch->work_list, node) {
2142
2143 /* If already submitted */
2144 if (desc->status == BUSY)
Jassi Brar30fb9802013-02-13 16:13:14 +05302145 continue;
Jassi Brarb3040e42010-05-23 20:28:19 -07002146
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002147 ret = pl330_submit_req(pch->thread, &desc->req);
Jassi Brarb3040e42010-05-23 20:28:19 -07002148 if (!ret) {
2149 desc->status = BUSY;
Jassi Brarb3040e42010-05-23 20:28:19 -07002150 } else if (ret == -EAGAIN) {
2151 /* QFull or DMAC Dying */
2152 break;
2153 } else {
2154 /* Unacceptable request */
2155 desc->status = DONE;
2156 dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
2157 __func__, __LINE__, desc->txd.cookie);
2158 tasklet_schedule(&pch->task);
2159 }
2160 }
2161}
2162
2163static void pl330_tasklet(unsigned long data)
2164{
2165 struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
2166 struct dma_pl330_desc *desc, *_dt;
2167 unsigned long flags;
Jassi Brarb3040e42010-05-23 20:28:19 -07002168
2169 spin_lock_irqsave(&pch->lock, flags);
2170
2171 /* Pick up ripe tomatoes */
2172 list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2173 if (desc->status == DONE) {
Tushar Behera30c1dc02012-05-23 16:47:31 +05302174 if (!pch->cyclic)
Vinod Kouleab21582012-05-11 11:24:41 +05302175 dma_cookie_complete(&desc->txd);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002176 list_move_tail(&desc->node, &pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002177 }
2178
2179 /* Try to submit a req imm. next to the last completed cookie */
2180 fill_queue(pch);
2181
2182 /* Make sure the PL330 Channel thread is active */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002183 pl330_chan_ctrl(pch->thread, PL330_OP_START);
Jassi Brarb3040e42010-05-23 20:28:19 -07002184
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002185 while (!list_empty(&pch->completed_list)) {
2186 dma_async_tx_callback callback;
2187 void *callback_param;
Jassi Brarb3040e42010-05-23 20:28:19 -07002188
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002189 desc = list_first_entry(&pch->completed_list,
2190 struct dma_pl330_desc, node);
2191
2192 callback = desc->txd.callback;
2193 callback_param = desc->txd.callback_param;
2194
2195 if (pch->cyclic) {
2196 desc->status = PREP;
2197 list_move_tail(&desc->node, &pch->work_list);
2198 } else {
2199 desc->status = FREE;
2200 list_move_tail(&desc->node, &pch->dmac->desc_pool);
2201 }
2202
Dan Williamsd38a8c62013-10-18 19:35:23 +02002203 dma_descriptor_unmap(&desc->txd);
2204
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002205 if (callback) {
2206 spin_unlock_irqrestore(&pch->lock, flags);
2207 callback(callback_param);
2208 spin_lock_irqsave(&pch->lock, flags);
2209 }
2210 }
2211 spin_unlock_irqrestore(&pch->lock, flags);
Jassi Brarb3040e42010-05-23 20:28:19 -07002212}
2213
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002214bool pl330_filter(struct dma_chan *chan, void *param)
2215{
Thomas Abrahamcd072512011-10-24 11:43:11 +02002216 u8 *peri_id;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002217
2218 if (chan->device->dev->driver != &pl330_driver.drv)
2219 return false;
2220
Thomas Abrahamcd072512011-10-24 11:43:11 +02002221 peri_id = chan->private;
Dan Carpenter2f986ec2013-11-08 12:51:16 +03002222 return *peri_id == (unsigned long)param;
Thomas Abraham3e2ec132011-10-24 11:43:02 +02002223}
2224EXPORT_SYMBOL(pl330_filter);
2225
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302226static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2227 struct of_dma *ofdma)
2228{
2229 int count = dma_spec->args_count;
2230 struct dma_pl330_dmac *pdmac = ofdma->of_dma_data;
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002231 unsigned int chan_id;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302232
2233 if (count != 1)
2234 return NULL;
2235
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002236 chan_id = dma_spec->args[0];
2237 if (chan_id >= pdmac->num_peripherals)
2238 return NULL;
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302239
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002240 return dma_get_slave_channel(&pdmac->peripherals[chan_id].chan);
Padmavathi Vennaa80258f2013-02-14 09:10:06 +05302241}
2242
Jassi Brarb3040e42010-05-23 20:28:19 -07002243static int pl330_alloc_chan_resources(struct dma_chan *chan)
2244{
2245 struct dma_pl330_chan *pch = to_pchan(chan);
2246 struct dma_pl330_dmac *pdmac = pch->dmac;
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&pch->lock, flags);
2250
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00002251 dma_cookie_init(chan);
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002252 pch->cyclic = false;
Jassi Brarb3040e42010-05-23 20:28:19 -07002253
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002254 pch->thread = pl330_request_channel(&pdmac->pif);
2255 if (!pch->thread) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002256 spin_unlock_irqrestore(&pch->lock, flags);
Inderpal Singh02747882012-09-17 09:57:45 +05302257 return -ENOMEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002258 }
2259
2260 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
2261
2262 spin_unlock_irqrestore(&pch->lock, flags);
2263
2264 return 1;
2265}
2266
2267static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
2268{
2269 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002270 struct dma_pl330_desc *desc;
Jassi Brarb3040e42010-05-23 20:28:19 -07002271 unsigned long flags;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002272 struct dma_pl330_dmac *pdmac = pch->dmac;
2273 struct dma_slave_config *slave_config;
Boojin Kimae43b882011-09-02 09:44:32 +09002274 LIST_HEAD(list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002275
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002276 switch (cmd) {
2277 case DMA_TERMINATE_ALL:
2278 spin_lock_irqsave(&pch->lock, flags);
2279
2280 /* FLUSH the PL330 Channel thread */
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002281 pl330_chan_ctrl(pch->thread, PL330_OP_FLUSH);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002282
2283 /* Mark all desc done */
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002284 list_for_each_entry(desc, &pch->submitted_list, node) {
2285 desc->status = FREE;
2286 dma_cookie_complete(&desc->txd);
2287 }
2288
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002289 list_for_each_entry(desc, &pch->work_list , node) {
2290 desc->status = FREE;
2291 dma_cookie_complete(&desc->txd);
Boojin Kimae43b882011-09-02 09:44:32 +09002292 }
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002293
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002294 list_for_each_entry(desc, &pch->completed_list , node) {
2295 desc->status = FREE;
2296 dma_cookie_complete(&desc->txd);
2297 }
2298
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002299 list_splice_tail_init(&pch->submitted_list, &pdmac->desc_pool);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002300 list_splice_tail_init(&pch->work_list, &pdmac->desc_pool);
2301 list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002302 spin_unlock_irqrestore(&pch->lock, flags);
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002303 break;
2304 case DMA_SLAVE_CONFIG:
2305 slave_config = (struct dma_slave_config *)arg;
2306
Vinod Kouldb8196d2011-10-13 22:34:23 +05302307 if (slave_config->direction == DMA_MEM_TO_DEV) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002308 if (slave_config->dst_addr)
2309 pch->fifo_addr = slave_config->dst_addr;
2310 if (slave_config->dst_addr_width)
2311 pch->burst_sz = __ffs(slave_config->dst_addr_width);
2312 if (slave_config->dst_maxburst)
2313 pch->burst_len = slave_config->dst_maxburst;
Vinod Kouldb8196d2011-10-13 22:34:23 +05302314 } else if (slave_config->direction == DMA_DEV_TO_MEM) {
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002315 if (slave_config->src_addr)
2316 pch->fifo_addr = slave_config->src_addr;
2317 if (slave_config->src_addr_width)
2318 pch->burst_sz = __ffs(slave_config->src_addr_width);
2319 if (slave_config->src_maxburst)
2320 pch->burst_len = slave_config->src_maxburst;
2321 }
2322 break;
2323 default:
2324 dev_err(pch->dmac->pif.dev, "Not supported command.\n");
Jassi Brarb3040e42010-05-23 20:28:19 -07002325 return -ENXIO;
Boojin Kim1d0c1d62011-09-02 09:44:31 +09002326 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002327
2328 return 0;
2329}
2330
2331static void pl330_free_chan_resources(struct dma_chan *chan)
2332{
2333 struct dma_pl330_chan *pch = to_pchan(chan);
2334 unsigned long flags;
2335
Jassi Brarb3040e42010-05-23 20:28:19 -07002336 tasklet_kill(&pch->task);
2337
Bartlomiej Zolnierkiewiczda331ba2013-07-03 15:00:43 -07002338 spin_lock_irqsave(&pch->lock, flags);
2339
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002340 pl330_release_channel(pch->thread);
2341 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002342
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002343 if (pch->cyclic)
2344 list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2345
Jassi Brarb3040e42010-05-23 20:28:19 -07002346 spin_unlock_irqrestore(&pch->lock, flags);
2347}
2348
2349static enum dma_status
2350pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2351 struct dma_tx_state *txstate)
2352{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00002353 return dma_cookie_status(chan, cookie, txstate);
Jassi Brarb3040e42010-05-23 20:28:19 -07002354}
2355
2356static void pl330_issue_pending(struct dma_chan *chan)
2357{
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002358 struct dma_pl330_chan *pch = to_pchan(chan);
2359 unsigned long flags;
2360
2361 spin_lock_irqsave(&pch->lock, flags);
2362 list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2363 spin_unlock_irqrestore(&pch->lock, flags);
2364
2365 pl330_tasklet((unsigned long)pch);
Jassi Brarb3040e42010-05-23 20:28:19 -07002366}
2367
2368/*
2369 * We returned the last one of the circular list of descriptor(s)
2370 * from prep_xxx, so the argument to submit corresponds to the last
2371 * descriptor of the list.
2372 */
2373static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2374{
2375 struct dma_pl330_desc *desc, *last = to_desc(tx);
2376 struct dma_pl330_chan *pch = to_pchan(tx->chan);
2377 dma_cookie_t cookie;
2378 unsigned long flags;
2379
2380 spin_lock_irqsave(&pch->lock, flags);
2381
2382 /* Assign cookies to all nodes */
Jassi Brarb3040e42010-05-23 20:28:19 -07002383 while (!list_empty(&last->node)) {
2384 desc = list_entry(last->node.next, struct dma_pl330_desc, node);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002385 if (pch->cyclic) {
2386 desc->txd.callback = last->txd.callback;
2387 desc->txd.callback_param = last->txd.callback_param;
2388 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002389
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002390 dma_cookie_assign(&desc->txd);
Jassi Brarb3040e42010-05-23 20:28:19 -07002391
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002392 list_move_tail(&desc->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002393 }
2394
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00002395 cookie = dma_cookie_assign(&last->txd);
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002396 list_add_tail(&last->node, &pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002397 spin_unlock_irqrestore(&pch->lock, flags);
2398
2399 return cookie;
2400}
2401
2402static inline void _init_desc(struct dma_pl330_desc *desc)
2403{
Jassi Brarb3040e42010-05-23 20:28:19 -07002404 desc->req.x = &desc->px;
Jassi Brarb3040e42010-05-23 20:28:19 -07002405 desc->rqcfg.swap = SWAP_NO;
Lars-Peter Clausenf0564c72014-07-06 20:32:19 +02002406 desc->rqcfg.scctl = CCTRL0;
2407 desc->rqcfg.dcctl = CCTRL0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002408 desc->req.cfg = &desc->rqcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002409 desc->txd.tx_submit = pl330_tx_submit;
2410
2411 INIT_LIST_HEAD(&desc->node);
2412}
2413
2414/* Returns the number of descriptors added to the DMAC pool */
Sachin Kamat5a67ac52012-06-04 17:09:45 +05302415static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
Jassi Brarb3040e42010-05-23 20:28:19 -07002416{
2417 struct dma_pl330_desc *desc;
2418 unsigned long flags;
2419 int i;
2420
2421 if (!pdmac)
2422 return 0;
2423
Will Deacon0baf8f62013-12-02 18:01:30 +00002424 desc = kcalloc(count, sizeof(*desc), flg);
Jassi Brarb3040e42010-05-23 20:28:19 -07002425 if (!desc)
2426 return 0;
2427
2428 spin_lock_irqsave(&pdmac->pool_lock, flags);
2429
2430 for (i = 0; i < count; i++) {
2431 _init_desc(&desc[i]);
2432 list_add_tail(&desc[i].node, &pdmac->desc_pool);
2433 }
2434
2435 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2436
2437 return count;
2438}
2439
2440static struct dma_pl330_desc *
2441pluck_desc(struct dma_pl330_dmac *pdmac)
2442{
2443 struct dma_pl330_desc *desc = NULL;
2444 unsigned long flags;
2445
2446 if (!pdmac)
2447 return NULL;
2448
2449 spin_lock_irqsave(&pdmac->pool_lock, flags);
2450
2451 if (!list_empty(&pdmac->desc_pool)) {
2452 desc = list_entry(pdmac->desc_pool.next,
2453 struct dma_pl330_desc, node);
2454
2455 list_del_init(&desc->node);
2456
2457 desc->status = PREP;
2458 desc->txd.callback = NULL;
2459 }
2460
2461 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2462
2463 return desc;
2464}
2465
2466static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2467{
2468 struct dma_pl330_dmac *pdmac = pch->dmac;
Thomas Abrahamcd072512011-10-24 11:43:11 +02002469 u8 *peri_id = pch->chan.private;
Jassi Brarb3040e42010-05-23 20:28:19 -07002470 struct dma_pl330_desc *desc;
2471
2472 /* Pluck one desc from the pool of DMAC */
2473 desc = pluck_desc(pdmac);
2474
2475 /* If the DMAC pool is empty, alloc new */
2476 if (!desc) {
2477 if (!add_desc(pdmac, GFP_ATOMIC, 1))
2478 return NULL;
2479
2480 /* Try again */
2481 desc = pluck_desc(pdmac);
2482 if (!desc) {
2483 dev_err(pch->dmac->pif.dev,
2484 "%s:%d ALERT!\n", __func__, __LINE__);
2485 return NULL;
2486 }
2487 }
2488
2489 /* Initialize the descriptor */
2490 desc->pchan = pch;
2491 desc->txd.cookie = 0;
2492 async_tx_ack(&desc->txd);
2493
Thomas Abrahamcd072512011-10-24 11:43:11 +02002494 desc->req.peri = peri_id ? pch->chan.chan_id : 0;
Boojin Kim3ecf51a2011-12-26 18:55:47 +09002495 desc->rqcfg.pcfg = &pch->dmac->pif.pcfg;
Jassi Brarb3040e42010-05-23 20:28:19 -07002496
2497 dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2498
2499 return desc;
2500}
2501
2502static inline void fill_px(struct pl330_xfer *px,
2503 dma_addr_t dst, dma_addr_t src, size_t len)
2504{
Jassi Brarb3040e42010-05-23 20:28:19 -07002505 px->bytes = len;
2506 px->dst_addr = dst;
2507 px->src_addr = src;
2508}
2509
2510static struct dma_pl330_desc *
2511__pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2512 dma_addr_t src, size_t len)
2513{
2514 struct dma_pl330_desc *desc = pl330_get_desc(pch);
2515
2516 if (!desc) {
2517 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2518 __func__, __LINE__);
2519 return NULL;
2520 }
2521
2522 /*
2523 * Ideally we should lookout for reqs bigger than
2524 * those that can be programmed with 256 bytes of
2525 * MC buffer, but considering a req size is seldom
2526 * going to be word-unaligned and more than 200MB,
2527 * we take it easy.
2528 * Also, should the limit is reached we'd rather
2529 * have the platform increase MC buffer size than
2530 * complicating this API driver.
2531 */
2532 fill_px(&desc->px, dst, src, len);
2533
2534 return desc;
2535}
2536
2537/* Call after fixing burst size */
2538static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2539{
2540 struct dma_pl330_chan *pch = desc->pchan;
2541 struct pl330_info *pi = &pch->dmac->pif;
2542 int burst_len;
2543
2544 burst_len = pi->pcfg.data_bus_width / 8;
2545 burst_len *= pi->pcfg.data_buf_dep;
2546 burst_len >>= desc->rqcfg.brst_size;
2547
2548 /* src/dst_burst_len can't be more than 16 */
2549 if (burst_len > 16)
2550 burst_len = 16;
2551
2552 while (burst_len > 1) {
2553 if (!(len % (burst_len << desc->rqcfg.brst_size)))
2554 break;
2555 burst_len--;
2556 }
2557
2558 return burst_len;
2559}
2560
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002561static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2562 struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002563 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03002564 unsigned long flags, void *context)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002565{
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002566 struct dma_pl330_desc *desc = NULL, *first = NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002567 struct dma_pl330_chan *pch = to_pchan(chan);
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002568 struct dma_pl330_dmac *pdmac = pch->dmac;
2569 unsigned int i;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002570 dma_addr_t dst;
2571 dma_addr_t src;
2572
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002573 if (len % period_len != 0)
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002574 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002575
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002576 if (!is_slave_direction(direction)) {
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002577 dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
2578 __func__, __LINE__);
2579 return NULL;
2580 }
2581
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002582 for (i = 0; i < len / period_len; i++) {
2583 desc = pl330_get_desc(pch);
2584 if (!desc) {
2585 dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
2586 __func__, __LINE__);
2587
2588 if (!first)
2589 return NULL;
2590
2591 spin_lock_irqsave(&pdmac->pool_lock, flags);
2592
2593 while (!list_empty(&first->node)) {
2594 desc = list_entry(first->node.next,
2595 struct dma_pl330_desc, node);
2596 list_move_tail(&desc->node, &pdmac->desc_pool);
2597 }
2598
2599 list_move_tail(&first->node, &pdmac->desc_pool);
2600
2601 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2602
2603 return NULL;
2604 }
2605
2606 switch (direction) {
2607 case DMA_MEM_TO_DEV:
2608 desc->rqcfg.src_inc = 1;
2609 desc->rqcfg.dst_inc = 0;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002610 src = dma_addr;
2611 dst = pch->fifo_addr;
2612 break;
2613 case DMA_DEV_TO_MEM:
2614 desc->rqcfg.src_inc = 0;
2615 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002616 src = pch->fifo_addr;
2617 dst = dma_addr;
2618 break;
2619 default:
2620 break;
2621 }
2622
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002623 desc->req.rqtype = direction;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002624 desc->rqcfg.brst_size = pch->burst_sz;
2625 desc->rqcfg.brst_len = 1;
2626 fill_px(&desc->px, dst, src, period_len);
2627
2628 if (!first)
2629 first = desc;
2630 else
2631 list_add_tail(&desc->node, &first->node);
2632
2633 dma_addr += period_len;
2634 }
2635
2636 if (!desc)
2637 return NULL;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002638
2639 pch->cyclic = true;
Lars-Peter Clausenfc514462013-07-23 10:24:50 +02002640 desc->txd.flags = flags;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002641
2642 return &desc->txd;
2643}
2644
Jassi Brarb3040e42010-05-23 20:28:19 -07002645static struct dma_async_tx_descriptor *
2646pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2647 dma_addr_t src, size_t len, unsigned long flags)
2648{
2649 struct dma_pl330_desc *desc;
2650 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002651 struct pl330_info *pi;
2652 int burst;
2653
Rob Herring4e0e6102011-07-25 16:05:04 -05002654 if (unlikely(!pch || !len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002655 return NULL;
2656
Jassi Brarb3040e42010-05-23 20:28:19 -07002657 pi = &pch->dmac->pif;
2658
2659 desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2660 if (!desc)
2661 return NULL;
2662
2663 desc->rqcfg.src_inc = 1;
2664 desc->rqcfg.dst_inc = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002665 desc->req.rqtype = DMA_MEM_TO_MEM;
Jassi Brarb3040e42010-05-23 20:28:19 -07002666
2667 /* Select max possible burst size */
2668 burst = pi->pcfg.data_bus_width / 8;
2669
2670 while (burst > 1) {
2671 if (!(len % burst))
2672 break;
2673 burst /= 2;
2674 }
2675
2676 desc->rqcfg.brst_size = 0;
2677 while (burst != (1 << desc->rqcfg.brst_size))
2678 desc->rqcfg.brst_size++;
2679
2680 desc->rqcfg.brst_len = get_burst_len(desc, len);
2681
2682 desc->txd.flags = flags;
2683
2684 return &desc->txd;
2685}
2686
Chanho Park52a9d172013-08-09 20:11:33 +09002687static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac,
2688 struct dma_pl330_desc *first)
2689{
2690 unsigned long flags;
2691 struct dma_pl330_desc *desc;
2692
2693 if (!first)
2694 return;
2695
2696 spin_lock_irqsave(&pdmac->pool_lock, flags);
2697
2698 while (!list_empty(&first->node)) {
2699 desc = list_entry(first->node.next,
2700 struct dma_pl330_desc, node);
2701 list_move_tail(&desc->node, &pdmac->desc_pool);
2702 }
2703
2704 list_move_tail(&first->node, &pdmac->desc_pool);
2705
2706 spin_unlock_irqrestore(&pdmac->pool_lock, flags);
2707}
2708
Jassi Brarb3040e42010-05-23 20:28:19 -07002709static struct dma_async_tx_descriptor *
2710pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05302711 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05002712 unsigned long flg, void *context)
Jassi Brarb3040e42010-05-23 20:28:19 -07002713{
2714 struct dma_pl330_desc *first, *desc = NULL;
2715 struct dma_pl330_chan *pch = to_pchan(chan);
Jassi Brarb3040e42010-05-23 20:28:19 -07002716 struct scatterlist *sg;
Boojin Kim1b9bb712011-09-02 09:44:30 +09002717 int i;
Jassi Brarb3040e42010-05-23 20:28:19 -07002718 dma_addr_t addr;
2719
Thomas Abrahamcd072512011-10-24 11:43:11 +02002720 if (unlikely(!pch || !sgl || !sg_len))
Jassi Brarb3040e42010-05-23 20:28:19 -07002721 return NULL;
2722
Boojin Kim1b9bb712011-09-02 09:44:30 +09002723 addr = pch->fifo_addr;
Jassi Brarb3040e42010-05-23 20:28:19 -07002724
2725 first = NULL;
2726
2727 for_each_sg(sgl, sg, sg_len, i) {
2728
2729 desc = pl330_get_desc(pch);
2730 if (!desc) {
2731 struct dma_pl330_dmac *pdmac = pch->dmac;
2732
2733 dev_err(pch->dmac->pif.dev,
2734 "%s:%d Unable to fetch desc\n",
2735 __func__, __LINE__);
Chanho Park52a9d172013-08-09 20:11:33 +09002736 __pl330_giveback_desc(pdmac, first);
Jassi Brarb3040e42010-05-23 20:28:19 -07002737
2738 return NULL;
2739 }
2740
2741 if (!first)
2742 first = desc;
2743 else
2744 list_add_tail(&desc->node, &first->node);
2745
Vinod Kouldb8196d2011-10-13 22:34:23 +05302746 if (direction == DMA_MEM_TO_DEV) {
Jassi Brarb3040e42010-05-23 20:28:19 -07002747 desc->rqcfg.src_inc = 1;
2748 desc->rqcfg.dst_inc = 0;
2749 fill_px(&desc->px,
2750 addr, sg_dma_address(sg), sg_dma_len(sg));
2751 } else {
2752 desc->rqcfg.src_inc = 0;
2753 desc->rqcfg.dst_inc = 1;
2754 fill_px(&desc->px,
2755 sg_dma_address(sg), addr, sg_dma_len(sg));
2756 }
2757
Boojin Kim1b9bb712011-09-02 09:44:30 +09002758 desc->rqcfg.brst_size = pch->burst_sz;
Jassi Brarb3040e42010-05-23 20:28:19 -07002759 desc->rqcfg.brst_len = 1;
Lars-Peter Clausen585a9d02014-07-06 20:32:18 +02002760 desc->req.rqtype = direction;
Jassi Brarb3040e42010-05-23 20:28:19 -07002761 }
2762
2763 /* Return the last desc in the chain */
2764 desc->txd.flags = flg;
2765 return &desc->txd;
2766}
2767
2768static irqreturn_t pl330_irq_handler(int irq, void *data)
2769{
2770 if (pl330_update(data))
2771 return IRQ_HANDLED;
2772 else
2773 return IRQ_NONE;
2774}
2775
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002776#define PL330_DMA_BUSWIDTHS \
2777 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2778 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2779 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2780 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2781 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2782
2783static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
2784 struct dma_slave_caps *caps)
2785{
2786 caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
2787 caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
2788 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2789 caps->cmd_pause = false;
2790 caps->cmd_terminate = true;
Lars-Peter Clausenbfb9bb42014-01-11 14:02:17 +01002791 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002792
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002793 return 0;
2794}
2795
Bill Pemberton463a1f82012-11-19 13:22:55 -05002796static int
Russell Kingaa25afa2011-02-19 15:55:00 +00002797pl330_probe(struct amba_device *adev, const struct amba_id *id)
Jassi Brarb3040e42010-05-23 20:28:19 -07002798{
2799 struct dma_pl330_platdata *pdat;
2800 struct dma_pl330_dmac *pdmac;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302801 struct dma_pl330_chan *pch, *_p;
Jassi Brarb3040e42010-05-23 20:28:19 -07002802 struct pl330_info *pi;
2803 struct dma_device *pd;
2804 struct resource *res;
2805 int i, ret, irq;
Rob Herring4e0e6102011-07-25 16:05:04 -05002806 int num_chan;
Jassi Brarb3040e42010-05-23 20:28:19 -07002807
Jingoo Hand4adcc02013-07-30 17:09:11 +09002808 pdat = dev_get_platdata(&adev->dev);
Jassi Brarb3040e42010-05-23 20:28:19 -07002809
Russell King64113012013-06-27 10:29:32 +01002810 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2811 if (ret)
2812 return ret;
2813
Jassi Brarb3040e42010-05-23 20:28:19 -07002814 /* Allocate a new DMAC and its Channels */
Sachin Kamate4d43c12012-11-15 06:27:50 +00002815 pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL);
Jassi Brarb3040e42010-05-23 20:28:19 -07002816 if (!pdmac) {
2817 dev_err(&adev->dev, "unable to allocate mem\n");
2818 return -ENOMEM;
2819 }
2820
2821 pi = &pdmac->pif;
2822 pi->dev = &adev->dev;
2823 pi->pl330_data = NULL;
Rob Herring4e0e6102011-07-25 16:05:04 -05002824 pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
Jassi Brarb3040e42010-05-23 20:28:19 -07002825
2826 res = &adev->res;
Sachin Kamatbcc7fa92013-03-04 14:36:27 +05302827 pi->base = devm_ioremap_resource(&adev->dev, res);
2828 if (IS_ERR(pi->base))
2829 return PTR_ERR(pi->base);
Jassi Brarb3040e42010-05-23 20:28:19 -07002830
Boojin Kima2f52032011-09-02 09:44:29 +09002831 amba_set_drvdata(adev, pdmac);
2832
Dan Carpenter02808b42013-11-08 12:50:24 +03002833 for (i = 0; i < AMBA_NR_IRQS; i++) {
Michal Simeke98b3ca2013-09-30 08:50:48 +02002834 irq = adev->irq[i];
2835 if (irq) {
2836 ret = devm_request_irq(&adev->dev, irq,
2837 pl330_irq_handler, 0,
2838 dev_name(&adev->dev), pi);
2839 if (ret)
2840 return ret;
2841 } else {
2842 break;
2843 }
2844 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002845
Will Deacon09677172013-06-10 19:34:37 +01002846 pi->pcfg.periph_id = adev->periphid;
Jassi Brarb3040e42010-05-23 20:28:19 -07002847 ret = pl330_add(pi);
2848 if (ret)
Michal Simek173e8382013-09-04 16:40:17 +02002849 return ret;
Jassi Brarb3040e42010-05-23 20:28:19 -07002850
2851 INIT_LIST_HEAD(&pdmac->desc_pool);
2852 spin_lock_init(&pdmac->pool_lock);
2853
2854 /* Create a descriptor pool of default size */
2855 if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
2856 dev_warn(&adev->dev, "unable to allocate desc\n");
2857
2858 pd = &pdmac->ddma;
2859 INIT_LIST_HEAD(&pd->channels);
2860
2861 /* Initialize channel parameters */
Olof Johanssonc8473822012-04-08 16:26:19 -07002862 if (pdat)
2863 num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan);
2864 else
2865 num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan);
2866
Lars-Peter Clausen70cbb162014-01-11 20:08:39 +01002867 pdmac->num_peripherals = num_chan;
2868
Rob Herring4e0e6102011-07-25 16:05:04 -05002869 pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
Sachin Kamat61c6e752012-09-17 15:20:23 +05302870 if (!pdmac->peripherals) {
2871 ret = -ENOMEM;
2872 dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n");
Sachin Kamate4d43c12012-11-15 06:27:50 +00002873 goto probe_err2;
Sachin Kamat61c6e752012-09-17 15:20:23 +05302874 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002875
Rob Herring4e0e6102011-07-25 16:05:04 -05002876 for (i = 0; i < num_chan; i++) {
2877 pch = &pdmac->peripherals[i];
Thomas Abraham93ed5542011-10-24 11:43:31 +02002878 if (!adev->dev.of_node)
2879 pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
2880 else
2881 pch->chan.private = adev->dev.of_node;
Jassi Brarb3040e42010-05-23 20:28:19 -07002882
Lars-Peter Clausen04abf5d2014-01-11 20:08:38 +01002883 INIT_LIST_HEAD(&pch->submitted_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002884 INIT_LIST_HEAD(&pch->work_list);
Lars-Peter Clausen39ff8612013-08-27 20:34:05 +02002885 INIT_LIST_HEAD(&pch->completed_list);
Jassi Brarb3040e42010-05-23 20:28:19 -07002886 spin_lock_init(&pch->lock);
Lars-Peter Clausen65ad6062014-07-06 20:32:26 +02002887 pch->thread = NULL;
Jassi Brarb3040e42010-05-23 20:28:19 -07002888 pch->chan.device = pd;
Jassi Brarb3040e42010-05-23 20:28:19 -07002889 pch->dmac = pdmac;
2890
2891 /* Add the channel to the DMAC list */
Jassi Brarb3040e42010-05-23 20:28:19 -07002892 list_add_tail(&pch->chan.device_node, &pd->channels);
2893 }
2894
2895 pd->dev = &adev->dev;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002896 if (pdat) {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002897 pd->cap_mask = pdat->cap_mask;
Thomas Abraham93ed5542011-10-24 11:43:31 +02002898 } else {
Thomas Abrahamcd072512011-10-24 11:43:11 +02002899 dma_cap_set(DMA_MEMCPY, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002900 if (pi->pcfg.num_peri) {
2901 dma_cap_set(DMA_SLAVE, pd->cap_mask);
2902 dma_cap_set(DMA_CYCLIC, pd->cap_mask);
Tushar Behera5557a412012-08-29 10:16:25 +05302903 dma_cap_set(DMA_PRIVATE, pd->cap_mask);
Thomas Abraham93ed5542011-10-24 11:43:31 +02002904 }
2905 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002906
2907 pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
2908 pd->device_free_chan_resources = pl330_free_chan_resources;
2909 pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
Boojin Kim42bc9cf2011-09-02 09:44:33 +09002910 pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
Jassi Brarb3040e42010-05-23 20:28:19 -07002911 pd->device_tx_status = pl330_tx_status;
2912 pd->device_prep_slave_sg = pl330_prep_slave_sg;
2913 pd->device_control = pl330_control;
2914 pd->device_issue_pending = pl330_issue_pending;
Lars-Peter Clausenca38ff12013-07-15 17:53:08 +02002915 pd->device_slave_caps = pl330_dma_device_slave_caps;
Jassi Brarb3040e42010-05-23 20:28:19 -07002916
2917 ret = dma_async_device_register(pd);
2918 if (ret) {
2919 dev_err(&adev->dev, "unable to register DMAC\n");
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302920 goto probe_err3;
2921 }
2922
2923 if (adev->dev.of_node) {
2924 ret = of_dma_controller_register(adev->dev.of_node,
2925 of_dma_pl330_xlate, pdmac);
2926 if (ret) {
2927 dev_err(&adev->dev,
2928 "unable to register DMA to the generic DT DMA helpers\n");
2929 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002930 }
Lars-Peter Clausenb714b842013-11-25 16:07:46 +01002931
2932 adev->dev.dma_parms = &pdmac->dma_parms;
2933
Vinod Kouldbaf6d82013-09-02 21:54:48 +05302934 /*
2935 * This is the limit for transfers with a buswidth of 1, larger
2936 * buswidths will have larger limits.
2937 */
2938 ret = dma_set_max_seg_size(&adev->dev, 1900800);
2939 if (ret)
2940 dev_err(&adev->dev, "unable to set the seg size\n");
2941
Jassi Brarb3040e42010-05-23 20:28:19 -07002942
Jassi Brarb3040e42010-05-23 20:28:19 -07002943 dev_info(&adev->dev,
2944 "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
2945 dev_info(&adev->dev,
2946 "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
2947 pi->pcfg.data_buf_dep,
2948 pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
2949 pi->pcfg.num_peri, pi->pcfg.num_events);
2950
2951 return 0;
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302952probe_err3:
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302953 /* Idle the DMAC */
2954 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
2955 chan.device_node) {
2956
2957 /* Remove the channel */
2958 list_del(&pch->chan.device_node);
2959
2960 /* Flush the channel */
2961 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2962 pl330_free_chan_resources(&pch->chan);
2963 }
Jassi Brarb3040e42010-05-23 20:28:19 -07002964probe_err2:
Sachin Kamate4d43c12012-11-15 06:27:50 +00002965 pl330_del(pi);
Jassi Brarb3040e42010-05-23 20:28:19 -07002966
2967 return ret;
2968}
2969
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08002970static int pl330_remove(struct amba_device *adev)
Jassi Brarb3040e42010-05-23 20:28:19 -07002971{
2972 struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
2973 struct dma_pl330_chan *pch, *_p;
2974 struct pl330_info *pi;
Jassi Brarb3040e42010-05-23 20:28:19 -07002975
2976 if (!pdmac)
2977 return 0;
2978
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302979 if (adev->dev.of_node)
2980 of_dma_controller_free(adev->dev.of_node);
Padmavathi Venna421da892013-02-14 09:10:07 +05302981
Padmavathi Venna0b94c572013-03-05 14:55:31 +05302982 dma_async_device_unregister(&pdmac->ddma);
Jassi Brarb3040e42010-05-23 20:28:19 -07002983
2984 /* Idle the DMAC */
2985 list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
2986 chan.device_node) {
2987
2988 /* Remove the channel */
2989 list_del(&pch->chan.device_node);
2990
2991 /* Flush the channel */
2992 pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
2993 pl330_free_chan_resources(&pch->chan);
2994 }
2995
2996 pi = &pdmac->pif;
2997
2998 pl330_del(pi);
2999
Jassi Brarb3040e42010-05-23 20:28:19 -07003000 return 0;
3001}
3002
3003static struct amba_id pl330_ids[] = {
3004 {
3005 .id = 0x00041330,
3006 .mask = 0x000fffff,
3007 },
3008 { 0, 0 },
3009};
3010
Dave Martine8fa5162011-10-05 15:15:20 +01003011MODULE_DEVICE_TABLE(amba, pl330_ids);
3012
Jassi Brarb3040e42010-05-23 20:28:19 -07003013static struct amba_driver pl330_driver = {
3014 .drv = {
3015 .owner = THIS_MODULE,
3016 .name = "dma-pl330",
Jassi Brarb3040e42010-05-23 20:28:19 -07003017 },
3018 .id_table = pl330_ids,
3019 .probe = pl330_probe,
3020 .remove = pl330_remove,
3021};
3022
viresh kumar9e5ed092012-03-15 10:40:38 +01003023module_amba_driver(pl330_driver);
Jassi Brarb3040e42010-05-23 20:28:19 -07003024
3025MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
3026MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3027MODULE_LICENSE("GPL");