xref: /openbmc/linux/drivers/dma/amba-pl08x.c (revision f7777dcc)
1 /*
2  * Copyright (c) 2006 ARM Ltd.
3  * Copyright (c) 2010 ST-Ericsson SA
4  *
5  * Author: Peter Pearse <peter.pearse@arm.com>
6  * Author: Linus Walleij <linus.walleij@stericsson.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 59
20  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The full GNU General Public License is in this distribution in the file
23  * called COPYING.
24  *
25  * Documentation: ARM DDI 0196G == PL080
26  * Documentation: ARM DDI 0218E == PL081
27  * Documentation: S3C6410 User's Manual == PL080S
28  *
29  * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
30  * channel.
31  *
32  * The PL080 has 8 channels available for simultaneous use, and the PL081
33  * has only two channels. So on these DMA controllers the number of channels
34  * and the number of incoming DMA signals are two totally different things.
35  * It is usually not possible to theoretically handle all physical signals,
36  * so a multiplexing scheme with possible denial of use is necessary.
37  *
38  * The PL080 has a dual bus master, PL081 has a single master.
39  *
40  * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
41  * It differs in following aspects:
42  * - CH_CONFIG register at different offset,
43  * - separate CH_CONTROL2 register for transfer size,
44  * - bigger maximum transfer size,
45  * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
46  * - no support for peripheral flow control.
47  *
48  * Memory to peripheral transfer may be visualized as
49  *	Get data from memory to DMAC
50  *	Until no data left
51  *		On burst request from peripheral
52  *			Destination burst from DMAC to peripheral
53  *			Clear burst request
54  *	Raise terminal count interrupt
55  *
56  * For peripherals with a FIFO:
57  * Source      burst size == half the depth of the peripheral FIFO
58  * Destination burst size == the depth of the peripheral FIFO
59  *
60  * (Bursts are irrelevant for mem to mem transfers - there are no burst
61  * signals, the DMA controller will simply facilitate its AHB master.)
62  *
63  * ASSUMES default (little) endianness for DMA transfers
64  *
65  * The PL08x has two flow control settings:
66  *  - DMAC flow control: the transfer size defines the number of transfers
67  *    which occur for the current LLI entry, and the DMAC raises TC at the
68  *    end of every LLI entry.  Observed behaviour shows the DMAC listening
69  *    to both the BREQ and SREQ signals (contrary to documented),
70  *    transferring data if either is active.  The LBREQ and LSREQ signals
71  *    are ignored.
72  *
73  *  - Peripheral flow control: the transfer size is ignored (and should be
74  *    zero).  The data is transferred from the current LLI entry, until
75  *    after the final transfer signalled by LBREQ or LSREQ.  The DMAC
76  *    will then move to the next LLI entry. Unsupported by PL080S.
77  */
78 #include <linux/amba/bus.h>
79 #include <linux/amba/pl08x.h>
80 #include <linux/debugfs.h>
81 #include <linux/delay.h>
82 #include <linux/device.h>
83 #include <linux/dmaengine.h>
84 #include <linux/dmapool.h>
85 #include <linux/dma-mapping.h>
86 #include <linux/init.h>
87 #include <linux/interrupt.h>
88 #include <linux/module.h>
89 #include <linux/pm_runtime.h>
90 #include <linux/seq_file.h>
91 #include <linux/slab.h>
92 #include <linux/amba/pl080.h>
93 
94 #include "dmaengine.h"
95 #include "virt-dma.h"
96 
97 #define DRIVER_NAME	"pl08xdmac"
98 
99 static struct amba_driver pl08x_amba_driver;
100 struct pl08x_driver_data;
101 
102 /**
103  * struct vendor_data - vendor-specific config parameters for PL08x derivatives
104  * @channels: the number of channels available in this variant
105  * @dualmaster: whether this version supports dual AHB masters or not.
106  * @nomadik: whether the channels have Nomadik security extension bits
107  *	that need to be checked for permission before use and some registers are
108  *	missing
109  * @pl080s: whether this version is a PL080S, which has separate register and
110  *	LLI word for transfer size.
111  */
112 struct vendor_data {
113 	u8 config_offset;
114 	u8 channels;
115 	bool dualmaster;
116 	bool nomadik;
117 	bool pl080s;
118 	u32 max_transfer_size;
119 };
120 
121 /**
122  * struct pl08x_bus_data - information of source or destination
123  * busses for a transfer
124  * @addr: current address
125  * @maxwidth: the maximum width of a transfer on this bus
126  * @buswidth: the width of this bus in bytes: 1, 2 or 4
127  */
128 struct pl08x_bus_data {
129 	dma_addr_t addr;
130 	u8 maxwidth;
131 	u8 buswidth;
132 };
133 
134 #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
135 
136 /**
137  * struct pl08x_phy_chan - holder for the physical channels
138  * @id: physical index to this channel
139  * @lock: a lock to use when altering an instance of this struct
140  * @serving: the virtual channel currently being served by this physical
141  * channel
142  * @locked: channel unavailable for the system, e.g. dedicated to secure
143  * world
144  */
145 struct pl08x_phy_chan {
146 	unsigned int id;
147 	void __iomem *base;
148 	void __iomem *reg_config;
149 	spinlock_t lock;
150 	struct pl08x_dma_chan *serving;
151 	bool locked;
152 };
153 
154 /**
155  * struct pl08x_sg - structure containing data per sg
156  * @src_addr: src address of sg
157  * @dst_addr: dst address of sg
158  * @len: transfer len in bytes
159  * @node: node for txd's dsg_list
160  */
161 struct pl08x_sg {
162 	dma_addr_t src_addr;
163 	dma_addr_t dst_addr;
164 	size_t len;
165 	struct list_head node;
166 };
167 
168 /**
169  * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
170  * @vd: virtual DMA descriptor
171  * @dsg_list: list of children sg's
172  * @llis_bus: DMA memory address (physical) start for the LLIs
173  * @llis_va: virtual memory address start for the LLIs
174  * @cctl: control reg values for current txd
175  * @ccfg: config reg values for current txd
176  * @done: this marks completed descriptors, which should not have their
177  *   mux released.
178  * @cyclic: indicate cyclic transfers
179  */
180 struct pl08x_txd {
181 	struct virt_dma_desc vd;
182 	struct list_head dsg_list;
183 	dma_addr_t llis_bus;
184 	u32 *llis_va;
185 	/* Default cctl value for LLIs */
186 	u32 cctl;
187 	/*
188 	 * Settings to be put into the physical channel when we
189 	 * trigger this txd.  Other registers are in llis_va[0].
190 	 */
191 	u32 ccfg;
192 	bool done;
193 	bool cyclic;
194 };
195 
196 /**
197  * struct pl08x_dma_chan_state - holds the PL08x specific virtual channel
198  * states
199  * @PL08X_CHAN_IDLE: the channel is idle
200  * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
201  * channel and is running a transfer on it
202  * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
203  * channel, but the transfer is currently paused
204  * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
205  * channel to become available (only pertains to memcpy channels)
206  */
207 enum pl08x_dma_chan_state {
208 	PL08X_CHAN_IDLE,
209 	PL08X_CHAN_RUNNING,
210 	PL08X_CHAN_PAUSED,
211 	PL08X_CHAN_WAITING,
212 };
213 
214 /**
215  * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
216  * @vc: wrappped virtual channel
217  * @phychan: the physical channel utilized by this channel, if there is one
218  * @name: name of channel
219  * @cd: channel platform data
220  * @runtime_addr: address for RX/TX according to the runtime config
221  * @at: active transaction on this channel
222  * @lock: a lock for this channel data
223  * @host: a pointer to the host (internal use)
224  * @state: whether the channel is idle, paused, running etc
225  * @slave: whether this channel is a device (slave) or for memcpy
226  * @signal: the physical DMA request signal which this channel is using
227  * @mux_use: count of descriptors using this DMA request signal setting
228  */
229 struct pl08x_dma_chan {
230 	struct virt_dma_chan vc;
231 	struct pl08x_phy_chan *phychan;
232 	const char *name;
233 	const struct pl08x_channel_data *cd;
234 	struct dma_slave_config cfg;
235 	struct pl08x_txd *at;
236 	struct pl08x_driver_data *host;
237 	enum pl08x_dma_chan_state state;
238 	bool slave;
239 	int signal;
240 	unsigned mux_use;
241 };
242 
243 /**
244  * struct pl08x_driver_data - the local state holder for the PL08x
245  * @slave: slave engine for this instance
246  * @memcpy: memcpy engine for this instance
247  * @base: virtual memory base (remapped) for the PL08x
248  * @adev: the corresponding AMBA (PrimeCell) bus entry
249  * @vd: vendor data for this PL08x variant
250  * @pd: platform data passed in from the platform/machine
251  * @phy_chans: array of data for the physical channels
252  * @pool: a pool for the LLI descriptors
253  * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
254  * fetches
255  * @mem_buses: set to indicate memory transfers on AHB2.
256  * @lock: a spinlock for this struct
257  */
258 struct pl08x_driver_data {
259 	struct dma_device slave;
260 	struct dma_device memcpy;
261 	void __iomem *base;
262 	struct amba_device *adev;
263 	const struct vendor_data *vd;
264 	struct pl08x_platform_data *pd;
265 	struct pl08x_phy_chan *phy_chans;
266 	struct dma_pool *pool;
267 	u8 lli_buses;
268 	u8 mem_buses;
269 	u8 lli_words;
270 };
271 
272 /*
273  * PL08X specific defines
274  */
275 
276 /* The order of words in an LLI. */
277 #define PL080_LLI_SRC		0
278 #define PL080_LLI_DST		1
279 #define PL080_LLI_LLI		2
280 #define PL080_LLI_CCTL		3
281 #define PL080S_LLI_CCTL2	4
282 
283 /* Total words in an LLI. */
284 #define PL080_LLI_WORDS		4
285 #define PL080S_LLI_WORDS	8
286 
287 /*
288  * Number of LLIs in each LLI buffer allocated for one transfer
289  * (maximum times we call dma_pool_alloc on this pool without freeing)
290  */
291 #define MAX_NUM_TSFR_LLIS	512
292 #define PL08X_ALIGN		8
293 
294 static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
295 {
296 	return container_of(chan, struct pl08x_dma_chan, vc.chan);
297 }
298 
299 static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
300 {
301 	return container_of(tx, struct pl08x_txd, vd.tx);
302 }
303 
304 /*
305  * Mux handling.
306  *
307  * This gives us the DMA request input to the PL08x primecell which the
308  * peripheral described by the channel data will be routed to, possibly
309  * via a board/SoC specific external MUX.  One important point to note
310  * here is that this does not depend on the physical channel.
311  */
312 static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
313 {
314 	const struct pl08x_platform_data *pd = plchan->host->pd;
315 	int ret;
316 
317 	if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
318 		ret = pd->get_xfer_signal(plchan->cd);
319 		if (ret < 0) {
320 			plchan->mux_use = 0;
321 			return ret;
322 		}
323 
324 		plchan->signal = ret;
325 	}
326 	return 0;
327 }
328 
329 static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
330 {
331 	const struct pl08x_platform_data *pd = plchan->host->pd;
332 
333 	if (plchan->signal >= 0) {
334 		WARN_ON(plchan->mux_use == 0);
335 
336 		if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
337 			pd->put_xfer_signal(plchan->cd, plchan->signal);
338 			plchan->signal = -1;
339 		}
340 	}
341 }
342 
343 /*
344  * Physical channel handling
345  */
346 
347 /* Whether a certain channel is busy or not */
348 static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
349 {
350 	unsigned int val;
351 
352 	val = readl(ch->reg_config);
353 	return val & PL080_CONFIG_ACTIVE;
354 }
355 
356 static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
357 		struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
358 {
359 	if (pl08x->vd->pl080s)
360 		dev_vdbg(&pl08x->adev->dev,
361 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
362 			"clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
363 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
364 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
365 			lli[PL080S_LLI_CCTL2], ccfg);
366 	else
367 		dev_vdbg(&pl08x->adev->dev,
368 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
369 			"clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
370 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
371 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
372 
373 	writel_relaxed(lli[PL080_LLI_SRC], phychan->base + PL080_CH_SRC_ADDR);
374 	writel_relaxed(lli[PL080_LLI_DST], phychan->base + PL080_CH_DST_ADDR);
375 	writel_relaxed(lli[PL080_LLI_LLI], phychan->base + PL080_CH_LLI);
376 	writel_relaxed(lli[PL080_LLI_CCTL], phychan->base + PL080_CH_CONTROL);
377 
378 	if (pl08x->vd->pl080s)
379 		writel_relaxed(lli[PL080S_LLI_CCTL2],
380 				phychan->base + PL080S_CH_CONTROL2);
381 
382 	writel(ccfg, phychan->reg_config);
383 }
384 
385 /*
386  * Set the initial DMA register values i.e. those for the first LLI
387  * The next LLI pointer and the configuration interrupt bit have
388  * been set when the LLIs were constructed.  Poke them into the hardware
389  * and start the transfer.
390  */
391 static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
392 {
393 	struct pl08x_driver_data *pl08x = plchan->host;
394 	struct pl08x_phy_chan *phychan = plchan->phychan;
395 	struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
396 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
397 	u32 val;
398 
399 	list_del(&txd->vd.node);
400 
401 	plchan->at = txd;
402 
403 	/* Wait for channel inactive */
404 	while (pl08x_phy_channel_busy(phychan))
405 		cpu_relax();
406 
407 	pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
408 
409 	/* Enable the DMA channel */
410 	/* Do not access config register until channel shows as disabled */
411 	while (readl(pl08x->base + PL080_EN_CHAN) & (1 << phychan->id))
412 		cpu_relax();
413 
414 	/* Do not access config register until channel shows as inactive */
415 	val = readl(phychan->reg_config);
416 	while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
417 		val = readl(phychan->reg_config);
418 
419 	writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
420 }
421 
422 /*
423  * Pause the channel by setting the HALT bit.
424  *
425  * For M->P transfers, pause the DMAC first and then stop the peripheral -
426  * the FIFO can only drain if the peripheral is still requesting data.
427  * (note: this can still timeout if the DMAC FIFO never drains of data.)
428  *
429  * For P->M transfers, disable the peripheral first to stop it filling
430  * the DMAC FIFO, and then pause the DMAC.
431  */
432 static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
433 {
434 	u32 val;
435 	int timeout;
436 
437 	/* Set the HALT bit and wait for the FIFO to drain */
438 	val = readl(ch->reg_config);
439 	val |= PL080_CONFIG_HALT;
440 	writel(val, ch->reg_config);
441 
442 	/* Wait for channel inactive */
443 	for (timeout = 1000; timeout; timeout--) {
444 		if (!pl08x_phy_channel_busy(ch))
445 			break;
446 		udelay(1);
447 	}
448 	if (pl08x_phy_channel_busy(ch))
449 		pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
450 }
451 
452 static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
453 {
454 	u32 val;
455 
456 	/* Clear the HALT bit */
457 	val = readl(ch->reg_config);
458 	val &= ~PL080_CONFIG_HALT;
459 	writel(val, ch->reg_config);
460 }
461 
462 /*
463  * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
464  * clears any pending interrupt status.  This should not be used for
465  * an on-going transfer, but as a method of shutting down a channel
466  * (eg, when it's no longer used) or terminating a transfer.
467  */
468 static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
469 	struct pl08x_phy_chan *ch)
470 {
471 	u32 val = readl(ch->reg_config);
472 
473 	val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
474 	         PL080_CONFIG_TC_IRQ_MASK);
475 
476 	writel(val, ch->reg_config);
477 
478 	writel(1 << ch->id, pl08x->base + PL080_ERR_CLEAR);
479 	writel(1 << ch->id, pl08x->base + PL080_TC_CLEAR);
480 }
481 
482 static inline u32 get_bytes_in_cctl(u32 cctl)
483 {
484 	/* The source width defines the number of bytes */
485 	u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
486 
487 	cctl &= PL080_CONTROL_SWIDTH_MASK;
488 
489 	switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
490 	case PL080_WIDTH_8BIT:
491 		break;
492 	case PL080_WIDTH_16BIT:
493 		bytes *= 2;
494 		break;
495 	case PL080_WIDTH_32BIT:
496 		bytes *= 4;
497 		break;
498 	}
499 	return bytes;
500 }
501 
502 static inline u32 get_bytes_in_cctl_pl080s(u32 cctl, u32 cctl1)
503 {
504 	/* The source width defines the number of bytes */
505 	u32 bytes = cctl1 & PL080S_CONTROL_TRANSFER_SIZE_MASK;
506 
507 	cctl &= PL080_CONTROL_SWIDTH_MASK;
508 
509 	switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
510 	case PL080_WIDTH_8BIT:
511 		break;
512 	case PL080_WIDTH_16BIT:
513 		bytes *= 2;
514 		break;
515 	case PL080_WIDTH_32BIT:
516 		bytes *= 4;
517 		break;
518 	}
519 	return bytes;
520 }
521 
522 /* The channel should be paused when calling this */
523 static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
524 {
525 	struct pl08x_driver_data *pl08x = plchan->host;
526 	const u32 *llis_va, *llis_va_limit;
527 	struct pl08x_phy_chan *ch;
528 	dma_addr_t llis_bus;
529 	struct pl08x_txd *txd;
530 	u32 llis_max_words;
531 	size_t bytes;
532 	u32 clli;
533 
534 	ch = plchan->phychan;
535 	txd = plchan->at;
536 
537 	if (!ch || !txd)
538 		return 0;
539 
540 	/*
541 	 * Follow the LLIs to get the number of remaining
542 	 * bytes in the currently active transaction.
543 	 */
544 	clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
545 
546 	/* First get the remaining bytes in the active transfer */
547 	if (pl08x->vd->pl080s)
548 		bytes = get_bytes_in_cctl_pl080s(
549 				readl(ch->base + PL080_CH_CONTROL),
550 				readl(ch->base + PL080S_CH_CONTROL2));
551 	else
552 		bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
553 
554 	if (!clli)
555 		return bytes;
556 
557 	llis_va = txd->llis_va;
558 	llis_bus = txd->llis_bus;
559 
560 	llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
561 	BUG_ON(clli < llis_bus || clli >= llis_bus +
562 						sizeof(u32) * llis_max_words);
563 
564 	/*
565 	 * Locate the next LLI - as this is an array,
566 	 * it's simple maths to find.
567 	 */
568 	llis_va += (clli - llis_bus) / sizeof(u32);
569 
570 	llis_va_limit = llis_va + llis_max_words;
571 
572 	for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
573 		if (pl08x->vd->pl080s)
574 			bytes += get_bytes_in_cctl_pl080s(
575 						llis_va[PL080_LLI_CCTL],
576 						llis_va[PL080S_LLI_CCTL2]);
577 		else
578 			bytes += get_bytes_in_cctl(llis_va[PL080_LLI_CCTL]);
579 
580 		/*
581 		 * A LLI pointer going backward terminates the LLI list
582 		 */
583 		if (llis_va[PL080_LLI_LLI] <= clli)
584 			break;
585 	}
586 
587 	return bytes;
588 }
589 
590 /*
591  * Allocate a physical channel for a virtual channel
592  *
593  * Try to locate a physical channel to be used for this transfer. If all
594  * are taken return NULL and the requester will have to cope by using
595  * some fallback PIO mode or retrying later.
596  */
597 static struct pl08x_phy_chan *
598 pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
599 		      struct pl08x_dma_chan *virt_chan)
600 {
601 	struct pl08x_phy_chan *ch = NULL;
602 	unsigned long flags;
603 	int i;
604 
605 	for (i = 0; i < pl08x->vd->channels; i++) {
606 		ch = &pl08x->phy_chans[i];
607 
608 		spin_lock_irqsave(&ch->lock, flags);
609 
610 		if (!ch->locked && !ch->serving) {
611 			ch->serving = virt_chan;
612 			spin_unlock_irqrestore(&ch->lock, flags);
613 			break;
614 		}
615 
616 		spin_unlock_irqrestore(&ch->lock, flags);
617 	}
618 
619 	if (i == pl08x->vd->channels) {
620 		/* No physical channel available, cope with it */
621 		return NULL;
622 	}
623 
624 	return ch;
625 }
626 
627 /* Mark the physical channel as free.  Note, this write is atomic. */
628 static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
629 					 struct pl08x_phy_chan *ch)
630 {
631 	ch->serving = NULL;
632 }
633 
634 /*
635  * Try to allocate a physical channel.  When successful, assign it to
636  * this virtual channel, and initiate the next descriptor.  The
637  * virtual channel lock must be held at this point.
638  */
639 static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
640 {
641 	struct pl08x_driver_data *pl08x = plchan->host;
642 	struct pl08x_phy_chan *ch;
643 
644 	ch = pl08x_get_phy_channel(pl08x, plchan);
645 	if (!ch) {
646 		dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
647 		plchan->state = PL08X_CHAN_WAITING;
648 		return;
649 	}
650 
651 	dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
652 		ch->id, plchan->name);
653 
654 	plchan->phychan = ch;
655 	plchan->state = PL08X_CHAN_RUNNING;
656 	pl08x_start_next_txd(plchan);
657 }
658 
659 static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
660 	struct pl08x_dma_chan *plchan)
661 {
662 	struct pl08x_driver_data *pl08x = plchan->host;
663 
664 	dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
665 		ch->id, plchan->name);
666 
667 	/*
668 	 * We do this without taking the lock; we're really only concerned
669 	 * about whether this pointer is NULL or not, and we're guaranteed
670 	 * that this will only be called when it _already_ is non-NULL.
671 	 */
672 	ch->serving = plchan;
673 	plchan->phychan = ch;
674 	plchan->state = PL08X_CHAN_RUNNING;
675 	pl08x_start_next_txd(plchan);
676 }
677 
678 /*
679  * Free a physical DMA channel, potentially reallocating it to another
680  * virtual channel if we have any pending.
681  */
682 static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
683 {
684 	struct pl08x_driver_data *pl08x = plchan->host;
685 	struct pl08x_dma_chan *p, *next;
686 
687  retry:
688 	next = NULL;
689 
690 	/* Find a waiting virtual channel for the next transfer. */
691 	list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
692 		if (p->state == PL08X_CHAN_WAITING) {
693 			next = p;
694 			break;
695 		}
696 
697 	if (!next) {
698 		list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
699 			if (p->state == PL08X_CHAN_WAITING) {
700 				next = p;
701 				break;
702 			}
703 	}
704 
705 	/* Ensure that the physical channel is stopped */
706 	pl08x_terminate_phy_chan(pl08x, plchan->phychan);
707 
708 	if (next) {
709 		bool success;
710 
711 		/*
712 		 * Eww.  We know this isn't going to deadlock
713 		 * but lockdep probably doesn't.
714 		 */
715 		spin_lock(&next->vc.lock);
716 		/* Re-check the state now that we have the lock */
717 		success = next->state == PL08X_CHAN_WAITING;
718 		if (success)
719 			pl08x_phy_reassign_start(plchan->phychan, next);
720 		spin_unlock(&next->vc.lock);
721 
722 		/* If the state changed, try to find another channel */
723 		if (!success)
724 			goto retry;
725 	} else {
726 		/* No more jobs, so free up the physical channel */
727 		pl08x_put_phy_channel(pl08x, plchan->phychan);
728 	}
729 
730 	plchan->phychan = NULL;
731 	plchan->state = PL08X_CHAN_IDLE;
732 }
733 
734 /*
735  * LLI handling
736  */
737 
738 static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
739 {
740 	switch (coded) {
741 	case PL080_WIDTH_8BIT:
742 		return 1;
743 	case PL080_WIDTH_16BIT:
744 		return 2;
745 	case PL080_WIDTH_32BIT:
746 		return 4;
747 	default:
748 		break;
749 	}
750 	BUG();
751 	return 0;
752 }
753 
754 static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
755 				  size_t tsize)
756 {
757 	u32 retbits = cctl;
758 
759 	/* Remove all src, dst and transfer size bits */
760 	retbits &= ~PL080_CONTROL_DWIDTH_MASK;
761 	retbits &= ~PL080_CONTROL_SWIDTH_MASK;
762 	retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
763 
764 	/* Then set the bits according to the parameters */
765 	switch (srcwidth) {
766 	case 1:
767 		retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
768 		break;
769 	case 2:
770 		retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
771 		break;
772 	case 4:
773 		retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
774 		break;
775 	default:
776 		BUG();
777 		break;
778 	}
779 
780 	switch (dstwidth) {
781 	case 1:
782 		retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
783 		break;
784 	case 2:
785 		retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
786 		break;
787 	case 4:
788 		retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
789 		break;
790 	default:
791 		BUG();
792 		break;
793 	}
794 
795 	tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
796 	retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
797 	return retbits;
798 }
799 
800 struct pl08x_lli_build_data {
801 	struct pl08x_txd *txd;
802 	struct pl08x_bus_data srcbus;
803 	struct pl08x_bus_data dstbus;
804 	size_t remainder;
805 	u32 lli_bus;
806 };
807 
808 /*
809  * Autoselect a master bus to use for the transfer. Slave will be the chosen as
810  * victim in case src & dest are not similarly aligned. i.e. If after aligning
811  * masters address with width requirements of transfer (by sending few byte by
812  * byte data), slave is still not aligned, then its width will be reduced to
813  * BYTE.
814  * - prefers the destination bus if both available
815  * - prefers bus with fixed address (i.e. peripheral)
816  */
817 static void pl08x_choose_master_bus(struct pl08x_lli_build_data *bd,
818 	struct pl08x_bus_data **mbus, struct pl08x_bus_data **sbus, u32 cctl)
819 {
820 	if (!(cctl & PL080_CONTROL_DST_INCR)) {
821 		*mbus = &bd->dstbus;
822 		*sbus = &bd->srcbus;
823 	} else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
824 		*mbus = &bd->srcbus;
825 		*sbus = &bd->dstbus;
826 	} else {
827 		if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
828 			*mbus = &bd->dstbus;
829 			*sbus = &bd->srcbus;
830 		} else {
831 			*mbus = &bd->srcbus;
832 			*sbus = &bd->dstbus;
833 		}
834 	}
835 }
836 
837 /*
838  * Fills in one LLI for a certain transfer descriptor and advance the counter
839  */
840 static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
841 				    struct pl08x_lli_build_data *bd,
842 				    int num_llis, int len, u32 cctl, u32 cctl2)
843 {
844 	u32 offset = num_llis * pl08x->lli_words;
845 	u32 *llis_va = bd->txd->llis_va + offset;
846 	dma_addr_t llis_bus = bd->txd->llis_bus;
847 
848 	BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
849 
850 	/* Advance the offset to next LLI. */
851 	offset += pl08x->lli_words;
852 
853 	llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
854 	llis_va[PL080_LLI_DST] = bd->dstbus.addr;
855 	llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
856 	llis_va[PL080_LLI_LLI] |= bd->lli_bus;
857 	llis_va[PL080_LLI_CCTL] = cctl;
858 	if (pl08x->vd->pl080s)
859 		llis_va[PL080S_LLI_CCTL2] = cctl2;
860 
861 	if (cctl & PL080_CONTROL_SRC_INCR)
862 		bd->srcbus.addr += len;
863 	if (cctl & PL080_CONTROL_DST_INCR)
864 		bd->dstbus.addr += len;
865 
866 	BUG_ON(bd->remainder < len);
867 
868 	bd->remainder -= len;
869 }
870 
871 static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
872 			struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
873 			int num_llis, size_t *total_bytes)
874 {
875 	*cctl = pl08x_cctl_bits(*cctl, 1, 1, len);
876 	pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
877 	(*total_bytes) += len;
878 }
879 
880 #ifdef VERBOSE_DEBUG
881 static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
882 			   const u32 *llis_va, int num_llis)
883 {
884 	int i;
885 
886 	if (pl08x->vd->pl080s) {
887 		dev_vdbg(&pl08x->adev->dev,
888 			"%-3s %-9s  %-10s %-10s %-10s %-10s %s\n",
889 			"lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
890 		for (i = 0; i < num_llis; i++) {
891 			dev_vdbg(&pl08x->adev->dev,
892 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
893 				i, llis_va, llis_va[PL080_LLI_SRC],
894 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
895 				llis_va[PL080_LLI_CCTL],
896 				llis_va[PL080S_LLI_CCTL2]);
897 			llis_va += pl08x->lli_words;
898 		}
899 	} else {
900 		dev_vdbg(&pl08x->adev->dev,
901 			"%-3s %-9s  %-10s %-10s %-10s %s\n",
902 			"lli", "", "csrc", "cdst", "clli", "cctl");
903 		for (i = 0; i < num_llis; i++) {
904 			dev_vdbg(&pl08x->adev->dev,
905 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
906 				i, llis_va, llis_va[PL080_LLI_SRC],
907 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
908 				llis_va[PL080_LLI_CCTL]);
909 			llis_va += pl08x->lli_words;
910 		}
911 	}
912 }
913 #else
914 static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
915 				  const u32 *llis_va, int num_llis) {}
916 #endif
917 
918 /*
919  * This fills in the table of LLIs for the transfer descriptor
920  * Note that we assume we never have to change the burst sizes
921  * Return 0 for error
922  */
923 static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
924 			      struct pl08x_txd *txd)
925 {
926 	struct pl08x_bus_data *mbus, *sbus;
927 	struct pl08x_lli_build_data bd;
928 	int num_llis = 0;
929 	u32 cctl, early_bytes = 0;
930 	size_t max_bytes_per_lli, total_bytes;
931 	u32 *llis_va, *last_lli;
932 	struct pl08x_sg *dsg;
933 
934 	txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
935 	if (!txd->llis_va) {
936 		dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
937 		return 0;
938 	}
939 
940 	bd.txd = txd;
941 	bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
942 	cctl = txd->cctl;
943 
944 	/* Find maximum width of the source bus */
945 	bd.srcbus.maxwidth =
946 		pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
947 				       PL080_CONTROL_SWIDTH_SHIFT);
948 
949 	/* Find maximum width of the destination bus */
950 	bd.dstbus.maxwidth =
951 		pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
952 				       PL080_CONTROL_DWIDTH_SHIFT);
953 
954 	list_for_each_entry(dsg, &txd->dsg_list, node) {
955 		total_bytes = 0;
956 		cctl = txd->cctl;
957 
958 		bd.srcbus.addr = dsg->src_addr;
959 		bd.dstbus.addr = dsg->dst_addr;
960 		bd.remainder = dsg->len;
961 		bd.srcbus.buswidth = bd.srcbus.maxwidth;
962 		bd.dstbus.buswidth = bd.dstbus.maxwidth;
963 
964 		pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl);
965 
966 		dev_vdbg(&pl08x->adev->dev,
967 			"src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
968 			(u64)bd.srcbus.addr,
969 			cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
970 			bd.srcbus.buswidth,
971 			(u64)bd.dstbus.addr,
972 			cctl & PL080_CONTROL_DST_INCR ? "+" : "",
973 			bd.dstbus.buswidth,
974 			bd.remainder);
975 		dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
976 			mbus == &bd.srcbus ? "src" : "dst",
977 			sbus == &bd.srcbus ? "src" : "dst");
978 
979 		/*
980 		 * Zero length is only allowed if all these requirements are
981 		 * met:
982 		 * - flow controller is peripheral.
983 		 * - src.addr is aligned to src.width
984 		 * - dst.addr is aligned to dst.width
985 		 *
986 		 * sg_len == 1 should be true, as there can be two cases here:
987 		 *
988 		 * - Memory addresses are contiguous and are not scattered.
989 		 *   Here, Only one sg will be passed by user driver, with
990 		 *   memory address and zero length. We pass this to controller
991 		 *   and after the transfer it will receive the last burst
992 		 *   request from peripheral and so transfer finishes.
993 		 *
994 		 * - Memory addresses are scattered and are not contiguous.
995 		 *   Here, Obviously as DMA controller doesn't know when a lli's
996 		 *   transfer gets over, it can't load next lli. So in this
997 		 *   case, there has to be an assumption that only one lli is
998 		 *   supported. Thus, we can't have scattered addresses.
999 		 */
1000 		if (!bd.remainder) {
1001 			u32 fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
1002 				PL080_CONFIG_FLOW_CONTROL_SHIFT;
1003 			if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
1004 					(fc <= PL080_FLOW_SRC2DST_SRC))) {
1005 				dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
1006 					__func__);
1007 				return 0;
1008 			}
1009 
1010 			if (!IS_BUS_ALIGNED(&bd.srcbus) ||
1011 				!IS_BUS_ALIGNED(&bd.dstbus)) {
1012 				dev_err(&pl08x->adev->dev,
1013 					"%s src & dst address must be aligned to src"
1014 					" & dst width if peripheral is flow controller",
1015 					__func__);
1016 				return 0;
1017 			}
1018 
1019 			cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
1020 					bd.dstbus.buswidth, 0);
1021 			pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1022 					0, cctl, 0);
1023 			break;
1024 		}
1025 
1026 		/*
1027 		 * Send byte by byte for following cases
1028 		 * - Less than a bus width available
1029 		 * - until master bus is aligned
1030 		 */
1031 		if (bd.remainder < mbus->buswidth)
1032 			early_bytes = bd.remainder;
1033 		else if (!IS_BUS_ALIGNED(mbus)) {
1034 			early_bytes = mbus->buswidth -
1035 				(mbus->addr & (mbus->buswidth - 1));
1036 			if ((bd.remainder - early_bytes) < mbus->buswidth)
1037 				early_bytes = bd.remainder;
1038 		}
1039 
1040 		if (early_bytes) {
1041 			dev_vdbg(&pl08x->adev->dev,
1042 				"%s byte width LLIs (remain 0x%08x)\n",
1043 				__func__, bd.remainder);
1044 			prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
1045 				num_llis++, &total_bytes);
1046 		}
1047 
1048 		if (bd.remainder) {
1049 			/*
1050 			 * Master now aligned
1051 			 * - if slave is not then we must set its width down
1052 			 */
1053 			if (!IS_BUS_ALIGNED(sbus)) {
1054 				dev_dbg(&pl08x->adev->dev,
1055 					"%s set down bus width to one byte\n",
1056 					__func__);
1057 
1058 				sbus->buswidth = 1;
1059 			}
1060 
1061 			/*
1062 			 * Bytes transferred = tsize * src width, not
1063 			 * MIN(buswidths)
1064 			 */
1065 			max_bytes_per_lli = bd.srcbus.buswidth *
1066 						pl08x->vd->max_transfer_size;
1067 			dev_vdbg(&pl08x->adev->dev,
1068 				"%s max bytes per lli = %zu\n",
1069 				__func__, max_bytes_per_lli);
1070 
1071 			/*
1072 			 * Make largest possible LLIs until less than one bus
1073 			 * width left
1074 			 */
1075 			while (bd.remainder > (mbus->buswidth - 1)) {
1076 				size_t lli_len, tsize, width;
1077 
1078 				/*
1079 				 * If enough left try to send max possible,
1080 				 * otherwise try to send the remainder
1081 				 */
1082 				lli_len = min(bd.remainder, max_bytes_per_lli);
1083 
1084 				/*
1085 				 * Check against maximum bus alignment:
1086 				 * Calculate actual transfer size in relation to
1087 				 * bus width an get a maximum remainder of the
1088 				 * highest bus width - 1
1089 				 */
1090 				width = max(mbus->buswidth, sbus->buswidth);
1091 				lli_len = (lli_len / width) * width;
1092 				tsize = lli_len / bd.srcbus.buswidth;
1093 
1094 				dev_vdbg(&pl08x->adev->dev,
1095 					"%s fill lli with single lli chunk of "
1096 					"size 0x%08zx (remainder 0x%08zx)\n",
1097 					__func__, lli_len, bd.remainder);
1098 
1099 				cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
1100 					bd.dstbus.buswidth, tsize);
1101 				pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1102 						lli_len, cctl, tsize);
1103 				total_bytes += lli_len;
1104 			}
1105 
1106 			/*
1107 			 * Send any odd bytes
1108 			 */
1109 			if (bd.remainder) {
1110 				dev_vdbg(&pl08x->adev->dev,
1111 					"%s align with boundary, send odd bytes (remain %zu)\n",
1112 					__func__, bd.remainder);
1113 				prep_byte_width_lli(pl08x, &bd, &cctl,
1114 					bd.remainder, num_llis++, &total_bytes);
1115 			}
1116 		}
1117 
1118 		if (total_bytes != dsg->len) {
1119 			dev_err(&pl08x->adev->dev,
1120 				"%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
1121 				__func__, total_bytes, dsg->len);
1122 			return 0;
1123 		}
1124 
1125 		if (num_llis >= MAX_NUM_TSFR_LLIS) {
1126 			dev_err(&pl08x->adev->dev,
1127 				"%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
1128 				__func__, MAX_NUM_TSFR_LLIS);
1129 			return 0;
1130 		}
1131 	}
1132 
1133 	llis_va = txd->llis_va;
1134 	last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
1135 
1136 	if (txd->cyclic) {
1137 		/* Link back to the first LLI. */
1138 		last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
1139 	} else {
1140 		/* The final LLI terminates the LLI. */
1141 		last_lli[PL080_LLI_LLI] = 0;
1142 		/* The final LLI element shall also fire an interrupt. */
1143 		last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
1144 	}
1145 
1146 	pl08x_dump_lli(pl08x, llis_va, num_llis);
1147 
1148 	return num_llis;
1149 }
1150 
1151 static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
1152 			   struct pl08x_txd *txd)
1153 {
1154 	struct pl08x_sg *dsg, *_dsg;
1155 
1156 	if (txd->llis_va)
1157 		dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
1158 
1159 	list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
1160 		list_del(&dsg->node);
1161 		kfree(dsg);
1162 	}
1163 
1164 	kfree(txd);
1165 }
1166 
1167 static void pl08x_unmap_buffers(struct pl08x_txd *txd)
1168 {
1169 	struct device *dev = txd->vd.tx.chan->device->dev;
1170 	struct pl08x_sg *dsg;
1171 
1172 	if (!(txd->vd.tx.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
1173 		if (txd->vd.tx.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
1174 			list_for_each_entry(dsg, &txd->dsg_list, node)
1175 				dma_unmap_single(dev, dsg->src_addr, dsg->len,
1176 						DMA_TO_DEVICE);
1177 		else {
1178 			list_for_each_entry(dsg, &txd->dsg_list, node)
1179 				dma_unmap_page(dev, dsg->src_addr, dsg->len,
1180 						DMA_TO_DEVICE);
1181 		}
1182 	}
1183 	if (!(txd->vd.tx.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
1184 		if (txd->vd.tx.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
1185 			list_for_each_entry(dsg, &txd->dsg_list, node)
1186 				dma_unmap_single(dev, dsg->dst_addr, dsg->len,
1187 						DMA_FROM_DEVICE);
1188 		else
1189 			list_for_each_entry(dsg, &txd->dsg_list, node)
1190 				dma_unmap_page(dev, dsg->dst_addr, dsg->len,
1191 						DMA_FROM_DEVICE);
1192 	}
1193 }
1194 
1195 static void pl08x_desc_free(struct virt_dma_desc *vd)
1196 {
1197 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1198 	struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
1199 
1200 	if (!plchan->slave)
1201 		pl08x_unmap_buffers(txd);
1202 
1203 	if (!txd->done)
1204 		pl08x_release_mux(plchan);
1205 
1206 	pl08x_free_txd(plchan->host, txd);
1207 }
1208 
1209 static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
1210 				struct pl08x_dma_chan *plchan)
1211 {
1212 	LIST_HEAD(head);
1213 
1214 	vchan_get_all_descriptors(&plchan->vc, &head);
1215 	vchan_dma_desc_free_list(&plchan->vc, &head);
1216 }
1217 
1218 /*
1219  * The DMA ENGINE API
1220  */
1221 static int pl08x_alloc_chan_resources(struct dma_chan *chan)
1222 {
1223 	return 0;
1224 }
1225 
1226 static void pl08x_free_chan_resources(struct dma_chan *chan)
1227 {
1228 	/* Ensure all queued descriptors are freed */
1229 	vchan_free_chan_resources(to_virt_chan(chan));
1230 }
1231 
1232 static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1233 		struct dma_chan *chan, unsigned long flags)
1234 {
1235 	struct dma_async_tx_descriptor *retval = NULL;
1236 
1237 	return retval;
1238 }
1239 
1240 /*
1241  * Code accessing dma_async_is_complete() in a tight loop may give problems.
1242  * If slaves are relying on interrupts to signal completion this function
1243  * must not be called with interrupts disabled.
1244  */
1245 static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
1246 		dma_cookie_t cookie, struct dma_tx_state *txstate)
1247 {
1248 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1249 	struct virt_dma_desc *vd;
1250 	unsigned long flags;
1251 	enum dma_status ret;
1252 	size_t bytes = 0;
1253 
1254 	ret = dma_cookie_status(chan, cookie, txstate);
1255 	if (ret == DMA_SUCCESS)
1256 		return ret;
1257 
1258 	/*
1259 	 * There's no point calculating the residue if there's
1260 	 * no txstate to store the value.
1261 	 */
1262 	if (!txstate) {
1263 		if (plchan->state == PL08X_CHAN_PAUSED)
1264 			ret = DMA_PAUSED;
1265 		return ret;
1266 	}
1267 
1268 	spin_lock_irqsave(&plchan->vc.lock, flags);
1269 	ret = dma_cookie_status(chan, cookie, txstate);
1270 	if (ret != DMA_SUCCESS) {
1271 		vd = vchan_find_desc(&plchan->vc, cookie);
1272 		if (vd) {
1273 			/* On the issued list, so hasn't been processed yet */
1274 			struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1275 			struct pl08x_sg *dsg;
1276 
1277 			list_for_each_entry(dsg, &txd->dsg_list, node)
1278 				bytes += dsg->len;
1279 		} else {
1280 			bytes = pl08x_getbytes_chan(plchan);
1281 		}
1282 	}
1283 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1284 
1285 	/*
1286 	 * This cookie not complete yet
1287 	 * Get number of bytes left in the active transactions and queue
1288 	 */
1289 	dma_set_residue(txstate, bytes);
1290 
1291 	if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
1292 		ret = DMA_PAUSED;
1293 
1294 	/* Whether waiting or running, we're in progress */
1295 	return ret;
1296 }
1297 
1298 /* PrimeCell DMA extension */
1299 struct burst_table {
1300 	u32 burstwords;
1301 	u32 reg;
1302 };
1303 
1304 static const struct burst_table burst_sizes[] = {
1305 	{
1306 		.burstwords = 256,
1307 		.reg = PL080_BSIZE_256,
1308 	},
1309 	{
1310 		.burstwords = 128,
1311 		.reg = PL080_BSIZE_128,
1312 	},
1313 	{
1314 		.burstwords = 64,
1315 		.reg = PL080_BSIZE_64,
1316 	},
1317 	{
1318 		.burstwords = 32,
1319 		.reg = PL080_BSIZE_32,
1320 	},
1321 	{
1322 		.burstwords = 16,
1323 		.reg = PL080_BSIZE_16,
1324 	},
1325 	{
1326 		.burstwords = 8,
1327 		.reg = PL080_BSIZE_8,
1328 	},
1329 	{
1330 		.burstwords = 4,
1331 		.reg = PL080_BSIZE_4,
1332 	},
1333 	{
1334 		.burstwords = 0,
1335 		.reg = PL080_BSIZE_1,
1336 	},
1337 };
1338 
1339 /*
1340  * Given the source and destination available bus masks, select which
1341  * will be routed to each port.  We try to have source and destination
1342  * on separate ports, but always respect the allowable settings.
1343  */
1344 static u32 pl08x_select_bus(u8 src, u8 dst)
1345 {
1346 	u32 cctl = 0;
1347 
1348 	if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1349 		cctl |= PL080_CONTROL_DST_AHB2;
1350 	if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1351 		cctl |= PL080_CONTROL_SRC_AHB2;
1352 
1353 	return cctl;
1354 }
1355 
1356 static u32 pl08x_cctl(u32 cctl)
1357 {
1358 	cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1359 		  PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1360 		  PL080_CONTROL_PROT_MASK);
1361 
1362 	/* Access the cell in privileged mode, non-bufferable, non-cacheable */
1363 	return cctl | PL080_CONTROL_PROT_SYS;
1364 }
1365 
1366 static u32 pl08x_width(enum dma_slave_buswidth width)
1367 {
1368 	switch (width) {
1369 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
1370 		return PL080_WIDTH_8BIT;
1371 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
1372 		return PL080_WIDTH_16BIT;
1373 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
1374 		return PL080_WIDTH_32BIT;
1375 	default:
1376 		return ~0;
1377 	}
1378 }
1379 
1380 static u32 pl08x_burst(u32 maxburst)
1381 {
1382 	int i;
1383 
1384 	for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1385 		if (burst_sizes[i].burstwords <= maxburst)
1386 			break;
1387 
1388 	return burst_sizes[i].reg;
1389 }
1390 
1391 static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
1392 	enum dma_slave_buswidth addr_width, u32 maxburst)
1393 {
1394 	u32 width, burst, cctl = 0;
1395 
1396 	width = pl08x_width(addr_width);
1397 	if (width == ~0)
1398 		return ~0;
1399 
1400 	cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
1401 	cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
1402 
1403 	/*
1404 	 * If this channel will only request single transfers, set this
1405 	 * down to ONE element.  Also select one element if no maxburst
1406 	 * is specified.
1407 	 */
1408 	if (plchan->cd->single)
1409 		maxburst = 1;
1410 
1411 	burst = pl08x_burst(maxburst);
1412 	cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
1413 	cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
1414 
1415 	return pl08x_cctl(cctl);
1416 }
1417 
1418 static int dma_set_runtime_config(struct dma_chan *chan,
1419 				  struct dma_slave_config *config)
1420 {
1421 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1422 	struct pl08x_driver_data *pl08x = plchan->host;
1423 
1424 	if (!plchan->slave)
1425 		return -EINVAL;
1426 
1427 	/* Reject definitely invalid configurations */
1428 	if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1429 	    config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
1430 		return -EINVAL;
1431 
1432 	if (config->device_fc && pl08x->vd->pl080s) {
1433 		dev_err(&pl08x->adev->dev,
1434 			"%s: PL080S does not support peripheral flow control\n",
1435 			__func__);
1436 		return -EINVAL;
1437 	}
1438 
1439 	plchan->cfg = *config;
1440 
1441 	return 0;
1442 }
1443 
1444 /*
1445  * Slave transactions callback to the slave device to allow
1446  * synchronization of slave DMA signals with the DMAC enable
1447  */
1448 static void pl08x_issue_pending(struct dma_chan *chan)
1449 {
1450 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1451 	unsigned long flags;
1452 
1453 	spin_lock_irqsave(&plchan->vc.lock, flags);
1454 	if (vchan_issue_pending(&plchan->vc)) {
1455 		if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
1456 			pl08x_phy_alloc_and_start(plchan);
1457 	}
1458 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1459 }
1460 
1461 static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1462 {
1463 	struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
1464 
1465 	if (txd) {
1466 		INIT_LIST_HEAD(&txd->dsg_list);
1467 
1468 		/* Always enable error and terminal interrupts */
1469 		txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1470 			    PL080_CONFIG_TC_IRQ_MASK;
1471 	}
1472 	return txd;
1473 }
1474 
1475 /*
1476  * Initialize a descriptor to be used by memcpy submit
1477  */
1478 static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1479 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1480 		size_t len, unsigned long flags)
1481 {
1482 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1483 	struct pl08x_driver_data *pl08x = plchan->host;
1484 	struct pl08x_txd *txd;
1485 	struct pl08x_sg *dsg;
1486 	int ret;
1487 
1488 	txd = pl08x_get_txd(plchan);
1489 	if (!txd) {
1490 		dev_err(&pl08x->adev->dev,
1491 			"%s no memory for descriptor\n", __func__);
1492 		return NULL;
1493 	}
1494 
1495 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
1496 	if (!dsg) {
1497 		pl08x_free_txd(pl08x, txd);
1498 		dev_err(&pl08x->adev->dev, "%s no memory for pl080 sg\n",
1499 				__func__);
1500 		return NULL;
1501 	}
1502 	list_add_tail(&dsg->node, &txd->dsg_list);
1503 
1504 	dsg->src_addr = src;
1505 	dsg->dst_addr = dest;
1506 	dsg->len = len;
1507 
1508 	/* Set platform data for m2m */
1509 	txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1510 	txd->cctl = pl08x->pd->memcpy_channel.cctl_memcpy &
1511 			~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
1512 
1513 	/* Both to be incremented or the code will break */
1514 	txd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
1515 
1516 	if (pl08x->vd->dualmaster)
1517 		txd->cctl |= pl08x_select_bus(pl08x->mem_buses,
1518 					      pl08x->mem_buses);
1519 
1520 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1521 	if (!ret) {
1522 		pl08x_free_txd(pl08x, txd);
1523 		return NULL;
1524 	}
1525 
1526 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1527 }
1528 
1529 static struct pl08x_txd *pl08x_init_txd(
1530 		struct dma_chan *chan,
1531 		enum dma_transfer_direction direction,
1532 		dma_addr_t *slave_addr)
1533 {
1534 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1535 	struct pl08x_driver_data *pl08x = plchan->host;
1536 	struct pl08x_txd *txd;
1537 	enum dma_slave_buswidth addr_width;
1538 	int ret, tmp;
1539 	u8 src_buses, dst_buses;
1540 	u32 maxburst, cctl;
1541 
1542 	txd = pl08x_get_txd(plchan);
1543 	if (!txd) {
1544 		dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1545 		return NULL;
1546 	}
1547 
1548 	/*
1549 	 * Set up addresses, the PrimeCell configured address
1550 	 * will take precedence since this may configure the
1551 	 * channel target address dynamically at runtime.
1552 	 */
1553 	if (direction == DMA_MEM_TO_DEV) {
1554 		cctl = PL080_CONTROL_SRC_INCR;
1555 		*slave_addr = plchan->cfg.dst_addr;
1556 		addr_width = plchan->cfg.dst_addr_width;
1557 		maxburst = plchan->cfg.dst_maxburst;
1558 		src_buses = pl08x->mem_buses;
1559 		dst_buses = plchan->cd->periph_buses;
1560 	} else if (direction == DMA_DEV_TO_MEM) {
1561 		cctl = PL080_CONTROL_DST_INCR;
1562 		*slave_addr = plchan->cfg.src_addr;
1563 		addr_width = plchan->cfg.src_addr_width;
1564 		maxburst = plchan->cfg.src_maxburst;
1565 		src_buses = plchan->cd->periph_buses;
1566 		dst_buses = pl08x->mem_buses;
1567 	} else {
1568 		pl08x_free_txd(pl08x, txd);
1569 		dev_err(&pl08x->adev->dev,
1570 			"%s direction unsupported\n", __func__);
1571 		return NULL;
1572 	}
1573 
1574 	cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
1575 	if (cctl == ~0) {
1576 		pl08x_free_txd(pl08x, txd);
1577 		dev_err(&pl08x->adev->dev,
1578 			"DMA slave configuration botched?\n");
1579 		return NULL;
1580 	}
1581 
1582 	txd->cctl = cctl | pl08x_select_bus(src_buses, dst_buses);
1583 
1584 	if (plchan->cfg.device_fc)
1585 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
1586 			PL080_FLOW_PER2MEM_PER;
1587 	else
1588 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
1589 			PL080_FLOW_PER2MEM;
1590 
1591 	txd->ccfg |= tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1592 
1593 	ret = pl08x_request_mux(plchan);
1594 	if (ret < 0) {
1595 		pl08x_free_txd(pl08x, txd);
1596 		dev_dbg(&pl08x->adev->dev,
1597 			"unable to mux for transfer on %s due to platform restrictions\n",
1598 			plchan->name);
1599 		return NULL;
1600 	}
1601 
1602 	dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
1603 		 plchan->signal, plchan->name);
1604 
1605 	/* Assign the flow control signal to this channel */
1606 	if (direction == DMA_MEM_TO_DEV)
1607 		txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
1608 	else
1609 		txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
1610 
1611 	return txd;
1612 }
1613 
1614 static int pl08x_tx_add_sg(struct pl08x_txd *txd,
1615 			   enum dma_transfer_direction direction,
1616 			   dma_addr_t slave_addr,
1617 			   dma_addr_t buf_addr,
1618 			   unsigned int len)
1619 {
1620 	struct pl08x_sg *dsg;
1621 
1622 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
1623 	if (!dsg)
1624 		return -ENOMEM;
1625 
1626 	list_add_tail(&dsg->node, &txd->dsg_list);
1627 
1628 	dsg->len = len;
1629 	if (direction == DMA_MEM_TO_DEV) {
1630 		dsg->src_addr = buf_addr;
1631 		dsg->dst_addr = slave_addr;
1632 	} else {
1633 		dsg->src_addr = slave_addr;
1634 		dsg->dst_addr = buf_addr;
1635 	}
1636 
1637 	return 0;
1638 }
1639 
1640 static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
1641 		struct dma_chan *chan, struct scatterlist *sgl,
1642 		unsigned int sg_len, enum dma_transfer_direction direction,
1643 		unsigned long flags, void *context)
1644 {
1645 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1646 	struct pl08x_driver_data *pl08x = plchan->host;
1647 	struct pl08x_txd *txd;
1648 	struct scatterlist *sg;
1649 	int ret, tmp;
1650 	dma_addr_t slave_addr;
1651 
1652 	dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1653 			__func__, sg_dma_len(sgl), plchan->name);
1654 
1655 	txd = pl08x_init_txd(chan, direction, &slave_addr);
1656 	if (!txd)
1657 		return NULL;
1658 
1659 	for_each_sg(sgl, sg, sg_len, tmp) {
1660 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
1661 				      sg_dma_address(sg),
1662 				      sg_dma_len(sg));
1663 		if (ret) {
1664 			pl08x_release_mux(plchan);
1665 			pl08x_free_txd(pl08x, txd);
1666 			dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
1667 					__func__);
1668 			return NULL;
1669 		}
1670 	}
1671 
1672 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1673 	if (!ret) {
1674 		pl08x_release_mux(plchan);
1675 		pl08x_free_txd(pl08x, txd);
1676 		return NULL;
1677 	}
1678 
1679 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1680 }
1681 
1682 static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
1683 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1684 		size_t period_len, enum dma_transfer_direction direction,
1685 		unsigned long flags, void *context)
1686 {
1687 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1688 	struct pl08x_driver_data *pl08x = plchan->host;
1689 	struct pl08x_txd *txd;
1690 	int ret, tmp;
1691 	dma_addr_t slave_addr;
1692 
1693 	dev_dbg(&pl08x->adev->dev,
1694 		"%s prepare cyclic transaction of %d/%d bytes %s %s\n",
1695 		__func__, period_len, buf_len,
1696 		direction == DMA_MEM_TO_DEV ? "to" : "from",
1697 		plchan->name);
1698 
1699 	txd = pl08x_init_txd(chan, direction, &slave_addr);
1700 	if (!txd)
1701 		return NULL;
1702 
1703 	txd->cyclic = true;
1704 	txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
1705 	for (tmp = 0; tmp < buf_len; tmp += period_len) {
1706 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
1707 				      buf_addr + tmp, period_len);
1708 		if (ret) {
1709 			pl08x_release_mux(plchan);
1710 			pl08x_free_txd(pl08x, txd);
1711 			return NULL;
1712 		}
1713 	}
1714 
1715 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1716 	if (!ret) {
1717 		pl08x_release_mux(plchan);
1718 		pl08x_free_txd(pl08x, txd);
1719 		return NULL;
1720 	}
1721 
1722 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1723 }
1724 
1725 static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1726 			 unsigned long arg)
1727 {
1728 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1729 	struct pl08x_driver_data *pl08x = plchan->host;
1730 	unsigned long flags;
1731 	int ret = 0;
1732 
1733 	/* Controls applicable to inactive channels */
1734 	if (cmd == DMA_SLAVE_CONFIG) {
1735 		return dma_set_runtime_config(chan,
1736 					      (struct dma_slave_config *)arg);
1737 	}
1738 
1739 	/*
1740 	 * Anything succeeds on channels with no physical allocation and
1741 	 * no queued transfers.
1742 	 */
1743 	spin_lock_irqsave(&plchan->vc.lock, flags);
1744 	if (!plchan->phychan && !plchan->at) {
1745 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
1746 		return 0;
1747 	}
1748 
1749 	switch (cmd) {
1750 	case DMA_TERMINATE_ALL:
1751 		plchan->state = PL08X_CHAN_IDLE;
1752 
1753 		if (plchan->phychan) {
1754 			/*
1755 			 * Mark physical channel as free and free any slave
1756 			 * signal
1757 			 */
1758 			pl08x_phy_free(plchan);
1759 		}
1760 		/* Dequeue jobs and free LLIs */
1761 		if (plchan->at) {
1762 			pl08x_desc_free(&plchan->at->vd);
1763 			plchan->at = NULL;
1764 		}
1765 		/* Dequeue jobs not yet fired as well */
1766 		pl08x_free_txd_list(pl08x, plchan);
1767 		break;
1768 	case DMA_PAUSE:
1769 		pl08x_pause_phy_chan(plchan->phychan);
1770 		plchan->state = PL08X_CHAN_PAUSED;
1771 		break;
1772 	case DMA_RESUME:
1773 		pl08x_resume_phy_chan(plchan->phychan);
1774 		plchan->state = PL08X_CHAN_RUNNING;
1775 		break;
1776 	default:
1777 		/* Unknown command */
1778 		ret = -ENXIO;
1779 		break;
1780 	}
1781 
1782 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1783 
1784 	return ret;
1785 }
1786 
1787 bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1788 {
1789 	struct pl08x_dma_chan *plchan;
1790 	char *name = chan_id;
1791 
1792 	/* Reject channels for devices not bound to this driver */
1793 	if (chan->device->dev->driver != &pl08x_amba_driver.drv)
1794 		return false;
1795 
1796 	plchan = to_pl08x_chan(chan);
1797 
1798 	/* Check that the channel is not taken! */
1799 	if (!strcmp(plchan->name, name))
1800 		return true;
1801 
1802 	return false;
1803 }
1804 
1805 /*
1806  * Just check that the device is there and active
1807  * TODO: turn this bit on/off depending on the number of physical channels
1808  * actually used, if it is zero... well shut it off. That will save some
1809  * power. Cut the clock at the same time.
1810  */
1811 static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1812 {
1813 	/* The Nomadik variant does not have the config register */
1814 	if (pl08x->vd->nomadik)
1815 		return;
1816 	writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
1817 }
1818 
1819 static irqreturn_t pl08x_irq(int irq, void *dev)
1820 {
1821 	struct pl08x_driver_data *pl08x = dev;
1822 	u32 mask = 0, err, tc, i;
1823 
1824 	/* check & clear - ERR & TC interrupts */
1825 	err = readl(pl08x->base + PL080_ERR_STATUS);
1826 	if (err) {
1827 		dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
1828 			__func__, err);
1829 		writel(err, pl08x->base + PL080_ERR_CLEAR);
1830 	}
1831 	tc = readl(pl08x->base + PL080_TC_STATUS);
1832 	if (tc)
1833 		writel(tc, pl08x->base + PL080_TC_CLEAR);
1834 
1835 	if (!err && !tc)
1836 		return IRQ_NONE;
1837 
1838 	for (i = 0; i < pl08x->vd->channels; i++) {
1839 		if (((1 << i) & err) || ((1 << i) & tc)) {
1840 			/* Locate physical channel */
1841 			struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1842 			struct pl08x_dma_chan *plchan = phychan->serving;
1843 			struct pl08x_txd *tx;
1844 
1845 			if (!plchan) {
1846 				dev_err(&pl08x->adev->dev,
1847 					"%s Error TC interrupt on unused channel: 0x%08x\n",
1848 					__func__, i);
1849 				continue;
1850 			}
1851 
1852 			spin_lock(&plchan->vc.lock);
1853 			tx = plchan->at;
1854 			if (tx && tx->cyclic) {
1855 				vchan_cyclic_callback(&tx->vd);
1856 			} else if (tx) {
1857 				plchan->at = NULL;
1858 				/*
1859 				 * This descriptor is done, release its mux
1860 				 * reservation.
1861 				 */
1862 				pl08x_release_mux(plchan);
1863 				tx->done = true;
1864 				vchan_cookie_complete(&tx->vd);
1865 
1866 				/*
1867 				 * And start the next descriptor (if any),
1868 				 * otherwise free this channel.
1869 				 */
1870 				if (vchan_next_desc(&plchan->vc))
1871 					pl08x_start_next_txd(plchan);
1872 				else
1873 					pl08x_phy_free(plchan);
1874 			}
1875 			spin_unlock(&plchan->vc.lock);
1876 
1877 			mask |= (1 << i);
1878 		}
1879 	}
1880 
1881 	return mask ? IRQ_HANDLED : IRQ_NONE;
1882 }
1883 
1884 static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
1885 {
1886 	chan->slave = true;
1887 	chan->name = chan->cd->bus_id;
1888 	chan->cfg.src_addr = chan->cd->addr;
1889 	chan->cfg.dst_addr = chan->cd->addr;
1890 }
1891 
1892 /*
1893  * Initialise the DMAC memcpy/slave channels.
1894  * Make a local wrapper to hold required data
1895  */
1896 static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1897 		struct dma_device *dmadev, unsigned int channels, bool slave)
1898 {
1899 	struct pl08x_dma_chan *chan;
1900 	int i;
1901 
1902 	INIT_LIST_HEAD(&dmadev->channels);
1903 
1904 	/*
1905 	 * Register as many many memcpy as we have physical channels,
1906 	 * we won't always be able to use all but the code will have
1907 	 * to cope with that situation.
1908 	 */
1909 	for (i = 0; i < channels; i++) {
1910 		chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1911 		if (!chan) {
1912 			dev_err(&pl08x->adev->dev,
1913 				"%s no memory for channel\n", __func__);
1914 			return -ENOMEM;
1915 		}
1916 
1917 		chan->host = pl08x;
1918 		chan->state = PL08X_CHAN_IDLE;
1919 		chan->signal = -1;
1920 
1921 		if (slave) {
1922 			chan->cd = &pl08x->pd->slave_channels[i];
1923 			pl08x_dma_slave_init(chan);
1924 		} else {
1925 			chan->cd = &pl08x->pd->memcpy_channel;
1926 			chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1927 			if (!chan->name) {
1928 				kfree(chan);
1929 				return -ENOMEM;
1930 			}
1931 		}
1932 		dev_dbg(&pl08x->adev->dev,
1933 			 "initialize virtual channel \"%s\"\n",
1934 			 chan->name);
1935 
1936 		chan->vc.desc_free = pl08x_desc_free;
1937 		vchan_init(&chan->vc, dmadev);
1938 	}
1939 	dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1940 		 i, slave ? "slave" : "memcpy");
1941 	return i;
1942 }
1943 
1944 static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1945 {
1946 	struct pl08x_dma_chan *chan = NULL;
1947 	struct pl08x_dma_chan *next;
1948 
1949 	list_for_each_entry_safe(chan,
1950 				 next, &dmadev->channels, vc.chan.device_node) {
1951 		list_del(&chan->vc.chan.device_node);
1952 		kfree(chan);
1953 	}
1954 }
1955 
1956 #ifdef CONFIG_DEBUG_FS
1957 static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1958 {
1959 	switch (state) {
1960 	case PL08X_CHAN_IDLE:
1961 		return "idle";
1962 	case PL08X_CHAN_RUNNING:
1963 		return "running";
1964 	case PL08X_CHAN_PAUSED:
1965 		return "paused";
1966 	case PL08X_CHAN_WAITING:
1967 		return "waiting";
1968 	default:
1969 		break;
1970 	}
1971 	return "UNKNOWN STATE";
1972 }
1973 
1974 static int pl08x_debugfs_show(struct seq_file *s, void *data)
1975 {
1976 	struct pl08x_driver_data *pl08x = s->private;
1977 	struct pl08x_dma_chan *chan;
1978 	struct pl08x_phy_chan *ch;
1979 	unsigned long flags;
1980 	int i;
1981 
1982 	seq_printf(s, "PL08x physical channels:\n");
1983 	seq_printf(s, "CHANNEL:\tUSER:\n");
1984 	seq_printf(s, "--------\t-----\n");
1985 	for (i = 0; i < pl08x->vd->channels; i++) {
1986 		struct pl08x_dma_chan *virt_chan;
1987 
1988 		ch = &pl08x->phy_chans[i];
1989 
1990 		spin_lock_irqsave(&ch->lock, flags);
1991 		virt_chan = ch->serving;
1992 
1993 		seq_printf(s, "%d\t\t%s%s\n",
1994 			   ch->id,
1995 			   virt_chan ? virt_chan->name : "(none)",
1996 			   ch->locked ? " LOCKED" : "");
1997 
1998 		spin_unlock_irqrestore(&ch->lock, flags);
1999 	}
2000 
2001 	seq_printf(s, "\nPL08x virtual memcpy channels:\n");
2002 	seq_printf(s, "CHANNEL:\tSTATE:\n");
2003 	seq_printf(s, "--------\t------\n");
2004 	list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
2005 		seq_printf(s, "%s\t\t%s\n", chan->name,
2006 			   pl08x_state_str(chan->state));
2007 	}
2008 
2009 	seq_printf(s, "\nPL08x virtual slave channels:\n");
2010 	seq_printf(s, "CHANNEL:\tSTATE:\n");
2011 	seq_printf(s, "--------\t------\n");
2012 	list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
2013 		seq_printf(s, "%s\t\t%s\n", chan->name,
2014 			   pl08x_state_str(chan->state));
2015 	}
2016 
2017 	return 0;
2018 }
2019 
2020 static int pl08x_debugfs_open(struct inode *inode, struct file *file)
2021 {
2022 	return single_open(file, pl08x_debugfs_show, inode->i_private);
2023 }
2024 
2025 static const struct file_operations pl08x_debugfs_operations = {
2026 	.open		= pl08x_debugfs_open,
2027 	.read		= seq_read,
2028 	.llseek		= seq_lseek,
2029 	.release	= single_release,
2030 };
2031 
2032 static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2033 {
2034 	/* Expose a simple debugfs interface to view all clocks */
2035 	(void) debugfs_create_file(dev_name(&pl08x->adev->dev),
2036 			S_IFREG | S_IRUGO, NULL, pl08x,
2037 			&pl08x_debugfs_operations);
2038 }
2039 
2040 #else
2041 static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2042 {
2043 }
2044 #endif
2045 
2046 static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
2047 {
2048 	struct pl08x_driver_data *pl08x;
2049 	const struct vendor_data *vd = id->data;
2050 	u32 tsfr_size;
2051 	int ret = 0;
2052 	int i;
2053 
2054 	ret = amba_request_regions(adev, NULL);
2055 	if (ret)
2056 		return ret;
2057 
2058 	/* Create the driver state holder */
2059 	pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
2060 	if (!pl08x) {
2061 		ret = -ENOMEM;
2062 		goto out_no_pl08x;
2063 	}
2064 
2065 	/* Initialize memcpy engine */
2066 	dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
2067 	pl08x->memcpy.dev = &adev->dev;
2068 	pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
2069 	pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
2070 	pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
2071 	pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
2072 	pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
2073 	pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
2074 	pl08x->memcpy.device_control = pl08x_control;
2075 
2076 	/* Initialize slave engine */
2077 	dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
2078 	dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
2079 	pl08x->slave.dev = &adev->dev;
2080 	pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
2081 	pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
2082 	pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
2083 	pl08x->slave.device_tx_status = pl08x_dma_tx_status;
2084 	pl08x->slave.device_issue_pending = pl08x_issue_pending;
2085 	pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
2086 	pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
2087 	pl08x->slave.device_control = pl08x_control;
2088 
2089 	/* Get the platform data */
2090 	pl08x->pd = dev_get_platdata(&adev->dev);
2091 	if (!pl08x->pd) {
2092 		dev_err(&adev->dev, "no platform data supplied\n");
2093 		ret = -EINVAL;
2094 		goto out_no_platdata;
2095 	}
2096 
2097 	/* Assign useful pointers to the driver state */
2098 	pl08x->adev = adev;
2099 	pl08x->vd = vd;
2100 
2101 	/* By default, AHB1 only.  If dualmaster, from platform */
2102 	pl08x->lli_buses = PL08X_AHB1;
2103 	pl08x->mem_buses = PL08X_AHB1;
2104 	if (pl08x->vd->dualmaster) {
2105 		pl08x->lli_buses = pl08x->pd->lli_buses;
2106 		pl08x->mem_buses = pl08x->pd->mem_buses;
2107 	}
2108 
2109 	if (vd->pl080s)
2110 		pl08x->lli_words = PL080S_LLI_WORDS;
2111 	else
2112 		pl08x->lli_words = PL080_LLI_WORDS;
2113 	tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
2114 
2115 	/* A DMA memory pool for LLIs, align on 1-byte boundary */
2116 	pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
2117 						tsfr_size, PL08X_ALIGN, 0);
2118 	if (!pl08x->pool) {
2119 		ret = -ENOMEM;
2120 		goto out_no_lli_pool;
2121 	}
2122 
2123 	pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
2124 	if (!pl08x->base) {
2125 		ret = -ENOMEM;
2126 		goto out_no_ioremap;
2127 	}
2128 
2129 	/* Turn on the PL08x */
2130 	pl08x_ensure_on(pl08x);
2131 
2132 	/* Attach the interrupt handler */
2133 	writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
2134 	writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
2135 
2136 	ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
2137 			  DRIVER_NAME, pl08x);
2138 	if (ret) {
2139 		dev_err(&adev->dev, "%s failed to request interrupt %d\n",
2140 			__func__, adev->irq[0]);
2141 		goto out_no_irq;
2142 	}
2143 
2144 	/* Initialize physical channels */
2145 	pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
2146 			GFP_KERNEL);
2147 	if (!pl08x->phy_chans) {
2148 		dev_err(&adev->dev, "%s failed to allocate "
2149 			"physical channel holders\n",
2150 			__func__);
2151 		ret = -ENOMEM;
2152 		goto out_no_phychans;
2153 	}
2154 
2155 	for (i = 0; i < vd->channels; i++) {
2156 		struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
2157 
2158 		ch->id = i;
2159 		ch->base = pl08x->base + PL080_Cx_BASE(i);
2160 		ch->reg_config = ch->base + vd->config_offset;
2161 		spin_lock_init(&ch->lock);
2162 
2163 		/*
2164 		 * Nomadik variants can have channels that are locked
2165 		 * down for the secure world only. Lock up these channels
2166 		 * by perpetually serving a dummy virtual channel.
2167 		 */
2168 		if (vd->nomadik) {
2169 			u32 val;
2170 
2171 			val = readl(ch->reg_config);
2172 			if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
2173 				dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
2174 				ch->locked = true;
2175 			}
2176 		}
2177 
2178 		dev_dbg(&adev->dev, "physical channel %d is %s\n",
2179 			i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
2180 	}
2181 
2182 	/* Register as many memcpy channels as there are physical channels */
2183 	ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
2184 					      pl08x->vd->channels, false);
2185 	if (ret <= 0) {
2186 		dev_warn(&pl08x->adev->dev,
2187 			 "%s failed to enumerate memcpy channels - %d\n",
2188 			 __func__, ret);
2189 		goto out_no_memcpy;
2190 	}
2191 	pl08x->memcpy.chancnt = ret;
2192 
2193 	/* Register slave channels */
2194 	ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
2195 			pl08x->pd->num_slave_channels, true);
2196 	if (ret <= 0) {
2197 		dev_warn(&pl08x->adev->dev,
2198 			"%s failed to enumerate slave channels - %d\n",
2199 				__func__, ret);
2200 		goto out_no_slave;
2201 	}
2202 	pl08x->slave.chancnt = ret;
2203 
2204 	ret = dma_async_device_register(&pl08x->memcpy);
2205 	if (ret) {
2206 		dev_warn(&pl08x->adev->dev,
2207 			"%s failed to register memcpy as an async device - %d\n",
2208 			__func__, ret);
2209 		goto out_no_memcpy_reg;
2210 	}
2211 
2212 	ret = dma_async_device_register(&pl08x->slave);
2213 	if (ret) {
2214 		dev_warn(&pl08x->adev->dev,
2215 			"%s failed to register slave as an async device - %d\n",
2216 			__func__, ret);
2217 		goto out_no_slave_reg;
2218 	}
2219 
2220 	amba_set_drvdata(adev, pl08x);
2221 	init_pl08x_debugfs(pl08x);
2222 	dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
2223 		 amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
2224 		 (unsigned long long)adev->res.start, adev->irq[0]);
2225 
2226 	return 0;
2227 
2228 out_no_slave_reg:
2229 	dma_async_device_unregister(&pl08x->memcpy);
2230 out_no_memcpy_reg:
2231 	pl08x_free_virtual_channels(&pl08x->slave);
2232 out_no_slave:
2233 	pl08x_free_virtual_channels(&pl08x->memcpy);
2234 out_no_memcpy:
2235 	kfree(pl08x->phy_chans);
2236 out_no_phychans:
2237 	free_irq(adev->irq[0], pl08x);
2238 out_no_irq:
2239 	iounmap(pl08x->base);
2240 out_no_ioremap:
2241 	dma_pool_destroy(pl08x->pool);
2242 out_no_lli_pool:
2243 out_no_platdata:
2244 	kfree(pl08x);
2245 out_no_pl08x:
2246 	amba_release_regions(adev);
2247 	return ret;
2248 }
2249 
2250 /* PL080 has 8 channels and the PL080 have just 2 */
2251 static struct vendor_data vendor_pl080 = {
2252 	.config_offset = PL080_CH_CONFIG,
2253 	.channels = 8,
2254 	.dualmaster = true,
2255 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2256 };
2257 
2258 static struct vendor_data vendor_nomadik = {
2259 	.config_offset = PL080_CH_CONFIG,
2260 	.channels = 8,
2261 	.dualmaster = true,
2262 	.nomadik = true,
2263 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2264 };
2265 
2266 static struct vendor_data vendor_pl080s = {
2267 	.config_offset = PL080S_CH_CONFIG,
2268 	.channels = 8,
2269 	.pl080s = true,
2270 	.max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
2271 };
2272 
2273 static struct vendor_data vendor_pl081 = {
2274 	.config_offset = PL080_CH_CONFIG,
2275 	.channels = 2,
2276 	.dualmaster = false,
2277 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2278 };
2279 
2280 static struct amba_id pl08x_ids[] = {
2281 	/* Samsung PL080S variant */
2282 	{
2283 		.id	= 0x0a141080,
2284 		.mask	= 0xffffffff,
2285 		.data	= &vendor_pl080s,
2286 	},
2287 	/* PL080 */
2288 	{
2289 		.id	= 0x00041080,
2290 		.mask	= 0x000fffff,
2291 		.data	= &vendor_pl080,
2292 	},
2293 	/* PL081 */
2294 	{
2295 		.id	= 0x00041081,
2296 		.mask	= 0x000fffff,
2297 		.data	= &vendor_pl081,
2298 	},
2299 	/* Nomadik 8815 PL080 variant */
2300 	{
2301 		.id	= 0x00280080,
2302 		.mask	= 0x00ffffff,
2303 		.data	= &vendor_nomadik,
2304 	},
2305 	{ 0, 0 },
2306 };
2307 
2308 MODULE_DEVICE_TABLE(amba, pl08x_ids);
2309 
2310 static struct amba_driver pl08x_amba_driver = {
2311 	.drv.name	= DRIVER_NAME,
2312 	.id_table	= pl08x_ids,
2313 	.probe		= pl08x_probe,
2314 };
2315 
2316 static int __init pl08x_init(void)
2317 {
2318 	int retval;
2319 	retval = amba_driver_register(&pl08x_amba_driver);
2320 	if (retval)
2321 		printk(KERN_WARNING DRIVER_NAME
2322 		       "failed to register as an AMBA device (%d)\n",
2323 		       retval);
2324 	return retval;
2325 }
2326 subsys_initcall(pl08x_init);
2327