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