]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/dma/amba-pl08x.c
dmaengine: dw-edma: Fix build error without CONFIG_PCI_MSI
[mirror_ubuntu-jammy-kernel.git] / drivers / dma / amba-pl08x.c
CommitLineData
e8689e63
LW
1/*
2 * Copyright (c) 2006 ARM Ltd.
3 * Copyright (c) 2010 ST-Ericsson SA
1e1cfc72 4 * Copyirght (c) 2017 Linaro Ltd.
e8689e63
LW
5 *
6 * Author: Peter Pearse <peter.pearse@arm.com>
1e1cfc72 7 * Author: Linus Walleij <linus.walleij@linaro.org>
e8689e63
LW
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
94ae8522
RKAL
19 * The full GNU General Public License is in this distribution in the file
20 * called COPYING.
e8689e63
LW
21 *
22 * Documentation: ARM DDI 0196G == PL080
94ae8522 23 * Documentation: ARM DDI 0218E == PL081
da1b6c05 24 * Documentation: S3C6410 User's Manual == PL080S
e8689e63 25 *
94ae8522
RKAL
26 * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
27 * channel.
e8689e63
LW
28 *
29 * The PL080 has 8 channels available for simultaneous use, and the PL081
30 * has only two channels. So on these DMA controllers the number of channels
31 * and the number of incoming DMA signals are two totally different things.
32 * It is usually not possible to theoretically handle all physical signals,
33 * so a multiplexing scheme with possible denial of use is necessary.
34 *
35 * The PL080 has a dual bus master, PL081 has a single master.
36 *
da1b6c05
TF
37 * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
38 * It differs in following aspects:
39 * - CH_CONFIG register at different offset,
40 * - separate CH_CONTROL2 register for transfer size,
41 * - bigger maximum transfer size,
42 * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
43 * - no support for peripheral flow control.
44 *
e8689e63
LW
45 * Memory to peripheral transfer may be visualized as
46 * Get data from memory to DMAC
47 * Until no data left
48 * On burst request from peripheral
49 * Destination burst from DMAC to peripheral
50 * Clear burst request
51 * Raise terminal count interrupt
52 *
53 * For peripherals with a FIFO:
54 * Source burst size == half the depth of the peripheral FIFO
55 * Destination burst size == the depth of the peripheral FIFO
56 *
57 * (Bursts are irrelevant for mem to mem transfers - there are no burst
58 * signals, the DMA controller will simply facilitate its AHB master.)
59 *
60 * ASSUMES default (little) endianness for DMA transfers
61 *
9dc2c200
RKAL
62 * The PL08x has two flow control settings:
63 * - DMAC flow control: the transfer size defines the number of transfers
64 * which occur for the current LLI entry, and the DMAC raises TC at the
65 * end of every LLI entry. Observed behaviour shows the DMAC listening
66 * to both the BREQ and SREQ signals (contrary to documented),
67 * transferring data if either is active. The LBREQ and LSREQ signals
68 * are ignored.
69 *
70 * - Peripheral flow control: the transfer size is ignored (and should be
71 * zero). The data is transferred from the current LLI entry, until
72 * after the final transfer signalled by LBREQ or LSREQ. The DMAC
da1b6c05 73 * will then move to the next LLI entry. Unsupported by PL080S.
e8689e63 74 */
730404ac 75#include <linux/amba/bus.h>
e8689e63
LW
76#include <linux/amba/pl08x.h>
77#include <linux/debugfs.h>
0c38d701
VK
78#include <linux/delay.h>
79#include <linux/device.h>
80#include <linux/dmaengine.h>
81#include <linux/dmapool.h>
8516f52f 82#include <linux/dma-mapping.h>
6d05c9fa 83#include <linux/export.h>
0c38d701
VK
84#include <linux/init.h>
85#include <linux/interrupt.h>
86#include <linux/module.h>
aa4734da
LW
87#include <linux/of.h>
88#include <linux/of_dma.h>
b7b6018b 89#include <linux/pm_runtime.h>
e8689e63 90#include <linux/seq_file.h>
0c38d701 91#include <linux/slab.h>
3a95b9fb 92#include <linux/amba/pl080.h>
e8689e63 93
d2ebfb33 94#include "dmaengine.h"
01d8dc64 95#include "virt-dma.h"
d2ebfb33 96
e8689e63
LW
97#define DRIVER_NAME "pl08xdmac"
98
ea524c7e
MB
99#define PL80X_DMA_BUSWIDTHS \
100 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
101 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
102 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
103 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
104
7703eac9 105static struct amba_driver pl08x_amba_driver;
b23f204c 106struct pl08x_driver_data;
7703eac9 107
e8689e63 108/**
94ae8522 109 * struct vendor_data - vendor-specific config parameters for PL08x derivatives
da7cbd20 110 * @config_offset: offset to the configuration register
e8689e63 111 * @channels: the number of channels available in this variant
f9cd4761 112 * @signals: the number of request signals available from the hardware
94ae8522 113 * @dualmaster: whether this version supports dual AHB masters or not.
1e1cfc72
LW
114 * @nomadik: whether this variant is a ST Microelectronics Nomadik, where the
115 * channels have Nomadik security extension bits that need to be checked
116 * for permission before use and some registers are missing
117 * @pl080s: whether this variant is a Samsung PL080S, which has separate
118 * register and LLI word for transfer size.
119 * @ftdmac020: whether this variant is a Faraday Technology FTDMAC020
f9cd4761
LW
120 * @max_transfer_size: the maximum single element transfer size for this
121 * PL08x variant.
e8689e63
LW
122 */
123struct vendor_data {
d86ccea7 124 u8 config_offset;
e8689e63 125 u8 channels;
f9cd4761 126 u8 signals;
e8689e63 127 bool dualmaster;
affa115e 128 bool nomadik;
da1b6c05 129 bool pl080s;
1e1cfc72 130 bool ftdmac020;
5110e51d 131 u32 max_transfer_size;
e8689e63
LW
132};
133
b23f204c
RK
134/**
135 * struct pl08x_bus_data - information of source or destination
136 * busses for a transfer
137 * @addr: current address
138 * @maxwidth: the maximum width of a transfer on this bus
139 * @buswidth: the width of this bus in bytes: 1, 2 or 4
140 */
141struct pl08x_bus_data {
142 dma_addr_t addr;
143 u8 maxwidth;
144 u8 buswidth;
145};
146
1c38b289
AP
147#define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
148
b23f204c
RK
149/**
150 * struct pl08x_phy_chan - holder for the physical channels
151 * @id: physical index to this channel
da7cbd20
LW
152 * @base: memory base address for this physical channel
153 * @reg_config: configuration address for this physical channel
1e1cfc72
LW
154 * @reg_control: control address for this physical channel
155 * @reg_src: transfer source address register
156 * @reg_dst: transfer destination address register
157 * @reg_lli: transfer LLI address register
158 * @reg_busy: if the variant has a special per-channel busy register,
159 * this contains a pointer to it
b23f204c 160 * @lock: a lock to use when altering an instance of this struct
b23f204c
RK
161 * @serving: the virtual channel currently being served by this physical
162 * channel
ad0de2ac
RK
163 * @locked: channel unavailable for the system, e.g. dedicated to secure
164 * world
1e1cfc72
LW
165 * @ftdmac020: channel is on a FTDMAC020
166 * @pl080s: channel is on a PL08s
b23f204c
RK
167 */
168struct pl08x_phy_chan {
169 unsigned int id;
170 void __iomem *base;
d86ccea7 171 void __iomem *reg_config;
1e1cfc72
LW
172 void __iomem *reg_control;
173 void __iomem *reg_src;
174 void __iomem *reg_dst;
175 void __iomem *reg_lli;
176 void __iomem *reg_busy;
b23f204c 177 spinlock_t lock;
b23f204c 178 struct pl08x_dma_chan *serving;
ad0de2ac 179 bool locked;
1e1cfc72
LW
180 bool ftdmac020;
181 bool pl080s;
b23f204c
RK
182};
183
184/**
185 * struct pl08x_sg - structure containing data per sg
186 * @src_addr: src address of sg
187 * @dst_addr: dst address of sg
188 * @len: transfer len in bytes
189 * @node: node for txd's dsg_list
190 */
191struct pl08x_sg {
192 dma_addr_t src_addr;
193 dma_addr_t dst_addr;
194 size_t len;
195 struct list_head node;
196};
197
198/**
199 * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
01d8dc64 200 * @vd: virtual DMA descriptor
b23f204c 201 * @dsg_list: list of children sg's
b23f204c
RK
202 * @llis_bus: DMA memory address (physical) start for the LLIs
203 * @llis_va: virtual memory address start for the LLIs
204 * @cctl: control reg values for current txd
205 * @ccfg: config reg values for current txd
18536134
RK
206 * @done: this marks completed descriptors, which should not have their
207 * mux released.
3b24c20b 208 * @cyclic: indicate cyclic transfers
b23f204c
RK
209 */
210struct pl08x_txd {
01d8dc64 211 struct virt_dma_desc vd;
b23f204c 212 struct list_head dsg_list;
b23f204c 213 dma_addr_t llis_bus;
ba6785ff 214 u32 *llis_va;
b23f204c
RK
215 /* Default cctl value for LLIs */
216 u32 cctl;
217 /*
218 * Settings to be put into the physical channel when we
219 * trigger this txd. Other registers are in llis_va[0].
220 */
221 u32 ccfg;
18536134 222 bool done;
3b24c20b 223 bool cyclic;
b23f204c
RK
224};
225
226/**
8ee1bdc5 227 * enum pl08x_dma_chan_state - holds the PL08x specific virtual channel
b23f204c
RK
228 * states
229 * @PL08X_CHAN_IDLE: the channel is idle
230 * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
231 * channel and is running a transfer on it
232 * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
233 * channel, but the transfer is currently paused
234 * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
235 * channel to become available (only pertains to memcpy channels)
236 */
237enum pl08x_dma_chan_state {
238 PL08X_CHAN_IDLE,
239 PL08X_CHAN_RUNNING,
240 PL08X_CHAN_PAUSED,
241 PL08X_CHAN_WAITING,
242};
243
244/**
245 * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
01d8dc64 246 * @vc: wrappped virtual channel
b23f204c 247 * @phychan: the physical channel utilized by this channel, if there is one
b23f204c
RK
248 * @name: name of channel
249 * @cd: channel platform data
da7cbd20 250 * @cfg: slave configuration
b23f204c 251 * @at: active transaction on this channel
b23f204c
RK
252 * @host: a pointer to the host (internal use)
253 * @state: whether the channel is idle, paused, running etc
254 * @slave: whether this channel is a device (slave) or for memcpy
ad0de2ac 255 * @signal: the physical DMA request signal which this channel is using
5e2479bd 256 * @mux_use: count of descriptors using this DMA request signal setting
2ff25c1c 257 * @waiting_at: time in jiffies when this channel moved to waiting state
b23f204c
RK
258 */
259struct pl08x_dma_chan {
01d8dc64 260 struct virt_dma_chan vc;
b23f204c 261 struct pl08x_phy_chan *phychan;
550ec36f 262 const char *name;
f9cd4761 263 struct pl08x_channel_data *cd;
ed91c13d 264 struct dma_slave_config cfg;
b23f204c 265 struct pl08x_txd *at;
b23f204c
RK
266 struct pl08x_driver_data *host;
267 enum pl08x_dma_chan_state state;
268 bool slave;
ad0de2ac 269 int signal;
5e2479bd 270 unsigned mux_use;
2ff25c1c 271 unsigned long waiting_at;
b23f204c
RK
272};
273
e8689e63
LW
274/**
275 * struct pl08x_driver_data - the local state holder for the PL08x
ebe9b300 276 * @slave: optional slave engine for this instance
e8689e63 277 * @memcpy: memcpy engine for this instance
ebe9b300 278 * @has_slave: the PL08x has a slave engine (routed signals)
e8689e63
LW
279 * @base: virtual memory base (remapped) for the PL08x
280 * @adev: the corresponding AMBA (PrimeCell) bus entry
281 * @vd: vendor data for this PL08x variant
282 * @pd: platform data passed in from the platform/machine
283 * @phy_chans: array of data for the physical channels
284 * @pool: a pool for the LLI descriptors
3e27ee84
VK
285 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
286 * fetches
30749cb4 287 * @mem_buses: set to indicate memory transfers on AHB2.
da7cbd20 288 * @lli_words: how many words are used in each LLI item for this variant
e8689e63
LW
289 */
290struct pl08x_driver_data {
291 struct dma_device slave;
292 struct dma_device memcpy;
ebe9b300 293 bool has_slave;
e8689e63
LW
294 void __iomem *base;
295 struct amba_device *adev;
f96ca9ec 296 const struct vendor_data *vd;
e8689e63
LW
297 struct pl08x_platform_data *pd;
298 struct pl08x_phy_chan *phy_chans;
299 struct dma_pool *pool;
30749cb4
RKAL
300 u8 lli_buses;
301 u8 mem_buses;
ba6785ff 302 u8 lli_words;
e8689e63
LW
303};
304
305/*
306 * PL08X specific defines
307 */
308
ba6785ff
TF
309/* The order of words in an LLI. */
310#define PL080_LLI_SRC 0
311#define PL080_LLI_DST 1
312#define PL080_LLI_LLI 2
313#define PL080_LLI_CCTL 3
da1b6c05 314#define PL080S_LLI_CCTL2 4
ba6785ff
TF
315
316/* Total words in an LLI. */
317#define PL080_LLI_WORDS 4
da1b6c05 318#define PL080S_LLI_WORDS 8
e8689e63 319
ba6785ff
TF
320/*
321 * Number of LLIs in each LLI buffer allocated for one transfer
322 * (maximum times we call dma_pool_alloc on this pool without freeing)
323 */
324#define MAX_NUM_TSFR_LLIS 512
e8689e63
LW
325#define PL08X_ALIGN 8
326
327static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
328{
01d8dc64 329 return container_of(chan, struct pl08x_dma_chan, vc.chan);
e8689e63
LW
330}
331
501e67e8
RKAL
332static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
333{
01d8dc64 334 return container_of(tx, struct pl08x_txd, vd.tx);
501e67e8
RKAL
335}
336
6b16c8b1
RK
337/*
338 * Mux handling.
339 *
340 * This gives us the DMA request input to the PL08x primecell which the
341 * peripheral described by the channel data will be routed to, possibly
342 * via a board/SoC specific external MUX. One important point to note
343 * here is that this does not depend on the physical channel.
344 */
ad0de2ac 345static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
6b16c8b1
RK
346{
347 const struct pl08x_platform_data *pd = plchan->host->pd;
348 int ret;
349
d7cabeed
MB
350 if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
351 ret = pd->get_xfer_signal(plchan->cd);
5e2479bd
RK
352 if (ret < 0) {
353 plchan->mux_use = 0;
6b16c8b1 354 return ret;
5e2479bd 355 }
6b16c8b1 356
ad0de2ac 357 plchan->signal = ret;
6b16c8b1
RK
358 }
359 return 0;
360}
361
362static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
363{
364 const struct pl08x_platform_data *pd = plchan->host->pd;
365
5e2479bd
RK
366 if (plchan->signal >= 0) {
367 WARN_ON(plchan->mux_use == 0);
368
d7cabeed
MB
369 if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
370 pd->put_xfer_signal(plchan->cd, plchan->signal);
5e2479bd
RK
371 plchan->signal = -1;
372 }
6b16c8b1
RK
373 }
374}
375
e8689e63
LW
376/*
377 * Physical channel handling
378 */
379
380/* Whether a certain channel is busy or not */
381static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
382{
383 unsigned int val;
384
1e1cfc72
LW
385 /* If we have a special busy register, take a shortcut */
386 if (ch->reg_busy) {
387 val = readl(ch->reg_busy);
388 return !!(val & BIT(ch->id));
389 }
d86ccea7 390 val = readl(ch->reg_config);
e8689e63
LW
391 return val & PL080_CONFIG_ACTIVE;
392}
393
1e1cfc72
LW
394/*
395 * pl08x_write_lli() - Write an LLI into the DMA controller.
396 *
397 * The PL08x derivatives support linked lists, but the first item of the
398 * list containing the source, destination, control word and next LLI is
399 * ignored. Instead the driver has to write those values directly into the
400 * SRC, DST, LLI and control registers. On FTDMAC020 also the SIZE
401 * register need to be set up for the first transfer.
402 */
ba6785ff
TF
403static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
404 struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
405{
da1b6c05
TF
406 if (pl08x->vd->pl080s)
407 dev_vdbg(&pl08x->adev->dev,
408 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
409 "clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
410 phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
411 lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
412 lli[PL080S_LLI_CCTL2], ccfg);
413 else
414 dev_vdbg(&pl08x->adev->dev,
415 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
416 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
417 phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
418 lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
ba6785ff 419
1e1cfc72
LW
420 writel_relaxed(lli[PL080_LLI_SRC], phychan->reg_src);
421 writel_relaxed(lli[PL080_LLI_DST], phychan->reg_dst);
422 writel_relaxed(lli[PL080_LLI_LLI], phychan->reg_lli);
ba6785ff 423
1e1cfc72
LW
424 /*
425 * The FTMAC020 has a different layout in the CCTL word of the LLI
426 * and the CCTL register which is split in CSR and SIZE registers.
427 * Convert the LLI item CCTL into the proper values to write into
428 * the CSR and SIZE registers.
429 */
430 if (phychan->ftdmac020) {
431 u32 llictl = lli[PL080_LLI_CCTL];
432 u32 val = 0;
433
434 /* Write the transfer size (12 bits) to the size register */
435 writel_relaxed(llictl & FTDMAC020_LLI_TRANSFER_SIZE_MASK,
436 phychan->base + FTDMAC020_CH_SIZE);
437 /*
438 * Then write the control bits 28..16 to the control register
439 * by shuffleing the bits around to where they are in the
440 * main register. The mapping is as follows:
441 * Bit 28: TC_MSK - mask on all except last LLI
442 * Bit 27..25: SRC_WIDTH
443 * Bit 24..22: DST_WIDTH
444 * Bit 21..20: SRCAD_CTRL
445 * Bit 19..17: DSTAD_CTRL
446 * Bit 17: SRC_SEL
447 * Bit 16: DST_SEL
448 */
449 if (llictl & FTDMAC020_LLI_TC_MSK)
450 val |= FTDMAC020_CH_CSR_TC_MSK;
451 val |= ((llictl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
452 (FTDMAC020_LLI_SRC_WIDTH_SHIFT -
453 FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT));
454 val |= ((llictl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
455 (FTDMAC020_LLI_DST_WIDTH_SHIFT -
456 FTDMAC020_CH_CSR_DST_WIDTH_SHIFT));
457 val |= ((llictl & FTDMAC020_LLI_SRCAD_CTL_MSK) >>
458 (FTDMAC020_LLI_SRCAD_CTL_SHIFT -
459 FTDMAC020_CH_CSR_SRCAD_CTL_SHIFT));
460 val |= ((llictl & FTDMAC020_LLI_DSTAD_CTL_MSK) >>
461 (FTDMAC020_LLI_DSTAD_CTL_SHIFT -
462 FTDMAC020_CH_CSR_DSTAD_CTL_SHIFT));
463 if (llictl & FTDMAC020_LLI_SRC_SEL)
464 val |= FTDMAC020_CH_CSR_SRC_SEL;
465 if (llictl & FTDMAC020_LLI_DST_SEL)
466 val |= FTDMAC020_CH_CSR_DST_SEL;
467
468 /*
469 * Set up the bits that exist in the CSR but are not
470 * part the LLI, i.e. only gets written to the control
471 * register right here.
472 *
473 * FIXME: do not just handle memcpy, also handle slave DMA.
474 */
475 switch (pl08x->pd->memcpy_burst_size) {
476 default:
477 case PL08X_BURST_SZ_1:
478 val |= PL080_BSIZE_1 <<
479 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
480 break;
481 case PL08X_BURST_SZ_4:
482 val |= PL080_BSIZE_4 <<
483 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
484 break;
485 case PL08X_BURST_SZ_8:
486 val |= PL080_BSIZE_8 <<
487 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
488 break;
489 case PL08X_BURST_SZ_16:
490 val |= PL080_BSIZE_16 <<
491 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
492 break;
493 case PL08X_BURST_SZ_32:
494 val |= PL080_BSIZE_32 <<
495 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
496 break;
497 case PL08X_BURST_SZ_64:
498 val |= PL080_BSIZE_64 <<
499 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
500 break;
501 case PL08X_BURST_SZ_128:
502 val |= PL080_BSIZE_128 <<
503 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
504 break;
505 case PL08X_BURST_SZ_256:
506 val |= PL080_BSIZE_256 <<
507 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
508 break;
509 }
510
511 /* Protection flags */
512 if (pl08x->pd->memcpy_prot_buff)
513 val |= FTDMAC020_CH_CSR_PROT2;
514 if (pl08x->pd->memcpy_prot_cache)
515 val |= FTDMAC020_CH_CSR_PROT3;
516 /* We are the kernel, so we are in privileged mode */
517 val |= FTDMAC020_CH_CSR_PROT1;
518
519 writel_relaxed(val, phychan->reg_control);
520 } else {
521 /* Bits are just identical */
522 writel_relaxed(lli[PL080_LLI_CCTL], phychan->reg_control);
523 }
524
525 /* Second control word on the PL080s */
da1b6c05
TF
526 if (pl08x->vd->pl080s)
527 writel_relaxed(lli[PL080S_LLI_CCTL2],
528 phychan->base + PL080S_CH_CONTROL2);
529
ba6785ff
TF
530 writel(ccfg, phychan->reg_config);
531}
532
e8689e63
LW
533/*
534 * Set the initial DMA register values i.e. those for the first LLI
e8b5e11d 535 * The next LLI pointer and the configuration interrupt bit have
c885bee4
RKAL
536 * been set when the LLIs were constructed. Poke them into the hardware
537 * and start the transfer.
e8689e63 538 */
eab82533 539static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
e8689e63 540{
c885bee4 541 struct pl08x_driver_data *pl08x = plchan->host;
e8689e63 542 struct pl08x_phy_chan *phychan = plchan->phychan;
879f127b
RK
543 struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
544 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
09b3c323 545 u32 val;
c885bee4 546
879f127b 547 list_del(&txd->vd.node);
eab82533 548
c885bee4 549 plchan->at = txd;
e8689e63 550
c885bee4
RKAL
551 /* Wait for channel inactive */
552 while (pl08x_phy_channel_busy(phychan))
553 cpu_relax();
e8689e63 554
ba6785ff 555 pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
c885bee4
RKAL
556
557 /* Enable the DMA channel */
558 /* Do not access config register until channel shows as disabled */
ded091fe 559 while (readl(pl08x->base + PL080_EN_CHAN) & BIT(phychan->id))
19386b32 560 cpu_relax();
e8689e63 561
c885bee4 562 /* Do not access config register until channel shows as inactive */
1e1cfc72
LW
563 if (phychan->ftdmac020) {
564 val = readl(phychan->reg_config);
565 while (val & FTDMAC020_CH_CFG_BUSY)
566 val = readl(phychan->reg_config);
567
568 val = readl(phychan->reg_control);
569 while (val & FTDMAC020_CH_CSR_EN)
570 val = readl(phychan->reg_control);
571
572 writel(val | FTDMAC020_CH_CSR_EN,
573 phychan->reg_control);
574 } else {
d86ccea7 575 val = readl(phychan->reg_config);
1e1cfc72
LW
576 while ((val & PL080_CONFIG_ACTIVE) ||
577 (val & PL080_CONFIG_ENABLE))
578 val = readl(phychan->reg_config);
e8689e63 579
1e1cfc72
LW
580 writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
581 }
e8689e63
LW
582}
583
584/*
81796616 585 * Pause the channel by setting the HALT bit.
e8689e63 586 *
81796616
RKAL
587 * For M->P transfers, pause the DMAC first and then stop the peripheral -
588 * the FIFO can only drain if the peripheral is still requesting data.
589 * (note: this can still timeout if the DMAC FIFO never drains of data.)
e8689e63 590 *
81796616
RKAL
591 * For P->M transfers, disable the peripheral first to stop it filling
592 * the DMAC FIFO, and then pause the DMAC.
e8689e63
LW
593 */
594static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
595{
596 u32 val;
81796616 597 int timeout;
e8689e63 598
1e1cfc72
LW
599 if (ch->ftdmac020) {
600 /* Use the enable bit on the FTDMAC020 */
601 val = readl(ch->reg_control);
602 val &= ~FTDMAC020_CH_CSR_EN;
603 writel(val, ch->reg_control);
604 return;
605 }
606
e8689e63 607 /* Set the HALT bit and wait for the FIFO to drain */
d86ccea7 608 val = readl(ch->reg_config);
e8689e63 609 val |= PL080_CONFIG_HALT;
d86ccea7 610 writel(val, ch->reg_config);
e8689e63
LW
611
612 /* Wait for channel inactive */
81796616
RKAL
613 for (timeout = 1000; timeout; timeout--) {
614 if (!pl08x_phy_channel_busy(ch))
615 break;
616 udelay(1);
617 }
618 if (pl08x_phy_channel_busy(ch))
619 pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
e8689e63
LW
620}
621
622static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
623{
624 u32 val;
625
1e1cfc72
LW
626 /* Use the enable bit on the FTDMAC020 */
627 if (ch->ftdmac020) {
628 val = readl(ch->reg_control);
629 val |= FTDMAC020_CH_CSR_EN;
630 writel(val, ch->reg_control);
631 return;
632 }
633
e8689e63 634 /* Clear the HALT bit */
d86ccea7 635 val = readl(ch->reg_config);
e8689e63 636 val &= ~PL080_CONFIG_HALT;
d86ccea7 637 writel(val, ch->reg_config);
e8689e63
LW
638}
639
fb526210
RKAL
640/*
641 * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
642 * clears any pending interrupt status. This should not be used for
643 * an on-going transfer, but as a method of shutting down a channel
644 * (eg, when it's no longer used) or terminating a transfer.
645 */
646static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
647 struct pl08x_phy_chan *ch)
e8689e63 648{
1e1cfc72
LW
649 u32 val;
650
651 /* The layout for the FTDMAC020 is different */
652 if (ch->ftdmac020) {
653 /* Disable all interrupts */
654 val = readl(ch->reg_config);
655 val |= (FTDMAC020_CH_CFG_INT_ABT_MASK |
656 FTDMAC020_CH_CFG_INT_ERR_MASK |
657 FTDMAC020_CH_CFG_INT_TC_MASK);
658 writel(val, ch->reg_config);
659
660 /* Abort and disable channel */
661 val = readl(ch->reg_control);
662 val &= ~FTDMAC020_CH_CSR_EN;
663 val |= FTDMAC020_CH_CSR_ABT;
664 writel(val, ch->reg_control);
665
666 /* Clear ABT and ERR interrupt flags */
667 writel(BIT(ch->id) | BIT(ch->id + 16),
668 pl08x->base + PL080_ERR_CLEAR);
669 writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
e8689e63 670
1e1cfc72
LW
671 return;
672 }
673
674 val = readl(ch->reg_config);
fb526210 675 val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
5835aa86 676 PL080_CONFIG_TC_IRQ_MASK);
d86ccea7 677 writel(val, ch->reg_config);
fb526210 678
ded091fe
LW
679 writel(BIT(ch->id), pl08x->base + PL080_ERR_CLEAR);
680 writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
e8689e63
LW
681}
682
1e1cfc72 683static u32 get_bytes_in_phy_channel(struct pl08x_phy_chan *ch)
e8689e63 684{
1e1cfc72
LW
685 u32 val;
686 u32 bytes;
687
688 if (ch->ftdmac020) {
689 bytes = readl(ch->base + FTDMAC020_CH_SIZE);
690
691 val = readl(ch->reg_control);
692 val &= FTDMAC020_CH_CSR_SRC_WIDTH_MSK;
693 val >>= FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT;
694 } else if (ch->pl080s) {
695 val = readl(ch->base + PL080S_CH_CONTROL2);
696 bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
e8689e63 697
1e1cfc72
LW
698 val = readl(ch->reg_control);
699 val &= PL080_CONTROL_SWIDTH_MASK;
700 val >>= PL080_CONTROL_SWIDTH_SHIFT;
701 } else {
702 /* Plain PL08x */
703 val = readl(ch->reg_control);
704 bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
705
706 val &= PL080_CONTROL_SWIDTH_MASK;
707 val >>= PL080_CONTROL_SWIDTH_SHIFT;
708 }
f3287a52 709
1e1cfc72 710 switch (val) {
e8689e63
LW
711 case PL080_WIDTH_8BIT:
712 break;
713 case PL080_WIDTH_16BIT:
714 bytes *= 2;
715 break;
716 case PL080_WIDTH_32BIT:
717 bytes *= 4;
718 break;
719 }
720 return bytes;
721}
722
1e1cfc72 723static u32 get_bytes_in_lli(struct pl08x_phy_chan *ch, const u32 *llis_va)
da1b6c05 724{
1e1cfc72
LW
725 u32 val;
726 u32 bytes;
727
728 if (ch->ftdmac020) {
729 val = llis_va[PL080_LLI_CCTL];
730 bytes = val & FTDMAC020_LLI_TRANSFER_SIZE_MASK;
731
732 val = llis_va[PL080_LLI_CCTL];
733 val &= FTDMAC020_LLI_SRC_WIDTH_MSK;
734 val >>= FTDMAC020_LLI_SRC_WIDTH_SHIFT;
735 } else if (ch->pl080s) {
736 val = llis_va[PL080S_LLI_CCTL2];
737 bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
738
739 val = llis_va[PL080_LLI_CCTL];
740 val &= PL080_CONTROL_SWIDTH_MASK;
741 val >>= PL080_CONTROL_SWIDTH_SHIFT;
742 } else {
743 /* Plain PL08x */
744 val = llis_va[PL080_LLI_CCTL];
745 bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
da1b6c05 746
1e1cfc72
LW
747 val &= PL080_CONTROL_SWIDTH_MASK;
748 val >>= PL080_CONTROL_SWIDTH_SHIFT;
749 }
f3287a52 750
1e1cfc72 751 switch (val) {
e8689e63
LW
752 case PL080_WIDTH_8BIT:
753 break;
754 case PL080_WIDTH_16BIT:
755 bytes *= 2;
756 break;
757 case PL080_WIDTH_32BIT:
758 bytes *= 4;
759 break;
760 }
761 return bytes;
762}
763
764/* The channel should be paused when calling this */
765static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
766{
ba6785ff
TF
767 struct pl08x_driver_data *pl08x = plchan->host;
768 const u32 *llis_va, *llis_va_limit;
e8689e63 769 struct pl08x_phy_chan *ch;
68a7faa2 770 dma_addr_t llis_bus;
e8689e63 771 struct pl08x_txd *txd;
ba6785ff 772 u32 llis_max_words;
68a7faa2 773 size_t bytes;
68a7faa2 774 u32 clli;
e8689e63 775
e8689e63
LW
776 ch = plchan->phychan;
777 txd = plchan->at;
778
68a7faa2
TF
779 if (!ch || !txd)
780 return 0;
781
e8689e63 782 /*
db9f136a
RKAL
783 * Follow the LLIs to get the number of remaining
784 * bytes in the currently active transaction.
e8689e63 785 */
1e1cfc72 786 clli = readl(ch->reg_lli) & ~PL080_LLI_LM_AHB2;
e8689e63 787
68a7faa2 788 /* First get the remaining bytes in the active transfer */
1e1cfc72 789 bytes = get_bytes_in_phy_channel(ch);
e8689e63 790
68a7faa2
TF
791 if (!clli)
792 return bytes;
db9f136a 793
68a7faa2
TF
794 llis_va = txd->llis_va;
795 llis_bus = txd->llis_bus;
e8689e63 796
ba6785ff 797 llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
68a7faa2 798 BUG_ON(clli < llis_bus || clli >= llis_bus +
ba6785ff 799 sizeof(u32) * llis_max_words);
db9f136a 800
68a7faa2
TF
801 /*
802 * Locate the next LLI - as this is an array,
803 * it's simple maths to find.
804 */
ba6785ff 805 llis_va += (clli - llis_bus) / sizeof(u32);
e8689e63 806
ba6785ff
TF
807 llis_va_limit = llis_va + llis_max_words;
808
809 for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
1e1cfc72 810 bytes += get_bytes_in_lli(ch, llis_va);
68a7faa2
TF
811
812 /*
3b24c20b 813 * A LLI pointer going backward terminates the LLI list
68a7faa2 814 */
3b24c20b 815 if (llis_va[PL080_LLI_LLI] <= clli)
68a7faa2 816 break;
e8689e63
LW
817 }
818
e8689e63
LW
819 return bytes;
820}
821
822/*
823 * Allocate a physical channel for a virtual channel
94ae8522
RKAL
824 *
825 * Try to locate a physical channel to be used for this transfer. If all
826 * are taken return NULL and the requester will have to cope by using
827 * some fallback PIO mode or retrying later.
e8689e63
LW
828 */
829static struct pl08x_phy_chan *
830pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
831 struct pl08x_dma_chan *virt_chan)
832{
833 struct pl08x_phy_chan *ch = NULL;
834 unsigned long flags;
835 int i;
836
e8689e63
LW
837 for (i = 0; i < pl08x->vd->channels; i++) {
838 ch = &pl08x->phy_chans[i];
839
840 spin_lock_irqsave(&ch->lock, flags);
841
affa115e 842 if (!ch->locked && !ch->serving) {
e8689e63 843 ch->serving = virt_chan;
e8689e63
LW
844 spin_unlock_irqrestore(&ch->lock, flags);
845 break;
846 }
847
848 spin_unlock_irqrestore(&ch->lock, flags);
849 }
850
851 if (i == pl08x->vd->channels) {
852 /* No physical channel available, cope with it */
853 return NULL;
854 }
855
856 return ch;
857}
858
a5a488db 859/* Mark the physical channel as free. Note, this write is atomic. */
e8689e63
LW
860static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
861 struct pl08x_phy_chan *ch)
862{
a5a488db
RK
863 ch->serving = NULL;
864}
e8689e63 865
a5a488db
RK
866/*
867 * Try to allocate a physical channel. When successful, assign it to
868 * this virtual channel, and initiate the next descriptor. The
869 * virtual channel lock must be held at this point.
870 */
871static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
872{
873 struct pl08x_driver_data *pl08x = plchan->host;
874 struct pl08x_phy_chan *ch;
fb526210 875
a5a488db
RK
876 ch = pl08x_get_phy_channel(pl08x, plchan);
877 if (!ch) {
878 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
879 plchan->state = PL08X_CHAN_WAITING;
2ff25c1c 880 plchan->waiting_at = jiffies;
a5a488db
RK
881 return;
882 }
e8689e63 883
a5a488db
RK
884 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
885 ch->id, plchan->name);
886
887 plchan->phychan = ch;
888 plchan->state = PL08X_CHAN_RUNNING;
889 pl08x_start_next_txd(plchan);
890}
891
892static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
893 struct pl08x_dma_chan *plchan)
894{
895 struct pl08x_driver_data *pl08x = plchan->host;
896
897 dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
898 ch->id, plchan->name);
899
900 /*
901 * We do this without taking the lock; we're really only concerned
902 * about whether this pointer is NULL or not, and we're guaranteed
903 * that this will only be called when it _already_ is non-NULL.
904 */
905 ch->serving = plchan;
906 plchan->phychan = ch;
907 plchan->state = PL08X_CHAN_RUNNING;
908 pl08x_start_next_txd(plchan);
909}
910
911/*
912 * Free a physical DMA channel, potentially reallocating it to another
913 * virtual channel if we have any pending.
914 */
915static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
916{
917 struct pl08x_driver_data *pl08x = plchan->host;
918 struct pl08x_dma_chan *p, *next;
2ff25c1c 919 unsigned long waiting_at;
a5a488db
RK
920 retry:
921 next = NULL;
2ff25c1c 922 waiting_at = jiffies;
a5a488db 923
2ff25c1c
JNG
924 /*
925 * Find a waiting virtual channel for the next transfer.
926 * To be fair, time when each channel reached waiting state is compared
927 * to select channel that is waiting for the longest time.
928 */
01d8dc64 929 list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
2ff25c1c
JNG
930 if (p->state == PL08X_CHAN_WAITING &&
931 p->waiting_at <= waiting_at) {
a5a488db 932 next = p;
2ff25c1c 933 waiting_at = p->waiting_at;
a5a488db
RK
934 }
935
ebe9b300 936 if (!next && pl08x->has_slave) {
01d8dc64 937 list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
2ff25c1c
JNG
938 if (p->state == PL08X_CHAN_WAITING &&
939 p->waiting_at <= waiting_at) {
a5a488db 940 next = p;
2ff25c1c 941 waiting_at = p->waiting_at;
a5a488db
RK
942 }
943 }
944
945 /* Ensure that the physical channel is stopped */
946 pl08x_terminate_phy_chan(pl08x, plchan->phychan);
947
948 if (next) {
949 bool success;
950
951 /*
952 * Eww. We know this isn't going to deadlock
953 * but lockdep probably doesn't.
954 */
083be28a 955 spin_lock(&next->vc.lock);
a5a488db
RK
956 /* Re-check the state now that we have the lock */
957 success = next->state == PL08X_CHAN_WAITING;
958 if (success)
959 pl08x_phy_reassign_start(plchan->phychan, next);
083be28a 960 spin_unlock(&next->vc.lock);
a5a488db
RK
961
962 /* If the state changed, try to find another channel */
963 if (!success)
964 goto retry;
965 } else {
966 /* No more jobs, so free up the physical channel */
967 pl08x_put_phy_channel(pl08x, plchan->phychan);
968 }
969
970 plchan->phychan = NULL;
971 plchan->state = PL08X_CHAN_IDLE;
e8689e63
LW
972}
973
974/*
975 * LLI handling
976 */
977
1e1cfc72
LW
978static inline unsigned int
979pl08x_get_bytes_for_lli(struct pl08x_driver_data *pl08x,
980 u32 cctl,
981 bool source)
e8689e63 982{
1e1cfc72
LW
983 u32 val;
984
985 if (pl08x->vd->ftdmac020) {
986 if (source)
987 val = (cctl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
988 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
989 else
990 val = (cctl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
991 FTDMAC020_LLI_DST_WIDTH_SHIFT;
992 } else {
993 if (source)
994 val = (cctl & PL080_CONTROL_SWIDTH_MASK) >>
995 PL080_CONTROL_SWIDTH_SHIFT;
996 else
997 val = (cctl & PL080_CONTROL_DWIDTH_MASK) >>
998 PL080_CONTROL_DWIDTH_SHIFT;
999 }
1000
1001 switch (val) {
e8689e63
LW
1002 case PL080_WIDTH_8BIT:
1003 return 1;
1004 case PL080_WIDTH_16BIT:
1005 return 2;
1006 case PL080_WIDTH_32BIT:
1007 return 4;
1008 default:
1009 break;
1010 }
1011 BUG();
1012 return 0;
1013}
1014
1e1cfc72
LW
1015static inline u32 pl08x_lli_control_bits(struct pl08x_driver_data *pl08x,
1016 u32 cctl,
1017 u8 srcwidth, u8 dstwidth,
1018 size_t tsize)
e8689e63
LW
1019{
1020 u32 retbits = cctl;
1021
1e1cfc72
LW
1022 /*
1023 * Remove all src, dst and transfer size bits, then set the
1024 * width and size according to the parameters. The bit offsets
1025 * are different in the FTDMAC020 so we need to accound for this.
1026 */
1027 if (pl08x->vd->ftdmac020) {
1028 retbits &= ~FTDMAC020_LLI_DST_WIDTH_MSK;
1029 retbits &= ~FTDMAC020_LLI_SRC_WIDTH_MSK;
1030 retbits &= ~FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1031
1032 switch (srcwidth) {
1033 case 1:
1034 retbits |= PL080_WIDTH_8BIT <<
1035 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1036 break;
1037 case 2:
1038 retbits |= PL080_WIDTH_16BIT <<
1039 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1040 break;
1041 case 4:
1042 retbits |= PL080_WIDTH_32BIT <<
1043 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1044 break;
1045 default:
1046 BUG();
1047 break;
1048 }
e8689e63 1049
1e1cfc72
LW
1050 switch (dstwidth) {
1051 case 1:
1052 retbits |= PL080_WIDTH_8BIT <<
1053 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1054 break;
1055 case 2:
1056 retbits |= PL080_WIDTH_16BIT <<
1057 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1058 break;
1059 case 4:
1060 retbits |= PL080_WIDTH_32BIT <<
1061 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1062 break;
1063 default:
1064 BUG();
1065 break;
1066 }
e8689e63 1067
1e1cfc72
LW
1068 tsize &= FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1069 retbits |= tsize << FTDMAC020_LLI_TRANSFER_SIZE_SHIFT;
1070 } else {
1071 retbits &= ~PL080_CONTROL_DWIDTH_MASK;
1072 retbits &= ~PL080_CONTROL_SWIDTH_MASK;
1073 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
1074
1075 switch (srcwidth) {
1076 case 1:
1077 retbits |= PL080_WIDTH_8BIT <<
1078 PL080_CONTROL_SWIDTH_SHIFT;
1079 break;
1080 case 2:
1081 retbits |= PL080_WIDTH_16BIT <<
1082 PL080_CONTROL_SWIDTH_SHIFT;
1083 break;
1084 case 4:
1085 retbits |= PL080_WIDTH_32BIT <<
1086 PL080_CONTROL_SWIDTH_SHIFT;
1087 break;
1088 default:
1089 BUG();
1090 break;
1091 }
1092
1093 switch (dstwidth) {
1094 case 1:
1095 retbits |= PL080_WIDTH_8BIT <<
1096 PL080_CONTROL_DWIDTH_SHIFT;
1097 break;
1098 case 2:
1099 retbits |= PL080_WIDTH_16BIT <<
1100 PL080_CONTROL_DWIDTH_SHIFT;
1101 break;
1102 case 4:
1103 retbits |= PL080_WIDTH_32BIT <<
1104 PL080_CONTROL_DWIDTH_SHIFT;
1105 break;
1106 default:
1107 BUG();
1108 break;
1109 }
1110
1111 tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
1112 retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
e8689e63
LW
1113 }
1114
e8689e63
LW
1115 return retbits;
1116}
1117
542361f8
RKAL
1118struct pl08x_lli_build_data {
1119 struct pl08x_txd *txd;
542361f8
RKAL
1120 struct pl08x_bus_data srcbus;
1121 struct pl08x_bus_data dstbus;
1122 size_t remainder;
25c94f7f 1123 u32 lli_bus;
542361f8
RKAL
1124};
1125
e8689e63 1126/*
0532e6fc
VK
1127 * Autoselect a master bus to use for the transfer. Slave will be the chosen as
1128 * victim in case src & dest are not similarly aligned. i.e. If after aligning
1129 * masters address with width requirements of transfer (by sending few byte by
1130 * byte data), slave is still not aligned, then its width will be reduced to
1131 * BYTE.
1132 * - prefers the destination bus if both available
036f05fd 1133 * - prefers bus with fixed address (i.e. peripheral)
e8689e63 1134 */
1e1cfc72
LW
1135static void pl08x_choose_master_bus(struct pl08x_driver_data *pl08x,
1136 struct pl08x_lli_build_data *bd,
1137 struct pl08x_bus_data **mbus,
1138 struct pl08x_bus_data **sbus,
1139 u32 cctl)
e8689e63 1140{
1e1cfc72
LW
1141 bool dst_incr;
1142 bool src_incr;
1143
1144 /*
1145 * The FTDMAC020 only supports memory-to-memory transfer, so
1146 * source and destination always increase.
1147 */
1148 if (pl08x->vd->ftdmac020) {
1149 dst_incr = true;
1150 src_incr = true;
1151 } else {
1152 dst_incr = !!(cctl & PL080_CONTROL_DST_INCR);
1153 src_incr = !!(cctl & PL080_CONTROL_SRC_INCR);
1154 }
1155
1156 /*
1157 * If either bus is not advancing, i.e. it is a peripheral, that
1158 * one becomes master
1159 */
1160 if (!dst_incr) {
542361f8
RKAL
1161 *mbus = &bd->dstbus;
1162 *sbus = &bd->srcbus;
1e1cfc72 1163 } else if (!src_incr) {
036f05fd
VK
1164 *mbus = &bd->srcbus;
1165 *sbus = &bd->dstbus;
e8689e63 1166 } else {
036f05fd 1167 if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
542361f8
RKAL
1168 *mbus = &bd->dstbus;
1169 *sbus = &bd->srcbus;
036f05fd 1170 } else {
542361f8
RKAL
1171 *mbus = &bd->srcbus;
1172 *sbus = &bd->dstbus;
e8689e63
LW
1173 }
1174 }
1175}
1176
1177/*
94ae8522 1178 * Fills in one LLI for a certain transfer descriptor and advance the counter
e8689e63 1179 */
ba6785ff
TF
1180static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
1181 struct pl08x_lli_build_data *bd,
da1b6c05 1182 int num_llis, int len, u32 cctl, u32 cctl2)
e8689e63 1183{
ba6785ff
TF
1184 u32 offset = num_llis * pl08x->lli_words;
1185 u32 *llis_va = bd->txd->llis_va + offset;
542361f8 1186 dma_addr_t llis_bus = bd->txd->llis_bus;
e8689e63
LW
1187
1188 BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
1189
ba6785ff
TF
1190 /* Advance the offset to next LLI. */
1191 offset += pl08x->lli_words;
1192
1193 llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
1194 llis_va[PL080_LLI_DST] = bd->dstbus.addr;
1195 llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
1196 llis_va[PL080_LLI_LLI] |= bd->lli_bus;
1197 llis_va[PL080_LLI_CCTL] = cctl;
da1b6c05
TF
1198 if (pl08x->vd->pl080s)
1199 llis_va[PL080S_LLI_CCTL2] = cctl2;
e8689e63 1200
1e1cfc72
LW
1201 if (pl08x->vd->ftdmac020) {
1202 /* FIXME: only memcpy so far so both increase */
542361f8 1203 bd->srcbus.addr += len;
542361f8 1204 bd->dstbus.addr += len;
1e1cfc72
LW
1205 } else {
1206 if (cctl & PL080_CONTROL_SRC_INCR)
1207 bd->srcbus.addr += len;
1208 if (cctl & PL080_CONTROL_DST_INCR)
1209 bd->dstbus.addr += len;
1210 }
e8689e63 1211
542361f8 1212 BUG_ON(bd->remainder < len);
cace6585 1213
542361f8 1214 bd->remainder -= len;
e8689e63
LW
1215}
1216
ba6785ff
TF
1217static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
1218 struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
1219 int num_llis, size_t *total_bytes)
e8689e63 1220{
1e1cfc72 1221 *cctl = pl08x_lli_control_bits(pl08x, *cctl, 1, 1, len);
da1b6c05 1222 pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
03af500f 1223 (*total_bytes) += len;
e8689e63
LW
1224}
1225
1e1cfc72 1226#if 1
48924e42
TF
1227static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1228 const u32 *llis_va, int num_llis)
1229{
1230 int i;
1231
da1b6c05 1232 if (pl08x->vd->pl080s) {
48924e42 1233 dev_vdbg(&pl08x->adev->dev,
da1b6c05
TF
1234 "%-3s %-9s %-10s %-10s %-10s %-10s %s\n",
1235 "lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
1236 for (i = 0; i < num_llis; i++) {
1237 dev_vdbg(&pl08x->adev->dev,
1238 "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
1239 i, llis_va, llis_va[PL080_LLI_SRC],
1240 llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1241 llis_va[PL080_LLI_CCTL],
1242 llis_va[PL080S_LLI_CCTL2]);
1243 llis_va += pl08x->lli_words;
1244 }
1245 } else {
1246 dev_vdbg(&pl08x->adev->dev,
1247 "%-3s %-9s %-10s %-10s %-10s %s\n",
1248 "lli", "", "csrc", "cdst", "clli", "cctl");
1249 for (i = 0; i < num_llis; i++) {
1250 dev_vdbg(&pl08x->adev->dev,
1251 "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1252 i, llis_va, llis_va[PL080_LLI_SRC],
1253 llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1254 llis_va[PL080_LLI_CCTL]);
1255 llis_va += pl08x->lli_words;
1256 }
48924e42
TF
1257 }
1258}
1259#else
1260static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1261 const u32 *llis_va, int num_llis) {}
1262#endif
1263
e8689e63
LW
1264/*
1265 * This fills in the table of LLIs for the transfer descriptor
1266 * Note that we assume we never have to change the burst sizes
1267 * Return 0 for error
1268 */
1269static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
1270 struct pl08x_txd *txd)
1271{
e8689e63 1272 struct pl08x_bus_data *mbus, *sbus;
542361f8 1273 struct pl08x_lli_build_data bd;
e8689e63 1274 int num_llis = 0;
03af500f 1275 u32 cctl, early_bytes = 0;
b7f69d9d 1276 size_t max_bytes_per_lli, total_bytes;
ba6785ff 1277 u32 *llis_va, *last_lli;
b7f69d9d 1278 struct pl08x_sg *dsg;
e8689e63 1279
3e27ee84 1280 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
e8689e63
LW
1281 if (!txd->llis_va) {
1282 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
1283 return 0;
1284 }
1285
542361f8 1286 bd.txd = txd;
25c94f7f 1287 bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
b7f69d9d 1288 cctl = txd->cctl;
542361f8 1289
e8689e63 1290 /* Find maximum width of the source bus */
1e1cfc72 1291 bd.srcbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, true);
e8689e63
LW
1292
1293 /* Find maximum width of the destination bus */
1e1cfc72 1294 bd.dstbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, false);
e8689e63 1295
b7f69d9d
VK
1296 list_for_each_entry(dsg, &txd->dsg_list, node) {
1297 total_bytes = 0;
1298 cctl = txd->cctl;
e8689e63 1299
b7f69d9d
VK
1300 bd.srcbus.addr = dsg->src_addr;
1301 bd.dstbus.addr = dsg->dst_addr;
1302 bd.remainder = dsg->len;
1303 bd.srcbus.buswidth = bd.srcbus.maxwidth;
1304 bd.dstbus.buswidth = bd.dstbus.maxwidth;
e8689e63 1305
1e1cfc72 1306 pl08x_choose_master_bus(pl08x, &bd, &mbus, &sbus, cctl);
e8689e63 1307
b90ca063
AP
1308 dev_vdbg(&pl08x->adev->dev,
1309 "src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
1310 (u64)bd.srcbus.addr,
1311 cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
b7f69d9d 1312 bd.srcbus.buswidth,
b90ca063
AP
1313 (u64)bd.dstbus.addr,
1314 cctl & PL080_CONTROL_DST_INCR ? "+" : "",
b7f69d9d
VK
1315 bd.dstbus.buswidth,
1316 bd.remainder);
1317 dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
1318 mbus == &bd.srcbus ? "src" : "dst",
1319 sbus == &bd.srcbus ? "src" : "dst");
fc74eb79 1320
b7f69d9d
VK
1321 /*
1322 * Zero length is only allowed if all these requirements are
1323 * met:
1324 * - flow controller is peripheral.
1325 * - src.addr is aligned to src.width
1326 * - dst.addr is aligned to dst.width
1327 *
1328 * sg_len == 1 should be true, as there can be two cases here:
1329 *
1330 * - Memory addresses are contiguous and are not scattered.
1331 * Here, Only one sg will be passed by user driver, with
1332 * memory address and zero length. We pass this to controller
1333 * and after the transfer it will receive the last burst
1334 * request from peripheral and so transfer finishes.
1335 *
1336 * - Memory addresses are scattered and are not contiguous.
1337 * Here, Obviously as DMA controller doesn't know when a lli's
1338 * transfer gets over, it can't load next lli. So in this
1339 * case, there has to be an assumption that only one lli is
1340 * supported. Thus, we can't have scattered addresses.
1341 */
1342 if (!bd.remainder) {
1e1cfc72
LW
1343 u32 fc;
1344
1345 /* FTDMAC020 only does memory-to-memory */
1346 if (pl08x->vd->ftdmac020)
1347 fc = PL080_FLOW_MEM2MEM;
1348 else
1349 fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
1350 PL080_CONFIG_FLOW_CONTROL_SHIFT;
b7f69d9d 1351 if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
0a235657 1352 (fc <= PL080_FLOW_SRC2DST_SRC))) {
b7f69d9d
VK
1353 dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
1354 __func__);
1355 return 0;
1356 }
0a235657 1357
1c38b289
AP
1358 if (!IS_BUS_ALIGNED(&bd.srcbus) ||
1359 !IS_BUS_ALIGNED(&bd.dstbus)) {
b7f69d9d
VK
1360 dev_err(&pl08x->adev->dev,
1361 "%s src & dst address must be aligned to src"
1362 " & dst width if peripheral is flow controller",
1363 __func__);
1364 return 0;
1365 }
03af500f 1366
1e1cfc72
LW
1367 cctl = pl08x_lli_control_bits(pl08x, cctl,
1368 bd.srcbus.buswidth, bd.dstbus.buswidth,
1369 0);
ba6785ff 1370 pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
da1b6c05 1371 0, cctl, 0);
b7f69d9d
VK
1372 break;
1373 }
e8689e63
LW
1374
1375 /*
b7f69d9d
VK
1376 * Send byte by byte for following cases
1377 * - Less than a bus width available
1378 * - until master bus is aligned
e8689e63 1379 */
b7f69d9d
VK
1380 if (bd.remainder < mbus->buswidth)
1381 early_bytes = bd.remainder;
1c38b289
AP
1382 else if (!IS_BUS_ALIGNED(mbus)) {
1383 early_bytes = mbus->buswidth -
1384 (mbus->addr & (mbus->buswidth - 1));
b7f69d9d
VK
1385 if ((bd.remainder - early_bytes) < mbus->buswidth)
1386 early_bytes = bd.remainder;
1387 }
e8689e63 1388
b7f69d9d
VK
1389 if (early_bytes) {
1390 dev_vdbg(&pl08x->adev->dev,
6fc8ae78 1391 "%s byte width LLIs (remain 0x%08zx)\n",
b7f69d9d 1392 __func__, bd.remainder);
ba6785ff
TF
1393 prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
1394 num_llis++, &total_bytes);
e8689e63
LW
1395 }
1396
b7f69d9d
VK
1397 if (bd.remainder) {
1398 /*
1399 * Master now aligned
1400 * - if slave is not then we must set its width down
1401 */
1c38b289 1402 if (!IS_BUS_ALIGNED(sbus)) {
b7f69d9d
VK
1403 dev_dbg(&pl08x->adev->dev,
1404 "%s set down bus width to one byte\n",
1405 __func__);
fa6a940b 1406
b7f69d9d
VK
1407 sbus->buswidth = 1;
1408 }
e8689e63
LW
1409
1410 /*
b7f69d9d
VK
1411 * Bytes transferred = tsize * src width, not
1412 * MIN(buswidths)
e8689e63 1413 */
b7f69d9d 1414 max_bytes_per_lli = bd.srcbus.buswidth *
5110e51d 1415 pl08x->vd->max_transfer_size;
b7f69d9d
VK
1416 dev_vdbg(&pl08x->adev->dev,
1417 "%s max bytes per lli = %zu\n",
1418 __func__, max_bytes_per_lli);
e8689e63
LW
1419
1420 /*
b7f69d9d
VK
1421 * Make largest possible LLIs until less than one bus
1422 * width left
e8689e63 1423 */
b7f69d9d
VK
1424 while (bd.remainder > (mbus->buswidth - 1)) {
1425 size_t lli_len, tsize, width;
e8689e63 1426
b7f69d9d
VK
1427 /*
1428 * If enough left try to send max possible,
1429 * otherwise try to send the remainder
1430 */
1431 lli_len = min(bd.remainder, max_bytes_per_lli);
16a2e7d3 1432
b7f69d9d
VK
1433 /*
1434 * Check against maximum bus alignment:
1435 * Calculate actual transfer size in relation to
1436 * bus width an get a maximum remainder of the
1437 * highest bus width - 1
1438 */
1439 width = max(mbus->buswidth, sbus->buswidth);
1440 lli_len = (lli_len / width) * width;
1441 tsize = lli_len / bd.srcbus.buswidth;
1442
1443 dev_vdbg(&pl08x->adev->dev,
1444 "%s fill lli with single lli chunk of "
1445 "size 0x%08zx (remainder 0x%08zx)\n",
1446 __func__, lli_len, bd.remainder);
1447
1e1cfc72
LW
1448 cctl = pl08x_lli_control_bits(pl08x, cctl,
1449 bd.srcbus.buswidth, bd.dstbus.buswidth,
1450 tsize);
ba6785ff 1451 pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
da1b6c05 1452 lli_len, cctl, tsize);
b7f69d9d
VK
1453 total_bytes += lli_len;
1454 }
e8689e63 1455
b7f69d9d
VK
1456 /*
1457 * Send any odd bytes
1458 */
1459 if (bd.remainder) {
1460 dev_vdbg(&pl08x->adev->dev,
1461 "%s align with boundary, send odd bytes (remain %zu)\n",
1462 __func__, bd.remainder);
ba6785ff
TF
1463 prep_byte_width_lli(pl08x, &bd, &cctl,
1464 bd.remainder, num_llis++, &total_bytes);
b7f69d9d 1465 }
e8689e63 1466 }
16a2e7d3 1467
b7f69d9d
VK
1468 if (total_bytes != dsg->len) {
1469 dev_err(&pl08x->adev->dev,
1470 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
1471 __func__, total_bytes, dsg->len);
1472 return 0;
1473 }
e8689e63 1474
b7f69d9d
VK
1475 if (num_llis >= MAX_NUM_TSFR_LLIS) {
1476 dev_err(&pl08x->adev->dev,
1477 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
ba6785ff 1478 __func__, MAX_NUM_TSFR_LLIS);
b7f69d9d
VK
1479 return 0;
1480 }
e8689e63 1481 }
b58b6b5b
RKAL
1482
1483 llis_va = txd->llis_va;
ba6785ff 1484 last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
e8689e63 1485
3b24c20b
AB
1486 if (txd->cyclic) {
1487 /* Link back to the first LLI. */
1488 last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
1489 } else {
1490 /* The final LLI terminates the LLI. */
1491 last_lli[PL080_LLI_LLI] = 0;
1492 /* The final LLI element shall also fire an interrupt. */
1e1cfc72
LW
1493 if (pl08x->vd->ftdmac020)
1494 last_lli[PL080_LLI_CCTL] &= ~FTDMAC020_LLI_TC_MSK;
1495 else
1496 last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
e8689e63 1497 }
e8689e63 1498
48924e42 1499 pl08x_dump_lli(pl08x, llis_va, num_llis);
e8689e63
LW
1500
1501 return num_llis;
1502}
1503
e8689e63
LW
1504static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
1505 struct pl08x_txd *txd)
1506{
b7f69d9d
VK
1507 struct pl08x_sg *dsg, *_dsg;
1508
c1205646
VK
1509 if (txd->llis_va)
1510 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
e8689e63 1511
b7f69d9d
VK
1512 list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
1513 list_del(&dsg->node);
1514 kfree(dsg);
1515 }
1516
e8689e63
LW
1517 kfree(txd);
1518}
1519
18536134
RK
1520static void pl08x_desc_free(struct virt_dma_desc *vd)
1521{
1522 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1523 struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
18536134 1524
89116bf9 1525 dma_descriptor_unmap(&vd->tx);
18536134
RK
1526 if (!txd->done)
1527 pl08x_release_mux(plchan);
1528
18536134 1529 pl08x_free_txd(plchan->host, txd);
18536134
RK
1530}
1531
e8689e63
LW
1532static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
1533 struct pl08x_dma_chan *plchan)
1534{
ea160561 1535 LIST_HEAD(head);
e8689e63 1536
879f127b 1537 vchan_get_all_descriptors(&plchan->vc, &head);
91998261 1538 vchan_dma_desc_free_list(&plchan->vc, &head);
e8689e63
LW
1539}
1540
1541/*
1542 * The DMA ENGINE API
1543 */
e8689e63
LW
1544static void pl08x_free_chan_resources(struct dma_chan *chan)
1545{
a068682c
RK
1546 /* Ensure all queued descriptors are freed */
1547 vchan_free_chan_resources(to_virt_chan(chan));
e8689e63
LW
1548}
1549
e8689e63
LW
1550static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1551 struct dma_chan *chan, unsigned long flags)
1552{
1553 struct dma_async_tx_descriptor *retval = NULL;
1554
1555 return retval;
1556}
1557
1558/*
94ae8522
RKAL
1559 * Code accessing dma_async_is_complete() in a tight loop may give problems.
1560 * If slaves are relying on interrupts to signal completion this function
1561 * must not be called with interrupts disabled.
e8689e63 1562 */
3e27ee84
VK
1563static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
1564 dma_cookie_t cookie, struct dma_tx_state *txstate)
e8689e63
LW
1565{
1566 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
06e885b7
RK
1567 struct virt_dma_desc *vd;
1568 unsigned long flags;
e8689e63 1569 enum dma_status ret;
06e885b7 1570 size_t bytes = 0;
e8689e63 1571
96a2af41 1572 ret = dma_cookie_status(chan, cookie, txstate);
0996e895 1573 if (ret == DMA_COMPLETE)
e8689e63 1574 return ret;
e8689e63 1575
06e885b7
RK
1576 /*
1577 * There's no point calculating the residue if there's
1578 * no txstate to store the value.
1579 */
1580 if (!txstate) {
1581 if (plchan->state == PL08X_CHAN_PAUSED)
1582 ret = DMA_PAUSED;
1583 return ret;
1584 }
1585
1586 spin_lock_irqsave(&plchan->vc.lock, flags);
1587 ret = dma_cookie_status(chan, cookie, txstate);
0996e895 1588 if (ret != DMA_COMPLETE) {
06e885b7
RK
1589 vd = vchan_find_desc(&plchan->vc, cookie);
1590 if (vd) {
1591 /* On the issued list, so hasn't been processed yet */
1592 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1593 struct pl08x_sg *dsg;
1594
1595 list_for_each_entry(dsg, &txd->dsg_list, node)
1596 bytes += dsg->len;
1597 } else {
1598 bytes = pl08x_getbytes_chan(plchan);
1599 }
1600 }
1601 spin_unlock_irqrestore(&plchan->vc.lock, flags);
1602
e8689e63
LW
1603 /*
1604 * This cookie not complete yet
96a2af41 1605 * Get number of bytes left in the active transactions and queue
e8689e63 1606 */
06e885b7 1607 dma_set_residue(txstate, bytes);
e8689e63 1608
06e885b7
RK
1609 if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
1610 ret = DMA_PAUSED;
e8689e63
LW
1611
1612 /* Whether waiting or running, we're in progress */
06e885b7 1613 return ret;
e8689e63
LW
1614}
1615
1616/* PrimeCell DMA extension */
1617struct burst_table {
760596c6 1618 u32 burstwords;
e8689e63
LW
1619 u32 reg;
1620};
1621
1622static const struct burst_table burst_sizes[] = {
1623 {
1624 .burstwords = 256,
760596c6 1625 .reg = PL080_BSIZE_256,
e8689e63
LW
1626 },
1627 {
1628 .burstwords = 128,
760596c6 1629 .reg = PL080_BSIZE_128,
e8689e63
LW
1630 },
1631 {
1632 .burstwords = 64,
760596c6 1633 .reg = PL080_BSIZE_64,
e8689e63
LW
1634 },
1635 {
1636 .burstwords = 32,
760596c6 1637 .reg = PL080_BSIZE_32,
e8689e63
LW
1638 },
1639 {
1640 .burstwords = 16,
760596c6 1641 .reg = PL080_BSIZE_16,
e8689e63
LW
1642 },
1643 {
1644 .burstwords = 8,
760596c6 1645 .reg = PL080_BSIZE_8,
e8689e63
LW
1646 },
1647 {
1648 .burstwords = 4,
760596c6 1649 .reg = PL080_BSIZE_4,
e8689e63
LW
1650 },
1651 {
760596c6
RKAL
1652 .burstwords = 0,
1653 .reg = PL080_BSIZE_1,
e8689e63
LW
1654 },
1655};
1656
121c8476
RKAL
1657/*
1658 * Given the source and destination available bus masks, select which
1659 * will be routed to each port. We try to have source and destination
1660 * on separate ports, but always respect the allowable settings.
1661 */
1e1cfc72 1662static u32 pl08x_select_bus(bool ftdmac020, u8 src, u8 dst)
121c8476
RKAL
1663{
1664 u32 cctl = 0;
1e1cfc72
LW
1665 u32 dst_ahb2;
1666 u32 src_ahb2;
1667
1668 /* The FTDMAC020 use different bits to indicate src/dst bus */
1669 if (ftdmac020) {
1670 dst_ahb2 = FTDMAC020_LLI_DST_SEL;
1671 src_ahb2 = FTDMAC020_LLI_SRC_SEL;
1672 } else {
1673 dst_ahb2 = PL080_CONTROL_DST_AHB2;
1674 src_ahb2 = PL080_CONTROL_SRC_AHB2;
1675 }
121c8476
RKAL
1676
1677 if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1e1cfc72 1678 cctl |= dst_ahb2;
121c8476 1679 if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1e1cfc72 1680 cctl |= src_ahb2;
121c8476
RKAL
1681
1682 return cctl;
1683}
1684
f14c426c
RKAL
1685static u32 pl08x_cctl(u32 cctl)
1686{
1687 cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1688 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1689 PL080_CONTROL_PROT_MASK);
1690
1691 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1692 return cctl | PL080_CONTROL_PROT_SYS;
1693}
1694
aa88cdaa
RKAL
1695static u32 pl08x_width(enum dma_slave_buswidth width)
1696{
1697 switch (width) {
1698 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1699 return PL080_WIDTH_8BIT;
1700 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1701 return PL080_WIDTH_16BIT;
1702 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1703 return PL080_WIDTH_32BIT;
f32807f1
VK
1704 default:
1705 return ~0;
aa88cdaa 1706 }
aa88cdaa
RKAL
1707}
1708
760596c6
RKAL
1709static u32 pl08x_burst(u32 maxburst)
1710{
1711 int i;
1712
1713 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1714 if (burst_sizes[i].burstwords <= maxburst)
1715 break;
1716
1717 return burst_sizes[i].reg;
1718}
1719
9862ba17
RK
1720static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
1721 enum dma_slave_buswidth addr_width, u32 maxburst)
1722{
1723 u32 width, burst, cctl = 0;
1724
1725 width = pl08x_width(addr_width);
1726 if (width == ~0)
1727 return ~0;
1728
1729 cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
1730 cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
1731
1732 /*
1733 * If this channel will only request single transfers, set this
1734 * down to ONE element. Also select one element if no maxburst
1735 * is specified.
1736 */
1737 if (plchan->cd->single)
1738 maxburst = 1;
1739
1740 burst = pl08x_burst(maxburst);
1741 cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
1742 cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
1743
1744 return pl08x_cctl(cctl);
1745}
1746
e8689e63
LW
1747/*
1748 * Slave transactions callback to the slave device to allow
1749 * synchronization of slave DMA signals with the DMAC enable
1750 */
1751static void pl08x_issue_pending(struct dma_chan *chan)
1752{
1753 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
e8689e63
LW
1754 unsigned long flags;
1755
083be28a 1756 spin_lock_irqsave(&plchan->vc.lock, flags);
879f127b 1757 if (vchan_issue_pending(&plchan->vc)) {
a5a488db
RK
1758 if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
1759 pl08x_phy_alloc_and_start(plchan);
e8689e63 1760 }
083be28a 1761 spin_unlock_irqrestore(&plchan->vc.lock, flags);
e8689e63
LW
1762}
1763
879f127b 1764static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
ac3cd20d 1765{
b201c111 1766 struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
ac3cd20d 1767
1e1cfc72 1768 if (txd)
b7f69d9d 1769 INIT_LIST_HEAD(&txd->dsg_list);
ac3cd20d
RKAL
1770 return txd;
1771}
1772
1e1cfc72 1773static u32 pl08x_memcpy_cctl(struct pl08x_driver_data *pl08x)
e8689e63 1774{
4166a56a 1775 u32 cctl = 0;
4166a56a
LW
1776
1777 /* Conjure cctl */
1778 switch (pl08x->pd->memcpy_burst_size) {
1779 default:
1780 dev_err(&pl08x->adev->dev,
1781 "illegal burst size for memcpy, set to 1\n");
1782 /* Fall through */
1783 case PL08X_BURST_SZ_1:
1784 cctl |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
1785 PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
1786 break;
1787 case PL08X_BURST_SZ_4:
1788 cctl |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
1789 PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
1790 break;
1791 case PL08X_BURST_SZ_8:
1792 cctl |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
1793 PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
1794 break;
1795 case PL08X_BURST_SZ_16:
1796 cctl |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
1797 PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
1798 break;
1799 case PL08X_BURST_SZ_32:
1800 cctl |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
1801 PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
1802 break;
1803 case PL08X_BURST_SZ_64:
1804 cctl |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
1805 PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
1806 break;
1807 case PL08X_BURST_SZ_128:
1808 cctl |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
1809 PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
1810 break;
1811 case PL08X_BURST_SZ_256:
1812 cctl |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
1813 PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
1814 break;
1815 }
1816
1817 switch (pl08x->pd->memcpy_bus_width) {
1818 default:
1819 dev_err(&pl08x->adev->dev,
1820 "illegal bus width for memcpy, set to 8 bits\n");
1821 /* Fall through */
1822 case PL08X_BUS_WIDTH_8_BITS:
1823 cctl |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
1824 PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
1825 break;
1826 case PL08X_BUS_WIDTH_16_BITS:
1827 cctl |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
1828 PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
1829 break;
1830 case PL08X_BUS_WIDTH_32_BITS:
1831 cctl |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
1832 PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
1833 break;
1834 }
1835
1836 /* Protection flags */
1837 if (pl08x->pd->memcpy_prot_buff)
1838 cctl |= PL080_CONTROL_PROT_BUFF;
1839 if (pl08x->pd->memcpy_prot_cache)
1840 cctl |= PL080_CONTROL_PROT_CACHE;
1841
1842 /* We are the kernel, so we are in privileged mode */
1843 cctl |= PL080_CONTROL_PROT_SYS;
4983a04f 1844
e8689e63 1845 /* Both to be incremented or the code will break */
4166a56a 1846 cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
c7da9a56 1847
c7da9a56 1848 if (pl08x->vd->dualmaster)
1e1cfc72
LW
1849 cctl |= pl08x_select_bus(false,
1850 pl08x->mem_buses,
1851 pl08x->mem_buses);
1852
1853 return cctl;
1854}
1855
1856static u32 pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data *pl08x)
1857{
1858 u32 cctl = 0;
1859
1860 /* Conjure cctl */
1861 switch (pl08x->pd->memcpy_bus_width) {
1862 default:
1863 dev_err(&pl08x->adev->dev,
1864 "illegal bus width for memcpy, set to 8 bits\n");
1865 /* Fall through */
1866 case PL08X_BUS_WIDTH_8_BITS:
1867 cctl |= PL080_WIDTH_8BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1868 PL080_WIDTH_8BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1869 break;
1870 case PL08X_BUS_WIDTH_16_BITS:
1871 cctl |= PL080_WIDTH_16BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1872 PL080_WIDTH_16BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1873 break;
1874 case PL08X_BUS_WIDTH_32_BITS:
1875 cctl |= PL080_WIDTH_32BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1876 PL080_WIDTH_32BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1877 break;
1878 }
1879
1880 /*
1881 * By default mask the TC IRQ on all LLIs, it will be unmasked on
1882 * the last LLI item by other code.
1883 */
1884 cctl |= FTDMAC020_LLI_TC_MSK;
1885
1886 /*
1887 * Both to be incremented so leave bits FTDMAC020_LLI_SRCAD_CTL
1888 * and FTDMAC020_LLI_DSTAD_CTL as zero
1889 */
1890 if (pl08x->vd->dualmaster)
1891 cctl |= pl08x_select_bus(true,
1892 pl08x->mem_buses,
4166a56a
LW
1893 pl08x->mem_buses);
1894
1e1cfc72
LW
1895 return cctl;
1896}
1897
1898/*
1899 * Initialize a descriptor to be used by memcpy submit
1900 */
1901static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1902 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1903 size_t len, unsigned long flags)
1904{
1905 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1906 struct pl08x_driver_data *pl08x = plchan->host;
1907 struct pl08x_txd *txd;
1908 struct pl08x_sg *dsg;
1909 int ret;
1910
1911 txd = pl08x_get_txd(plchan);
1912 if (!txd) {
1913 dev_err(&pl08x->adev->dev,
1914 "%s no memory for descriptor\n", __func__);
1915 return NULL;
1916 }
1917
1918 dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
1919 if (!dsg) {
1920 pl08x_free_txd(pl08x, txd);
1921 return NULL;
1922 }
1923 list_add_tail(&dsg->node, &txd->dsg_list);
1924
1925 dsg->src_addr = src;
1926 dsg->dst_addr = dest;
1927 dsg->len = len;
1928 if (pl08x->vd->ftdmac020) {
1929 /* Writing CCFG zero ENABLES all interrupts */
1930 txd->ccfg = 0;
1931 txd->cctl = pl08x_ftdmac020_memcpy_cctl(pl08x);
1932 } else {
1933 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1934 PL080_CONFIG_TC_IRQ_MASK |
1935 PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1936 txd->cctl = pl08x_memcpy_cctl(pl08x);
1937 }
e8689e63 1938
aa4afb75
RK
1939 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1940 if (!ret) {
1941 pl08x_free_txd(pl08x, txd);
e8689e63 1942 return NULL;
aa4afb75 1943 }
e8689e63 1944
879f127b 1945 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
e8689e63
LW
1946}
1947
3b24c20b
AB
1948static struct pl08x_txd *pl08x_init_txd(
1949 struct dma_chan *chan,
1950 enum dma_transfer_direction direction,
1951 dma_addr_t *slave_addr)
e8689e63
LW
1952{
1953 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1954 struct pl08x_driver_data *pl08x = plchan->host;
1955 struct pl08x_txd *txd;
dc8d5f8d 1956 enum dma_slave_buswidth addr_width;
0a235657 1957 int ret, tmp;
409ec8db 1958 u8 src_buses, dst_buses;
dc8d5f8d 1959 u32 maxburst, cctl;
e8689e63 1960
879f127b 1961 txd = pl08x_get_txd(plchan);
e8689e63
LW
1962 if (!txd) {
1963 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1964 return NULL;
1965 }
1966
e8689e63
LW
1967 /*
1968 * Set up addresses, the PrimeCell configured address
1969 * will take precedence since this may configure the
1970 * channel target address dynamically at runtime.
1971 */
db8196df 1972 if (direction == DMA_MEM_TO_DEV) {
dc8d5f8d 1973 cctl = PL080_CONTROL_SRC_INCR;
3b24c20b 1974 *slave_addr = plchan->cfg.dst_addr;
dc8d5f8d
RK
1975 addr_width = plchan->cfg.dst_addr_width;
1976 maxburst = plchan->cfg.dst_maxburst;
409ec8db
RK
1977 src_buses = pl08x->mem_buses;
1978 dst_buses = plchan->cd->periph_buses;
db8196df 1979 } else if (direction == DMA_DEV_TO_MEM) {
dc8d5f8d 1980 cctl = PL080_CONTROL_DST_INCR;
3b24c20b 1981 *slave_addr = plchan->cfg.src_addr;
dc8d5f8d
RK
1982 addr_width = plchan->cfg.src_addr_width;
1983 maxburst = plchan->cfg.src_maxburst;
409ec8db
RK
1984 src_buses = plchan->cd->periph_buses;
1985 dst_buses = pl08x->mem_buses;
e8689e63 1986 } else {
b7f69d9d 1987 pl08x_free_txd(pl08x, txd);
e8689e63
LW
1988 dev_err(&pl08x->adev->dev,
1989 "%s direction unsupported\n", __func__);
1990 return NULL;
1991 }
e8689e63 1992
dc8d5f8d 1993 cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
800d683e
RK
1994 if (cctl == ~0) {
1995 pl08x_free_txd(pl08x, txd);
1996 dev_err(&pl08x->adev->dev,
1997 "DMA slave configuration botched?\n");
1998 return NULL;
1999 }
2000
1e1cfc72 2001 txd->cctl = cctl | pl08x_select_bus(false, src_buses, dst_buses);
409ec8db 2002
95442b22 2003 if (plchan->cfg.device_fc)
db8196df 2004 tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
0a235657
VK
2005 PL080_FLOW_PER2MEM_PER;
2006 else
db8196df 2007 tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
0a235657
VK
2008 PL080_FLOW_PER2MEM;
2009
1e1cfc72
LW
2010 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
2011 PL080_CONFIG_TC_IRQ_MASK |
2012 tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
0a235657 2013
c48d4963
RK
2014 ret = pl08x_request_mux(plchan);
2015 if (ret < 0) {
2016 pl08x_free_txd(pl08x, txd);
2017 dev_dbg(&pl08x->adev->dev,
2018 "unable to mux for transfer on %s due to platform restrictions\n",
2019 plchan->name);
2020 return NULL;
2021 }
2022
2023 dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
2024 plchan->signal, plchan->name);
2025
2026 /* Assign the flow control signal to this channel */
2027 if (direction == DMA_MEM_TO_DEV)
2028 txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
2029 else
2030 txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
2031
3b24c20b
AB
2032 return txd;
2033}
2034
2035static int pl08x_tx_add_sg(struct pl08x_txd *txd,
2036 enum dma_transfer_direction direction,
2037 dma_addr_t slave_addr,
2038 dma_addr_t buf_addr,
2039 unsigned int len)
2040{
2041 struct pl08x_sg *dsg;
2042
2043 dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
2044 if (!dsg)
2045 return -ENOMEM;
2046
2047 list_add_tail(&dsg->node, &txd->dsg_list);
2048
2049 dsg->len = len;
2050 if (direction == DMA_MEM_TO_DEV) {
2051 dsg->src_addr = buf_addr;
2052 dsg->dst_addr = slave_addr;
2053 } else {
2054 dsg->src_addr = slave_addr;
2055 dsg->dst_addr = buf_addr;
2056 }
2057
2058 return 0;
2059}
2060
2061static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
2062 struct dma_chan *chan, struct scatterlist *sgl,
2063 unsigned int sg_len, enum dma_transfer_direction direction,
2064 unsigned long flags, void *context)
2065{
2066 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2067 struct pl08x_driver_data *pl08x = plchan->host;
2068 struct pl08x_txd *txd;
2069 struct scatterlist *sg;
2070 int ret, tmp;
2071 dma_addr_t slave_addr;
2072
2073 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
2074 __func__, sg_dma_len(sgl), plchan->name);
2075
2076 txd = pl08x_init_txd(chan, direction, &slave_addr);
2077 if (!txd)
2078 return NULL;
2079
b7f69d9d 2080 for_each_sg(sgl, sg, sg_len, tmp) {
3b24c20b
AB
2081 ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2082 sg_dma_address(sg),
2083 sg_dma_len(sg));
2084 if (ret) {
c48d4963 2085 pl08x_release_mux(plchan);
b7f69d9d
VK
2086 pl08x_free_txd(pl08x, txd);
2087 dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
2088 __func__);
2089 return NULL;
2090 }
3b24c20b 2091 }
b7f69d9d 2092
3b24c20b
AB
2093 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2094 if (!ret) {
2095 pl08x_release_mux(plchan);
2096 pl08x_free_txd(pl08x, txd);
2097 return NULL;
2098 }
2099
2100 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2101}
2102
2103static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
2104 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
2105 size_t period_len, enum dma_transfer_direction direction,
31c1e5a1 2106 unsigned long flags)
3b24c20b
AB
2107{
2108 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2109 struct pl08x_driver_data *pl08x = plchan->host;
2110 struct pl08x_txd *txd;
2111 int ret, tmp;
2112 dma_addr_t slave_addr;
2113
2114 dev_dbg(&pl08x->adev->dev,
6fc8ae78 2115 "%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
3b24c20b
AB
2116 __func__, period_len, buf_len,
2117 direction == DMA_MEM_TO_DEV ? "to" : "from",
2118 plchan->name);
2119
2120 txd = pl08x_init_txd(chan, direction, &slave_addr);
2121 if (!txd)
2122 return NULL;
2123
2124 txd->cyclic = true;
2125 txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
2126 for (tmp = 0; tmp < buf_len; tmp += period_len) {
2127 ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2128 buf_addr + tmp, period_len);
2129 if (ret) {
2130 pl08x_release_mux(plchan);
2131 pl08x_free_txd(pl08x, txd);
2132 return NULL;
b7f69d9d
VK
2133 }
2134 }
2135
aa4afb75
RK
2136 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2137 if (!ret) {
2138 pl08x_release_mux(plchan);
2139 pl08x_free_txd(pl08x, txd);
e8689e63 2140 return NULL;
aa4afb75 2141 }
e8689e63 2142
879f127b 2143 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
e8689e63
LW
2144}
2145
bcd1b0b9
MR
2146static int pl08x_config(struct dma_chan *chan,
2147 struct dma_slave_config *config)
2148{
2149 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2150 struct pl08x_driver_data *pl08x = plchan->host;
2151
2152 if (!plchan->slave)
2153 return -EINVAL;
2154
2155 /* Reject definitely invalid configurations */
2156 if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
2157 config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
2158 return -EINVAL;
2159
2160 if (config->device_fc && pl08x->vd->pl080s) {
2161 dev_err(&pl08x->adev->dev,
2162 "%s: PL080S does not support peripheral flow control\n",
2163 __func__);
2164 return -EINVAL;
2165 }
2166
2167 plchan->cfg = *config;
2168
2169 return 0;
2170}
2171
2172static int pl08x_terminate_all(struct dma_chan *chan)
e8689e63
LW
2173{
2174 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2175 struct pl08x_driver_data *pl08x = plchan->host;
2176 unsigned long flags;
e8689e63 2177
bcd1b0b9
MR
2178 spin_lock_irqsave(&plchan->vc.lock, flags);
2179 if (!plchan->phychan && !plchan->at) {
2180 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2181 return 0;
e8689e63
LW
2182 }
2183
bcd1b0b9
MR
2184 plchan->state = PL08X_CHAN_IDLE;
2185
2186 if (plchan->phychan) {
2187 /*
2188 * Mark physical channel as free and free any slave
2189 * signal
2190 */
2191 pl08x_phy_free(plchan);
2192 }
2193 /* Dequeue jobs and free LLIs */
2194 if (plchan->at) {
47d71bc7 2195 vchan_terminate_vdesc(&plchan->at->vd);
bcd1b0b9
MR
2196 plchan->at = NULL;
2197 }
2198 /* Dequeue jobs not yet fired as well */
2199 pl08x_free_txd_list(pl08x, plchan);
2200
2201 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2202
2203 return 0;
2204}
2205
47d71bc7
PU
2206static void pl08x_synchronize(struct dma_chan *chan)
2207{
2208 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2209
2210 vchan_synchronize(&plchan->vc);
2211}
2212
bcd1b0b9
MR
2213static int pl08x_pause(struct dma_chan *chan)
2214{
2215 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2216 unsigned long flags;
2217
e8689e63
LW
2218 /*
2219 * Anything succeeds on channels with no physical allocation and
2220 * no queued transfers.
2221 */
083be28a 2222 spin_lock_irqsave(&plchan->vc.lock, flags);
e8689e63 2223 if (!plchan->phychan && !plchan->at) {
083be28a 2224 spin_unlock_irqrestore(&plchan->vc.lock, flags);
e8689e63
LW
2225 return 0;
2226 }
2227
bcd1b0b9
MR
2228 pl08x_pause_phy_chan(plchan->phychan);
2229 plchan->state = PL08X_CHAN_PAUSED;
e8689e63 2230
bcd1b0b9
MR
2231 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2232
2233 return 0;
2234}
2235
2236static int pl08x_resume(struct dma_chan *chan)
2237{
2238 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2239 unsigned long flags;
2240
2241 /*
2242 * Anything succeeds on channels with no physical allocation and
2243 * no queued transfers.
2244 */
2245 spin_lock_irqsave(&plchan->vc.lock, flags);
2246 if (!plchan->phychan && !plchan->at) {
2247 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2248 return 0;
e8689e63
LW
2249 }
2250
bcd1b0b9
MR
2251 pl08x_resume_phy_chan(plchan->phychan);
2252 plchan->state = PL08X_CHAN_RUNNING;
2253
083be28a 2254 spin_unlock_irqrestore(&plchan->vc.lock, flags);
e8689e63 2255
bcd1b0b9 2256 return 0;
e8689e63
LW
2257}
2258
2259bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
2260{
7703eac9 2261 struct pl08x_dma_chan *plchan;
e8689e63
LW
2262 char *name = chan_id;
2263
7703eac9
RKAL
2264 /* Reject channels for devices not bound to this driver */
2265 if (chan->device->dev->driver != &pl08x_amba_driver.drv)
2266 return false;
2267
2268 plchan = to_pl08x_chan(chan);
2269
e8689e63
LW
2270 /* Check that the channel is not taken! */
2271 if (!strcmp(plchan->name, name))
2272 return true;
2273
2274 return false;
2275}
6d05c9fa 2276EXPORT_SYMBOL_GPL(pl08x_filter_id);
e8689e63 2277
da6f8ca1
SN
2278static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
2279{
2280 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2281
2282 return plchan->cd == chan_id;
2283}
2284
e8689e63
LW
2285/*
2286 * Just check that the device is there and active
94ae8522
RKAL
2287 * TODO: turn this bit on/off depending on the number of physical channels
2288 * actually used, if it is zero... well shut it off. That will save some
2289 * power. Cut the clock at the same time.
e8689e63
LW
2290 */
2291static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
2292{
affa115e
LW
2293 /* The Nomadik variant does not have the config register */
2294 if (pl08x->vd->nomadik)
2295 return;
1e1cfc72
LW
2296 /* The FTDMAC020 variant does this in another register */
2297 if (pl08x->vd->ftdmac020) {
2298 writel(PL080_CONFIG_ENABLE, pl08x->base + FTDMAC020_CSR);
2299 return;
2300 }
48a59ef3 2301 writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
e8689e63
LW
2302}
2303
e8689e63
LW
2304static irqreturn_t pl08x_irq(int irq, void *dev)
2305{
2306 struct pl08x_driver_data *pl08x = dev;
28da2836
VK
2307 u32 mask = 0, err, tc, i;
2308
2309 /* check & clear - ERR & TC interrupts */
2310 err = readl(pl08x->base + PL080_ERR_STATUS);
2311 if (err) {
2312 dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
2313 __func__, err);
2314 writel(err, pl08x->base + PL080_ERR_CLEAR);
e8689e63 2315 }
d29bf019 2316 tc = readl(pl08x->base + PL080_TC_STATUS);
28da2836
VK
2317 if (tc)
2318 writel(tc, pl08x->base + PL080_TC_CLEAR);
2319
2320 if (!err && !tc)
2321 return IRQ_NONE;
2322
e8689e63 2323 for (i = 0; i < pl08x->vd->channels; i++) {
ded091fe 2324 if ((BIT(i) & err) || (BIT(i) & tc)) {
e8689e63
LW
2325 /* Locate physical channel */
2326 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
2327 struct pl08x_dma_chan *plchan = phychan->serving;
a936e793 2328 struct pl08x_txd *tx;
e8689e63 2329
28da2836
VK
2330 if (!plchan) {
2331 dev_err(&pl08x->adev->dev,
2332 "%s Error TC interrupt on unused channel: 0x%08x\n",
2333 __func__, i);
2334 continue;
2335 }
2336
083be28a 2337 spin_lock(&plchan->vc.lock);
a936e793 2338 tx = plchan->at;
3b24c20b
AB
2339 if (tx && tx->cyclic) {
2340 vchan_cyclic_callback(&tx->vd);
2341 } else if (tx) {
a936e793 2342 plchan->at = NULL;
c48d4963
RK
2343 /*
2344 * This descriptor is done, release its mux
2345 * reservation.
2346 */
2347 pl08x_release_mux(plchan);
18536134
RK
2348 tx->done = true;
2349 vchan_cookie_complete(&tx->vd);
c33b644c 2350
a5a488db
RK
2351 /*
2352 * And start the next descriptor (if any),
2353 * otherwise free this channel.
2354 */
879f127b 2355 if (vchan_next_desc(&plchan->vc))
c33b644c 2356 pl08x_start_next_txd(plchan);
a5a488db
RK
2357 else
2358 pl08x_phy_free(plchan);
a936e793 2359 }
083be28a 2360 spin_unlock(&plchan->vc.lock);
a936e793 2361
ded091fe 2362 mask |= BIT(i);
e8689e63
LW
2363 }
2364 }
e8689e63
LW
2365
2366 return mask ? IRQ_HANDLED : IRQ_NONE;
2367}
2368
121c8476
RKAL
2369static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
2370{
121c8476
RKAL
2371 chan->slave = true;
2372 chan->name = chan->cd->bus_id;
ed91c13d
RK
2373 chan->cfg.src_addr = chan->cd->addr;
2374 chan->cfg.dst_addr = chan->cd->addr;
121c8476
RKAL
2375}
2376
e8689e63
LW
2377/*
2378 * Initialise the DMAC memcpy/slave channels.
2379 * Make a local wrapper to hold required data
2380 */
2381static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
3e27ee84 2382 struct dma_device *dmadev, unsigned int channels, bool slave)
e8689e63
LW
2383{
2384 struct pl08x_dma_chan *chan;
2385 int i;
2386
2387 INIT_LIST_HEAD(&dmadev->channels);
94ae8522 2388
e8689e63
LW
2389 /*
2390 * Register as many many memcpy as we have physical channels,
2391 * we won't always be able to use all but the code will have
2392 * to cope with that situation.
2393 */
2394 for (i = 0; i < channels; i++) {
b201c111 2395 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
aef94fea 2396 if (!chan)
e8689e63 2397 return -ENOMEM;
e8689e63
LW
2398
2399 chan->host = pl08x;
2400 chan->state = PL08X_CHAN_IDLE;
ad0de2ac 2401 chan->signal = -1;
e8689e63
LW
2402
2403 if (slave) {
e8689e63 2404 chan->cd = &pl08x->pd->slave_channels[i];
f9cd4761
LW
2405 /*
2406 * Some implementations have muxed signals, whereas some
2407 * use a mux in front of the signals and need dynamic
2408 * assignment of signals.
2409 */
2410 chan->signal = i;
121c8476 2411 pl08x_dma_slave_init(chan);
e8689e63 2412 } else {
4166a56a
LW
2413 chan->cd = kzalloc(sizeof(*chan->cd), GFP_KERNEL);
2414 if (!chan->cd) {
2415 kfree(chan);
2416 return -ENOMEM;
2417 }
2418 chan->cd->bus_id = "memcpy";
2419 chan->cd->periph_buses = pl08x->pd->mem_buses;
e8689e63
LW
2420 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
2421 if (!chan->name) {
4166a56a 2422 kfree(chan->cd);
e8689e63
LW
2423 kfree(chan);
2424 return -ENOMEM;
2425 }
2426 }
175a5e61 2427 dev_dbg(&pl08x->adev->dev,
e8689e63
LW
2428 "initialize virtual channel \"%s\"\n",
2429 chan->name);
2430
18536134 2431 chan->vc.desc_free = pl08x_desc_free;
083be28a 2432 vchan_init(&chan->vc, dmadev);
e8689e63
LW
2433 }
2434 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
2435 i, slave ? "slave" : "memcpy");
2436 return i;
2437}
2438
2439static void pl08x_free_virtual_channels(struct dma_device *dmadev)
2440{
2441 struct pl08x_dma_chan *chan = NULL;
2442 struct pl08x_dma_chan *next;
2443
2444 list_for_each_entry_safe(chan,
01d8dc64
RK
2445 next, &dmadev->channels, vc.chan.device_node) {
2446 list_del(&chan->vc.chan.device_node);
e8689e63
LW
2447 kfree(chan);
2448 }
2449}
2450
2451#ifdef CONFIG_DEBUG_FS
2452static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
2453{
2454 switch (state) {
2455 case PL08X_CHAN_IDLE:
2456 return "idle";
2457 case PL08X_CHAN_RUNNING:
2458 return "running";
2459 case PL08X_CHAN_PAUSED:
2460 return "paused";
2461 case PL08X_CHAN_WAITING:
2462 return "waiting";
2463 default:
2464 break;
2465 }
2466 return "UNKNOWN STATE";
2467}
2468
2469static int pl08x_debugfs_show(struct seq_file *s, void *data)
2470{
2471 struct pl08x_driver_data *pl08x = s->private;
2472 struct pl08x_dma_chan *chan;
2473 struct pl08x_phy_chan *ch;
2474 unsigned long flags;
2475 int i;
2476
2477 seq_printf(s, "PL08x physical channels:\n");
2478 seq_printf(s, "CHANNEL:\tUSER:\n");
2479 seq_printf(s, "--------\t-----\n");
2480 for (i = 0; i < pl08x->vd->channels; i++) {
2481 struct pl08x_dma_chan *virt_chan;
2482
2483 ch = &pl08x->phy_chans[i];
2484
2485 spin_lock_irqsave(&ch->lock, flags);
2486 virt_chan = ch->serving;
2487
affa115e
LW
2488 seq_printf(s, "%d\t\t%s%s\n",
2489 ch->id,
2490 virt_chan ? virt_chan->name : "(none)",
2491 ch->locked ? " LOCKED" : "");
e8689e63
LW
2492
2493 spin_unlock_irqrestore(&ch->lock, flags);
2494 }
2495
2496 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
2497 seq_printf(s, "CHANNEL:\tSTATE:\n");
2498 seq_printf(s, "--------\t------\n");
01d8dc64 2499 list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
3e2a037c 2500 seq_printf(s, "%s\t\t%s\n", chan->name,
e8689e63
LW
2501 pl08x_state_str(chan->state));
2502 }
2503
ebe9b300
LW
2504 if (pl08x->has_slave) {
2505 seq_printf(s, "\nPL08x virtual slave channels:\n");
2506 seq_printf(s, "CHANNEL:\tSTATE:\n");
2507 seq_printf(s, "--------\t------\n");
2508 list_for_each_entry(chan, &pl08x->slave.channels,
2509 vc.chan.device_node) {
2510 seq_printf(s, "%s\t\t%s\n", chan->name,
2511 pl08x_state_str(chan->state));
2512 }
e8689e63
LW
2513 }
2514
2515 return 0;
2516}
2517
8e1897bc 2518DEFINE_SHOW_ATTRIBUTE(pl08x_debugfs);
e8689e63
LW
2519
2520static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2521{
2522 /* Expose a simple debugfs interface to view all clocks */
3e27ee84
VK
2523 (void) debugfs_create_file(dev_name(&pl08x->adev->dev),
2524 S_IFREG | S_IRUGO, NULL, pl08x,
8e1897bc 2525 &pl08x_debugfs_fops);
e8689e63
LW
2526}
2527
2528#else
2529static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2530{
2531}
2532#endif
2533
aa4734da
LW
2534#ifdef CONFIG_OF
2535static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
2536 u32 id)
2537{
2538 struct pl08x_dma_chan *chan;
2539
ebe9b300
LW
2540 /* Trying to get a slave channel from something with no slave support */
2541 if (!pl08x->has_slave)
2542 return NULL;
2543
aa4734da
LW
2544 list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
2545 if (chan->signal == id)
2546 return &chan->vc.chan;
2547 }
2548
2549 return NULL;
2550}
2551
2552static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
2553 struct of_dma *ofdma)
2554{
2555 struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
aa4734da 2556 struct dma_chan *dma_chan;
f9cd4761 2557 struct pl08x_dma_chan *plchan;
aa4734da
LW
2558
2559 if (!pl08x)
2560 return NULL;
2561
f9cd4761
LW
2562 if (dma_spec->args_count != 2) {
2563 dev_err(&pl08x->adev->dev,
2564 "DMA channel translation requires two cells\n");
aa4734da 2565 return NULL;
f9cd4761 2566 }
aa4734da
LW
2567
2568 dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
f9cd4761
LW
2569 if (!dma_chan) {
2570 dev_err(&pl08x->adev->dev,
2571 "DMA slave channel not found\n");
aa4734da 2572 return NULL;
f9cd4761 2573 }
aa4734da 2574
f9cd4761
LW
2575 plchan = to_pl08x_chan(dma_chan);
2576 dev_dbg(&pl08x->adev->dev,
2577 "translated channel for signal %d\n",
2578 dma_spec->args[0]);
aa4734da 2579
f9cd4761
LW
2580 /* Augment channel data for applicable AHB buses */
2581 plchan->cd->periph_buses = dma_spec->args[1];
2582 return dma_get_slave_channel(dma_chan);
aa4734da
LW
2583}
2584
2585static int pl08x_of_probe(struct amba_device *adev,
2586 struct pl08x_driver_data *pl08x,
2587 struct device_node *np)
2588{
2589 struct pl08x_platform_data *pd;
f9cd4761 2590 struct pl08x_channel_data *chanp = NULL;
aa4734da
LW
2591 u32 val;
2592 int ret;
f9cd4761 2593 int i;
aa4734da
LW
2594
2595 pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
2596 if (!pd)
2597 return -ENOMEM;
2598
2599 /* Eligible bus masters for fetching LLIs */
2600 if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
2601 pd->lli_buses |= PL08X_AHB1;
2602 if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
2603 pd->lli_buses |= PL08X_AHB2;
2604 if (!pd->lli_buses) {
2605 dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
2606 pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
2607 }
2608
2609 /* Eligible bus masters for memory access */
2610 if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
2611 pd->mem_buses |= PL08X_AHB1;
2612 if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
2613 pd->mem_buses |= PL08X_AHB2;
2614 if (!pd->mem_buses) {
2615 dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
2616 pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
2617 }
2618
2619 /* Parse the memcpy channel properties */
2620 ret = of_property_read_u32(np, "memcpy-burst-size", &val);
2621 if (ret) {
2622 dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
2623 val = 1;
2624 }
2625 switch (val) {
2626 default:
2627 dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
2628 /* Fall through */
2629 case 1:
4166a56a 2630 pd->memcpy_burst_size = PL08X_BURST_SZ_1;
aa4734da
LW
2631 break;
2632 case 4:
4166a56a 2633 pd->memcpy_burst_size = PL08X_BURST_SZ_4;
aa4734da
LW
2634 break;
2635 case 8:
4166a56a 2636 pd->memcpy_burst_size = PL08X_BURST_SZ_8;
aa4734da
LW
2637 break;
2638 case 16:
4166a56a 2639 pd->memcpy_burst_size = PL08X_BURST_SZ_16;
aa4734da
LW
2640 break;
2641 case 32:
4166a56a 2642 pd->memcpy_burst_size = PL08X_BURST_SZ_32;
aa4734da
LW
2643 break;
2644 case 64:
4166a56a 2645 pd->memcpy_burst_size = PL08X_BURST_SZ_64;
aa4734da
LW
2646 break;
2647 case 128:
4166a56a 2648 pd->memcpy_burst_size = PL08X_BURST_SZ_128;
aa4734da
LW
2649 break;
2650 case 256:
4166a56a 2651 pd->memcpy_burst_size = PL08X_BURST_SZ_256;
aa4734da
LW
2652 break;
2653 }
2654
2655 ret = of_property_read_u32(np, "memcpy-bus-width", &val);
2656 if (ret) {
2657 dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
2658 val = 8;
2659 }
2660 switch (val) {
2661 default:
2662 dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
2663 /* Fall through */
2664 case 8:
4166a56a 2665 pd->memcpy_bus_width = PL08X_BUS_WIDTH_8_BITS;
aa4734da
LW
2666 break;
2667 case 16:
4166a56a 2668 pd->memcpy_bus_width = PL08X_BUS_WIDTH_16_BITS;
aa4734da
LW
2669 break;
2670 case 32:
4166a56a 2671 pd->memcpy_bus_width = PL08X_BUS_WIDTH_32_BITS;
aa4734da
LW
2672 break;
2673 }
2674
f9cd4761
LW
2675 /*
2676 * Allocate channel data for all possible slave channels (one
2677 * for each possible signal), channels will then be allocated
2678 * for a device and have it's AHB interfaces set up at
2679 * translation time.
2680 */
ebe9b300
LW
2681 if (pl08x->vd->signals) {
2682 chanp = devm_kcalloc(&adev->dev,
2683 pl08x->vd->signals,
2684 sizeof(struct pl08x_channel_data),
2685 GFP_KERNEL);
2686 if (!chanp)
2687 return -ENOMEM;
f9cd4761 2688
ebe9b300
LW
2689 pd->slave_channels = chanp;
2690 for (i = 0; i < pl08x->vd->signals; i++) {
2691 /*
2692 * chanp->periph_buses will be assigned at translation
2693 */
2694 chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
2695 chanp++;
2696 }
2697 pd->num_slave_channels = pl08x->vd->signals;
f9cd4761 2698 }
f9cd4761 2699
aa4734da
LW
2700 pl08x->pd = pd;
2701
2702 return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
2703 pl08x);
2704}
2705#else
2706static inline int pl08x_of_probe(struct amba_device *adev,
2707 struct pl08x_driver_data *pl08x,
2708 struct device_node *np)
2709{
2710 return -EINVAL;
2711}
2712#endif
2713
aa25afad 2714static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
e8689e63
LW
2715{
2716 struct pl08x_driver_data *pl08x;
1e1cfc72 2717 struct vendor_data *vd = id->data;
aa4734da 2718 struct device_node *np = adev->dev.of_node;
ba6785ff 2719 u32 tsfr_size;
e8689e63
LW
2720 int ret = 0;
2721 int i;
2722
2723 ret = amba_request_regions(adev, NULL);
2724 if (ret)
2725 return ret;
2726
de1a2419
RK
2727 /* Ensure that we can do DMA */
2728 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2729 if (ret)
2730 goto out_no_pl08x;
2731
e8689e63 2732 /* Create the driver state holder */
b201c111 2733 pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
e8689e63
LW
2734 if (!pl08x) {
2735 ret = -ENOMEM;
2736 goto out_no_pl08x;
2737 }
2738
f9cd4761
LW
2739 /* Assign useful pointers to the driver state */
2740 pl08x->adev = adev;
2741 pl08x->vd = vd;
2742
1e1cfc72
LW
2743 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
2744 if (!pl08x->base) {
2745 ret = -ENOMEM;
2746 goto out_no_ioremap;
2747 }
2748
2749 if (vd->ftdmac020) {
2750 u32 val;
2751
2752 val = readl(pl08x->base + FTDMAC020_REVISION);
2753 dev_info(&pl08x->adev->dev, "FTDMAC020 %d.%d rel %d\n",
2754 (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
2755 val = readl(pl08x->base + FTDMAC020_FEATURE);
2756 dev_info(&pl08x->adev->dev, "FTDMAC020 %d channels, "
2757 "%s built-in bridge, %s, %s linked lists\n",
2758 (val >> 12) & 0x0f,
2759 (val & BIT(10)) ? "no" : "has",
2760 (val & BIT(9)) ? "AHB0 and AHB1" : "AHB0",
2761 (val & BIT(8)) ? "supports" : "does not support");
2762
2763 /* Vendor data from feature register */
2764 if (!(val & BIT(8)))
2765 dev_warn(&pl08x->adev->dev,
2766 "linked lists not supported, required\n");
2767 vd->channels = (val >> 12) & 0x0f;
2768 vd->dualmaster = !!(val & BIT(9));
2769 }
2770
e8689e63
LW
2771 /* Initialize memcpy engine */
2772 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
2773 pl08x->memcpy.dev = &adev->dev;
e8689e63
LW
2774 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
2775 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
2776 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
2777 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
2778 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
bcd1b0b9
MR
2779 pl08x->memcpy.device_config = pl08x_config;
2780 pl08x->memcpy.device_pause = pl08x_pause;
2781 pl08x->memcpy.device_resume = pl08x_resume;
2782 pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
47d71bc7 2783 pl08x->memcpy.device_synchronize = pl08x_synchronize;
ea524c7e
MB
2784 pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2785 pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2786 pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
2787 pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
1e1cfc72
LW
2788 if (vd->ftdmac020)
2789 pl08x->memcpy.copy_align = DMAENGINE_ALIGN_4_BYTES;
2790
e8689e63 2791
ebe9b300
LW
2792 /*
2793 * Initialize slave engine, if the block has no signals, that means
2794 * we have no slave support.
2795 */
2796 if (vd->signals) {
2797 pl08x->has_slave = true;
2798 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
2799 dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
2800 pl08x->slave.dev = &adev->dev;
2801 pl08x->slave.device_free_chan_resources =
2802 pl08x_free_chan_resources;
2803 pl08x->slave.device_prep_dma_interrupt =
2804 pl08x_prep_dma_interrupt;
2805 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
2806 pl08x->slave.device_issue_pending = pl08x_issue_pending;
2807 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
2808 pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
2809 pl08x->slave.device_config = pl08x_config;
2810 pl08x->slave.device_pause = pl08x_pause;
2811 pl08x->slave.device_resume = pl08x_resume;
2812 pl08x->slave.device_terminate_all = pl08x_terminate_all;
47d71bc7 2813 pl08x->slave.device_synchronize = pl08x_synchronize;
ebe9b300
LW
2814 pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2815 pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2816 pl08x->slave.directions =
2817 BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2818 pl08x->slave.residue_granularity =
2819 DMA_RESIDUE_GRANULARITY_SEGMENT;
2820 }
e8689e63
LW
2821
2822 /* Get the platform data */
2823 pl08x->pd = dev_get_platdata(&adev->dev);
2824 if (!pl08x->pd) {
aa4734da
LW
2825 if (np) {
2826 ret = pl08x_of_probe(adev, pl08x, np);
2827 if (ret)
2828 goto out_no_platdata;
2829 } else {
2830 dev_err(&adev->dev, "no platform data supplied\n");
2831 ret = -EINVAL;
2832 goto out_no_platdata;
2833 }
da6f8ca1
SN
2834 } else {
2835 pl08x->slave.filter.map = pl08x->pd->slave_map;
2836 pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
2837 pl08x->slave.filter.fn = pl08x_filter_fn;
e8689e63
LW
2838 }
2839
30749cb4
RKAL
2840 /* By default, AHB1 only. If dualmaster, from platform */
2841 pl08x->lli_buses = PL08X_AHB1;
2842 pl08x->mem_buses = PL08X_AHB1;
2843 if (pl08x->vd->dualmaster) {
2844 pl08x->lli_buses = pl08x->pd->lli_buses;
2845 pl08x->mem_buses = pl08x->pd->mem_buses;
2846 }
2847
da1b6c05
TF
2848 if (vd->pl080s)
2849 pl08x->lli_words = PL080S_LLI_WORDS;
2850 else
2851 pl08x->lli_words = PL080_LLI_WORDS;
ba6785ff
TF
2852 tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
2853
e8689e63
LW
2854 /* A DMA memory pool for LLIs, align on 1-byte boundary */
2855 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
ba6785ff 2856 tsfr_size, PL08X_ALIGN, 0);
e8689e63
LW
2857 if (!pl08x->pool) {
2858 ret = -ENOMEM;
2859 goto out_no_lli_pool;
2860 }
2861
e8689e63
LW
2862 /* Turn on the PL08x */
2863 pl08x_ensure_on(pl08x);
2864
1e1cfc72
LW
2865 /* Clear any pending interrupts */
2866 if (vd->ftdmac020)
2867 /* This variant has error IRQs in bits 16-19 */
2868 writel(0x0000FFFF, pl08x->base + PL080_ERR_CLEAR);
2869 else
2870 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
e8689e63
LW
2871 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
2872
1e1cfc72 2873 /* Attach the interrupt handler */
174b537a 2874 ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
e8689e63
LW
2875 if (ret) {
2876 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
2877 __func__, adev->irq[0]);
2878 goto out_no_irq;
2879 }
2880
2881 /* Initialize physical channels */
affa115e 2882 pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
e8689e63
LW
2883 GFP_KERNEL);
2884 if (!pl08x->phy_chans) {
983d7beb 2885 ret = -ENOMEM;
e8689e63
LW
2886 goto out_no_phychans;
2887 }
2888
2889 for (i = 0; i < vd->channels; i++) {
2890 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
2891
2892 ch->id = i;
2893 ch->base = pl08x->base + PL080_Cx_BASE(i);
1e1cfc72
LW
2894 if (vd->ftdmac020) {
2895 /* FTDMA020 has a special channel busy register */
2896 ch->reg_busy = ch->base + FTDMAC020_CH_BUSY;
2897 ch->reg_config = ch->base + FTDMAC020_CH_CFG;
2898 ch->reg_control = ch->base + FTDMAC020_CH_CSR;
2899 ch->reg_src = ch->base + FTDMAC020_CH_SRC_ADDR;
2900 ch->reg_dst = ch->base + FTDMAC020_CH_DST_ADDR;
2901 ch->reg_lli = ch->base + FTDMAC020_CH_LLP;
2902 ch->ftdmac020 = true;
2903 } else {
2904 ch->reg_config = ch->base + vd->config_offset;
2905 ch->reg_control = ch->base + PL080_CH_CONTROL;
2906 ch->reg_src = ch->base + PL080_CH_SRC_ADDR;
2907 ch->reg_dst = ch->base + PL080_CH_DST_ADDR;
2908 ch->reg_lli = ch->base + PL080_CH_LLI;
2909 }
2910 if (vd->pl080s)
2911 ch->pl080s = true;
2912
e8689e63 2913 spin_lock_init(&ch->lock);
affa115e
LW
2914
2915 /*
2916 * Nomadik variants can have channels that are locked
2917 * down for the secure world only. Lock up these channels
2918 * by perpetually serving a dummy virtual channel.
2919 */
2920 if (vd->nomadik) {
2921 u32 val;
2922
d86ccea7 2923 val = readl(ch->reg_config);
affa115e
LW
2924 if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
2925 dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
2926 ch->locked = true;
2927 }
2928 }
2929
175a5e61
VK
2930 dev_dbg(&adev->dev, "physical channel %d is %s\n",
2931 i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
e8689e63
LW
2932 }
2933
2934 /* Register as many memcpy channels as there are physical channels */
2935 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
2936 pl08x->vd->channels, false);
2937 if (ret <= 0) {
2938 dev_warn(&pl08x->adev->dev,
2939 "%s failed to enumerate memcpy channels - %d\n",
2940 __func__, ret);
2941 goto out_no_memcpy;
2942 }
e8689e63
LW
2943
2944 /* Register slave channels */
ebe9b300
LW
2945 if (pl08x->has_slave) {
2946 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
2947 pl08x->pd->num_slave_channels, true);
2948 if (ret < 0) {
2949 dev_warn(&pl08x->adev->dev,
2950 "%s failed to enumerate slave channels - %d\n",
2951 __func__, ret);
2952 goto out_no_slave;
2953 }
e8689e63 2954 }
e8689e63
LW
2955
2956 ret = dma_async_device_register(&pl08x->memcpy);
2957 if (ret) {
2958 dev_warn(&pl08x->adev->dev,
2959 "%s failed to register memcpy as an async device - %d\n",
2960 __func__, ret);
2961 goto out_no_memcpy_reg;
2962 }
2963
ebe9b300
LW
2964 if (pl08x->has_slave) {
2965 ret = dma_async_device_register(&pl08x->slave);
2966 if (ret) {
2967 dev_warn(&pl08x->adev->dev,
e8689e63
LW
2968 "%s failed to register slave as an async device - %d\n",
2969 __func__, ret);
ebe9b300
LW
2970 goto out_no_slave_reg;
2971 }
e8689e63
LW
2972 }
2973
2974 amba_set_drvdata(adev, pl08x);
2975 init_pl08x_debugfs(pl08x);
da1b6c05
TF
2976 dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
2977 amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
b05cd8f4 2978 (unsigned long long)adev->res.start, adev->irq[0]);
b7b6018b 2979
e8689e63
LW
2980 return 0;
2981
2982out_no_slave_reg:
2983 dma_async_device_unregister(&pl08x->memcpy);
2984out_no_memcpy_reg:
ebe9b300
LW
2985 if (pl08x->has_slave)
2986 pl08x_free_virtual_channels(&pl08x->slave);
e8689e63
LW
2987out_no_slave:
2988 pl08x_free_virtual_channels(&pl08x->memcpy);
2989out_no_memcpy:
2990 kfree(pl08x->phy_chans);
2991out_no_phychans:
2992 free_irq(adev->irq[0], pl08x);
2993out_no_irq:
e8689e63
LW
2994 dma_pool_destroy(pl08x->pool);
2995out_no_lli_pool:
2996out_no_platdata:
1e1cfc72
LW
2997 iounmap(pl08x->base);
2998out_no_ioremap:
e8689e63
LW
2999 kfree(pl08x);
3000out_no_pl08x:
3001 amba_release_regions(adev);
3002 return ret;
3003}
3004
3005/* PL080 has 8 channels and the PL080 have just 2 */
3006static struct vendor_data vendor_pl080 = {
d86ccea7 3007 .config_offset = PL080_CH_CONFIG,
e8689e63 3008 .channels = 8,
f9cd4761 3009 .signals = 16,
e8689e63 3010 .dualmaster = true,
5110e51d 3011 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
e8689e63
LW
3012};
3013
affa115e 3014static struct vendor_data vendor_nomadik = {
d86ccea7 3015 .config_offset = PL080_CH_CONFIG,
affa115e 3016 .channels = 8,
f9cd4761 3017 .signals = 32,
affa115e
LW
3018 .dualmaster = true,
3019 .nomadik = true,
5110e51d 3020 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
affa115e
LW
3021};
3022
da1b6c05
TF
3023static struct vendor_data vendor_pl080s = {
3024 .config_offset = PL080S_CH_CONFIG,
3025 .channels = 8,
f9cd4761 3026 .signals = 32,
da1b6c05 3027 .pl080s = true,
5110e51d 3028 .max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
affa115e
LW
3029};
3030
e8689e63 3031static struct vendor_data vendor_pl081 = {
d86ccea7 3032 .config_offset = PL080_CH_CONFIG,
e8689e63 3033 .channels = 2,
f9cd4761 3034 .signals = 16,
e8689e63 3035 .dualmaster = false,
5110e51d 3036 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
e8689e63
LW
3037};
3038
1e1cfc72
LW
3039static struct vendor_data vendor_ftdmac020 = {
3040 .config_offset = PL080_CH_CONFIG,
3041 .ftdmac020 = true,
3042 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3043};
3044
b80fa121 3045static const struct amba_id pl08x_ids[] = {
da1b6c05
TF
3046 /* Samsung PL080S variant */
3047 {
3048 .id = 0x0a141080,
3049 .mask = 0xffffffff,
3050 .data = &vendor_pl080s,
3051 },
e8689e63
LW
3052 /* PL080 */
3053 {
3054 .id = 0x00041080,
3055 .mask = 0x000fffff,
3056 .data = &vendor_pl080,
3057 },
3058 /* PL081 */
3059 {
3060 .id = 0x00041081,
3061 .mask = 0x000fffff,
3062 .data = &vendor_pl081,
3063 },
3064 /* Nomadik 8815 PL080 variant */
3065 {
affa115e 3066 .id = 0x00280080,
e8689e63 3067 .mask = 0x00ffffff,
affa115e 3068 .data = &vendor_nomadik,
e8689e63 3069 },
1e1cfc72
LW
3070 /* Faraday Technology FTDMAC020 */
3071 {
3072 .id = 0x0003b080,
3073 .mask = 0x000fffff,
3074 .data = &vendor_ftdmac020,
3075 },
e8689e63
LW
3076 { 0, 0 },
3077};
3078
037566df
DM
3079MODULE_DEVICE_TABLE(amba, pl08x_ids);
3080
e8689e63
LW
3081static struct amba_driver pl08x_amba_driver = {
3082 .drv.name = DRIVER_NAME,
3083 .id_table = pl08x_ids,
3084 .probe = pl08x_probe,
3085};
3086
3087static int __init pl08x_init(void)
3088{
3089 int retval;
3090 retval = amba_driver_register(&pl08x_amba_driver);
3091 if (retval)
3092 printk(KERN_WARNING DRIVER_NAME
e8b5e11d 3093 "failed to register as an AMBA device (%d)\n",
e8689e63
LW
3094 retval);
3095 return retval;
3096}
3097subsys_initcall(pl08x_init);