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