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