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