ARM: S3C64XX: DMA: Unify callback functions for success/failure
[safe/jmp/linux-2.6] / arch / arm / plat-s3c64xx / dma.c
1 /* linux/arch/arm/plat-s3c64xx/dma.c
2  *
3  * Copyright 2009 Openmoko, Inc.
4  * Copyright 2009 Simtec Electronics
5  *      Ben Dooks <ben@simtec.co.uk>
6  *      http://armlinux.simtec.co.uk/
7  *
8  * S3C64XX DMA core
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/dmapool.h>
19 #include <linux/sysdev.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
24 #include <linux/io.h>
25
26 #include <mach/dma.h>
27 #include <mach/map.h>
28 #include <mach/irqs.h>
29
30 #include <plat/dma-plat.h>
31 #include <plat/regs-sys.h>
32
33 #include <asm/hardware/pl080.h>
34
35 /* dma channel state information */
36
37 struct s3c64xx_dmac {
38         struct sys_device        sysdev;
39         struct clk              *clk;
40         void __iomem            *regs;
41         struct s3c2410_dma_chan *channels;
42         enum dma_ch              chanbase;
43 };
44
45 /* pool to provide LLI buffers */
46 static struct dma_pool *dma_pool;
47
48 /* Debug configuration and code */
49
50 static unsigned char debug_show_buffs = 0;
51
52 static void dbg_showchan(struct s3c2410_dma_chan *chan)
53 {
54         pr_debug("DMA%d: %08x->%08x L %08x C %08x,%08x S %08x\n",
55                  chan->number,
56                  readl(chan->regs + PL080_CH_SRC_ADDR),
57                  readl(chan->regs + PL080_CH_DST_ADDR),
58                  readl(chan->regs + PL080_CH_LLI),
59                  readl(chan->regs + PL080_CH_CONTROL),
60                  readl(chan->regs + PL080S_CH_CONTROL2),
61                  readl(chan->regs + PL080S_CH_CONFIG));
62 }
63
64 static void show_lli(struct pl080s_lli *lli)
65 {
66         pr_debug("LLI[%p] %08x->%08x, NL %08x C %08x,%08x\n",
67                  lli, lli->src_addr, lli->dst_addr, lli->next_lli,
68                  lli->control0, lli->control1);
69 }
70
71 static void dbg_showbuffs(struct s3c2410_dma_chan *chan)
72 {
73         struct s3c64xx_dma_buff *ptr;
74         struct s3c64xx_dma_buff *end;
75
76         pr_debug("DMA%d: buffs next %p, curr %p, end %p\n",
77                  chan->number, chan->next, chan->curr, chan->end);
78
79         ptr = chan->next;
80         end = chan->end;
81
82         if (debug_show_buffs) {
83                 for (; ptr != NULL; ptr = ptr->next) {
84                         pr_debug("DMA%d: %08x ",
85                                  chan->number, ptr->lli_dma);
86                         show_lli(ptr->lli);
87                 }
88         }
89 }
90
91 /* End of Debug */
92
93 static struct s3c2410_dma_chan *s3c64xx_dma_map_channel(unsigned int channel)
94 {
95         struct s3c2410_dma_chan *chan;
96         unsigned int start, offs;
97
98         start = 0;
99
100         if (channel >= DMACH_PCM1_TX)
101                 start = 8;
102
103         for (offs = 0; offs < 8; offs++) {
104                 chan = &s3c2410_chans[start + offs];
105                 if (!chan->in_use)
106                         goto found;
107         }
108
109         return NULL;
110
111 found:
112         s3c_dma_chan_map[channel] = chan;
113         return chan;
114 }
115
116 int s3c2410_dma_config(unsigned int channel, int xferunit)
117 {
118         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
119
120         if (chan == NULL)
121                 return -EINVAL;
122
123         switch (xferunit) {
124         case 1:
125                 chan->hw_width = 0;
126                 break;
127         case 2:
128                 chan->hw_width = 1;
129                 break;
130         case 4:
131                 chan->hw_width = 2;
132                 break;
133         default:
134                 printk(KERN_ERR "%s: illegal width %d\n", __func__, xferunit);
135                 return -EINVAL;
136         }
137
138         return 0;
139 }
140 EXPORT_SYMBOL(s3c2410_dma_config);
141
142 static void s3c64xx_dma_fill_lli(struct s3c2410_dma_chan *chan,
143                                  struct pl080s_lli *lli,
144                                  dma_addr_t data, int size)
145 {
146         dma_addr_t src, dst;
147         u32 control0, control1;
148
149         switch (chan->source) {
150         case S3C2410_DMASRC_HW:
151                 src = chan->dev_addr;
152                 dst = data;
153                 control0 = PL080_CONTROL_SRC_AHB2;
154                 control0 |= (u32)chan->hw_width << PL080_CONTROL_SWIDTH_SHIFT;
155                 control0 |= 2 << PL080_CONTROL_DWIDTH_SHIFT;
156                 control0 |= PL080_CONTROL_DST_INCR;
157                 break;
158
159         case S3C2410_DMASRC_MEM:
160                 src = data;
161                 dst = chan->dev_addr;
162                 control0 = PL080_CONTROL_DST_AHB2;
163                 control0 |= (u32)chan->hw_width << PL080_CONTROL_DWIDTH_SHIFT;
164                 control0 |= 2 << PL080_CONTROL_SWIDTH_SHIFT;
165                 control0 |= PL080_CONTROL_SRC_INCR;
166                 break;
167         default:
168                 BUG();
169         }
170
171         /* note, we do not currently setup any of the burst controls */
172
173         control1 = size >> chan->hw_width;      /* size in no of xfers */
174         control0 |= PL080_CONTROL_PROT_SYS;     /* always in priv. mode */
175         control0 |= PL080_CONTROL_TC_IRQ_EN;    /* always fire IRQ */
176
177         lli->src_addr = src;
178         lli->dst_addr = dst;
179         lli->next_lli = 0;
180         lli->control0 = control0;
181         lli->control1 = control1;
182 }
183
184 static void s3c64xx_lli_to_regs(struct s3c2410_dma_chan *chan,
185                                 struct pl080s_lli *lli)
186 {
187         void __iomem *regs = chan->regs;
188
189         pr_debug("%s: LLI %p => regs\n", __func__, lli);
190         show_lli(lli);
191
192         writel(lli->src_addr, regs + PL080_CH_SRC_ADDR);
193         writel(lli->dst_addr, regs + PL080_CH_DST_ADDR);
194         writel(lli->next_lli, regs + PL080_CH_LLI);
195         writel(lli->control0, regs + PL080_CH_CONTROL);
196         writel(lli->control1, regs + PL080S_CH_CONTROL2);
197 }
198
199 static int s3c64xx_dma_start(struct s3c2410_dma_chan *chan)
200 {
201         struct s3c64xx_dmac *dmac = chan->dmac;
202         u32 config;
203         u32 bit = chan->bit;
204
205         dbg_showchan(chan);
206
207         pr_debug("%s: clearing interrupts\n", __func__);
208
209         /* clear interrupts */
210         writel(bit, dmac->regs + PL080_TC_CLEAR);
211         writel(bit, dmac->regs + PL080_ERR_CLEAR);
212
213         pr_debug("%s: starting channel\n", __func__);
214
215         config = readl(chan->regs + PL080S_CH_CONFIG);
216         config |= PL080_CONFIG_ENABLE;
217
218         pr_debug("%s: writing config %08x\n", __func__, config);
219         writel(config, chan->regs + PL080S_CH_CONFIG);
220
221         return 0;
222 }
223
224 static int s3c64xx_dma_stop(struct s3c2410_dma_chan *chan)
225 {
226         u32 config;
227         int timeout;
228
229         pr_debug("%s: stopping channel\n", __func__);
230
231         dbg_showchan(chan);
232
233         config = readl(chan->regs + PL080S_CH_CONFIG);
234         config |= PL080_CONFIG_HALT;
235         writel(config, chan->regs + PL080S_CH_CONFIG);
236
237         timeout = 1000;
238         do {
239                 config = readl(chan->regs + PL080S_CH_CONFIG);
240                 pr_debug("%s: %d - config %08x\n", __func__, timeout, config);
241                 if (config & PL080_CONFIG_ACTIVE)
242                         udelay(10);
243                 else
244                         break;
245                 } while (--timeout > 0);
246
247         if (config & PL080_CONFIG_ACTIVE) {
248                 printk(KERN_ERR "%s: channel still active\n", __func__);
249                 return -EFAULT;
250         }
251
252         config = readl(chan->regs + PL080S_CH_CONFIG);
253         config &= ~PL080_CONFIG_ENABLE;
254         writel(config, chan->regs + PL080S_CH_CONFIG);
255
256         return 0;
257 }
258
259 static inline void s3c64xx_dma_bufffdone(struct s3c2410_dma_chan *chan,
260                                          struct s3c64xx_dma_buff *buf,
261                                          enum s3c2410_dma_buffresult result)
262 {
263         if (chan->callback_fn != NULL)
264                 (chan->callback_fn)(chan, buf->pw, 0, result);
265 }
266
267 static void s3c64xx_dma_freebuff(struct s3c64xx_dma_buff *buff)
268 {
269         dma_pool_free(dma_pool, buff->lli, buff->lli_dma);
270         kfree(buff);
271 }
272
273 static int s3c64xx_dma_flush(struct s3c2410_dma_chan *chan)
274 {
275         struct s3c64xx_dma_buff *buff, *next;
276         u32 config;
277
278         dbg_showchan(chan);
279
280         pr_debug("%s: flushing channel\n", __func__);
281
282         config = readl(chan->regs + PL080S_CH_CONFIG);
283         config &= ~PL080_CONFIG_ENABLE;
284         writel(config, chan->regs + PL080S_CH_CONFIG);
285
286         /* dump all the buffers associated with this channel */
287
288         for (buff = chan->curr; buff != NULL; buff = next) {
289                 next = buff->next;
290                 pr_debug("%s: buff %p (next %p)\n", __func__, buff, buff->next);
291
292                 s3c64xx_dma_bufffdone(chan, buff, S3C2410_RES_ABORT);
293                 s3c64xx_dma_freebuff(buff);
294         }
295
296         chan->curr = chan->next = chan->end = NULL;
297
298         return 0;
299 }
300
301 int s3c2410_dma_ctrl(unsigned int channel, enum s3c2410_chan_op op)
302 {
303         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
304
305         WARN_ON(!chan);
306         if (!chan)
307                 return -EINVAL;
308
309         switch (op) {
310         case S3C2410_DMAOP_START:
311                 return s3c64xx_dma_start(chan);
312
313         case S3C2410_DMAOP_STOP:
314                 return s3c64xx_dma_stop(chan);
315
316         case S3C2410_DMAOP_FLUSH:
317                 return s3c64xx_dma_flush(chan);
318
319         /* belive PAUSE/RESUME are no-ops */
320         case S3C2410_DMAOP_PAUSE:
321         case S3C2410_DMAOP_RESUME:
322         case S3C2410_DMAOP_STARTED:
323         case S3C2410_DMAOP_TIMEOUT:
324                 return 0;
325         }
326
327         return -ENOENT;
328 }
329 EXPORT_SYMBOL(s3c2410_dma_ctrl);
330
331 /* s3c2410_dma_enque
332  *
333  */
334
335 int s3c2410_dma_enqueue(unsigned int channel, void *id,
336                         dma_addr_t data, int size)
337 {
338         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
339         struct s3c64xx_dma_buff *next;
340         struct s3c64xx_dma_buff *buff;
341         struct pl080s_lli *lli;
342         unsigned long flags;
343         int ret;
344
345         WARN_ON(!chan);
346         if (!chan)
347                 return -EINVAL;
348
349         buff = kzalloc(sizeof(struct s3c64xx_dma_buff), GFP_ATOMIC);
350         if (!buff) {
351                 printk(KERN_ERR "%s: no memory for buffer\n", __func__);
352                 return -ENOMEM;
353         }
354
355         lli = dma_pool_alloc(dma_pool, GFP_ATOMIC, &buff->lli_dma);
356         if (!lli) {
357                 printk(KERN_ERR "%s: no memory for lli\n", __func__);
358                 ret = -ENOMEM;
359                 goto err_buff;
360         }
361
362         pr_debug("%s: buff %p, dp %08x lli (%p, %08x) %d\n",
363                  __func__, buff, data, lli, (u32)buff->lli_dma, size);
364
365         buff->lli = lli;
366         buff->pw = id;
367
368         s3c64xx_dma_fill_lli(chan, lli, data, size);
369
370         local_irq_save(flags);
371
372         if ((next = chan->next) != NULL) {
373                 struct s3c64xx_dma_buff *end = chan->end;
374                 struct pl080s_lli *endlli = end->lli;
375
376                 pr_debug("enquing onto channel\n");
377
378                 end->next = buff;
379                 endlli->next_lli = buff->lli_dma;
380
381                 if (chan->flags & S3C2410_DMAF_CIRCULAR) {
382                         struct s3c64xx_dma_buff *curr = chan->curr;
383                         lli->next_lli = curr->lli_dma;
384                 }
385
386                 if (next == chan->curr) {
387                         writel(buff->lli_dma, chan->regs + PL080_CH_LLI);
388                         chan->next = buff;
389                 }
390
391                 show_lli(endlli);
392                 chan->end = buff;
393         } else {
394                 pr_debug("enquing onto empty channel\n");
395
396                 chan->curr = buff;
397                 chan->next = buff;
398                 chan->end = buff;
399
400                 s3c64xx_lli_to_regs(chan, lli);
401         }
402
403         local_irq_restore(flags);
404
405         show_lli(lli);
406
407         dbg_showchan(chan);
408         dbg_showbuffs(chan);
409         return 0;
410
411 err_buff:
412         kfree(buff);
413         return ret;
414 }
415
416 EXPORT_SYMBOL(s3c2410_dma_enqueue);
417
418
419 int s3c2410_dma_devconfig(int channel,
420                           enum s3c2410_dmasrc source,
421                           unsigned long devaddr)
422 {
423         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
424         u32 peripheral;
425         u32 config = 0;
426
427         pr_debug("%s: channel %d, source %d, dev %08lx, chan %p\n",
428                  __func__, channel, source, devaddr, chan);
429
430         WARN_ON(!chan);
431         if (!chan)
432                 return -EINVAL;
433
434         peripheral = (chan->peripheral & 0xf);
435         chan->source = source;
436         chan->dev_addr = devaddr;
437
438         pr_debug("%s: peripheral %d\n", __func__, peripheral);
439
440         switch (source) {
441         case S3C2410_DMASRC_HW:
442                 config = 2 << PL080_CONFIG_FLOW_CONTROL_SHIFT;
443                 config |= peripheral << PL080_CONFIG_SRC_SEL_SHIFT;
444                 break;
445         case S3C2410_DMASRC_MEM:
446                 config = 1 << PL080_CONFIG_FLOW_CONTROL_SHIFT;
447                 config |= peripheral << PL080_CONFIG_DST_SEL_SHIFT;
448                 break;
449         default:
450                 printk(KERN_ERR "%s: bad source\n", __func__);
451                 return -EINVAL;
452         }
453
454         /* allow TC and ERR interrupts */
455         config |= PL080_CONFIG_TC_IRQ_MASK;
456         config |= PL080_CONFIG_ERR_IRQ_MASK;
457
458         pr_debug("%s: config %08x\n", __func__, config);
459
460         writel(config, chan->regs + PL080S_CH_CONFIG);
461
462         return 0;
463 }
464 EXPORT_SYMBOL(s3c2410_dma_devconfig);
465
466
467 int s3c2410_dma_getposition(unsigned int channel,
468                             dma_addr_t *src, dma_addr_t *dst)
469 {
470         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
471
472         WARN_ON(!chan);
473         if (!chan)
474                 return -EINVAL;
475
476         if (src != NULL)
477                 *src = readl(chan->regs + PL080_CH_SRC_ADDR);
478
479         if (dst != NULL)
480                 *dst = readl(chan->regs + PL080_CH_DST_ADDR);
481
482         return 0;
483 }
484 EXPORT_SYMBOL(s3c2410_dma_getposition);
485
486 /* s3c2410_request_dma
487  *
488  * get control of an dma channel
489 */
490
491 int s3c2410_dma_request(unsigned int channel,
492                         struct s3c2410_dma_client *client,
493                         void *dev)
494 {
495         struct s3c2410_dma_chan *chan;
496         unsigned long flags;
497
498         pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
499                  channel, client->name, dev);
500
501         local_irq_save(flags);
502
503         chan = s3c64xx_dma_map_channel(channel);
504         if (chan == NULL) {
505                 local_irq_restore(flags);
506                 return -EBUSY;
507         }
508
509         dbg_showchan(chan);
510
511         chan->client = client;
512         chan->in_use = 1;
513         chan->peripheral = channel;
514
515         local_irq_restore(flags);
516
517         /* need to setup */
518
519         pr_debug("%s: channel initialised, %p\n", __func__, chan);
520
521         return chan->number | DMACH_LOW_LEVEL;
522 }
523
524 EXPORT_SYMBOL(s3c2410_dma_request);
525
526 /* s3c2410_dma_free
527  *
528  * release the given channel back to the system, will stop and flush
529  * any outstanding transfers, and ensure the channel is ready for the
530  * next claimant.
531  *
532  * Note, although a warning is currently printed if the freeing client
533  * info is not the same as the registrant's client info, the free is still
534  * allowed to go through.
535 */
536
537 int s3c2410_dma_free(unsigned int channel, struct s3c2410_dma_client *client)
538 {
539         struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
540         unsigned long flags;
541
542         if (chan == NULL)
543                 return -EINVAL;
544
545         local_irq_save(flags);
546
547         if (chan->client != client) {
548                 printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
549                        channel, chan->client, client);
550         }
551
552         /* sort out stopping and freeing the channel */
553
554
555         chan->client = NULL;
556         chan->in_use = 0;
557
558         if (!(channel & DMACH_LOW_LEVEL))
559                 s3c_dma_chan_map[channel] = NULL;
560
561         local_irq_restore(flags);
562
563         return 0;
564 }
565
566 EXPORT_SYMBOL(s3c2410_dma_free);
567
568 static irqreturn_t s3c64xx_dma_irq(int irq, void *pw)
569 {
570         struct s3c64xx_dmac *dmac = pw;
571         struct s3c2410_dma_chan *chan;
572         enum s3c2410_dma_buffresult res;
573         u32 tcstat, errstat;
574         u32 bit;
575         int offs;
576
577         tcstat = readl(dmac->regs + PL080_TC_STATUS);
578         errstat = readl(dmac->regs + PL080_ERR_STATUS);
579
580         for (offs = 0, bit = 1; offs < 8; offs++, bit <<= 1) {
581
582                 if (!(errstat & bit) && !(tcstat & bit))
583                         continue;
584
585                 chan = dmac->channels + offs;
586                 res = S3C2410_RES_ERR;
587
588                 if (tcstat & bit) {
589                         writel(bit, dmac->regs + PL080_TC_CLEAR);
590                         res = S3C2410_RES_OK;
591                 }
592
593                 if (errstat & bit)
594                         writel(bit, dmac->regs + PL080_ERR_CLEAR);
595
596                 s3c64xx_dma_bufffdone(chan, chan->curr, res);
597         }
598
599         return IRQ_HANDLED;
600 }
601
602 static struct sysdev_class dma_sysclass = {
603         .name           = "s3c64xx-dma",
604 };
605
606 static int s3c64xx_dma_init1(int chno, enum dma_ch chbase,
607                              int irq, unsigned int base)
608 {
609         struct s3c2410_dma_chan *chptr = &s3c2410_chans[chno];
610         struct s3c64xx_dmac *dmac;
611         char clkname[16];
612         void __iomem *regs;
613         void __iomem *regptr;
614         int err, ch;
615
616         dmac = kzalloc(sizeof(struct s3c64xx_dmac), GFP_KERNEL);
617         if (!dmac) {
618                 printk(KERN_ERR "%s: failed to alloc mem\n", __func__);
619                 return -ENOMEM;
620         }
621
622         dmac->sysdev.id = chno / 8;
623         dmac->sysdev.cls = &dma_sysclass;
624
625         err = sysdev_register(&dmac->sysdev);
626         if (err) {
627                 printk(KERN_ERR "%s: failed to register sysdevice\n", __func__);
628                 goto err_alloc;
629         }
630
631         regs = ioremap(base, 0x200);
632         if (!regs) {
633                 printk(KERN_ERR "%s: failed to ioremap()\n", __func__);
634                 err = -ENXIO;
635                 goto err_dev;
636         }
637
638         snprintf(clkname, sizeof(clkname), "dma%d", dmac->sysdev.id);
639
640         dmac->clk = clk_get(NULL, clkname);
641         if (IS_ERR(dmac->clk)) {
642                 printk(KERN_ERR "%s: failed to get clock %s\n", __func__, clkname);
643                 err = PTR_ERR(dmac->clk);
644                 goto err_map;
645         }
646
647         clk_enable(dmac->clk);
648
649         dmac->regs = regs;
650         dmac->chanbase = chbase;
651         dmac->channels = chptr;
652
653         err = request_irq(irq, s3c64xx_dma_irq, 0, "DMA", dmac);
654         if (err < 0) {
655                 printk(KERN_ERR "%s: failed to get irq\n", __func__);
656                 goto err_clk;
657         }
658
659         regptr = regs + PL080_Cx_BASE(0);
660
661         for (ch = 0; ch < 8; ch++, chno++, chptr++) {
662                 printk(KERN_INFO "%s: registering DMA %d (%p)\n",
663                        __func__, chno, regptr);
664
665                 chptr->bit = 1 << ch;
666                 chptr->number = chno;
667                 chptr->dmac = dmac;
668                 chptr->regs = regptr;
669                 regptr += PL008_Cx_STRIDE;
670         }
671
672         /* for the moment, permanently enable the controller */
673         writel(PL080_CONFIG_ENABLE, regs + PL080_CONFIG);
674
675         printk(KERN_INFO "PL080: IRQ %d, at %p\n", irq, regs);
676
677         return 0;
678
679 err_clk:
680         clk_disable(dmac->clk);
681         clk_put(dmac->clk);
682 err_map:
683         iounmap(regs);
684 err_dev:
685         sysdev_unregister(&dmac->sysdev);
686 err_alloc:
687         kfree(dmac);
688         return err;
689 }
690
691 static int __init s3c64xx_dma_init(void)
692 {
693         int ret;
694
695         printk(KERN_INFO "%s: Registering DMA channels\n", __func__);
696
697         dma_pool = dma_pool_create("DMA-LLI", NULL, sizeof(struct pl080s_lli), 16, 0);
698         if (!dma_pool) {
699                 printk(KERN_ERR "%s: failed to create pool\n", __func__);
700                 return -ENOMEM;
701         }
702
703         ret = sysdev_class_register(&dma_sysclass);
704         if (ret) {
705                 printk(KERN_ERR "%s: failed to create sysclass\n", __func__);
706                 return -ENOMEM;
707         }
708
709         /* Set all DMA configuration to be DMA, not SDMA */
710         writel(0xffffff, S3C_SYSREG(0x110));
711
712         /* Register standard DMA controlers */
713         s3c64xx_dma_init1(0, DMACH_UART0, IRQ_DMA0, 0x75000000);
714         s3c64xx_dma_init1(8, DMACH_PCM1_TX, IRQ_DMA1, 0x75100000);
715
716         return 0;
717 }
718
719 arch_initcall(s3c64xx_dma_init);