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