blob: 7c327c3158788ae83dbdac393979a64fc8338500 [file] [log] [blame]
Linus Walleije8689e62010-09-28 15:57:37 +02001/*
2 * Copyright (c) 2006 ARM Ltd.
3 * Copyright (c) 2010 ST-Ericsson SA
4 *
5 * Author: Peter Pearse <peter.pearse@arm.com>
6 * Author: Linus Walleij <linus.walleij@stericsson.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +000022 * The full GNU General Public License is in this distribution in the
Linus Walleije8689e62010-09-28 15:57:37 +020023 * file called COPYING.
24 *
25 * Documentation: ARM DDI 0196G == PL080
26 * Documentation: ARM DDI 0218E == PL081
27 *
28 * PL080 & PL081 both have 16 sets of DMA signals that can be routed to
29 * any channel.
30 *
31 * The PL080 has 8 channels available for simultaneous use, and the PL081
32 * has only two channels. So on these DMA controllers the number of channels
33 * and the number of incoming DMA signals are two totally different things.
34 * It is usually not possible to theoretically handle all physical signals,
35 * so a multiplexing scheme with possible denial of use is necessary.
36 *
37 * The PL080 has a dual bus master, PL081 has a single master.
38 *
39 * Memory to peripheral transfer may be visualized as
40 * Get data from memory to DMAC
41 * Until no data left
42 * On burst request from peripheral
43 * Destination burst from DMAC to peripheral
44 * Clear burst request
45 * Raise terminal count interrupt
46 *
47 * For peripherals with a FIFO:
48 * Source burst size == half the depth of the peripheral FIFO
49 * Destination burst size == the depth of the peripheral FIFO
50 *
51 * (Bursts are irrelevant for mem to mem transfers - there are no burst
52 * signals, the DMA controller will simply facilitate its AHB master.)
53 *
54 * ASSUMES default (little) endianness for DMA transfers
55 *
Russell King - ARM Linux9dc2c202011-01-03 22:33:06 +000056 * The PL08x has two flow control settings:
57 * - DMAC flow control: the transfer size defines the number of transfers
58 * which occur for the current LLI entry, and the DMAC raises TC at the
59 * end of every LLI entry. Observed behaviour shows the DMAC listening
60 * to both the BREQ and SREQ signals (contrary to documented),
61 * transferring data if either is active. The LBREQ and LSREQ signals
62 * are ignored.
63 *
64 * - Peripheral flow control: the transfer size is ignored (and should be
65 * zero). The data is transferred from the current LLI entry, until
66 * after the final transfer signalled by LBREQ or LSREQ. The DMAC
67 * will then move to the next LLI entry.
68 *
69 * Only the former works sanely with scatter lists, so we only implement
70 * the DMAC flow control method. However, peripherals which use the LBREQ
71 * and LSREQ signals (eg, MMCI) are unable to use this mode, which through
72 * these hardware restrictions prevents them from using scatter DMA.
Linus Walleije8689e62010-09-28 15:57:37 +020073 *
74 * Global TODO:
75 * - Break out common code from arch/arm/mach-s3c64xx and share
76 */
77#include <linux/device.h>
78#include <linux/init.h>
79#include <linux/module.h>
Linus Walleije8689e62010-09-28 15:57:37 +020080#include <linux/interrupt.h>
81#include <linux/slab.h>
82#include <linux/dmapool.h>
Linus Walleije8689e62010-09-28 15:57:37 +020083#include <linux/dmaengine.h>
Russell King - ARM Linux730404a2011-01-03 22:34:07 +000084#include <linux/amba/bus.h>
Linus Walleije8689e62010-09-28 15:57:37 +020085#include <linux/amba/pl08x.h>
86#include <linux/debugfs.h>
87#include <linux/seq_file.h>
88
89#include <asm/hardware/pl080.h>
Linus Walleije8689e62010-09-28 15:57:37 +020090
91#define DRIVER_NAME "pl08xdmac"
92
93/**
94 * struct vendor_data - vendor-specific config parameters
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +000095 * for PL08x derivatives
Linus Walleije8689e62010-09-28 15:57:37 +020096 * @channels: the number of channels available in this variant
97 * @dualmaster: whether this version supports dual AHB masters
98 * or not.
99 */
100struct vendor_data {
Linus Walleije8689e62010-09-28 15:57:37 +0200101 u8 channels;
102 bool dualmaster;
103};
104
105/*
106 * PL08X private data structures
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000107 * An LLI struct - see PL08x TRM. Note that next uses bit[0] as a bus bit,
Russell King - ARM Linuxe25761d2011-01-03 22:37:52 +0000108 * start & end do not - their bus bit info is in cctl. Also note that these
109 * are fixed 32-bit quantities.
Linus Walleije8689e62010-09-28 15:57:37 +0200110 */
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000111struct pl08x_lli {
Russell King - ARM Linuxe25761d2011-01-03 22:37:52 +0000112 u32 src;
113 u32 dst;
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000114 u32 lli;
Linus Walleije8689e62010-09-28 15:57:37 +0200115 u32 cctl;
116};
117
118/**
119 * struct pl08x_driver_data - the local state holder for the PL08x
120 * @slave: slave engine for this instance
121 * @memcpy: memcpy engine for this instance
122 * @base: virtual memory base (remapped) for the PL08x
123 * @adev: the corresponding AMBA (PrimeCell) bus entry
124 * @vd: vendor data for this PL08x variant
125 * @pd: platform data passed in from the platform/machine
126 * @phy_chans: array of data for the physical channels
127 * @pool: a pool for the LLI descriptors
128 * @pool_ctr: counter of LLIs in the pool
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000129 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI fetches
130 * @mem_buses: set to indicate memory transfers on AHB2.
Linus Walleije8689e62010-09-28 15:57:37 +0200131 * @lock: a spinlock for this struct
132 */
133struct pl08x_driver_data {
134 struct dma_device slave;
135 struct dma_device memcpy;
136 void __iomem *base;
137 struct amba_device *adev;
Russell King - ARM Linuxf96ca9e2011-01-03 22:35:08 +0000138 const struct vendor_data *vd;
Linus Walleije8689e62010-09-28 15:57:37 +0200139 struct pl08x_platform_data *pd;
140 struct pl08x_phy_chan *phy_chans;
141 struct dma_pool *pool;
142 int pool_ctr;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000143 u8 lli_buses;
144 u8 mem_buses;
Linus Walleije8689e62010-09-28 15:57:37 +0200145 spinlock_t lock;
146};
147
148/*
149 * PL08X specific defines
150 */
151
152/*
153 * Memory boundaries: the manual for PL08x says that the controller
154 * cannot read past a 1KiB boundary, so these defines are used to
155 * create transfer LLIs that do not cross such boundaries.
156 */
157#define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */
158#define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)
159
160/* Minimum period between work queue runs */
161#define PL08X_WQ_PERIODMIN 20
162
163/* Size (bytes) of each LLI buffer allocated for one transfer */
164# define PL08X_LLI_TSFR_SIZE 0x2000
165
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000166/* Maximum times we call dma_pool_alloc on this pool without freeing */
Linus Walleije8689e62010-09-28 15:57:37 +0200167#define PL08X_MAX_ALLOCS 0x40
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000168#define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
Linus Walleije8689e62010-09-28 15:57:37 +0200169#define PL08X_ALIGN 8
170
171static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
172{
173 return container_of(chan, struct pl08x_dma_chan, chan);
174}
175
176/*
177 * Physical channel handling
178 */
179
180/* Whether a certain channel is busy or not */
181static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
182{
183 unsigned int val;
184
185 val = readl(ch->base + PL080_CH_CONFIG);
186 return val & PL080_CONFIG_ACTIVE;
187}
188
189/*
190 * Set the initial DMA register values i.e. those for the first LLI
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000191 * The next LLI pointer and the configuration interrupt bit have
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000192 * been set when the LLIs were constructed. Poke them into the hardware
193 * and start the transfer.
Linus Walleije8689e62010-09-28 15:57:37 +0200194 */
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000195static void pl08x_start_txd(struct pl08x_dma_chan *plchan,
196 struct pl08x_txd *txd)
Linus Walleije8689e62010-09-28 15:57:37 +0200197{
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000198 struct pl08x_driver_data *pl08x = plchan->host;
Linus Walleije8689e62010-09-28 15:57:37 +0200199 struct pl08x_phy_chan *phychan = plchan->phychan;
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000200 struct pl08x_lli *lli = &txd->llis_va[0];
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000201 u32 val;
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000202
203 plchan->at = txd;
Linus Walleije8689e62010-09-28 15:57:37 +0200204
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000205 /* Wait for channel inactive */
206 while (pl08x_phy_channel_busy(phychan))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000207 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200208
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000209 dev_vdbg(&pl08x->adev->dev,
210 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000211 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
212 phychan->id, lli->src, lli->dst, lli->lli, lli->cctl,
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000213 txd->ccfg);
Linus Walleije8689e62010-09-28 15:57:37 +0200214
Russell King - ARM Linux19524d72011-01-03 22:39:13 +0000215 writel(lli->src, phychan->base + PL080_CH_SRC_ADDR);
216 writel(lli->dst, phychan->base + PL080_CH_DST_ADDR);
217 writel(lli->lli, phychan->base + PL080_CH_LLI);
218 writel(lli->cctl, phychan->base + PL080_CH_CONTROL);
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000219 writel(txd->ccfg, phychan->base + PL080_CH_CONFIG);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +0000220
221 /* Enable the DMA channel */
222 /* Do not access config register until channel shows as disabled */
223 while (readl(pl08x->base + PL080_EN_CHAN) & (1 << phychan->id))
224 cpu_relax();
225
226 /* Do not access config register until channel shows as inactive */
227 val = readl(phychan->base + PL080_CH_CONFIG);
228 while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
229 val = readl(phychan->base + PL080_CH_CONFIG);
230
231 writel(val | PL080_CONFIG_ENABLE, phychan->base + PL080_CH_CONFIG);
Linus Walleije8689e62010-09-28 15:57:37 +0200232}
233
234/*
235 * Overall DMAC remains enabled always.
236 *
237 * Disabling individual channels could lose data.
238 *
239 * Disable the peripheral DMA after disabling the DMAC
240 * in order to allow the DMAC FIFO to drain, and
241 * hence allow the channel to show inactive
242 *
243 */
244static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
245{
246 u32 val;
247
248 /* Set the HALT bit and wait for the FIFO to drain */
249 val = readl(ch->base + PL080_CH_CONFIG);
250 val |= PL080_CONFIG_HALT;
251 writel(val, ch->base + PL080_CH_CONFIG);
252
253 /* Wait for channel inactive */
254 while (pl08x_phy_channel_busy(ch))
Russell King - ARM Linux19386b322011-01-03 22:36:29 +0000255 cpu_relax();
Linus Walleije8689e62010-09-28 15:57:37 +0200256}
257
258static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
259{
260 u32 val;
261
262 /* Clear the HALT bit */
263 val = readl(ch->base + PL080_CH_CONFIG);
264 val &= ~PL080_CONFIG_HALT;
265 writel(val, ch->base + PL080_CH_CONFIG);
266}
267
268
269/* Stops the channel */
270static void pl08x_stop_phy_chan(struct pl08x_phy_chan *ch)
271{
272 u32 val;
273
274 pl08x_pause_phy_chan(ch);
275
276 /* Disable channel */
277 val = readl(ch->base + PL080_CH_CONFIG);
278 val &= ~PL080_CONFIG_ENABLE;
279 val &= ~PL080_CONFIG_ERR_IRQ_MASK;
280 val &= ~PL080_CONFIG_TC_IRQ_MASK;
281 writel(val, ch->base + PL080_CH_CONFIG);
282}
283
284static inline u32 get_bytes_in_cctl(u32 cctl)
285{
286 /* The source width defines the number of bytes */
287 u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
288
289 switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
290 case PL080_WIDTH_8BIT:
291 break;
292 case PL080_WIDTH_16BIT:
293 bytes *= 2;
294 break;
295 case PL080_WIDTH_32BIT:
296 bytes *= 4;
297 break;
298 }
299 return bytes;
300}
301
302/* The channel should be paused when calling this */
303static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
304{
305 struct pl08x_phy_chan *ch;
Linus Walleije8689e62010-09-28 15:57:37 +0200306 struct pl08x_txd *txd;
307 unsigned long flags;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000308 size_t bytes = 0;
Linus Walleije8689e62010-09-28 15:57:37 +0200309
310 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +0200311 ch = plchan->phychan;
312 txd = plchan->at;
313
314 /*
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000315 * Follow the LLIs to get the number of remaining
316 * bytes in the currently active transaction.
Linus Walleije8689e62010-09-28 15:57:37 +0200317 */
318 if (ch && txd) {
Russell King - ARM Linux4c0df6a2011-01-03 22:36:50 +0000319 u32 clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200320
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000321 /* First get the remaining bytes in the active transfer */
Linus Walleije8689e62010-09-28 15:57:37 +0200322 bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
323
324 if (clli) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000325 struct pl08x_lli *llis_va = txd->llis_va;
326 dma_addr_t llis_bus = txd->llis_bus;
327 int index;
Linus Walleije8689e62010-09-28 15:57:37 +0200328
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000329 BUG_ON(clli < llis_bus || clli >= llis_bus +
330 sizeof(struct pl08x_lli) * MAX_NUM_TSFR_LLIS);
Linus Walleije8689e62010-09-28 15:57:37 +0200331
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000332 /*
333 * Locate the next LLI - as this is an array,
334 * it's simple maths to find.
335 */
336 index = (clli - llis_bus) / sizeof(struct pl08x_lli);
337
338 for (; index < MAX_NUM_TSFR_LLIS; index++) {
339 bytes += get_bytes_in_cctl(llis_va[index].cctl);
340
Linus Walleije8689e62010-09-28 15:57:37 +0200341 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000342 * A LLI pointer of 0 terminates the LLI list
Linus Walleije8689e62010-09-28 15:57:37 +0200343 */
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000344 if (!llis_va[index].lli)
345 break;
Linus Walleije8689e62010-09-28 15:57:37 +0200346 }
347 }
348 }
349
350 /* Sum up all queued transactions */
351 if (!list_empty(&plchan->desc_list)) {
Russell King - ARM Linuxdb9f1362011-01-03 22:38:32 +0000352 struct pl08x_txd *txdi;
Linus Walleije8689e62010-09-28 15:57:37 +0200353 list_for_each_entry(txdi, &plchan->desc_list, node) {
354 bytes += txdi->len;
355 }
Linus Walleije8689e62010-09-28 15:57:37 +0200356 }
357
358 spin_unlock_irqrestore(&plchan->lock, flags);
359
360 return bytes;
361}
362
363/*
364 * Allocate a physical channel for a virtual channel
365 */
366static struct pl08x_phy_chan *
367pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
368 struct pl08x_dma_chan *virt_chan)
369{
370 struct pl08x_phy_chan *ch = NULL;
371 unsigned long flags;
372 int i;
373
374 /*
375 * Try to locate a physical channel to be used for
376 * this transfer. If all are taken return NULL and
377 * the requester will have to cope by using some fallback
378 * PIO mode or retrying later.
379 */
380 for (i = 0; i < pl08x->vd->channels; i++) {
381 ch = &pl08x->phy_chans[i];
382
383 spin_lock_irqsave(&ch->lock, flags);
384
385 if (!ch->serving) {
386 ch->serving = virt_chan;
387 ch->signal = -1;
388 spin_unlock_irqrestore(&ch->lock, flags);
389 break;
390 }
391
392 spin_unlock_irqrestore(&ch->lock, flags);
393 }
394
395 if (i == pl08x->vd->channels) {
396 /* No physical channel available, cope with it */
397 return NULL;
398 }
399
400 return ch;
401}
402
403static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
404 struct pl08x_phy_chan *ch)
405{
406 unsigned long flags;
407
408 /* Stop the channel and clear its interrupts */
409 pl08x_stop_phy_chan(ch);
410 writel((1 << ch->id), pl08x->base + PL080_ERR_CLEAR);
411 writel((1 << ch->id), pl08x->base + PL080_TC_CLEAR);
412
413 /* Mark it as free */
414 spin_lock_irqsave(&ch->lock, flags);
415 ch->serving = NULL;
416 spin_unlock_irqrestore(&ch->lock, flags);
417}
418
419/*
420 * LLI handling
421 */
422
423static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
424{
425 switch (coded) {
426 case PL080_WIDTH_8BIT:
427 return 1;
428 case PL080_WIDTH_16BIT:
429 return 2;
430 case PL080_WIDTH_32BIT:
431 return 4;
432 default:
433 break;
434 }
435 BUG();
436 return 0;
437}
438
439static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000440 size_t tsize)
Linus Walleije8689e62010-09-28 15:57:37 +0200441{
442 u32 retbits = cctl;
443
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000444 /* Remove all src, dst and transfer size bits */
Linus Walleije8689e62010-09-28 15:57:37 +0200445 retbits &= ~PL080_CONTROL_DWIDTH_MASK;
446 retbits &= ~PL080_CONTROL_SWIDTH_MASK;
447 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
448
449 /* Then set the bits according to the parameters */
450 switch (srcwidth) {
451 case 1:
452 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
453 break;
454 case 2:
455 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
456 break;
457 case 4:
458 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
459 break;
460 default:
461 BUG();
462 break;
463 }
464
465 switch (dstwidth) {
466 case 1:
467 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
468 break;
469 case 2:
470 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
471 break;
472 case 4:
473 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
474 break;
475 default:
476 BUG();
477 break;
478 }
479
480 retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
481 return retbits;
482}
483
484/*
485 * Autoselect a master bus to use for the transfer
486 * this prefers the destination bus if both available
487 * if fixed address on one bus the other will be chosen
488 */
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +0000489static void pl08x_choose_master_bus(struct pl08x_bus_data *src_bus,
Linus Walleije8689e62010-09-28 15:57:37 +0200490 struct pl08x_bus_data *dst_bus, struct pl08x_bus_data **mbus,
491 struct pl08x_bus_data **sbus, u32 cctl)
492{
493 if (!(cctl & PL080_CONTROL_DST_INCR)) {
494 *mbus = src_bus;
495 *sbus = dst_bus;
496 } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
497 *mbus = dst_bus;
498 *sbus = src_bus;
499 } else {
500 if (dst_bus->buswidth == 4) {
501 *mbus = dst_bus;
502 *sbus = src_bus;
503 } else if (src_bus->buswidth == 4) {
504 *mbus = src_bus;
505 *sbus = dst_bus;
506 } else if (dst_bus->buswidth == 2) {
507 *mbus = dst_bus;
508 *sbus = src_bus;
509 } else if (src_bus->buswidth == 2) {
510 *mbus = src_bus;
511 *sbus = dst_bus;
512 } else {
513 /* src_bus->buswidth == 1 */
514 *mbus = dst_bus;
515 *sbus = src_bus;
516 }
517 }
518}
519
520/*
521 * Fills in one LLI for a certain transfer descriptor
522 * and advance the counter
523 */
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000524static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
525 struct pl08x_txd *txd, int num_llis, int len, u32 cctl, u32 *remainder)
Linus Walleije8689e62010-09-28 15:57:37 +0200526{
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000527 struct pl08x_lli *llis_va = txd->llis_va;
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000528 dma_addr_t llis_bus = txd->llis_bus;
Linus Walleije8689e62010-09-28 15:57:37 +0200529
530 BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
531
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000532 llis_va[num_llis].cctl = cctl;
533 llis_va[num_llis].src = txd->srcbus.addr;
534 llis_va[num_llis].dst = txd->dstbus.addr;
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000535 llis_va[num_llis].lli = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli);
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +0000536 if (pl08x->lli_buses & PL08X_AHB2)
537 llis_va[num_llis].lli |= PL080_LLI_LM_AHB2;
Linus Walleije8689e62010-09-28 15:57:37 +0200538
539 if (cctl & PL080_CONTROL_SRC_INCR)
540 txd->srcbus.addr += len;
541 if (cctl & PL080_CONTROL_DST_INCR)
542 txd->dstbus.addr += len;
543
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000544 BUG_ON(*remainder < len);
545
Linus Walleije8689e62010-09-28 15:57:37 +0200546 *remainder -= len;
Linus Walleije8689e62010-09-28 15:57:37 +0200547}
548
549/*
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000550 * Return number of bytes to fill to boundary, or len.
551 * This calculation works for any value of addr.
Linus Walleije8689e62010-09-28 15:57:37 +0200552 */
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000553static inline size_t pl08x_pre_boundary(u32 addr, size_t len)
Linus Walleije8689e62010-09-28 15:57:37 +0200554{
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000555 size_t boundary_len = PL08X_BOUNDARY_SIZE -
556 (addr & (PL08X_BOUNDARY_SIZE - 1));
Linus Walleije8689e62010-09-28 15:57:37 +0200557
Russell King - ARM Linuxb61be8d2011-01-03 22:42:14 +0000558 return min(boundary_len, len);
Linus Walleije8689e62010-09-28 15:57:37 +0200559}
560
561/*
562 * This fills in the table of LLIs for the transfer descriptor
563 * Note that we assume we never have to change the burst sizes
564 * Return 0 for error
565 */
566static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
567 struct pl08x_txd *txd)
568{
Linus Walleije8689e62010-09-28 15:57:37 +0200569 struct pl08x_bus_data *mbus, *sbus;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000570 size_t remainder;
Linus Walleije8689e62010-09-28 15:57:37 +0200571 int num_llis = 0;
572 u32 cctl;
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000573 size_t max_bytes_per_lli;
574 size_t total_bytes = 0;
Russell King - ARM Linux7cb72ad2011-01-03 22:35:28 +0000575 struct pl08x_lli *llis_va;
Linus Walleije8689e62010-09-28 15:57:37 +0200576
Linus Walleije8689e62010-09-28 15:57:37 +0200577 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
578 &txd->llis_bus);
579 if (!txd->llis_va) {
580 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
581 return 0;
582 }
583
584 pl08x->pool_ctr++;
585
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +0000586 /* Get the default CCTL */
587 cctl = txd->cctl;
Linus Walleije8689e62010-09-28 15:57:37 +0200588
Linus Walleije8689e62010-09-28 15:57:37 +0200589 /* Find maximum width of the source bus */
590 txd->srcbus.maxwidth =
591 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
592 PL080_CONTROL_SWIDTH_SHIFT);
593
594 /* Find maximum width of the destination bus */
595 txd->dstbus.maxwidth =
596 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
597 PL080_CONTROL_DWIDTH_SHIFT);
598
599 /* Set up the bus widths to the maximum */
600 txd->srcbus.buswidth = txd->srcbus.maxwidth;
601 txd->dstbus.buswidth = txd->dstbus.maxwidth;
602 dev_vdbg(&pl08x->adev->dev,
603 "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
604 __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
605
606
607 /*
608 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
609 */
610 max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
611 PL080_CONTROL_TRANSFER_SIZE_MASK;
612 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000613 "%s max bytes per lli = %zu\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200614 __func__, max_bytes_per_lli);
615
616 /* We need to count this down to zero */
617 remainder = txd->len;
618 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000619 "%s remainder = %zu\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200620 __func__, remainder);
621
622 /*
623 * Choose bus to align to
624 * - prefers destination bus if both available
625 * - if fixed address on one bus chooses other
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000626 * - modifies cctl to choose an appropriate master
Linus Walleije8689e62010-09-28 15:57:37 +0200627 */
628 pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
629 &mbus, &sbus, cctl);
630
Linus Walleije8689e62010-09-28 15:57:37 +0200631 if (txd->len < mbus->buswidth) {
632 /*
633 * Less than a bus width available
634 * - send as single bytes
635 */
636 while (remainder) {
637 dev_vdbg(&pl08x->adev->dev,
638 "%s single byte LLIs for a transfer of "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000639 "less than a bus width (remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200640 __func__, remainder);
641 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000642 pl08x_fill_lli_for_desc(pl08x, txd, num_llis++, 1,
Linus Walleije8689e62010-09-28 15:57:37 +0200643 cctl, &remainder);
644 total_bytes++;
645 }
646 } else {
647 /*
648 * Make one byte LLIs until master bus is aligned
649 * - slave will then be aligned also
650 */
651 while ((mbus->addr) % (mbus->buswidth)) {
652 dev_vdbg(&pl08x->adev->dev,
653 "%s adjustment lli for less than bus width "
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000654 "(remain 0x%08x)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200655 __func__, remainder);
656 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000657 pl08x_fill_lli_for_desc(pl08x, txd, num_llis++, 1,
658 cctl, &remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200659 total_bytes++;
660 }
661
662 /*
663 * Master now aligned
664 * - if slave is not then we must set its width down
665 */
666 if (sbus->addr % sbus->buswidth) {
667 dev_dbg(&pl08x->adev->dev,
668 "%s set down bus width to one byte\n",
669 __func__);
670
671 sbus->buswidth = 1;
672 }
673
674 /*
675 * Make largest possible LLIs until less than one bus
676 * width left
677 */
678 while (remainder > (mbus->buswidth - 1)) {
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000679 size_t lli_len, target_len, tsize, odd_bytes;
Linus Walleije8689e62010-09-28 15:57:37 +0200680
681 /*
682 * If enough left try to send max possible,
683 * otherwise try to send the remainder
684 */
685 target_len = remainder;
686 if (remainder > max_bytes_per_lli)
687 target_len = max_bytes_per_lli;
688
689 /*
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000690 * Set bus lengths for incrementing buses
Linus Walleije8689e62010-09-28 15:57:37 +0200691 * to number of bytes which fill to next memory
692 * boundary
693 */
694 if (cctl & PL080_CONTROL_SRC_INCR)
695 txd->srcbus.fill_bytes =
696 pl08x_pre_boundary(
697 txd->srcbus.addr,
698 remainder);
699 else
700 txd->srcbus.fill_bytes =
701 max_bytes_per_lli;
702
703 if (cctl & PL080_CONTROL_DST_INCR)
704 txd->dstbus.fill_bytes =
705 pl08x_pre_boundary(
706 txd->dstbus.addr,
707 remainder);
708 else
709 txd->dstbus.fill_bytes =
710 max_bytes_per_lli;
711
712 /*
713 * Find the nearest
714 */
715 lli_len = min(txd->srcbus.fill_bytes,
716 txd->dstbus.fill_bytes);
717
718 BUG_ON(lli_len > remainder);
719
720 if (lli_len <= 0) {
721 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000722 "%s lli_len is %zu, <= 0\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200723 __func__, lli_len);
724 return 0;
725 }
726
727 if (lli_len == target_len) {
728 /*
729 * Can send what we wanted
730 */
731 /*
732 * Maintain alignment
733 */
734 lli_len = (lli_len/mbus->buswidth) *
735 mbus->buswidth;
736 odd_bytes = 0;
737 } else {
738 /*
739 * So now we know how many bytes to transfer
740 * to get to the nearest boundary
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +0000741 * The next LLI will past the boundary
Linus Walleije8689e62010-09-28 15:57:37 +0200742 * - however we may be working to a boundary
743 * on the slave bus
744 * We need to ensure the master stays aligned
745 */
746 odd_bytes = lli_len % mbus->buswidth;
747 /*
748 * - and that we are working in multiples
749 * of the bus widths
750 */
751 lli_len -= odd_bytes;
752
753 }
754
755 if (lli_len) {
756 /*
757 * Check against minimum bus alignment:
758 * Calculate actual transfer size in relation
759 * to bus width an get a maximum remainder of
760 * the smallest bus width - 1
761 */
762 /* FIXME: use round_down()? */
763 tsize = lli_len / min(mbus->buswidth,
764 sbus->buswidth);
765 lli_len = tsize * min(mbus->buswidth,
766 sbus->buswidth);
767
768 if (target_len != lli_len) {
769 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000770 "%s can't send what we want. Desired 0x%08zx, lli of 0x%08zx bytes in txd of 0x%08zx\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200771 __func__, target_len, lli_len, txd->len);
772 }
773
774 cctl = pl08x_cctl_bits(cctl,
775 txd->srcbus.buswidth,
776 txd->dstbus.buswidth,
777 tsize);
778
779 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000780 "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200781 __func__, lli_len, remainder);
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000782 pl08x_fill_lli_for_desc(pl08x, txd, num_llis++,
783 lli_len, cctl, &remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200784 total_bytes += lli_len;
785 }
786
787
788 if (odd_bytes) {
789 /*
790 * Creep past the boundary,
791 * maintaining master alignment
792 */
793 int j;
794 for (j = 0; (j < mbus->buswidth)
795 && (remainder); j++) {
796 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
797 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000798 "%s align with boundary, single byte (remain 0x%08zx)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200799 __func__, remainder);
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000800 pl08x_fill_lli_for_desc(pl08x, txd,
801 num_llis++, 1, cctl,
802 &remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200803 total_bytes++;
804 }
805 }
806 }
807
808 /*
809 * Send any odd bytes
810 */
Linus Walleije8689e62010-09-28 15:57:37 +0200811 while (remainder) {
812 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
813 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000814 "%s align with boundary, single odd byte (remain %zu)\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200815 __func__, remainder);
Russell King - ARM Linux00590052011-01-03 22:41:54 +0000816 pl08x_fill_lli_for_desc(pl08x, txd, num_llis++, 1,
817 cctl, &remainder);
Linus Walleije8689e62010-09-28 15:57:37 +0200818 total_bytes++;
819 }
820 }
821 if (total_bytes != txd->len) {
822 dev_err(&pl08x->adev->dev,
Russell King - ARM Linuxcace6582011-01-03 22:37:31 +0000823 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200824 __func__, total_bytes, txd->len);
825 return 0;
826 }
827
828 if (num_llis >= MAX_NUM_TSFR_LLIS) {
829 dev_err(&pl08x->adev->dev,
830 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
831 __func__, (u32) MAX_NUM_TSFR_LLIS);
832 return 0;
833 }
Linus Walleije8689e62010-09-28 15:57:37 +0200834
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000835 llis_va = txd->llis_va;
836 /*
837 * The final LLI terminates the LLI.
838 */
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000839 llis_va[num_llis - 1].lli = 0;
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +0000840 /*
841 * The final LLI element shall also fire an interrupt
842 */
843 llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
Linus Walleije8689e62010-09-28 15:57:37 +0200844
Linus Walleije8689e62010-09-28 15:57:37 +0200845#ifdef VERBOSE_DEBUG
846 {
847 int i;
848
849 for (i = 0; i < num_llis; i++) {
850 dev_vdbg(&pl08x->adev->dev,
Russell King - ARM Linux9c132992011-01-03 22:33:47 +0000851 "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +0200852 i,
853 &llis_va[i],
854 llis_va[i].src,
855 llis_va[i].dst,
856 llis_va[i].cctl,
Russell King - ARM Linuxbfddfb42011-01-03 22:38:12 +0000857 llis_va[i].lli
Linus Walleije8689e62010-09-28 15:57:37 +0200858 );
859 }
860 }
861#endif
862
863 return num_llis;
864}
865
866/* You should call this with the struct pl08x lock held */
867static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
868 struct pl08x_txd *txd)
869{
Linus Walleije8689e62010-09-28 15:57:37 +0200870 /* Free the LLI */
Russell King - ARM Linux56b61882011-01-03 22:37:10 +0000871 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
Linus Walleije8689e62010-09-28 15:57:37 +0200872
873 pl08x->pool_ctr--;
874
875 kfree(txd);
876}
877
878static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
879 struct pl08x_dma_chan *plchan)
880{
881 struct pl08x_txd *txdi = NULL;
882 struct pl08x_txd *next;
883
884 if (!list_empty(&plchan->desc_list)) {
885 list_for_each_entry_safe(txdi,
886 next, &plchan->desc_list, node) {
887 list_del(&txdi->node);
888 pl08x_free_txd(pl08x, txdi);
889 }
890
891 }
892}
893
894/*
895 * The DMA ENGINE API
896 */
897static int pl08x_alloc_chan_resources(struct dma_chan *chan)
898{
899 return 0;
900}
901
902static void pl08x_free_chan_resources(struct dma_chan *chan)
903{
904}
905
906/*
907 * This should be called with the channel plchan->lock held
908 */
909static int prep_phy_channel(struct pl08x_dma_chan *plchan,
910 struct pl08x_txd *txd)
911{
912 struct pl08x_driver_data *pl08x = plchan->host;
913 struct pl08x_phy_chan *ch;
914 int ret;
915
916 /* Check if we already have a channel */
917 if (plchan->phychan)
918 return 0;
919
920 ch = pl08x_get_phy_channel(pl08x, plchan);
921 if (!ch) {
922 /* No physical channel available, cope with it */
923 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
924 return -EBUSY;
925 }
926
927 /*
928 * OK we have a physical channel: for memcpy() this is all we
929 * need, but for slaves the physical signals may be muxed!
930 * Can the platform allow us to use this channel?
931 */
932 if (plchan->slave &&
933 ch->signal < 0 &&
934 pl08x->pd->get_signal) {
935 ret = pl08x->pd->get_signal(plchan);
936 if (ret < 0) {
937 dev_dbg(&pl08x->adev->dev,
938 "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
939 ch->id, plchan->name);
940 /* Release physical channel & return */
941 pl08x_put_phy_channel(pl08x, ch);
942 return -EBUSY;
943 }
944 ch->signal = ret;
Russell King - ARM Linux09b3c322011-01-03 22:39:53 +0000945
946 /* Assign the flow control signal to this channel */
947 if (txd->direction == DMA_TO_DEVICE)
948 txd->ccfg |= ch->signal << PL080_CONFIG_DST_SEL_SHIFT;
949 else if (txd->direction == DMA_FROM_DEVICE)
950 txd->ccfg |= ch->signal << PL080_CONFIG_SRC_SEL_SHIFT;
Linus Walleije8689e62010-09-28 15:57:37 +0200951 }
952
953 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
954 ch->id,
955 ch->signal,
956 plchan->name);
957
958 plchan->phychan = ch;
959
960 return 0;
961}
962
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +0000963static void release_phy_channel(struct pl08x_dma_chan *plchan)
964{
965 struct pl08x_driver_data *pl08x = plchan->host;
966
967 if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
968 pl08x->pd->put_signal(plchan);
969 plchan->phychan->signal = -1;
970 }
971 pl08x_put_phy_channel(pl08x, plchan->phychan);
972 plchan->phychan = NULL;
973}
974
Linus Walleije8689e62010-09-28 15:57:37 +0200975static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
976{
977 struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
978
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +0000979 plchan->chan.cookie += 1;
980 if (plchan->chan.cookie < 0)
981 plchan->chan.cookie = 1;
982 tx->cookie = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +0200983 /* This unlock follows the lock in the prep() function */
984 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
985
986 return tx->cookie;
987}
988
989static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
990 struct dma_chan *chan, unsigned long flags)
991{
992 struct dma_async_tx_descriptor *retval = NULL;
993
994 return retval;
995}
996
997/*
998 * Code accessing dma_async_is_complete() in a tight loop
999 * may give problems - could schedule where indicated.
1000 * If slaves are relying on interrupts to signal completion this
1001 * function must not be called with interrupts disabled
1002 */
1003static enum dma_status
1004pl08x_dma_tx_status(struct dma_chan *chan,
1005 dma_cookie_t cookie,
1006 struct dma_tx_state *txstate)
1007{
1008 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1009 dma_cookie_t last_used;
1010 dma_cookie_t last_complete;
1011 enum dma_status ret;
1012 u32 bytesleft = 0;
1013
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001014 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001015 last_complete = plchan->lc;
1016
1017 ret = dma_async_is_complete(cookie, last_complete, last_used);
1018 if (ret == DMA_SUCCESS) {
1019 dma_set_tx_state(txstate, last_complete, last_used, 0);
1020 return ret;
1021 }
1022
1023 /*
1024 * schedule(); could be inserted here
1025 */
1026
1027 /*
1028 * This cookie not complete yet
1029 */
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001030 last_used = plchan->chan.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001031 last_complete = plchan->lc;
1032
1033 /* Get number of bytes left in the active transactions and queue */
1034 bytesleft = pl08x_getbytes_chan(plchan);
1035
1036 dma_set_tx_state(txstate, last_complete, last_used,
1037 bytesleft);
1038
1039 if (plchan->state == PL08X_CHAN_PAUSED)
1040 return DMA_PAUSED;
1041
1042 /* Whether waiting or running, we're in progress */
1043 return DMA_IN_PROGRESS;
1044}
1045
1046/* PrimeCell DMA extension */
1047struct burst_table {
1048 int burstwords;
1049 u32 reg;
1050};
1051
1052static const struct burst_table burst_sizes[] = {
1053 {
1054 .burstwords = 256,
1055 .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
1056 (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
1057 },
1058 {
1059 .burstwords = 128,
1060 .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
1061 (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
1062 },
1063 {
1064 .burstwords = 64,
1065 .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
1066 (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
1067 },
1068 {
1069 .burstwords = 32,
1070 .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
1071 (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
1072 },
1073 {
1074 .burstwords = 16,
1075 .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
1076 (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
1077 },
1078 {
1079 .burstwords = 8,
1080 .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
1081 (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
1082 },
1083 {
1084 .burstwords = 4,
1085 .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
1086 (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
1087 },
1088 {
1089 .burstwords = 1,
1090 .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1091 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
1092 },
1093};
1094
1095static void dma_set_runtime_config(struct dma_chan *chan,
1096 struct dma_slave_config *config)
1097{
1098 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1099 struct pl08x_driver_data *pl08x = plchan->host;
1100 struct pl08x_channel_data *cd = plchan->cd;
1101 enum dma_slave_buswidth addr_width;
1102 u32 maxburst;
1103 u32 cctl = 0;
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001104 int i;
Linus Walleije8689e62010-09-28 15:57:37 +02001105
1106 /* Transfer direction */
1107 plchan->runtime_direction = config->direction;
1108 if (config->direction == DMA_TO_DEVICE) {
1109 plchan->runtime_addr = config->dst_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001110 addr_width = config->dst_addr_width;
1111 maxburst = config->dst_maxburst;
1112 } else if (config->direction == DMA_FROM_DEVICE) {
1113 plchan->runtime_addr = config->src_addr;
Linus Walleije8689e62010-09-28 15:57:37 +02001114 addr_width = config->src_addr_width;
1115 maxburst = config->src_maxburst;
1116 } else {
1117 dev_err(&pl08x->adev->dev,
1118 "bad runtime_config: alien transfer direction\n");
1119 return;
1120 }
1121
1122 switch (addr_width) {
1123 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1124 cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1125 (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
1126 break;
1127 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1128 cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1129 (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
1130 break;
1131 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1132 cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1133 (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
1134 break;
1135 default:
1136 dev_err(&pl08x->adev->dev,
1137 "bad runtime_config: alien address width\n");
1138 return;
1139 }
1140
1141 /*
1142 * Now decide on a maxburst:
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001143 * If this channel will only request single transfers, set this
1144 * down to ONE element. Also select one element if no maxburst
1145 * is specified.
Linus Walleije8689e62010-09-28 15:57:37 +02001146 */
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001147 if (plchan->cd->single || maxburst == 0) {
Linus Walleije8689e62010-09-28 15:57:37 +02001148 cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1149 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
1150 } else {
Russell King - ARM Linux4440aac2011-01-03 22:30:44 +00001151 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
Linus Walleije8689e62010-09-28 15:57:37 +02001152 if (burst_sizes[i].burstwords <= maxburst)
1153 break;
Linus Walleije8689e62010-09-28 15:57:37 +02001154 cctl |= burst_sizes[i].reg;
1155 }
1156
Linus Walleije8689e62010-09-28 15:57:37 +02001157 /* Modify the default channel data to fit PrimeCell request */
1158 cd->cctl = cctl;
Linus Walleije8689e62010-09-28 15:57:37 +02001159
1160 dev_dbg(&pl08x->adev->dev,
1161 "configured channel %s (%s) for %s, data width %d, "
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001162 "maxburst %d words, LE, CCTL=0x%08x\n",
Linus Walleije8689e62010-09-28 15:57:37 +02001163 dma_chan_name(chan), plchan->name,
1164 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
1165 addr_width,
1166 maxburst,
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001167 cctl);
Linus Walleije8689e62010-09-28 15:57:37 +02001168}
1169
1170/*
1171 * Slave transactions callback to the slave device to allow
1172 * synchronization of slave DMA signals with the DMAC enable
1173 */
1174static void pl08x_issue_pending(struct dma_chan *chan)
1175{
1176 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
Linus Walleije8689e62010-09-28 15:57:37 +02001177 unsigned long flags;
1178
1179 spin_lock_irqsave(&plchan->lock, flags);
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001180 /* Something is already active, or we're waiting for a channel... */
1181 if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
1182 spin_unlock_irqrestore(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001183 return;
Russell King - ARM Linux9c0bb432011-01-03 22:32:05 +00001184 }
Linus Walleije8689e62010-09-28 15:57:37 +02001185
1186 /* Take the first element in the queue and execute it */
1187 if (!list_empty(&plchan->desc_list)) {
1188 struct pl08x_txd *next;
1189
1190 next = list_first_entry(&plchan->desc_list,
1191 struct pl08x_txd,
1192 node);
1193 list_del(&next->node);
Linus Walleije8689e62010-09-28 15:57:37 +02001194 plchan->state = PL08X_CHAN_RUNNING;
1195
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001196 pl08x_start_txd(plchan, next);
Linus Walleije8689e62010-09-28 15:57:37 +02001197 }
1198
1199 spin_unlock_irqrestore(&plchan->lock, flags);
1200}
1201
1202static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1203 struct pl08x_txd *txd)
1204{
1205 int num_llis;
1206 struct pl08x_driver_data *pl08x = plchan->host;
1207 int ret;
1208
1209 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001210 if (!num_llis) {
1211 kfree(txd);
Linus Walleije8689e62010-09-28 15:57:37 +02001212 return -EINVAL;
Russell King - ARM Linuxdafa7312011-01-03 22:31:45 +00001213 }
Linus Walleije8689e62010-09-28 15:57:37 +02001214
1215 spin_lock_irqsave(&plchan->lock, plchan->lockflags);
1216
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001217 list_add_tail(&txd->node, &plchan->desc_list);
Linus Walleije8689e62010-09-28 15:57:37 +02001218
1219 /*
1220 * See if we already have a physical channel allocated,
1221 * else this is the time to try to get one.
1222 */
1223 ret = prep_phy_channel(plchan, txd);
1224 if (ret) {
1225 /*
1226 * No physical channel available, we will
1227 * stack up the memcpy channels until there is a channel
1228 * available to handle it whereas slave transfers may
1229 * have been denied due to platform channel muxing restrictions
1230 * and since there is no guarantee that this will ever be
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001231 * resolved, and since the signal must be acquired AFTER
1232 * acquiring the physical channel, we will let them be NACK:ed
Linus Walleije8689e62010-09-28 15:57:37 +02001233 * with -EBUSY here. The drivers can alway retry the prep()
1234 * call if they are eager on doing this using DMA.
1235 */
1236 if (plchan->slave) {
1237 pl08x_free_txd_list(pl08x, plchan);
1238 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1239 return -EBUSY;
1240 }
1241 /* Do this memcpy whenever there is a channel ready */
1242 plchan->state = PL08X_CHAN_WAITING;
1243 plchan->waiting = txd;
1244 } else
1245 /*
1246 * Else we're all set, paused and ready to roll,
1247 * status will switch to PL08X_CHAN_RUNNING when
1248 * we call issue_pending(). If there is something
1249 * running on the channel already we don't change
1250 * its state.
1251 */
1252 if (plchan->state == PL08X_CHAN_IDLE)
1253 plchan->state = PL08X_CHAN_PAUSED;
1254
1255 /*
1256 * Notice that we leave plchan->lock locked on purpose:
1257 * it will be unlocked in the subsequent tx_submit()
1258 * call. This is a consequence of the current API.
1259 */
1260
1261 return 0;
1262}
1263
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001264/*
1265 * Given the source and destination available bus masks, select which
1266 * will be routed to each port. We try to have source and destination
1267 * on separate ports, but always respect the allowable settings.
1268 */
1269static u32 pl08x_select_bus(struct pl08x_driver_data *pl08x, u8 src, u8 dst)
1270{
1271 u32 cctl = 0;
1272
1273 if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1274 cctl |= PL080_CONTROL_DST_AHB2;
1275 if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1276 cctl |= PL080_CONTROL_SRC_AHB2;
1277
1278 return cctl;
1279}
1280
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001281static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1282{
1283 struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
1284
1285 if (txd) {
1286 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
1287 txd->tx.tx_submit = pl08x_tx_submit;
1288 INIT_LIST_HEAD(&txd->node);
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001289
1290 /* Always enable error and terminal interrupts */
1291 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1292 PL080_CONFIG_TC_IRQ_MASK;
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001293 }
1294 return txd;
1295}
1296
Linus Walleije8689e62010-09-28 15:57:37 +02001297/*
1298 * Initialize a descriptor to be used by memcpy submit
1299 */
1300static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1301 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1302 size_t len, unsigned long flags)
1303{
1304 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1305 struct pl08x_driver_data *pl08x = plchan->host;
1306 struct pl08x_txd *txd;
1307 int ret;
1308
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001309 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001310 if (!txd) {
1311 dev_err(&pl08x->adev->dev,
1312 "%s no memory for descriptor\n", __func__);
1313 return NULL;
1314 }
1315
Linus Walleije8689e62010-09-28 15:57:37 +02001316 txd->direction = DMA_NONE;
1317 txd->srcbus.addr = src;
1318 txd->dstbus.addr = dest;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001319 txd->len = len;
Linus Walleije8689e62010-09-28 15:57:37 +02001320
1321 /* Set platform data for m2m */
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001322 txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001323 txd->cctl = pl08x->pd->memcpy_channel.cctl &
1324 ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001325
Linus Walleije8689e62010-09-28 15:57:37 +02001326 /* Both to be incremented or the code will break */
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +00001327 txd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001328
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001329 if (pl08x->vd->dualmaster)
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001330 txd->cctl |= pl08x_select_bus(pl08x,
1331 pl08x->mem_buses, pl08x->mem_buses);
Linus Walleije8689e62010-09-28 15:57:37 +02001332
Linus Walleije8689e62010-09-28 15:57:37 +02001333 ret = pl08x_prep_channel_resources(plchan, txd);
1334 if (ret)
1335 return NULL;
1336 /*
1337 * NB: the channel lock is held at this point so tx_submit()
1338 * must be called in direct succession.
1339 */
1340
1341 return &txd->tx;
1342}
1343
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001344static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
Linus Walleije8689e62010-09-28 15:57:37 +02001345 struct dma_chan *chan, struct scatterlist *sgl,
1346 unsigned int sg_len, enum dma_data_direction direction,
1347 unsigned long flags)
1348{
1349 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1350 struct pl08x_driver_data *pl08x = plchan->host;
1351 struct pl08x_txd *txd;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001352 u8 src_buses, dst_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001353 int ret;
1354
1355 /*
1356 * Current implementation ASSUMES only one sg
1357 */
1358 if (sg_len != 1) {
1359 dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
1360 __func__);
1361 BUG();
1362 }
1363
1364 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1365 __func__, sgl->length, plchan->name);
1366
Russell King - ARM Linuxac3cd202011-01-03 22:35:49 +00001367 txd = pl08x_get_txd(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001368 if (!txd) {
1369 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1370 return NULL;
1371 }
1372
Linus Walleije8689e62010-09-28 15:57:37 +02001373 if (direction != plchan->runtime_direction)
1374 dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
1375 "the direction configured for the PrimeCell\n",
1376 __func__);
1377
1378 /*
1379 * Set up addresses, the PrimeCell configured address
1380 * will take precedence since this may configure the
1381 * channel target address dynamically at runtime.
1382 */
1383 txd->direction = direction;
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001384 txd->len = sgl->length;
1385
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001386 txd->cctl = plchan->cd->cctl &
Russell King - ARM Linuxc7da9a52011-01-03 22:40:53 +00001387 ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1388 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001389 PL080_CONTROL_PROT_MASK);
1390
1391 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1392 txd->cctl |= PL080_CONTROL_PROT_SYS;
Russell King - ARM Linux70b5ed62011-01-03 22:40:13 +00001393
Linus Walleije8689e62010-09-28 15:57:37 +02001394 if (direction == DMA_TO_DEVICE) {
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001395 txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001396 txd->cctl |= PL080_CONTROL_SRC_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001397 txd->srcbus.addr = sgl->dma_address;
1398 if (plchan->runtime_addr)
1399 txd->dstbus.addr = plchan->runtime_addr;
1400 else
1401 txd->dstbus.addr = plchan->cd->addr;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001402 src_buses = pl08x->mem_buses;
1403 dst_buses = plchan->cd->periph_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001404 } else if (direction == DMA_FROM_DEVICE) {
Russell King - ARM Linux4983a042011-01-03 22:39:33 +00001405 txd->ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
Russell King - ARM Linux1cae78f2011-01-03 22:40:33 +00001406 txd->cctl |= PL080_CONTROL_DST_INCR;
Linus Walleije8689e62010-09-28 15:57:37 +02001407 if (plchan->runtime_addr)
1408 txd->srcbus.addr = plchan->runtime_addr;
1409 else
1410 txd->srcbus.addr = plchan->cd->addr;
1411 txd->dstbus.addr = sgl->dma_address;
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001412 src_buses = plchan->cd->periph_buses;
1413 dst_buses = pl08x->mem_buses;
Linus Walleije8689e62010-09-28 15:57:37 +02001414 } else {
1415 dev_err(&pl08x->adev->dev,
1416 "%s direction unsupported\n", __func__);
1417 return NULL;
1418 }
Linus Walleije8689e62010-09-28 15:57:37 +02001419
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001420 txd->cctl |= pl08x_select_bus(pl08x, src_buses, dst_buses);
1421
Linus Walleije8689e62010-09-28 15:57:37 +02001422 ret = pl08x_prep_channel_resources(plchan, txd);
1423 if (ret)
1424 return NULL;
1425 /*
1426 * NB: the channel lock is held at this point so tx_submit()
1427 * must be called in direct succession.
1428 */
1429
1430 return &txd->tx;
1431}
1432
1433static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1434 unsigned long arg)
1435{
1436 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1437 struct pl08x_driver_data *pl08x = plchan->host;
1438 unsigned long flags;
1439 int ret = 0;
1440
1441 /* Controls applicable to inactive channels */
1442 if (cmd == DMA_SLAVE_CONFIG) {
1443 dma_set_runtime_config(chan,
1444 (struct dma_slave_config *)
1445 arg);
1446 return 0;
1447 }
1448
1449 /*
1450 * Anything succeeds on channels with no physical allocation and
1451 * no queued transfers.
1452 */
1453 spin_lock_irqsave(&plchan->lock, flags);
1454 if (!plchan->phychan && !plchan->at) {
1455 spin_unlock_irqrestore(&plchan->lock, flags);
1456 return 0;
1457 }
1458
1459 switch (cmd) {
1460 case DMA_TERMINATE_ALL:
1461 plchan->state = PL08X_CHAN_IDLE;
1462
1463 if (plchan->phychan) {
1464 pl08x_stop_phy_chan(plchan->phychan);
1465
1466 /*
1467 * Mark physical channel as free and free any slave
1468 * signal
1469 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001470 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001471 }
Linus Walleije8689e62010-09-28 15:57:37 +02001472 /* Dequeue jobs and free LLIs */
1473 if (plchan->at) {
1474 pl08x_free_txd(pl08x, plchan->at);
1475 plchan->at = NULL;
1476 }
1477 /* Dequeue jobs not yet fired as well */
1478 pl08x_free_txd_list(pl08x, plchan);
1479 break;
1480 case DMA_PAUSE:
1481 pl08x_pause_phy_chan(plchan->phychan);
1482 plchan->state = PL08X_CHAN_PAUSED;
1483 break;
1484 case DMA_RESUME:
1485 pl08x_resume_phy_chan(plchan->phychan);
1486 plchan->state = PL08X_CHAN_RUNNING;
1487 break;
1488 default:
1489 /* Unknown command */
1490 ret = -ENXIO;
1491 break;
1492 }
1493
1494 spin_unlock_irqrestore(&plchan->lock, flags);
1495
1496 return ret;
1497}
1498
1499bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1500{
1501 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1502 char *name = chan_id;
1503
1504 /* Check that the channel is not taken! */
1505 if (!strcmp(plchan->name, name))
1506 return true;
1507
1508 return false;
1509}
1510
1511/*
1512 * Just check that the device is there and active
1513 * TODO: turn this bit on/off depending on the number of
1514 * physical channels actually used, if it is zero... well
1515 * shut it off. That will save some power. Cut the clock
1516 * at the same time.
1517 */
1518static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1519{
1520 u32 val;
1521
1522 val = readl(pl08x->base + PL080_CONFIG);
1523 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00001524 /* We implicitly clear bit 1 and that means little-endian mode */
Linus Walleije8689e62010-09-28 15:57:37 +02001525 val |= PL080_CONFIG_ENABLE;
1526 writel(val, pl08x->base + PL080_CONFIG);
1527}
1528
1529static void pl08x_tasklet(unsigned long data)
1530{
1531 struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
Linus Walleije8689e62010-09-28 15:57:37 +02001532 struct pl08x_driver_data *pl08x = plchan->host;
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001533 struct pl08x_txd *txd;
1534 dma_async_tx_callback callback = NULL;
1535 void *callback_param = NULL;
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001536 unsigned long flags;
Linus Walleije8689e62010-09-28 15:57:37 +02001537
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001538 spin_lock_irqsave(&plchan->lock, flags);
Linus Walleije8689e62010-09-28 15:57:37 +02001539
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001540 txd = plchan->at;
1541 plchan->at = NULL;
1542
1543 if (txd) {
1544 callback = txd->tx.callback;
1545 callback_param = txd->tx.callback_param;
Linus Walleije8689e62010-09-28 15:57:37 +02001546
1547 /*
1548 * Update last completed
1549 */
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001550 plchan->lc = txd->tx.cookie;
Linus Walleije8689e62010-09-28 15:57:37 +02001551
1552 /*
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001553 * Free the descriptor
Linus Walleije8689e62010-09-28 15:57:37 +02001554 */
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001555 pl08x_free_txd(pl08x, txd);
Linus Walleije8689e62010-09-28 15:57:37 +02001556 }
1557 /*
1558 * If a new descriptor is queued, set it up
1559 * plchan->at is NULL here
1560 */
1561 if (!list_empty(&plchan->desc_list)) {
1562 struct pl08x_txd *next;
1563
1564 next = list_first_entry(&plchan->desc_list,
1565 struct pl08x_txd,
1566 node);
1567 list_del(&next->node);
Russell King - ARM Linuxc885bee2011-01-03 22:38:52 +00001568
1569 pl08x_start_txd(plchan, next);
Linus Walleije8689e62010-09-28 15:57:37 +02001570 } else {
1571 struct pl08x_dma_chan *waiting = NULL;
1572
1573 /*
1574 * No more jobs, so free up the physical channel
1575 * Free any allocated signal on slave transfers too
1576 */
Russell King - ARM Linux8c8cc2b2011-01-03 22:36:09 +00001577 release_phy_channel(plchan);
Linus Walleije8689e62010-09-28 15:57:37 +02001578 plchan->state = PL08X_CHAN_IDLE;
1579
1580 /*
1581 * And NOW before anyone else can grab that free:d
1582 * up physical channel, see if there is some memcpy
1583 * pending that seriously needs to start because of
1584 * being stacked up while we were choking the
1585 * physical channels with data.
1586 */
1587 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1588 chan.device_node) {
1589 if (waiting->state == PL08X_CHAN_WAITING &&
1590 waiting->waiting != NULL) {
1591 int ret;
1592
1593 /* This should REALLY not fail now */
1594 ret = prep_phy_channel(waiting,
1595 waiting->waiting);
1596 BUG_ON(ret);
1597 waiting->state = PL08X_CHAN_RUNNING;
1598 waiting->waiting = NULL;
1599 pl08x_issue_pending(&waiting->chan);
1600 break;
1601 }
1602 }
1603 }
1604
Russell King - ARM Linuxbf072af2011-01-03 22:31:24 +00001605 spin_unlock_irqrestore(&plchan->lock, flags);
Russell King - ARM Linux858c21c2011-01-03 22:41:34 +00001606
1607 /* Callback to signal completion */
1608 if (callback)
1609 callback(callback_param);
Linus Walleije8689e62010-09-28 15:57:37 +02001610}
1611
1612static irqreturn_t pl08x_irq(int irq, void *dev)
1613{
1614 struct pl08x_driver_data *pl08x = dev;
1615 u32 mask = 0;
1616 u32 val;
1617 int i;
1618
1619 val = readl(pl08x->base + PL080_ERR_STATUS);
1620 if (val) {
1621 /*
1622 * An error interrupt (on one or more channels)
1623 */
1624 dev_err(&pl08x->adev->dev,
1625 "%s error interrupt, register value 0x%08x\n",
1626 __func__, val);
1627 /*
1628 * Simply clear ALL PL08X error interrupts,
1629 * regardless of channel and cause
1630 * FIXME: should be 0x00000003 on PL081 really.
1631 */
1632 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1633 }
1634 val = readl(pl08x->base + PL080_INT_STATUS);
1635 for (i = 0; i < pl08x->vd->channels; i++) {
1636 if ((1 << i) & val) {
1637 /* Locate physical channel */
1638 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1639 struct pl08x_dma_chan *plchan = phychan->serving;
1640
1641 /* Schedule tasklet on this channel */
1642 tasklet_schedule(&plchan->tasklet);
1643
1644 mask |= (1 << i);
1645 }
1646 }
1647 /*
1648 * Clear only the terminal interrupts on channels we processed
1649 */
1650 writel(mask, pl08x->base + PL080_TC_CLEAR);
1651
1652 return mask ? IRQ_HANDLED : IRQ_NONE;
1653}
1654
1655/*
1656 * Initialise the DMAC memcpy/slave channels.
1657 * Make a local wrapper to hold required data
1658 */
1659static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1660 struct dma_device *dmadev,
1661 unsigned int channels,
1662 bool slave)
1663{
1664 struct pl08x_dma_chan *chan;
1665 int i;
1666
1667 INIT_LIST_HEAD(&dmadev->channels);
1668 /*
1669 * Register as many many memcpy as we have physical channels,
1670 * we won't always be able to use all but the code will have
1671 * to cope with that situation.
1672 */
1673 for (i = 0; i < channels; i++) {
1674 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
1675 if (!chan) {
1676 dev_err(&pl08x->adev->dev,
1677 "%s no memory for channel\n", __func__);
1678 return -ENOMEM;
1679 }
1680
1681 chan->host = pl08x;
1682 chan->state = PL08X_CHAN_IDLE;
1683
1684 if (slave) {
1685 chan->slave = true;
1686 chan->name = pl08x->pd->slave_channels[i].bus_id;
1687 chan->cd = &pl08x->pd->slave_channels[i];
1688 } else {
1689 chan->cd = &pl08x->pd->memcpy_channel;
1690 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1691 if (!chan->name) {
1692 kfree(chan);
1693 return -ENOMEM;
1694 }
1695 }
Russell King - ARM Linuxb58b6b52011-01-03 22:34:48 +00001696 if (chan->cd->circular_buffer) {
1697 dev_err(&pl08x->adev->dev,
1698 "channel %s: circular buffers not supported\n",
1699 chan->name);
1700 kfree(chan);
1701 continue;
1702 }
Linus Walleije8689e62010-09-28 15:57:37 +02001703 dev_info(&pl08x->adev->dev,
1704 "initialize virtual channel \"%s\"\n",
1705 chan->name);
1706
1707 chan->chan.device = dmadev;
Russell King - ARM Linux91aa5fa2011-01-03 22:31:04 +00001708 chan->chan.cookie = 0;
1709 chan->lc = 0;
Linus Walleije8689e62010-09-28 15:57:37 +02001710
1711 spin_lock_init(&chan->lock);
1712 INIT_LIST_HEAD(&chan->desc_list);
1713 tasklet_init(&chan->tasklet, pl08x_tasklet,
1714 (unsigned long) chan);
1715
1716 list_add_tail(&chan->chan.device_node, &dmadev->channels);
1717 }
1718 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1719 i, slave ? "slave" : "memcpy");
1720 return i;
1721}
1722
1723static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1724{
1725 struct pl08x_dma_chan *chan = NULL;
1726 struct pl08x_dma_chan *next;
1727
1728 list_for_each_entry_safe(chan,
1729 next, &dmadev->channels, chan.device_node) {
1730 list_del(&chan->chan.device_node);
1731 kfree(chan);
1732 }
1733}
1734
1735#ifdef CONFIG_DEBUG_FS
1736static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1737{
1738 switch (state) {
1739 case PL08X_CHAN_IDLE:
1740 return "idle";
1741 case PL08X_CHAN_RUNNING:
1742 return "running";
1743 case PL08X_CHAN_PAUSED:
1744 return "paused";
1745 case PL08X_CHAN_WAITING:
1746 return "waiting";
1747 default:
1748 break;
1749 }
1750 return "UNKNOWN STATE";
1751}
1752
1753static int pl08x_debugfs_show(struct seq_file *s, void *data)
1754{
1755 struct pl08x_driver_data *pl08x = s->private;
1756 struct pl08x_dma_chan *chan;
1757 struct pl08x_phy_chan *ch;
1758 unsigned long flags;
1759 int i;
1760
1761 seq_printf(s, "PL08x physical channels:\n");
1762 seq_printf(s, "CHANNEL:\tUSER:\n");
1763 seq_printf(s, "--------\t-----\n");
1764 for (i = 0; i < pl08x->vd->channels; i++) {
1765 struct pl08x_dma_chan *virt_chan;
1766
1767 ch = &pl08x->phy_chans[i];
1768
1769 spin_lock_irqsave(&ch->lock, flags);
1770 virt_chan = ch->serving;
1771
1772 seq_printf(s, "%d\t\t%s\n",
1773 ch->id, virt_chan ? virt_chan->name : "(none)");
1774
1775 spin_unlock_irqrestore(&ch->lock, flags);
1776 }
1777
1778 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
1779 seq_printf(s, "CHANNEL:\tSTATE:\n");
1780 seq_printf(s, "--------\t------\n");
1781 list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001782 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001783 pl08x_state_str(chan->state));
1784 }
1785
1786 seq_printf(s, "\nPL08x virtual slave channels:\n");
1787 seq_printf(s, "CHANNEL:\tSTATE:\n");
1788 seq_printf(s, "--------\t------\n");
1789 list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
Russell King - ARM Linux3e2a0372011-01-03 22:32:46 +00001790 seq_printf(s, "%s\t\t%s\n", chan->name,
Linus Walleije8689e62010-09-28 15:57:37 +02001791 pl08x_state_str(chan->state));
1792 }
1793
1794 return 0;
1795}
1796
1797static int pl08x_debugfs_open(struct inode *inode, struct file *file)
1798{
1799 return single_open(file, pl08x_debugfs_show, inode->i_private);
1800}
1801
1802static const struct file_operations pl08x_debugfs_operations = {
1803 .open = pl08x_debugfs_open,
1804 .read = seq_read,
1805 .llseek = seq_lseek,
1806 .release = single_release,
1807};
1808
1809static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1810{
1811 /* Expose a simple debugfs interface to view all clocks */
1812 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
1813 NULL, pl08x,
1814 &pl08x_debugfs_operations);
1815}
1816
1817#else
1818static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1819{
1820}
1821#endif
1822
1823static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
1824{
1825 struct pl08x_driver_data *pl08x;
Russell King - ARM Linuxf96ca9e2011-01-03 22:35:08 +00001826 const struct vendor_data *vd = id->data;
Linus Walleije8689e62010-09-28 15:57:37 +02001827 int ret = 0;
1828 int i;
1829
1830 ret = amba_request_regions(adev, NULL);
1831 if (ret)
1832 return ret;
1833
1834 /* Create the driver state holder */
1835 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
1836 if (!pl08x) {
1837 ret = -ENOMEM;
1838 goto out_no_pl08x;
1839 }
1840
1841 /* Initialize memcpy engine */
1842 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1843 pl08x->memcpy.dev = &adev->dev;
1844 pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1845 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1846 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1847 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1848 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
1849 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
1850 pl08x->memcpy.device_control = pl08x_control;
1851
1852 /* Initialize slave engine */
1853 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
1854 pl08x->slave.dev = &adev->dev;
1855 pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1856 pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
1857 pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1858 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
1859 pl08x->slave.device_issue_pending = pl08x_issue_pending;
1860 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1861 pl08x->slave.device_control = pl08x_control;
1862
1863 /* Get the platform data */
1864 pl08x->pd = dev_get_platdata(&adev->dev);
1865 if (!pl08x->pd) {
1866 dev_err(&adev->dev, "no platform data supplied\n");
1867 goto out_no_platdata;
1868 }
1869
1870 /* Assign useful pointers to the driver state */
1871 pl08x->adev = adev;
1872 pl08x->vd = vd;
1873
Russell King - ARM Linux30749cb2011-01-03 22:41:13 +00001874 /* By default, AHB1 only. If dualmaster, from platform */
1875 pl08x->lli_buses = PL08X_AHB1;
1876 pl08x->mem_buses = PL08X_AHB1;
1877 if (pl08x->vd->dualmaster) {
1878 pl08x->lli_buses = pl08x->pd->lli_buses;
1879 pl08x->mem_buses = pl08x->pd->mem_buses;
1880 }
1881
Linus Walleije8689e62010-09-28 15:57:37 +02001882 /* A DMA memory pool for LLIs, align on 1-byte boundary */
1883 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
1884 PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
1885 if (!pl08x->pool) {
1886 ret = -ENOMEM;
1887 goto out_no_lli_pool;
1888 }
1889
1890 spin_lock_init(&pl08x->lock);
1891
1892 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
1893 if (!pl08x->base) {
1894 ret = -ENOMEM;
1895 goto out_no_ioremap;
1896 }
1897
1898 /* Turn on the PL08x */
1899 pl08x_ensure_on(pl08x);
1900
1901 /*
1902 * Attach the interrupt handler
1903 */
1904 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1905 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
1906
1907 ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001908 DRIVER_NAME, pl08x);
Linus Walleije8689e62010-09-28 15:57:37 +02001909 if (ret) {
1910 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
1911 __func__, adev->irq[0]);
1912 goto out_no_irq;
1913 }
1914
1915 /* Initialize physical channels */
1916 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
1917 GFP_KERNEL);
1918 if (!pl08x->phy_chans) {
1919 dev_err(&adev->dev, "%s failed to allocate "
1920 "physical channel holders\n",
1921 __func__);
1922 goto out_no_phychans;
1923 }
1924
1925 for (i = 0; i < vd->channels; i++) {
1926 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
1927
1928 ch->id = i;
1929 ch->base = pl08x->base + PL080_Cx_BASE(i);
1930 spin_lock_init(&ch->lock);
1931 ch->serving = NULL;
1932 ch->signal = -1;
1933 dev_info(&adev->dev,
1934 "physical channel %d is %s\n", i,
1935 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1936 }
1937
1938 /* Register as many memcpy channels as there are physical channels */
1939 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
1940 pl08x->vd->channels, false);
1941 if (ret <= 0) {
1942 dev_warn(&pl08x->adev->dev,
1943 "%s failed to enumerate memcpy channels - %d\n",
1944 __func__, ret);
1945 goto out_no_memcpy;
1946 }
1947 pl08x->memcpy.chancnt = ret;
1948
1949 /* Register slave channels */
1950 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
1951 pl08x->pd->num_slave_channels,
1952 true);
1953 if (ret <= 0) {
1954 dev_warn(&pl08x->adev->dev,
1955 "%s failed to enumerate slave channels - %d\n",
1956 __func__, ret);
1957 goto out_no_slave;
1958 }
1959 pl08x->slave.chancnt = ret;
1960
1961 ret = dma_async_device_register(&pl08x->memcpy);
1962 if (ret) {
1963 dev_warn(&pl08x->adev->dev,
1964 "%s failed to register memcpy as an async device - %d\n",
1965 __func__, ret);
1966 goto out_no_memcpy_reg;
1967 }
1968
1969 ret = dma_async_device_register(&pl08x->slave);
1970 if (ret) {
1971 dev_warn(&pl08x->adev->dev,
1972 "%s failed to register slave as an async device - %d\n",
1973 __func__, ret);
1974 goto out_no_slave_reg;
1975 }
1976
1977 amba_set_drvdata(adev, pl08x);
1978 init_pl08x_debugfs(pl08x);
Russell King - ARM Linuxb05cd8f2011-01-03 22:33:26 +00001979 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
1980 amba_part(adev), amba_rev(adev),
1981 (unsigned long long)adev->res.start, adev->irq[0]);
Linus Walleije8689e62010-09-28 15:57:37 +02001982 return 0;
1983
1984out_no_slave_reg:
1985 dma_async_device_unregister(&pl08x->memcpy);
1986out_no_memcpy_reg:
1987 pl08x_free_virtual_channels(&pl08x->slave);
1988out_no_slave:
1989 pl08x_free_virtual_channels(&pl08x->memcpy);
1990out_no_memcpy:
1991 kfree(pl08x->phy_chans);
1992out_no_phychans:
1993 free_irq(adev->irq[0], pl08x);
1994out_no_irq:
1995 iounmap(pl08x->base);
1996out_no_ioremap:
1997 dma_pool_destroy(pl08x->pool);
1998out_no_lli_pool:
1999out_no_platdata:
2000 kfree(pl08x);
2001out_no_pl08x:
2002 amba_release_regions(adev);
2003 return ret;
2004}
2005
2006/* PL080 has 8 channels and the PL080 have just 2 */
2007static struct vendor_data vendor_pl080 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002008 .channels = 8,
2009 .dualmaster = true,
2010};
2011
2012static struct vendor_data vendor_pl081 = {
Linus Walleije8689e62010-09-28 15:57:37 +02002013 .channels = 2,
2014 .dualmaster = false,
2015};
2016
2017static struct amba_id pl08x_ids[] = {
2018 /* PL080 */
2019 {
2020 .id = 0x00041080,
2021 .mask = 0x000fffff,
2022 .data = &vendor_pl080,
2023 },
2024 /* PL081 */
2025 {
2026 .id = 0x00041081,
2027 .mask = 0x000fffff,
2028 .data = &vendor_pl081,
2029 },
2030 /* Nomadik 8815 PL080 variant */
2031 {
2032 .id = 0x00280880,
2033 .mask = 0x00ffffff,
2034 .data = &vendor_pl080,
2035 },
2036 { 0, 0 },
2037};
2038
2039static struct amba_driver pl08x_amba_driver = {
2040 .drv.name = DRIVER_NAME,
2041 .id_table = pl08x_ids,
2042 .probe = pl08x_probe,
2043};
2044
2045static int __init pl08x_init(void)
2046{
2047 int retval;
2048 retval = amba_driver_register(&pl08x_amba_driver);
2049 if (retval)
2050 printk(KERN_WARNING DRIVER_NAME
Russell King - ARM Linuxe8b5e112011-01-03 22:30:24 +00002051 "failed to register as an AMBA device (%d)\n",
Linus Walleije8689e62010-09-28 15:57:37 +02002052 retval);
2053 return retval;
2054}
2055subsys_initcall(pl08x_init);