]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/dma/apbh_dma.c
MX28: DMA: make mxs_dma_flush_desc() static
[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
294         INIT_LIST_HEAD(&pchan->active);
295         INIT_LIST_HEAD(&pchan->done);
296
297         return 0;
298 }
299
300 /*
301  * Release a DMA channel.
302  *
303  * This function releases a DMA channel from its current owner.
304  *
305  * The channel will NOT be released if it's marked "busy" (see
306  * mxs_dma_enable()).
307  */
308 int mxs_dma_release(int channel)
309 {
310         struct mxs_dma_chan *pchan;
311         int ret;
312
313         ret = mxs_dma_validate_chan(channel);
314         if (ret)
315                 return ret;
316
317         pchan = mxs_dma_channels + channel;
318
319         if (pchan->flags & MXS_DMA_FLAGS_BUSY)
320                 return -EBUSY;
321
322         pchan->dev = 0;
323         pchan->active_num = 0;
324         pchan->pending_num = 0;
325         pchan->flags &= ~MXS_DMA_FLAGS_ALLOCATED;
326
327         return 0;
328 }
329
330 /*
331  * Allocate DMA descriptor
332  */
333 struct mxs_dma_desc *mxs_dma_desc_alloc(void)
334 {
335         struct mxs_dma_desc *pdesc;
336         uint32_t size;
337
338         size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
339         pdesc = memalign(MXS_DMA_ALIGNMENT, size);
340
341         if (pdesc == NULL)
342                 return NULL;
343
344         memset(pdesc, 0, sizeof(*pdesc));
345         pdesc->address = (dma_addr_t)pdesc;
346
347         return pdesc;
348 };
349
350 /*
351  * Free DMA descriptor
352  */
353 void mxs_dma_desc_free(struct mxs_dma_desc *pdesc)
354 {
355         if (pdesc == NULL)
356                 return;
357
358         free(pdesc);
359 }
360
361 /*
362  * Add a DMA descriptor to a channel.
363  *
364  * If the descriptor list for this channel is not empty, this function sets the
365  * CHAIN bit and the NEXTCMD_ADDR fields in the last descriptor's DMA command so
366  * it will chain to the new descriptor's command.
367  *
368  * Then, this function marks the new descriptor as "ready," adds it to the end
369  * of the active descriptor list, and increments the count of pending
370  * descriptors.
371  *
372  * The MXS platform DMA software imposes some rules on DMA commands to maintain
373  * important invariants. These rules are NOT checked, but they must be carefully
374  * applied by software that uses MXS DMA channels.
375  *
376  * Invariant:
377  *     The DMA channel's hardware semaphore must reflect the number of DMA
378  *     commands the hardware will process, but has not yet finished.
379  *
380  * Explanation:
381  *     A DMA channel begins processing commands when its hardware semaphore is
382  *     written with a value greater than zero, and it stops processing commands
383  *     when the semaphore returns to zero.
384  *
385  *     When a channel finishes a DMA command, it will decrement its semaphore if
386  *     the DECREMENT_SEMAPHORE bit is set in that command's flags bits.
387  *
388  *     In principle, it's not necessary for the DECREMENT_SEMAPHORE to be set,
389  *     unless it suits the purposes of the software. For example, one could
390  *     construct a series of five DMA commands, with the DECREMENT_SEMAPHORE
391  *     bit set only in the last one. Then, setting the DMA channel's hardware
392  *     semaphore to one would cause the entire series of five commands to be
393  *     processed. However, this example would violate the invariant given above.
394  *
395  * Rule:
396  *    ALL DMA commands MUST have the DECREMENT_SEMAPHORE bit set so that the DMA
397  *    channel's hardware semaphore will be decremented EVERY time a command is
398  *    processed.
399  */
400 int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc)
401 {
402         struct mxs_dma_chan *pchan;
403         struct mxs_dma_desc *last;
404         int ret;
405
406         ret = mxs_dma_validate_chan(channel);
407         if (ret)
408                 return ret;
409
410         pchan = mxs_dma_channels + channel;
411
412         pdesc->cmd.next = mxs_dma_cmd_address(pdesc);
413         pdesc->flags |= MXS_DMA_DESC_FIRST | MXS_DMA_DESC_LAST;
414
415         if (!list_empty(&pchan->active)) {
416                 last = list_entry(pchan->active.prev, struct mxs_dma_desc,
417                                         node);
418
419                 pdesc->flags &= ~MXS_DMA_DESC_FIRST;
420                 last->flags &= ~MXS_DMA_DESC_LAST;
421
422                 last->cmd.next = mxs_dma_cmd_address(pdesc);
423                 last->cmd.data |= MXS_DMA_DESC_CHAIN;
424
425                 mxs_dma_flush_desc(last);
426         }
427         pdesc->flags |= MXS_DMA_DESC_READY;
428         if (pdesc->flags & MXS_DMA_DESC_FIRST)
429                 pchan->pending_num++;
430         list_add_tail(&pdesc->node, &pchan->active);
431
432         mxs_dma_flush_desc(pdesc);
433
434         return ret;
435 }
436
437 /*
438  * Clean up processed DMA descriptors.
439  *
440  * This function removes processed DMA descriptors from the "active" list. Pass
441  * in a non-NULL list head to get the descriptors moved to your list. Pass NULL
442  * to get the descriptors moved to the channel's "done" list. Descriptors on
443  * the "done" list can be retrieved with mxs_dma_get_finished().
444  *
445  * This function marks the DMA channel as "not busy" if no unprocessed
446  * descriptors remain on the "active" list.
447  */
448 static int mxs_dma_finish(int channel, struct list_head *head)
449 {
450         int sem;
451         struct mxs_dma_chan *pchan;
452         struct list_head *p, *q;
453         struct mxs_dma_desc *pdesc;
454         int ret;
455
456         ret = mxs_dma_validate_chan(channel);
457         if (ret)
458                 return ret;
459
460         pchan = mxs_dma_channels + channel;
461
462         sem = mxs_dma_read_semaphore(channel);
463         if (sem < 0)
464                 return sem;
465
466         if (sem == pchan->active_num)
467                 return 0;
468
469         list_for_each_safe(p, q, &pchan->active) {
470                 if ((pchan->active_num) <= sem)
471                         break;
472
473                 pdesc = list_entry(p, struct mxs_dma_desc, node);
474                 pdesc->flags &= ~MXS_DMA_DESC_READY;
475
476                 if (head)
477                         list_move_tail(p, head);
478                 else
479                         list_move_tail(p, &pchan->done);
480
481                 if (pdesc->flags & MXS_DMA_DESC_LAST)
482                         pchan->active_num--;
483         }
484
485         if (sem == 0)
486                 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
487
488         return 0;
489 }
490
491 /*
492  * Wait for DMA channel to complete
493  */
494 static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
495 {
496         int ret;
497
498         ret = mxs_dma_validate_chan(chan);
499         if (ret)
500                 return ret;
501
502         if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
503                                 1 << chan, timeout)) {
504                 ret = -ETIMEDOUT;
505                 mxs_dma_reset(chan);
506         }
507
508         return ret;
509 }
510
511 /*
512  * Execute the DMA channel
513  */
514 int mxs_dma_go(int chan)
515 {
516         uint32_t timeout = 10000;
517         int ret;
518
519         LIST_HEAD(tmp_desc_list);
520
521         mxs_dma_enable_irq(chan, 1);
522         mxs_dma_enable(chan);
523
524         /* Wait for DMA to finish. */
525         ret = mxs_dma_wait_complete(timeout, chan);
526
527         /* Clear out the descriptors we just ran. */
528         mxs_dma_finish(chan, &tmp_desc_list);
529
530         /* Shut the DMA channel down. */
531         mxs_dma_ack_irq(chan);
532         mxs_dma_reset(chan);
533         mxs_dma_enable_irq(chan, 0);
534         mxs_dma_disable(chan);
535
536         return ret;
537 }
538
539 /*
540  * Initialize the DMA hardware
541  */
542 void mxs_dma_init(void)
543 {
544         mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
545
546 #ifdef CONFIG_APBH_DMA_BURST8
547         writel(APBH_CTRL0_AHB_BURST8_EN,
548                 &apbh_regs->hw_apbh_ctrl0_set);
549 #else
550         writel(APBH_CTRL0_AHB_BURST8_EN,
551                 &apbh_regs->hw_apbh_ctrl0_clr);
552 #endif
553
554 #ifdef CONFIG_APBH_DMA_BURST
555         writel(APBH_CTRL0_APB_BURST_EN,
556                 &apbh_regs->hw_apbh_ctrl0_set);
557 #else
558         writel(APBH_CTRL0_APB_BURST_EN,
559                 &apbh_regs->hw_apbh_ctrl0_clr);
560 #endif
561 }
562
563 int mxs_dma_init_channel(int channel)
564 {
565         struct mxs_dma_chan *pchan;
566         int ret;
567
568         pchan = mxs_dma_channels + channel;
569         pchan->flags = MXS_DMA_FLAGS_VALID;
570
571         ret = mxs_dma_request(channel);
572
573         if (ret) {
574                 printf("MXS DMA: Can't acquire DMA channel %i\n",
575                         channel);
576                 return ret;
577         }
578
579         mxs_dma_reset(channel);
580         mxs_dma_ack_irq(channel);
581
582         return 0;
583 }