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