blob: 760b71eec84c9fcf1987da6c34c607e351efe353 [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 */
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000110struct pl08x_lli {
Linus Walleije8689e62010-09-28 15:57:37 +0200111 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;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +0000135 const struct vendor_data *vd;
Linus Walleije8689e62010-09-28 15:57:37 +0200136 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
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000163#define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
Linus Walleije8689e62010-09-28 15:57:37 +0200164#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))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000194 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200195
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))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000258 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200259
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))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000291 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200292}
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) {
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000357 struct pl08x_lli *llis_va = txd->llis_va;
358 struct pl08x_lli *llis_bus = (struct pl08x_lli *) txd->llis_bus;
Russell King - ARM Linux4c0df6a2011-01-03 22:36:50 +0000359 u32 clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200360
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{
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000561 struct pl08x_lli *llis_va = txd->llis_va;
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000562 dma_addr_t llis_bus = txd->llis_bus;
Linus Walleije8689e62010-09-28 15:57:37 +0200563
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
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000579 llis_va[num_llis].next = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli);
Linus Walleije8689e62010-09-28 15:57:37 +0200580
581 if (cctl & PL080_CONTROL_SRC_INCR)
582 txd->srcbus.addr += len;
583 if (cctl & PL080_CONTROL_DST_INCR)
584 txd->dstbus.addr += len;
585
586 *remainder -= len;
587
588 return num_llis + 1;
589}
590
591/*
592 * Return number of bytes to fill to boundary, or len
593 */
594static inline u32 pl08x_pre_boundary(u32 addr, u32 len)
595{
596 u32 boundary;
597
598 boundary = ((addr >> PL08X_BOUNDARY_SHIFT) + 1)
599 << PL08X_BOUNDARY_SHIFT;
600
601 if (boundary < addr + len)
602 return boundary - addr;
603 else
604 return len;
605}
606
607/*
608 * This fills in the table of LLIs for the transfer descriptor
609 * Note that we assume we never have to change the burst sizes
610 * Return 0 for error
611 */
612static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
613 struct pl08x_txd *txd)
614{
615 struct pl08x_channel_data *cd = txd->cd;
616 struct pl08x_bus_data *mbus, *sbus;
617 u32 remainder;
618 int num_llis = 0;
619 u32 cctl;
620 int max_bytes_per_lli;
621 int total_bytes = 0;
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000622 struct pl08x_lli *llis_va;
Linus Walleije8689e62010-09-28 15:57:37 +0200623
Linus Walleije8689e62010-09-28 15:57:37 +0200624 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
625 &txd->llis_bus);
626 if (!txd->llis_va) {
627 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
628 return 0;
629 }
630
631 pl08x->pool_ctr++;
632
633 /*
634 * Initialize bus values for this transfer
635 * from the passed optimal values
636 */
637 if (!cd) {
638 dev_err(&pl08x->adev->dev, "%s no channel data\n", __func__);
639 return 0;
640 }
641
642 /* Get the default CCTL from the platform data */
643 cctl = cd->cctl;
644
645 /*
646 * On the PL080 we have two bus masters and we
647 * should select one for source and one for
648 * destination. We try to use AHB2 for the
649 * bus which does not increment (typically the
650 * peripheral) else we just choose something.
651 */
652 cctl &= ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
653 if (pl08x->vd->dualmaster) {
654 if (cctl & PL080_CONTROL_SRC_INCR)
655 /* Source increments, use AHB2 for destination */
656 cctl |= PL080_CONTROL_DST_AHB2;
657 else if (cctl & PL080_CONTROL_DST_INCR)
658 /* Destination increments, use AHB2 for source */
659 cctl |= PL080_CONTROL_SRC_AHB2;
660 else
661 /* Just pick something, source AHB1 dest AHB2 */
662 cctl |= PL080_CONTROL_DST_AHB2;
663 }
664
665 /* Find maximum width of the source bus */
666 txd->srcbus.maxwidth =
667 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
668 PL080_CONTROL_SWIDTH_SHIFT);
669
670 /* Find maximum width of the destination bus */
671 txd->dstbus.maxwidth =
672 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
673 PL080_CONTROL_DWIDTH_SHIFT);
674
675 /* Set up the bus widths to the maximum */
676 txd->srcbus.buswidth = txd->srcbus.maxwidth;
677 txd->dstbus.buswidth = txd->dstbus.maxwidth;
678 dev_vdbg(&pl08x->adev->dev,
679 "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
680 __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
681
682
683 /*
684 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
685 */
686 max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
687 PL080_CONTROL_TRANSFER_SIZE_MASK;
688 dev_vdbg(&pl08x->adev->dev,
689 "%s max bytes per lli = %d\n",
690 __func__, max_bytes_per_lli);
691
692 /* We need to count this down to zero */
693 remainder = txd->len;
694 dev_vdbg(&pl08x->adev->dev,
695 "%s remainder = %d\n",
696 __func__, remainder);
697
698 /*
699 * Choose bus to align to
700 * - prefers destination bus if both available
701 * - if fixed address on one bus chooses other
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000702 * - modifies cctl to choose an appropriate master
Linus Walleije8689e62010-09-28 15:57:37 +0200703 */
704 pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
705 &mbus, &sbus, cctl);
706
707
708 /*
709 * The lowest bit of the LLI register
710 * is also used to indicate which master to
711 * use for reading the LLIs.
712 */
713
714 if (txd->len < mbus->buswidth) {
715 /*
716 * Less than a bus width available
717 * - send as single bytes
718 */
719 while (remainder) {
720 dev_vdbg(&pl08x->adev->dev,
721 "%s single byte LLIs for a transfer of "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000722 "less than a bus width (remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200723 __func__, remainder);
724 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
725 num_llis =
726 pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,
727 cctl, &remainder);
728 total_bytes++;
729 }
730 } else {
731 /*
732 * Make one byte LLIs until master bus is aligned
733 * - slave will then be aligned also
734 */
735 while ((mbus->addr) % (mbus->buswidth)) {
736 dev_vdbg(&pl08x->adev->dev,
737 "%s adjustment lli for less than bus width "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000738 "(remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200739 __func__, remainder);
740 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
741 num_llis = pl08x_fill_lli_for_desc
742 (pl08x, txd, num_llis, 1, cctl, &remainder);
743 total_bytes++;
744 }
745
746 /*
747 * Master now aligned
748 * - if slave is not then we must set its width down
749 */
750 if (sbus->addr % sbus->buswidth) {
751 dev_dbg(&pl08x->adev->dev,
752 "%s set down bus width to one byte\n",
753 __func__);
754
755 sbus->buswidth = 1;
756 }
757
758 /*
759 * Make largest possible LLIs until less than one bus
760 * width left
761 */
762 while (remainder > (mbus->buswidth - 1)) {
763 int lli_len, target_len;
764 int tsize;
765 int odd_bytes;
766
767 /*
768 * If enough left try to send max possible,
769 * otherwise try to send the remainder
770 */
771 target_len = remainder;
772 if (remainder > max_bytes_per_lli)
773 target_len = max_bytes_per_lli;
774
775 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000776 * Set bus lengths for incrementing buses
Linus Walleije8689e62010-09-28 15:57:37 +0200777 * to number of bytes which fill to next memory
778 * boundary
779 */
780 if (cctl & PL080_CONTROL_SRC_INCR)
781 txd->srcbus.fill_bytes =
782 pl08x_pre_boundary(
783 txd->srcbus.addr,
784 remainder);
785 else
786 txd->srcbus.fill_bytes =
787 max_bytes_per_lli;
788
789 if (cctl & PL080_CONTROL_DST_INCR)
790 txd->dstbus.fill_bytes =
791 pl08x_pre_boundary(
792 txd->dstbus.addr,
793 remainder);
794 else
795 txd->dstbus.fill_bytes =
796 max_bytes_per_lli;
797
798 /*
799 * Find the nearest
800 */
801 lli_len = min(txd->srcbus.fill_bytes,
802 txd->dstbus.fill_bytes);
803
804 BUG_ON(lli_len > remainder);
805
806 if (lli_len <= 0) {
807 dev_err(&pl08x->adev->dev,
808 "%s lli_len is %d, <= 0\n",
809 __func__, lli_len);
810 return 0;
811 }
812
813 if (lli_len == target_len) {
814 /*
815 * Can send what we wanted
816 */
817 /*
818 * Maintain alignment
819 */
820 lli_len = (lli_len/mbus->buswidth) *
821 mbus->buswidth;
822 odd_bytes = 0;
823 } else {
824 /*
825 * So now we know how many bytes to transfer
826 * to get to the nearest boundary
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000827 * The next LLI will past the boundary
Linus Walleije8689e62010-09-28 15:57:37 +0200828 * - however we may be working to a boundary
829 * on the slave bus
830 * We need to ensure the master stays aligned
831 */
832 odd_bytes = lli_len % mbus->buswidth;
833 /*
834 * - and that we are working in multiples
835 * of the bus widths
836 */
837 lli_len -= odd_bytes;
838
839 }
840
841 if (lli_len) {
842 /*
843 * Check against minimum bus alignment:
844 * Calculate actual transfer size in relation
845 * to bus width an get a maximum remainder of
846 * the smallest bus width - 1
847 */
848 /* FIXME: use round_down()? */
849 tsize = lli_len / min(mbus->buswidth,
850 sbus->buswidth);
851 lli_len = tsize * min(mbus->buswidth,
852 sbus->buswidth);
853
854 if (target_len != lli_len) {
855 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000856 "%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 +0200857 __func__, target_len, lli_len, txd->len);
858 }
859
860 cctl = pl08x_cctl_bits(cctl,
861 txd->srcbus.buswidth,
862 txd->dstbus.buswidth,
863 tsize);
864
865 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000866 "%s fill lli with single lli chunk of size 0x%08x (remainder 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200867 __func__, lli_len, remainder);
868 num_llis = pl08x_fill_lli_for_desc(pl08x, txd,
869 num_llis, lli_len, cctl,
870 &remainder);
871 total_bytes += lli_len;
872 }
873
874
875 if (odd_bytes) {
876 /*
877 * Creep past the boundary,
878 * maintaining master alignment
879 */
880 int j;
881 for (j = 0; (j < mbus->buswidth)
882 && (remainder); j++) {
883 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
884 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000885 "%s align with boundary, single byte (remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200886 __func__, remainder);
887 num_llis =
888 pl08x_fill_lli_for_desc(pl08x,
889 txd, num_llis, 1,
890 cctl, &remainder);
891 total_bytes++;
892 }
893 }
894 }
895
896 /*
897 * Send any odd bytes
898 */
899 if (remainder < 0) {
900 dev_err(&pl08x->adev->dev, "%s remainder not fitted 0x%08x bytes\n",
901 __func__, remainder);
902 return 0;
903 }
904
905 while (remainder) {
906 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
907 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000908 "%s align with boundary, single odd byte (remain %d)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200909 __func__, remainder);
910 num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,
911 1, cctl, &remainder);
912 total_bytes++;
913 }
914 }
915 if (total_bytes != txd->len) {
916 dev_err(&pl08x->adev->dev,
917 "%s size of encoded lli:s don't match total txd, transferred 0x%08x from size 0x%08x\n",
918 __func__, total_bytes, txd->len);
919 return 0;
920 }
921
922 if (num_llis >= MAX_NUM_TSFR_LLIS) {
923 dev_err(&pl08x->adev->dev,
924 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
925 __func__, (u32) MAX_NUM_TSFR_LLIS);
926 return 0;
927 }
Linus Walleije8689e62010-09-28 15:57:37 +0200928
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000929 llis_va = txd->llis_va;
930 /*
931 * The final LLI terminates the LLI.
932 */
933 llis_va[num_llis - 1].next = 0;
934 /*
935 * The final LLI element shall also fire an interrupt
936 */
937 llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
Linus Walleije8689e62010-09-28 15:57:37 +0200938
939 /* Now store the channel register values */
940 txd->csrc = llis_va[0].src;
941 txd->cdst = llis_va[0].dst;
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000942 txd->clli = llis_va[0].next;
Linus Walleije8689e62010-09-28 15:57:37 +0200943 txd->cctl = llis_va[0].cctl;
944 /* ccfg will be set at physical channel allocation time */
945
946#ifdef VERBOSE_DEBUG
947 {
948 int i;
949
950 for (i = 0; i < num_llis; i++) {
951 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000952 "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200953 i,
954 &llis_va[i],
955 llis_va[i].src,
956 llis_va[i].dst,
957 llis_va[i].cctl,
958 llis_va[i].next
959 );
960 }
961 }
962#endif
963
964 return num_llis;
965}
966
967/* You should call this with the struct pl08x lock held */
968static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
969 struct pl08x_txd *txd)
970{
Linus Walleije8689e62010-09-28 15:57:37 +0200971 /* Free the LLI */
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000972 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
Linus Walleije8689e62010-09-28 15:57:37 +0200973
974 pl08x->pool_ctr--;
975
976 kfree(txd);
977}
978
979static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
980 struct pl08x_dma_chan *plchan)
981{
982 struct pl08x_txd *txdi = NULL;
983 struct pl08x_txd *next;
984
985 if (!list_empty(&plchan->desc_list)) {
986 list_for_each_entry_safe(txdi,
987 next, &plchan->desc_list, node) {
988 list_del(&txdi->node);
989 pl08x_free_txd(pl08x, txdi);
990 }
991
992 }
993}
994
995/*
996 * The DMA ENGINE API
997 */
998static int pl08x_alloc_chan_resources(struct dma_chan *chan)
999{
1000 return 0;
1001}
1002
1003static void pl08x_free_chan_resources(struct dma_chan *chan)
1004{
1005}
1006
1007/*
1008 * This should be called with the channel plchan->lock held
1009 */
1010static int prep_phy_channel(struct pl08x_dma_chan *plchan,
1011 struct pl08x_txd *txd)
1012{
1013 struct pl08x_driver_data *pl08x = plchan->host;
1014 struct pl08x_phy_chan *ch;
1015 int ret;
1016
1017 /* Check if we already have a channel */
1018 if (plchan->phychan)
1019 return 0;
1020
1021 ch = pl08x_get_phy_channel(pl08x, plchan);
1022 if (!ch) {
1023 /* No physical channel available, cope with it */
1024 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
1025 return -EBUSY;
1026 }
1027
1028 /*
1029 * OK we have a physical channel: for memcpy() this is all we
1030 * need, but for slaves the physical signals may be muxed!
1031 * Can the platform allow us to use this channel?
1032 */
1033 if (plchan->slave &&
1034 ch->signal < 0 &&
1035 pl08x->pd->get_signal) {
1036 ret = pl08x->pd->get_signal(plchan);
1037 if (ret < 0) {
1038 dev_dbg(&pl08x->adev->dev,
1039 "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
1040 ch->id, plchan->name);
1041 /* Release physical channel & return */
1042 pl08x_put_phy_channel(pl08x, ch);
1043 return -EBUSY;
1044 }
1045 ch->signal = ret;
1046 }
1047
1048 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
1049 ch->id,
1050 ch->signal,
1051 plchan->name);
1052
1053 plchan->phychan = ch;
1054
1055 return 0;
1056}
1057
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001058static void release_phy_channel(struct pl08x_dma_chan *plchan)
1059{
1060 struct pl08x_driver_data *pl08x = plchan->host;
1061
1062 if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
1063 pl08x->pd->put_signal(plchan);
1064 plchan->phychan->signal = -1;
1065 }
1066 pl08x_put_phy_channel(pl08x, plchan->phychan);
1067 plchan->phychan = NULL;
1068}
1069
Linus Walleije8689e62010-09-28 15:57:37 +02001070static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
1071{
1072 struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
1073
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001074 plchan->chan.cookie += 1;
1075 if (plchan->chan.cookie < 0)
1076 plchan->chan.cookie = 1;
1077 tx->cookie = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001078 /* This unlock follows the lock in the prep() function */
1079 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1080
1081 return tx->cookie;
1082}
1083
1084static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1085 struct dma_chan *chan, unsigned long flags)
1086{
1087 struct dma_async_tx_descriptor *retval = NULL;
1088
1089 return retval;
1090}
1091
1092/*
1093 * Code accessing dma_async_is_complete() in a tight loop
1094 * may give problems - could schedule where indicated.
1095 * If slaves are relying on interrupts to signal completion this
1096 * function must not be called with interrupts disabled
1097 */
1098static enum dma_status
1099pl08x_dma_tx_status(struct dma_chan *chan,
1100 dma_cookie_t cookie,
1101 struct dma_tx_state *txstate)
1102{
1103 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1104 dma_cookie_t last_used;
1105 dma_cookie_t last_complete;
1106 enum dma_status ret;
1107 u32 bytesleft = 0;
1108
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001109 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001110 last_complete = plchan->lc;
1111
1112 ret = dma_async_is_complete(cookie, last_complete, last_used);
1113 if (ret == DMA_SUCCESS) {
1114 dma_set_tx_state(txstate, last_complete, last_used, 0);
1115 return ret;
1116 }
1117
1118 /*
1119 * schedule(); could be inserted here
1120 */
1121
1122 /*
1123 * This cookie not complete yet
1124 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001125 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001126 last_complete = plchan->lc;
1127
1128 /* Get number of bytes left in the active transactions and queue */
1129 bytesleft = pl08x_getbytes_chan(plchan);
1130
1131 dma_set_tx_state(txstate, last_complete, last_used,
1132 bytesleft);
1133
1134 if (plchan->state == PL08X_CHAN_PAUSED)
1135 return DMA_PAUSED;
1136
1137 /* Whether waiting or running, we're in progress */
1138 return DMA_IN_PROGRESS;
1139}
1140
1141/* PrimeCell DMA extension */
1142struct burst_table {
1143 int burstwords;
1144 u32 reg;
1145};
1146
1147static const struct burst_table burst_sizes[] = {
1148 {
1149 .burstwords = 256,
1150 .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
1151 (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
1152 },
1153 {
1154 .burstwords = 128,
1155 .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
1156 (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
1157 },
1158 {
1159 .burstwords = 64,
1160 .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
1161 (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
1162 },
1163 {
1164 .burstwords = 32,
1165 .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
1166 (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
1167 },
1168 {
1169 .burstwords = 16,
1170 .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
1171 (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
1172 },
1173 {
1174 .burstwords = 8,
1175 .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
1176 (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
1177 },
1178 {
1179 .burstwords = 4,
1180 .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
1181 (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
1182 },
1183 {
1184 .burstwords = 1,
1185 .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1186 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
1187 },
1188};
1189
1190static void dma_set_runtime_config(struct dma_chan *chan,
1191 struct dma_slave_config *config)
1192{
1193 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1194 struct pl08x_driver_data *pl08x = plchan->host;
1195 struct pl08x_channel_data *cd = plchan->cd;
1196 enum dma_slave_buswidth addr_width;
1197 u32 maxburst;
1198 u32 cctl = 0;
1199 /* Mask out all except src and dst channel */
1200 u32 ccfg = cd->ccfg & 0x000003DEU;
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001201 int i;
Linus Walleije8689e62010-09-28 15:57:37 +02001202
1203 /* Transfer direction */
1204 plchan->runtime_direction = config->direction;
1205 if (config->direction == DMA_TO_DEVICE) {
1206 plchan->runtime_addr = config->dst_addr;
1207 cctl |= PL080_CONTROL_SRC_INCR;
1208 ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1209 addr_width = config->dst_addr_width;
1210 maxburst = config->dst_maxburst;
1211 } else if (config->direction == DMA_FROM_DEVICE) {
1212 plchan->runtime_addr = config->src_addr;
1213 cctl |= PL080_CONTROL_DST_INCR;
1214 ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1215 addr_width = config->src_addr_width;
1216 maxburst = config->src_maxburst;
1217 } else {
1218 dev_err(&pl08x->adev->dev,
1219 "bad runtime_config: alien transfer direction\n");
1220 return;
1221 }
1222
1223 switch (addr_width) {
1224 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1225 cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1226 (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
1227 break;
1228 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1229 cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1230 (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
1231 break;
1232 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1233 cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1234 (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
1235 break;
1236 default:
1237 dev_err(&pl08x->adev->dev,
1238 "bad runtime_config: alien address width\n");
1239 return;
1240 }
1241
1242 /*
1243 * Now decide on a maxburst:
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001244 * If this channel will only request single transfers, set this
1245 * down to ONE element. Also select one element if no maxburst
1246 * is specified.
Linus Walleije8689e62010-09-28 15:57:37 +02001247 */
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001248 if (plchan->cd->single || maxburst == 0) {
Linus Walleije8689e62010-09-28 15:57:37 +02001249 cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1250 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
1251 } else {
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001252 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
Linus Walleije8689e62010-09-28 15:57:37 +02001253 if (burst_sizes[i].burstwords <= maxburst)
1254 break;
Linus Walleije8689e62010-09-28 15:57:37 +02001255 cctl |= burst_sizes[i].reg;
1256 }
1257
1258 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1259 cctl &= ~PL080_CONTROL_PROT_MASK;
1260 cctl |= PL080_CONTROL_PROT_SYS;
1261
1262 /* Modify the default channel data to fit PrimeCell request */
1263 cd->cctl = cctl;
1264 cd->ccfg = ccfg;
1265
1266 dev_dbg(&pl08x->adev->dev,
1267 "configured channel %s (%s) for %s, data width %d, "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +00001268 "maxburst %d words, LE, CCTL=0x%08x, CCFG=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +02001269 dma_chan_name(chan), plchan->name,
1270 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
1271 addr_width,
1272 maxburst,
1273 cctl, ccfg);
1274}
1275
1276/*
1277 * Slave transactions callback to the slave device to allow
1278 * synchronization of slave DMA signals with the DMAC enable
1279 */
1280static void pl08x_issue_pending(struct dma_chan *chan)
1281{
1282 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1283 struct pl08x_driver_data *pl08x = plchan->host;
1284 unsigned long flags;
1285
1286 spin_lock_irqsave(&plchan->lock, flags);
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001287 /* Something is already active, or we're waiting for a channel... */
1288 if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
1289 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001290 return;
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001291 }
Linus Walleije8689e62010-09-28 15:57:37 +02001292
1293 /* Take the first element in the queue and execute it */
1294 if (!list_empty(&plchan->desc_list)) {
1295 struct pl08x_txd *next;
1296
1297 next = list_first_entry(&plchan->desc_list,
1298 struct pl08x_txd,
1299 node);
1300 list_del(&next->node);
1301 plchan->at = next;
1302 plchan->state = PL08X_CHAN_RUNNING;
1303
1304 /* Configure the physical channel for the active txd */
1305 pl08x_config_phychan_for_txd(plchan);
1306 pl08x_set_cregs(pl08x, plchan->phychan);
1307 pl08x_enable_phy_chan(pl08x, plchan->phychan);
1308 }
1309
1310 spin_unlock_irqrestore(&plchan->lock, flags);
1311}
1312
1313static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1314 struct pl08x_txd *txd)
1315{
1316 int num_llis;
1317 struct pl08x_driver_data *pl08x = plchan->host;
1318 int ret;
1319
1320 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001321 if (!num_llis) {
1322 kfree(txd);
Linus Walleije8689e62010-09-28 15:57:37 +02001323 return -EINVAL;
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001324 }
Linus Walleije8689e62010-09-28 15:57:37 +02001325
1326 spin_lock_irqsave(&plchan->lock, plchan->lockflags);
1327
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001328 list_add_tail(&txd->node, &plchan->desc_list);
Linus Walleije8689e62010-09-28 15:57:37 +02001329
1330 /*
1331 * See if we already have a physical channel allocated,
1332 * else this is the time to try to get one.
1333 */
1334 ret = prep_phy_channel(plchan, txd);
1335 if (ret) {
1336 /*
1337 * No physical channel available, we will
1338 * stack up the memcpy channels until there is a channel
1339 * available to handle it whereas slave transfers may
1340 * have been denied due to platform channel muxing restrictions
1341 * and since there is no guarantee that this will ever be
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001342 * resolved, and since the signal must be acquired AFTER
1343 * acquiring the physical channel, we will let them be NACK:ed
Linus Walleije8689e62010-09-28 15:57:37 +02001344 * with -EBUSY here. The drivers can alway retry the prep()
1345 * call if they are eager on doing this using DMA.
1346 */
1347 if (plchan->slave) {
1348 pl08x_free_txd_list(pl08x, plchan);
1349 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1350 return -EBUSY;
1351 }
1352 /* Do this memcpy whenever there is a channel ready */
1353 plchan->state = PL08X_CHAN_WAITING;
1354 plchan->waiting = txd;
1355 } else
1356 /*
1357 * Else we're all set, paused and ready to roll,
1358 * status will switch to PL08X_CHAN_RUNNING when
1359 * we call issue_pending(). If there is something
1360 * running on the channel already we don't change
1361 * its state.
1362 */
1363 if (plchan->state == PL08X_CHAN_IDLE)
1364 plchan->state = PL08X_CHAN_PAUSED;
1365
1366 /*
1367 * Notice that we leave plchan->lock locked on purpose:
1368 * it will be unlocked in the subsequent tx_submit()
1369 * call. This is a consequence of the current API.
1370 */
1371
1372 return 0;
1373}
1374
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001375static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1376{
1377 struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
1378
1379 if (txd) {
1380 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
1381 txd->tx.tx_submit = pl08x_tx_submit;
1382 INIT_LIST_HEAD(&txd->node);
1383 }
1384 return txd;
1385}
1386
Linus Walleije8689e62010-09-28 15:57:37 +02001387/*
1388 * Initialize a descriptor to be used by memcpy submit
1389 */
1390static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1391 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1392 size_t len, unsigned long flags)
1393{
1394 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1395 struct pl08x_driver_data *pl08x = plchan->host;
1396 struct pl08x_txd *txd;
1397 int ret;
1398
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001399 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001400 if (!txd) {
1401 dev_err(&pl08x->adev->dev,
1402 "%s no memory for descriptor\n", __func__);
1403 return NULL;
1404 }
1405
Linus Walleije8689e62010-09-28 15:57:37 +02001406 txd->direction = DMA_NONE;
1407 txd->srcbus.addr = src;
1408 txd->dstbus.addr = dest;
1409
1410 /* Set platform data for m2m */
1411 txd->cd = &pl08x->pd->memcpy_channel;
1412 /* Both to be incremented or the code will break */
1413 txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001414 txd->len = len;
1415
Linus Walleije8689e62010-09-28 15:57:37 +02001416 ret = pl08x_prep_channel_resources(plchan, txd);
1417 if (ret)
1418 return NULL;
1419 /*
1420 * NB: the channel lock is held at this point so tx_submit()
1421 * must be called in direct succession.
1422 */
1423
1424 return &txd->tx;
1425}
1426
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001427static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
Linus Walleije8689e62010-09-28 15:57:37 +02001428 struct dma_chan *chan, struct scatterlist *sgl,
1429 unsigned int sg_len, enum dma_data_direction direction,
1430 unsigned long flags)
1431{
1432 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1433 struct pl08x_driver_data *pl08x = plchan->host;
1434 struct pl08x_txd *txd;
1435 int ret;
1436
1437 /*
1438 * Current implementation ASSUMES only one sg
1439 */
1440 if (sg_len != 1) {
1441 dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
1442 __func__);
1443 BUG();
1444 }
1445
1446 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1447 __func__, sgl->length, plchan->name);
1448
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001449 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001450 if (!txd) {
1451 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1452 return NULL;
1453 }
1454
Linus Walleije8689e62010-09-28 15:57:37 +02001455 if (direction != plchan->runtime_direction)
1456 dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
1457 "the direction configured for the PrimeCell\n",
1458 __func__);
1459
1460 /*
1461 * Set up addresses, the PrimeCell configured address
1462 * will take precedence since this may configure the
1463 * channel target address dynamically at runtime.
1464 */
1465 txd->direction = direction;
1466 if (direction == DMA_TO_DEVICE) {
1467 txd->srcbus.addr = sgl->dma_address;
1468 if (plchan->runtime_addr)
1469 txd->dstbus.addr = plchan->runtime_addr;
1470 else
1471 txd->dstbus.addr = plchan->cd->addr;
1472 } else if (direction == DMA_FROM_DEVICE) {
1473 if (plchan->runtime_addr)
1474 txd->srcbus.addr = plchan->runtime_addr;
1475 else
1476 txd->srcbus.addr = plchan->cd->addr;
1477 txd->dstbus.addr = sgl->dma_address;
1478 } else {
1479 dev_err(&pl08x->adev->dev,
1480 "%s direction unsupported\n", __func__);
1481 return NULL;
1482 }
1483 txd->cd = plchan->cd;
Linus Walleije8689e62010-09-28 15:57:37 +02001484 txd->len = sgl->length;
Linus Walleije8689e62010-09-28 15:57:37 +02001485
1486 ret = pl08x_prep_channel_resources(plchan, txd);
1487 if (ret)
1488 return NULL;
1489 /*
1490 * NB: the channel lock is held at this point so tx_submit()
1491 * must be called in direct succession.
1492 */
1493
1494 return &txd->tx;
1495}
1496
1497static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1498 unsigned long arg)
1499{
1500 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1501 struct pl08x_driver_data *pl08x = plchan->host;
1502 unsigned long flags;
1503 int ret = 0;
1504
1505 /* Controls applicable to inactive channels */
1506 if (cmd == DMA_SLAVE_CONFIG) {
1507 dma_set_runtime_config(chan,
1508 (struct dma_slave_config *)
1509 arg);
1510 return 0;
1511 }
1512
1513 /*
1514 * Anything succeeds on channels with no physical allocation and
1515 * no queued transfers.
1516 */
1517 spin_lock_irqsave(&plchan->lock, flags);
1518 if (!plchan->phychan && !plchan->at) {
1519 spin_unlock_irqrestore(&plchan->lock, flags);
1520 return 0;
1521 }
1522
1523 switch (cmd) {
1524 case DMA_TERMINATE_ALL:
1525 plchan->state = PL08X_CHAN_IDLE;
1526
1527 if (plchan->phychan) {
1528 pl08x_stop_phy_chan(plchan->phychan);
1529
1530 /*
1531 * Mark physical channel as free and free any slave
1532 * signal
1533 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001534 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001535 }
Linus Walleije8689e62010-09-28 15:57:37 +02001536 /* Dequeue jobs and free LLIs */
1537 if (plchan->at) {
1538 pl08x_free_txd(pl08x, plchan->at);
1539 plchan->at = NULL;
1540 }
1541 /* Dequeue jobs not yet fired as well */
1542 pl08x_free_txd_list(pl08x, plchan);
1543 break;
1544 case DMA_PAUSE:
1545 pl08x_pause_phy_chan(plchan->phychan);
1546 plchan->state = PL08X_CHAN_PAUSED;
1547 break;
1548 case DMA_RESUME:
1549 pl08x_resume_phy_chan(plchan->phychan);
1550 plchan->state = PL08X_CHAN_RUNNING;
1551 break;
1552 default:
1553 /* Unknown command */
1554 ret = -ENXIO;
1555 break;
1556 }
1557
1558 spin_unlock_irqrestore(&plchan->lock, flags);
1559
1560 return ret;
1561}
1562
1563bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1564{
1565 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1566 char *name = chan_id;
1567
1568 /* Check that the channel is not taken! */
1569 if (!strcmp(plchan->name, name))
1570 return true;
1571
1572 return false;
1573}
1574
1575/*
1576 * Just check that the device is there and active
1577 * TODO: turn this bit on/off depending on the number of
1578 * physical channels actually used, if it is zero... well
1579 * shut it off. That will save some power. Cut the clock
1580 * at the same time.
1581 */
1582static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1583{
1584 u32 val;
1585
1586 val = readl(pl08x->base + PL080_CONFIG);
1587 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001588 /* We implicitly clear bit 1 and that means little-endian mode */
Linus Walleije8689e62010-09-28 15:57:37 +02001589 val |= PL080_CONFIG_ENABLE;
1590 writel(val, pl08x->base + PL080_CONFIG);
1591}
1592
1593static void pl08x_tasklet(unsigned long data)
1594{
1595 struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
Linus Walleije8689e62010-09-28 15:57:37 +02001596 struct pl08x_driver_data *pl08x = plchan->host;
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001597 unsigned long flags;
Linus Walleije8689e62010-09-28 15:57:37 +02001598
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001599 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001600
1601 if (plchan->at) {
1602 dma_async_tx_callback callback =
1603 plchan->at->tx.callback;
1604 void *callback_param =
1605 plchan->at->tx.callback_param;
1606
1607 /*
1608 * Update last completed
1609 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001610 plchan->lc = plchan->at->tx.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001611
1612 /*
1613 * Callback to signal completion
1614 */
1615 if (callback)
1616 callback(callback_param);
1617
1618 /*
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001619 * Free the descriptor
Linus Walleije8689e62010-09-28 15:57:37 +02001620 */
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001621 pl08x_free_txd(pl08x, plchan->at);
1622 plchan->at = NULL;
Linus Walleije8689e62010-09-28 15:57:37 +02001623 }
1624 /*
1625 * If a new descriptor is queued, set it up
1626 * plchan->at is NULL here
1627 */
1628 if (!list_empty(&plchan->desc_list)) {
1629 struct pl08x_txd *next;
1630
1631 next = list_first_entry(&plchan->desc_list,
1632 struct pl08x_txd,
1633 node);
1634 list_del(&next->node);
1635 plchan->at = next;
1636 /* Configure the physical channel for the next txd */
1637 pl08x_config_phychan_for_txd(plchan);
1638 pl08x_set_cregs(pl08x, plchan->phychan);
1639 pl08x_enable_phy_chan(pl08x, plchan->phychan);
1640 } else {
1641 struct pl08x_dma_chan *waiting = NULL;
1642
1643 /*
1644 * No more jobs, so free up the physical channel
1645 * Free any allocated signal on slave transfers too
1646 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001647 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001648 plchan->state = PL08X_CHAN_IDLE;
1649
1650 /*
1651 * And NOW before anyone else can grab that free:d
1652 * up physical channel, see if there is some memcpy
1653 * pending that seriously needs to start because of
1654 * being stacked up while we were choking the
1655 * physical channels with data.
1656 */
1657 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1658 chan.device_node) {
1659 if (waiting->state == PL08X_CHAN_WAITING &&
1660 waiting->waiting != NULL) {
1661 int ret;
1662
1663 /* This should REALLY not fail now */
1664 ret = prep_phy_channel(waiting,
1665 waiting->waiting);
1666 BUG_ON(ret);
1667 waiting->state = PL08X_CHAN_RUNNING;
1668 waiting->waiting = NULL;
1669 pl08x_issue_pending(&waiting->chan);
1670 break;
1671 }
1672 }
1673 }
1674
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001675 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001676}
1677
1678static irqreturn_t pl08x_irq(int irq, void *dev)
1679{
1680 struct pl08x_driver_data *pl08x = dev;
1681 u32 mask = 0;
1682 u32 val;
1683 int i;
1684
1685 val = readl(pl08x->base + PL080_ERR_STATUS);
1686 if (val) {
1687 /*
1688 * An error interrupt (on one or more channels)
1689 */
1690 dev_err(&pl08x->adev->dev,
1691 "%s error interrupt, register value 0x%08x\n",
1692 __func__, val);
1693 /*
1694 * Simply clear ALL PL08X error interrupts,
1695 * regardless of channel and cause
1696 * FIXME: should be 0x00000003 on PL081 really.
1697 */
1698 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1699 }
1700 val = readl(pl08x->base + PL080_INT_STATUS);
1701 for (i = 0; i < pl08x->vd->channels; i++) {
1702 if ((1 << i) & val) {
1703 /* Locate physical channel */
1704 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1705 struct pl08x_dma_chan *plchan = phychan->serving;
1706
1707 /* Schedule tasklet on this channel */
1708 tasklet_schedule(&plchan->tasklet);
1709
1710 mask |= (1 << i);
1711 }
1712 }
1713 /*
1714 * Clear only the terminal interrupts on channels we processed
1715 */
1716 writel(mask, pl08x->base + PL080_TC_CLEAR);
1717
1718 return mask ? IRQ_HANDLED : IRQ_NONE;
1719}
1720
1721/*
1722 * Initialise the DMAC memcpy/slave channels.
1723 * Make a local wrapper to hold required data
1724 */
1725static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1726 struct dma_device *dmadev,
1727 unsigned int channels,
1728 bool slave)
1729{
1730 struct pl08x_dma_chan *chan;
1731 int i;
1732
1733 INIT_LIST_HEAD(&dmadev->channels);
1734 /*
1735 * Register as many many memcpy as we have physical channels,
1736 * we won't always be able to use all but the code will have
1737 * to cope with that situation.
1738 */
1739 for (i = 0; i < channels; i++) {
1740 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
1741 if (!chan) {
1742 dev_err(&pl08x->adev->dev,
1743 "%s no memory for channel\n", __func__);
1744 return -ENOMEM;
1745 }
1746
1747 chan->host = pl08x;
1748 chan->state = PL08X_CHAN_IDLE;
1749
1750 if (slave) {
1751 chan->slave = true;
1752 chan->name = pl08x->pd->slave_channels[i].bus_id;
1753 chan->cd = &pl08x->pd->slave_channels[i];
1754 } else {
1755 chan->cd = &pl08x->pd->memcpy_channel;
1756 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1757 if (!chan->name) {
1758 kfree(chan);
1759 return -ENOMEM;
1760 }
1761 }
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001762 if (chan->cd->circular_buffer) {
1763 dev_err(&pl08x->adev->dev,
1764 "channel %s: circular buffers not supported\n",
1765 chan->name);
1766 kfree(chan);
1767 continue;
1768 }
Linus Walleije8689e62010-09-28 15:57:37 +02001769 dev_info(&pl08x->adev->dev,
1770 "initialize virtual channel \"%s\"\n",
1771 chan->name);
1772
1773 chan->chan.device = dmadev;
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001774 chan->chan.cookie = 0;
1775 chan->lc = 0;
Linus Walleije8689e62010-09-28 15:57:37 +02001776
1777 spin_lock_init(&chan->lock);
1778 INIT_LIST_HEAD(&chan->desc_list);
1779 tasklet_init(&chan->tasklet, pl08x_tasklet,
1780 (unsigned long) chan);
1781
1782 list_add_tail(&chan->chan.device_node, &dmadev->channels);
1783 }
1784 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1785 i, slave ? "slave" : "memcpy");
1786 return i;
1787}
1788
1789static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1790{
1791 struct pl08x_dma_chan *chan = NULL;
1792 struct pl08x_dma_chan *next;
1793
1794 list_for_each_entry_safe(chan,
1795 next, &dmadev->channels, chan.device_node) {
1796 list_del(&chan->chan.device_node);
1797 kfree(chan);
1798 }
1799}
1800
1801#ifdef CONFIG_DEBUG_FS
1802static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1803{
1804 switch (state) {
1805 case PL08X_CHAN_IDLE:
1806 return "idle";
1807 case PL08X_CHAN_RUNNING:
1808 return "running";
1809 case PL08X_CHAN_PAUSED:
1810 return "paused";
1811 case PL08X_CHAN_WAITING:
1812 return "waiting";
1813 default:
1814 break;
1815 }
1816 return "UNKNOWN STATE";
1817}
1818
1819static int pl08x_debugfs_show(struct seq_file *s, void *data)
1820{
1821 struct pl08x_driver_data *pl08x = s->private;
1822 struct pl08x_dma_chan *chan;
1823 struct pl08x_phy_chan *ch;
1824 unsigned long flags;
1825 int i;
1826
1827 seq_printf(s, "PL08x physical channels:\n");
1828 seq_printf(s, "CHANNEL:\tUSER:\n");
1829 seq_printf(s, "--------\t-----\n");
1830 for (i = 0; i < pl08x->vd->channels; i++) {
1831 struct pl08x_dma_chan *virt_chan;
1832
1833 ch = &pl08x->phy_chans[i];
1834
1835 spin_lock_irqsave(&ch->lock, flags);
1836 virt_chan = ch->serving;
1837
1838 seq_printf(s, "%d\t\t%s\n",
1839 ch->id, virt_chan ? virt_chan->name : "(none)");
1840
1841 spin_unlock_irqrestore(&ch->lock, flags);
1842 }
1843
1844 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
1845 seq_printf(s, "CHANNEL:\tSTATE:\n");
1846 seq_printf(s, "--------\t------\n");
1847 list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001848 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001849 pl08x_state_str(chan->state));
1850 }
1851
1852 seq_printf(s, "\nPL08x virtual slave channels:\n");
1853 seq_printf(s, "CHANNEL:\tSTATE:\n");
1854 seq_printf(s, "--------\t------\n");
1855 list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001856 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001857 pl08x_state_str(chan->state));
1858 }
1859
1860 return 0;
1861}
1862
1863static int pl08x_debugfs_open(struct inode *inode, struct file *file)
1864{
1865 return single_open(file, pl08x_debugfs_show, inode->i_private);
1866}
1867
1868static const struct file_operations pl08x_debugfs_operations = {
1869 .open = pl08x_debugfs_open,
1870 .read = seq_read,
1871 .llseek = seq_lseek,
1872 .release = single_release,
1873};
1874
1875static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1876{
1877 /* Expose a simple debugfs interface to view all clocks */
1878 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
1879 NULL, pl08x,
1880 &pl08x_debugfs_operations);
1881}
1882
1883#else
1884static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1885{
1886}
1887#endif
1888
1889static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
1890{
1891 struct pl08x_driver_data *pl08x;
Russell King - ARM Linuxf96ca9ec2011-01-03 22:35:08 +00001892 const struct vendor_data *vd = id->data;
Linus Walleije8689e62010-09-28 15:57:37 +02001893 int ret = 0;
1894 int i;
1895
1896 ret = amba_request_regions(adev, NULL);
1897 if (ret)
1898 return ret;
1899
1900 /* Create the driver state holder */
1901 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
1902 if (!pl08x) {
1903 ret = -ENOMEM;
1904 goto out_no_pl08x;
1905 }
1906
1907 /* Initialize memcpy engine */
1908 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1909 pl08x->memcpy.dev = &adev->dev;
1910 pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1911 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1912 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1913 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1914 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
1915 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
1916 pl08x->memcpy.device_control = pl08x_control;
1917
1918 /* Initialize slave engine */
1919 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
1920 pl08x->slave.dev = &adev->dev;
1921 pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1922 pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
1923 pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1924 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
1925 pl08x->slave.device_issue_pending = pl08x_issue_pending;
1926 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1927 pl08x->slave.device_control = pl08x_control;
1928
1929 /* Get the platform data */
1930 pl08x->pd = dev_get_platdata(&adev->dev);
1931 if (!pl08x->pd) {
1932 dev_err(&adev->dev, "no platform data supplied\n");
1933 goto out_no_platdata;
1934 }
1935
1936 /* Assign useful pointers to the driver state */
1937 pl08x->adev = adev;
1938 pl08x->vd = vd;
1939
1940 /* A DMA memory pool for LLIs, align on 1-byte boundary */
1941 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
1942 PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
1943 if (!pl08x->pool) {
1944 ret = -ENOMEM;
1945 goto out_no_lli_pool;
1946 }
1947
1948 spin_lock_init(&pl08x->lock);
1949
1950 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
1951 if (!pl08x->base) {
1952 ret = -ENOMEM;
1953 goto out_no_ioremap;
1954 }
1955
1956 /* Turn on the PL08x */
1957 pl08x_ensure_on(pl08x);
1958
1959 /*
1960 * Attach the interrupt handler
1961 */
1962 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1963 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
1964
1965 ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001966 DRIVER_NAME, pl08x);
Linus Walleije8689e62010-09-28 15:57:37 +02001967 if (ret) {
1968 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
1969 __func__, adev->irq[0]);
1970 goto out_no_irq;
1971 }
1972
1973 /* Initialize physical channels */
1974 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
1975 GFP_KERNEL);
1976 if (!pl08x->phy_chans) {
1977 dev_err(&adev->dev, "%s failed to allocate "
1978 "physical channel holders\n",
1979 __func__);
1980 goto out_no_phychans;
1981 }
1982
1983 for (i = 0; i < vd->channels; i++) {
1984 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
1985
1986 ch->id = i;
1987 ch->base = pl08x->base + PL080_Cx_BASE(i);
1988 spin_lock_init(&ch->lock);
1989 ch->serving = NULL;
1990 ch->signal = -1;
1991 dev_info(&adev->dev,
1992 "physical channel %d is %s\n", i,
1993 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1994 }
1995
1996 /* Register as many memcpy channels as there are physical channels */
1997 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
1998 pl08x->vd->channels, false);
1999 if (ret <= 0) {
2000 dev_warn(&pl08x->adev->dev,
2001 "%s failed to enumerate memcpy channels - %d\n",
2002 __func__, ret);
2003 goto out_no_memcpy;
2004 }
2005 pl08x->memcpy.chancnt = ret;
2006
2007 /* Register slave channels */
2008 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
2009 pl08x->pd->num_slave_channels,
2010 true);
2011 if (ret <= 0) {
2012 dev_warn(&pl08x->adev->dev,
2013 "%s failed to enumerate slave channels - %d\n",
2014 __func__, ret);
2015 goto out_no_slave;
2016 }
2017 pl08x->slave.chancnt = ret;
2018
2019 ret = dma_async_device_register(&pl08x->memcpy);
2020 if (ret) {
2021 dev_warn(&pl08x->adev->dev,
2022 "%s failed to register memcpy as an async device - %d\n",
2023 __func__, ret);
2024 goto out_no_memcpy_reg;
2025 }
2026
2027 ret = dma_async_device_register(&pl08x->slave);
2028 if (ret) {
2029 dev_warn(&pl08x->adev->dev,
2030 "%s failed to register slave as an async device - %d\n",
2031 __func__, ret);
2032 goto out_no_slave_reg;
2033 }
2034
2035 amba_set_drvdata(adev, pl08x);
2036 init_pl08x_debugfs(pl08x);
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00002037 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
2038 amba_part(adev), amba_rev(adev),
2039 (unsigned long long)adev->res.start, adev->irq[0]);
Linus Walleije8689e62010-09-28 15:57:37 +02002040 return 0;
2041
2042out_no_slave_reg:
2043 dma_async_device_unregister(&pl08x->memcpy);
2044out_no_memcpy_reg:
2045 pl08x_free_virtual_channels(&pl08x->slave);
2046out_no_slave:
2047 pl08x_free_virtual_channels(&pl08x->memcpy);
2048out_no_memcpy:
2049 kfree(pl08x->phy_chans);
2050out_no_phychans:
2051 free_irq(adev->irq[0], pl08x);
2052out_no_irq:
2053 iounmap(pl08x->base);
2054out_no_ioremap:
2055 dma_pool_destroy(pl08x->pool);
2056out_no_lli_pool:
2057out_no_platdata:
2058 kfree(pl08x);
2059out_no_pl08x:
2060 amba_release_regions(adev);
2061 return ret;
2062}
2063
2064/* PL080 has 8 channels and the PL080 have just 2 */
2065static struct vendor_data vendor_pl080 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002066 .channels = 8,
2067 .dualmaster = true,
2068};
2069
2070static struct vendor_data vendor_pl081 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002071 .channels = 2,
2072 .dualmaster = false,
2073};
2074
2075static struct amba_id pl08x_ids[] = {
2076 /* PL080 */
2077 {
2078 .id = 0x00041080,
2079 .mask = 0x000fffff,
2080 .data = &vendor_pl080,
2081 },
2082 /* PL081 */
2083 {
2084 .id = 0x00041081,
2085 .mask = 0x000fffff,
2086 .data = &vendor_pl081,
2087 },
2088 /* Nomadik 8815 PL080 variant */
2089 {
2090 .id = 0x00280880,
2091 .mask = 0x00ffffff,
2092 .data = &vendor_pl080,
2093 },
2094 { 0, 0 },
2095};
2096
2097static struct amba_driver pl08x_amba_driver = {
2098 .drv.name = DRIVER_NAME,
2099 .id_table = pl08x_ids,
2100 .probe = pl08x_probe,
2101};
2102
2103static int __init pl08x_init(void)
2104{
2105 int retval;
2106 retval = amba_driver_register(&pl08x_amba_driver);
2107 if (retval)
2108 printk(KERN_WARNING DRIVER_NAME
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00002109 "failed to register as an AMBA device (%d)\n",
Linus Walleije8689e62010-09-28 15:57:37 +02002110 retval);
2111 return retval;
2112}
2113subsys_initcall(pl08x_init);