blob: f8671110168bb16a4041f446dbcbb86d00bc87be [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
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>
Felipe Balbi550a7372008-07-24 12:27:36 +030014#include <linux/usb.h>
15#include <linux/platform_device.h>
16#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Tony Lindgren45c3eb72012-11-30 08:41:50 -080018#include <linux/omap-dma.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030019
20#include "musb_core.h"
Felipe Balbi240a16e2011-08-05 13:29:49 +030021#include "tusb6010.h"
Felipe Balbi550a7372008-07-24 12:27:36 +030022
23#define to_chdat(c) ((struct tusb_omap_dma_ch *)(c)->private_data)
24
25#define MAX_DMAREQ 5 /* REVISIT: Really 6, but req5 not OK */
26
Lokesh Vutlad5e7c862012-10-15 14:03:51 -070027#define OMAP24XX_DMA_EXT_DMAREQ0 2
28#define OMAP24XX_DMA_EXT_DMAREQ1 3
29#define OMAP242X_DMA_EXT_DMAREQ2 14
30#define OMAP242X_DMA_EXT_DMAREQ3 15
31#define OMAP242X_DMA_EXT_DMAREQ4 16
32#define OMAP242X_DMA_EXT_DMAREQ5 64
33
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -050034struct tusb_dma_data {
35 int ch;
36 s8 dmareq;
37 s8 sync_dev;
38};
39
Felipe Balbi550a7372008-07-24 12:27:36 +030040struct tusb_omap_dma_ch {
41 struct musb *musb;
42 void __iomem *tbase;
43 unsigned long phys_offset;
44 int epnum;
45 u8 tx;
46 struct musb_hw_ep *hw_ep;
47
Peter Ujfalusi4cadc712017-06-16 10:41:00 -050048 struct tusb_dma_data *dma_data;
Felipe Balbi550a7372008-07-24 12:27:36 +030049
50 struct tusb_omap_dma *tusb_dma;
51
Tony Lindgren1d0f11b2010-04-23 17:41:15 -070052 dma_addr_t dma_addr;
Felipe Balbi550a7372008-07-24 12:27:36 +030053
54 u32 len;
55 u16 packet_sz;
56 u16 transfer_packet_sz;
57 u32 transfer_len;
58 u32 completed_len;
59};
60
61struct tusb_omap_dma {
62 struct dma_controller controller;
Felipe Balbi550a7372008-07-24 12:27:36 +030063 void __iomem *tbase;
64
Peter Ujfalusi4cadc712017-06-16 10:41:00 -050065 struct tusb_dma_data dma_pool[MAX_DMAREQ];
Felipe Balbi550a7372008-07-24 12:27:36 +030066 unsigned multichannel:1;
67};
68
Felipe Balbi550a7372008-07-24 12:27:36 +030069/*
70 * Allocate dmareq0 to the current channel unless it's already taken
71 */
72static inline int tusb_omap_use_shared_dmareq(struct tusb_omap_dma_ch *chdat)
73{
74 u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
75
76 if (reg != 0) {
Sergei Trofimovich74c6f3a2011-07-17 18:28:00 +030077 dev_dbg(chdat->musb->controller, "ep%i dmareq0 is busy for ep%i\n",
Felipe Balbi550a7372008-07-24 12:27:36 +030078 chdat->epnum, reg & 0xf);
79 return -EAGAIN;
80 }
81
82 if (chdat->tx)
83 reg = (1 << 4) | chdat->epnum;
84 else
85 reg = chdat->epnum;
86
87 musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
88
89 return 0;
90}
91
92static inline void tusb_omap_free_shared_dmareq(struct tusb_omap_dma_ch *chdat)
93{
94 u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
95
96 if ((reg & 0xf) != chdat->epnum) {
97 printk(KERN_ERR "ep%i trying to release dmareq0 for ep%i\n",
98 chdat->epnum, reg & 0xf);
99 return;
100 }
101 musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, 0);
102}
103
104/*
105 * See also musb_dma_completion in plat_uds.c and musb_g_[tx|rx]() in
106 * musb_gadget.c.
107 */
108static void tusb_omap_dma_cb(int lch, u16 ch_status, void *data)
109{
110 struct dma_channel *channel = (struct dma_channel *)data;
111 struct tusb_omap_dma_ch *chdat = to_chdat(channel);
112 struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
113 struct musb *musb = chdat->musb;
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700114 struct device *dev = musb->controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300115 struct musb_hw_ep *hw_ep = chdat->hw_ep;
116 void __iomem *ep_conf = hw_ep->conf;
117 void __iomem *mbase = musb->mregs;
118 unsigned long remaining, flags, pio;
119 int ch;
120
121 spin_lock_irqsave(&musb->lock, flags);
122
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500123 ch = chdat->dma_data->ch;
Felipe Balbi550a7372008-07-24 12:27:36 +0300124
125 if (ch_status != OMAP_DMA_BLOCK_IRQ)
126 printk(KERN_ERR "TUSB DMA error status: %i\n", ch_status);
127
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300128 dev_dbg(musb->controller, "ep%i %s dma callback ch: %i status: %x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300129 chdat->epnum, chdat->tx ? "tx" : "rx",
130 ch, ch_status);
131
132 if (chdat->tx)
133 remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
134 else
135 remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
136
137 remaining = TUSB_EP_CONFIG_XFR_SIZE(remaining);
138
139 /* HW issue #10: XFR_SIZE may get corrupt on DMA (both async & sync) */
140 if (unlikely(remaining > chdat->transfer_len)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300141 dev_dbg(musb->controller, "Corrupt %s dma ch%i XFR_SIZE: 0x%08lx\n",
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500142 chdat->tx ? "tx" : "rx", ch, remaining);
Felipe Balbi550a7372008-07-24 12:27:36 +0300143 remaining = 0;
144 }
145
146 channel->actual_len = chdat->transfer_len - remaining;
147 pio = chdat->len - channel->actual_len;
148
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300149 dev_dbg(musb->controller, "DMA remaining %lu/%u\n", remaining, chdat->transfer_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300150
151 /* Transfer remaining 1 - 31 bytes */
152 if (pio > 0 && pio < 32) {
153 u8 *buf;
154
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300155 dev_dbg(musb->controller, "Using PIO for remaining %lu bytes\n", pio);
Felipe Balbi550a7372008-07-24 12:27:36 +0300156 buf = phys_to_virt((u32)chdat->dma_addr) + chdat->transfer_len;
157 if (chdat->tx) {
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700158 dma_unmap_single(dev, chdat->dma_addr,
159 chdat->transfer_len,
160 DMA_TO_DEVICE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300161 musb_write_fifo(hw_ep, pio, buf);
162 } else {
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700163 dma_unmap_single(dev, chdat->dma_addr,
164 chdat->transfer_len,
165 DMA_FROM_DEVICE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300166 musb_read_fifo(hw_ep, pio, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300167 }
168 channel->actual_len += pio;
169 }
170
171 if (!tusb_dma->multichannel)
172 tusb_omap_free_shared_dmareq(chdat);
173
174 channel->status = MUSB_DMA_STATUS_FREE;
175
176 /* Handle only RX callbacks here. TX callbacks must be handled based
177 * on the TUSB DMA status interrupt.
178 * REVISIT: Use both TUSB DMA status interrupt and OMAP DMA callback
179 * interrupt for RX and TX.
180 */
181 if (!chdat->tx)
182 musb_dma_completion(musb, chdat->epnum, chdat->tx);
183
184 /* We must terminate short tx transfers manually by setting TXPKTRDY.
185 * REVISIT: This same problem may occur with other MUSB dma as well.
186 * Easy to test with g_ether by pinging the MUSB board with ping -s54.
187 */
188 if ((chdat->transfer_len < chdat->packet_sz)
189 || (chdat->transfer_len % chdat->packet_sz != 0)) {
190 u16 csr;
191
192 if (chdat->tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300193 dev_dbg(musb->controller, "terminating short tx packet\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300194 musb_ep_select(mbase, chdat->epnum);
195 csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
196 csr |= MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY
197 | MUSB_TXCSR_P_WZC_BITS;
198 musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
199 }
200 }
201
202 spin_unlock_irqrestore(&musb->lock, flags);
203}
204
205static int tusb_omap_dma_program(struct dma_channel *channel, u16 packet_sz,
206 u8 rndis_mode, dma_addr_t dma_addr, u32 len)
207{
208 struct tusb_omap_dma_ch *chdat = to_chdat(channel);
209 struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
210 struct musb *musb = chdat->musb;
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700211 struct device *dev = musb->controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300212 struct musb_hw_ep *hw_ep = chdat->hw_ep;
213 void __iomem *mbase = musb->mregs;
214 void __iomem *ep_conf = hw_ep->conf;
215 dma_addr_t fifo = hw_ep->fifo_sync;
216 struct omap_dma_channel_params dma_params;
217 u32 dma_remaining;
218 int src_burst, dst_burst;
219 u16 csr;
Peter Ujfalusi6df2b422017-05-17 11:23:11 -0500220 u32 psize;
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500221 struct tusb_dma_data *dma_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300222
223 if (unlikely(dma_addr & 0x1) || (len < 32) || (len > packet_sz))
224 return false;
225
226 /*
227 * HW issue #10: Async dma will eventually corrupt the XFR_SIZE
228 * register which will cause missed DMA interrupt. We could try to
229 * use a timer for the callback, but it is unsafe as the XFR_SIZE
230 * register is corrupt, and we won't know if the DMA worked.
231 */
232 if (dma_addr & 0x2)
233 return false;
234
235 /*
236 * Because of HW issue #10, it seems like mixing sync DMA and async
237 * PIO access can confuse the DMA. Make sure XFR_SIZE is reset before
238 * using the channel for DMA.
239 */
240 if (chdat->tx)
241 dma_remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
242 else
243 dma_remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
244
245 dma_remaining = TUSB_EP_CONFIG_XFR_SIZE(dma_remaining);
246 if (dma_remaining) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300247 dev_dbg(musb->controller, "Busy %s dma ch%i, not using: %08x\n",
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500248 chdat->tx ? "tx" : "rx",
249 chdat->dma_data ? chdat->dma_data->ch : -1,
Felipe Balbi550a7372008-07-24 12:27:36 +0300250 dma_remaining);
251 return false;
252 }
253
254 chdat->transfer_len = len & ~0x1f;
255
256 if (len < packet_sz)
257 chdat->transfer_packet_sz = chdat->transfer_len;
258 else
259 chdat->transfer_packet_sz = packet_sz;
260
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500261 dma_data = chdat->dma_data;
262 if (!tusb_dma->multichannel) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300263 if (tusb_omap_use_shared_dmareq(chdat) != 0) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300264 dev_dbg(musb->controller, "could not get dma for ep%i\n", chdat->epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300265 return false;
266 }
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500267 if (dma_data->ch < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300268 /* REVISIT: This should get blocked earlier, happens
269 * with MSC ErrorRecoveryTest
270 */
271 WARN_ON(1);
272 return false;
273 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300274 }
275
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500276 omap_set_dma_callback(dma_data->ch, tusb_omap_dma_cb, channel);
277
Felipe Balbi550a7372008-07-24 12:27:36 +0300278 chdat->packet_sz = packet_sz;
279 chdat->len = len;
280 channel->actual_len = 0;
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700281 chdat->dma_addr = dma_addr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300282 channel->status = MUSB_DMA_STATUS_BUSY;
283
284 /* Since we're recycling dma areas, we need to clean or invalidate */
285 if (chdat->tx)
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700286 dma_map_single(dev, phys_to_virt(dma_addr), len,
287 DMA_TO_DEVICE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300288 else
Tony Lindgren1d0f11b2010-04-23 17:41:15 -0700289 dma_map_single(dev, phys_to_virt(dma_addr), len,
290 DMA_FROM_DEVICE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300291
292 /* Use 16-bit transfer if dma_addr is not 32-bit aligned */
293 if ((dma_addr & 0x3) == 0) {
294 dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
295 dma_params.elem_count = 8; /* Elements in frame */
296 } else {
297 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
298 dma_params.elem_count = 16; /* Elements in frame */
299 fifo = hw_ep->fifo_async;
300 }
301
302 dma_params.frame_count = chdat->transfer_len / 32; /* Burst sz frame */
303
Arnd Bergmann3ec08dd2016-01-28 17:23:14 +0100304 dev_dbg(musb->controller, "ep%i %s dma ch%i dma: %pad len: %u(%u) packet_sz: %i(%i)\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300305 chdat->epnum, chdat->tx ? "tx" : "rx",
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500306 dma_data->ch, &dma_addr, chdat->transfer_len, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300307 chdat->transfer_packet_sz, packet_sz);
308
309 /*
310 * Prepare omap DMA for transfer
311 */
312 if (chdat->tx) {
313 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
314 dma_params.src_start = (unsigned long)dma_addr;
315 dma_params.src_ei = 0;
316 dma_params.src_fi = 0;
317
318 dma_params.dst_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
319 dma_params.dst_start = (unsigned long)fifo;
320 dma_params.dst_ei = 1;
321 dma_params.dst_fi = -31; /* Loop 32 byte window */
322
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500323 dma_params.trigger = dma_data->sync_dev;
Felipe Balbi550a7372008-07-24 12:27:36 +0300324 dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
325 dma_params.src_or_dst_synch = 0; /* Dest sync */
326
327 src_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 read */
328 dst_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 write */
329 } else {
330 dma_params.src_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
331 dma_params.src_start = (unsigned long)fifo;
332 dma_params.src_ei = 1;
333 dma_params.src_fi = -31; /* Loop 32 byte window */
334
335 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
336 dma_params.dst_start = (unsigned long)dma_addr;
337 dma_params.dst_ei = 0;
338 dma_params.dst_fi = 0;
339
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500340 dma_params.trigger = dma_data->sync_dev;
Felipe Balbi550a7372008-07-24 12:27:36 +0300341 dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
342 dma_params.src_or_dst_synch = 1; /* Source sync */
343
344 src_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 read */
345 dst_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 write */
346 }
347
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300348 dev_dbg(musb->controller, "ep%i %s using %i-bit %s dma from 0x%08lx to 0x%08lx\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300349 chdat->epnum, chdat->tx ? "tx" : "rx",
350 (dma_params.data_type == OMAP_DMA_DATA_TYPE_S32) ? 32 : 16,
351 ((dma_addr & 0x3) == 0) ? "sync" : "async",
352 dma_params.src_start, dma_params.dst_start);
353
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500354 omap_set_dma_params(dma_data->ch, &dma_params);
355 omap_set_dma_src_burst_mode(dma_data->ch, src_burst);
356 omap_set_dma_dest_burst_mode(dma_data->ch, dst_burst);
357 omap_set_dma_write_mode(dma_data->ch, OMAP_DMA_WRITE_LAST_NON_POSTED);
Felipe Balbi550a7372008-07-24 12:27:36 +0300358
359 /*
360 * Prepare MUSB for DMA transfer
361 */
Peter Ujfalusi3565b782017-06-16 10:40:58 -0500362 musb_ep_select(mbase, chdat->epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300363 if (chdat->tx) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
365 csr |= (MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB
366 | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_MODE);
367 csr &= ~MUSB_TXCSR_P_UNDERRUN;
368 musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
369 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300370 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
371 csr |= MUSB_RXCSR_DMAENAB;
372 csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAMODE);
373 musb_writew(hw_ep->regs, MUSB_RXCSR,
374 csr | MUSB_RXCSR_P_WZC_BITS);
375 }
376
377 /*
378 * Start DMA transfer
379 */
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500380 omap_start_dma(dma_data->ch);
Felipe Balbi550a7372008-07-24 12:27:36 +0300381
382 if (chdat->tx) {
383 /* Send transfer_packet_sz packets at a time */
Peter Ujfalusi6df2b422017-05-17 11:23:11 -0500384 psize = musb_readl(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET);
385 psize &= ~0x7ff;
386 psize |= chdat->transfer_packet_sz;
387 musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET, psize);
Felipe Balbi550a7372008-07-24 12:27:36 +0300388
389 musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
390 TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
391 } else {
392 /* Receive transfer_packet_sz packets at a time */
Peter Ujfalusi6df2b422017-05-17 11:23:11 -0500393 psize = musb_readl(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET);
394 psize &= ~(0x7ff << 16);
395 psize |= (chdat->transfer_packet_sz << 16);
396 musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET, psize);
Felipe Balbi550a7372008-07-24 12:27:36 +0300397
398 musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
399 TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
400 }
401
402 return true;
403}
404
405static int tusb_omap_dma_abort(struct dma_channel *channel)
406{
407 struct tusb_omap_dma_ch *chdat = to_chdat(channel);
Felipe Balbi550a7372008-07-24 12:27:36 +0300408
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500409 if (chdat->dma_data)
410 omap_stop_dma(chdat->dma_data->ch);
Felipe Balbi550a7372008-07-24 12:27:36 +0300411
412 channel->status = MUSB_DMA_STATUS_FREE;
413
414 return 0;
415}
416
417static inline int tusb_omap_dma_allocate_dmareq(struct tusb_omap_dma_ch *chdat)
418{
419 u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
420 int i, dmareq_nr = -1;
421
Felipe Balbi550a7372008-07-24 12:27:36 +0300422 for (i = 0; i < MAX_DMAREQ; i++) {
423 int cur = (reg & (0xf << (i * 5))) >> (i * 5);
424 if (cur == 0) {
425 dmareq_nr = i;
426 break;
427 }
428 }
429
430 if (dmareq_nr == -1)
431 return -EAGAIN;
432
433 reg |= (chdat->epnum << (dmareq_nr * 5));
434 if (chdat->tx)
435 reg |= ((1 << 4) << (dmareq_nr * 5));
436 musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
437
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500438 chdat->dma_data = &chdat->tusb_dma->dma_pool[dmareq_nr];
Felipe Balbi550a7372008-07-24 12:27:36 +0300439
440 return 0;
441}
442
443static inline void tusb_omap_dma_free_dmareq(struct tusb_omap_dma_ch *chdat)
444{
445 u32 reg;
446
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500447 if (!chdat || !chdat->dma_data || chdat->dma_data->dmareq < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +0300448 return;
449
450 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500451 reg &= ~(0x1f << (chdat->dma_data->dmareq * 5));
Felipe Balbi550a7372008-07-24 12:27:36 +0300452 musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
453
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500454 chdat->dma_data = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300455}
456
457static struct dma_channel *dma_channel_pool[MAX_DMAREQ];
458
459static struct dma_channel *
460tusb_omap_dma_allocate(struct dma_controller *c,
461 struct musb_hw_ep *hw_ep,
462 u8 tx)
463{
464 int ret, i;
Felipe Balbi550a7372008-07-24 12:27:36 +0300465 struct tusb_omap_dma *tusb_dma;
466 struct musb *musb;
467 void __iomem *tbase;
468 struct dma_channel *channel = NULL;
469 struct tusb_omap_dma_ch *chdat = NULL;
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500470 struct tusb_dma_data *dma_data = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300471 u32 reg;
472
473 tusb_dma = container_of(c, struct tusb_omap_dma, controller);
Alexandre Bailona96ca0d2017-02-06 22:53:55 -0600474 musb = tusb_dma->controller.musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300475 tbase = musb->ctrl_base;
476
477 reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
478 if (tx)
479 reg &= ~(1 << hw_ep->epnum);
480 else
481 reg &= ~(1 << (hw_ep->epnum + 15));
482 musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
483
484 /* REVISIT: Why does dmareq5 not work? */
485 if (hw_ep->epnum == 0) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300486 dev_dbg(musb->controller, "Not allowing DMA for ep0 %s\n", tx ? "tx" : "rx");
Felipe Balbi550a7372008-07-24 12:27:36 +0300487 return NULL;
488 }
489
490 for (i = 0; i < MAX_DMAREQ; i++) {
491 struct dma_channel *ch = dma_channel_pool[i];
492 if (ch->status == MUSB_DMA_STATUS_UNKNOWN) {
493 ch->status = MUSB_DMA_STATUS_FREE;
494 channel = ch;
495 chdat = ch->private_data;
496 break;
497 }
498 }
499
500 if (!channel)
501 return NULL;
502
Alexandre Bailona96ca0d2017-02-06 22:53:55 -0600503 chdat->musb = tusb_dma->controller.musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300504 chdat->tbase = tusb_dma->tbase;
505 chdat->hw_ep = hw_ep;
506 chdat->epnum = hw_ep->epnum;
Felipe Balbi550a7372008-07-24 12:27:36 +0300507 chdat->completed_len = 0;
508 chdat->tusb_dma = tusb_dma;
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500509 if (tx)
510 chdat->tx = 1;
511 else
512 chdat->tx = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300513
514 channel->max_len = 0x7fffffff;
515 channel->desired_mode = 0;
516 channel->actual_len = 0;
517
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500518 if (!chdat->dma_data) {
519 if (tusb_dma->multichannel) {
520 ret = tusb_omap_dma_allocate_dmareq(chdat);
521 if (ret != 0)
522 goto free_dmareq;
523 } else {
524 chdat->dma_data = &tusb_dma->dma_pool[0];
525 }
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500526 }
527
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500528 dma_data = chdat->dma_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300529
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300530 dev_dbg(musb->controller, "ep%i %s dma: %s dma%i dmareq%i sync%i\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300531 chdat->epnum,
532 chdat->tx ? "tx" : "rx",
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500533 tusb_dma->multichannel ? "shared" : "dedicated",
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500534 dma_data->ch, dma_data->dmareq, dma_data->sync_dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300535
536 return channel;
537
538free_dmareq:
539 tusb_omap_dma_free_dmareq(chdat);
540
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300541 dev_dbg(musb->controller, "ep%i: Could not get a DMA channel\n", chdat->epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300542 channel->status = MUSB_DMA_STATUS_UNKNOWN;
543
544 return NULL;
545}
546
547static void tusb_omap_dma_release(struct dma_channel *channel)
548{
549 struct tusb_omap_dma_ch *chdat = to_chdat(channel);
550 struct musb *musb = chdat->musb;
551 void __iomem *tbase = musb->ctrl_base;
552 u32 reg;
553
Peter Ujfalusi1df9d9e2017-06-16 10:40:59 -0500554 dev_dbg(musb->controller, "ep%i ch%i\n", chdat->epnum,
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500555 chdat->dma_data->ch);
Felipe Balbi550a7372008-07-24 12:27:36 +0300556
557 reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
558 if (chdat->tx)
559 reg |= (1 << chdat->epnum);
560 else
561 reg |= (1 << (chdat->epnum + 15));
562 musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
563
564 reg = musb_readl(tbase, TUSB_DMA_INT_CLEAR);
565 if (chdat->tx)
566 reg |= (1 << chdat->epnum);
567 else
568 reg |= (1 << (chdat->epnum + 15));
569 musb_writel(tbase, TUSB_DMA_INT_CLEAR, reg);
570
571 channel->status = MUSB_DMA_STATUS_UNKNOWN;
572
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500573 omap_stop_dma(chdat->dma_data->ch);
574 tusb_omap_dma_free_dmareq(chdat);
Felipe Balbi550a7372008-07-24 12:27:36 +0300575
576 channel = NULL;
577}
578
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700579void tusb_dma_controller_destroy(struct dma_controller *c)
Felipe Balbi550a7372008-07-24 12:27:36 +0300580{
581 struct tusb_omap_dma *tusb_dma;
582 int i;
583
584 tusb_dma = container_of(c, struct tusb_omap_dma, controller);
585 for (i = 0; i < MAX_DMAREQ; i++) {
586 struct dma_channel *ch = dma_channel_pool[i];
587 if (ch) {
588 kfree(ch->private_data);
589 kfree(ch);
590 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300591
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500592 /* Free up the DMA channels */
593 if (tusb_dma && tusb_dma->dma_pool[i].ch >= 0)
594 omap_free_dma(tusb_dma->dma_pool[i].ch);
595 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300596
597 kfree(tusb_dma);
598}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700599EXPORT_SYMBOL_GPL(tusb_dma_controller_destroy);
Felipe Balbi550a7372008-07-24 12:27:36 +0300600
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500601static int tusb_omap_allocate_dma_pool(struct tusb_omap_dma *tusb_dma)
602{
603 int i;
604 int ret = 0;
605 const int sync_dev[6] = {
606 OMAP24XX_DMA_EXT_DMAREQ0,
607 OMAP24XX_DMA_EXT_DMAREQ1,
608 OMAP242X_DMA_EXT_DMAREQ2,
609 OMAP242X_DMA_EXT_DMAREQ3,
610 OMAP242X_DMA_EXT_DMAREQ4,
611 OMAP242X_DMA_EXT_DMAREQ5,
612 };
613
614 for (i = 0; i < MAX_DMAREQ; i++) {
615 struct tusb_dma_data *dma_data = &tusb_dma->dma_pool[i];
616
617 /*
618 * Request DMA channels:
619 * - one channel in case of non multichannel mode
620 * - MAX_DMAREQ number of channels in multichannel mode
621 */
622 if (i == 0 || tusb_dma->multichannel) {
623 char ch_name[8];
624
625 sprintf(ch_name, "dmareq%d", i);
626 dma_data->sync_dev = sync_dev[i];
627 dma_data->ch = -1;
628 /* callback data is ngoing to be set later */
629 ret = omap_request_dma(dma_data->sync_dev, ch_name,
630 tusb_omap_dma_cb, NULL, &dma_data->ch);
631 if (ret != 0) {
632 dev_err(tusb_dma->controller.musb->controller,
633 "Failed to request %s\n", ch_name);
634 goto dma_error;
635 }
636
637 dma_data->dmareq = i;
638 } else {
639 dma_data->dmareq = -1;
640 dma_data->sync_dev = -1;
641 dma_data->ch = -1;
642 }
643 }
644
645 return 0;
646
647dma_error:
648 for (; i >= 0; i--) {
649 struct tusb_dma_data *dma_data = &tusb_dma->dma_pool[i];
650
651 if (dma_data->ch >= 0)
652 omap_free_dma(dma_data->ch);
653 }
654
655 return ret;
656}
657
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700658struct dma_controller *
659tusb_dma_controller_create(struct musb *musb, void __iomem *base)
Felipe Balbi550a7372008-07-24 12:27:36 +0300660{
661 void __iomem *tbase = musb->ctrl_base;
662 struct tusb_omap_dma *tusb_dma;
663 int i;
664
665 /* REVISIT: Get dmareq lines used from board-*.c */
666
667 musb_writel(musb->ctrl_base, TUSB_DMA_INT_MASK, 0x7fffffff);
668 musb_writel(musb->ctrl_base, TUSB_DMA_EP_MAP, 0);
669
670 musb_writel(tbase, TUSB_DMA_REQ_CONF,
671 TUSB_DMA_REQ_CONF_BURST_SIZE(2)
672 | TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f)
673 | TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
674
675 tusb_dma = kzalloc(sizeof(struct tusb_omap_dma), GFP_KERNEL);
676 if (!tusb_dma)
Huzaifa Sidhpurwalac88ba392011-03-01 15:54:22 +0530677 goto out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300678
Alexandre Bailona96ca0d2017-02-06 22:53:55 -0600679 tusb_dma->controller.musb = musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300680 tusb_dma->tbase = musb->ctrl_base;
681
Felipe Balbi550a7372008-07-24 12:27:36 +0300682 tusb_dma->controller.channel_alloc = tusb_omap_dma_allocate;
683 tusb_dma->controller.channel_release = tusb_omap_dma_release;
684 tusb_dma->controller.channel_program = tusb_omap_dma_program;
685 tusb_dma->controller.channel_abort = tusb_omap_dma_abort;
686
Matwey V. Kornilov7751b6f2014-05-16 18:20:52 +0400687 if (musb->tusb_revision >= TUSB_REV_30)
Felipe Balbi550a7372008-07-24 12:27:36 +0300688 tusb_dma->multichannel = 1;
689
690 for (i = 0; i < MAX_DMAREQ; i++) {
691 struct dma_channel *ch;
692 struct tusb_omap_dma_ch *chdat;
693
694 ch = kzalloc(sizeof(struct dma_channel), GFP_KERNEL);
695 if (!ch)
696 goto cleanup;
697
698 dma_channel_pool[i] = ch;
699
700 chdat = kzalloc(sizeof(struct tusb_omap_dma_ch), GFP_KERNEL);
701 if (!chdat)
702 goto cleanup;
703
704 ch->status = MUSB_DMA_STATUS_UNKNOWN;
705 ch->private_data = chdat;
706 }
707
Peter Ujfalusi4cadc712017-06-16 10:41:00 -0500708 if (tusb_omap_allocate_dma_pool(tusb_dma))
709 goto cleanup;
710
Felipe Balbi550a7372008-07-24 12:27:36 +0300711 return &tusb_dma->controller;
712
713cleanup:
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700714 musb_dma_controller_destroy(&tusb_dma->controller);
Huzaifa Sidhpurwalac88ba392011-03-01 15:54:22 +0530715out:
Felipe Balbi550a7372008-07-24 12:27:36 +0300716 return NULL;
717}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700718EXPORT_SYMBOL_GPL(tusb_dma_controller_create);