blob: f0a29885cb838d714cfa8d9f0bd9dfe886fabf6c [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
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;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +0000136 const struct vendor_data *vd;
Linus Walleije8689e62010-09-28 15:57:37 +0200137 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
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000162/* Maximum times we call dma_pool_alloc on this pool without freeing */
Linus Walleije8689e62010-09-28 15:57:37 +0200163#define PL08X_MAX_ALLOCS 0x40
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000164#define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
Linus Walleije8689e62010-09-28 15:57:37 +0200165#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
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000187 * The next LLI pointer and the configuration interrupt bit have
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000188 * been set when the LLIs were constructed. Poke them into the hardware
189 * and start the transfer.
Linus Walleije8689e62010-09-28 15:57:37 +0200190 */
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000191static void pl08x_start_txd(struct pl08x_dma_chan *plchan,
192 struct pl08x_txd *txd)
Linus Walleije8689e62010-09-28 15:57:37 +0200193{
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000194 struct pl08x_driver_data *pl08x = plchan->host;
Linus Walleije8689e62010-09-28 15:57:37 +0200195 struct pl08x_phy_chan *phychan = plchan->phychan;
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000196 struct pl08x_lli *lli = &txd->llis_va[0];
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000197 u32 val;
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000198
199 plchan->at = txd;
Linus Walleije8689e62010-09-28 15:57:37 +0200200
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000201 /* Wait for channel inactive */
202 while (pl08x_phy_channel_busy(phychan))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000203 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200204
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000205 dev_vdbg(&pl08x->adev->dev,
206 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000207 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
208 phychan->id, lli->src, lli->dst, lli->lli, lli->cctl,
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000209 txd->ccfg);
Linus Walleije8689e62010-09-28 15:57:37 +0200210
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000211 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);
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000215 writel(txd->ccfg, phychan->base + PL080_CH_CONFIG);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000216
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))
220 cpu_relax();
221
222 /* Do not access config register until channel shows as inactive */
223 val = readl(phychan->base + PL080_CH_CONFIG);
224 while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
225 val = readl(phychan->base + PL080_CH_CONFIG);
226
227 writel(val | PL080_CONFIG_ENABLE, phychan->base + PL080_CH_CONFIG);
Linus Walleije8689e62010-09-28 15:57:37 +0200228}
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))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000251 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200252}
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;
Linus Walleije8689e62010-09-28 15:57:37 +0200302 struct pl08x_txd *txd;
303 unsigned long flags;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000304 size_t bytes = 0;
Linus Walleije8689e62010-09-28 15:57:37 +0200305
306 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +0200307 ch = plchan->phychan;
308 txd = plchan->at;
309
310 /*
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000311 * Follow the LLIs to get the number of remaining
312 * bytes in the currently active transaction.
Linus Walleije8689e62010-09-28 15:57:37 +0200313 */
314 if (ch && txd) {
Russell King - ARM Linux4c0df6a2011-01-03 22:36:50 +0000315 u32 clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200316
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000317 /* First get the remaining bytes in the active transfer */
Linus Walleije8689e62010-09-28 15:57:37 +0200318 bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
319
320 if (clli) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000321 struct pl08x_lli *llis_va = txd->llis_va;
322 dma_addr_t llis_bus = txd->llis_bus;
323 int index;
Linus Walleije8689e62010-09-28 15:57:37 +0200324
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000325 BUG_ON(clli < llis_bus || clli >= llis_bus +
326 sizeof(struct pl08x_lli) * MAX_NUM_TSFR_LLIS);
Linus Walleije8689e62010-09-28 15:57:37 +0200327
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000328 /*
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);
336
Linus Walleije8689e62010-09-28 15:57:37 +0200337 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000338 * A LLI pointer of 0 terminates the LLI list
Linus Walleije8689e62010-09-28 15:57:37 +0200339 */
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000340 if (!llis_va[index].lli)
341 break;
Linus Walleije8689e62010-09-28 15:57:37 +0200342 }
343 }
344 }
345
346 /* Sum up all queued transactions */
347 if (!list_empty(&plchan->desc_list)) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000348 struct pl08x_txd *txdi;
Linus Walleije8689e62010-09-28 15:57:37 +0200349 list_for_each_entry(txdi, &plchan->desc_list, node) {
350 bytes += txdi->len;
351 }
Linus Walleije8689e62010-09-28 15:57:37 +0200352 }
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,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000436 size_t tsize)
Linus Walleije8689e62010-09-28 15:57:37 +0200437{
438 u32 retbits = cctl;
439
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000440 /* Remove all src, dst and transfer size bits */
Linus Walleije8689e62010-09-28 15:57:37 +0200441 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 */
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +0000485static void pl08x_choose_master_bus(struct pl08x_bus_data *src_bus,
Linus Walleije8689e62010-09-28 15:57:37 +0200486 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 */
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +0000520static int pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
Linus Walleije8689e62010-09-28 15:57:37 +0200521 struct pl08x_txd *txd, int num_llis, int len,
522 u32 cctl, u32 *remainder)
523{
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000524 struct pl08x_lli *llis_va = txd->llis_va;
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000525 dma_addr_t llis_bus = txd->llis_bus;
Linus Walleije8689e62010-09-28 15:57:37 +0200526
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
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000542 llis_va[num_llis].lli = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli);
Linus Walleije8689e62010-09-28 15:57:37 +0200543
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
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000549 BUG_ON(*remainder < len);
550
Linus Walleije8689e62010-09-28 15:57:37 +0200551 *remainder -= len;
552
553 return num_llis + 1;
554}
555
556/*
557 * Return number of bytes to fill to boundary, or len
558 */
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000559static inline size_t pl08x_pre_boundary(u32 addr, size_t len)
Linus Walleije8689e62010-09-28 15:57:37 +0200560{
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{
580 struct pl08x_channel_data *cd = txd->cd;
581 struct pl08x_bus_data *mbus, *sbus;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000582 size_t remainder;
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
598 /*
599 * Initialize bus values for this transfer
600 * from the passed optimal values
601 */
602 if (!cd) {
603 dev_err(&pl08x->adev->dev, "%s no channel data\n", __func__);
604 return 0;
605 }
606
607 /* Get the default CCTL from the platform data */
608 cctl = cd->cctl;
609
610 /*
611 * On the PL080 we have two bus masters and we
612 * should select one for source and one for
613 * destination. We try to use AHB2 for the
614 * bus which does not increment (typically the
615 * peripheral) else we just choose something.
616 */
617 cctl &= ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
618 if (pl08x->vd->dualmaster) {
619 if (cctl & PL080_CONTROL_SRC_INCR)
620 /* Source increments, use AHB2 for destination */
621 cctl |= PL080_CONTROL_DST_AHB2;
622 else if (cctl & PL080_CONTROL_DST_INCR)
623 /* Destination increments, use AHB2 for source */
624 cctl |= PL080_CONTROL_SRC_AHB2;
625 else
626 /* Just pick something, source AHB1 dest AHB2 */
627 cctl |= PL080_CONTROL_DST_AHB2;
628 }
629
630 /* Find maximum width of the source bus */
631 txd->srcbus.maxwidth =
632 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
633 PL080_CONTROL_SWIDTH_SHIFT);
634
635 /* Find maximum width of the destination bus */
636 txd->dstbus.maxwidth =
637 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
638 PL080_CONTROL_DWIDTH_SHIFT);
639
640 /* Set up the bus widths to the maximum */
641 txd->srcbus.buswidth = txd->srcbus.maxwidth;
642 txd->dstbus.buswidth = txd->dstbus.maxwidth;
643 dev_vdbg(&pl08x->adev->dev,
644 "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
645 __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
646
647
648 /*
649 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
650 */
651 max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
652 PL080_CONTROL_TRANSFER_SIZE_MASK;
653 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000654 "%s max bytes per lli = %zu\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200655 __func__, max_bytes_per_lli);
656
657 /* We need to count this down to zero */
658 remainder = txd->len;
659 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000660 "%s remainder = %zu\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200661 __func__, remainder);
662
663 /*
664 * Choose bus to align to
665 * - prefers destination bus if both available
666 * - if fixed address on one bus chooses other
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000667 * - modifies cctl to choose an appropriate master
Linus Walleije8689e62010-09-28 15:57:37 +0200668 */
669 pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
670 &mbus, &sbus, cctl);
671
672
673 /*
674 * The lowest bit of the LLI register
675 * is also used to indicate which master to
676 * use for reading the LLIs.
677 */
678
679 if (txd->len < mbus->buswidth) {
680 /*
681 * Less than a bus width available
682 * - send as single bytes
683 */
684 while (remainder) {
685 dev_vdbg(&pl08x->adev->dev,
686 "%s single byte LLIs for a transfer of "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000687 "less than a bus width (remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200688 __func__, remainder);
689 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
690 num_llis =
691 pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,
692 cctl, &remainder);
693 total_bytes++;
694 }
695 } else {
696 /*
697 * Make one byte LLIs until master bus is aligned
698 * - slave will then be aligned also
699 */
700 while ((mbus->addr) % (mbus->buswidth)) {
701 dev_vdbg(&pl08x->adev->dev,
702 "%s adjustment lli for less than bus width "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000703 "(remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200704 __func__, remainder);
705 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
706 num_llis = pl08x_fill_lli_for_desc
707 (pl08x, txd, num_llis, 1, cctl, &remainder);
708 total_bytes++;
709 }
710
711 /*
712 * Master now aligned
713 * - if slave is not then we must set its width down
714 */
715 if (sbus->addr % sbus->buswidth) {
716 dev_dbg(&pl08x->adev->dev,
717 "%s set down bus width to one byte\n",
718 __func__);
719
720 sbus->buswidth = 1;
721 }
722
723 /*
724 * Make largest possible LLIs until less than one bus
725 * width left
726 */
727 while (remainder > (mbus->buswidth - 1)) {
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000728 size_t lli_len, target_len, tsize, odd_bytes;
Linus Walleije8689e62010-09-28 15:57:37 +0200729
730 /*
731 * If enough left try to send max possible,
732 * otherwise try to send the remainder
733 */
734 target_len = remainder;
735 if (remainder > max_bytes_per_lli)
736 target_len = max_bytes_per_lli;
737
738 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000739 * Set bus lengths for incrementing buses
Linus Walleije8689e62010-09-28 15:57:37 +0200740 * to number of bytes which fill to next memory
741 * boundary
742 */
743 if (cctl & PL080_CONTROL_SRC_INCR)
744 txd->srcbus.fill_bytes =
745 pl08x_pre_boundary(
746 txd->srcbus.addr,
747 remainder);
748 else
749 txd->srcbus.fill_bytes =
750 max_bytes_per_lli;
751
752 if (cctl & PL080_CONTROL_DST_INCR)
753 txd->dstbus.fill_bytes =
754 pl08x_pre_boundary(
755 txd->dstbus.addr,
756 remainder);
757 else
758 txd->dstbus.fill_bytes =
759 max_bytes_per_lli;
760
761 /*
762 * Find the nearest
763 */
764 lli_len = min(txd->srcbus.fill_bytes,
765 txd->dstbus.fill_bytes);
766
767 BUG_ON(lli_len > remainder);
768
769 if (lli_len <= 0) {
770 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000771 "%s lli_len is %zu, <= 0\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200772 __func__, lli_len);
773 return 0;
774 }
775
776 if (lli_len == target_len) {
777 /*
778 * Can send what we wanted
779 */
780 /*
781 * Maintain alignment
782 */
783 lli_len = (lli_len/mbus->buswidth) *
784 mbus->buswidth;
785 odd_bytes = 0;
786 } else {
787 /*
788 * So now we know how many bytes to transfer
789 * to get to the nearest boundary
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000790 * The next LLI will past the boundary
Linus Walleije8689e62010-09-28 15:57:37 +0200791 * - however we may be working to a boundary
792 * on the slave bus
793 * We need to ensure the master stays aligned
794 */
795 odd_bytes = lli_len % mbus->buswidth;
796 /*
797 * - and that we are working in multiples
798 * of the bus widths
799 */
800 lli_len -= odd_bytes;
801
802 }
803
804 if (lli_len) {
805 /*
806 * Check against minimum bus alignment:
807 * Calculate actual transfer size in relation
808 * to bus width an get a maximum remainder of
809 * the smallest bus width - 1
810 */
811 /* FIXME: use round_down()? */
812 tsize = lli_len / min(mbus->buswidth,
813 sbus->buswidth);
814 lli_len = tsize * min(mbus->buswidth,
815 sbus->buswidth);
816
817 if (target_len != lli_len) {
818 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000819 "%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 +0200820 __func__, target_len, lli_len, txd->len);
821 }
822
823 cctl = pl08x_cctl_bits(cctl,
824 txd->srcbus.buswidth,
825 txd->dstbus.buswidth,
826 tsize);
827
828 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000829 "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200830 __func__, lli_len, remainder);
831 num_llis = pl08x_fill_lli_for_desc(pl08x, txd,
832 num_llis, lli_len, cctl,
833 &remainder);
834 total_bytes += lli_len;
835 }
836
837
838 if (odd_bytes) {
839 /*
840 * Creep past the boundary,
841 * maintaining master alignment
842 */
843 int j;
844 for (j = 0; (j < mbus->buswidth)
845 && (remainder); j++) {
846 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
847 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000848 "%s align with boundary, single byte (remain 0x%08zx)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200849 __func__, remainder);
850 num_llis =
851 pl08x_fill_lli_for_desc(pl08x,
852 txd, num_llis, 1,
853 cctl, &remainder);
854 total_bytes++;
855 }
856 }
857 }
858
859 /*
860 * Send any odd bytes
861 */
Linus Walleije8689e62010-09-28 15:57:37 +0200862 while (remainder) {
863 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
864 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000865 "%s align with boundary, single odd byte (remain %zu)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200866 __func__, remainder);
867 num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,
868 1, cctl, &remainder);
869 total_bytes++;
870 }
871 }
872 if (total_bytes != txd->len) {
873 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000874 "%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 +0200875 __func__, total_bytes, txd->len);
876 return 0;
877 }
878
879 if (num_llis >= MAX_NUM_TSFR_LLIS) {
880 dev_err(&pl08x->adev->dev,
881 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
882 __func__, (u32) MAX_NUM_TSFR_LLIS);
883 return 0;
884 }
Linus Walleije8689e62010-09-28 15:57:37 +0200885
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000886 llis_va = txd->llis_va;
887 /*
888 * The final LLI terminates the LLI.
889 */
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000890 llis_va[num_llis - 1].lli = 0;
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000891 /*
892 * The final LLI element shall also fire an interrupt
893 */
894 llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
Linus Walleije8689e62010-09-28 15:57:37 +0200895
Linus Walleije8689e62010-09-28 15:57:37 +0200896#ifdef VERBOSE_DEBUG
897 {
898 int i;
899
900 for (i = 0; i < num_llis; i++) {
901 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000902 "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200903 i,
904 &llis_va[i],
905 llis_va[i].src,
906 llis_va[i].dst,
907 llis_va[i].cctl,
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000908 llis_va[i].lli
Linus Walleije8689e62010-09-28 15:57:37 +0200909 );
910 }
911 }
912#endif
913
914 return num_llis;
915}
916
917/* You should call this with the struct pl08x lock held */
918static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
919 struct pl08x_txd *txd)
920{
Linus Walleije8689e62010-09-28 15:57:37 +0200921 /* Free the LLI */
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000922 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
Linus Walleije8689e62010-09-28 15:57:37 +0200923
924 pl08x->pool_ctr--;
925
926 kfree(txd);
927}
928
929static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
930 struct pl08x_dma_chan *plchan)
931{
932 struct pl08x_txd *txdi = NULL;
933 struct pl08x_txd *next;
934
935 if (!list_empty(&plchan->desc_list)) {
936 list_for_each_entry_safe(txdi,
937 next, &plchan->desc_list, node) {
938 list_del(&txdi->node);
939 pl08x_free_txd(pl08x, txdi);
940 }
941
942 }
943}
944
945/*
946 * The DMA ENGINE API
947 */
948static int pl08x_alloc_chan_resources(struct dma_chan *chan)
949{
950 return 0;
951}
952
953static void pl08x_free_chan_resources(struct dma_chan *chan)
954{
955}
956
957/*
958 * This should be called with the channel plchan->lock held
959 */
960static int prep_phy_channel(struct pl08x_dma_chan *plchan,
961 struct pl08x_txd *txd)
962{
963 struct pl08x_driver_data *pl08x = plchan->host;
964 struct pl08x_phy_chan *ch;
965 int ret;
966
967 /* Check if we already have a channel */
968 if (plchan->phychan)
969 return 0;
970
971 ch = pl08x_get_phy_channel(pl08x, plchan);
972 if (!ch) {
973 /* No physical channel available, cope with it */
974 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
975 return -EBUSY;
976 }
977
978 /*
979 * OK we have a physical channel: for memcpy() this is all we
980 * need, but for slaves the physical signals may be muxed!
981 * Can the platform allow us to use this channel?
982 */
983 if (plchan->slave &&
984 ch->signal < 0 &&
985 pl08x->pd->get_signal) {
986 ret = pl08x->pd->get_signal(plchan);
987 if (ret < 0) {
988 dev_dbg(&pl08x->adev->dev,
989 "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
990 ch->id, plchan->name);
991 /* Release physical channel & return */
992 pl08x_put_phy_channel(pl08x, ch);
993 return -EBUSY;
994 }
995 ch->signal = ret;
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000996
997 /* Assign the flow control signal to this channel */
998 if (txd->direction == DMA_TO_DEVICE)
999 txd->ccfg |= ch->signal << PL080_CONFIG_DST_SEL_SHIFT;
1000 else if (txd->direction == DMA_FROM_DEVICE)
1001 txd->ccfg |= ch->signal << PL080_CONFIG_SRC_SEL_SHIFT;
Linus Walleije8689e62010-09-28 15:57:37 +02001002 }
1003
1004 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
1005 ch->id,
1006 ch->signal,
1007 plchan->name);
1008
1009 plchan->phychan = ch;
1010
1011 return 0;
1012}
1013
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001014static void release_phy_channel(struct pl08x_dma_chan *plchan)
1015{
1016 struct pl08x_driver_data *pl08x = plchan->host;
1017
1018 if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
1019 pl08x->pd->put_signal(plchan);
1020 plchan->phychan->signal = -1;
1021 }
1022 pl08x_put_phy_channel(pl08x, plchan->phychan);
1023 plchan->phychan = NULL;
1024}
1025
Linus Walleije8689e62010-09-28 15:57:37 +02001026static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
1027{
1028 struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
1029
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001030 plchan->chan.cookie += 1;
1031 if (plchan->chan.cookie < 0)
1032 plchan->chan.cookie = 1;
1033 tx->cookie = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001034 /* This unlock follows the lock in the prep() function */
1035 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1036
1037 return tx->cookie;
1038}
1039
1040static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1041 struct dma_chan *chan, unsigned long flags)
1042{
1043 struct dma_async_tx_descriptor *retval = NULL;
1044
1045 return retval;
1046}
1047
1048/*
1049 * Code accessing dma_async_is_complete() in a tight loop
1050 * may give problems - could schedule where indicated.
1051 * If slaves are relying on interrupts to signal completion this
1052 * function must not be called with interrupts disabled
1053 */
1054static enum dma_status
1055pl08x_dma_tx_status(struct dma_chan *chan,
1056 dma_cookie_t cookie,
1057 struct dma_tx_state *txstate)
1058{
1059 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1060 dma_cookie_t last_used;
1061 dma_cookie_t last_complete;
1062 enum dma_status ret;
1063 u32 bytesleft = 0;
1064
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001065 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001066 last_complete = plchan->lc;
1067
1068 ret = dma_async_is_complete(cookie, last_complete, last_used);
1069 if (ret == DMA_SUCCESS) {
1070 dma_set_tx_state(txstate, last_complete, last_used, 0);
1071 return ret;
1072 }
1073
1074 /*
1075 * schedule(); could be inserted here
1076 */
1077
1078 /*
1079 * This cookie not complete yet
1080 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001081 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001082 last_complete = plchan->lc;
1083
1084 /* Get number of bytes left in the active transactions and queue */
1085 bytesleft = pl08x_getbytes_chan(plchan);
1086
1087 dma_set_tx_state(txstate, last_complete, last_used,
1088 bytesleft);
1089
1090 if (plchan->state == PL08X_CHAN_PAUSED)
1091 return DMA_PAUSED;
1092
1093 /* Whether waiting or running, we're in progress */
1094 return DMA_IN_PROGRESS;
1095}
1096
1097/* PrimeCell DMA extension */
1098struct burst_table {
1099 int burstwords;
1100 u32 reg;
1101};
1102
1103static const struct burst_table burst_sizes[] = {
1104 {
1105 .burstwords = 256,
1106 .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
1107 (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
1108 },
1109 {
1110 .burstwords = 128,
1111 .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
1112 (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
1113 },
1114 {
1115 .burstwords = 64,
1116 .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
1117 (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
1118 },
1119 {
1120 .burstwords = 32,
1121 .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
1122 (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
1123 },
1124 {
1125 .burstwords = 16,
1126 .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
1127 (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
1128 },
1129 {
1130 .burstwords = 8,
1131 .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
1132 (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
1133 },
1134 {
1135 .burstwords = 4,
1136 .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
1137 (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
1138 },
1139 {
1140 .burstwords = 1,
1141 .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1142 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
1143 },
1144};
1145
1146static void dma_set_runtime_config(struct dma_chan *chan,
1147 struct dma_slave_config *config)
1148{
1149 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1150 struct pl08x_driver_data *pl08x = plchan->host;
1151 struct pl08x_channel_data *cd = plchan->cd;
1152 enum dma_slave_buswidth addr_width;
1153 u32 maxburst;
1154 u32 cctl = 0;
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001155 int i;
Linus Walleije8689e62010-09-28 15:57:37 +02001156
1157 /* Transfer direction */
1158 plchan->runtime_direction = config->direction;
1159 if (config->direction == DMA_TO_DEVICE) {
1160 plchan->runtime_addr = config->dst_addr;
1161 cctl |= PL080_CONTROL_SRC_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001162 addr_width = config->dst_addr_width;
1163 maxburst = config->dst_maxburst;
1164 } else if (config->direction == DMA_FROM_DEVICE) {
1165 plchan->runtime_addr = config->src_addr;
1166 cctl |= PL080_CONTROL_DST_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001167 addr_width = config->src_addr_width;
1168 maxburst = config->src_maxburst;
1169 } else {
1170 dev_err(&pl08x->adev->dev,
1171 "bad runtime_config: alien transfer direction\n");
1172 return;
1173 }
1174
1175 switch (addr_width) {
1176 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1177 cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1178 (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
1179 break;
1180 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1181 cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1182 (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
1183 break;
1184 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1185 cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1186 (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
1187 break;
1188 default:
1189 dev_err(&pl08x->adev->dev,
1190 "bad runtime_config: alien address width\n");
1191 return;
1192 }
1193
1194 /*
1195 * Now decide on a maxburst:
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001196 * If this channel will only request single transfers, set this
1197 * down to ONE element. Also select one element if no maxburst
1198 * is specified.
Linus Walleije8689e62010-09-28 15:57:37 +02001199 */
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001200 if (plchan->cd->single || maxburst == 0) {
Linus Walleije8689e62010-09-28 15:57:37 +02001201 cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1202 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
1203 } else {
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001204 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
Linus Walleije8689e62010-09-28 15:57:37 +02001205 if (burst_sizes[i].burstwords <= maxburst)
1206 break;
Linus Walleije8689e62010-09-28 15:57:37 +02001207 cctl |= burst_sizes[i].reg;
1208 }
1209
1210 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1211 cctl &= ~PL080_CONTROL_PROT_MASK;
1212 cctl |= PL080_CONTROL_PROT_SYS;
1213
1214 /* Modify the default channel data to fit PrimeCell request */
1215 cd->cctl = cctl;
Linus Walleije8689e62010-09-28 15:57:37 +02001216
1217 dev_dbg(&pl08x->adev->dev,
1218 "configured channel %s (%s) for %s, data width %d, "
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001219 "maxburst %d words, LE, CCTL=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +02001220 dma_chan_name(chan), plchan->name,
1221 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
1222 addr_width,
1223 maxburst,
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001224 cctl);
Linus Walleije8689e62010-09-28 15:57:37 +02001225}
1226
1227/*
1228 * Slave transactions callback to the slave device to allow
1229 * synchronization of slave DMA signals with the DMAC enable
1230 */
1231static void pl08x_issue_pending(struct dma_chan *chan)
1232{
1233 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
Linus Walleije8689e62010-09-28 15:57:37 +02001234 unsigned long flags;
1235
1236 spin_lock_irqsave(&plchan->lock, flags);
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001237 /* Something is already active, or we're waiting for a channel... */
1238 if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
1239 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001240 return;
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001241 }
Linus Walleije8689e62010-09-28 15:57:37 +02001242
1243 /* Take the first element in the queue and execute it */
1244 if (!list_empty(&plchan->desc_list)) {
1245 struct pl08x_txd *next;
1246
1247 next = list_first_entry(&plchan->desc_list,
1248 struct pl08x_txd,
1249 node);
1250 list_del(&next->node);
Linus Walleije8689e62010-09-28 15:57:37 +02001251 plchan->state = PL08X_CHAN_RUNNING;
1252
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001253 pl08x_start_txd(plchan, next);
Linus Walleije8689e62010-09-28 15:57:37 +02001254 }
1255
1256 spin_unlock_irqrestore(&plchan->lock, flags);
1257}
1258
1259static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1260 struct pl08x_txd *txd)
1261{
1262 int num_llis;
1263 struct pl08x_driver_data *pl08x = plchan->host;
1264 int ret;
1265
1266 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001267 if (!num_llis) {
1268 kfree(txd);
Linus Walleije8689e62010-09-28 15:57:37 +02001269 return -EINVAL;
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001270 }
Linus Walleije8689e62010-09-28 15:57:37 +02001271
1272 spin_lock_irqsave(&plchan->lock, plchan->lockflags);
1273
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001274 list_add_tail(&txd->node, &plchan->desc_list);
Linus Walleije8689e62010-09-28 15:57:37 +02001275
1276 /*
1277 * See if we already have a physical channel allocated,
1278 * else this is the time to try to get one.
1279 */
1280 ret = prep_phy_channel(plchan, txd);
1281 if (ret) {
1282 /*
1283 * No physical channel available, we will
1284 * stack up the memcpy channels until there is a channel
1285 * available to handle it whereas slave transfers may
1286 * have been denied due to platform channel muxing restrictions
1287 * and since there is no guarantee that this will ever be
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001288 * resolved, and since the signal must be acquired AFTER
1289 * acquiring the physical channel, we will let them be NACK:ed
Linus Walleije8689e62010-09-28 15:57:37 +02001290 * with -EBUSY here. The drivers can alway retry the prep()
1291 * call if they are eager on doing this using DMA.
1292 */
1293 if (plchan->slave) {
1294 pl08x_free_txd_list(pl08x, plchan);
1295 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1296 return -EBUSY;
1297 }
1298 /* Do this memcpy whenever there is a channel ready */
1299 plchan->state = PL08X_CHAN_WAITING;
1300 plchan->waiting = txd;
1301 } else
1302 /*
1303 * Else we're all set, paused and ready to roll,
1304 * status will switch to PL08X_CHAN_RUNNING when
1305 * we call issue_pending(). If there is something
1306 * running on the channel already we don't change
1307 * its state.
1308 */
1309 if (plchan->state == PL08X_CHAN_IDLE)
1310 plchan->state = PL08X_CHAN_PAUSED;
1311
1312 /*
1313 * Notice that we leave plchan->lock locked on purpose:
1314 * it will be unlocked in the subsequent tx_submit()
1315 * call. This is a consequence of the current API.
1316 */
1317
1318 return 0;
1319}
1320
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001321static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1322{
1323 struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
1324
1325 if (txd) {
1326 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
1327 txd->tx.tx_submit = pl08x_tx_submit;
1328 INIT_LIST_HEAD(&txd->node);
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001329
1330 /* Always enable error and terminal interrupts */
1331 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1332 PL080_CONFIG_TC_IRQ_MASK;
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001333 }
1334 return txd;
1335}
1336
Linus Walleije8689e62010-09-28 15:57:37 +02001337/*
1338 * Initialize a descriptor to be used by memcpy submit
1339 */
1340static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1341 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1342 size_t len, unsigned long flags)
1343{
1344 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1345 struct pl08x_driver_data *pl08x = plchan->host;
1346 struct pl08x_txd *txd;
1347 int ret;
1348
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001349 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001350 if (!txd) {
1351 dev_err(&pl08x->adev->dev,
1352 "%s no memory for descriptor\n", __func__);
1353 return NULL;
1354 }
1355
Linus Walleije8689e62010-09-28 15:57:37 +02001356 txd->direction = DMA_NONE;
1357 txd->srcbus.addr = src;
1358 txd->dstbus.addr = dest;
1359
1360 /* Set platform data for m2m */
1361 txd->cd = &pl08x->pd->memcpy_channel;
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001362 txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1363
Linus Walleije8689e62010-09-28 15:57:37 +02001364 /* Both to be incremented or the code will break */
1365 txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001366 txd->len = len;
1367
Linus Walleije8689e62010-09-28 15:57:37 +02001368 ret = pl08x_prep_channel_resources(plchan, txd);
1369 if (ret)
1370 return NULL;
1371 /*
1372 * NB: the channel lock is held at this point so tx_submit()
1373 * must be called in direct succession.
1374 */
1375
1376 return &txd->tx;
1377}
1378
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001379static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
Linus Walleije8689e62010-09-28 15:57:37 +02001380 struct dma_chan *chan, struct scatterlist *sgl,
1381 unsigned int sg_len, enum dma_data_direction direction,
1382 unsigned long flags)
1383{
1384 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1385 struct pl08x_driver_data *pl08x = plchan->host;
1386 struct pl08x_txd *txd;
1387 int ret;
1388
1389 /*
1390 * Current implementation ASSUMES only one sg
1391 */
1392 if (sg_len != 1) {
1393 dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
1394 __func__);
1395 BUG();
1396 }
1397
1398 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1399 __func__, sgl->length, plchan->name);
1400
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001401 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001402 if (!txd) {
1403 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1404 return NULL;
1405 }
1406
Linus Walleije8689e62010-09-28 15:57:37 +02001407 if (direction != plchan->runtime_direction)
1408 dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
1409 "the direction configured for the PrimeCell\n",
1410 __func__);
1411
1412 /*
1413 * Set up addresses, the PrimeCell configured address
1414 * will take precedence since this may configure the
1415 * channel target address dynamically at runtime.
1416 */
1417 txd->direction = direction;
1418 if (direction == DMA_TO_DEVICE) {
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001419 txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Linus Walleije8689e62010-09-28 15:57:37 +02001420 txd->srcbus.addr = sgl->dma_address;
1421 if (plchan->runtime_addr)
1422 txd->dstbus.addr = plchan->runtime_addr;
1423 else
1424 txd->dstbus.addr = plchan->cd->addr;
1425 } 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;
Linus Walleije8689e62010-09-28 15:57:37 +02001427 if (plchan->runtime_addr)
1428 txd->srcbus.addr = plchan->runtime_addr;
1429 else
1430 txd->srcbus.addr = plchan->cd->addr;
1431 txd->dstbus.addr = sgl->dma_address;
1432 } else {
1433 dev_err(&pl08x->adev->dev,
1434 "%s direction unsupported\n", __func__);
1435 return NULL;
1436 }
1437 txd->cd = plchan->cd;
Linus Walleije8689e62010-09-28 15:57:37 +02001438 txd->len = sgl->length;
Linus Walleije8689e62010-09-28 15:57:37 +02001439
1440 ret = pl08x_prep_channel_resources(plchan, txd);
1441 if (ret)
1442 return NULL;
1443 /*
1444 * NB: the channel lock is held at this point so tx_submit()
1445 * must be called in direct succession.
1446 */
1447
1448 return &txd->tx;
1449}
1450
1451static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1452 unsigned long arg)
1453{
1454 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1455 struct pl08x_driver_data *pl08x = plchan->host;
1456 unsigned long flags;
1457 int ret = 0;
1458
1459 /* Controls applicable to inactive channels */
1460 if (cmd == DMA_SLAVE_CONFIG) {
1461 dma_set_runtime_config(chan,
1462 (struct dma_slave_config *)
1463 arg);
1464 return 0;
1465 }
1466
1467 /*
1468 * Anything succeeds on channels with no physical allocation and
1469 * no queued transfers.
1470 */
1471 spin_lock_irqsave(&plchan->lock, flags);
1472 if (!plchan->phychan && !plchan->at) {
1473 spin_unlock_irqrestore(&plchan->lock, flags);
1474 return 0;
1475 }
1476
1477 switch (cmd) {
1478 case DMA_TERMINATE_ALL:
1479 plchan->state = PL08X_CHAN_IDLE;
1480
1481 if (plchan->phychan) {
1482 pl08x_stop_phy_chan(plchan->phychan);
1483
1484 /*
1485 * Mark physical channel as free and free any slave
1486 * signal
1487 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001488 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001489 }
Linus Walleije8689e62010-09-28 15:57:37 +02001490 /* Dequeue jobs and free LLIs */
1491 if (plchan->at) {
1492 pl08x_free_txd(pl08x, plchan->at);
1493 plchan->at = NULL;
1494 }
1495 /* Dequeue jobs not yet fired as well */
1496 pl08x_free_txd_list(pl08x, plchan);
1497 break;
1498 case DMA_PAUSE:
1499 pl08x_pause_phy_chan(plchan->phychan);
1500 plchan->state = PL08X_CHAN_PAUSED;
1501 break;
1502 case DMA_RESUME:
1503 pl08x_resume_phy_chan(plchan->phychan);
1504 plchan->state = PL08X_CHAN_RUNNING;
1505 break;
1506 default:
1507 /* Unknown command */
1508 ret = -ENXIO;
1509 break;
1510 }
1511
1512 spin_unlock_irqrestore(&plchan->lock, flags);
1513
1514 return ret;
1515}
1516
1517bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1518{
1519 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1520 char *name = chan_id;
1521
1522 /* Check that the channel is not taken! */
1523 if (!strcmp(plchan->name, name))
1524 return true;
1525
1526 return false;
1527}
1528
1529/*
1530 * Just check that the device is there and active
1531 * TODO: turn this bit on/off depending on the number of
1532 * physical channels actually used, if it is zero... well
1533 * shut it off. That will save some power. Cut the clock
1534 * at the same time.
1535 */
1536static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1537{
1538 u32 val;
1539
1540 val = readl(pl08x->base + PL080_CONFIG);
1541 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001542 /* We implicitly clear bit 1 and that means little-endian mode */
Linus Walleije8689e62010-09-28 15:57:37 +02001543 val |= PL080_CONFIG_ENABLE;
1544 writel(val, pl08x->base + PL080_CONFIG);
1545}
1546
1547static void pl08x_tasklet(unsigned long data)
1548{
1549 struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
Linus Walleije8689e62010-09-28 15:57:37 +02001550 struct pl08x_driver_data *pl08x = plchan->host;
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001551 unsigned long flags;
Linus Walleije8689e62010-09-28 15:57:37 +02001552
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001553 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001554
1555 if (plchan->at) {
1556 dma_async_tx_callback callback =
1557 plchan->at->tx.callback;
1558 void *callback_param =
1559 plchan->at->tx.callback_param;
1560
1561 /*
1562 * Update last completed
1563 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001564 plchan->lc = plchan->at->tx.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001565
1566 /*
1567 * Callback to signal completion
1568 */
1569 if (callback)
1570 callback(callback_param);
1571
1572 /*
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001573 * Free the descriptor
Linus Walleije8689e62010-09-28 15:57:37 +02001574 */
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001575 pl08x_free_txd(pl08x, plchan->at);
1576 plchan->at = NULL;
Linus Walleije8689e62010-09-28 15:57:37 +02001577 }
1578 /*
1579 * If a new descriptor is queued, set it up
1580 * plchan->at is NULL here
1581 */
1582 if (!list_empty(&plchan->desc_list)) {
1583 struct pl08x_txd *next;
1584
1585 next = list_first_entry(&plchan->desc_list,
1586 struct pl08x_txd,
1587 node);
1588 list_del(&next->node);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001589
1590 pl08x_start_txd(plchan, next);
Linus Walleije8689e62010-09-28 15:57:37 +02001591 } else {
1592 struct pl08x_dma_chan *waiting = NULL;
1593
1594 /*
1595 * No more jobs, so free up the physical channel
1596 * Free any allocated signal on slave transfers too
1597 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001598 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001599 plchan->state = PL08X_CHAN_IDLE;
1600
1601 /*
1602 * And NOW before anyone else can grab that free:d
1603 * up physical channel, see if there is some memcpy
1604 * pending that seriously needs to start because of
1605 * being stacked up while we were choking the
1606 * physical channels with data.
1607 */
1608 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1609 chan.device_node) {
1610 if (waiting->state == PL08X_CHAN_WAITING &&
1611 waiting->waiting != NULL) {
1612 int ret;
1613
1614 /* This should REALLY not fail now */
1615 ret = prep_phy_channel(waiting,
1616 waiting->waiting);
1617 BUG_ON(ret);
1618 waiting->state = PL08X_CHAN_RUNNING;
1619 waiting->waiting = NULL;
1620 pl08x_issue_pending(&waiting->chan);
1621 break;
1622 }
1623 }
1624 }
1625
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001626 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001627}
1628
1629static irqreturn_t pl08x_irq(int irq, void *dev)
1630{
1631 struct pl08x_driver_data *pl08x = dev;
1632 u32 mask = 0;
1633 u32 val;
1634 int i;
1635
1636 val = readl(pl08x->base + PL080_ERR_STATUS);
1637 if (val) {
1638 /*
1639 * An error interrupt (on one or more channels)
1640 */
1641 dev_err(&pl08x->adev->dev,
1642 "%s error interrupt, register value 0x%08x\n",
1643 __func__, val);
1644 /*
1645 * Simply clear ALL PL08X error interrupts,
1646 * regardless of channel and cause
1647 * FIXME: should be 0x00000003 on PL081 really.
1648 */
1649 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1650 }
1651 val = readl(pl08x->base + PL080_INT_STATUS);
1652 for (i = 0; i < pl08x->vd->channels; i++) {
1653 if ((1 << i) & val) {
1654 /* Locate physical channel */
1655 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1656 struct pl08x_dma_chan *plchan = phychan->serving;
1657
1658 /* Schedule tasklet on this channel */
1659 tasklet_schedule(&plchan->tasklet);
1660
1661 mask |= (1 << i);
1662 }
1663 }
1664 /*
1665 * Clear only the terminal interrupts on channels we processed
1666 */
1667 writel(mask, pl08x->base + PL080_TC_CLEAR);
1668
1669 return mask ? IRQ_HANDLED : IRQ_NONE;
1670}
1671
1672/*
1673 * Initialise the DMAC memcpy/slave channels.
1674 * Make a local wrapper to hold required data
1675 */
1676static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1677 struct dma_device *dmadev,
1678 unsigned int channels,
1679 bool slave)
1680{
1681 struct pl08x_dma_chan *chan;
1682 int i;
1683
1684 INIT_LIST_HEAD(&dmadev->channels);
1685 /*
1686 * Register as many many memcpy as we have physical channels,
1687 * we won't always be able to use all but the code will have
1688 * to cope with that situation.
1689 */
1690 for (i = 0; i < channels; i++) {
1691 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
1692 if (!chan) {
1693 dev_err(&pl08x->adev->dev,
1694 "%s no memory for channel\n", __func__);
1695 return -ENOMEM;
1696 }
1697
1698 chan->host = pl08x;
1699 chan->state = PL08X_CHAN_IDLE;
1700
1701 if (slave) {
1702 chan->slave = true;
1703 chan->name = pl08x->pd->slave_channels[i].bus_id;
1704 chan->cd = &pl08x->pd->slave_channels[i];
1705 } else {
1706 chan->cd = &pl08x->pd->memcpy_channel;
1707 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1708 if (!chan->name) {
1709 kfree(chan);
1710 return -ENOMEM;
1711 }
1712 }
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001713 if (chan->cd->circular_buffer) {
1714 dev_err(&pl08x->adev->dev,
1715 "channel %s: circular buffers not supported\n",
1716 chan->name);
1717 kfree(chan);
1718 continue;
1719 }
Linus Walleije8689e62010-09-28 15:57:37 +02001720 dev_info(&pl08x->adev->dev,
1721 "initialize virtual channel \"%s\"\n",
1722 chan->name);
1723
1724 chan->chan.device = dmadev;
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001725 chan->chan.cookie = 0;
1726 chan->lc = 0;
Linus Walleije8689e62010-09-28 15:57:37 +02001727
1728 spin_lock_init(&chan->lock);
1729 INIT_LIST_HEAD(&chan->desc_list);
1730 tasklet_init(&chan->tasklet, pl08x_tasklet,
1731 (unsigned long) chan);
1732
1733 list_add_tail(&chan->chan.device_node, &dmadev->channels);
1734 }
1735 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1736 i, slave ? "slave" : "memcpy");
1737 return i;
1738}
1739
1740static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1741{
1742 struct pl08x_dma_chan *chan = NULL;
1743 struct pl08x_dma_chan *next;
1744
1745 list_for_each_entry_safe(chan,
1746 next, &dmadev->channels, chan.device_node) {
1747 list_del(&chan->chan.device_node);
1748 kfree(chan);
1749 }
1750}
1751
1752#ifdef CONFIG_DEBUG_FS
1753static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1754{
1755 switch (state) {
1756 case PL08X_CHAN_IDLE:
1757 return "idle";
1758 case PL08X_CHAN_RUNNING:
1759 return "running";
1760 case PL08X_CHAN_PAUSED:
1761 return "paused";
1762 case PL08X_CHAN_WAITING:
1763 return "waiting";
1764 default:
1765 break;
1766 }
1767 return "UNKNOWN STATE";
1768}
1769
1770static int pl08x_debugfs_show(struct seq_file *s, void *data)
1771{
1772 struct pl08x_driver_data *pl08x = s->private;
1773 struct pl08x_dma_chan *chan;
1774 struct pl08x_phy_chan *ch;
1775 unsigned long flags;
1776 int i;
1777
1778 seq_printf(s, "PL08x physical channels:\n");
1779 seq_printf(s, "CHANNEL:\tUSER:\n");
1780 seq_printf(s, "--------\t-----\n");
1781 for (i = 0; i < pl08x->vd->channels; i++) {
1782 struct pl08x_dma_chan *virt_chan;
1783
1784 ch = &pl08x->phy_chans[i];
1785
1786 spin_lock_irqsave(&ch->lock, flags);
1787 virt_chan = ch->serving;
1788
1789 seq_printf(s, "%d\t\t%s\n",
1790 ch->id, virt_chan ? virt_chan->name : "(none)");
1791
1792 spin_unlock_irqrestore(&ch->lock, flags);
1793 }
1794
1795 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
1796 seq_printf(s, "CHANNEL:\tSTATE:\n");
1797 seq_printf(s, "--------\t------\n");
1798 list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001799 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001800 pl08x_state_str(chan->state));
1801 }
1802
1803 seq_printf(s, "\nPL08x virtual slave channels:\n");
1804 seq_printf(s, "CHANNEL:\tSTATE:\n");
1805 seq_printf(s, "--------\t------\n");
1806 list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001807 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001808 pl08x_state_str(chan->state));
1809 }
1810
1811 return 0;
1812}
1813
1814static int pl08x_debugfs_open(struct inode *inode, struct file *file)
1815{
1816 return single_open(file, pl08x_debugfs_show, inode->i_private);
1817}
1818
1819static const struct file_operations pl08x_debugfs_operations = {
1820 .open = pl08x_debugfs_open,
1821 .read = seq_read,
1822 .llseek = seq_lseek,
1823 .release = single_release,
1824};
1825
1826static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1827{
1828 /* Expose a simple debugfs interface to view all clocks */
1829 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
1830 NULL, pl08x,
1831 &pl08x_debugfs_operations);
1832}
1833
1834#else
1835static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1836{
1837}
1838#endif
1839
1840static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
1841{
1842 struct pl08x_driver_data *pl08x;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +00001843 const struct vendor_data *vd = id->data;
Linus Walleije8689e62010-09-28 15:57:37 +02001844 int ret = 0;
1845 int i;
1846
1847 ret = amba_request_regions(adev, NULL);
1848 if (ret)
1849 return ret;
1850
1851 /* Create the driver state holder */
1852 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
1853 if (!pl08x) {
1854 ret = -ENOMEM;
1855 goto out_no_pl08x;
1856 }
1857
1858 /* Initialize memcpy engine */
1859 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1860 pl08x->memcpy.dev = &adev->dev;
1861 pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1862 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1863 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1864 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1865 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
1866 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
1867 pl08x->memcpy.device_control = pl08x_control;
1868
1869 /* Initialize slave engine */
1870 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
1871 pl08x->slave.dev = &adev->dev;
1872 pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1873 pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
1874 pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1875 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
1876 pl08x->slave.device_issue_pending = pl08x_issue_pending;
1877 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1878 pl08x->slave.device_control = pl08x_control;
1879
1880 /* Get the platform data */
1881 pl08x->pd = dev_get_platdata(&adev->dev);
1882 if (!pl08x->pd) {
1883 dev_err(&adev->dev, "no platform data supplied\n");
1884 goto out_no_platdata;
1885 }
1886
1887 /* Assign useful pointers to the driver state */
1888 pl08x->adev = adev;
1889 pl08x->vd = vd;
1890
1891 /* A DMA memory pool for LLIs, align on 1-byte boundary */
1892 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
1893 PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
1894 if (!pl08x->pool) {
1895 ret = -ENOMEM;
1896 goto out_no_lli_pool;
1897 }
1898
1899 spin_lock_init(&pl08x->lock);
1900
1901 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
1902 if (!pl08x->base) {
1903 ret = -ENOMEM;
1904 goto out_no_ioremap;
1905 }
1906
1907 /* Turn on the PL08x */
1908 pl08x_ensure_on(pl08x);
1909
1910 /*
1911 * Attach the interrupt handler
1912 */
1913 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1914 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
1915
1916 ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001917 DRIVER_NAME, pl08x);
Linus Walleije8689e62010-09-28 15:57:37 +02001918 if (ret) {
1919 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
1920 __func__, adev->irq[0]);
1921 goto out_no_irq;
1922 }
1923
1924 /* Initialize physical channels */
1925 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
1926 GFP_KERNEL);
1927 if (!pl08x->phy_chans) {
1928 dev_err(&adev->dev, "%s failed to allocate "
1929 "physical channel holders\n",
1930 __func__);
1931 goto out_no_phychans;
1932 }
1933
1934 for (i = 0; i < vd->channels; i++) {
1935 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
1936
1937 ch->id = i;
1938 ch->base = pl08x->base + PL080_Cx_BASE(i);
1939 spin_lock_init(&ch->lock);
1940 ch->serving = NULL;
1941 ch->signal = -1;
1942 dev_info(&adev->dev,
1943 "physical channel %d is %s\n", i,
1944 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1945 }
1946
1947 /* Register as many memcpy channels as there are physical channels */
1948 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
1949 pl08x->vd->channels, false);
1950 if (ret <= 0) {
1951 dev_warn(&pl08x->adev->dev,
1952 "%s failed to enumerate memcpy channels - %d\n",
1953 __func__, ret);
1954 goto out_no_memcpy;
1955 }
1956 pl08x->memcpy.chancnt = ret;
1957
1958 /* Register slave channels */
1959 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
1960 pl08x->pd->num_slave_channels,
1961 true);
1962 if (ret <= 0) {
1963 dev_warn(&pl08x->adev->dev,
1964 "%s failed to enumerate slave channels - %d\n",
1965 __func__, ret);
1966 goto out_no_slave;
1967 }
1968 pl08x->slave.chancnt = ret;
1969
1970 ret = dma_async_device_register(&pl08x->memcpy);
1971 if (ret) {
1972 dev_warn(&pl08x->adev->dev,
1973 "%s failed to register memcpy as an async device - %d\n",
1974 __func__, ret);
1975 goto out_no_memcpy_reg;
1976 }
1977
1978 ret = dma_async_device_register(&pl08x->slave);
1979 if (ret) {
1980 dev_warn(&pl08x->adev->dev,
1981 "%s failed to register slave as an async device - %d\n",
1982 __func__, ret);
1983 goto out_no_slave_reg;
1984 }
1985
1986 amba_set_drvdata(adev, pl08x);
1987 init_pl08x_debugfs(pl08x);
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001988 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
1989 amba_part(adev), amba_rev(adev),
1990 (unsigned long long)adev->res.start, adev->irq[0]);
Linus Walleije8689e62010-09-28 15:57:37 +02001991 return 0;
1992
1993out_no_slave_reg:
1994 dma_async_device_unregister(&pl08x->memcpy);
1995out_no_memcpy_reg:
1996 pl08x_free_virtual_channels(&pl08x->slave);
1997out_no_slave:
1998 pl08x_free_virtual_channels(&pl08x->memcpy);
1999out_no_memcpy:
2000 kfree(pl08x->phy_chans);
2001out_no_phychans:
2002 free_irq(adev->irq[0], pl08x);
2003out_no_irq:
2004 iounmap(pl08x->base);
2005out_no_ioremap:
2006 dma_pool_destroy(pl08x->pool);
2007out_no_lli_pool:
2008out_no_platdata:
2009 kfree(pl08x);
2010out_no_pl08x:
2011 amba_release_regions(adev);
2012 return ret;
2013}
2014
2015/* PL080 has 8 channels and the PL080 have just 2 */
2016static struct vendor_data vendor_pl080 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002017 .channels = 8,
2018 .dualmaster = true,
2019};
2020
2021static struct vendor_data vendor_pl081 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002022 .channels = 2,
2023 .dualmaster = false,
2024};
2025
2026static struct amba_id pl08x_ids[] = {
2027 /* PL080 */
2028 {
2029 .id = 0x00041080,
2030 .mask = 0x000fffff,
2031 .data = &vendor_pl080,
2032 },
2033 /* PL081 */
2034 {
2035 .id = 0x00041081,
2036 .mask = 0x000fffff,
2037 .data = &vendor_pl081,
2038 },
2039 /* Nomadik 8815 PL080 variant */
2040 {
2041 .id = 0x00280880,
2042 .mask = 0x00ffffff,
2043 .data = &vendor_pl080,
2044 },
2045 { 0, 0 },
2046};
2047
2048static struct amba_driver pl08x_amba_driver = {
2049 .drv.name = DRIVER_NAME,
2050 .id_table = pl08x_ids,
2051 .probe = pl08x_probe,
2052};
2053
2054static int __init pl08x_init(void)
2055{
2056 int retval;
2057 retval = amba_driver_register(&pl08x_amba_driver);
2058 if (retval)
2059 printk(KERN_WARNING DRIVER_NAME
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00002060 "failed to register as an AMBA device (%d)\n",
Linus Walleije8689e62010-09-28 15:57:37 +02002061 retval);
2062 return retval;
2063}
2064subsys_initcall(pl08x_init);