xref: /linux/drivers/usb/musb/tusb6010_omap.c (revision 07a67bbb95ea7977846bd851dab5f4f2be8e488c)
1550a7375SFelipe Balbi /*
2550a7375SFelipe Balbi  * TUSB6010 USB 2.0 OTG Dual Role controller OMAP DMA interface
3550a7375SFelipe Balbi  *
4550a7375SFelipe Balbi  * Copyright (C) 2006 Nokia Corporation
5550a7375SFelipe Balbi  * Tony Lindgren <tony@atomide.com>
6550a7375SFelipe Balbi  *
7550a7375SFelipe Balbi  * This program is free software; you can redistribute it and/or modify
8550a7375SFelipe Balbi  * it under the terms of the GNU General Public License version 2 as
9550a7375SFelipe Balbi  * published by the Free Software Foundation.
10550a7375SFelipe Balbi  */
11550a7375SFelipe Balbi #include <linux/module.h>
12550a7375SFelipe Balbi #include <linux/kernel.h>
13550a7375SFelipe Balbi #include <linux/errno.h>
14550a7375SFelipe Balbi #include <linux/init.h>
15550a7375SFelipe Balbi #include <linux/usb.h>
16550a7375SFelipe Balbi #include <linux/platform_device.h>
17550a7375SFelipe Balbi #include <linux/dma-mapping.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
19ce491cf8STony Lindgren #include <plat/dma.h>
20ce491cf8STony Lindgren #include <plat/mux.h>
21550a7375SFelipe Balbi 
22550a7375SFelipe Balbi #include "musb_core.h"
23240a16e2SFelipe Balbi #include "tusb6010.h"
24550a7375SFelipe Balbi 
25550a7375SFelipe Balbi #define to_chdat(c)		((struct tusb_omap_dma_ch *)(c)->private_data)
26550a7375SFelipe Balbi 
27550a7375SFelipe Balbi #define MAX_DMAREQ		5	/* REVISIT: Really 6, but req5 not OK */
28550a7375SFelipe Balbi 
29550a7375SFelipe Balbi struct tusb_omap_dma_ch {
30550a7375SFelipe Balbi 	struct musb		*musb;
31550a7375SFelipe Balbi 	void __iomem		*tbase;
32550a7375SFelipe Balbi 	unsigned long		phys_offset;
33550a7375SFelipe Balbi 	int			epnum;
34550a7375SFelipe Balbi 	u8			tx;
35550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
36550a7375SFelipe Balbi 
37550a7375SFelipe Balbi 	int			ch;
38550a7375SFelipe Balbi 	s8			dmareq;
39550a7375SFelipe Balbi 	s8			sync_dev;
40550a7375SFelipe Balbi 
41550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
42550a7375SFelipe Balbi 
431d0f11b3STony Lindgren 	dma_addr_t		dma_addr;
44550a7375SFelipe Balbi 
45550a7375SFelipe Balbi 	u32			len;
46550a7375SFelipe Balbi 	u16			packet_sz;
47550a7375SFelipe Balbi 	u16			transfer_packet_sz;
48550a7375SFelipe Balbi 	u32			transfer_len;
49550a7375SFelipe Balbi 	u32			completed_len;
50550a7375SFelipe Balbi };
51550a7375SFelipe Balbi 
52550a7375SFelipe Balbi struct tusb_omap_dma {
53550a7375SFelipe Balbi 	struct dma_controller		controller;
54550a7375SFelipe Balbi 	struct musb			*musb;
55550a7375SFelipe Balbi 	void __iomem			*tbase;
56550a7375SFelipe Balbi 
57550a7375SFelipe Balbi 	int				ch;
58550a7375SFelipe Balbi 	s8				dmareq;
59550a7375SFelipe Balbi 	s8				sync_dev;
60550a7375SFelipe Balbi 	unsigned			multichannel:1;
61550a7375SFelipe Balbi };
62550a7375SFelipe Balbi 
63550a7375SFelipe Balbi static int tusb_omap_dma_start(struct dma_controller *c)
64550a7375SFelipe Balbi {
65550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
66550a7375SFelipe Balbi 
67550a7375SFelipe Balbi 	tusb_dma = container_of(c, struct tusb_omap_dma, controller);
68550a7375SFelipe Balbi 
695c8a86e1SFelipe Balbi 	/* dev_dbg(musb->controller, "ep%i ch: %i\n", chdat->epnum, chdat->ch); */
70550a7375SFelipe Balbi 
71550a7375SFelipe Balbi 	return 0;
72550a7375SFelipe Balbi }
73550a7375SFelipe Balbi 
74550a7375SFelipe Balbi static int tusb_omap_dma_stop(struct dma_controller *c)
75550a7375SFelipe Balbi {
76550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
77550a7375SFelipe Balbi 
78550a7375SFelipe Balbi 	tusb_dma = container_of(c, struct tusb_omap_dma, controller);
79550a7375SFelipe Balbi 
805c8a86e1SFelipe Balbi 	/* dev_dbg(musb->controller, "ep%i ch: %i\n", chdat->epnum, chdat->ch); */
81550a7375SFelipe Balbi 
82550a7375SFelipe Balbi 	return 0;
83550a7375SFelipe Balbi }
84550a7375SFelipe Balbi 
85550a7375SFelipe Balbi /*
86550a7375SFelipe Balbi  * Allocate dmareq0 to the current channel unless it's already taken
87550a7375SFelipe Balbi  */
88550a7375SFelipe Balbi static inline int tusb_omap_use_shared_dmareq(struct tusb_omap_dma_ch *chdat)
89550a7375SFelipe Balbi {
90550a7375SFelipe Balbi 	u32		reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
91550a7375SFelipe Balbi 
92550a7375SFelipe Balbi 	if (reg != 0) {
9374c6f3a4SSergei Trofimovich 		dev_dbg(chdat->musb->controller, "ep%i dmareq0 is busy for ep%i\n",
94550a7375SFelipe Balbi 			chdat->epnum, reg & 0xf);
95550a7375SFelipe Balbi 		return -EAGAIN;
96550a7375SFelipe Balbi 	}
97550a7375SFelipe Balbi 
98550a7375SFelipe Balbi 	if (chdat->tx)
99550a7375SFelipe Balbi 		reg = (1 << 4) | chdat->epnum;
100550a7375SFelipe Balbi 	else
101550a7375SFelipe Balbi 		reg = chdat->epnum;
102550a7375SFelipe Balbi 
103550a7375SFelipe Balbi 	musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
104550a7375SFelipe Balbi 
105550a7375SFelipe Balbi 	return 0;
106550a7375SFelipe Balbi }
107550a7375SFelipe Balbi 
108550a7375SFelipe Balbi static inline void tusb_omap_free_shared_dmareq(struct tusb_omap_dma_ch *chdat)
109550a7375SFelipe Balbi {
110550a7375SFelipe Balbi 	u32		reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
111550a7375SFelipe Balbi 
112550a7375SFelipe Balbi 	if ((reg & 0xf) != chdat->epnum) {
113550a7375SFelipe Balbi 		printk(KERN_ERR "ep%i trying to release dmareq0 for ep%i\n",
114550a7375SFelipe Balbi 			chdat->epnum, reg & 0xf);
115550a7375SFelipe Balbi 		return;
116550a7375SFelipe Balbi 	}
117550a7375SFelipe Balbi 	musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, 0);
118550a7375SFelipe Balbi }
119550a7375SFelipe Balbi 
120550a7375SFelipe Balbi /*
121550a7375SFelipe Balbi  * See also musb_dma_completion in plat_uds.c and musb_g_[tx|rx]() in
122550a7375SFelipe Balbi  * musb_gadget.c.
123550a7375SFelipe Balbi  */
124550a7375SFelipe Balbi static void tusb_omap_dma_cb(int lch, u16 ch_status, void *data)
125550a7375SFelipe Balbi {
126550a7375SFelipe Balbi 	struct dma_channel	*channel = (struct dma_channel *)data;
127550a7375SFelipe Balbi 	struct tusb_omap_dma_ch	*chdat = to_chdat(channel);
128550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma = chdat->tusb_dma;
129550a7375SFelipe Balbi 	struct musb		*musb = chdat->musb;
1301d0f11b3STony Lindgren 	struct device		*dev = musb->controller;
131550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = chdat->hw_ep;
132550a7375SFelipe Balbi 	void __iomem		*ep_conf = hw_ep->conf;
133550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
134550a7375SFelipe Balbi 	unsigned long		remaining, flags, pio;
135550a7375SFelipe Balbi 	int			ch;
136550a7375SFelipe Balbi 
137550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
138550a7375SFelipe Balbi 
139550a7375SFelipe Balbi 	if (tusb_dma->multichannel)
140550a7375SFelipe Balbi 		ch = chdat->ch;
141550a7375SFelipe Balbi 	else
142550a7375SFelipe Balbi 		ch = tusb_dma->ch;
143550a7375SFelipe Balbi 
144550a7375SFelipe Balbi 	if (ch_status != OMAP_DMA_BLOCK_IRQ)
145550a7375SFelipe Balbi 		printk(KERN_ERR "TUSB DMA error status: %i\n", ch_status);
146550a7375SFelipe Balbi 
1475c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i %s dma callback ch: %i status: %x\n",
148550a7375SFelipe Balbi 		chdat->epnum, chdat->tx ? "tx" : "rx",
149550a7375SFelipe Balbi 		ch, ch_status);
150550a7375SFelipe Balbi 
151550a7375SFelipe Balbi 	if (chdat->tx)
152550a7375SFelipe Balbi 		remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
153550a7375SFelipe Balbi 	else
154550a7375SFelipe Balbi 		remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
155550a7375SFelipe Balbi 
156550a7375SFelipe Balbi 	remaining = TUSB_EP_CONFIG_XFR_SIZE(remaining);
157550a7375SFelipe Balbi 
158550a7375SFelipe Balbi 	/* HW issue #10: XFR_SIZE may get corrupt on DMA (both async & sync) */
159550a7375SFelipe Balbi 	if (unlikely(remaining > chdat->transfer_len)) {
1605c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Corrupt %s dma ch%i XFR_SIZE: 0x%08lx\n",
161550a7375SFelipe Balbi 			chdat->tx ? "tx" : "rx", chdat->ch,
162550a7375SFelipe Balbi 			remaining);
163550a7375SFelipe Balbi 		remaining = 0;
164550a7375SFelipe Balbi 	}
165550a7375SFelipe Balbi 
166550a7375SFelipe Balbi 	channel->actual_len = chdat->transfer_len - remaining;
167550a7375SFelipe Balbi 	pio = chdat->len - channel->actual_len;
168550a7375SFelipe Balbi 
1695c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "DMA remaining %lu/%u\n", remaining, chdat->transfer_len);
170550a7375SFelipe Balbi 
171550a7375SFelipe Balbi 	/* Transfer remaining 1 - 31 bytes */
172550a7375SFelipe Balbi 	if (pio > 0 && pio < 32) {
173550a7375SFelipe Balbi 		u8	*buf;
174550a7375SFelipe Balbi 
1755c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Using PIO for remaining %lu bytes\n", pio);
176550a7375SFelipe Balbi 		buf = phys_to_virt((u32)chdat->dma_addr) + chdat->transfer_len;
177550a7375SFelipe Balbi 		if (chdat->tx) {
1781d0f11b3STony Lindgren 			dma_unmap_single(dev, chdat->dma_addr,
1791d0f11b3STony Lindgren 						chdat->transfer_len,
1801d0f11b3STony Lindgren 						DMA_TO_DEVICE);
181550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, pio, buf);
182550a7375SFelipe Balbi 		} else {
1831d0f11b3STony Lindgren 			dma_unmap_single(dev, chdat->dma_addr,
1841d0f11b3STony Lindgren 						chdat->transfer_len,
1851d0f11b3STony Lindgren 						DMA_FROM_DEVICE);
186550a7375SFelipe Balbi 			musb_read_fifo(hw_ep, pio, buf);
187550a7375SFelipe Balbi 		}
188550a7375SFelipe Balbi 		channel->actual_len += pio;
189550a7375SFelipe Balbi 	}
190550a7375SFelipe Balbi 
191550a7375SFelipe Balbi 	if (!tusb_dma->multichannel)
192550a7375SFelipe Balbi 		tusb_omap_free_shared_dmareq(chdat);
193550a7375SFelipe Balbi 
194550a7375SFelipe Balbi 	channel->status = MUSB_DMA_STATUS_FREE;
195550a7375SFelipe Balbi 
196550a7375SFelipe Balbi 	/* Handle only RX callbacks here. TX callbacks must be handled based
197550a7375SFelipe Balbi 	 * on the TUSB DMA status interrupt.
198550a7375SFelipe Balbi 	 * REVISIT: Use both TUSB DMA status interrupt and OMAP DMA callback
199550a7375SFelipe Balbi 	 * interrupt for RX and TX.
200550a7375SFelipe Balbi 	 */
201550a7375SFelipe Balbi 	if (!chdat->tx)
202550a7375SFelipe Balbi 		musb_dma_completion(musb, chdat->epnum, chdat->tx);
203550a7375SFelipe Balbi 
204550a7375SFelipe Balbi 	/* We must terminate short tx transfers manually by setting TXPKTRDY.
205550a7375SFelipe Balbi 	 * REVISIT: This same problem may occur with other MUSB dma as well.
206550a7375SFelipe Balbi 	 * Easy to test with g_ether by pinging the MUSB board with ping -s54.
207550a7375SFelipe Balbi 	 */
208550a7375SFelipe Balbi 	if ((chdat->transfer_len < chdat->packet_sz)
209550a7375SFelipe Balbi 			|| (chdat->transfer_len % chdat->packet_sz != 0)) {
210550a7375SFelipe Balbi 		u16	csr;
211550a7375SFelipe Balbi 
212550a7375SFelipe Balbi 		if (chdat->tx) {
2135c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "terminating short tx packet\n");
214550a7375SFelipe Balbi 			musb_ep_select(mbase, chdat->epnum);
215550a7375SFelipe Balbi 			csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
216550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY
217550a7375SFelipe Balbi 				| MUSB_TXCSR_P_WZC_BITS;
218550a7375SFelipe Balbi 			musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
219550a7375SFelipe Balbi 		}
220550a7375SFelipe Balbi 	}
221550a7375SFelipe Balbi 
222550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
223550a7375SFelipe Balbi }
224550a7375SFelipe Balbi 
225550a7375SFelipe Balbi static int tusb_omap_dma_program(struct dma_channel *channel, u16 packet_sz,
226550a7375SFelipe Balbi 				u8 rndis_mode, dma_addr_t dma_addr, u32 len)
227550a7375SFelipe Balbi {
228550a7375SFelipe Balbi 	struct tusb_omap_dma_ch		*chdat = to_chdat(channel);
229550a7375SFelipe Balbi 	struct tusb_omap_dma		*tusb_dma = chdat->tusb_dma;
230550a7375SFelipe Balbi 	struct musb			*musb = chdat->musb;
2311d0f11b3STony Lindgren 	struct device			*dev = musb->controller;
232550a7375SFelipe Balbi 	struct musb_hw_ep		*hw_ep = chdat->hw_ep;
233550a7375SFelipe Balbi 	void __iomem			*mbase = musb->mregs;
234550a7375SFelipe Balbi 	void __iomem			*ep_conf = hw_ep->conf;
235550a7375SFelipe Balbi 	dma_addr_t			fifo = hw_ep->fifo_sync;
236550a7375SFelipe Balbi 	struct omap_dma_channel_params	dma_params;
237550a7375SFelipe Balbi 	u32				dma_remaining;
238550a7375SFelipe Balbi 	int				src_burst, dst_burst;
239550a7375SFelipe Balbi 	u16				csr;
240550a7375SFelipe Balbi 	int				ch;
241550a7375SFelipe Balbi 	s8				dmareq;
242550a7375SFelipe Balbi 	s8				sync_dev;
243550a7375SFelipe Balbi 
244550a7375SFelipe Balbi 	if (unlikely(dma_addr & 0x1) || (len < 32) || (len > packet_sz))
245550a7375SFelipe Balbi 		return false;
246550a7375SFelipe Balbi 
247550a7375SFelipe Balbi 	/*
248550a7375SFelipe Balbi 	 * HW issue #10: Async dma will eventually corrupt the XFR_SIZE
249550a7375SFelipe Balbi 	 * register which will cause missed DMA interrupt. We could try to
250550a7375SFelipe Balbi 	 * use a timer for the callback, but it is unsafe as the XFR_SIZE
251550a7375SFelipe Balbi 	 * register is corrupt, and we won't know if the DMA worked.
252550a7375SFelipe Balbi 	 */
253550a7375SFelipe Balbi 	if (dma_addr & 0x2)
254550a7375SFelipe Balbi 		return false;
255550a7375SFelipe Balbi 
256550a7375SFelipe Balbi 	/*
257550a7375SFelipe Balbi 	 * Because of HW issue #10, it seems like mixing sync DMA and async
258550a7375SFelipe Balbi 	 * PIO access can confuse the DMA. Make sure XFR_SIZE is reset before
259550a7375SFelipe Balbi 	 * using the channel for DMA.
260550a7375SFelipe Balbi 	 */
261550a7375SFelipe Balbi 	if (chdat->tx)
262550a7375SFelipe Balbi 		dma_remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
263550a7375SFelipe Balbi 	else
264550a7375SFelipe Balbi 		dma_remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
265550a7375SFelipe Balbi 
266550a7375SFelipe Balbi 	dma_remaining = TUSB_EP_CONFIG_XFR_SIZE(dma_remaining);
267550a7375SFelipe Balbi 	if (dma_remaining) {
2685c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Busy %s dma ch%i, not using: %08x\n",
269550a7375SFelipe Balbi 			chdat->tx ? "tx" : "rx", chdat->ch,
270550a7375SFelipe Balbi 			dma_remaining);
271550a7375SFelipe Balbi 		return false;
272550a7375SFelipe Balbi 	}
273550a7375SFelipe Balbi 
274550a7375SFelipe Balbi 	chdat->transfer_len = len & ~0x1f;
275550a7375SFelipe Balbi 
276550a7375SFelipe Balbi 	if (len < packet_sz)
277550a7375SFelipe Balbi 		chdat->transfer_packet_sz = chdat->transfer_len;
278550a7375SFelipe Balbi 	else
279550a7375SFelipe Balbi 		chdat->transfer_packet_sz = packet_sz;
280550a7375SFelipe Balbi 
281550a7375SFelipe Balbi 	if (tusb_dma->multichannel) {
282550a7375SFelipe Balbi 		ch = chdat->ch;
283550a7375SFelipe Balbi 		dmareq = chdat->dmareq;
284550a7375SFelipe Balbi 		sync_dev = chdat->sync_dev;
285550a7375SFelipe Balbi 	} else {
286550a7375SFelipe Balbi 		if (tusb_omap_use_shared_dmareq(chdat) != 0) {
2875c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "could not get dma for ep%i\n", chdat->epnum);
288550a7375SFelipe Balbi 			return false;
289550a7375SFelipe Balbi 		}
290550a7375SFelipe Balbi 		if (tusb_dma->ch < 0) {
291550a7375SFelipe Balbi 			/* REVISIT: This should get blocked earlier, happens
292550a7375SFelipe Balbi 			 * with MSC ErrorRecoveryTest
293550a7375SFelipe Balbi 			 */
294550a7375SFelipe Balbi 			WARN_ON(1);
295550a7375SFelipe Balbi 			return false;
296550a7375SFelipe Balbi 		}
297550a7375SFelipe Balbi 
298550a7375SFelipe Balbi 		ch = tusb_dma->ch;
299550a7375SFelipe Balbi 		dmareq = tusb_dma->dmareq;
300550a7375SFelipe Balbi 		sync_dev = tusb_dma->sync_dev;
301550a7375SFelipe Balbi 		omap_set_dma_callback(ch, tusb_omap_dma_cb, channel);
302550a7375SFelipe Balbi 	}
303550a7375SFelipe Balbi 
304550a7375SFelipe Balbi 	chdat->packet_sz = packet_sz;
305550a7375SFelipe Balbi 	chdat->len = len;
306550a7375SFelipe Balbi 	channel->actual_len = 0;
3071d0f11b3STony Lindgren 	chdat->dma_addr = dma_addr;
308550a7375SFelipe Balbi 	channel->status = MUSB_DMA_STATUS_BUSY;
309550a7375SFelipe Balbi 
310550a7375SFelipe Balbi 	/* Since we're recycling dma areas, we need to clean or invalidate */
311550a7375SFelipe Balbi 	if (chdat->tx)
3121d0f11b3STony Lindgren 		dma_map_single(dev, phys_to_virt(dma_addr), len,
3131d0f11b3STony Lindgren 				DMA_TO_DEVICE);
314550a7375SFelipe Balbi 	else
3151d0f11b3STony Lindgren 		dma_map_single(dev, phys_to_virt(dma_addr), len,
3161d0f11b3STony Lindgren 				DMA_FROM_DEVICE);
317550a7375SFelipe Balbi 
318550a7375SFelipe Balbi 	/* Use 16-bit transfer if dma_addr is not 32-bit aligned */
319550a7375SFelipe Balbi 	if ((dma_addr & 0x3) == 0) {
320550a7375SFelipe Balbi 		dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
321550a7375SFelipe Balbi 		dma_params.elem_count = 8;		/* Elements in frame */
322550a7375SFelipe Balbi 	} else {
323550a7375SFelipe Balbi 		dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
324550a7375SFelipe Balbi 		dma_params.elem_count = 16;		/* Elements in frame */
325550a7375SFelipe Balbi 		fifo = hw_ep->fifo_async;
326550a7375SFelipe Balbi 	}
327550a7375SFelipe Balbi 
328550a7375SFelipe Balbi 	dma_params.frame_count	= chdat->transfer_len / 32; /* Burst sz frame */
329550a7375SFelipe Balbi 
3305c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i %s dma ch%i dma: %08x len: %u(%u) packet_sz: %i(%i)\n",
331550a7375SFelipe Balbi 		chdat->epnum, chdat->tx ? "tx" : "rx",
332550a7375SFelipe Balbi 		ch, dma_addr, chdat->transfer_len, len,
333550a7375SFelipe Balbi 		chdat->transfer_packet_sz, packet_sz);
334550a7375SFelipe Balbi 
335550a7375SFelipe Balbi 	/*
336550a7375SFelipe Balbi 	 * Prepare omap DMA for transfer
337550a7375SFelipe Balbi 	 */
338550a7375SFelipe Balbi 	if (chdat->tx) {
339550a7375SFelipe Balbi 		dma_params.src_amode	= OMAP_DMA_AMODE_POST_INC;
340550a7375SFelipe Balbi 		dma_params.src_start	= (unsigned long)dma_addr;
341550a7375SFelipe Balbi 		dma_params.src_ei	= 0;
342550a7375SFelipe Balbi 		dma_params.src_fi	= 0;
343550a7375SFelipe Balbi 
344550a7375SFelipe Balbi 		dma_params.dst_amode	= OMAP_DMA_AMODE_DOUBLE_IDX;
345550a7375SFelipe Balbi 		dma_params.dst_start	= (unsigned long)fifo;
346550a7375SFelipe Balbi 		dma_params.dst_ei	= 1;
347550a7375SFelipe Balbi 		dma_params.dst_fi	= -31;	/* Loop 32 byte window */
348550a7375SFelipe Balbi 
349550a7375SFelipe Balbi 		dma_params.trigger	= sync_dev;
350550a7375SFelipe Balbi 		dma_params.sync_mode	= OMAP_DMA_SYNC_FRAME;
351550a7375SFelipe Balbi 		dma_params.src_or_dst_synch	= 0;	/* Dest sync */
352550a7375SFelipe Balbi 
353550a7375SFelipe Balbi 		src_burst = OMAP_DMA_DATA_BURST_16;	/* 16x32 read */
354550a7375SFelipe Balbi 		dst_burst = OMAP_DMA_DATA_BURST_8;	/* 8x32 write */
355550a7375SFelipe Balbi 	} else {
356550a7375SFelipe Balbi 		dma_params.src_amode	= OMAP_DMA_AMODE_DOUBLE_IDX;
357550a7375SFelipe Balbi 		dma_params.src_start	= (unsigned long)fifo;
358550a7375SFelipe Balbi 		dma_params.src_ei	= 1;
359550a7375SFelipe Balbi 		dma_params.src_fi	= -31;	/* Loop 32 byte window */
360550a7375SFelipe Balbi 
361550a7375SFelipe Balbi 		dma_params.dst_amode	= OMAP_DMA_AMODE_POST_INC;
362550a7375SFelipe Balbi 		dma_params.dst_start	= (unsigned long)dma_addr;
363550a7375SFelipe Balbi 		dma_params.dst_ei	= 0;
364550a7375SFelipe Balbi 		dma_params.dst_fi	= 0;
365550a7375SFelipe Balbi 
366550a7375SFelipe Balbi 		dma_params.trigger	= sync_dev;
367550a7375SFelipe Balbi 		dma_params.sync_mode	= OMAP_DMA_SYNC_FRAME;
368550a7375SFelipe Balbi 		dma_params.src_or_dst_synch	= 1;	/* Source sync */
369550a7375SFelipe Balbi 
370550a7375SFelipe Balbi 		src_burst = OMAP_DMA_DATA_BURST_8;	/* 8x32 read */
371550a7375SFelipe Balbi 		dst_burst = OMAP_DMA_DATA_BURST_16;	/* 16x32 write */
372550a7375SFelipe Balbi 	}
373550a7375SFelipe Balbi 
3745c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i %s using %i-bit %s dma from 0x%08lx to 0x%08lx\n",
375550a7375SFelipe Balbi 		chdat->epnum, chdat->tx ? "tx" : "rx",
376550a7375SFelipe Balbi 		(dma_params.data_type == OMAP_DMA_DATA_TYPE_S32) ? 32 : 16,
377550a7375SFelipe Balbi 		((dma_addr & 0x3) == 0) ? "sync" : "async",
378550a7375SFelipe Balbi 		dma_params.src_start, dma_params.dst_start);
379550a7375SFelipe Balbi 
380550a7375SFelipe Balbi 	omap_set_dma_params(ch, &dma_params);
381550a7375SFelipe Balbi 	omap_set_dma_src_burst_mode(ch, src_burst);
382550a7375SFelipe Balbi 	omap_set_dma_dest_burst_mode(ch, dst_burst);
383550a7375SFelipe Balbi 	omap_set_dma_write_mode(ch, OMAP_DMA_WRITE_LAST_NON_POSTED);
384550a7375SFelipe Balbi 
385550a7375SFelipe Balbi 	/*
386550a7375SFelipe Balbi 	 * Prepare MUSB for DMA transfer
387550a7375SFelipe Balbi 	 */
388550a7375SFelipe Balbi 	if (chdat->tx) {
389550a7375SFelipe Balbi 		musb_ep_select(mbase, chdat->epnum);
390550a7375SFelipe Balbi 		csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
391550a7375SFelipe Balbi 		csr |= (MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB
392550a7375SFelipe Balbi 			| MUSB_TXCSR_DMAMODE | MUSB_TXCSR_MODE);
393550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_UNDERRUN;
394550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
395550a7375SFelipe Balbi 	} else {
396550a7375SFelipe Balbi 		musb_ep_select(mbase, chdat->epnum);
397550a7375SFelipe Balbi 		csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
398550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_DMAENAB;
399550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAMODE);
400550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR,
401550a7375SFelipe Balbi 			csr | MUSB_RXCSR_P_WZC_BITS);
402550a7375SFelipe Balbi 	}
403550a7375SFelipe Balbi 
404550a7375SFelipe Balbi 	/*
405550a7375SFelipe Balbi 	 * Start DMA transfer
406550a7375SFelipe Balbi 	 */
407550a7375SFelipe Balbi 	omap_start_dma(ch);
408550a7375SFelipe Balbi 
409550a7375SFelipe Balbi 	if (chdat->tx) {
410550a7375SFelipe Balbi 		/* Send transfer_packet_sz packets at a time */
411550a7375SFelipe Balbi 		musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
412550a7375SFelipe Balbi 			chdat->transfer_packet_sz);
413550a7375SFelipe Balbi 
414550a7375SFelipe Balbi 		musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
415550a7375SFelipe Balbi 			TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
416550a7375SFelipe Balbi 	} else {
417550a7375SFelipe Balbi 		/* Receive transfer_packet_sz packets at a time */
418550a7375SFelipe Balbi 		musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
419550a7375SFelipe Balbi 			chdat->transfer_packet_sz << 16);
420550a7375SFelipe Balbi 
421550a7375SFelipe Balbi 		musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
422550a7375SFelipe Balbi 			TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
423550a7375SFelipe Balbi 	}
424550a7375SFelipe Balbi 
425550a7375SFelipe Balbi 	return true;
426550a7375SFelipe Balbi }
427550a7375SFelipe Balbi 
428550a7375SFelipe Balbi static int tusb_omap_dma_abort(struct dma_channel *channel)
429550a7375SFelipe Balbi {
430550a7375SFelipe Balbi 	struct tusb_omap_dma_ch	*chdat = to_chdat(channel);
431550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma = chdat->tusb_dma;
432550a7375SFelipe Balbi 
433550a7375SFelipe Balbi 	if (!tusb_dma->multichannel) {
434550a7375SFelipe Balbi 		if (tusb_dma->ch >= 0) {
435550a7375SFelipe Balbi 			omap_stop_dma(tusb_dma->ch);
436550a7375SFelipe Balbi 			omap_free_dma(tusb_dma->ch);
437550a7375SFelipe Balbi 			tusb_dma->ch = -1;
438550a7375SFelipe Balbi 		}
439550a7375SFelipe Balbi 
440550a7375SFelipe Balbi 		tusb_dma->dmareq = -1;
441550a7375SFelipe Balbi 		tusb_dma->sync_dev = -1;
442550a7375SFelipe Balbi 	}
443550a7375SFelipe Balbi 
444550a7375SFelipe Balbi 	channel->status = MUSB_DMA_STATUS_FREE;
445550a7375SFelipe Balbi 
446550a7375SFelipe Balbi 	return 0;
447550a7375SFelipe Balbi }
448550a7375SFelipe Balbi 
449550a7375SFelipe Balbi static inline int tusb_omap_dma_allocate_dmareq(struct tusb_omap_dma_ch *chdat)
450550a7375SFelipe Balbi {
451550a7375SFelipe Balbi 	u32		reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
452550a7375SFelipe Balbi 	int		i, dmareq_nr = -1;
453550a7375SFelipe Balbi 
454550a7375SFelipe Balbi 	const int sync_dev[6] = {
455550a7375SFelipe Balbi 		OMAP24XX_DMA_EXT_DMAREQ0,
456550a7375SFelipe Balbi 		OMAP24XX_DMA_EXT_DMAREQ1,
457550a7375SFelipe Balbi 		OMAP242X_DMA_EXT_DMAREQ2,
458550a7375SFelipe Balbi 		OMAP242X_DMA_EXT_DMAREQ3,
459550a7375SFelipe Balbi 		OMAP242X_DMA_EXT_DMAREQ4,
460550a7375SFelipe Balbi 		OMAP242X_DMA_EXT_DMAREQ5,
461550a7375SFelipe Balbi 	};
462550a7375SFelipe Balbi 
463550a7375SFelipe Balbi 	for (i = 0; i < MAX_DMAREQ; i++) {
464550a7375SFelipe Balbi 		int cur = (reg & (0xf << (i * 5))) >> (i * 5);
465550a7375SFelipe Balbi 		if (cur == 0) {
466550a7375SFelipe Balbi 			dmareq_nr = i;
467550a7375SFelipe Balbi 			break;
468550a7375SFelipe Balbi 		}
469550a7375SFelipe Balbi 	}
470550a7375SFelipe Balbi 
471550a7375SFelipe Balbi 	if (dmareq_nr == -1)
472550a7375SFelipe Balbi 		return -EAGAIN;
473550a7375SFelipe Balbi 
474550a7375SFelipe Balbi 	reg |= (chdat->epnum << (dmareq_nr * 5));
475550a7375SFelipe Balbi 	if (chdat->tx)
476550a7375SFelipe Balbi 		reg |= ((1 << 4) << (dmareq_nr * 5));
477550a7375SFelipe Balbi 	musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
478550a7375SFelipe Balbi 
479550a7375SFelipe Balbi 	chdat->dmareq = dmareq_nr;
480550a7375SFelipe Balbi 	chdat->sync_dev = sync_dev[chdat->dmareq];
481550a7375SFelipe Balbi 
482550a7375SFelipe Balbi 	return 0;
483550a7375SFelipe Balbi }
484550a7375SFelipe Balbi 
485550a7375SFelipe Balbi static inline void tusb_omap_dma_free_dmareq(struct tusb_omap_dma_ch *chdat)
486550a7375SFelipe Balbi {
487550a7375SFelipe Balbi 	u32 reg;
488550a7375SFelipe Balbi 
489550a7375SFelipe Balbi 	if (!chdat || chdat->dmareq < 0)
490550a7375SFelipe Balbi 		return;
491550a7375SFelipe Balbi 
492550a7375SFelipe Balbi 	reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
493550a7375SFelipe Balbi 	reg &= ~(0x1f << (chdat->dmareq * 5));
494550a7375SFelipe Balbi 	musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
495550a7375SFelipe Balbi 
496550a7375SFelipe Balbi 	chdat->dmareq = -1;
497550a7375SFelipe Balbi 	chdat->sync_dev = -1;
498550a7375SFelipe Balbi }
499550a7375SFelipe Balbi 
500550a7375SFelipe Balbi static struct dma_channel *dma_channel_pool[MAX_DMAREQ];
501550a7375SFelipe Balbi 
502550a7375SFelipe Balbi static struct dma_channel *
503550a7375SFelipe Balbi tusb_omap_dma_allocate(struct dma_controller *c,
504550a7375SFelipe Balbi 		struct musb_hw_ep *hw_ep,
505550a7375SFelipe Balbi 		u8 tx)
506550a7375SFelipe Balbi {
507550a7375SFelipe Balbi 	int ret, i;
508550a7375SFelipe Balbi 	const char		*dev_name;
509550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
510550a7375SFelipe Balbi 	struct musb		*musb;
511550a7375SFelipe Balbi 	void __iomem		*tbase;
512550a7375SFelipe Balbi 	struct dma_channel	*channel = NULL;
513550a7375SFelipe Balbi 	struct tusb_omap_dma_ch	*chdat = NULL;
514550a7375SFelipe Balbi 	u32			reg;
515550a7375SFelipe Balbi 
516550a7375SFelipe Balbi 	tusb_dma = container_of(c, struct tusb_omap_dma, controller);
517550a7375SFelipe Balbi 	musb = tusb_dma->musb;
518550a7375SFelipe Balbi 	tbase = musb->ctrl_base;
519550a7375SFelipe Balbi 
520550a7375SFelipe Balbi 	reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
521550a7375SFelipe Balbi 	if (tx)
522550a7375SFelipe Balbi 		reg &= ~(1 << hw_ep->epnum);
523550a7375SFelipe Balbi 	else
524550a7375SFelipe Balbi 		reg &= ~(1 << (hw_ep->epnum + 15));
525550a7375SFelipe Balbi 	musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
526550a7375SFelipe Balbi 
527550a7375SFelipe Balbi 	/* REVISIT: Why does dmareq5 not work? */
528550a7375SFelipe Balbi 	if (hw_ep->epnum == 0) {
5295c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Not allowing DMA for ep0 %s\n", tx ? "tx" : "rx");
530550a7375SFelipe Balbi 		return NULL;
531550a7375SFelipe Balbi 	}
532550a7375SFelipe Balbi 
533550a7375SFelipe Balbi 	for (i = 0; i < MAX_DMAREQ; i++) {
534550a7375SFelipe Balbi 		struct dma_channel *ch = dma_channel_pool[i];
535550a7375SFelipe Balbi 		if (ch->status == MUSB_DMA_STATUS_UNKNOWN) {
536550a7375SFelipe Balbi 			ch->status = MUSB_DMA_STATUS_FREE;
537550a7375SFelipe Balbi 			channel = ch;
538550a7375SFelipe Balbi 			chdat = ch->private_data;
539550a7375SFelipe Balbi 			break;
540550a7375SFelipe Balbi 		}
541550a7375SFelipe Balbi 	}
542550a7375SFelipe Balbi 
543550a7375SFelipe Balbi 	if (!channel)
544550a7375SFelipe Balbi 		return NULL;
545550a7375SFelipe Balbi 
546550a7375SFelipe Balbi 	if (tx) {
547550a7375SFelipe Balbi 		chdat->tx = 1;
548550a7375SFelipe Balbi 		dev_name = "TUSB transmit";
549550a7375SFelipe Balbi 	} else {
550550a7375SFelipe Balbi 		chdat->tx = 0;
551550a7375SFelipe Balbi 		dev_name = "TUSB receive";
552550a7375SFelipe Balbi 	}
553550a7375SFelipe Balbi 
554550a7375SFelipe Balbi 	chdat->musb = tusb_dma->musb;
555550a7375SFelipe Balbi 	chdat->tbase = tusb_dma->tbase;
556550a7375SFelipe Balbi 	chdat->hw_ep = hw_ep;
557550a7375SFelipe Balbi 	chdat->epnum = hw_ep->epnum;
558550a7375SFelipe Balbi 	chdat->dmareq = -1;
559550a7375SFelipe Balbi 	chdat->completed_len = 0;
560550a7375SFelipe Balbi 	chdat->tusb_dma = tusb_dma;
561550a7375SFelipe Balbi 
562550a7375SFelipe Balbi 	channel->max_len = 0x7fffffff;
563550a7375SFelipe Balbi 	channel->desired_mode = 0;
564550a7375SFelipe Balbi 	channel->actual_len = 0;
565550a7375SFelipe Balbi 
566550a7375SFelipe Balbi 	if (tusb_dma->multichannel) {
567550a7375SFelipe Balbi 		ret = tusb_omap_dma_allocate_dmareq(chdat);
568550a7375SFelipe Balbi 		if (ret != 0)
569550a7375SFelipe Balbi 			goto free_dmareq;
570550a7375SFelipe Balbi 
571550a7375SFelipe Balbi 		ret = omap_request_dma(chdat->sync_dev, dev_name,
572550a7375SFelipe Balbi 				tusb_omap_dma_cb, channel, &chdat->ch);
573550a7375SFelipe Balbi 		if (ret != 0)
574550a7375SFelipe Balbi 			goto free_dmareq;
575550a7375SFelipe Balbi 	} else if (tusb_dma->ch == -1) {
576550a7375SFelipe Balbi 		tusb_dma->dmareq = 0;
577550a7375SFelipe Balbi 		tusb_dma->sync_dev = OMAP24XX_DMA_EXT_DMAREQ0;
578550a7375SFelipe Balbi 
579550a7375SFelipe Balbi 		/* Callback data gets set later in the shared dmareq case */
580550a7375SFelipe Balbi 		ret = omap_request_dma(tusb_dma->sync_dev, "TUSB shared",
581550a7375SFelipe Balbi 				tusb_omap_dma_cb, NULL, &tusb_dma->ch);
582550a7375SFelipe Balbi 		if (ret != 0)
583550a7375SFelipe Balbi 			goto free_dmareq;
584550a7375SFelipe Balbi 
585550a7375SFelipe Balbi 		chdat->dmareq = -1;
586550a7375SFelipe Balbi 		chdat->ch = -1;
587550a7375SFelipe Balbi 	}
588550a7375SFelipe Balbi 
5895c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i %s dma: %s dma%i dmareq%i sync%i\n",
590550a7375SFelipe Balbi 		chdat->epnum,
591550a7375SFelipe Balbi 		chdat->tx ? "tx" : "rx",
592550a7375SFelipe Balbi 		chdat->ch >= 0 ? "dedicated" : "shared",
593550a7375SFelipe Balbi 		chdat->ch >= 0 ? chdat->ch : tusb_dma->ch,
594550a7375SFelipe Balbi 		chdat->dmareq >= 0 ? chdat->dmareq : tusb_dma->dmareq,
595550a7375SFelipe Balbi 		chdat->sync_dev >= 0 ? chdat->sync_dev : tusb_dma->sync_dev);
596550a7375SFelipe Balbi 
597550a7375SFelipe Balbi 	return channel;
598550a7375SFelipe Balbi 
599550a7375SFelipe Balbi free_dmareq:
600550a7375SFelipe Balbi 	tusb_omap_dma_free_dmareq(chdat);
601550a7375SFelipe Balbi 
6025c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i: Could not get a DMA channel\n", chdat->epnum);
603550a7375SFelipe Balbi 	channel->status = MUSB_DMA_STATUS_UNKNOWN;
604550a7375SFelipe Balbi 
605550a7375SFelipe Balbi 	return NULL;
606550a7375SFelipe Balbi }
607550a7375SFelipe Balbi 
608550a7375SFelipe Balbi static void tusb_omap_dma_release(struct dma_channel *channel)
609550a7375SFelipe Balbi {
610550a7375SFelipe Balbi 	struct tusb_omap_dma_ch	*chdat = to_chdat(channel);
611550a7375SFelipe Balbi 	struct musb		*musb = chdat->musb;
612550a7375SFelipe Balbi 	void __iomem		*tbase = musb->ctrl_base;
613550a7375SFelipe Balbi 	u32			reg;
614550a7375SFelipe Balbi 
6155c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "ep%i ch%i\n", chdat->epnum, chdat->ch);
616550a7375SFelipe Balbi 
617550a7375SFelipe Balbi 	reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
618550a7375SFelipe Balbi 	if (chdat->tx)
619550a7375SFelipe Balbi 		reg |= (1 << chdat->epnum);
620550a7375SFelipe Balbi 	else
621550a7375SFelipe Balbi 		reg |= (1 << (chdat->epnum + 15));
622550a7375SFelipe Balbi 	musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
623550a7375SFelipe Balbi 
624550a7375SFelipe Balbi 	reg = musb_readl(tbase, TUSB_DMA_INT_CLEAR);
625550a7375SFelipe Balbi 	if (chdat->tx)
626550a7375SFelipe Balbi 		reg |= (1 << chdat->epnum);
627550a7375SFelipe Balbi 	else
628550a7375SFelipe Balbi 		reg |= (1 << (chdat->epnum + 15));
629550a7375SFelipe Balbi 	musb_writel(tbase, TUSB_DMA_INT_CLEAR, reg);
630550a7375SFelipe Balbi 
631550a7375SFelipe Balbi 	channel->status = MUSB_DMA_STATUS_UNKNOWN;
632550a7375SFelipe Balbi 
633550a7375SFelipe Balbi 	if (chdat->ch >= 0) {
634550a7375SFelipe Balbi 		omap_stop_dma(chdat->ch);
635550a7375SFelipe Balbi 		omap_free_dma(chdat->ch);
636550a7375SFelipe Balbi 		chdat->ch = -1;
637550a7375SFelipe Balbi 	}
638550a7375SFelipe Balbi 
639550a7375SFelipe Balbi 	if (chdat->dmareq >= 0)
640550a7375SFelipe Balbi 		tusb_omap_dma_free_dmareq(chdat);
641550a7375SFelipe Balbi 
642550a7375SFelipe Balbi 	channel = NULL;
643550a7375SFelipe Balbi }
644550a7375SFelipe Balbi 
645550a7375SFelipe Balbi void dma_controller_destroy(struct dma_controller *c)
646550a7375SFelipe Balbi {
647550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
648550a7375SFelipe Balbi 	int			i;
649550a7375SFelipe Balbi 
650550a7375SFelipe Balbi 	tusb_dma = container_of(c, struct tusb_omap_dma, controller);
651550a7375SFelipe Balbi 	for (i = 0; i < MAX_DMAREQ; i++) {
652550a7375SFelipe Balbi 		struct dma_channel *ch = dma_channel_pool[i];
653550a7375SFelipe Balbi 		if (ch) {
654550a7375SFelipe Balbi 			kfree(ch->private_data);
655550a7375SFelipe Balbi 			kfree(ch);
656550a7375SFelipe Balbi 		}
657550a7375SFelipe Balbi 	}
658550a7375SFelipe Balbi 
65994089d56SRoel Kluin 	if (tusb_dma && !tusb_dma->multichannel && tusb_dma->ch >= 0)
660550a7375SFelipe Balbi 		omap_free_dma(tusb_dma->ch);
661550a7375SFelipe Balbi 
662550a7375SFelipe Balbi 	kfree(tusb_dma);
663550a7375SFelipe Balbi }
664550a7375SFelipe Balbi 
665*07a67bbbSShubhrajyoti D struct dma_controller *__devinit
666550a7375SFelipe Balbi dma_controller_create(struct musb *musb, void __iomem *base)
667550a7375SFelipe Balbi {
668550a7375SFelipe Balbi 	void __iomem		*tbase = musb->ctrl_base;
669550a7375SFelipe Balbi 	struct tusb_omap_dma	*tusb_dma;
670550a7375SFelipe Balbi 	int			i;
671550a7375SFelipe Balbi 
672550a7375SFelipe Balbi 	/* REVISIT: Get dmareq lines used from board-*.c */
673550a7375SFelipe Balbi 
674550a7375SFelipe Balbi 	musb_writel(musb->ctrl_base, TUSB_DMA_INT_MASK, 0x7fffffff);
675550a7375SFelipe Balbi 	musb_writel(musb->ctrl_base, TUSB_DMA_EP_MAP, 0);
676550a7375SFelipe Balbi 
677550a7375SFelipe Balbi 	musb_writel(tbase, TUSB_DMA_REQ_CONF,
678550a7375SFelipe Balbi 		TUSB_DMA_REQ_CONF_BURST_SIZE(2)
679550a7375SFelipe Balbi 		| TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f)
680550a7375SFelipe Balbi 		| TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
681550a7375SFelipe Balbi 
682550a7375SFelipe Balbi 	tusb_dma = kzalloc(sizeof(struct tusb_omap_dma), GFP_KERNEL);
683550a7375SFelipe Balbi 	if (!tusb_dma)
684c88ba39cSHuzaifa Sidhpurwala 		goto out;
685550a7375SFelipe Balbi 
686550a7375SFelipe Balbi 	tusb_dma->musb = musb;
687550a7375SFelipe Balbi 	tusb_dma->tbase = musb->ctrl_base;
688550a7375SFelipe Balbi 
689550a7375SFelipe Balbi 	tusb_dma->ch = -1;
690550a7375SFelipe Balbi 	tusb_dma->dmareq = -1;
691550a7375SFelipe Balbi 	tusb_dma->sync_dev = -1;
692550a7375SFelipe Balbi 
693550a7375SFelipe Balbi 	tusb_dma->controller.start = tusb_omap_dma_start;
694550a7375SFelipe Balbi 	tusb_dma->controller.stop = tusb_omap_dma_stop;
695550a7375SFelipe Balbi 	tusb_dma->controller.channel_alloc = tusb_omap_dma_allocate;
696550a7375SFelipe Balbi 	tusb_dma->controller.channel_release = tusb_omap_dma_release;
697550a7375SFelipe Balbi 	tusb_dma->controller.channel_program = tusb_omap_dma_program;
698550a7375SFelipe Balbi 	tusb_dma->controller.channel_abort = tusb_omap_dma_abort;
699550a7375SFelipe Balbi 
700550a7375SFelipe Balbi 	if (tusb_get_revision(musb) >= TUSB_REV_30)
701550a7375SFelipe Balbi 		tusb_dma->multichannel = 1;
702550a7375SFelipe Balbi 
703550a7375SFelipe Balbi 	for (i = 0; i < MAX_DMAREQ; i++) {
704550a7375SFelipe Balbi 		struct dma_channel	*ch;
705550a7375SFelipe Balbi 		struct tusb_omap_dma_ch	*chdat;
706550a7375SFelipe Balbi 
707550a7375SFelipe Balbi 		ch = kzalloc(sizeof(struct dma_channel), GFP_KERNEL);
708550a7375SFelipe Balbi 		if (!ch)
709550a7375SFelipe Balbi 			goto cleanup;
710550a7375SFelipe Balbi 
711550a7375SFelipe Balbi 		dma_channel_pool[i] = ch;
712550a7375SFelipe Balbi 
713550a7375SFelipe Balbi 		chdat = kzalloc(sizeof(struct tusb_omap_dma_ch), GFP_KERNEL);
714550a7375SFelipe Balbi 		if (!chdat)
715550a7375SFelipe Balbi 			goto cleanup;
716550a7375SFelipe Balbi 
717550a7375SFelipe Balbi 		ch->status = MUSB_DMA_STATUS_UNKNOWN;
718550a7375SFelipe Balbi 		ch->private_data = chdat;
719550a7375SFelipe Balbi 	}
720550a7375SFelipe Balbi 
721550a7375SFelipe Balbi 	return &tusb_dma->controller;
722550a7375SFelipe Balbi 
723550a7375SFelipe Balbi cleanup:
724550a7375SFelipe Balbi 	dma_controller_destroy(&tusb_dma->controller);
725c88ba39cSHuzaifa Sidhpurwala out:
726550a7375SFelipe Balbi 	return NULL;
727550a7375SFelipe Balbi }
728