blob: 4d5a84815ba7bdd63379c97b189ddd14f5003964 [file] [log] [blame]
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001/*
2 * S3C24XX DMA handling
3 *
4 * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
5 *
6 * based on amba-pl08x.c
7 *
8 * Copyright (c) 2006 ARM Ltd.
9 * Copyright (c) 2010 ST-Ericsson SA
10 *
11 * Author: Peter Pearse <peter.pearse@arm.com>
12 * Author: Linus Walleij <linus.walleij@stericsson.com>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 *
19 * The DMA controllers in S3C24XX SoCs have a varying number of DMA signals
20 * that can be routed to any of the 4 to 8 hardware-channels.
21 *
22 * Therefore on these DMA controllers the number of channels
23 * and the number of incoming DMA signals are two totally different things.
24 * It is usually not possible to theoretically handle all physical signals,
25 * so a multiplexing scheme with possible denial of use is necessary.
26 *
27 * Open items:
28 * - bursts
29 */
30
31#include <linux/platform_device.h>
32#include <linux/types.h>
33#include <linux/dmaengine.h>
34#include <linux/dma-mapping.h>
35#include <linux/interrupt.h>
36#include <linux/clk.h>
37#include <linux/module.h>
38#include <linux/slab.h>
39#include <linux/platform_data/dma-s3c24xx.h>
40
41#include "dmaengine.h"
42#include "virt-dma.h"
43
44#define MAX_DMA_CHANNELS 8
45
46#define S3C24XX_DISRC 0x00
47#define S3C24XX_DISRCC 0x04
48#define S3C24XX_DISRCC_INC_INCREMENT 0
49#define S3C24XX_DISRCC_INC_FIXED BIT(0)
50#define S3C24XX_DISRCC_LOC_AHB 0
51#define S3C24XX_DISRCC_LOC_APB BIT(1)
52
53#define S3C24XX_DIDST 0x08
54#define S3C24XX_DIDSTC 0x0c
55#define S3C24XX_DIDSTC_INC_INCREMENT 0
56#define S3C24XX_DIDSTC_INC_FIXED BIT(0)
57#define S3C24XX_DIDSTC_LOC_AHB 0
58#define S3C24XX_DIDSTC_LOC_APB BIT(1)
59#define S3C24XX_DIDSTC_INT_TC0 0
60#define S3C24XX_DIDSTC_INT_RELOAD BIT(2)
61
62#define S3C24XX_DCON 0x10
63
64#define S3C24XX_DCON_TC_MASK 0xfffff
65#define S3C24XX_DCON_DSZ_BYTE (0 << 20)
66#define S3C24XX_DCON_DSZ_HALFWORD (1 << 20)
67#define S3C24XX_DCON_DSZ_WORD (2 << 20)
68#define S3C24XX_DCON_DSZ_MASK (3 << 20)
69#define S3C24XX_DCON_DSZ_SHIFT 20
70#define S3C24XX_DCON_AUTORELOAD 0
71#define S3C24XX_DCON_NORELOAD BIT(22)
72#define S3C24XX_DCON_HWTRIG BIT(23)
73#define S3C24XX_DCON_HWSRC_SHIFT 24
74#define S3C24XX_DCON_SERV_SINGLE 0
75#define S3C24XX_DCON_SERV_WHOLE BIT(27)
76#define S3C24XX_DCON_TSZ_UNIT 0
77#define S3C24XX_DCON_TSZ_BURST4 BIT(28)
78#define S3C24XX_DCON_INT BIT(29)
79#define S3C24XX_DCON_SYNC_PCLK 0
80#define S3C24XX_DCON_SYNC_HCLK BIT(30)
81#define S3C24XX_DCON_DEMAND 0
82#define S3C24XX_DCON_HANDSHAKE BIT(31)
83
84#define S3C24XX_DSTAT 0x14
85#define S3C24XX_DSTAT_STAT_BUSY BIT(20)
86#define S3C24XX_DSTAT_CURRTC_MASK 0xfffff
87
88#define S3C24XX_DMASKTRIG 0x20
89#define S3C24XX_DMASKTRIG_SWTRIG BIT(0)
90#define S3C24XX_DMASKTRIG_ON BIT(1)
91#define S3C24XX_DMASKTRIG_STOP BIT(2)
92
93#define S3C24XX_DMAREQSEL 0x24
94#define S3C24XX_DMAREQSEL_HW BIT(0)
95
96/*
97 * S3C2410, S3C2440 and S3C2442 SoCs cannot select any physical channel
98 * for a DMA source. Instead only specific channels are valid.
99 * All of these SoCs have 4 physical channels and the number of request
100 * source bits is 3. Additionally we also need 1 bit to mark the channel
101 * as valid.
102 * Therefore we separate the chansel element of the channel data into 4
103 * parts of 4 bits each, to hold the information if the channel is valid
104 * and the hw request source to use.
105 *
106 * Example:
107 * SDI is valid on channels 0, 2 and 3 - with varying hw request sources.
108 * For it the chansel field would look like
109 *
110 * ((BIT(3) | 1) << 3 * 4) | // channel 3, with request source 1
111 * ((BIT(3) | 2) << 2 * 4) | // channel 2, with request source 2
112 * ((BIT(3) | 2) << 0 * 4) // channel 0, with request source 2
113 */
114#define S3C24XX_CHANSEL_WIDTH 4
115#define S3C24XX_CHANSEL_VALID BIT(3)
116#define S3C24XX_CHANSEL_REQ_MASK 7
117
118/*
119 * struct soc_data - vendor-specific config parameters for individual SoCs
120 * @stride: spacing between the registers of each channel
121 * @has_reqsel: does the controller use the newer requestselection mechanism
122 * @has_clocks: are controllable dma-clocks present
123 */
124struct soc_data {
125 int stride;
126 bool has_reqsel;
127 bool has_clocks;
128};
129
130/*
131 * enum s3c24xx_dma_chan_state - holds the virtual channel states
132 * @S3C24XX_DMA_CHAN_IDLE: the channel is idle
133 * @S3C24XX_DMA_CHAN_RUNNING: the channel has allocated a physical transport
134 * channel and is running a transfer on it
135 * @S3C24XX_DMA_CHAN_WAITING: the channel is waiting for a physical transport
136 * channel to become available (only pertains to memcpy channels)
137 */
138enum s3c24xx_dma_chan_state {
139 S3C24XX_DMA_CHAN_IDLE,
140 S3C24XX_DMA_CHAN_RUNNING,
141 S3C24XX_DMA_CHAN_WAITING,
142};
143
144/*
145 * struct s3c24xx_sg - structure containing data per sg
146 * @src_addr: src address of sg
147 * @dst_addr: dst address of sg
148 * @len: transfer len in bytes
149 * @node: node for txd's dsg_list
150 */
151struct s3c24xx_sg {
152 dma_addr_t src_addr;
153 dma_addr_t dst_addr;
154 size_t len;
155 struct list_head node;
156};
157
158/*
159 * struct s3c24xx_txd - wrapper for struct dma_async_tx_descriptor
160 * @vd: virtual DMA descriptor
161 * @dsg_list: list of children sg's
162 * @at: sg currently being transfered
163 * @width: transfer width
164 * @disrcc: value for source control register
165 * @didstc: value for destination control register
166 * @dcon: base value for dcon register
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300167 * @cyclic: indicate cyclic transfer
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900168 */
169struct s3c24xx_txd {
170 struct virt_dma_desc vd;
171 struct list_head dsg_list;
172 struct list_head *at;
173 u8 width;
174 u32 disrcc;
175 u32 didstc;
176 u32 dcon;
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300177 bool cyclic;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900178};
179
180struct s3c24xx_dma_chan;
181
182/*
183 * struct s3c24xx_dma_phy - holder for the physical channels
184 * @id: physical index to this channel
185 * @valid: does the channel have all required elements
186 * @base: virtual memory base (remapped) for the this channel
187 * @irq: interrupt for this channel
188 * @clk: clock for this channel
189 * @lock: a lock to use when altering an instance of this struct
190 * @serving: virtual channel currently being served by this physicalchannel
191 * @host: a pointer to the host (internal use)
192 */
193struct s3c24xx_dma_phy {
194 unsigned int id;
195 bool valid;
196 void __iomem *base;
Dan Carpenter8f83f502013-11-07 10:52:00 +0300197 int irq;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900198 struct clk *clk;
199 spinlock_t lock;
200 struct s3c24xx_dma_chan *serving;
201 struct s3c24xx_dma_engine *host;
202};
203
204/*
205 * struct s3c24xx_dma_chan - this structure wraps a DMA ENGINE channel
206 * @id: the id of the channel
207 * @name: name of the channel
208 * @vc: wrappped virtual channel
209 * @phy: the physical channel utilized by this channel, if there is one
210 * @runtime_addr: address for RX/TX according to the runtime config
211 * @at: active transaction on this channel
212 * @lock: a lock for this channel data
213 * @host: a pointer to the host (internal use)
214 * @state: whether the channel is idle, running etc
215 * @slave: whether this channel is a device (slave) or for memcpy
216 */
217struct s3c24xx_dma_chan {
218 int id;
219 const char *name;
220 struct virt_dma_chan vc;
221 struct s3c24xx_dma_phy *phy;
222 struct dma_slave_config cfg;
223 struct s3c24xx_txd *at;
224 struct s3c24xx_dma_engine *host;
225 enum s3c24xx_dma_chan_state state;
226 bool slave;
227};
228
229/*
230 * struct s3c24xx_dma_engine - the local state holder for the S3C24XX
231 * @pdev: the corresponding platform device
232 * @pdata: platform data passed in from the platform/machine
233 * @base: virtual memory base (remapped)
234 * @slave: slave engine for this instance
235 * @memcpy: memcpy engine for this instance
236 * @phy_chans: array of data for the physical channels
237 */
238struct s3c24xx_dma_engine {
239 struct platform_device *pdev;
240 const struct s3c24xx_dma_platdata *pdata;
241 struct soc_data *sdata;
242 void __iomem *base;
243 struct dma_device slave;
244 struct dma_device memcpy;
245 struct s3c24xx_dma_phy *phy_chans;
246};
247
248/*
249 * Physical channel handling
250 */
251
252/*
253 * Check whether a certain channel is busy or not.
254 */
255static int s3c24xx_dma_phy_busy(struct s3c24xx_dma_phy *phy)
256{
257 unsigned int val = readl(phy->base + S3C24XX_DSTAT);
258 return val & S3C24XX_DSTAT_STAT_BUSY;
259}
260
261static bool s3c24xx_dma_phy_valid(struct s3c24xx_dma_chan *s3cchan,
262 struct s3c24xx_dma_phy *phy)
263{
264 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
265 const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
266 struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
267 int phyvalid;
268
269 /* every phy is valid for memcopy channels */
270 if (!s3cchan->slave)
271 return true;
272
273 /* On newer variants all phys can be used for all virtual channels */
274 if (s3cdma->sdata->has_reqsel)
275 return true;
276
277 phyvalid = (cdata->chansel >> (phy->id * S3C24XX_CHANSEL_WIDTH));
278 return (phyvalid & S3C24XX_CHANSEL_VALID) ? true : false;
279}
280
281/*
282 * Allocate a physical channel for a virtual channel
283 *
284 * Try to locate a physical channel to be used for this transfer. If all
285 * are taken return NULL and the requester will have to cope by using
286 * some fallback PIO mode or retrying later.
287 */
288static
289struct s3c24xx_dma_phy *s3c24xx_dma_get_phy(struct s3c24xx_dma_chan *s3cchan)
290{
291 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
292 const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
293 struct s3c24xx_dma_channel *cdata;
294 struct s3c24xx_dma_phy *phy = NULL;
295 unsigned long flags;
296 int i;
297 int ret;
298
299 if (s3cchan->slave)
300 cdata = &pdata->channels[s3cchan->id];
301
302 for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {
303 phy = &s3cdma->phy_chans[i];
304
305 if (!phy->valid)
306 continue;
307
308 if (!s3c24xx_dma_phy_valid(s3cchan, phy))
309 continue;
310
311 spin_lock_irqsave(&phy->lock, flags);
312
313 if (!phy->serving) {
314 phy->serving = s3cchan;
315 spin_unlock_irqrestore(&phy->lock, flags);
316 break;
317 }
318
319 spin_unlock_irqrestore(&phy->lock, flags);
320 }
321
322 /* No physical channel available, cope with it */
323 if (i == s3cdma->pdata->num_phy_channels) {
324 dev_warn(&s3cdma->pdev->dev, "no phy channel available\n");
325 return NULL;
326 }
327
328 /* start the phy clock */
329 if (s3cdma->sdata->has_clocks) {
330 ret = clk_enable(phy->clk);
331 if (ret) {
332 dev_err(&s3cdma->pdev->dev, "could not enable clock for channel %d, err %d\n",
333 phy->id, ret);
334 phy->serving = NULL;
335 return NULL;
336 }
337 }
338
339 return phy;
340}
341
342/*
343 * Mark the physical channel as free.
344 *
345 * This drops the link between the physical and virtual channel.
346 */
347static inline void s3c24xx_dma_put_phy(struct s3c24xx_dma_phy *phy)
348{
349 struct s3c24xx_dma_engine *s3cdma = phy->host;
350
351 if (s3cdma->sdata->has_clocks)
352 clk_disable(phy->clk);
353
354 phy->serving = NULL;
355}
356
357/*
358 * Stops the channel by writing the stop bit.
359 * This should not be used for an on-going transfer, but as a method of
360 * shutting down a channel (eg, when it's no longer used) or terminating a
361 * transfer.
362 */
363static void s3c24xx_dma_terminate_phy(struct s3c24xx_dma_phy *phy)
364{
365 writel(S3C24XX_DMASKTRIG_STOP, phy->base + S3C24XX_DMASKTRIG);
366}
367
368/*
369 * Virtual channel handling
370 */
371
372static inline
373struct s3c24xx_dma_chan *to_s3c24xx_dma_chan(struct dma_chan *chan)
374{
375 return container_of(chan, struct s3c24xx_dma_chan, vc.chan);
376}
377
378static u32 s3c24xx_dma_getbytes_chan(struct s3c24xx_dma_chan *s3cchan)
379{
380 struct s3c24xx_dma_phy *phy = s3cchan->phy;
381 struct s3c24xx_txd *txd = s3cchan->at;
382 u32 tc = readl(phy->base + S3C24XX_DSTAT) & S3C24XX_DSTAT_CURRTC_MASK;
383
384 return tc * txd->width;
385}
386
Maxime Ripard39ad4602014-11-17 14:42:31 +0100387static int s3c24xx_dma_set_runtime_config(struct dma_chan *chan,
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900388 struct dma_slave_config *config)
389{
Maxime Ripard39ad4602014-11-17 14:42:31 +0100390 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
391 unsigned long flags;
392 int ret = 0;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900393
394 /* Reject definitely invalid configurations */
395 if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
396 config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
397 return -EINVAL;
398
Maxime Ripard39ad4602014-11-17 14:42:31 +0100399 spin_lock_irqsave(&s3cchan->vc.lock, flags);
400
401 if (!s3cchan->slave) {
402 ret = -EINVAL;
403 goto out;
404 }
405
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900406 s3cchan->cfg = *config;
407
Maxime Ripard39ad4602014-11-17 14:42:31 +0100408out:
Maxime Ripard848e10b2015-01-13 21:16:23 +0100409 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
Maxime Ripard39ad4602014-11-17 14:42:31 +0100410 return ret;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900411}
412
413/*
414 * Transfer handling
415 */
416
417static inline
418struct s3c24xx_txd *to_s3c24xx_txd(struct dma_async_tx_descriptor *tx)
419{
420 return container_of(tx, struct s3c24xx_txd, vd.tx);
421}
422
423static struct s3c24xx_txd *s3c24xx_dma_get_txd(void)
424{
425 struct s3c24xx_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
426
427 if (txd) {
428 INIT_LIST_HEAD(&txd->dsg_list);
429 txd->dcon = S3C24XX_DCON_INT | S3C24XX_DCON_NORELOAD;
430 }
431
432 return txd;
433}
434
435static void s3c24xx_dma_free_txd(struct s3c24xx_txd *txd)
436{
437 struct s3c24xx_sg *dsg, *_dsg;
438
439 list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
440 list_del(&dsg->node);
441 kfree(dsg);
442 }
443
444 kfree(txd);
445}
446
447static void s3c24xx_dma_start_next_sg(struct s3c24xx_dma_chan *s3cchan,
448 struct s3c24xx_txd *txd)
449{
450 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
451 struct s3c24xx_dma_phy *phy = s3cchan->phy;
452 const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
453 struct s3c24xx_sg *dsg = list_entry(txd->at, struct s3c24xx_sg, node);
454 u32 dcon = txd->dcon;
455 u32 val;
456
457 /* transfer-size and -count from len and width */
458 switch (txd->width) {
459 case 1:
460 dcon |= S3C24XX_DCON_DSZ_BYTE | dsg->len;
461 break;
462 case 2:
463 dcon |= S3C24XX_DCON_DSZ_HALFWORD | (dsg->len / 2);
464 break;
465 case 4:
466 dcon |= S3C24XX_DCON_DSZ_WORD | (dsg->len / 4);
467 break;
468 }
469
470 if (s3cchan->slave) {
471 struct s3c24xx_dma_channel *cdata =
472 &pdata->channels[s3cchan->id];
473
474 if (s3cdma->sdata->has_reqsel) {
475 writel_relaxed((cdata->chansel << 1) |
476 S3C24XX_DMAREQSEL_HW,
477 phy->base + S3C24XX_DMAREQSEL);
478 } else {
479 int csel = cdata->chansel >> (phy->id *
480 S3C24XX_CHANSEL_WIDTH);
481
482 csel &= S3C24XX_CHANSEL_REQ_MASK;
483 dcon |= csel << S3C24XX_DCON_HWSRC_SHIFT;
484 dcon |= S3C24XX_DCON_HWTRIG;
485 }
486 } else {
487 if (s3cdma->sdata->has_reqsel)
488 writel_relaxed(0, phy->base + S3C24XX_DMAREQSEL);
489 }
490
491 writel_relaxed(dsg->src_addr, phy->base + S3C24XX_DISRC);
492 writel_relaxed(txd->disrcc, phy->base + S3C24XX_DISRCC);
493 writel_relaxed(dsg->dst_addr, phy->base + S3C24XX_DIDST);
494 writel_relaxed(txd->didstc, phy->base + S3C24XX_DIDSTC);
495 writel_relaxed(dcon, phy->base + S3C24XX_DCON);
496
497 val = readl_relaxed(phy->base + S3C24XX_DMASKTRIG);
498 val &= ~S3C24XX_DMASKTRIG_STOP;
499 val |= S3C24XX_DMASKTRIG_ON;
500
501 /* trigger the dma operation for memcpy transfers */
502 if (!s3cchan->slave)
503 val |= S3C24XX_DMASKTRIG_SWTRIG;
504
505 writel(val, phy->base + S3C24XX_DMASKTRIG);
506}
507
508/*
509 * Set the initial DMA register values and start first sg.
510 */
511static void s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan *s3cchan)
512{
513 struct s3c24xx_dma_phy *phy = s3cchan->phy;
514 struct virt_dma_desc *vd = vchan_next_desc(&s3cchan->vc);
515 struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
516
517 list_del(&txd->vd.node);
518
519 s3cchan->at = txd;
520
521 /* Wait for channel inactive */
522 while (s3c24xx_dma_phy_busy(phy))
523 cpu_relax();
524
525 /* point to the first element of the sg list */
526 txd->at = txd->dsg_list.next;
527 s3c24xx_dma_start_next_sg(s3cchan, txd);
528}
529
530static void s3c24xx_dma_free_txd_list(struct s3c24xx_dma_engine *s3cdma,
531 struct s3c24xx_dma_chan *s3cchan)
532{
533 LIST_HEAD(head);
534
535 vchan_get_all_descriptors(&s3cchan->vc, &head);
536 vchan_dma_desc_free_list(&s3cchan->vc, &head);
537}
538
539/*
540 * Try to allocate a physical channel. When successful, assign it to
541 * this virtual channel, and initiate the next descriptor. The
542 * virtual channel lock must be held at this point.
543 */
544static void s3c24xx_dma_phy_alloc_and_start(struct s3c24xx_dma_chan *s3cchan)
545{
546 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
547 struct s3c24xx_dma_phy *phy;
548
549 phy = s3c24xx_dma_get_phy(s3cchan);
550 if (!phy) {
551 dev_dbg(&s3cdma->pdev->dev, "no physical channel available for xfer on %s\n",
552 s3cchan->name);
553 s3cchan->state = S3C24XX_DMA_CHAN_WAITING;
554 return;
555 }
556
557 dev_dbg(&s3cdma->pdev->dev, "allocated physical channel %d for xfer on %s\n",
558 phy->id, s3cchan->name);
559
560 s3cchan->phy = phy;
561 s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
562
563 s3c24xx_dma_start_next_txd(s3cchan);
564}
565
566static void s3c24xx_dma_phy_reassign_start(struct s3c24xx_dma_phy *phy,
567 struct s3c24xx_dma_chan *s3cchan)
568{
569 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
570
571 dev_dbg(&s3cdma->pdev->dev, "reassigned physical channel %d for xfer on %s\n",
572 phy->id, s3cchan->name);
573
574 /*
575 * We do this without taking the lock; we're really only concerned
576 * about whether this pointer is NULL or not, and we're guaranteed
577 * that this will only be called when it _already_ is non-NULL.
578 */
579 phy->serving = s3cchan;
580 s3cchan->phy = phy;
581 s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
582 s3c24xx_dma_start_next_txd(s3cchan);
583}
584
585/*
586 * Free a physical DMA channel, potentially reallocating it to another
587 * virtual channel if we have any pending.
588 */
589static void s3c24xx_dma_phy_free(struct s3c24xx_dma_chan *s3cchan)
590{
591 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
592 struct s3c24xx_dma_chan *p, *next;
593
594retry:
595 next = NULL;
596
597 /* Find a waiting virtual channel for the next transfer. */
598 list_for_each_entry(p, &s3cdma->memcpy.channels, vc.chan.device_node)
599 if (p->state == S3C24XX_DMA_CHAN_WAITING) {
600 next = p;
601 break;
602 }
603
604 if (!next) {
605 list_for_each_entry(p, &s3cdma->slave.channels,
606 vc.chan.device_node)
607 if (p->state == S3C24XX_DMA_CHAN_WAITING &&
608 s3c24xx_dma_phy_valid(p, s3cchan->phy)) {
609 next = p;
610 break;
611 }
612 }
613
614 /* Ensure that the physical channel is stopped */
615 s3c24xx_dma_terminate_phy(s3cchan->phy);
616
617 if (next) {
618 bool success;
619
620 /*
621 * Eww. We know this isn't going to deadlock
622 * but lockdep probably doesn't.
623 */
624 spin_lock(&next->vc.lock);
625 /* Re-check the state now that we have the lock */
626 success = next->state == S3C24XX_DMA_CHAN_WAITING;
627 if (success)
628 s3c24xx_dma_phy_reassign_start(s3cchan->phy, next);
629 spin_unlock(&next->vc.lock);
630
631 /* If the state changed, try to find another channel */
632 if (!success)
633 goto retry;
634 } else {
635 /* No more jobs, so free up the physical channel */
636 s3c24xx_dma_put_phy(s3cchan->phy);
637 }
638
639 s3cchan->phy = NULL;
640 s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
641}
642
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900643static void s3c24xx_dma_desc_free(struct virt_dma_desc *vd)
644{
645 struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
646 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(vd->tx.chan);
647
648 if (!s3cchan->slave)
Dan Williams85726de2013-12-03 14:55:50 -0800649 dma_descriptor_unmap(&vd->tx);
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900650
651 s3c24xx_dma_free_txd(txd);
652}
653
654static irqreturn_t s3c24xx_dma_irq(int irq, void *data)
655{
656 struct s3c24xx_dma_phy *phy = data;
657 struct s3c24xx_dma_chan *s3cchan = phy->serving;
658 struct s3c24xx_txd *txd;
659
660 dev_dbg(&phy->host->pdev->dev, "interrupt on channel %d\n", phy->id);
661
662 /*
663 * Interrupts happen to notify the completion of a transfer and the
664 * channel should have moved into its stop state already on its own.
665 * Therefore interrupts on channels not bound to a virtual channel
666 * should never happen. Nevertheless send a terminate command to the
667 * channel if the unlikely case happens.
668 */
669 if (unlikely(!s3cchan)) {
670 dev_err(&phy->host->pdev->dev, "interrupt on unused channel %d\n",
671 phy->id);
672
673 s3c24xx_dma_terminate_phy(phy);
674
675 return IRQ_HANDLED;
676 }
677
678 spin_lock(&s3cchan->vc.lock);
679 txd = s3cchan->at;
680 if (txd) {
681 /* when more sg's are in this txd, start the next one */
682 if (!list_is_last(txd->at, &txd->dsg_list)) {
683 txd->at = txd->at->next;
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300684 if (txd->cyclic)
685 vchan_cyclic_callback(&txd->vd);
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900686 s3c24xx_dma_start_next_sg(s3cchan, txd);
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300687 } else if (!txd->cyclic) {
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900688 s3cchan->at = NULL;
689 vchan_cookie_complete(&txd->vd);
690
691 /*
692 * And start the next descriptor (if any),
693 * otherwise free this channel.
694 */
695 if (vchan_next_desc(&s3cchan->vc))
696 s3c24xx_dma_start_next_txd(s3cchan);
697 else
698 s3c24xx_dma_phy_free(s3cchan);
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300699 } else {
700 vchan_cyclic_callback(&txd->vd);
701
702 /* Cyclic: reset at beginning */
703 txd->at = txd->dsg_list.next;
704 s3c24xx_dma_start_next_sg(s3cchan, txd);
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900705 }
706 }
707 spin_unlock(&s3cchan->vc.lock);
708
709 return IRQ_HANDLED;
710}
711
712/*
713 * The DMA ENGINE API
714 */
715
Maxime Ripard39ad4602014-11-17 14:42:31 +0100716static int s3c24xx_dma_terminate_all(struct dma_chan *chan)
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900717{
718 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
719 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
720 unsigned long flags;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900721
722 spin_lock_irqsave(&s3cchan->vc.lock, flags);
723
Maxime Ripard39ad4602014-11-17 14:42:31 +0100724 if (!s3cchan->phy && !s3cchan->at) {
725 dev_err(&s3cdma->pdev->dev, "trying to terminate already stopped channel %d\n",
726 s3cchan->id);
727 return -EINVAL;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900728 }
729
Maxime Ripard39ad4602014-11-17 14:42:31 +0100730 s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
731
732 /* Mark physical channel as free */
733 if (s3cchan->phy)
734 s3c24xx_dma_phy_free(s3cchan);
735
736 /* Dequeue current job */
737 if (s3cchan->at) {
738 s3c24xx_dma_desc_free(&s3cchan->at->vd);
739 s3cchan->at = NULL;
740 }
741
742 /* Dequeue jobs not yet fired as well */
743 s3c24xx_dma_free_txd_list(s3cdma, s3cchan);
744
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900745 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
746
Maxime Ripard39ad4602014-11-17 14:42:31 +0100747 return 0;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900748}
749
750static int s3c24xx_dma_alloc_chan_resources(struct dma_chan *chan)
751{
752 return 0;
753}
754
755static void s3c24xx_dma_free_chan_resources(struct dma_chan *chan)
756{
757 /* Ensure all queued descriptors are freed */
758 vchan_free_chan_resources(to_virt_chan(chan));
759}
760
761static enum dma_status s3c24xx_dma_tx_status(struct dma_chan *chan,
762 dma_cookie_t cookie, struct dma_tx_state *txstate)
763{
764 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
765 struct s3c24xx_txd *txd;
766 struct s3c24xx_sg *dsg;
767 struct virt_dma_desc *vd;
768 unsigned long flags;
769 enum dma_status ret;
770 size_t bytes = 0;
771
772 spin_lock_irqsave(&s3cchan->vc.lock, flags);
773 ret = dma_cookie_status(chan, cookie, txstate);
Sachin Kamat415612c2013-10-31 10:48:09 +0530774 if (ret == DMA_COMPLETE) {
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900775 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
776 return ret;
777 }
778
779 /*
780 * There's no point calculating the residue if there's
781 * no txstate to store the value.
782 */
783 if (!txstate) {
784 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
785 return ret;
786 }
787
788 vd = vchan_find_desc(&s3cchan->vc, cookie);
789 if (vd) {
790 /* On the issued list, so hasn't been processed yet */
791 txd = to_s3c24xx_txd(&vd->tx);
792
793 list_for_each_entry(dsg, &txd->dsg_list, node)
794 bytes += dsg->len;
795 } else {
796 /*
797 * Currently running, so sum over the pending sg's and
798 * the currently active one.
799 */
800 txd = s3cchan->at;
801
802 dsg = list_entry(txd->at, struct s3c24xx_sg, node);
803 list_for_each_entry_from(dsg, &txd->dsg_list, node)
804 bytes += dsg->len;
805
806 bytes += s3c24xx_dma_getbytes_chan(s3cchan);
807 }
808 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
809
810 /*
811 * This cookie not complete yet
812 * Get number of bytes left in the active transactions and queue
813 */
814 dma_set_residue(txstate, bytes);
815
816 /* Whether waiting or running, we're in progress */
817 return ret;
818}
819
820/*
821 * Initialize a descriptor to be used by memcpy submit
822 */
823static struct dma_async_tx_descriptor *s3c24xx_dma_prep_memcpy(
824 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
825 size_t len, unsigned long flags)
826{
827 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
828 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
829 struct s3c24xx_txd *txd;
830 struct s3c24xx_sg *dsg;
831 int src_mod, dest_mod;
832
833 dev_dbg(&s3cdma->pdev->dev, "prepare memcpy of %d bytes from %s\n",
834 len, s3cchan->name);
835
836 if ((len & S3C24XX_DCON_TC_MASK) != len) {
837 dev_err(&s3cdma->pdev->dev, "memcpy size %d to large\n", len);
838 return NULL;
839 }
840
841 txd = s3c24xx_dma_get_txd();
842 if (!txd)
843 return NULL;
844
845 dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
846 if (!dsg) {
847 s3c24xx_dma_free_txd(txd);
848 return NULL;
849 }
850 list_add_tail(&dsg->node, &txd->dsg_list);
851
852 dsg->src_addr = src;
853 dsg->dst_addr = dest;
854 dsg->len = len;
855
856 /*
857 * Determine a suitable transfer width.
858 * The DMA controller cannot fetch/store information which is not
859 * naturally aligned on the bus, i.e., a 4 byte fetch must start at
860 * an address divisible by 4 - more generally addr % width must be 0.
861 */
862 src_mod = src % 4;
863 dest_mod = dest % 4;
864 switch (len % 4) {
865 case 0:
866 txd->width = (src_mod == 0 && dest_mod == 0) ? 4 : 1;
867 break;
868 case 2:
869 txd->width = ((src_mod == 2 || src_mod == 0) &&
870 (dest_mod == 2 || dest_mod == 0)) ? 2 : 1;
871 break;
872 default:
873 txd->width = 1;
874 break;
875 }
876
877 txd->disrcc = S3C24XX_DISRCC_LOC_AHB | S3C24XX_DISRCC_INC_INCREMENT;
878 txd->didstc = S3C24XX_DIDSTC_LOC_AHB | S3C24XX_DIDSTC_INC_INCREMENT;
879 txd->dcon |= S3C24XX_DCON_DEMAND | S3C24XX_DCON_SYNC_HCLK |
880 S3C24XX_DCON_SERV_WHOLE;
881
882 return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
883}
884
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300885static struct dma_async_tx_descriptor *s3c24xx_dma_prep_dma_cyclic(
886 struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200887 enum dma_transfer_direction direction, unsigned long flags)
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +0300888{
889 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
890 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
891 const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
892 struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
893 struct s3c24xx_txd *txd;
894 struct s3c24xx_sg *dsg;
895 unsigned sg_len;
896 dma_addr_t slave_addr;
897 u32 hwcfg = 0;
898 int i;
899
900 dev_dbg(&s3cdma->pdev->dev,
901 "prepare cyclic transaction of %zu bytes with period %zu from %s\n",
902 size, period, s3cchan->name);
903
904 if (!is_slave_direction(direction)) {
905 dev_err(&s3cdma->pdev->dev,
906 "direction %d unsupported\n", direction);
907 return NULL;
908 }
909
910 txd = s3c24xx_dma_get_txd();
911 if (!txd)
912 return NULL;
913
914 txd->cyclic = 1;
915
916 if (cdata->handshake)
917 txd->dcon |= S3C24XX_DCON_HANDSHAKE;
918
919 switch (cdata->bus) {
920 case S3C24XX_DMA_APB:
921 txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
922 hwcfg |= S3C24XX_DISRCC_LOC_APB;
923 break;
924 case S3C24XX_DMA_AHB:
925 txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
926 hwcfg |= S3C24XX_DISRCC_LOC_AHB;
927 break;
928 }
929
930 /*
931 * Always assume our peripheral desintation is a fixed
932 * address in memory.
933 */
934 hwcfg |= S3C24XX_DISRCC_INC_FIXED;
935
936 /*
937 * Individual dma operations are requested by the slave,
938 * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
939 */
940 txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
941
942 if (direction == DMA_MEM_TO_DEV) {
943 txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
944 S3C24XX_DISRCC_INC_INCREMENT;
945 txd->didstc = hwcfg;
946 slave_addr = s3cchan->cfg.dst_addr;
947 txd->width = s3cchan->cfg.dst_addr_width;
948 } else {
949 txd->disrcc = hwcfg;
950 txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
951 S3C24XX_DIDSTC_INC_INCREMENT;
952 slave_addr = s3cchan->cfg.src_addr;
953 txd->width = s3cchan->cfg.src_addr_width;
954 }
955
956 sg_len = size / period;
957
958 for (i = 0; i < sg_len; i++) {
959 dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
960 if (!dsg) {
961 s3c24xx_dma_free_txd(txd);
962 return NULL;
963 }
964 list_add_tail(&dsg->node, &txd->dsg_list);
965
966 dsg->len = period;
967 /* Check last period length */
968 if (i == sg_len - 1)
969 dsg->len = size - period * i;
970 if (direction == DMA_MEM_TO_DEV) {
971 dsg->src_addr = addr + period * i;
972 dsg->dst_addr = slave_addr;
973 } else { /* DMA_DEV_TO_MEM */
974 dsg->src_addr = slave_addr;
975 dsg->dst_addr = addr + period * i;
976 }
977 }
978
979 return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
980}
981
Heiko Stuebnerddeccb82013-10-08 06:42:10 +0900982static struct dma_async_tx_descriptor *s3c24xx_dma_prep_slave_sg(
983 struct dma_chan *chan, struct scatterlist *sgl,
984 unsigned int sg_len, enum dma_transfer_direction direction,
985 unsigned long flags, void *context)
986{
987 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
988 struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
989 const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
990 struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
991 struct s3c24xx_txd *txd;
992 struct s3c24xx_sg *dsg;
993 struct scatterlist *sg;
994 dma_addr_t slave_addr;
995 u32 hwcfg = 0;
996 int tmp;
997
998 dev_dbg(&s3cdma->pdev->dev, "prepare transaction of %d bytes from %s\n",
999 sg_dma_len(sgl), s3cchan->name);
1000
1001 txd = s3c24xx_dma_get_txd();
1002 if (!txd)
1003 return NULL;
1004
1005 if (cdata->handshake)
1006 txd->dcon |= S3C24XX_DCON_HANDSHAKE;
1007
1008 switch (cdata->bus) {
1009 case S3C24XX_DMA_APB:
1010 txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
1011 hwcfg |= S3C24XX_DISRCC_LOC_APB;
1012 break;
1013 case S3C24XX_DMA_AHB:
1014 txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
1015 hwcfg |= S3C24XX_DISRCC_LOC_AHB;
1016 break;
1017 }
1018
1019 /*
1020 * Always assume our peripheral desintation is a fixed
1021 * address in memory.
1022 */
1023 hwcfg |= S3C24XX_DISRCC_INC_FIXED;
1024
1025 /*
1026 * Individual dma operations are requested by the slave,
1027 * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
1028 */
1029 txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
1030
1031 if (direction == DMA_MEM_TO_DEV) {
1032 txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
1033 S3C24XX_DISRCC_INC_INCREMENT;
1034 txd->didstc = hwcfg;
1035 slave_addr = s3cchan->cfg.dst_addr;
1036 txd->width = s3cchan->cfg.dst_addr_width;
1037 } else if (direction == DMA_DEV_TO_MEM) {
1038 txd->disrcc = hwcfg;
1039 txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
1040 S3C24XX_DIDSTC_INC_INCREMENT;
1041 slave_addr = s3cchan->cfg.src_addr;
1042 txd->width = s3cchan->cfg.src_addr_width;
1043 } else {
1044 s3c24xx_dma_free_txd(txd);
1045 dev_err(&s3cdma->pdev->dev,
1046 "direction %d unsupported\n", direction);
1047 return NULL;
1048 }
1049
1050 for_each_sg(sgl, sg, sg_len, tmp) {
1051 dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
1052 if (!dsg) {
1053 s3c24xx_dma_free_txd(txd);
1054 return NULL;
1055 }
1056 list_add_tail(&dsg->node, &txd->dsg_list);
1057
1058 dsg->len = sg_dma_len(sg);
1059 if (direction == DMA_MEM_TO_DEV) {
1060 dsg->src_addr = sg_dma_address(sg);
1061 dsg->dst_addr = slave_addr;
1062 } else { /* DMA_DEV_TO_MEM */
1063 dsg->src_addr = slave_addr;
1064 dsg->dst_addr = sg_dma_address(sg);
1065 }
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001066 }
1067
1068 return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
1069}
1070
1071/*
1072 * Slave transactions callback to the slave device to allow
1073 * synchronization of slave DMA signals with the DMAC enable
1074 */
1075static void s3c24xx_dma_issue_pending(struct dma_chan *chan)
1076{
1077 struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
1078 unsigned long flags;
1079
1080 spin_lock_irqsave(&s3cchan->vc.lock, flags);
1081 if (vchan_issue_pending(&s3cchan->vc)) {
1082 if (!s3cchan->phy && s3cchan->state != S3C24XX_DMA_CHAN_WAITING)
1083 s3c24xx_dma_phy_alloc_and_start(s3cchan);
1084 }
1085 spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
1086}
1087
1088/*
1089 * Bringup and teardown
1090 */
1091
1092/*
1093 * Initialise the DMAC memcpy/slave channels.
1094 * Make a local wrapper to hold required data
1095 */
1096static int s3c24xx_dma_init_virtual_channels(struct s3c24xx_dma_engine *s3cdma,
1097 struct dma_device *dmadev, unsigned int channels, bool slave)
1098{
1099 struct s3c24xx_dma_chan *chan;
1100 int i;
1101
1102 INIT_LIST_HEAD(&dmadev->channels);
1103
1104 /*
1105 * Register as many many memcpy as we have physical channels,
1106 * we won't always be able to use all but the code will have
1107 * to cope with that situation.
1108 */
1109 for (i = 0; i < channels; i++) {
1110 chan = devm_kzalloc(dmadev->dev, sizeof(*chan), GFP_KERNEL);
1111 if (!chan) {
1112 dev_err(dmadev->dev,
1113 "%s no memory for channel\n", __func__);
1114 return -ENOMEM;
1115 }
1116
1117 chan->id = i;
1118 chan->host = s3cdma;
1119 chan->state = S3C24XX_DMA_CHAN_IDLE;
1120
1121 if (slave) {
1122 chan->slave = true;
1123 chan->name = kasprintf(GFP_KERNEL, "slave%d", i);
1124 if (!chan->name)
1125 return -ENOMEM;
1126 } else {
1127 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1128 if (!chan->name)
1129 return -ENOMEM;
1130 }
1131 dev_dbg(dmadev->dev,
1132 "initialize virtual channel \"%s\"\n",
1133 chan->name);
1134
1135 chan->vc.desc_free = s3c24xx_dma_desc_free;
1136 vchan_init(&chan->vc, dmadev);
1137 }
1138 dev_info(dmadev->dev, "initialized %d virtual %s channels\n",
1139 i, slave ? "slave" : "memcpy");
1140 return i;
1141}
1142
1143static void s3c24xx_dma_free_virtual_channels(struct dma_device *dmadev)
1144{
1145 struct s3c24xx_dma_chan *chan = NULL;
1146 struct s3c24xx_dma_chan *next;
1147
1148 list_for_each_entry_safe(chan,
1149 next, &dmadev->channels, vc.chan.device_node)
1150 list_del(&chan->vc.chan.device_node);
1151}
1152
Heiko Stuebner681a2fd2013-10-21 05:32:48 +09001153/* s3c2410, s3c2440 and s3c2442 have a 0x40 stride without separate clocks */
1154static struct soc_data soc_s3c2410 = {
1155 .stride = 0x40,
1156 .has_reqsel = false,
1157 .has_clocks = false,
1158};
1159
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001160/* s3c2412 and s3c2413 have a 0x40 stride and dmareqsel mechanism */
1161static struct soc_data soc_s3c2412 = {
1162 .stride = 0x40,
1163 .has_reqsel = true,
1164 .has_clocks = true,
1165};
1166
1167/* s3c2443 and following have a 0x100 stride and dmareqsel mechanism */
1168static struct soc_data soc_s3c2443 = {
1169 .stride = 0x100,
1170 .has_reqsel = true,
1171 .has_clocks = true,
1172};
1173
1174static struct platform_device_id s3c24xx_dma_driver_ids[] = {
1175 {
Heiko Stuebner681a2fd2013-10-21 05:32:48 +09001176 .name = "s3c2410-dma",
1177 .driver_data = (kernel_ulong_t)&soc_s3c2410,
1178 }, {
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001179 .name = "s3c2412-dma",
1180 .driver_data = (kernel_ulong_t)&soc_s3c2412,
1181 }, {
1182 .name = "s3c2443-dma",
1183 .driver_data = (kernel_ulong_t)&soc_s3c2443,
1184 },
1185 { },
1186};
1187
1188static struct soc_data *s3c24xx_dma_get_soc_data(struct platform_device *pdev)
1189{
1190 return (struct soc_data *)
1191 platform_get_device_id(pdev)->driver_data;
1192}
1193
1194static int s3c24xx_dma_probe(struct platform_device *pdev)
1195{
1196 const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
1197 struct s3c24xx_dma_engine *s3cdma;
1198 struct soc_data *sdata;
1199 struct resource *res;
1200 int ret;
1201 int i;
1202
1203 if (!pdata) {
1204 dev_err(&pdev->dev, "platform data missing\n");
1205 return -ENODEV;
1206 }
1207
1208 /* Basic sanity check */
1209 if (pdata->num_phy_channels > MAX_DMA_CHANNELS) {
1210 dev_err(&pdev->dev, "to many dma channels %d, max %d\n",
1211 pdata->num_phy_channels, MAX_DMA_CHANNELS);
1212 return -EINVAL;
1213 }
1214
1215 sdata = s3c24xx_dma_get_soc_data(pdev);
1216 if (!sdata)
1217 return -EINVAL;
1218
1219 s3cdma = devm_kzalloc(&pdev->dev, sizeof(*s3cdma), GFP_KERNEL);
1220 if (!s3cdma)
1221 return -ENOMEM;
1222
1223 s3cdma->pdev = pdev;
1224 s3cdma->pdata = pdata;
1225 s3cdma->sdata = sdata;
1226
1227 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1228 s3cdma->base = devm_ioremap_resource(&pdev->dev, res);
1229 if (IS_ERR(s3cdma->base))
1230 return PTR_ERR(s3cdma->base);
1231
1232 s3cdma->phy_chans = devm_kzalloc(&pdev->dev,
1233 sizeof(struct s3c24xx_dma_phy) *
1234 pdata->num_phy_channels,
1235 GFP_KERNEL);
1236 if (!s3cdma->phy_chans)
1237 return -ENOMEM;
1238
1239 /* aquire irqs and clocks for all physical channels */
1240 for (i = 0; i < pdata->num_phy_channels; i++) {
1241 struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1242 char clk_name[6];
1243
1244 phy->id = i;
1245 phy->base = s3cdma->base + (i * sdata->stride);
1246 phy->host = s3cdma;
1247
1248 phy->irq = platform_get_irq(pdev, i);
1249 if (phy->irq < 0) {
1250 dev_err(&pdev->dev, "failed to get irq %d, err %d\n",
1251 i, phy->irq);
1252 continue;
1253 }
1254
1255 ret = devm_request_irq(&pdev->dev, phy->irq, s3c24xx_dma_irq,
1256 0, pdev->name, phy);
1257 if (ret) {
1258 dev_err(&pdev->dev, "Unable to request irq for channel %d, error %d\n",
1259 i, ret);
1260 continue;
1261 }
1262
1263 if (sdata->has_clocks) {
1264 sprintf(clk_name, "dma.%d", i);
1265 phy->clk = devm_clk_get(&pdev->dev, clk_name);
1266 if (IS_ERR(phy->clk) && sdata->has_clocks) {
1267 dev_err(&pdev->dev, "unable to aquire clock for channel %d, error %lu",
1268 i, PTR_ERR(phy->clk));
1269 continue;
1270 }
1271
1272 ret = clk_prepare(phy->clk);
1273 if (ret) {
1274 dev_err(&pdev->dev, "clock for phy %d failed, error %d\n",
1275 i, ret);
1276 continue;
1277 }
1278 }
1279
1280 spin_lock_init(&phy->lock);
1281 phy->valid = true;
1282
1283 dev_dbg(&pdev->dev, "physical channel %d is %s\n",
1284 i, s3c24xx_dma_phy_busy(phy) ? "BUSY" : "FREE");
1285 }
1286
1287 /* Initialize memcpy engine */
1288 dma_cap_set(DMA_MEMCPY, s3cdma->memcpy.cap_mask);
1289 dma_cap_set(DMA_PRIVATE, s3cdma->memcpy.cap_mask);
1290 s3cdma->memcpy.dev = &pdev->dev;
1291 s3cdma->memcpy.device_alloc_chan_resources =
1292 s3c24xx_dma_alloc_chan_resources;
1293 s3cdma->memcpy.device_free_chan_resources =
1294 s3c24xx_dma_free_chan_resources;
1295 s3cdma->memcpy.device_prep_dma_memcpy = s3c24xx_dma_prep_memcpy;
1296 s3cdma->memcpy.device_tx_status = s3c24xx_dma_tx_status;
1297 s3cdma->memcpy.device_issue_pending = s3c24xx_dma_issue_pending;
Maxime Ripard39ad4602014-11-17 14:42:31 +01001298 s3cdma->memcpy.device_config = s3c24xx_dma_set_runtime_config;
1299 s3cdma->memcpy.device_terminate_all = s3c24xx_dma_terminate_all;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001300
1301 /* Initialize slave engine for SoC internal dedicated peripherals */
1302 dma_cap_set(DMA_SLAVE, s3cdma->slave.cap_mask);
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +03001303 dma_cap_set(DMA_CYCLIC, s3cdma->slave.cap_mask);
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001304 dma_cap_set(DMA_PRIVATE, s3cdma->slave.cap_mask);
1305 s3cdma->slave.dev = &pdev->dev;
1306 s3cdma->slave.device_alloc_chan_resources =
1307 s3c24xx_dma_alloc_chan_resources;
1308 s3cdma->slave.device_free_chan_resources =
1309 s3c24xx_dma_free_chan_resources;
1310 s3cdma->slave.device_tx_status = s3c24xx_dma_tx_status;
1311 s3cdma->slave.device_issue_pending = s3c24xx_dma_issue_pending;
1312 s3cdma->slave.device_prep_slave_sg = s3c24xx_dma_prep_slave_sg;
Vasily Khoruzhickc3e175e2014-05-20 23:23:02 +03001313 s3cdma->slave.device_prep_dma_cyclic = s3c24xx_dma_prep_dma_cyclic;
Maxime Ripard39ad4602014-11-17 14:42:31 +01001314 s3cdma->slave.device_config = s3c24xx_dma_set_runtime_config;
1315 s3cdma->slave.device_terminate_all = s3c24xx_dma_terminate_all;
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001316
1317 /* Register as many memcpy channels as there are physical channels */
1318 ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->memcpy,
1319 pdata->num_phy_channels, false);
1320 if (ret <= 0) {
1321 dev_warn(&pdev->dev,
1322 "%s failed to enumerate memcpy channels - %d\n",
1323 __func__, ret);
1324 goto err_memcpy;
1325 }
1326
1327 /* Register slave channels */
1328 ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->slave,
1329 pdata->num_channels, true);
1330 if (ret <= 0) {
1331 dev_warn(&pdev->dev,
1332 "%s failed to enumerate slave channels - %d\n",
1333 __func__, ret);
1334 goto err_slave;
1335 }
1336
1337 ret = dma_async_device_register(&s3cdma->memcpy);
1338 if (ret) {
1339 dev_warn(&pdev->dev,
1340 "%s failed to register memcpy as an async device - %d\n",
1341 __func__, ret);
1342 goto err_memcpy_reg;
1343 }
1344
1345 ret = dma_async_device_register(&s3cdma->slave);
1346 if (ret) {
1347 dev_warn(&pdev->dev,
1348 "%s failed to register slave as an async device - %d\n",
1349 __func__, ret);
1350 goto err_slave_reg;
1351 }
1352
1353 platform_set_drvdata(pdev, s3cdma);
1354 dev_info(&pdev->dev, "Loaded dma driver with %d physical channels\n",
1355 pdata->num_phy_channels);
1356
1357 return 0;
1358
1359err_slave_reg:
1360 dma_async_device_unregister(&s3cdma->memcpy);
1361err_memcpy_reg:
1362 s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
1363err_slave:
1364 s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
1365err_memcpy:
1366 if (sdata->has_clocks)
1367 for (i = 0; i < pdata->num_phy_channels; i++) {
1368 struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1369 if (phy->valid)
1370 clk_unprepare(phy->clk);
1371 }
1372
1373 return ret;
1374}
1375
1376static int s3c24xx_dma_remove(struct platform_device *pdev)
1377{
1378 const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
1379 struct s3c24xx_dma_engine *s3cdma = platform_get_drvdata(pdev);
1380 struct soc_data *sdata = s3c24xx_dma_get_soc_data(pdev);
1381 int i;
1382
1383 dma_async_device_unregister(&s3cdma->slave);
1384 dma_async_device_unregister(&s3cdma->memcpy);
1385
1386 s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
1387 s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
1388
1389 if (sdata->has_clocks)
1390 for (i = 0; i < pdata->num_phy_channels; i++) {
1391 struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1392 if (phy->valid)
1393 clk_unprepare(phy->clk);
1394 }
1395
1396 return 0;
1397}
1398
1399static struct platform_driver s3c24xx_dma_driver = {
1400 .driver = {
1401 .name = "s3c24xx-dma",
Heiko Stuebnerddeccb82013-10-08 06:42:10 +09001402 },
1403 .id_table = s3c24xx_dma_driver_ids,
1404 .probe = s3c24xx_dma_probe,
1405 .remove = s3c24xx_dma_remove,
1406};
1407
1408module_platform_driver(s3c24xx_dma_driver);
1409
1410bool s3c24xx_dma_filter(struct dma_chan *chan, void *param)
1411{
1412 struct s3c24xx_dma_chan *s3cchan;
1413
1414 if (chan->device->dev->driver != &s3c24xx_dma_driver.driver)
1415 return false;
1416
1417 s3cchan = to_s3c24xx_dma_chan(chan);
1418
1419 return s3cchan->id == (int)param;
1420}
1421EXPORT_SYMBOL(s3c24xx_dma_filter);
1422
1423MODULE_DESCRIPTION("S3C24XX DMA Driver");
1424MODULE_AUTHOR("Heiko Stuebner");
1425MODULE_LICENSE("GPL v2");