blob: 73ac9835485d617456dd98d6c4019ac803e71fef [file] [log] [blame]
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02001#include <linux/device.h>
2#include <linux/dma-mapping.h>
3#include <linux/dmaengine.h>
4#include <linux/sizes.h>
5#include <linux/platform_device.h>
6#include <linux/of.h>
7
8#include "musb_core.h"
9
10#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
11
Bin Liu0149b072015-01-26 16:22:06 -060012#define EP_MODE_AUTOREQ_NONE 0
13#define EP_MODE_AUTOREQ_ALL_NEOP 1
14#define EP_MODE_AUTOREQ_ALWAYS 3
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020015
16#define EP_MODE_DMA_TRANSPARENT 0
17#define EP_MODE_DMA_RNDIS 1
18#define EP_MODE_DMA_GEN_RNDIS 3
19
20#define USB_CTRL_TX_MODE 0x70
21#define USB_CTRL_RX_MODE 0x74
22#define USB_CTRL_AUTOREQ 0xd0
23#define USB_TDOWN 0xd8
24
25struct cppi41_dma_channel {
26 struct dma_channel channel;
27 struct cppi41_dma_controller *controller;
28 struct musb_hw_ep *hw_ep;
29 struct dma_chan *dc;
30 dma_cookie_t cookie;
31 u8 port_num;
32 u8 is_tx;
33 u8 is_allocated;
34 u8 usb_toggle;
35
36 dma_addr_t buf_addr;
37 u32 total_len;
38 u32 prog_len;
39 u32 transferred;
40 u32 packet_sz;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010041 struct list_head tx_check;
Bin Liu9267eda2014-08-12 14:18:43 -050042 int tx_zlp;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020043};
44
45#define MUSB_DMA_NUM_CHANNELS 15
46
47struct cppi41_dma_controller {
48 struct dma_controller controller;
49 struct cppi41_dma_channel rx_channel[MUSB_DMA_NUM_CHANNELS];
50 struct cppi41_dma_channel tx_channel[MUSB_DMA_NUM_CHANNELS];
51 struct musb *musb;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010052 struct hrtimer early_tx;
53 struct list_head early_tx_list;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020054 u32 rx_mode;
55 u32 tx_mode;
56 u32 auto_req;
57};
58
59static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
60{
61 u16 csr;
62 u8 toggle;
63
64 if (cppi41_channel->is_tx)
65 return;
66 if (!is_host_active(cppi41_channel->controller->musb))
67 return;
68
69 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
70 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
71
72 cppi41_channel->usb_toggle = toggle;
73}
74
75static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
76{
Daniel Mackf50e6782014-05-26 14:52:39 +020077 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
78 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020079 u16 csr;
80 u8 toggle;
81
82 if (cppi41_channel->is_tx)
83 return;
Daniel Mackf50e6782014-05-26 14:52:39 +020084 if (!is_host_active(musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020085 return;
86
Daniel Mackf50e6782014-05-26 14:52:39 +020087 musb_ep_select(musb->mregs, hw_ep->epnum);
88 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020089 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
90
91 /*
92 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
93 * data toggle may reset from DATA1 to DATA0 during receiving data from
94 * more than one endpoint.
95 */
96 if (!toggle && toggle == cppi41_channel->usb_toggle) {
97 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
98 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
99 dev_dbg(cppi41_channel->controller->musb->controller,
100 "Restoring DATA1 toggle.\n");
101 }
102
103 cppi41_channel->usb_toggle = toggle;
104}
105
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100106static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
107{
108 u8 epnum = hw_ep->epnum;
109 struct musb *musb = hw_ep->musb;
110 void __iomem *epio = musb->endpoints[epnum].regs;
111 u16 csr;
112
Daniel Mackf50e6782014-05-26 14:52:39 +0200113 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100114 csr = musb_readw(epio, MUSB_TXCSR);
115 if (csr & MUSB_TXCSR_TXPKTRDY)
116 return false;
117 return true;
118}
119
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100120static void cppi41_dma_callback(void *private_data);
121
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100122static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200123{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200124 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
125 struct musb *musb = hw_ep->musb;
Bin Liu9267eda2014-08-12 14:18:43 -0500126 void __iomem *epio = hw_ep->regs;
127 u16 csr;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200128
George Cherianaecbc312014-02-27 10:44:41 +0530129 if (!cppi41_channel->prog_len ||
130 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200131
132 /* done, complete */
133 cppi41_channel->channel.actual_len =
134 cppi41_channel->transferred;
135 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200136 cppi41_channel->channel.rx_packet_done = true;
Bin Liu9267eda2014-08-12 14:18:43 -0500137
138 /*
139 * transmit ZLP using PIO mode for transfers which size is
140 * multiple of EP packet size.
141 */
142 if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
143 cppi41_channel->packet_sz) == 0) {
144 musb_ep_select(musb->mregs, hw_ep->epnum);
145 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
146 musb_writew(epio, MUSB_TXCSR, csr);
147 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200148 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
149 } else {
150 /* next iteration, reload */
151 struct dma_chan *dc = cppi41_channel->dc;
152 struct dma_async_tx_descriptor *dma_desc;
153 enum dma_transfer_direction direction;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200154 u32 remain_bytes;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200155
156 cppi41_channel->buf_addr += cppi41_channel->packet_sz;
157
158 remain_bytes = cppi41_channel->total_len;
159 remain_bytes -= cppi41_channel->transferred;
160 remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
161 cppi41_channel->prog_len = remain_bytes;
162
163 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
164 : DMA_DEV_TO_MEM;
165 dma_desc = dmaengine_prep_slave_single(dc,
166 cppi41_channel->buf_addr,
167 remain_bytes,
168 direction,
169 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100170 if (WARN_ON(!dma_desc))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200171 return;
172
173 dma_desc->callback = cppi41_dma_callback;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100174 dma_desc->callback_param = &cppi41_channel->channel;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200175 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
176 dma_async_issue_pending(dc);
177
178 if (!cppi41_channel->is_tx) {
Daniel Mackf50e6782014-05-26 14:52:39 +0200179 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200180 csr = musb_readw(epio, MUSB_RXCSR);
181 csr |= MUSB_RXCSR_H_REQPKT;
182 musb_writew(epio, MUSB_RXCSR, csr);
183 }
184 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100185}
186
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100187static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
188{
189 struct cppi41_dma_controller *controller;
190 struct cppi41_dma_channel *cppi41_channel, *n;
191 struct musb *musb;
192 unsigned long flags;
193 enum hrtimer_restart ret = HRTIMER_NORESTART;
194
195 controller = container_of(timer, struct cppi41_dma_controller,
196 early_tx);
197 musb = controller->musb;
198
199 spin_lock_irqsave(&musb->lock, flags);
200 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
201 tx_check) {
202 bool empty;
203 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
204
205 empty = musb_is_tx_fifo_empty(hw_ep);
206 if (empty) {
207 list_del_init(&cppi41_channel->tx_check);
208 cppi41_trans_done(cppi41_channel);
209 }
210 }
211
Thomas Gleixnerd2e6d622014-10-02 17:32:16 +0200212 if (!list_empty(&controller->early_tx_list) &&
213 !hrtimer_is_queued(&controller->early_tx)) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100214 ret = HRTIMER_RESTART;
215 hrtimer_forward_now(&controller->early_tx,
Daniel Macka5e4aa42014-09-03 17:21:24 +0200216 ktime_set(0, 20 * NSEC_PER_USEC));
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100217 }
218
219 spin_unlock_irqrestore(&musb->lock, flags);
220 return ret;
221}
222
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100223static void cppi41_dma_callback(void *private_data)
224{
225 struct dma_channel *channel = private_data;
226 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
227 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
Felipe Balbi1b616252015-02-27 13:19:39 -0600228 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100229 struct musb *musb = hw_ep->musb;
230 unsigned long flags;
231 struct dma_tx_state txstate;
232 u32 transferred;
Felipe Balbi1b616252015-02-27 13:19:39 -0600233 int is_hs = 0;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100234 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100235
236 spin_lock_irqsave(&musb->lock, flags);
237
238 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
239 &txstate);
240 transferred = cppi41_channel->prog_len - txstate.residue;
241 cppi41_channel->transferred += transferred;
242
243 dev_dbg(musb->controller, "DMA transfer done on hw_ep=%d bytes=%d/%d\n",
244 hw_ep->epnum, cppi41_channel->transferred,
245 cppi41_channel->total_len);
246
247 update_rx_toggle(cppi41_channel);
248
249 if (cppi41_channel->transferred == cppi41_channel->total_len ||
250 transferred < cppi41_channel->packet_sz)
251 cppi41_channel->prog_len = 0;
252
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100253 empty = musb_is_tx_fifo_empty(hw_ep);
254 if (empty) {
255 cppi41_trans_done(cppi41_channel);
Felipe Balbi1b616252015-02-27 13:19:39 -0600256 goto out;
257 }
258
259 /*
260 * On AM335x it has been observed that the TX interrupt fires
261 * too early that means the TXFIFO is not yet empty but the DMA
262 * engine says that it is done with the transfer. We don't
263 * receive a FIFO empty interrupt so the only thing we can do is
264 * to poll for the bit. On HS it usually takes 2us, on FS around
265 * 110us - 150us depending on the transfer size.
266 * We spin on HS (no longer than than 25us and setup a timer on
267 * FS to check for the bit and complete the transfer.
268 */
269 controller = cppi41_channel->controller;
270
271 if (is_host_active(musb)) {
272 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
273 is_hs = 1;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100274 } else {
Felipe Balbi1b616252015-02-27 13:19:39 -0600275 if (musb->g.speed == USB_SPEED_HIGH)
276 is_hs = 1;
277 }
278 if (is_hs) {
279 unsigned wait = 25;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100280
Felipe Balbi1b616252015-02-27 13:19:39 -0600281 do {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100282 empty = musb_is_tx_fifo_empty(hw_ep);
Felipe Balbi1b616252015-02-27 13:19:39 -0600283 if (empty)
284 break;
285 wait--;
286 if (!wait)
287 break;
288 udelay(1);
289 } while (1);
Daniel Mack50aea6f2014-06-20 00:20:44 +0200290
Felipe Balbi1b616252015-02-27 13:19:39 -0600291 empty = musb_is_tx_fifo_empty(hw_ep);
292 if (empty) {
293 cppi41_trans_done(cppi41_channel);
294 goto out;
295 }
296 }
297 list_add_tail(&cppi41_channel->tx_check,
298 &controller->early_tx_list);
299 if (!hrtimer_is_queued(&controller->early_tx)) {
300 unsigned long usecs = cppi41_channel->total_len / 10;
301
302 hrtimer_start_range_ns(&controller->early_tx,
Daniel Mack50aea6f2014-06-20 00:20:44 +0200303 ktime_set(0, usecs * NSEC_PER_USEC),
Daniel Macka5e4aa42014-09-03 17:21:24 +0200304 20 * NSEC_PER_USEC,
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100305 HRTIMER_MODE_REL);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100306 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600307
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100308out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200309 spin_unlock_irqrestore(&musb->lock, flags);
310}
311
312static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
313{
314 unsigned shift;
315
316 shift = (ep - 1) * 2;
317 old &= ~(3 << shift);
318 old |= mode << shift;
319 return old;
320}
321
322static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
323 unsigned mode)
324{
325 struct cppi41_dma_controller *controller = cppi41_channel->controller;
326 u32 port;
327 u32 new_mode;
328 u32 old_mode;
329
330 if (cppi41_channel->is_tx)
331 old_mode = controller->tx_mode;
332 else
333 old_mode = controller->rx_mode;
334 port = cppi41_channel->port_num;
335 new_mode = update_ep_mode(port, mode, old_mode);
336
337 if (new_mode == old_mode)
338 return;
339 if (cppi41_channel->is_tx) {
340 controller->tx_mode = new_mode;
341 musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
342 new_mode);
343 } else {
344 controller->rx_mode = new_mode;
345 musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
346 new_mode);
347 }
348}
349
350static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
351 unsigned mode)
352{
353 struct cppi41_dma_controller *controller = cppi41_channel->controller;
354 u32 port;
355 u32 new_mode;
356 u32 old_mode;
357
358 old_mode = controller->auto_req;
359 port = cppi41_channel->port_num;
360 new_mode = update_ep_mode(port, mode, old_mode);
361
362 if (new_mode == old_mode)
363 return;
364 controller->auto_req = new_mode;
365 musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
366}
367
368static bool cppi41_configure_channel(struct dma_channel *channel,
369 u16 packet_sz, u8 mode,
370 dma_addr_t dma_addr, u32 len)
371{
372 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
373 struct dma_chan *dc = cppi41_channel->dc;
374 struct dma_async_tx_descriptor *dma_desc;
375 enum dma_transfer_direction direction;
376 struct musb *musb = cppi41_channel->controller->musb;
377 unsigned use_gen_rndis = 0;
378
379 dev_dbg(musb->controller,
380 "configure ep%d/%x packet_sz=%d, mode=%d, dma_addr=0x%llx, len=%d is_tx=%d\n",
381 cppi41_channel->port_num, RNDIS_REG(cppi41_channel->port_num),
382 packet_sz, mode, (unsigned long long) dma_addr,
383 len, cppi41_channel->is_tx);
384
385 cppi41_channel->buf_addr = dma_addr;
386 cppi41_channel->total_len = len;
387 cppi41_channel->transferred = 0;
388 cppi41_channel->packet_sz = packet_sz;
Bin Liu9267eda2014-08-12 14:18:43 -0500389 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200390
391 /*
392 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
393 * than max packet size at a time.
394 */
395 if (cppi41_channel->is_tx)
396 use_gen_rndis = 1;
397
398 if (use_gen_rndis) {
399 /* RNDIS mode */
400 if (len > packet_sz) {
401 musb_writel(musb->ctrl_base,
402 RNDIS_REG(cppi41_channel->port_num), len);
403 /* gen rndis */
404 cppi41_set_dma_mode(cppi41_channel,
405 EP_MODE_DMA_GEN_RNDIS);
406
407 /* auto req */
408 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600409 EP_MODE_AUTOREQ_ALL_NEOP);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200410 } else {
411 musb_writel(musb->ctrl_base,
412 RNDIS_REG(cppi41_channel->port_num), 0);
413 cppi41_set_dma_mode(cppi41_channel,
414 EP_MODE_DMA_TRANSPARENT);
415 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600416 EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200417 }
418 } else {
419 /* fallback mode */
420 cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
Bin Liu0149b072015-01-26 16:22:06 -0600421 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200422 len = min_t(u32, packet_sz, len);
423 }
424 cppi41_channel->prog_len = len;
425 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
426 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
427 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
428 if (!dma_desc)
429 return false;
430
431 dma_desc->callback = cppi41_dma_callback;
432 dma_desc->callback_param = channel;
433 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200434 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200435
436 save_rx_toggle(cppi41_channel);
437 dma_async_issue_pending(dc);
438 return true;
439}
440
441static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
442 struct musb_hw_ep *hw_ep, u8 is_tx)
443{
444 struct cppi41_dma_controller *controller = container_of(c,
445 struct cppi41_dma_controller, controller);
446 struct cppi41_dma_channel *cppi41_channel = NULL;
447 u8 ch_num = hw_ep->epnum - 1;
448
449 if (ch_num >= MUSB_DMA_NUM_CHANNELS)
450 return NULL;
451
452 if (is_tx)
453 cppi41_channel = &controller->tx_channel[ch_num];
454 else
455 cppi41_channel = &controller->rx_channel[ch_num];
456
457 if (!cppi41_channel->dc)
458 return NULL;
459
460 if (cppi41_channel->is_allocated)
461 return NULL;
462
463 cppi41_channel->hw_ep = hw_ep;
464 cppi41_channel->is_allocated = 1;
465
466 return &cppi41_channel->channel;
467}
468
469static void cppi41_dma_channel_release(struct dma_channel *channel)
470{
471 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
472
473 if (cppi41_channel->is_allocated) {
474 cppi41_channel->is_allocated = 0;
475 channel->status = MUSB_DMA_STATUS_FREE;
476 channel->actual_len = 0;
477 }
478}
479
480static int cppi41_dma_channel_program(struct dma_channel *channel,
481 u16 packet_sz, u8 mode,
482 dma_addr_t dma_addr, u32 len)
483{
484 int ret;
George Cherianf82503f2014-01-27 15:07:25 +0530485 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
486 int hb_mult = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200487
488 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
489 channel->status == MUSB_DMA_STATUS_BUSY);
490
George Cherianf82503f2014-01-27 15:07:25 +0530491 if (is_host_active(cppi41_channel->controller->musb)) {
492 if (cppi41_channel->is_tx)
493 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
494 else
495 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
496 }
497
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200498 channel->status = MUSB_DMA_STATUS_BUSY;
499 channel->actual_len = 0;
George Cherianf82503f2014-01-27 15:07:25 +0530500
501 if (hb_mult)
502 packet_sz = hb_mult * (packet_sz & 0x7FF);
503
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200504 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
505 if (!ret)
506 channel->status = MUSB_DMA_STATUS_FREE;
507
508 return ret;
509}
510
511static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
512 void *buf, u32 length)
513{
514 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
515 struct cppi41_dma_controller *controller = cppi41_channel->controller;
516 struct musb *musb = controller->musb;
517
518 if (is_host_active(musb)) {
519 WARN_ON(1);
520 return 1;
521 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100522 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
523 return 0;
Sebastian Andrzej Siewior13266fe2013-08-13 19:38:24 +0200524 if (cppi41_channel->is_tx)
525 return 1;
526 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200527 return 0;
528}
529
530static int cppi41_dma_channel_abort(struct dma_channel *channel)
531{
532 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
533 struct cppi41_dma_controller *controller = cppi41_channel->controller;
534 struct musb *musb = controller->musb;
535 void __iomem *epio = cppi41_channel->hw_ep->regs;
536 int tdbit;
537 int ret;
538 unsigned is_tx;
539 u16 csr;
540
541 is_tx = cppi41_channel->is_tx;
542 dev_dbg(musb->controller, "abort channel=%d, is_tx=%d\n",
543 cppi41_channel->port_num, is_tx);
544
545 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
546 return 0;
547
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100548 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200549 if (is_tx) {
550 csr = musb_readw(epio, MUSB_TXCSR);
551 csr &= ~MUSB_TXCSR_DMAENAB;
552 musb_writew(epio, MUSB_TXCSR, csr);
553 } else {
Bin Liucb83df72015-01-26 16:22:07 -0600554 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
555
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200556 csr = musb_readw(epio, MUSB_RXCSR);
557 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
558 musb_writew(epio, MUSB_RXCSR, csr);
559
Bin Liucb83df72015-01-26 16:22:07 -0600560 /* wait to drain cppi dma pipe line */
561 udelay(50);
562
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200563 csr = musb_readw(epio, MUSB_RXCSR);
564 if (csr & MUSB_RXCSR_RXPKTRDY) {
565 csr |= MUSB_RXCSR_FLUSHFIFO;
566 musb_writew(epio, MUSB_RXCSR, csr);
567 musb_writew(epio, MUSB_RXCSR, csr);
568 }
569 }
570
571 tdbit = 1 << cppi41_channel->port_num;
572 if (is_tx)
573 tdbit <<= 16;
574
575 do {
Bin Liucb83df72015-01-26 16:22:07 -0600576 if (is_tx)
577 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200578 ret = dmaengine_terminate_all(cppi41_channel->dc);
579 } while (ret == -EAGAIN);
580
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200581 if (is_tx) {
Bin Liucb83df72015-01-26 16:22:07 -0600582 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
583
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200584 csr = musb_readw(epio, MUSB_TXCSR);
585 if (csr & MUSB_TXCSR_TXPKTRDY) {
586 csr |= MUSB_TXCSR_FLUSHFIFO;
587 musb_writew(epio, MUSB_TXCSR, csr);
588 }
589 }
590
591 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
592 return 0;
593}
594
595static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
596{
597 struct dma_chan *dc;
598 int i;
599
600 for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
601 dc = ctrl->tx_channel[i].dc;
602 if (dc)
603 dma_release_channel(dc);
604 dc = ctrl->rx_channel[i].dc;
605 if (dc)
606 dma_release_channel(dc);
607 }
608}
609
610static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
611{
612 cppi41_release_all_dma_chans(controller);
613}
614
615static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
616{
617 struct musb *musb = controller->musb;
618 struct device *dev = musb->controller;
619 struct device_node *np = dev->of_node;
620 struct cppi41_dma_channel *cppi41_channel;
621 int count;
622 int i;
623 int ret;
624
625 count = of_property_count_strings(np, "dma-names");
626 if (count < 0)
627 return count;
628
629 for (i = 0; i < count; i++) {
630 struct dma_chan *dc;
631 struct dma_channel *musb_dma;
632 const char *str;
633 unsigned is_tx;
634 unsigned int port;
635
636 ret = of_property_read_string_index(np, "dma-names", i, &str);
637 if (ret)
638 goto err;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100639 if (strstarts(str, "tx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200640 is_tx = 1;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100641 else if (strstarts(str, "rx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200642 is_tx = 0;
643 else {
644 dev_err(dev, "Wrong dmatype %s\n", str);
645 goto err;
646 }
647 ret = kstrtouint(str + 2, 0, &port);
648 if (ret)
649 goto err;
650
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200651 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200652 if (port > MUSB_DMA_NUM_CHANNELS || !port)
653 goto err;
654 if (is_tx)
655 cppi41_channel = &controller->tx_channel[port - 1];
656 else
657 cppi41_channel = &controller->rx_channel[port - 1];
658
659 cppi41_channel->controller = controller;
660 cppi41_channel->port_num = port;
661 cppi41_channel->is_tx = is_tx;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100662 INIT_LIST_HEAD(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200663
664 musb_dma = &cppi41_channel->channel;
665 musb_dma->private_data = cppi41_channel;
666 musb_dma->status = MUSB_DMA_STATUS_FREE;
667 musb_dma->max_len = SZ_4M;
668
669 dc = dma_request_slave_channel(dev, str);
670 if (!dc) {
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +0530671 dev_err(dev, "Failed to request %s.\n", str);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200672 ret = -EPROBE_DEFER;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200673 goto err;
674 }
675 cppi41_channel->dc = dc;
676 }
677 return 0;
678err:
679 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200680 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200681}
682
683void dma_controller_destroy(struct dma_controller *c)
684{
685 struct cppi41_dma_controller *controller = container_of(c,
686 struct cppi41_dma_controller, controller);
687
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100688 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200689 cppi41_dma_controller_stop(controller);
690 kfree(controller);
691}
692
693struct dma_controller *dma_controller_create(struct musb *musb,
694 void __iomem *base)
695{
696 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200697 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200698
699 if (!musb->controller->of_node) {
700 dev_err(musb->controller, "Need DT for the DMA engine.\n");
701 return NULL;
702 }
703
704 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
705 if (!controller)
706 goto kzalloc_fail;
707
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100708 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
709 controller->early_tx.function = cppi41_recheck_tx_req;
710 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200711 controller->musb = musb;
712
713 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
714 controller->controller.channel_release = cppi41_dma_channel_release;
715 controller->controller.channel_program = cppi41_dma_channel_program;
716 controller->controller.channel_abort = cppi41_dma_channel_abort;
717 controller->controller.is_compatible = cppi41_is_compatible;
718
719 ret = cppi41_dma_controller_start(controller);
720 if (ret)
721 goto plat_get_fail;
722 return &controller->controller;
723
724plat_get_fail:
725 kfree(controller);
726kzalloc_fail:
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200727 if (ret == -EPROBE_DEFER)
728 return ERR_PTR(ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200729 return NULL;
730}