blob: c7f7b82f61557ddb2712dc56fda0164bbd87c1aa [file] [log] [blame]
Linus Walleije8689e62010-09-28 15:57:37 +02001/*
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 *
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +000022 * The full GNU General Public License is in this distribution in the
Linus Walleije8689e62010-09-28 15:57:37 +020023 * 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 *
Russell King - ARM Linux9dc2c202011-01-03 22:33:06 +000056 * 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.
Linus Walleije8689e62010-09-28 15:57:37 +020073 *
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>
Linus Walleije8689e62010-09-28 15:57:37 +020080#include <linux/interrupt.h>
81#include <linux/slab.h>
82#include <linux/dmapool.h>
Linus Walleije8689e62010-09-28 15:57:37 +020083#include <linux/dmaengine.h>
Russell King - ARM Linux730404a2011-01-03 22:34:07 +000084#include <linux/amba/bus.h>
Linus Walleije8689e62010-09-28 15:57:37 +020085#include <linux/amba/pl08x.h>
86#include <linux/debugfs.h>
87#include <linux/seq_file.h>
88
89#include <asm/hardware/pl080.h>
Linus Walleije8689e62010-09-28 15:57:37 +020090
91#define DRIVER_NAME "pl08xdmac"
92
93/**
94 * struct vendor_data - vendor-specific config parameters
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +000095 * for PL08x derivatives
Linus Walleije8689e62010-09-28 15:57:37 +020096 * @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 {
Linus Walleije8689e62010-09-28 15:57:37 +0200101 u8 channels;
102 bool dualmaster;
103};
104
105/*
106 * PL08X private data structures
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000107 * An LLI struct - see PL08x TRM. Note that next uses bit[0] as a bus bit,
Russell King - ARM Linuxe25761d2011-01-03 22:37:52 +0000108 * start & end do not - their bus bit info is in cctl. Also note that these
109 * are fixed 32-bit quantities.
Linus Walleije8689e62010-09-28 15:57:37 +0200110 */
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000111struct pl08x_lli {
Russell King - ARM Linuxe25761d2011-01-03 22:37:52 +0000112 u32 src;
113 u32 dst;
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000114 u32 lli;
Linus Walleije8689e62010-09-28 15:57:37 +0200115 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
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000129 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI fetches
130 * @mem_buses: set to indicate memory transfers on AHB2.
Linus Walleije8689e62010-09-28 15:57:37 +0200131 * @lock: a spinlock for this struct
132 */
133struct pl08x_driver_data {
134 struct dma_device slave;
135 struct dma_device memcpy;
136 void __iomem *base;
137 struct amba_device *adev;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +0000138 const struct vendor_data *vd;
Linus Walleije8689e62010-09-28 15:57:37 +0200139 struct pl08x_platform_data *pd;
140 struct pl08x_phy_chan *phy_chans;
141 struct dma_pool *pool;
142 int pool_ctr;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000143 u8 lli_buses;
144 u8 mem_buses;
Linus Walleije8689e62010-09-28 15:57:37 +0200145 spinlock_t lock;
146};
147
148/*
149 * PL08X specific defines
150 */
151
152/*
153 * Memory boundaries: the manual for PL08x says that the controller
154 * cannot read past a 1KiB boundary, so these defines are used to
155 * create transfer LLIs that do not cross such boundaries.
156 */
157#define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */
158#define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)
159
160/* Minimum period between work queue runs */
161#define PL08X_WQ_PERIODMIN 20
162
163/* Size (bytes) of each LLI buffer allocated for one transfer */
164# define PL08X_LLI_TSFR_SIZE 0x2000
165
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000166/* Maximum times we call dma_pool_alloc on this pool without freeing */
Linus Walleije8689e62010-09-28 15:57:37 +0200167#define PL08X_MAX_ALLOCS 0x40
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000168#define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
Linus Walleije8689e62010-09-28 15:57:37 +0200169#define PL08X_ALIGN 8
170
171static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
172{
173 return container_of(chan, struct pl08x_dma_chan, chan);
174}
175
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +0000176static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
177{
178 return container_of(tx, struct pl08x_txd, tx);
179}
180
Linus Walleije8689e62010-09-28 15:57:37 +0200181/*
182 * Physical channel handling
183 */
184
185/* Whether a certain channel is busy or not */
186static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
187{
188 unsigned int val;
189
190 val = readl(ch->base + PL080_CH_CONFIG);
191 return val & PL080_CONFIG_ACTIVE;
192}
193
194/*
195 * Set the initial DMA register values i.e. those for the first LLI
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000196 * The next LLI pointer and the configuration interrupt bit have
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000197 * been set when the LLIs were constructed. Poke them into the hardware
198 * and start the transfer.
Linus Walleije8689e62010-09-28 15:57:37 +0200199 */
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000200static void pl08x_start_txd(struct pl08x_dma_chan *plchan,
201 struct pl08x_txd *txd)
Linus Walleije8689e62010-09-28 15:57:37 +0200202{
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000203 struct pl08x_driver_data *pl08x = plchan->host;
Linus Walleije8689e62010-09-28 15:57:37 +0200204 struct pl08x_phy_chan *phychan = plchan->phychan;
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000205 struct pl08x_lli *lli = &txd->llis_va[0];
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000206 u32 val;
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000207
208 plchan->at = txd;
Linus Walleije8689e62010-09-28 15:57:37 +0200209
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000210 /* Wait for channel inactive */
211 while (pl08x_phy_channel_busy(phychan))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000212 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200213
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000214 dev_vdbg(&pl08x->adev->dev,
215 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000216 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
217 phychan->id, lli->src, lli->dst, lli->lli, lli->cctl,
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000218 txd->ccfg);
Linus Walleije8689e62010-09-28 15:57:37 +0200219
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000220 writel(lli->src, phychan->base + PL080_CH_SRC_ADDR);
221 writel(lli->dst, phychan->base + PL080_CH_DST_ADDR);
222 writel(lli->lli, phychan->base + PL080_CH_LLI);
223 writel(lli->cctl, phychan->base + PL080_CH_CONTROL);
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000224 writel(txd->ccfg, phychan->base + PL080_CH_CONFIG);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000225
226 /* Enable the DMA channel */
227 /* Do not access config register until channel shows as disabled */
228 while (readl(pl08x->base + PL080_EN_CHAN) & (1 << phychan->id))
229 cpu_relax();
230
231 /* Do not access config register until channel shows as inactive */
232 val = readl(phychan->base + PL080_CH_CONFIG);
233 while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
234 val = readl(phychan->base + PL080_CH_CONFIG);
235
236 writel(val | PL080_CONFIG_ENABLE, phychan->base + PL080_CH_CONFIG);
Linus Walleije8689e62010-09-28 15:57:37 +0200237}
238
239/*
240 * Overall DMAC remains enabled always.
241 *
242 * Disabling individual channels could lose data.
243 *
244 * Disable the peripheral DMA after disabling the DMAC
245 * in order to allow the DMAC FIFO to drain, and
246 * hence allow the channel to show inactive
247 *
248 */
249static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
250{
251 u32 val;
252
253 /* Set the HALT bit and wait for the FIFO to drain */
254 val = readl(ch->base + PL080_CH_CONFIG);
255 val |= PL080_CONFIG_HALT;
256 writel(val, ch->base + PL080_CH_CONFIG);
257
258 /* Wait for channel inactive */
259 while (pl08x_phy_channel_busy(ch))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000260 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200261}
262
263static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
264{
265 u32 val;
266
267 /* Clear the HALT bit */
268 val = readl(ch->base + PL080_CH_CONFIG);
269 val &= ~PL080_CONFIG_HALT;
270 writel(val, ch->base + PL080_CH_CONFIG);
271}
272
273
274/* Stops the channel */
275static void pl08x_stop_phy_chan(struct pl08x_phy_chan *ch)
276{
277 u32 val;
278
279 pl08x_pause_phy_chan(ch);
280
281 /* Disable channel */
282 val = readl(ch->base + PL080_CH_CONFIG);
283 val &= ~PL080_CONFIG_ENABLE;
284 val &= ~PL080_CONFIG_ERR_IRQ_MASK;
285 val &= ~PL080_CONFIG_TC_IRQ_MASK;
286 writel(val, ch->base + PL080_CH_CONFIG);
287}
288
289static inline u32 get_bytes_in_cctl(u32 cctl)
290{
291 /* The source width defines the number of bytes */
292 u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
293
294 switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
295 case PL080_WIDTH_8BIT:
296 break;
297 case PL080_WIDTH_16BIT:
298 bytes *= 2;
299 break;
300 case PL080_WIDTH_32BIT:
301 bytes *= 4;
302 break;
303 }
304 return bytes;
305}
306
307/* The channel should be paused when calling this */
308static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
309{
310 struct pl08x_phy_chan *ch;
Linus Walleije8689e62010-09-28 15:57:37 +0200311 struct pl08x_txd *txd;
312 unsigned long flags;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000313 size_t bytes = 0;
Linus Walleije8689e62010-09-28 15:57:37 +0200314
315 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +0200316 ch = plchan->phychan;
317 txd = plchan->at;
318
319 /*
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000320 * Follow the LLIs to get the number of remaining
321 * bytes in the currently active transaction.
Linus Walleije8689e62010-09-28 15:57:37 +0200322 */
323 if (ch && txd) {
Russell King - ARM Linux4c0df6a2011-01-03 22:36:50 +0000324 u32 clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200325
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000326 /* First get the remaining bytes in the active transfer */
Linus Walleije8689e62010-09-28 15:57:37 +0200327 bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
328
329 if (clli) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000330 struct pl08x_lli *llis_va = txd->llis_va;
331 dma_addr_t llis_bus = txd->llis_bus;
332 int index;
Linus Walleije8689e62010-09-28 15:57:37 +0200333
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000334 BUG_ON(clli < llis_bus || clli >= llis_bus +
335 sizeof(struct pl08x_lli) * MAX_NUM_TSFR_LLIS);
Linus Walleije8689e62010-09-28 15:57:37 +0200336
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000337 /*
338 * Locate the next LLI - as this is an array,
339 * it's simple maths to find.
340 */
341 index = (clli - llis_bus) / sizeof(struct pl08x_lli);
342
343 for (; index < MAX_NUM_TSFR_LLIS; index++) {
344 bytes += get_bytes_in_cctl(llis_va[index].cctl);
345
Linus Walleije8689e62010-09-28 15:57:37 +0200346 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000347 * A LLI pointer of 0 terminates the LLI list
Linus Walleije8689e62010-09-28 15:57:37 +0200348 */
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000349 if (!llis_va[index].lli)
350 break;
Linus Walleije8689e62010-09-28 15:57:37 +0200351 }
352 }
353 }
354
355 /* Sum up all queued transactions */
Russell King - ARM Linux15c17232011-01-03 22:44:36 +0000356 if (!list_empty(&plchan->pend_list)) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000357 struct pl08x_txd *txdi;
Russell King - ARM Linux15c17232011-01-03 22:44:36 +0000358 list_for_each_entry(txdi, &plchan->pend_list, node) {
Linus Walleije8689e62010-09-28 15:57:37 +0200359 bytes += txdi->len;
360 }
Linus Walleije8689e62010-09-28 15:57:37 +0200361 }
362
363 spin_unlock_irqrestore(&plchan->lock, flags);
364
365 return bytes;
366}
367
368/*
369 * Allocate a physical channel for a virtual channel
370 */
371static struct pl08x_phy_chan *
372pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
373 struct pl08x_dma_chan *virt_chan)
374{
375 struct pl08x_phy_chan *ch = NULL;
376 unsigned long flags;
377 int i;
378
379 /*
380 * Try to locate a physical channel to be used for
381 * this transfer. If all are taken return NULL and
382 * the requester will have to cope by using some fallback
383 * PIO mode or retrying later.
384 */
385 for (i = 0; i < pl08x->vd->channels; i++) {
386 ch = &pl08x->phy_chans[i];
387
388 spin_lock_irqsave(&ch->lock, flags);
389
390 if (!ch->serving) {
391 ch->serving = virt_chan;
392 ch->signal = -1;
393 spin_unlock_irqrestore(&ch->lock, flags);
394 break;
395 }
396
397 spin_unlock_irqrestore(&ch->lock, flags);
398 }
399
400 if (i == pl08x->vd->channels) {
401 /* No physical channel available, cope with it */
402 return NULL;
403 }
404
405 return ch;
406}
407
408static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
409 struct pl08x_phy_chan *ch)
410{
411 unsigned long flags;
412
413 /* Stop the channel and clear its interrupts */
414 pl08x_stop_phy_chan(ch);
415 writel((1 << ch->id), pl08x->base + PL080_ERR_CLEAR);
416 writel((1 << ch->id), pl08x->base + PL080_TC_CLEAR);
417
418 /* Mark it as free */
419 spin_lock_irqsave(&ch->lock, flags);
420 ch->serving = NULL;
421 spin_unlock_irqrestore(&ch->lock, flags);
422}
423
424/*
425 * LLI handling
426 */
427
428static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
429{
430 switch (coded) {
431 case PL080_WIDTH_8BIT:
432 return 1;
433 case PL080_WIDTH_16BIT:
434 return 2;
435 case PL080_WIDTH_32BIT:
436 return 4;
437 default:
438 break;
439 }
440 BUG();
441 return 0;
442}
443
444static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000445 size_t tsize)
Linus Walleije8689e62010-09-28 15:57:37 +0200446{
447 u32 retbits = cctl;
448
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000449 /* Remove all src, dst and transfer size bits */
Linus Walleije8689e62010-09-28 15:57:37 +0200450 retbits &= ~PL080_CONTROL_DWIDTH_MASK;
451 retbits &= ~PL080_CONTROL_SWIDTH_MASK;
452 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
453
454 /* Then set the bits according to the parameters */
455 switch (srcwidth) {
456 case 1:
457 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
458 break;
459 case 2:
460 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
461 break;
462 case 4:
463 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
464 break;
465 default:
466 BUG();
467 break;
468 }
469
470 switch (dstwidth) {
471 case 1:
472 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
473 break;
474 case 2:
475 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
476 break;
477 case 4:
478 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
479 break;
480 default:
481 BUG();
482 break;
483 }
484
485 retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
486 return retbits;
487}
488
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000489struct pl08x_lli_build_data {
490 struct pl08x_txd *txd;
491 struct pl08x_driver_data *pl08x;
492 struct pl08x_bus_data srcbus;
493 struct pl08x_bus_data dstbus;
494 size_t remainder;
495};
496
Linus Walleije8689e62010-09-28 15:57:37 +0200497/*
498 * Autoselect a master bus to use for the transfer
499 * this prefers the destination bus if both available
500 * if fixed address on one bus the other will be chosen
501 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000502static void pl08x_choose_master_bus(struct pl08x_lli_build_data *bd,
503 struct pl08x_bus_data **mbus, struct pl08x_bus_data **sbus, u32 cctl)
Linus Walleije8689e62010-09-28 15:57:37 +0200504{
505 if (!(cctl & PL080_CONTROL_DST_INCR)) {
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000506 *mbus = &bd->srcbus;
507 *sbus = &bd->dstbus;
Linus Walleije8689e62010-09-28 15:57:37 +0200508 } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000509 *mbus = &bd->dstbus;
510 *sbus = &bd->srcbus;
Linus Walleije8689e62010-09-28 15:57:37 +0200511 } else {
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000512 if (bd->dstbus.buswidth == 4) {
513 *mbus = &bd->dstbus;
514 *sbus = &bd->srcbus;
515 } else if (bd->srcbus.buswidth == 4) {
516 *mbus = &bd->srcbus;
517 *sbus = &bd->dstbus;
518 } else if (bd->dstbus.buswidth == 2) {
519 *mbus = &bd->dstbus;
520 *sbus = &bd->srcbus;
521 } else if (bd->srcbus.buswidth == 2) {
522 *mbus = &bd->srcbus;
523 *sbus = &bd->dstbus;
Linus Walleije8689e62010-09-28 15:57:37 +0200524 } else {
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000525 /* bd->srcbus.buswidth == 1 */
526 *mbus = &bd->dstbus;
527 *sbus = &bd->srcbus;
Linus Walleije8689e62010-09-28 15:57:37 +0200528 }
529 }
530}
531
532/*
533 * Fills in one LLI for a certain transfer descriptor
534 * and advance the counter
535 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000536static void pl08x_fill_lli_for_desc(struct pl08x_lli_build_data *bd,
537 int num_llis, int len, u32 cctl)
Linus Walleije8689e62010-09-28 15:57:37 +0200538{
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000539 struct pl08x_lli *llis_va = bd->txd->llis_va;
540 dma_addr_t llis_bus = bd->txd->llis_bus;
Linus Walleije8689e62010-09-28 15:57:37 +0200541
542 BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
543
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000544 llis_va[num_llis].cctl = cctl;
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000545 llis_va[num_llis].src = bd->srcbus.addr;
546 llis_va[num_llis].dst = bd->dstbus.addr;
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000547 llis_va[num_llis].lli = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli);
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000548 if (bd->pl08x->lli_buses & PL08X_AHB2)
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000549 llis_va[num_llis].lli |= PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200550
551 if (cctl & PL080_CONTROL_SRC_INCR)
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000552 bd->srcbus.addr += len;
Linus Walleije8689e62010-09-28 15:57:37 +0200553 if (cctl & PL080_CONTROL_DST_INCR)
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000554 bd->dstbus.addr += len;
Linus Walleije8689e62010-09-28 15:57:37 +0200555
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000556 BUG_ON(bd->remainder < len);
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000557
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000558 bd->remainder -= len;
Linus Walleije8689e62010-09-28 15:57:37 +0200559}
560
561/*
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000562 * Return number of bytes to fill to boundary, or len.
563 * This calculation works for any value of addr.
Linus Walleije8689e62010-09-28 15:57:37 +0200564 */
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000565static inline size_t pl08x_pre_boundary(u32 addr, size_t len)
Linus Walleije8689e62010-09-28 15:57:37 +0200566{
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000567 size_t boundary_len = PL08X_BOUNDARY_SIZE -
568 (addr & (PL08X_BOUNDARY_SIZE - 1));
Linus Walleije8689e62010-09-28 15:57:37 +0200569
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000570 return min(boundary_len, len);
Linus Walleije8689e62010-09-28 15:57:37 +0200571}
572
573/*
574 * This fills in the table of LLIs for the transfer descriptor
575 * Note that we assume we never have to change the burst sizes
576 * Return 0 for error
577 */
578static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
579 struct pl08x_txd *txd)
580{
Linus Walleije8689e62010-09-28 15:57:37 +0200581 struct pl08x_bus_data *mbus, *sbus;
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000582 struct pl08x_lli_build_data bd;
Linus Walleije8689e62010-09-28 15:57:37 +0200583 int num_llis = 0;
584 u32 cctl;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000585 size_t max_bytes_per_lli;
586 size_t total_bytes = 0;
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000587 struct pl08x_lli *llis_va;
Linus Walleije8689e62010-09-28 15:57:37 +0200588
Linus Walleije8689e62010-09-28 15:57:37 +0200589 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
590 &txd->llis_bus);
591 if (!txd->llis_va) {
592 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
593 return 0;
594 }
595
596 pl08x->pool_ctr++;
597
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +0000598 /* Get the default CCTL */
599 cctl = txd->cctl;
Linus Walleije8689e62010-09-28 15:57:37 +0200600
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000601 bd.txd = txd;
602 bd.pl08x = pl08x;
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +0000603 bd.srcbus.addr = txd->src_addr;
604 bd.dstbus.addr = txd->dst_addr;
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000605
Linus Walleije8689e62010-09-28 15:57:37 +0200606 /* Find maximum width of the source bus */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000607 bd.srcbus.maxwidth =
Linus Walleije8689e62010-09-28 15:57:37 +0200608 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
609 PL080_CONTROL_SWIDTH_SHIFT);
610
611 /* Find maximum width of the destination bus */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000612 bd.dstbus.maxwidth =
Linus Walleije8689e62010-09-28 15:57:37 +0200613 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
614 PL080_CONTROL_DWIDTH_SHIFT);
615
616 /* Set up the bus widths to the maximum */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000617 bd.srcbus.buswidth = bd.srcbus.maxwidth;
618 bd.dstbus.buswidth = bd.dstbus.maxwidth;
Linus Walleije8689e62010-09-28 15:57:37 +0200619 dev_vdbg(&pl08x->adev->dev,
620 "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000621 __func__, bd.srcbus.buswidth, bd.dstbus.buswidth);
Linus Walleije8689e62010-09-28 15:57:37 +0200622
623
624 /*
625 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
626 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000627 max_bytes_per_lli = min(bd.srcbus.buswidth, bd.dstbus.buswidth) *
Linus Walleije8689e62010-09-28 15:57:37 +0200628 PL080_CONTROL_TRANSFER_SIZE_MASK;
629 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000630 "%s max bytes per lli = %zu\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200631 __func__, max_bytes_per_lli);
632
633 /* We need to count this down to zero */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000634 bd.remainder = txd->len;
Linus Walleije8689e62010-09-28 15:57:37 +0200635 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000636 "%s remainder = %zu\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000637 __func__, bd.remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200638
639 /*
640 * Choose bus to align to
641 * - prefers destination bus if both available
642 * - if fixed address on one bus chooses other
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000643 * - modifies cctl to choose an appropriate master
Linus Walleije8689e62010-09-28 15:57:37 +0200644 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000645 pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200646
Linus Walleije8689e62010-09-28 15:57:37 +0200647 if (txd->len < mbus->buswidth) {
648 /*
649 * Less than a bus width available
650 * - send as single bytes
651 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000652 while (bd.remainder) {
Linus Walleije8689e62010-09-28 15:57:37 +0200653 dev_vdbg(&pl08x->adev->dev,
654 "%s single byte LLIs for a transfer of "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000655 "less than a bus width (remain 0x%08x)\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000656 __func__, bd.remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200657 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000658 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200659 total_bytes++;
660 }
661 } else {
662 /*
663 * Make one byte LLIs until master bus is aligned
664 * - slave will then be aligned also
665 */
666 while ((mbus->addr) % (mbus->buswidth)) {
667 dev_vdbg(&pl08x->adev->dev,
668 "%s adjustment lli for less than bus width "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000669 "(remain 0x%08x)\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000670 __func__, bd.remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200671 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000672 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200673 total_bytes++;
674 }
675
676 /*
677 * Master now aligned
678 * - if slave is not then we must set its width down
679 */
680 if (sbus->addr % sbus->buswidth) {
681 dev_dbg(&pl08x->adev->dev,
682 "%s set down bus width to one byte\n",
683 __func__);
684
685 sbus->buswidth = 1;
686 }
687
688 /*
689 * Make largest possible LLIs until less than one bus
690 * width left
691 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000692 while (bd.remainder > (mbus->buswidth - 1)) {
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000693 size_t lli_len, target_len, tsize, odd_bytes;
Linus Walleije8689e62010-09-28 15:57:37 +0200694
695 /*
696 * If enough left try to send max possible,
697 * otherwise try to send the remainder
698 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000699 target_len = min(bd.remainder, max_bytes_per_lli);
Linus Walleije8689e62010-09-28 15:57:37 +0200700
701 /*
Russell King - ARM Linux5f638b42011-01-03 22:42:55 +0000702 * Set bus lengths for incrementing buses to the
703 * number of bytes which fill to next memory boundary,
704 * limiting on the target length calculated above.
Linus Walleije8689e62010-09-28 15:57:37 +0200705 */
706 if (cctl & PL080_CONTROL_SRC_INCR)
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000707 bd.srcbus.fill_bytes =
708 pl08x_pre_boundary(bd.srcbus.addr,
Russell King - ARM Linux5f638b42011-01-03 22:42:55 +0000709 target_len);
Linus Walleije8689e62010-09-28 15:57:37 +0200710 else
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000711 bd.srcbus.fill_bytes = target_len;
Linus Walleije8689e62010-09-28 15:57:37 +0200712
713 if (cctl & PL080_CONTROL_DST_INCR)
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000714 bd.dstbus.fill_bytes =
715 pl08x_pre_boundary(bd.dstbus.addr,
Russell King - ARM Linux5f638b42011-01-03 22:42:55 +0000716 target_len);
Linus Walleije8689e62010-09-28 15:57:37 +0200717 else
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000718 bd.dstbus.fill_bytes = target_len;
Linus Walleije8689e62010-09-28 15:57:37 +0200719
Russell King - ARM Linux5f638b42011-01-03 22:42:55 +0000720 /* Find the nearest */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000721 lli_len = min(bd.srcbus.fill_bytes,
722 bd.dstbus.fill_bytes);
Linus Walleije8689e62010-09-28 15:57:37 +0200723
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000724 BUG_ON(lli_len > bd.remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200725
726 if (lli_len <= 0) {
727 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000728 "%s lli_len is %zu, <= 0\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200729 __func__, lli_len);
730 return 0;
731 }
732
733 if (lli_len == target_len) {
734 /*
735 * Can send what we wanted
736 */
737 /*
738 * Maintain alignment
739 */
740 lli_len = (lli_len/mbus->buswidth) *
741 mbus->buswidth;
742 odd_bytes = 0;
743 } else {
744 /*
745 * So now we know how many bytes to transfer
746 * to get to the nearest boundary
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000747 * The next LLI will past the boundary
Linus Walleije8689e62010-09-28 15:57:37 +0200748 * - however we may be working to a boundary
749 * on the slave bus
750 * We need to ensure the master stays aligned
751 */
752 odd_bytes = lli_len % mbus->buswidth;
753 /*
754 * - and that we are working in multiples
755 * of the bus widths
756 */
757 lli_len -= odd_bytes;
758
759 }
760
761 if (lli_len) {
762 /*
763 * Check against minimum bus alignment:
764 * Calculate actual transfer size in relation
765 * to bus width an get a maximum remainder of
766 * the smallest bus width - 1
767 */
768 /* FIXME: use round_down()? */
769 tsize = lli_len / min(mbus->buswidth,
770 sbus->buswidth);
771 lli_len = tsize * min(mbus->buswidth,
772 sbus->buswidth);
773
774 if (target_len != lli_len) {
775 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000776 "%s can't send what we want. Desired 0x%08zx, lli of 0x%08zx bytes in txd of 0x%08zx\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200777 __func__, target_len, lli_len, txd->len);
778 }
779
780 cctl = pl08x_cctl_bits(cctl,
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000781 bd.srcbus.buswidth,
782 bd.dstbus.buswidth,
Linus Walleije8689e62010-09-28 15:57:37 +0200783 tsize);
784
785 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000786 "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000787 __func__, lli_len, bd.remainder);
788 pl08x_fill_lli_for_desc(&bd, num_llis++,
789 lli_len, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200790 total_bytes += lli_len;
791 }
792
793
794 if (odd_bytes) {
795 /*
796 * Creep past the boundary,
797 * maintaining master alignment
798 */
799 int j;
800 for (j = 0; (j < mbus->buswidth)
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000801 && (bd.remainder); j++) {
Linus Walleije8689e62010-09-28 15:57:37 +0200802 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
803 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000804 "%s align with boundary, single byte (remain 0x%08zx)\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000805 __func__, bd.remainder);
806 pl08x_fill_lli_for_desc(&bd,
807 num_llis++, 1, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200808 total_bytes++;
809 }
810 }
811 }
812
813 /*
814 * Send any odd bytes
815 */
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000816 while (bd.remainder) {
Linus Walleije8689e62010-09-28 15:57:37 +0200817 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
818 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000819 "%s align with boundary, single odd byte (remain %zu)\n",
Russell King - ARM Linux542361f2011-01-03 22:43:15 +0000820 __func__, bd.remainder);
821 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl);
Linus Walleije8689e62010-09-28 15:57:37 +0200822 total_bytes++;
823 }
824 }
825 if (total_bytes != txd->len) {
826 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000827 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200828 __func__, total_bytes, txd->len);
829 return 0;
830 }
831
832 if (num_llis >= MAX_NUM_TSFR_LLIS) {
833 dev_err(&pl08x->adev->dev,
834 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
835 __func__, (u32) MAX_NUM_TSFR_LLIS);
836 return 0;
837 }
Linus Walleije8689e62010-09-28 15:57:37 +0200838
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000839 llis_va = txd->llis_va;
840 /*
841 * The final LLI terminates the LLI.
842 */
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000843 llis_va[num_llis - 1].lli = 0;
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000844 /*
845 * The final LLI element shall also fire an interrupt
846 */
847 llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
Linus Walleije8689e62010-09-28 15:57:37 +0200848
Linus Walleije8689e62010-09-28 15:57:37 +0200849#ifdef VERBOSE_DEBUG
850 {
851 int i;
852
853 for (i = 0; i < num_llis; i++) {
854 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000855 "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200856 i,
857 &llis_va[i],
858 llis_va[i].src,
859 llis_va[i].dst,
860 llis_va[i].cctl,
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000861 llis_va[i].lli
Linus Walleije8689e62010-09-28 15:57:37 +0200862 );
863 }
864 }
865#endif
866
867 return num_llis;
868}
869
870/* You should call this with the struct pl08x lock held */
871static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
872 struct pl08x_txd *txd)
873{
Linus Walleije8689e62010-09-28 15:57:37 +0200874 /* Free the LLI */
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000875 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
Linus Walleije8689e62010-09-28 15:57:37 +0200876
877 pl08x->pool_ctr--;
878
879 kfree(txd);
880}
881
882static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
883 struct pl08x_dma_chan *plchan)
884{
885 struct pl08x_txd *txdi = NULL;
886 struct pl08x_txd *next;
887
Russell King - ARM Linux15c17232011-01-03 22:44:36 +0000888 if (!list_empty(&plchan->pend_list)) {
Linus Walleije8689e62010-09-28 15:57:37 +0200889 list_for_each_entry_safe(txdi,
Russell King - ARM Linux15c17232011-01-03 22:44:36 +0000890 next, &plchan->pend_list, node) {
Linus Walleije8689e62010-09-28 15:57:37 +0200891 list_del(&txdi->node);
892 pl08x_free_txd(pl08x, txdi);
893 }
894
895 }
896}
897
898/*
899 * The DMA ENGINE API
900 */
901static int pl08x_alloc_chan_resources(struct dma_chan *chan)
902{
903 return 0;
904}
905
906static void pl08x_free_chan_resources(struct dma_chan *chan)
907{
908}
909
910/*
911 * This should be called with the channel plchan->lock held
912 */
913static int prep_phy_channel(struct pl08x_dma_chan *plchan,
914 struct pl08x_txd *txd)
915{
916 struct pl08x_driver_data *pl08x = plchan->host;
917 struct pl08x_phy_chan *ch;
918 int ret;
919
920 /* Check if we already have a channel */
921 if (plchan->phychan)
922 return 0;
923
924 ch = pl08x_get_phy_channel(pl08x, plchan);
925 if (!ch) {
926 /* No physical channel available, cope with it */
927 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
928 return -EBUSY;
929 }
930
931 /*
932 * OK we have a physical channel: for memcpy() this is all we
933 * need, but for slaves the physical signals may be muxed!
934 * Can the platform allow us to use this channel?
935 */
936 if (plchan->slave &&
937 ch->signal < 0 &&
938 pl08x->pd->get_signal) {
939 ret = pl08x->pd->get_signal(plchan);
940 if (ret < 0) {
941 dev_dbg(&pl08x->adev->dev,
942 "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
943 ch->id, plchan->name);
944 /* Release physical channel & return */
945 pl08x_put_phy_channel(pl08x, ch);
946 return -EBUSY;
947 }
948 ch->signal = ret;
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000949
950 /* Assign the flow control signal to this channel */
951 if (txd->direction == DMA_TO_DEVICE)
952 txd->ccfg |= ch->signal << PL080_CONFIG_DST_SEL_SHIFT;
953 else if (txd->direction == DMA_FROM_DEVICE)
954 txd->ccfg |= ch->signal << PL080_CONFIG_SRC_SEL_SHIFT;
Linus Walleije8689e62010-09-28 15:57:37 +0200955 }
956
957 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
958 ch->id,
959 ch->signal,
960 plchan->name);
961
Russell King - ARM Linux8087aac2011-01-03 22:45:17 +0000962 plchan->phychan_hold++;
Linus Walleije8689e62010-09-28 15:57:37 +0200963 plchan->phychan = ch;
964
965 return 0;
966}
967
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +0000968static void release_phy_channel(struct pl08x_dma_chan *plchan)
969{
970 struct pl08x_driver_data *pl08x = plchan->host;
971
972 if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
973 pl08x->pd->put_signal(plchan);
974 plchan->phychan->signal = -1;
975 }
976 pl08x_put_phy_channel(pl08x, plchan->phychan);
977 plchan->phychan = NULL;
978}
979
Linus Walleije8689e62010-09-28 15:57:37 +0200980static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
981{
982 struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +0000983 struct pl08x_txd *txd = to_pl08x_txd(tx);
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +0000984 unsigned long flags;
985
986 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +0200987
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +0000988 plchan->chan.cookie += 1;
989 if (plchan->chan.cookie < 0)
990 plchan->chan.cookie = 1;
991 tx->cookie = plchan->chan.cookie;
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +0000992
993 /* Put this onto the pending list */
994 list_add_tail(&txd->node, &plchan->pend_list);
995
996 /*
997 * If there was no physical channel available for this memcpy,
998 * stack the request up and indicate that the channel is waiting
999 * for a free physical channel.
1000 */
1001 if (!plchan->slave && !plchan->phychan) {
1002 /* Do this memcpy whenever there is a channel ready */
1003 plchan->state = PL08X_CHAN_WAITING;
1004 plchan->waiting = txd;
Russell King - ARM Linux8087aac2011-01-03 22:45:17 +00001005 } else {
1006 plchan->phychan_hold--;
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +00001007 }
1008
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +00001009 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001010
1011 return tx->cookie;
1012}
1013
1014static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1015 struct dma_chan *chan, unsigned long flags)
1016{
1017 struct dma_async_tx_descriptor *retval = NULL;
1018
1019 return retval;
1020}
1021
1022/*
1023 * Code accessing dma_async_is_complete() in a tight loop
1024 * may give problems - could schedule where indicated.
1025 * If slaves are relying on interrupts to signal completion this
1026 * function must not be called with interrupts disabled
1027 */
1028static enum dma_status
1029pl08x_dma_tx_status(struct dma_chan *chan,
1030 dma_cookie_t cookie,
1031 struct dma_tx_state *txstate)
1032{
1033 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1034 dma_cookie_t last_used;
1035 dma_cookie_t last_complete;
1036 enum dma_status ret;
1037 u32 bytesleft = 0;
1038
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001039 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001040 last_complete = plchan->lc;
1041
1042 ret = dma_async_is_complete(cookie, last_complete, last_used);
1043 if (ret == DMA_SUCCESS) {
1044 dma_set_tx_state(txstate, last_complete, last_used, 0);
1045 return ret;
1046 }
1047
1048 /*
1049 * schedule(); could be inserted here
1050 */
1051
1052 /*
1053 * This cookie not complete yet
1054 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001055 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001056 last_complete = plchan->lc;
1057
1058 /* Get number of bytes left in the active transactions and queue */
1059 bytesleft = pl08x_getbytes_chan(plchan);
1060
1061 dma_set_tx_state(txstate, last_complete, last_used,
1062 bytesleft);
1063
1064 if (plchan->state == PL08X_CHAN_PAUSED)
1065 return DMA_PAUSED;
1066
1067 /* Whether waiting or running, we're in progress */
1068 return DMA_IN_PROGRESS;
1069}
1070
1071/* PrimeCell DMA extension */
1072struct burst_table {
1073 int burstwords;
1074 u32 reg;
1075};
1076
1077static const struct burst_table burst_sizes[] = {
1078 {
1079 .burstwords = 256,
1080 .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
1081 (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
1082 },
1083 {
1084 .burstwords = 128,
1085 .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
1086 (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
1087 },
1088 {
1089 .burstwords = 64,
1090 .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
1091 (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
1092 },
1093 {
1094 .burstwords = 32,
1095 .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
1096 (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
1097 },
1098 {
1099 .burstwords = 16,
1100 .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
1101 (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
1102 },
1103 {
1104 .burstwords = 8,
1105 .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
1106 (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
1107 },
1108 {
1109 .burstwords = 4,
1110 .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
1111 (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
1112 },
1113 {
1114 .burstwords = 1,
1115 .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1116 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
1117 },
1118};
1119
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001120static int dma_set_runtime_config(struct dma_chan *chan,
1121 struct dma_slave_config *config)
Linus Walleije8689e62010-09-28 15:57:37 +02001122{
1123 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1124 struct pl08x_driver_data *pl08x = plchan->host;
1125 struct pl08x_channel_data *cd = plchan->cd;
1126 enum dma_slave_buswidth addr_width;
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001127 dma_addr_t addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001128 u32 maxburst;
1129 u32 cctl = 0;
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001130 int i;
Linus Walleije8689e62010-09-28 15:57:37 +02001131
1132 /* Transfer direction */
1133 plchan->runtime_direction = config->direction;
1134 if (config->direction == DMA_TO_DEVICE) {
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001135 addr = config->dst_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001136 addr_width = config->dst_addr_width;
1137 maxburst = config->dst_maxburst;
1138 } else if (config->direction == DMA_FROM_DEVICE) {
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001139 addr = config->src_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001140 addr_width = config->src_addr_width;
1141 maxburst = config->src_maxburst;
1142 } else {
1143 dev_err(&pl08x->adev->dev,
1144 "bad runtime_config: alien transfer direction\n");
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001145 return -EINVAL;
Linus Walleije8689e62010-09-28 15:57:37 +02001146 }
1147
1148 switch (addr_width) {
1149 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1150 cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1151 (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
1152 break;
1153 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1154 cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1155 (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
1156 break;
1157 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1158 cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1159 (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
1160 break;
1161 default:
1162 dev_err(&pl08x->adev->dev,
1163 "bad runtime_config: alien address width\n");
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001164 return -EINVAL;
Linus Walleije8689e62010-09-28 15:57:37 +02001165 }
1166
1167 /*
1168 * Now decide on a maxburst:
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001169 * If this channel will only request single transfers, set this
1170 * down to ONE element. Also select one element if no maxburst
1171 * is specified.
Linus Walleije8689e62010-09-28 15:57:37 +02001172 */
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001173 if (plchan->cd->single || maxburst == 0) {
Linus Walleije8689e62010-09-28 15:57:37 +02001174 cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1175 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
1176 } else {
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001177 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
Linus Walleije8689e62010-09-28 15:57:37 +02001178 if (burst_sizes[i].burstwords <= maxburst)
1179 break;
Linus Walleije8689e62010-09-28 15:57:37 +02001180 cctl |= burst_sizes[i].reg;
1181 }
1182
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001183 plchan->runtime_addr = addr;
1184
Linus Walleije8689e62010-09-28 15:57:37 +02001185 /* Modify the default channel data to fit PrimeCell request */
1186 cd->cctl = cctl;
Linus Walleije8689e62010-09-28 15:57:37 +02001187
1188 dev_dbg(&pl08x->adev->dev,
1189 "configured channel %s (%s) for %s, data width %d, "
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001190 "maxburst %d words, LE, CCTL=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +02001191 dma_chan_name(chan), plchan->name,
1192 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
1193 addr_width,
1194 maxburst,
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001195 cctl);
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001196
1197 return 0;
Linus Walleije8689e62010-09-28 15:57:37 +02001198}
1199
1200/*
1201 * Slave transactions callback to the slave device to allow
1202 * synchronization of slave DMA signals with the DMAC enable
1203 */
1204static void pl08x_issue_pending(struct dma_chan *chan)
1205{
1206 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
Linus Walleije8689e62010-09-28 15:57:37 +02001207 unsigned long flags;
1208
1209 spin_lock_irqsave(&plchan->lock, flags);
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001210 /* Something is already active, or we're waiting for a channel... */
1211 if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
1212 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001213 return;
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001214 }
Linus Walleije8689e62010-09-28 15:57:37 +02001215
1216 /* Take the first element in the queue and execute it */
Russell King - ARM Linux15c17232011-01-03 22:44:36 +00001217 if (!list_empty(&plchan->pend_list)) {
Linus Walleije8689e62010-09-28 15:57:37 +02001218 struct pl08x_txd *next;
1219
Russell King - ARM Linux15c17232011-01-03 22:44:36 +00001220 next = list_first_entry(&plchan->pend_list,
Linus Walleije8689e62010-09-28 15:57:37 +02001221 struct pl08x_txd,
1222 node);
1223 list_del(&next->node);
Linus Walleije8689e62010-09-28 15:57:37 +02001224 plchan->state = PL08X_CHAN_RUNNING;
1225
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001226 pl08x_start_txd(plchan, next);
Linus Walleije8689e62010-09-28 15:57:37 +02001227 }
1228
1229 spin_unlock_irqrestore(&plchan->lock, flags);
1230}
1231
1232static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1233 struct pl08x_txd *txd)
1234{
Linus Walleije8689e62010-09-28 15:57:37 +02001235 struct pl08x_driver_data *pl08x = plchan->host;
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +00001236 unsigned long flags;
1237 int num_llis, ret;
Linus Walleije8689e62010-09-28 15:57:37 +02001238
1239 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001240 if (!num_llis) {
1241 kfree(txd);
Linus Walleije8689e62010-09-28 15:57:37 +02001242 return -EINVAL;
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001243 }
Linus Walleije8689e62010-09-28 15:57:37 +02001244
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +00001245 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001246
Linus Walleije8689e62010-09-28 15:57:37 +02001247 /*
1248 * See if we already have a physical channel allocated,
1249 * else this is the time to try to get one.
1250 */
1251 ret = prep_phy_channel(plchan, txd);
1252 if (ret) {
1253 /*
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +00001254 * No physical channel was available.
1255 *
1256 * memcpy transfers can be sorted out at submission time.
1257 *
1258 * Slave transfers may have been denied due to platform
1259 * channel muxing restrictions. Since there is no guarantee
1260 * that this will ever be resolved, and the signal must be
1261 * acquired AFTER acquiring the physical channel, we will let
1262 * them be NACK:ed with -EBUSY here. The drivers can retry
1263 * the prep() call if they are eager on doing this using DMA.
Linus Walleije8689e62010-09-28 15:57:37 +02001264 */
1265 if (plchan->slave) {
1266 pl08x_free_txd_list(pl08x, plchan);
Russell King - ARM Linux501e67e2011-01-03 22:44:57 +00001267 pl08x_free_txd(pl08x, txd);
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +00001268 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001269 return -EBUSY;
1270 }
Linus Walleije8689e62010-09-28 15:57:37 +02001271 } else
1272 /*
1273 * Else we're all set, paused and ready to roll,
1274 * status will switch to PL08X_CHAN_RUNNING when
1275 * we call issue_pending(). If there is something
1276 * running on the channel already we don't change
1277 * its state.
1278 */
1279 if (plchan->state == PL08X_CHAN_IDLE)
1280 plchan->state = PL08X_CHAN_PAUSED;
1281
Russell King - ARM Linuxc370e592011-01-03 22:45:37 +00001282 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001283
1284 return 0;
1285}
1286
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001287/*
1288 * Given the source and destination available bus masks, select which
1289 * will be routed to each port. We try to have source and destination
1290 * on separate ports, but always respect the allowable settings.
1291 */
1292static u32 pl08x_select_bus(struct pl08x_driver_data *pl08x, u8 src, u8 dst)
1293{
1294 u32 cctl = 0;
1295
1296 if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1297 cctl |= PL080_CONTROL_DST_AHB2;
1298 if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1299 cctl |= PL080_CONTROL_SRC_AHB2;
1300
1301 return cctl;
1302}
1303
Russell King - ARM Linuxc0428792011-01-03 22:43:56 +00001304static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan,
1305 unsigned long flags)
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001306{
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);
Russell King - ARM Linuxc0428792011-01-03 22:43:56 +00001311 txd->tx.flags = flags;
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001312 txd->tx.tx_submit = pl08x_tx_submit;
1313 INIT_LIST_HEAD(&txd->node);
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001314
1315 /* Always enable error and terminal interrupts */
1316 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1317 PL080_CONFIG_TC_IRQ_MASK;
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001318 }
1319 return txd;
1320}
1321
Linus Walleije8689e62010-09-28 15:57:37 +02001322/*
1323 * Initialize a descriptor to be used by memcpy submit
1324 */
1325static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1326 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1327 size_t len, unsigned long flags)
1328{
1329 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1330 struct pl08x_driver_data *pl08x = plchan->host;
1331 struct pl08x_txd *txd;
1332 int ret;
1333
Russell King - ARM Linuxc0428792011-01-03 22:43:56 +00001334 txd = pl08x_get_txd(plchan, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001335 if (!txd) {
1336 dev_err(&pl08x->adev->dev,
1337 "%s no memory for descriptor\n", __func__);
1338 return NULL;
1339 }
1340
Linus Walleije8689e62010-09-28 15:57:37 +02001341 txd->direction = DMA_NONE;
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001342 txd->src_addr = src;
1343 txd->dst_addr = dest;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001344 txd->len = len;
Linus Walleije8689e62010-09-28 15:57:37 +02001345
1346 /* Set platform data for m2m */
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001347 txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001348 txd->cctl = pl08x->pd->memcpy_channel.cctl &
1349 ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001350
Linus Walleije8689e62010-09-28 15:57:37 +02001351 /* Both to be incremented or the code will break */
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +00001352 txd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001353
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001354 if (pl08x->vd->dualmaster)
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001355 txd->cctl |= pl08x_select_bus(pl08x,
1356 pl08x->mem_buses, pl08x->mem_buses);
Linus Walleije8689e62010-09-28 15:57:37 +02001357
Linus Walleije8689e62010-09-28 15:57:37 +02001358 ret = pl08x_prep_channel_resources(plchan, txd);
1359 if (ret)
1360 return NULL;
Linus Walleije8689e62010-09-28 15:57:37 +02001361
1362 return &txd->tx;
1363}
1364
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001365static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
Linus Walleije8689e62010-09-28 15:57:37 +02001366 struct dma_chan *chan, struct scatterlist *sgl,
1367 unsigned int sg_len, enum dma_data_direction direction,
1368 unsigned long flags)
1369{
1370 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1371 struct pl08x_driver_data *pl08x = plchan->host;
1372 struct pl08x_txd *txd;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001373 u8 src_buses, dst_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001374 int ret;
1375
1376 /*
1377 * Current implementation ASSUMES only one sg
1378 */
1379 if (sg_len != 1) {
1380 dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
1381 __func__);
1382 BUG();
1383 }
1384
1385 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1386 __func__, sgl->length, plchan->name);
1387
Russell King - ARM Linuxc0428792011-01-03 22:43:56 +00001388 txd = pl08x_get_txd(plchan, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001389 if (!txd) {
1390 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1391 return NULL;
1392 }
1393
Linus Walleije8689e62010-09-28 15:57:37 +02001394 if (direction != plchan->runtime_direction)
1395 dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
1396 "the direction configured for the PrimeCell\n",
1397 __func__);
1398
1399 /*
1400 * Set up addresses, the PrimeCell configured address
1401 * will take precedence since this may configure the
1402 * channel target address dynamically at runtime.
1403 */
1404 txd->direction = direction;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001405 txd->len = sgl->length;
1406
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001407 txd->cctl = plchan->cd->cctl &
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001408 ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1409 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001410 PL080_CONTROL_PROT_MASK);
1411
1412 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1413 txd->cctl |= PL080_CONTROL_PROT_SYS;
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +00001414
Linus Walleije8689e62010-09-28 15:57:37 +02001415 if (direction == DMA_TO_DEVICE) {
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001416 txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001417 txd->cctl |= PL080_CONTROL_SRC_INCR;
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001418 txd->src_addr = sgl->dma_address;
Linus Walleije8689e62010-09-28 15:57:37 +02001419 if (plchan->runtime_addr)
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001420 txd->dst_addr = plchan->runtime_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001421 else
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001422 txd->dst_addr = plchan->cd->addr;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001423 src_buses = pl08x->mem_buses;
1424 dst_buses = plchan->cd->periph_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001425 } else if (direction == DMA_FROM_DEVICE) {
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001426 txd->ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001427 txd->cctl |= PL080_CONTROL_DST_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001428 if (plchan->runtime_addr)
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001429 txd->src_addr = plchan->runtime_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001430 else
Russell King - ARM Linuxd7244e92011-01-03 22:43:35 +00001431 txd->src_addr = plchan->cd->addr;
1432 txd->dst_addr = sgl->dma_address;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001433 src_buses = plchan->cd->periph_buses;
1434 dst_buses = pl08x->mem_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001435 } else {
1436 dev_err(&pl08x->adev->dev,
1437 "%s direction unsupported\n", __func__);
1438 return NULL;
1439 }
Linus Walleije8689e62010-09-28 15:57:37 +02001440
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001441 txd->cctl |= pl08x_select_bus(pl08x, src_buses, dst_buses);
1442
Linus Walleije8689e62010-09-28 15:57:37 +02001443 ret = pl08x_prep_channel_resources(plchan, txd);
1444 if (ret)
1445 return NULL;
Linus Walleije8689e62010-09-28 15:57:37 +02001446
1447 return &txd->tx;
1448}
1449
1450static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1451 unsigned long arg)
1452{
1453 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1454 struct pl08x_driver_data *pl08x = plchan->host;
1455 unsigned long flags;
1456 int ret = 0;
1457
1458 /* Controls applicable to inactive channels */
1459 if (cmd == DMA_SLAVE_CONFIG) {
Russell King - ARM Linuxf0fd9442011-01-03 22:45:57 +00001460 return dma_set_runtime_config(chan,
1461 (struct dma_slave_config *)arg);
Linus Walleije8689e62010-09-28 15:57:37 +02001462 }
1463
1464 /*
1465 * Anything succeeds on channels with no physical allocation and
1466 * no queued transfers.
1467 */
1468 spin_lock_irqsave(&plchan->lock, flags);
1469 if (!plchan->phychan && !plchan->at) {
1470 spin_unlock_irqrestore(&plchan->lock, flags);
1471 return 0;
1472 }
1473
1474 switch (cmd) {
1475 case DMA_TERMINATE_ALL:
1476 plchan->state = PL08X_CHAN_IDLE;
1477
1478 if (plchan->phychan) {
1479 pl08x_stop_phy_chan(plchan->phychan);
1480
1481 /*
1482 * Mark physical channel as free and free any slave
1483 * signal
1484 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001485 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001486 }
Linus Walleije8689e62010-09-28 15:57:37 +02001487 /* Dequeue jobs and free LLIs */
1488 if (plchan->at) {
1489 pl08x_free_txd(pl08x, plchan->at);
1490 plchan->at = NULL;
1491 }
1492 /* Dequeue jobs not yet fired as well */
1493 pl08x_free_txd_list(pl08x, plchan);
1494 break;
1495 case DMA_PAUSE:
1496 pl08x_pause_phy_chan(plchan->phychan);
1497 plchan->state = PL08X_CHAN_PAUSED;
1498 break;
1499 case DMA_RESUME:
1500 pl08x_resume_phy_chan(plchan->phychan);
1501 plchan->state = PL08X_CHAN_RUNNING;
1502 break;
1503 default:
1504 /* Unknown command */
1505 ret = -ENXIO;
1506 break;
1507 }
1508
1509 spin_unlock_irqrestore(&plchan->lock, flags);
1510
1511 return ret;
1512}
1513
1514bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1515{
1516 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1517 char *name = chan_id;
1518
1519 /* Check that the channel is not taken! */
1520 if (!strcmp(plchan->name, name))
1521 return true;
1522
1523 return false;
1524}
1525
1526/*
1527 * Just check that the device is there and active
1528 * TODO: turn this bit on/off depending on the number of
1529 * physical channels actually used, if it is zero... well
1530 * shut it off. That will save some power. Cut the clock
1531 * at the same time.
1532 */
1533static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1534{
1535 u32 val;
1536
1537 val = readl(pl08x->base + PL080_CONFIG);
1538 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001539 /* We implicitly clear bit 1 and that means little-endian mode */
Linus Walleije8689e62010-09-28 15:57:37 +02001540 val |= PL080_CONFIG_ENABLE;
1541 writel(val, pl08x->base + PL080_CONFIG);
1542}
1543
Russell King - ARM Linux3d992e12011-01-03 22:44:16 +00001544static void pl08x_unmap_buffers(struct pl08x_txd *txd)
1545{
1546 struct device *dev = txd->tx.chan->device->dev;
1547
1548 if (!(txd->tx.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
1549 if (txd->tx.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
1550 dma_unmap_single(dev, txd->src_addr, txd->len,
1551 DMA_TO_DEVICE);
1552 else
1553 dma_unmap_page(dev, txd->src_addr, txd->len,
1554 DMA_TO_DEVICE);
1555 }
1556 if (!(txd->tx.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
1557 if (txd->tx.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
1558 dma_unmap_single(dev, txd->dst_addr, txd->len,
1559 DMA_FROM_DEVICE);
1560 else
1561 dma_unmap_page(dev, txd->dst_addr, txd->len,
1562 DMA_FROM_DEVICE);
1563 }
1564}
1565
Linus Walleije8689e62010-09-28 15:57:37 +02001566static void pl08x_tasklet(unsigned long data)
1567{
1568 struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
Linus Walleije8689e62010-09-28 15:57:37 +02001569 struct pl08x_driver_data *pl08x = plchan->host;
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001570 struct pl08x_txd *txd;
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001571 unsigned long flags;
Linus Walleije8689e62010-09-28 15:57:37 +02001572
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001573 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001574
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001575 txd = plchan->at;
1576 plchan->at = NULL;
1577
1578 if (txd) {
Linus Walleije8689e62010-09-28 15:57:37 +02001579 /*
1580 * Update last completed
1581 */
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001582 plchan->lc = txd->tx.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001583 }
Russell King - ARM Linux8087aac2011-01-03 22:45:17 +00001584
Linus Walleije8689e62010-09-28 15:57:37 +02001585 /*
1586 * If a new descriptor is queued, set it up
1587 * plchan->at is NULL here
1588 */
Russell King - ARM Linux15c17232011-01-03 22:44:36 +00001589 if (!list_empty(&plchan->pend_list)) {
Linus Walleije8689e62010-09-28 15:57:37 +02001590 struct pl08x_txd *next;
1591
Russell King - ARM Linux15c17232011-01-03 22:44:36 +00001592 next = list_first_entry(&plchan->pend_list,
Linus Walleije8689e62010-09-28 15:57:37 +02001593 struct pl08x_txd,
1594 node);
1595 list_del(&next->node);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001596
1597 pl08x_start_txd(plchan, next);
Russell King - ARM Linux8087aac2011-01-03 22:45:17 +00001598 } else if (plchan->phychan_hold) {
1599 /*
1600 * This channel is still in use - we have a new txd being
1601 * prepared and will soon be queued. Don't give up the
1602 * physical channel.
1603 */
Linus Walleije8689e62010-09-28 15:57:37 +02001604 } else {
1605 struct pl08x_dma_chan *waiting = NULL;
1606
1607 /*
1608 * No more jobs, so free up the physical channel
1609 * Free any allocated signal on slave transfers too
1610 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001611 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001612 plchan->state = PL08X_CHAN_IDLE;
1613
1614 /*
1615 * And NOW before anyone else can grab that free:d
1616 * up physical channel, see if there is some memcpy
1617 * pending that seriously needs to start because of
1618 * being stacked up while we were choking the
1619 * physical channels with data.
1620 */
1621 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1622 chan.device_node) {
1623 if (waiting->state == PL08X_CHAN_WAITING &&
1624 waiting->waiting != NULL) {
1625 int ret;
1626
1627 /* This should REALLY not fail now */
1628 ret = prep_phy_channel(waiting,
1629 waiting->waiting);
1630 BUG_ON(ret);
Russell King - ARM Linux8087aac2011-01-03 22:45:17 +00001631 waiting->phychan_hold--;
Linus Walleije8689e62010-09-28 15:57:37 +02001632 waiting->state = PL08X_CHAN_RUNNING;
1633 waiting->waiting = NULL;
1634 pl08x_issue_pending(&waiting->chan);
1635 break;
1636 }
1637 }
1638 }
1639
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001640 spin_unlock_irqrestore(&plchan->lock, flags);
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001641
Russell King - ARM Linux3d992e12011-01-03 22:44:16 +00001642 if (txd) {
1643 dma_async_tx_callback callback = txd->tx.callback;
1644 void *callback_param = txd->tx.callback_param;
1645
1646 /* Don't try to unmap buffers on slave channels */
1647 if (!plchan->slave)
1648 pl08x_unmap_buffers(txd);
1649
1650 /* Free the descriptor */
1651 spin_lock_irqsave(&plchan->lock, flags);
1652 pl08x_free_txd(pl08x, txd);
1653 spin_unlock_irqrestore(&plchan->lock, flags);
1654
1655 /* Callback to signal completion */
1656 if (callback)
1657 callback(callback_param);
1658 }
Linus Walleije8689e62010-09-28 15:57:37 +02001659}
1660
1661static irqreturn_t pl08x_irq(int irq, void *dev)
1662{
1663 struct pl08x_driver_data *pl08x = dev;
1664 u32 mask = 0;
1665 u32 val;
1666 int i;
1667
1668 val = readl(pl08x->base + PL080_ERR_STATUS);
1669 if (val) {
1670 /*
1671 * An error interrupt (on one or more channels)
1672 */
1673 dev_err(&pl08x->adev->dev,
1674 "%s error interrupt, register value 0x%08x\n",
1675 __func__, val);
1676 /*
1677 * Simply clear ALL PL08X error interrupts,
1678 * regardless of channel and cause
1679 * FIXME: should be 0x00000003 on PL081 really.
1680 */
1681 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1682 }
1683 val = readl(pl08x->base + PL080_INT_STATUS);
1684 for (i = 0; i < pl08x->vd->channels; i++) {
1685 if ((1 << i) & val) {
1686 /* Locate physical channel */
1687 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1688 struct pl08x_dma_chan *plchan = phychan->serving;
1689
1690 /* Schedule tasklet on this channel */
1691 tasklet_schedule(&plchan->tasklet);
1692
1693 mask |= (1 << i);
1694 }
1695 }
1696 /*
1697 * Clear only the terminal interrupts on channels we processed
1698 */
1699 writel(mask, pl08x->base + PL080_TC_CLEAR);
1700
1701 return mask ? IRQ_HANDLED : IRQ_NONE;
1702}
1703
1704/*
1705 * Initialise the DMAC memcpy/slave channels.
1706 * Make a local wrapper to hold required data
1707 */
1708static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1709 struct dma_device *dmadev,
1710 unsigned int channels,
1711 bool slave)
1712{
1713 struct pl08x_dma_chan *chan;
1714 int i;
1715
1716 INIT_LIST_HEAD(&dmadev->channels);
1717 /*
1718 * Register as many many memcpy as we have physical channels,
1719 * we won't always be able to use all but the code will have
1720 * to cope with that situation.
1721 */
1722 for (i = 0; i < channels; i++) {
1723 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
1724 if (!chan) {
1725 dev_err(&pl08x->adev->dev,
1726 "%s no memory for channel\n", __func__);
1727 return -ENOMEM;
1728 }
1729
1730 chan->host = pl08x;
1731 chan->state = PL08X_CHAN_IDLE;
1732
1733 if (slave) {
1734 chan->slave = true;
1735 chan->name = pl08x->pd->slave_channels[i].bus_id;
1736 chan->cd = &pl08x->pd->slave_channels[i];
1737 } else {
1738 chan->cd = &pl08x->pd->memcpy_channel;
1739 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1740 if (!chan->name) {
1741 kfree(chan);
1742 return -ENOMEM;
1743 }
1744 }
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001745 if (chan->cd->circular_buffer) {
1746 dev_err(&pl08x->adev->dev,
1747 "channel %s: circular buffers not supported\n",
1748 chan->name);
1749 kfree(chan);
1750 continue;
1751 }
Linus Walleije8689e62010-09-28 15:57:37 +02001752 dev_info(&pl08x->adev->dev,
1753 "initialize virtual channel \"%s\"\n",
1754 chan->name);
1755
1756 chan->chan.device = dmadev;
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001757 chan->chan.cookie = 0;
1758 chan->lc = 0;
Linus Walleije8689e62010-09-28 15:57:37 +02001759
1760 spin_lock_init(&chan->lock);
Russell King - ARM Linux15c17232011-01-03 22:44:36 +00001761 INIT_LIST_HEAD(&chan->pend_list);
Linus Walleije8689e62010-09-28 15:57:37 +02001762 tasklet_init(&chan->tasklet, pl08x_tasklet,
1763 (unsigned long) chan);
1764
1765 list_add_tail(&chan->chan.device_node, &dmadev->channels);
1766 }
1767 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1768 i, slave ? "slave" : "memcpy");
1769 return i;
1770}
1771
1772static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1773{
1774 struct pl08x_dma_chan *chan = NULL;
1775 struct pl08x_dma_chan *next;
1776
1777 list_for_each_entry_safe(chan,
1778 next, &dmadev->channels, chan.device_node) {
1779 list_del(&chan->chan.device_node);
1780 kfree(chan);
1781 }
1782}
1783
1784#ifdef CONFIG_DEBUG_FS
1785static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1786{
1787 switch (state) {
1788 case PL08X_CHAN_IDLE:
1789 return "idle";
1790 case PL08X_CHAN_RUNNING:
1791 return "running";
1792 case PL08X_CHAN_PAUSED:
1793 return "paused";
1794 case PL08X_CHAN_WAITING:
1795 return "waiting";
1796 default:
1797 break;
1798 }
1799 return "UNKNOWN STATE";
1800}
1801
1802static int pl08x_debugfs_show(struct seq_file *s, void *data)
1803{
1804 struct pl08x_driver_data *pl08x = s->private;
1805 struct pl08x_dma_chan *chan;
1806 struct pl08x_phy_chan *ch;
1807 unsigned long flags;
1808 int i;
1809
1810 seq_printf(s, "PL08x physical channels:\n");
1811 seq_printf(s, "CHANNEL:\tUSER:\n");
1812 seq_printf(s, "--------\t-----\n");
1813 for (i = 0; i < pl08x->vd->channels; i++) {
1814 struct pl08x_dma_chan *virt_chan;
1815
1816 ch = &pl08x->phy_chans[i];
1817
1818 spin_lock_irqsave(&ch->lock, flags);
1819 virt_chan = ch->serving;
1820
1821 seq_printf(s, "%d\t\t%s\n",
1822 ch->id, virt_chan ? virt_chan->name : "(none)");
1823
1824 spin_unlock_irqrestore(&ch->lock, flags);
1825 }
1826
1827 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
1828 seq_printf(s, "CHANNEL:\tSTATE:\n");
1829 seq_printf(s, "--------\t------\n");
1830 list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001831 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001832 pl08x_state_str(chan->state));
1833 }
1834
1835 seq_printf(s, "\nPL08x virtual slave channels:\n");
1836 seq_printf(s, "CHANNEL:\tSTATE:\n");
1837 seq_printf(s, "--------\t------\n");
1838 list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001839 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001840 pl08x_state_str(chan->state));
1841 }
1842
1843 return 0;
1844}
1845
1846static int pl08x_debugfs_open(struct inode *inode, struct file *file)
1847{
1848 return single_open(file, pl08x_debugfs_show, inode->i_private);
1849}
1850
1851static const struct file_operations pl08x_debugfs_operations = {
1852 .open = pl08x_debugfs_open,
1853 .read = seq_read,
1854 .llseek = seq_lseek,
1855 .release = single_release,
1856};
1857
1858static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1859{
1860 /* Expose a simple debugfs interface to view all clocks */
1861 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
1862 NULL, pl08x,
1863 &pl08x_debugfs_operations);
1864}
1865
1866#else
1867static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1868{
1869}
1870#endif
1871
1872static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
1873{
1874 struct pl08x_driver_data *pl08x;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +00001875 const struct vendor_data *vd = id->data;
Linus Walleije8689e62010-09-28 15:57:37 +02001876 int ret = 0;
1877 int i;
1878
1879 ret = amba_request_regions(adev, NULL);
1880 if (ret)
1881 return ret;
1882
1883 /* Create the driver state holder */
1884 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
1885 if (!pl08x) {
1886 ret = -ENOMEM;
1887 goto out_no_pl08x;
1888 }
1889
1890 /* Initialize memcpy engine */
1891 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1892 pl08x->memcpy.dev = &adev->dev;
1893 pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1894 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1895 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1896 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1897 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
1898 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
1899 pl08x->memcpy.device_control = pl08x_control;
1900
1901 /* Initialize slave engine */
1902 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
1903 pl08x->slave.dev = &adev->dev;
1904 pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1905 pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
1906 pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1907 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
1908 pl08x->slave.device_issue_pending = pl08x_issue_pending;
1909 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1910 pl08x->slave.device_control = pl08x_control;
1911
1912 /* Get the platform data */
1913 pl08x->pd = dev_get_platdata(&adev->dev);
1914 if (!pl08x->pd) {
1915 dev_err(&adev->dev, "no platform data supplied\n");
1916 goto out_no_platdata;
1917 }
1918
1919 /* Assign useful pointers to the driver state */
1920 pl08x->adev = adev;
1921 pl08x->vd = vd;
1922
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001923 /* By default, AHB1 only. If dualmaster, from platform */
1924 pl08x->lli_buses = PL08X_AHB1;
1925 pl08x->mem_buses = PL08X_AHB1;
1926 if (pl08x->vd->dualmaster) {
1927 pl08x->lli_buses = pl08x->pd->lli_buses;
1928 pl08x->mem_buses = pl08x->pd->mem_buses;
1929 }
1930
Linus Walleije8689e62010-09-28 15:57:37 +02001931 /* A DMA memory pool for LLIs, align on 1-byte boundary */
1932 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
1933 PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
1934 if (!pl08x->pool) {
1935 ret = -ENOMEM;
1936 goto out_no_lli_pool;
1937 }
1938
1939 spin_lock_init(&pl08x->lock);
1940
1941 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
1942 if (!pl08x->base) {
1943 ret = -ENOMEM;
1944 goto out_no_ioremap;
1945 }
1946
1947 /* Turn on the PL08x */
1948 pl08x_ensure_on(pl08x);
1949
1950 /*
1951 * Attach the interrupt handler
1952 */
1953 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1954 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
1955
1956 ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001957 DRIVER_NAME, pl08x);
Linus Walleije8689e62010-09-28 15:57:37 +02001958 if (ret) {
1959 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
1960 __func__, adev->irq[0]);
1961 goto out_no_irq;
1962 }
1963
1964 /* Initialize physical channels */
1965 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
1966 GFP_KERNEL);
1967 if (!pl08x->phy_chans) {
1968 dev_err(&adev->dev, "%s failed to allocate "
1969 "physical channel holders\n",
1970 __func__);
1971 goto out_no_phychans;
1972 }
1973
1974 for (i = 0; i < vd->channels; i++) {
1975 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
1976
1977 ch->id = i;
1978 ch->base = pl08x->base + PL080_Cx_BASE(i);
1979 spin_lock_init(&ch->lock);
1980 ch->serving = NULL;
1981 ch->signal = -1;
1982 dev_info(&adev->dev,
1983 "physical channel %d is %s\n", i,
1984 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1985 }
1986
1987 /* Register as many memcpy channels as there are physical channels */
1988 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
1989 pl08x->vd->channels, false);
1990 if (ret <= 0) {
1991 dev_warn(&pl08x->adev->dev,
1992 "%s failed to enumerate memcpy channels - %d\n",
1993 __func__, ret);
1994 goto out_no_memcpy;
1995 }
1996 pl08x->memcpy.chancnt = ret;
1997
1998 /* Register slave channels */
1999 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
2000 pl08x->pd->num_slave_channels,
2001 true);
2002 if (ret <= 0) {
2003 dev_warn(&pl08x->adev->dev,
2004 "%s failed to enumerate slave channels - %d\n",
2005 __func__, ret);
2006 goto out_no_slave;
2007 }
2008 pl08x->slave.chancnt = ret;
2009
2010 ret = dma_async_device_register(&pl08x->memcpy);
2011 if (ret) {
2012 dev_warn(&pl08x->adev->dev,
2013 "%s failed to register memcpy as an async device - %d\n",
2014 __func__, ret);
2015 goto out_no_memcpy_reg;
2016 }
2017
2018 ret = dma_async_device_register(&pl08x->slave);
2019 if (ret) {
2020 dev_warn(&pl08x->adev->dev,
2021 "%s failed to register slave as an async device - %d\n",
2022 __func__, ret);
2023 goto out_no_slave_reg;
2024 }
2025
2026 amba_set_drvdata(adev, pl08x);
2027 init_pl08x_debugfs(pl08x);
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00002028 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
2029 amba_part(adev), amba_rev(adev),
2030 (unsigned long long)adev->res.start, adev->irq[0]);
Linus Walleije8689e62010-09-28 15:57:37 +02002031 return 0;
2032
2033out_no_slave_reg:
2034 dma_async_device_unregister(&pl08x->memcpy);
2035out_no_memcpy_reg:
2036 pl08x_free_virtual_channels(&pl08x->slave);
2037out_no_slave:
2038 pl08x_free_virtual_channels(&pl08x->memcpy);
2039out_no_memcpy:
2040 kfree(pl08x->phy_chans);
2041out_no_phychans:
2042 free_irq(adev->irq[0], pl08x);
2043out_no_irq:
2044 iounmap(pl08x->base);
2045out_no_ioremap:
2046 dma_pool_destroy(pl08x->pool);
2047out_no_lli_pool:
2048out_no_platdata:
2049 kfree(pl08x);
2050out_no_pl08x:
2051 amba_release_regions(adev);
2052 return ret;
2053}
2054
2055/* PL080 has 8 channels and the PL080 have just 2 */
2056static struct vendor_data vendor_pl080 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002057 .channels = 8,
2058 .dualmaster = true,
2059};
2060
2061static struct vendor_data vendor_pl081 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002062 .channels = 2,
2063 .dualmaster = false,
2064};
2065
2066static struct amba_id pl08x_ids[] = {
2067 /* PL080 */
2068 {
2069 .id = 0x00041080,
2070 .mask = 0x000fffff,
2071 .data = &vendor_pl080,
2072 },
2073 /* PL081 */
2074 {
2075 .id = 0x00041081,
2076 .mask = 0x000fffff,
2077 .data = &vendor_pl081,
2078 },
2079 /* Nomadik 8815 PL080 variant */
2080 {
2081 .id = 0x00280880,
2082 .mask = 0x00ffffff,
2083 .data = &vendor_pl080,
2084 },
2085 { 0, 0 },
2086};
2087
2088static struct amba_driver pl08x_amba_driver = {
2089 .drv.name = DRIVER_NAME,
2090 .id_table = pl08x_ids,
2091 .probe = pl08x_probe,
2092};
2093
2094static int __init pl08x_init(void)
2095{
2096 int retval;
2097 retval = amba_driver_register(&pl08x_amba_driver);
2098 if (retval)
2099 printk(KERN_WARNING DRIVER_NAME
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00002100 "failed to register as an AMBA device (%d)\n",
Linus Walleije8689e62010-09-28 15:57:37 +02002101 retval);
2102 return retval;
2103}
2104subsys_initcall(pl08x_init);