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