]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/tusb6010_omap.c
84192a8f26a5b38a9ac346a383028255a813ca90
[linux-2.6-omap-h63xx.git] / drivers / usb / musb / tusb6010_omap.c
1 /*
2  * TUSB6010 USB 2.0 OTG Dual Role controller OMAP DMA interface
3  *
4  * Copyright (C) 2006 Nokia Corporation
5  * Tony Lindgren <tony@atomide.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/usb.h>
16 #include <linux/platform_device.h>
17 #include <linux/dma-mapping.h>
18 #include <asm/arch/dma.h>
19 #include <asm/arch/mux.h>
20
21 #include "musb_core.h"
22
23 /*
24  * REVISIT: With TUSB2.0 only one dmareq line can be used at a time.
25  * This should get fixed in hardware at some point.
26  */
27 #define BROKEN_DMAREQ
28
29 #ifdef BROKEN_DMAREQ
30 #define dmareq_works()          0
31 #else
32 #define dmareq_works()          1
33 #endif
34
35 #define to_chdat(c)             (struct tusb_omap_dma_ch *)(c)->private_data
36
37 #define MAX_DMAREQ              5       /* REVISIT: Really 6, but req5 not OK */
38
39 struct tusb_omap_dma_ch {
40         struct musb             *musb;
41         void __iomem            *tusb_base;
42         unsigned long           phys_offset;
43         int                     epnum;
44         u8                      tx;
45         struct musb_hw_ep       *hw_ep;
46
47         int                     ch;
48         s8                      dmareq;
49         s8                      sync_dev;
50
51         struct tusb_omap_dma    *tusb_dma;
52
53         void __iomem            *dma_addr;
54
55         u32                     len;
56         u16                     packet_sz;
57         u16                     transfer_packet_sz;
58         u32                     transfer_len;
59         u32                     completed_len;
60 };
61
62 struct tusb_omap_dma {
63         struct dma_controller           controller;
64         struct musb                     *musb;
65         void __iomem                    *tusb_base;
66
67         int                             ch;
68         s8                              dmareq;
69         s8                              sync_dev;
70 };
71
72 static int tusb_omap_dma_start(struct dma_controller *c)
73 {
74         struct tusb_omap_dma    *tusb_dma;
75
76         tusb_dma = container_of(c, struct tusb_omap_dma, controller);
77
78         // DBG(3, "ep%i ch: %i\n", chdat->epnum, chdat->ch);
79
80         return 0;
81 }
82
83 static int tusb_omap_dma_stop(struct dma_controller *c)
84 {
85         struct tusb_omap_dma    *tusb_dma;
86
87         tusb_dma = container_of(c, struct tusb_omap_dma, controller);
88
89         // DBG(3, "ep%i ch: %i\n", chdat->epnum, chdat->ch);
90
91         return 0;
92 }
93
94 #ifdef BROKEN_DMAREQ
95
96 /*
97  * Allocate dmareq0 to the current channel unless it's already taken
98  */
99 static inline int tusb_omap_use_shared_dmareq(struct tusb_omap_dma_ch *chdat)
100 {
101         u32             reg = musb_readl(chdat->tusb_base, TUSB_DMA_EP_MAP);
102
103         if (reg != 0) {
104                 DBG(3, "ep%i dmareq0 is busy for ep%i\n",
105                         chdat->epnum, reg & 0xf);
106                 return -EAGAIN;
107         }
108
109         if (chdat->tx)
110                 reg = (1 << 4) | chdat->epnum;
111         else
112                 reg = chdat->epnum;
113
114         musb_writel(chdat->tusb_base, TUSB_DMA_EP_MAP, reg);
115
116         return 0;
117 }
118
119 static inline void tusb_omap_free_shared_dmareq(struct tusb_omap_dma_ch *chdat)
120 {
121         u32             reg = musb_readl(chdat->tusb_base, TUSB_DMA_EP_MAP);
122
123         if ((reg & 0xf) != chdat->epnum) {
124                 printk(KERN_ERR "ep%i trying to release dmareq0 for ep%i\n",
125                         chdat->epnum, reg & 0xf);
126                 return;
127         }
128         musb_writel(chdat->tusb_base, TUSB_DMA_EP_MAP, 0);
129 }
130
131 #else
132 #define tusb_omap_use_shared_dmareq(x, y)       do {} while (0)
133 #define tusb_omap_free_shared_dmareq(x, y)      do {} while (0)
134 #endif
135
136 /*
137  * See also musb_dma_completion in plat_uds.c and musb_g_[tx|rx]() in
138  * musb_gadget.c.
139  */
140 static void tusb_omap_dma_cb(int lch, u16 ch_status, void *data)
141 {
142         struct dma_channel      *channel = (struct dma_channel *)data;
143         struct tusb_omap_dma_ch *chdat = to_chdat(channel);
144         struct tusb_omap_dma    *tusb_dma = chdat->tusb_dma;
145         struct musb             *musb = chdat->musb;
146         struct musb_hw_ep       *hw_ep = chdat->hw_ep;
147         void __iomem            *ep_conf = hw_ep->conf;
148         void __iomem            *musb_base = musb->mregs;
149         unsigned long           remaining, flags, pio;
150         int                     ch;
151
152         spin_lock_irqsave(&musb->lock, flags);
153
154         if (dmareq_works())
155                 ch = chdat->ch;
156         else
157                 ch = tusb_dma->ch;
158
159         if (ch_status != OMAP_DMA_BLOCK_IRQ)
160                 printk(KERN_ERR "TUSB DMA error status: %i\n", ch_status);
161
162         DBG(2, "ep%i %s dma callback ch: %i status: %x\n",
163                 chdat->epnum, chdat->tx ? "tx" : "rx",
164                 ch, ch_status);
165
166         if (chdat->tx)
167                 remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
168         else
169                 remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
170
171         remaining = TUSB_EP_CONFIG_XFR_SIZE(remaining);
172
173         /* HW issue #10: XFR_SIZE may get corrupt on async DMA */
174         if (unlikely(remaining > chdat->transfer_len)) {
175                 WARN("Corrupt XFR_SIZE with async DMA: %lu\n", remaining);
176                 remaining = 0;
177         }
178
179         channel->actual_len = chdat->transfer_len - remaining;
180         pio = chdat->len - channel->actual_len;
181
182         DBG(2, "DMA remaining %lu/%u\n", remaining, chdat->transfer_len);
183
184         /* Transfer remaining 1 - 31 bytes */
185         if (pio > 0 && pio < 32) {
186                 u8      *buf;
187
188                 DBG(2, "Using PIO for remaining %lu bytes\n", pio);
189                 buf = phys_to_virt((u32)chdat->dma_addr) + chdat->transfer_len;
190                 if (chdat->tx) {
191                         consistent_sync(phys_to_virt((u32)chdat->dma_addr),
192                                         chdat->transfer_len, DMA_TO_DEVICE);
193                         musb_write_fifo(hw_ep, pio, buf);
194                 } else {
195                         musb_read_fifo(hw_ep, pio, buf);
196                         consistent_sync(phys_to_virt((u32)chdat->dma_addr),
197                                         chdat->transfer_len, DMA_FROM_DEVICE);
198                 }
199                 channel->actual_len += pio;
200         }
201
202         if (!dmareq_works())
203                 tusb_omap_free_shared_dmareq(chdat);
204
205         channel->status = MUSB_DMA_STATUS_FREE;
206
207         /* Handle only RX callbacks here. TX callbacks musb be handled based
208          * on the TUSB DMA status interrupt.
209          * REVISIT: Use both TUSB DMA status interrupt and OMAP DMA callback
210          * interrupt for RX and TX.
211          */
212         if (!chdat->tx)
213                 musb_dma_completion(musb, chdat->epnum, chdat->tx);
214
215         /* We must terminate short tx transfers manually by setting TXPKTRDY.
216          * REVISIT: This same problem may occur with other MUSB dma as well.
217          * Easy to test with g_ether by pinging the MUSB board with ping -s54.
218          */
219         if ((chdat->transfer_len < chdat->packet_sz)
220                         || (chdat->transfer_len % chdat->packet_sz != 0)) {
221                 u16     csr;
222
223                 if (chdat->tx) {
224                         DBG(2, "terminating short tx packet\n");
225                         musb_ep_select(musb_base, chdat->epnum);
226                         csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
227                         csr |= MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY
228                                 | MUSB_TXCSR_P_WZC_BITS;
229                         musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
230                 }
231         }
232
233         spin_unlock_irqrestore(&musb->lock, flags);
234 }
235
236 static int tusb_omap_dma_program(struct dma_channel *channel, u16 packet_sz,
237                                 u8 rndis_mode, dma_addr_t dma_addr, u32 len)
238 {
239         struct tusb_omap_dma_ch         *chdat = to_chdat(channel);
240         struct tusb_omap_dma            *tusb_dma = chdat->tusb_dma;
241         struct musb                     *musb = chdat->musb;
242         struct musb_hw_ep               *hw_ep = chdat->hw_ep;
243         void __iomem                    *musb_base = musb->mregs;
244         void __iomem                    *ep_conf = hw_ep->conf;
245         dma_addr_t                      fifo = hw_ep->fifo_sync;
246         struct omap_dma_channel_params  dma_params;
247         int                             src_burst, dst_burst;
248         u16                             csr;
249         int                             ch;
250         s8                              dmareq;
251         s8                              sync_dev;
252
253         if (unlikely(dma_addr & 0x1) || (len < 32) || (len > packet_sz))
254                 return false;
255
256         /*
257          * HW issue #10: Async dma will eventually corrupt the XFR_SIZE
258          * register which will cause missed DMA interrupt. We could try to
259          * use a timer for the callback, but it is unsafe as the XFR_SIZE
260          * register is corrupt, and we won't know if the DMA worked.
261          */
262         if (dma_addr & 0x2)
263                 return false;
264
265         chdat->transfer_len = len & ~0x1f;
266
267         if (len < packet_sz)
268                 chdat->transfer_packet_sz = chdat->transfer_len;
269         else
270                 chdat->transfer_packet_sz = packet_sz;
271
272         if (dmareq_works()) {
273                 ch = chdat->ch;
274                 dmareq = chdat->dmareq;
275                 sync_dev = chdat->sync_dev;
276         } else {
277                 if (tusb_omap_use_shared_dmareq(chdat) != 0) {
278                         DBG(3, "could not get dma for ep%i\n", chdat->epnum);
279                         return false;
280                 }
281                 if (tusb_dma->ch < 0) {
282                         /* REVISIT: This should get blocked earlier, happens
283                          * with MSC ErrorRecoveryTest
284                          */
285                         WARN_ON(1);
286                         return false;
287                 }
288
289                 ch = tusb_dma->ch;
290                 dmareq = tusb_dma->dmareq;
291                 sync_dev = tusb_dma->sync_dev;
292                 omap_set_dma_callback(ch, tusb_omap_dma_cb, channel);
293         }
294
295         chdat->packet_sz = packet_sz;
296         chdat->len = len;
297         channel->actual_len = 0;
298         chdat->dma_addr = (void __iomem *)dma_addr;
299         channel->status = MUSB_DMA_STATUS_BUSY;
300
301         /* Since we're recycling dma areas, we need to clean or invalidate */
302         if (chdat->tx) {
303                 consistent_sync(phys_to_virt(dma_addr), len, DMA_TO_DEVICE);
304         } else
305                 consistent_sync(phys_to_virt(dma_addr), len, DMA_FROM_DEVICE);
306
307         /* Use 16-bit transfer if dma_addr is not 32-bit aligned */
308         if ((dma_addr & 0x3) == 0) {
309                 dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
310                 dma_params.elem_count = 8;              /* Elements in frame */
311         } else {
312                 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
313                 dma_params.elem_count = 16;             /* Elements in frame */
314                 fifo = hw_ep->fifo_async;
315         }
316
317         dma_params.frame_count  = chdat->transfer_len / 32; /* Burst sz frame */
318
319         DBG(2, "ep%i %s dma ch%i dma: %08x len: %u(%u) packet_sz: %i(%i)\n",
320                 chdat->epnum, chdat->tx ? "tx" : "rx",
321                 ch, dma_addr, chdat->transfer_len, len,
322                 chdat->transfer_packet_sz, packet_sz);
323
324         /*
325          * Prepare omap DMA for transfer
326          */
327         if (chdat->tx) {
328                 dma_params.src_amode    = OMAP_DMA_AMODE_POST_INC;
329                 dma_params.src_start    = (unsigned long)dma_addr;
330                 dma_params.src_ei       = 0;
331                 dma_params.src_fi       = 0;
332
333                 dma_params.dst_amode    = OMAP_DMA_AMODE_DOUBLE_IDX;
334                 dma_params.dst_start    = (unsigned long)fifo;
335                 dma_params.dst_ei       = 1;
336                 dma_params.dst_fi       = -31;          /* Loop 32 byte window */
337
338                 dma_params.trigger      = sync_dev;
339                 dma_params.sync_mode    = OMAP_DMA_SYNC_FRAME;
340                 dma_params.src_or_dst_synch     = 0;    /* Dest sync */
341
342                 src_burst = OMAP_DMA_DATA_BURST_16;     /* 16x32 read */
343                 dst_burst = OMAP_DMA_DATA_BURST_8;      /* 8x32 write */
344         } else {
345                 dma_params.src_amode    = OMAP_DMA_AMODE_DOUBLE_IDX;
346                 dma_params.src_start    = (unsigned long)fifo;
347                 dma_params.src_ei       = 1;
348                 dma_params.src_fi       = -31;          /* Loop 32 byte window */
349
350                 dma_params.dst_amode    = OMAP_DMA_AMODE_POST_INC;
351                 dma_params.dst_start    = (unsigned long)dma_addr;
352                 dma_params.dst_ei       = 0;
353                 dma_params.dst_fi       = 0;
354
355                 dma_params.trigger      = sync_dev;
356                 dma_params.sync_mode    = OMAP_DMA_SYNC_FRAME;
357                 dma_params.src_or_dst_synch     = 1;    /* Source sync */
358
359                 src_burst = OMAP_DMA_DATA_BURST_8;      /* 8x32 read */
360                 dst_burst = OMAP_DMA_DATA_BURST_16;     /* 16x32 write */
361         }
362
363         DBG(2, "ep%i %s using %i-bit %s dma from 0x%08lx to 0x%08lx\n",
364                 chdat->epnum, chdat->tx ? "tx" : "rx",
365                 (dma_params.data_type == OMAP_DMA_DATA_TYPE_S32) ? 32 : 16,
366                 ((dma_addr & 0x3) == 0) ? "sync" : "async",
367                 dma_params.src_start, dma_params.dst_start);
368
369         omap_set_dma_params(ch, &dma_params);
370         omap_set_dma_src_burst_mode(ch, src_burst);
371         omap_set_dma_dest_burst_mode(ch, dst_burst);
372         omap_set_dma_write_mode(ch, OMAP_DMA_WRITE_LAST_NON_POSTED);
373
374         /*
375          * Prepare MUSB for DMA transfer
376          */
377         if (chdat->tx) {
378                 musb_ep_select(musb_base, chdat->epnum);
379                 csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
380                 csr |= (MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB
381                         | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_MODE);
382                 csr &= ~MUSB_TXCSR_P_UNDERRUN;
383                 musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
384         } else {
385                 musb_ep_select(musb_base, chdat->epnum);
386                 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
387                 csr |= MUSB_RXCSR_DMAENAB;
388                 csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAMODE);
389                 musb_writew(hw_ep->regs, MUSB_RXCSR,
390                         csr | MUSB_RXCSR_P_WZC_BITS);
391         }
392
393         /*
394          * Start DMA transfer
395          */
396         omap_start_dma(ch);
397
398         if (chdat->tx) {
399                 /* Send transfer_packet_sz packets at a time */
400                 musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
401                         chdat->transfer_packet_sz);
402
403                 musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
404                         TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
405         } else {
406                 /* Receive transfer_packet_sz packets at a time */
407                 musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
408                         chdat->transfer_packet_sz << 16);
409
410                 musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
411                         TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
412         }
413
414         return true;
415 }
416
417 static int tusb_omap_dma_abort(struct dma_channel *channel)
418 {
419         struct tusb_omap_dma_ch *chdat = to_chdat(channel);
420         struct tusb_omap_dma    *tusb_dma = chdat->tusb_dma;
421
422         if (!dmareq_works()) {
423                 if (tusb_dma->ch >= 0) {
424                         omap_stop_dma(tusb_dma->ch);
425                         omap_free_dma(tusb_dma->ch);
426                         tusb_dma->ch = -1;
427                 }
428
429                 tusb_dma->dmareq = -1;
430                 tusb_dma->sync_dev = -1;
431         }
432
433         channel->status = MUSB_DMA_STATUS_FREE;
434
435         return 0;
436 }
437
438 static inline int tusb_omap_dma_allocate_dmareq(struct tusb_omap_dma_ch *chdat)
439 {
440         u32             reg = musb_readl(chdat->tusb_base, TUSB_DMA_EP_MAP);
441         int             i, dmareq_nr = -1;
442
443         const int sync_dev[6] = {
444                 OMAP24XX_DMA_EXT_DMAREQ0,
445                 OMAP24XX_DMA_EXT_DMAREQ1,
446                 OMAP24XX_DMA_EXT_DMAREQ2,
447                 OMAP24XX_DMA_EXT_DMAREQ3,
448                 OMAP24XX_DMA_EXT_DMAREQ4,
449                 OMAP24XX_DMA_EXT_DMAREQ5,
450         };
451
452         for (i = 0; i < MAX_DMAREQ; i++) {
453                 int cur = (reg & (0xf << (i * 5))) >> (i * 5);
454                 if (cur == 0) {
455                         dmareq_nr = i;
456                         break;
457                 }
458         }
459
460         if (dmareq_nr == -1)
461                 return -EAGAIN;
462
463         reg |= (chdat->epnum << (dmareq_nr * 5));
464         if (chdat->tx)
465                 reg |= ((1 << 4) << (dmareq_nr * 5));
466         musb_writel(chdat->tusb_base, TUSB_DMA_EP_MAP, reg);
467
468         chdat->dmareq = dmareq_nr;
469         chdat->sync_dev = sync_dev[chdat->dmareq];
470
471         return 0;
472 }
473
474 static inline void tusb_omap_dma_free_dmareq(struct tusb_omap_dma_ch *chdat)
475 {
476         u32 reg;
477
478         if (!chdat || chdat->dmareq < 0)
479                 return;
480
481         reg = musb_readl(chdat->tusb_base, TUSB_DMA_EP_MAP);
482         reg &= ~(0x1f << (chdat->dmareq * 5));
483         musb_writel(chdat->tusb_base, TUSB_DMA_EP_MAP, reg);
484
485         chdat->dmareq = -1;
486         chdat->sync_dev = -1;
487 }
488
489 static struct dma_channel *dma_channel_pool[MAX_DMAREQ];
490
491 static struct dma_channel *
492 tusb_omap_dma_allocate(struct dma_controller *c,
493                 struct musb_hw_ep *hw_ep,
494                 u8 tx)
495 {
496         int ret, i;
497         const char              *dev_name;
498         struct tusb_omap_dma    *tusb_dma;
499         struct musb             *musb;
500         void __iomem            *tusb_base;
501         struct dma_channel      *channel = NULL;
502         struct tusb_omap_dma_ch *chdat = NULL;
503         u32                     reg;
504
505         tusb_dma = container_of(c, struct tusb_omap_dma, controller);
506         musb = tusb_dma->musb;
507         tusb_base = musb->ctrl_base;
508
509         reg = musb_readl(tusb_base, TUSB_DMA_INT_MASK);
510         if (tx)
511                 reg &= ~(1 << hw_ep->epnum);
512         else
513                 reg &= ~(1 << (hw_ep->epnum + 15));
514         musb_writel(tusb_base, TUSB_DMA_INT_MASK, reg);
515
516         /* REVISIT: Why does dmareq5 not work? */
517         if (hw_ep->epnum == 0) {
518                 DBG(3, "Not allowing DMA for ep0 %s\n", tx ? "tx" : "rx");
519                 return NULL;
520         }
521
522         for (i = 0; i < MAX_DMAREQ; i++) {
523                 struct dma_channel *ch = dma_channel_pool[i];
524                 if (ch->status == MUSB_DMA_STATUS_UNKNOWN) {
525                         ch->status = MUSB_DMA_STATUS_FREE;
526                         channel = ch;
527                         chdat = ch->private_data;
528                         break;
529                 }
530         }
531
532         if (!channel)
533                 return NULL;
534
535         if (tx) {
536                 chdat->tx = 1;
537                 dev_name = "TUSB transmit";
538         } else {
539                 chdat->tx = 0;
540                 dev_name = "TUSB receive";
541         }
542
543         chdat->musb = tusb_dma->musb;
544         chdat->tusb_base = tusb_dma->tusb_base;
545         chdat->hw_ep = hw_ep;
546         chdat->epnum = hw_ep->epnum;
547         chdat->dmareq = -1;
548         chdat->completed_len = 0;
549         chdat->tusb_dma = tusb_dma;
550
551         channel->max_len = 0x7fffffff;
552         channel->desired_mode = 0;
553         channel->actual_len = 0;
554
555         if (dmareq_works()) {
556                 ret = tusb_omap_dma_allocate_dmareq(chdat);
557                 if (ret != 0)
558                         goto free_dmareq;
559
560                 ret = omap_request_dma(chdat->sync_dev, dev_name,
561                                 tusb_omap_dma_cb, channel, &chdat->ch);
562                 if (ret != 0)
563                         goto free_dmareq;
564         } else if (tusb_dma->ch == -1) {
565                 tusb_dma->dmareq = 0;
566                 tusb_dma->sync_dev = OMAP24XX_DMA_EXT_DMAREQ0;
567
568                 /* Callback data gets set later in the shared dmareq case */
569                 ret = omap_request_dma(tusb_dma->sync_dev, "TUSB shared",
570                                 tusb_omap_dma_cb, NULL, &tusb_dma->ch);
571                 if (ret != 0)
572                         goto free_dmareq;
573
574                 chdat->dmareq = -1;
575                 chdat->ch = -1;
576         }
577
578         DBG(3, "ep%i %s dma: %s dma%i dmareq%i sync%i\n",
579                 chdat->epnum,
580                 chdat->tx ? "tx" : "rx",
581                 chdat->ch >=0 ? "dedicated" : "shared",
582                 chdat->ch >= 0 ? chdat->ch : tusb_dma->ch,
583                 chdat->dmareq >= 0 ? chdat->dmareq : tusb_dma->dmareq,
584                 chdat->sync_dev >= 0 ? chdat->sync_dev : tusb_dma->sync_dev);
585
586         return channel;
587
588 free_dmareq:
589         tusb_omap_dma_free_dmareq(chdat);
590
591         DBG(3, "ep%i: Could not get a DMA channel\n", chdat->epnum);
592         channel->status = MUSB_DMA_STATUS_UNKNOWN;
593
594         return NULL;
595 }
596
597 static void tusb_omap_dma_release(struct dma_channel *channel)
598 {
599         struct tusb_omap_dma_ch *chdat = to_chdat(channel);
600         struct musb             *musb = chdat->musb;
601         void __iomem            *tusb_base = musb->ctrl_base;
602         u32                     reg;
603
604         DBG(3, "ep%i ch%i\n", chdat->epnum, chdat->ch);
605
606         reg = musb_readl(tusb_base, TUSB_DMA_INT_MASK);
607         if (chdat->tx)
608                 reg |= (1 << chdat->epnum);
609         else
610                 reg |= (1 << (chdat->epnum + 15));
611         musb_writel(tusb_base, TUSB_DMA_INT_MASK, reg);
612
613         reg = musb_readl(tusb_base, TUSB_DMA_INT_CLEAR);
614         if (chdat->tx)
615                 reg |= (1 << chdat->epnum);
616         else
617                 reg |= (1 << (chdat->epnum + 15));
618         musb_writel(tusb_base, TUSB_DMA_INT_CLEAR, reg);
619
620         channel->status = MUSB_DMA_STATUS_UNKNOWN;
621
622         if (chdat->ch >= 0) {
623                 omap_stop_dma(chdat->ch);
624                 omap_free_dma(chdat->ch);
625                 chdat->ch = -1;
626         }
627
628         if (chdat->dmareq >= 0)
629                 tusb_omap_dma_free_dmareq(chdat);
630
631         channel = NULL;
632 }
633
634 void dma_controller_destroy(struct dma_controller *c)
635 {
636         struct tusb_omap_dma    *tusb_dma;
637         int                     i;
638
639         tusb_dma = container_of(c, struct tusb_omap_dma, controller);
640         for (i = 0; i < MAX_DMAREQ; i++) {
641                 struct dma_channel *ch = dma_channel_pool[i];
642                 if (ch) {
643                         if (ch->private_data)
644                                 kfree(ch->private_data);
645                         kfree(ch);
646                 }
647         }
648
649         if (!dmareq_works() && tusb_dma && tusb_dma->ch >= 0)
650                 omap_free_dma(tusb_dma->ch);
651
652         kfree(tusb_dma);
653 }
654
655 struct dma_controller *__init
656 dma_controller_create(struct musb *musb, void __iomem *base)
657 {
658         void __iomem            *tusb_base = musb->ctrl_base;
659         struct tusb_omap_dma    *tusb_dma;
660         int                     i;
661
662         /* REVISIT: Get dmareq lines used from board-*.c */
663
664         musb_writel(musb->ctrl_base, TUSB_DMA_INT_MASK, 0x7fffffff);
665         musb_writel(musb->ctrl_base, TUSB_DMA_EP_MAP, 0);
666
667         musb_writel(tusb_base, TUSB_DMA_REQ_CONF,
668                 TUSB_DMA_REQ_CONF_BURST_SIZE(2)
669                 | TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f)
670                 | TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
671
672         tusb_dma = kzalloc(sizeof(struct tusb_omap_dma), GFP_KERNEL);
673         if (!tusb_dma)
674                 goto cleanup;
675
676         tusb_dma->musb = musb;
677         tusb_dma->tusb_base = musb->ctrl_base;
678
679         tusb_dma->ch = -1;
680         tusb_dma->dmareq = -1;
681         tusb_dma->sync_dev = -1;
682
683         tusb_dma->controller.start = tusb_omap_dma_start;
684         tusb_dma->controller.stop = tusb_omap_dma_stop;
685         tusb_dma->controller.channel_alloc = tusb_omap_dma_allocate;
686         tusb_dma->controller.channel_release = tusb_omap_dma_release;
687         tusb_dma->controller.channel_program = tusb_omap_dma_program;
688         tusb_dma->controller.channel_abort = tusb_omap_dma_abort;
689         tusb_dma->controller.private_data = tusb_dma;
690
691         for (i = 0; i < MAX_DMAREQ; i++) {
692                 struct dma_channel      *ch;
693                 struct tusb_omap_dma_ch *chdat;
694
695                 ch = kzalloc(sizeof(struct dma_channel), GFP_KERNEL);
696                 if (!ch)
697                         goto cleanup;
698
699                 dma_channel_pool[i] = ch;
700
701                 chdat = kzalloc(sizeof(struct tusb_omap_dma_ch), GFP_KERNEL);
702                 if (!chdat)
703                         goto cleanup;
704
705                 ch->status = MUSB_DMA_STATUS_UNKNOWN;
706                 ch->private_data = chdat;
707         }
708
709         return &tusb_dma->controller;
710
711 cleanup:
712         dma_controller_destroy(&tusb_dma->controller);
713
714         return NULL;
715 }