]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/dma/apbh_dma.c
TX6 Release 2013-04-22
[karo-tx-uboot.git] / drivers / dma / apbh_dma.c
1 /*
2  * Freescale i.MX28 APBH DMA driver
3  *
4  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5  * on behalf of DENX Software Engineering GmbH
6  *
7  * Based on code from LTIB:
8  * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
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 as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <linux/list.h>
26
27 #include <common.h>
28 #include <malloc.h>
29 #include <asm/errno.h>
30 #include <asm/io.h>
31 #include <asm/arch/clock.h>
32 #include <asm/arch/imx-regs.h>
33 #include <asm/arch/regs-apbh.h>
34 #include <asm/arch/sys_proto.h>
35 #include <asm/arch/dma.h>
36
37 #ifdef DEBUG_
38 static inline u32 mxs_readl(void *addr,
39                         const char *fn, int ln)
40 {
41         u32 val = readl(addr);
42         static void *last_addr;
43         static u32 last_val;
44
45         if (addr != last_addr || last_val != val) {
46                 printf("%s@%d: Read %08x from %p\n", fn, ln, val, addr);
47                 last_addr = addr;
48                 last_val = val;
49         }
50         return val;
51 }
52
53 static inline void mxs_writel(u32 val, void *addr,
54                         const char *name, const char *fn, int ln)
55 {
56 #if 0
57         printf("%s@%d: Writing %08x to %s[%p]...", fn, ln, val, name, addr);
58 #else
59         printf("%s@%d: Writing %08x to %p...", fn, ln, val, addr);
60 #endif
61         writel(val, addr);
62         printf(" result: %08x\n", readl(addr));
63 }
64
65 #undef readl
66 #define readl(a) mxs_readl(a, __func__, __LINE__)
67
68 #undef writel
69 #define writel(v, a) mxs_writel(v, a, #a, __func__, __LINE__)
70 #endif
71
72 #define pr_dma_flag(c,f)        do { if ((c) & MXS_DMA_DESC_##f) printf("%s ", #f); } while (0)
73 static inline void dump_dma_desc(struct mxs_dma_desc *desc)
74 {
75         struct mxs_dma_cmd *cmd = &desc->cmd;
76
77         printf("DMA desc %p:\n", desc);
78         printf("NXT: %08lx\n", cmd->next);
79         printf("CMD: %08lx - ", cmd->data);
80         printf("CNT: %04lx ", (cmd->data & MXS_DMA_DESC_BYTES_MASK) >> MXS_DMA_DESC_BYTES_OFFSET);
81         printf("PIO: %ld ", (cmd->data & MXS_DMA_DESC_PIO_WORDS_MASK) >> MXS_DMA_DESC_PIO_WORDS_OFFSET);
82         pr_dma_flag(cmd->data, HALT_ON_TERMINATE);
83         pr_dma_flag(cmd->data, WAIT4END);
84         pr_dma_flag(cmd->data, DEC_SEM);
85         pr_dma_flag(cmd->data, NAND_WAIT_4_READY);
86         pr_dma_flag(cmd->data, NAND_LOCK);
87         pr_dma_flag(cmd->data, IRQ);
88         pr_dma_flag(cmd->data, CHAIN);
89         printf("\nMOD: ");
90         switch (cmd->data & MXS_DMA_DESC_COMMAND_MASK) {
91         case MXS_DMA_DESC_COMMAND_NO_DMAXFER:
92                 printf("NO_DMA\n");
93                 break;
94         case MXS_DMA_DESC_COMMAND_DMA_WRITE:
95                 printf("WRITE\n");
96                 break;
97         case MXS_DMA_DESC_COMMAND_DMA_READ:
98                 printf("READ\n");
99                 break;
100         case MXS_DMA_DESC_COMMAND_DMA_SENSE:
101                 printf("SENSE\n");
102         }
103         if (cmd->data & MXS_DMA_DESC_PIO_WORDS_MASK) {
104                 int pio_words = (cmd->data & MXS_DMA_DESC_PIO_WORDS_MASK) >> MXS_DMA_DESC_PIO_WORDS_OFFSET;
105                 int i;
106
107                 printf("PIO: ");
108                 for (i = 0; i < pio_words; i++) {
109                         printf("%08lx ", cmd->pio_words[i]);
110                 }
111                 printf("\n");
112         }
113 }
114
115 static struct mxs_dma_chan mxs_dma_channels[MXS_MAX_DMA_CHANNELS];
116 static struct apbh_regs *apbh_regs = (struct apbh_regs *)MXS_APBH_BASE;
117
118 /*
119  * Test is the DMA channel is valid channel
120  */
121 int mxs_dma_validate_chan(int channel)
122 {
123         struct mxs_dma_chan *pchan;
124
125         if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS)) {
126                 printf("Invalid DMA channel %d\n", channel);
127                 return -EINVAL;
128         }
129
130         pchan = mxs_dma_channels + channel;
131         if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED)) {
132                 printf("DMA channel %d not allocated\n", channel);
133                 return -EINVAL;
134         }
135
136         return 0;
137 }
138
139 /*
140  * Return the address of the command within a descriptor.
141  */
142 static unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc)
143 {
144         return desc->address + offsetof(struct mxs_dma_desc, cmd);
145 }
146
147 /*
148  * Read a DMA channel's hardware semaphore.
149  *
150  * As used by the MXS platform's DMA software, the DMA channel's hardware
151  * semaphore reflects the number of DMA commands the hardware will process, but
152  * has not yet finished. This is a volatile value read directly from hardware,
153  * so it must be be viewed as immediately stale.
154  *
155  * If the channel is not marked busy, or has finished processing all its
156  * commands, this value should be zero.
157  *
158  * See mxs_dma_append() for details on how DMA command blocks must be configured
159  * to maintain the expected behavior of the semaphore's value.
160  */
161 static int mxs_dma_read_semaphore(int channel)
162 {
163         uint32_t tmp;
164         int ret;
165
166         ret = mxs_dma_validate_chan(channel);
167         if (ret)
168                 return ret;
169
170         tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema);
171
172         tmp &= APBH_CHn_SEMA_PHORE_MASK;
173         tmp >>= APBH_CHn_SEMA_PHORE_OFFSET;
174
175         return tmp;
176 }
177
178 #ifndef CONFIG_SYS_DCACHE_OFF
179 void mxs_dma_flush_desc(struct mxs_dma_desc *desc)
180 {
181         uint32_t addr;
182         uint32_t size;
183
184         addr = (uint32_t)desc;
185         size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
186
187         flush_dcache_range(addr, addr + size);
188 }
189 #else
190 inline void mxs_dma_flush_desc(struct mxs_dma_desc *desc) {}
191 #endif
192
193 /*
194  * Enable a DMA channel.
195  *
196  * If the given channel has any DMA descriptors on its active list, this
197  * function causes the DMA hardware to begin processing them.
198  *
199  * This function marks the DMA channel as "busy," whether or not there are any
200  * descriptors to process.
201  */
202 static int mxs_dma_enable(int channel)
203 {
204         unsigned int sem;
205         struct mxs_dma_chan *pchan;
206         struct mxs_dma_desc *pdesc;
207         int ret;
208
209         ret = mxs_dma_validate_chan(channel);
210         if (ret)
211                 return ret;
212
213         pchan = mxs_dma_channels + channel;
214
215         if (pchan->pending_num == 0) {
216                 pchan->flags |= MXS_DMA_FLAGS_BUSY;
217                 return 0;
218         }
219
220         pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node);
221         if (pdesc == NULL)
222                 return -EINVAL;
223
224         if (pchan->flags & MXS_DMA_FLAGS_BUSY) {
225                 if (!(pdesc->cmd.data & MXS_DMA_DESC_CHAIN))
226                         return 0;
227
228                 sem = mxs_dma_read_semaphore(channel);
229                 if (sem == 0)
230                         return 0;
231
232                 if (sem == 1) {
233                         pdesc = list_entry(pdesc->node.next,
234                                            struct mxs_dma_desc, node);
235                         writel(mxs_dma_cmd_address(pdesc),
236                                 &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
237                 }
238                 writel(pchan->pending_num,
239                         &apbh_regs->ch[channel].hw_apbh_ch_sema);
240                 pchan->active_num += pchan->pending_num;
241                 pchan->pending_num = 0;
242         } else {
243                 pchan->active_num += pchan->pending_num;
244                 pchan->pending_num = 0;
245 #if 1
246                 writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
247                         &apbh_regs->hw_apbh_ctrl0_clr);
248 #endif
249                 writel(mxs_dma_cmd_address(pdesc),
250                         &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
251                 writel(pchan->active_num,
252                         &apbh_regs->ch[channel].hw_apbh_ch_sema);
253         }
254
255         pchan->flags |= MXS_DMA_FLAGS_BUSY;
256         return 0;
257 }
258
259 /*
260  * Disable a DMA channel.
261  *
262  * This function shuts down a DMA channel and marks it as "not busy." Any
263  * descriptors on the active list are immediately moved to the head of the
264  * "done" list, whether or not they have actually been processed by the
265  * hardware. The "ready" flags of these descriptors are NOT cleared, so they
266  * still appear to be active.
267  *
268  * This function immediately shuts down a DMA channel's hardware, aborting any
269  * I/O that may be in progress, potentially leaving I/O hardware in an undefined
270  * state. It is unwise to call this function if there is ANY chance the hardware
271  * is still processing a command.
272  */
273 static int mxs_dma_disable(int channel)
274 {
275         struct mxs_dma_chan *pchan;
276         int ret;
277
278         ret = mxs_dma_validate_chan(channel);
279         if (ret)
280                 return ret;
281
282         pchan = mxs_dma_channels + channel;
283
284         if ((pchan->flags & MXS_DMA_FLAGS_BUSY)) {
285                 printf("%s: DMA channel %d busy\n", __func__, channel);
286                 return -EINVAL;
287         }
288 #if 0
289         writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
290                 &apbh_regs->hw_apbh_ctrl0_set);
291 #endif
292         pchan->active_num = 0;
293         pchan->pending_num = 0;
294         list_splice_init(&pchan->active, &pchan->done);
295
296         return 0;
297 }
298
299 /*
300  * Resets the DMA channel hardware.
301  */
302 static int mxs_dma_reset(int channel)
303 {
304         int ret;
305
306         ret = mxs_dma_validate_chan(channel);
307         if (ret)
308                 return ret;
309
310         writel(1 << (channel + APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET),
311                 &apbh_regs->hw_apbh_channel_ctrl_set);
312
313         return 0;
314 }
315
316 /*
317  * Enable or disable DMA interrupt.
318  *
319  * This function enables the given DMA channel to interrupt the CPU.
320  */
321 static int mxs_dma_enable_irq(int channel, int enable)
322 {
323         int ret;
324
325         ret = mxs_dma_validate_chan(channel);
326         if (ret)
327                 return ret;
328
329         if (enable)
330                 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
331                         &apbh_regs->hw_apbh_ctrl1_set);
332         else
333                 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
334                         &apbh_regs->hw_apbh_ctrl1_clr);
335
336         return 0;
337 }
338
339 /*
340  * Clear DMA interrupt.
341  *
342  * The software that is using the DMA channel must register to receive its
343  * interrupts and, when they arrive, must call this function to clear them.
344  */
345 static int mxs_dma_ack_irq(int channel)
346 {
347         int ret;
348
349         ret = mxs_dma_validate_chan(channel);
350         if (ret)
351                 return ret;
352
353         writel(1 << channel, &apbh_regs->hw_apbh_ctrl1_clr);
354         writel(1 << channel, &apbh_regs->hw_apbh_ctrl2_clr);
355
356         return 0;
357 }
358
359 /*
360  * Request to reserve a DMA channel
361  */
362 static int mxs_dma_request(int channel)
363 {
364         struct mxs_dma_chan *pchan;
365
366         if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
367                 return -EINVAL;
368
369         pchan = mxs_dma_channels + channel;
370         if ((pchan->flags & MXS_DMA_FLAGS_VALID) != MXS_DMA_FLAGS_VALID)
371                 return -ENODEV;
372
373         if (pchan->flags & MXS_DMA_FLAGS_ALLOCATED)
374                 return -EBUSY;
375
376         pchan->flags |= MXS_DMA_FLAGS_ALLOCATED;
377         pchan->active_num = 0;
378         pchan->pending_num = 0;
379
380         INIT_LIST_HEAD(&pchan->active);
381         INIT_LIST_HEAD(&pchan->done);
382
383         return 0;
384 }
385
386 /*
387  * Release a DMA channel.
388  *
389  * This function releases a DMA channel from its current owner.
390  *
391  * The channel will NOT be released if it's marked "busy" (see
392  * mxs_dma_enable()).
393  */
394 int mxs_dma_release(int channel)
395 {
396         struct mxs_dma_chan *pchan;
397         int ret;
398
399         ret = mxs_dma_validate_chan(channel);
400         if (ret)
401                 return ret;
402
403         pchan = mxs_dma_channels + channel;
404
405         if (pchan->flags & MXS_DMA_FLAGS_BUSY)
406                 return -EBUSY;
407
408         pchan->dev = 0;
409         pchan->active_num = 0;
410         pchan->pending_num = 0;
411         pchan->flags &= ~MXS_DMA_FLAGS_ALLOCATED;
412
413         return 0;
414 }
415
416 /*
417  * Allocate DMA descriptor
418  */
419 struct mxs_dma_desc *mxs_dma_desc_alloc(void)
420 {
421         struct mxs_dma_desc *pdesc;
422         uint32_t size;
423
424         size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
425         pdesc = memalign(MXS_DMA_ALIGNMENT, size);
426
427         if (pdesc == NULL)
428                 return NULL;
429
430         memset(pdesc, 0, sizeof(*pdesc));
431         pdesc->address = (dma_addr_t)pdesc;
432
433         return pdesc;
434 };
435
436 /*
437  * Free DMA descriptor
438  */
439 void mxs_dma_desc_free(struct mxs_dma_desc *pdesc)
440 {
441         if (pdesc == NULL)
442                 return;
443
444         free(pdesc);
445 }
446
447 /*
448  * Add a DMA descriptor to a channel.
449  *
450  * If the descriptor list for this channel is not empty, this function sets the
451  * CHAIN bit and the NEXTCMD_ADDR fields in the last descriptor's DMA command so
452  * it will chain to the new descriptor's command.
453  *
454  * Then, this function marks the new descriptor as "ready," adds it to the end
455  * of the active descriptor list, and increments the count of pending
456  * descriptors.
457  *
458  * The MXS platform DMA software imposes some rules on DMA commands to maintain
459  * important invariants. These rules are NOT checked, but they must be carefully
460  * applied by software that uses MXS DMA channels.
461  *
462  * Invariant:
463  *     The DMA channel's hardware semaphore must reflect the number of DMA
464  *     commands the hardware will process, but has not yet finished.
465  *
466  * Explanation:
467  *     A DMA channel begins processing commands when its hardware semaphore is
468  *     written with a value greater than zero, and it stops processing commands
469  *     when the semaphore returns to zero.
470  *
471  *     When a channel finishes a DMA command, it will decrement its semaphore if
472  *     the DECREMENT_SEMAPHORE bit is set in that command's flags bits.
473  *
474  *     In principle, it's not necessary for the DECREMENT_SEMAPHORE to be set,
475  *     unless it suits the purposes of the software. For example, one could
476  *     construct a series of five DMA commands, with the DECREMENT_SEMAPHORE
477  *     bit set only in the last one. Then, setting the DMA channel's hardware
478  *     semaphore to one would cause the entire series of five commands to be
479  *     processed. However, this example would violate the invariant given above.
480  *
481  * Rule:
482  *    ALL DMA commands MUST have the DECREMENT_SEMAPHORE bit set so that the DMA
483  *    channel's hardware semaphore will be decremented EVERY time a command is
484  *    processed.
485  */
486 int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc)
487 {
488         struct mxs_dma_chan *pchan;
489         struct mxs_dma_desc *last;
490         int ret;
491
492         ret = mxs_dma_validate_chan(channel);
493         if (ret)
494                 return ret;
495
496         pchan = mxs_dma_channels + channel;
497
498         pdesc->cmd.next = mxs_dma_cmd_address(pdesc);
499         pdesc->flags |= MXS_DMA_DESC_FIRST | MXS_DMA_DESC_LAST;
500
501         if (!list_empty(&pchan->active)) {
502                 last = list_entry(pchan->active.prev, struct mxs_dma_desc,
503                                         node);
504
505                 pdesc->flags &= ~MXS_DMA_DESC_FIRST;
506                 last->flags &= ~MXS_DMA_DESC_LAST;
507
508                 last->cmd.next = mxs_dma_cmd_address(pdesc);
509                 last->cmd.data |= MXS_DMA_DESC_CHAIN;
510
511                 mxs_dma_flush_desc(last);
512         }
513         pdesc->flags |= MXS_DMA_DESC_READY;
514         if (pdesc->flags & MXS_DMA_DESC_FIRST)
515                 pchan->pending_num++;
516         list_add_tail(&pdesc->node, &pchan->active);
517
518         mxs_dma_flush_desc(pdesc);
519
520         return ret;
521 }
522
523 /*
524  * Clean up processed DMA descriptors.
525  *
526  * This function removes processed DMA descriptors from the "active" list. Pass
527  * in a non-NULL list head to get the descriptors moved to your list. Pass NULL
528  * to get the descriptors moved to the channel's "done" list. Descriptors on
529  * the "done" list can be retrieved with mxs_dma_get_finished().
530  *
531  * This function marks the DMA channel as "not busy" if no unprocessed
532  * descriptors remain on the "active" list.
533  */
534 static int mxs_dma_finish(int channel, struct list_head *head)
535 {
536         int sem;
537         struct mxs_dma_chan *pchan;
538         struct list_head *p, *q;
539         struct mxs_dma_desc *pdesc;
540         int ret;
541
542         ret = mxs_dma_validate_chan(channel);
543         if (ret)
544                 return ret;
545
546         pchan = mxs_dma_channels + channel;
547
548         sem = mxs_dma_read_semaphore(channel);
549         if (sem < 0)
550                 return sem;
551
552         if (sem == pchan->active_num)
553                 return 0;
554
555         list_for_each_safe(p, q, &pchan->active) {
556                 if ((pchan->active_num) <= sem)
557                         break;
558
559                 pdesc = list_entry(p, struct mxs_dma_desc, node);
560                 pdesc->flags &= ~MXS_DMA_DESC_READY;
561
562                 if (head)
563                         list_move_tail(p, head);
564                 else
565                         list_move_tail(p, &pchan->done);
566
567                 if (pdesc->flags & MXS_DMA_DESC_LAST)
568                         pchan->active_num--;
569         }
570
571         if (sem == 0)
572                 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
573
574         return 0;
575 }
576
577 /*
578  * Wait for DMA channel to complete
579  */
580 static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
581 {
582         int ret;
583
584         ret = mxs_dma_validate_chan(chan);
585         if (ret)
586                 return ret;
587
588         if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
589                                 1 << chan, timeout)) {
590                 ret = -ETIMEDOUT;
591                 mxs_dma_reset(chan);
592         }
593
594         return ret;
595 }
596
597 /*
598  * Execute the DMA channel
599  */
600 int mxs_dma_go(int chan)
601 {
602         uint32_t timeout = 10000;
603         int ret;
604
605         LIST_HEAD(tmp_desc_list);
606
607         mxs_dma_enable_irq(chan, 1);
608         mxs_dma_enable(chan);
609
610         /* Wait for DMA to finish. */
611         ret = mxs_dma_wait_complete(timeout, chan);
612
613         /* Clear out the descriptors we just ran. */
614         mxs_dma_finish(chan, &tmp_desc_list);
615
616         /* Shut the DMA channel down. */
617         mxs_dma_ack_irq(chan);
618         mxs_dma_reset(chan);
619         mxs_dma_enable_irq(chan, 0);
620         mxs_dma_disable(chan);
621
622         return ret;
623 }
624
625 /*
626  * Initialize the DMA hardware
627  */
628 void mxs_dma_init(void)
629 {
630         mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
631
632 #ifdef CONFIG_APBH_DMA_BURST8
633         writel(APBH_CTRL0_AHB_BURST8_EN,
634                 &apbh_regs->hw_apbh_ctrl0_set);
635 #else
636         writel(APBH_CTRL0_AHB_BURST8_EN,
637                 &apbh_regs->hw_apbh_ctrl0_clr);
638 #endif
639
640 #ifdef CONFIG_APBH_DMA_BURST
641         writel(APBH_CTRL0_APB_BURST_EN,
642                 &apbh_regs->hw_apbh_ctrl0_set);
643 #else
644         writel(APBH_CTRL0_APB_BURST_EN,
645                 &apbh_regs->hw_apbh_ctrl0_clr);
646 #endif
647 }
648
649 int mxs_dma_init_channel(int channel)
650 {
651         struct mxs_dma_chan *pchan;
652         int ret;
653
654         pchan = mxs_dma_channels + channel;
655         pchan->flags = MXS_DMA_FLAGS_VALID;
656
657         ret = mxs_dma_request(channel);
658
659         if (ret) {
660                 printf("MXS DMA: Can't acquire DMA channel %i\n",
661                         channel);
662                 return ret;
663         }
664
665         mxs_dma_reset(channel);
666         mxs_dma_ack_irq(channel);
667
668         return 0;
669 }