Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/device.h>
3#include <linux/dma-mapping.h>
4#include <linux/dmaengine.h>
5#include <linux/sizes.h>
6#include <linux/platform_device.h>
7#include <linux/of.h>
8
9#include "cppi_dma.h"
10#include "musb_core.h"
11#include "musb_trace.h"
12
13#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
14
15#define EP_MODE_AUTOREQ_NONE 0
16#define EP_MODE_AUTOREQ_ALL_NEOP 1
17#define EP_MODE_AUTOREQ_ALWAYS 3
18
19#define EP_MODE_DMA_TRANSPARENT 0
20#define EP_MODE_DMA_RNDIS 1
21#define EP_MODE_DMA_GEN_RNDIS 3
22
23#define USB_CTRL_TX_MODE 0x70
24#define USB_CTRL_RX_MODE 0x74
25#define USB_CTRL_AUTOREQ 0xd0
26#define USB_TDOWN 0xd8
27
28#define MUSB_DMA_NUM_CHANNELS 15
29
30#define DA8XX_USB_MODE 0x10
31#define DA8XX_USB_AUTOREQ 0x14
32#define DA8XX_USB_TEARDOWN 0x1c
33
34#define DA8XX_DMA_NUM_CHANNELS 4
35
36struct cppi41_dma_controller {
37 struct dma_controller controller;
38 struct cppi41_dma_channel *rx_channel;
39 struct cppi41_dma_channel *tx_channel;
40 struct hrtimer early_tx;
41 struct list_head early_tx_list;
42 u32 rx_mode;
43 u32 tx_mode;
44 u32 auto_req;
45
46 u32 tdown_reg;
47 u32 autoreq_reg;
48
49 void (*set_dma_mode)(struct cppi41_dma_channel *cppi41_channel,
50 unsigned int mode);
51 u8 num_channels;
52};
53
54static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
55{
56 u16 csr;
57 u8 toggle;
58
59 if (cppi41_channel->is_tx)
60 return;
61 if (!is_host_active(cppi41_channel->controller->controller.musb))
62 return;
63
64 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
65 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
66
67 cppi41_channel->usb_toggle = toggle;
68}
69
70static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
71{
72 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
73 struct musb *musb = hw_ep->musb;
74 u16 csr;
75 u8 toggle;
76
77 if (cppi41_channel->is_tx)
78 return;
79 if (!is_host_active(musb))
80 return;
81
82 musb_ep_select(musb->mregs, hw_ep->epnum);
83 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
84 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
85
86 /*
87 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
88 * data toggle may reset from DATA1 to DATA0 during receiving data from
89 * more than one endpoint.
90 */
91 if (!toggle && toggle == cppi41_channel->usb_toggle) {
92 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
93 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
94 musb_dbg(musb, "Restoring DATA1 toggle.");
95 }
96
97 cppi41_channel->usb_toggle = toggle;
98}
99
100static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
101{
102 u8 epnum = hw_ep->epnum;
103 struct musb *musb = hw_ep->musb;
104 void __iomem *epio = musb->endpoints[epnum].regs;
105 u16 csr;
106
107 musb_ep_select(musb->mregs, hw_ep->epnum);
108 csr = musb_readw(epio, MUSB_TXCSR);
109 if (csr & MUSB_TXCSR_TXPKTRDY)
110 return false;
111 return true;
112}
113
114static void cppi41_dma_callback(void *private_data,
115 const struct dmaengine_result *result);
116
117static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
118{
119 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
120 struct musb *musb = hw_ep->musb;
121 void __iomem *epio = hw_ep->regs;
122 u16 csr;
123
124 if (!cppi41_channel->prog_len ||
125 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
126
127 /* done, complete */
128 cppi41_channel->channel.actual_len =
129 cppi41_channel->transferred;
130 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
131 cppi41_channel->channel.rx_packet_done = true;
132
133 /*
134 * transmit ZLP using PIO mode for transfers which size is
135 * multiple of EP packet size.
136 */
137 if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
138 cppi41_channel->packet_sz) == 0) {
139 musb_ep_select(musb->mregs, hw_ep->epnum);
140 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
141 musb_writew(epio, MUSB_TXCSR, csr);
142 }
143
144 trace_musb_cppi41_done(cppi41_channel);
145 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
146 } else {
147 /* next iteration, reload */
148 struct dma_chan *dc = cppi41_channel->dc;
149 struct dma_async_tx_descriptor *dma_desc;
150 enum dma_transfer_direction direction;
151 u32 remain_bytes;
152
153 cppi41_channel->buf_addr += cppi41_channel->packet_sz;
154
155 remain_bytes = cppi41_channel->total_len;
156 remain_bytes -= cppi41_channel->transferred;
157 remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
158 cppi41_channel->prog_len = remain_bytes;
159
160 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
161 : DMA_DEV_TO_MEM;
162 dma_desc = dmaengine_prep_slave_single(dc,
163 cppi41_channel->buf_addr,
164 remain_bytes,
165 direction,
166 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
167 if (WARN_ON(!dma_desc))
168 return;
169
170 dma_desc->callback_result = cppi41_dma_callback;
171 dma_desc->callback_param = &cppi41_channel->channel;
172 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
173 trace_musb_cppi41_cont(cppi41_channel);
174 dma_async_issue_pending(dc);
175
176 if (!cppi41_channel->is_tx) {
177 musb_ep_select(musb->mregs, hw_ep->epnum);
178 csr = musb_readw(epio, MUSB_RXCSR);
179 csr |= MUSB_RXCSR_H_REQPKT;
180 musb_writew(epio, MUSB_RXCSR, csr);
181 }
182 }
183}
184
185static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
186{
187 struct cppi41_dma_controller *controller;
188 struct cppi41_dma_channel *cppi41_channel, *n;
189 struct musb *musb;
190 unsigned long flags;
191 enum hrtimer_restart ret = HRTIMER_NORESTART;
192
193 controller = container_of(timer, struct cppi41_dma_controller,
194 early_tx);
195 musb = controller->controller.musb;
196
197 spin_lock_irqsave(&musb->lock, flags);
198 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
199 tx_check) {
200 bool empty;
201 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
202
203 empty = musb_is_tx_fifo_empty(hw_ep);
204 if (empty) {
205 list_del_init(&cppi41_channel->tx_check);
206 cppi41_trans_done(cppi41_channel);
207 }
208 }
209
210 if (!list_empty(&controller->early_tx_list) &&
211 !hrtimer_is_queued(&controller->early_tx)) {
212 ret = HRTIMER_RESTART;
213 hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
214 }
215
216 spin_unlock_irqrestore(&musb->lock, flags);
217 return ret;
218}
219
220static void cppi41_dma_callback(void *private_data,
221 const struct dmaengine_result *result)
222{
223 struct dma_channel *channel = private_data;
224 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
225 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
226 struct cppi41_dma_controller *controller;
227 struct musb *musb = hw_ep->musb;
228 unsigned long flags;
229 struct dma_tx_state txstate;
230 u32 transferred;
231 int is_hs = 0;
232 bool empty;
233
234 controller = cppi41_channel->controller;
235 if (controller->controller.dma_callback)
236 controller->controller.dma_callback(&controller->controller);
237
238 if (result->result == DMA_TRANS_ABORTED)
239 return;
240
241 spin_lock_irqsave(&musb->lock, flags);
242
243 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
244 &txstate);
245 transferred = cppi41_channel->prog_len - txstate.residue;
246 cppi41_channel->transferred += transferred;
247
248 trace_musb_cppi41_gb(cppi41_channel);
249 update_rx_toggle(cppi41_channel);
250
251 if (cppi41_channel->transferred == cppi41_channel->total_len ||
252 transferred < cppi41_channel->packet_sz)
253 cppi41_channel->prog_len = 0;
254
255 if (cppi41_channel->is_tx) {
256 u8 type;
257
258 if (is_host_active(musb))
259 type = hw_ep->out_qh->type;
260 else
261 type = hw_ep->ep_in.type;
262
263 if (type == USB_ENDPOINT_XFER_ISOC)
264 /*
265 * Don't use the early-TX-interrupt workaround below
266 * for Isoch transfter. Since Isoch are periodic
267 * transfer, by the time the next transfer is
268 * scheduled, the current one should be done already.
269 *
270 * This avoids audio playback underrun issue.
271 */
272 empty = true;
273 else
274 empty = musb_is_tx_fifo_empty(hw_ep);
275 }
276
277 if (!cppi41_channel->is_tx || empty) {
278 cppi41_trans_done(cppi41_channel);
279 goto out;
280 }
281
282 /*
283 * On AM335x it has been observed that the TX interrupt fires
284 * too early that means the TXFIFO is not yet empty but the DMA
285 * engine says that it is done with the transfer. We don't
286 * receive a FIFO empty interrupt so the only thing we can do is
287 * to poll for the bit. On HS it usually takes 2us, on FS around
288 * 110us - 150us depending on the transfer size.
289 * We spin on HS (no longer than 25us and setup a timer on
290 * FS to check for the bit and complete the transfer.
291 */
292 if (is_host_active(musb)) {
293 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
294 is_hs = 1;
295 } else {
296 if (musb->g.speed == USB_SPEED_HIGH)
297 is_hs = 1;
298 }
299 if (is_hs) {
300 unsigned wait = 25;
301
302 do {
303 empty = musb_is_tx_fifo_empty(hw_ep);
304 if (empty) {
305 cppi41_trans_done(cppi41_channel);
306 goto out;
307 }
308 wait--;
309 if (!wait)
310 break;
311 cpu_relax();
312 } while (1);
313 }
314 list_add_tail(&cppi41_channel->tx_check,
315 &controller->early_tx_list);
316 if (!hrtimer_is_queued(&controller->early_tx)) {
317 unsigned long usecs = cppi41_channel->total_len / 10;
318
319 hrtimer_start_range_ns(&controller->early_tx,
320 usecs * NSEC_PER_USEC,
321 20 * NSEC_PER_USEC,
322 HRTIMER_MODE_REL);
323 }
324
325out:
326 spin_unlock_irqrestore(&musb->lock, flags);
327}
328
329static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
330{
331 unsigned shift;
332
333 shift = (ep - 1) * 2;
334 old &= ~(3 << shift);
335 old |= mode << shift;
336 return old;
337}
338
339static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
340 unsigned mode)
341{
342 struct cppi41_dma_controller *controller = cppi41_channel->controller;
343 struct musb *musb = controller->controller.musb;
344 u32 port;
345 u32 new_mode;
346 u32 old_mode;
347
348 if (cppi41_channel->is_tx)
349 old_mode = controller->tx_mode;
350 else
351 old_mode = controller->rx_mode;
352 port = cppi41_channel->port_num;
353 new_mode = update_ep_mode(port, mode, old_mode);
354
355 if (new_mode == old_mode)
356 return;
357 if (cppi41_channel->is_tx) {
358 controller->tx_mode = new_mode;
359 musb_writel(musb->ctrl_base, USB_CTRL_TX_MODE, new_mode);
360 } else {
361 controller->rx_mode = new_mode;
362 musb_writel(musb->ctrl_base, USB_CTRL_RX_MODE, new_mode);
363 }
364}
365
366static void da8xx_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
367 unsigned int mode)
368{
369 struct cppi41_dma_controller *controller = cppi41_channel->controller;
370 struct musb *musb = controller->controller.musb;
371 unsigned int shift;
372 u32 port;
373 u32 new_mode;
374 u32 old_mode;
375
376 old_mode = controller->tx_mode;
377 port = cppi41_channel->port_num;
378
379 shift = (port - 1) * 4;
380 if (!cppi41_channel->is_tx)
381 shift += 16;
382 new_mode = old_mode & ~(3 << shift);
383 new_mode |= mode << shift;
384
385 if (new_mode == old_mode)
386 return;
387 controller->tx_mode = new_mode;
388 musb_writel(musb->ctrl_base, DA8XX_USB_MODE, new_mode);
389}
390
391
392static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
393 unsigned mode)
394{
395 struct cppi41_dma_controller *controller = cppi41_channel->controller;
396 u32 port;
397 u32 new_mode;
398 u32 old_mode;
399
400 old_mode = controller->auto_req;
401 port = cppi41_channel->port_num;
402 new_mode = update_ep_mode(port, mode, old_mode);
403
404 if (new_mode == old_mode)
405 return;
406 controller->auto_req = new_mode;
407 musb_writel(controller->controller.musb->ctrl_base,
408 controller->autoreq_reg, new_mode);
409}
410
411static bool cppi41_configure_channel(struct dma_channel *channel,
412 u16 packet_sz, u8 mode,
413 dma_addr_t dma_addr, u32 len)
414{
415 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
416 struct cppi41_dma_controller *controller = cppi41_channel->controller;
417 struct dma_chan *dc = cppi41_channel->dc;
418 struct dma_async_tx_descriptor *dma_desc;
419 enum dma_transfer_direction direction;
420 struct musb *musb = cppi41_channel->controller->controller.musb;
421 unsigned use_gen_rndis = 0;
422
423 cppi41_channel->buf_addr = dma_addr;
424 cppi41_channel->total_len = len;
425 cppi41_channel->transferred = 0;
426 cppi41_channel->packet_sz = packet_sz;
427 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
428
429 /*
430 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
431 * than max packet size at a time.
432 */
433 if (cppi41_channel->is_tx)
434 use_gen_rndis = 1;
435
436 if (use_gen_rndis) {
437 /* RNDIS mode */
438 if (len > packet_sz) {
439 musb_writel(musb->ctrl_base,
440 RNDIS_REG(cppi41_channel->port_num), len);
441 /* gen rndis */
442 controller->set_dma_mode(cppi41_channel,
443 EP_MODE_DMA_GEN_RNDIS);
444
445 /* auto req */
446 cppi41_set_autoreq_mode(cppi41_channel,
447 EP_MODE_AUTOREQ_ALL_NEOP);
448 } else {
449 musb_writel(musb->ctrl_base,
450 RNDIS_REG(cppi41_channel->port_num), 0);
451 controller->set_dma_mode(cppi41_channel,
452 EP_MODE_DMA_TRANSPARENT);
453 cppi41_set_autoreq_mode(cppi41_channel,
454 EP_MODE_AUTOREQ_NONE);
455 }
456 } else {
457 /* fallback mode */
458 controller->set_dma_mode(cppi41_channel,
459 EP_MODE_DMA_TRANSPARENT);
460 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
461 len = min_t(u32, packet_sz, len);
462 }
463 cppi41_channel->prog_len = len;
464 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
465 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
466 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
467 if (!dma_desc)
468 return false;
469
470 dma_desc->callback_result = cppi41_dma_callback;
471 dma_desc->callback_param = channel;
472 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
473 cppi41_channel->channel.rx_packet_done = false;
474
475 trace_musb_cppi41_config(cppi41_channel);
476
477 save_rx_toggle(cppi41_channel);
478 dma_async_issue_pending(dc);
479 return true;
480}
481
482static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
483 struct musb_hw_ep *hw_ep, u8 is_tx)
484{
485 struct cppi41_dma_controller *controller = container_of(c,
486 struct cppi41_dma_controller, controller);
487 struct cppi41_dma_channel *cppi41_channel = NULL;
488 u8 ch_num = hw_ep->epnum - 1;
489
490 if (ch_num >= controller->num_channels)
491 return NULL;
492
493 if (is_tx)
494 cppi41_channel = &controller->tx_channel[ch_num];
495 else
496 cppi41_channel = &controller->rx_channel[ch_num];
497
498 if (!cppi41_channel->dc)
499 return NULL;
500
501 if (cppi41_channel->is_allocated)
502 return NULL;
503
504 cppi41_channel->hw_ep = hw_ep;
505 cppi41_channel->is_allocated = 1;
506
507 trace_musb_cppi41_alloc(cppi41_channel);
508 return &cppi41_channel->channel;
509}
510
511static void cppi41_dma_channel_release(struct dma_channel *channel)
512{
513 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
514
515 trace_musb_cppi41_free(cppi41_channel);
516 if (cppi41_channel->is_allocated) {
517 cppi41_channel->is_allocated = 0;
518 channel->status = MUSB_DMA_STATUS_FREE;
519 channel->actual_len = 0;
520 }
521}
522
523static int cppi41_dma_channel_program(struct dma_channel *channel,
524 u16 packet_sz, u8 mode,
525 dma_addr_t dma_addr, u32 len)
526{
527 int ret;
528 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
529 int hb_mult = 0;
530
531 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
532 channel->status == MUSB_DMA_STATUS_BUSY);
533
534 if (is_host_active(cppi41_channel->controller->controller.musb)) {
535 if (cppi41_channel->is_tx)
536 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
537 else
538 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
539 }
540
541 channel->status = MUSB_DMA_STATUS_BUSY;
542 channel->actual_len = 0;
543
544 if (hb_mult)
545 packet_sz = hb_mult * (packet_sz & 0x7FF);
546
547 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
548 if (!ret)
549 channel->status = MUSB_DMA_STATUS_FREE;
550
551 return ret;
552}
553
554static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
555 void *buf, u32 length)
556{
557 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
558 struct cppi41_dma_controller *controller = cppi41_channel->controller;
559 struct musb *musb = controller->controller.musb;
560
561 if (is_host_active(musb)) {
562 WARN_ON(1);
563 return 1;
564 }
565 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
566 return 0;
567 if (cppi41_channel->is_tx)
568 return 1;
569 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
570 return 0;
571}
572
573static int cppi41_dma_channel_abort(struct dma_channel *channel)
574{
575 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
576 struct cppi41_dma_controller *controller = cppi41_channel->controller;
577 struct musb *musb = controller->controller.musb;
578 void __iomem *epio = cppi41_channel->hw_ep->regs;
579 int tdbit;
580 int ret;
581 unsigned is_tx;
582 u16 csr;
583
584 is_tx = cppi41_channel->is_tx;
585 trace_musb_cppi41_abort(cppi41_channel);
586
587 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
588 return 0;
589
590 list_del_init(&cppi41_channel->tx_check);
591 if (is_tx) {
592 csr = musb_readw(epio, MUSB_TXCSR);
593 csr &= ~MUSB_TXCSR_DMAENAB;
594 musb_writew(epio, MUSB_TXCSR, csr);
595 } else {
596 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
597
598 /* delay to drain to cppi dma pipeline for isoch */
599 udelay(250);
600
601 csr = musb_readw(epio, MUSB_RXCSR);
602 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
603 musb_writew(epio, MUSB_RXCSR, csr);
604
605 /* wait to drain cppi dma pipe line */
606 udelay(50);
607
608 csr = musb_readw(epio, MUSB_RXCSR);
609 if (csr & MUSB_RXCSR_RXPKTRDY) {
610 csr |= MUSB_RXCSR_FLUSHFIFO;
611 musb_writew(epio, MUSB_RXCSR, csr);
612 musb_writew(epio, MUSB_RXCSR, csr);
613 }
614 }
615
616 /* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
617 if (musb->ops->quirks & MUSB_DA8XX)
618 mdelay(250);
619
620 tdbit = 1 << cppi41_channel->port_num;
621 if (is_tx)
622 tdbit <<= 16;
623
624 do {
625 if (is_tx)
626 musb_writel(musb->ctrl_base, controller->tdown_reg,
627 tdbit);
628 ret = dmaengine_terminate_all(cppi41_channel->dc);
629 } while (ret == -EAGAIN);
630
631 if (is_tx) {
632 musb_writel(musb->ctrl_base, controller->tdown_reg, tdbit);
633
634 csr = musb_readw(epio, MUSB_TXCSR);
635 if (csr & MUSB_TXCSR_TXPKTRDY) {
636 csr |= MUSB_TXCSR_FLUSHFIFO;
637 musb_writew(epio, MUSB_TXCSR, csr);
638 }
639 }
640
641 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
642 return 0;
643}
644
645static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
646{
647 struct dma_chan *dc;
648 int i;
649
650 for (i = 0; i < ctrl->num_channels; i++) {
651 dc = ctrl->tx_channel[i].dc;
652 if (dc)
653 dma_release_channel(dc);
654 dc = ctrl->rx_channel[i].dc;
655 if (dc)
656 dma_release_channel(dc);
657 }
658}
659
660static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
661{
662 cppi41_release_all_dma_chans(controller);
663}
664
665static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
666{
667 struct musb *musb = controller->controller.musb;
668 struct device *dev = musb->controller;
669 struct device_node *np = dev->parent->of_node;
670 struct cppi41_dma_channel *cppi41_channel;
671 int count;
672 int i;
673 int ret;
674
675 count = of_property_count_strings(np, "dma-names");
676 if (count < 0)
677 return count;
678
679 for (i = 0; i < count; i++) {
680 struct dma_chan *dc;
681 struct dma_channel *musb_dma;
682 const char *str;
683 unsigned is_tx;
684 unsigned int port;
685
686 ret = of_property_read_string_index(np, "dma-names", i, &str);
687 if (ret)
688 goto err;
689 if (strstarts(str, "tx"))
690 is_tx = 1;
691 else if (strstarts(str, "rx"))
692 is_tx = 0;
693 else {
694 dev_err(dev, "Wrong dmatype %s\n", str);
695 goto err;
696 }
697 ret = kstrtouint(str + 2, 0, &port);
698 if (ret)
699 goto err;
700
701 ret = -EINVAL;
702 if (port > controller->num_channels || !port)
703 goto err;
704 if (is_tx)
705 cppi41_channel = &controller->tx_channel[port - 1];
706 else
707 cppi41_channel = &controller->rx_channel[port - 1];
708
709 cppi41_channel->controller = controller;
710 cppi41_channel->port_num = port;
711 cppi41_channel->is_tx = is_tx;
712 INIT_LIST_HEAD(&cppi41_channel->tx_check);
713
714 musb_dma = &cppi41_channel->channel;
715 musb_dma->private_data = cppi41_channel;
716 musb_dma->status = MUSB_DMA_STATUS_FREE;
717 musb_dma->max_len = SZ_4M;
718
719 dc = dma_request_chan(dev->parent, str);
720 if (IS_ERR(dc)) {
721 ret = dev_err_probe(dev, PTR_ERR(dc),
722 "Failed to request %s.\n", str);
723 goto err;
724 }
725
726 cppi41_channel->dc = dc;
727 }
728 return 0;
729err:
730 cppi41_release_all_dma_chans(controller);
731 return ret;
732}
733
734void cppi41_dma_controller_destroy(struct dma_controller *c)
735{
736 struct cppi41_dma_controller *controller = container_of(c,
737 struct cppi41_dma_controller, controller);
738
739 hrtimer_cancel(&controller->early_tx);
740 cppi41_dma_controller_stop(controller);
741 kfree(controller->rx_channel);
742 kfree(controller->tx_channel);
743 kfree(controller);
744}
745EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
746
747struct dma_controller *
748cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
749{
750 struct cppi41_dma_controller *controller;
751 int channel_size;
752 int ret = 0;
753
754 if (!musb->controller->parent->of_node) {
755 dev_err(musb->controller, "Need DT for the DMA engine.\n");
756 return NULL;
757 }
758
759 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
760 if (!controller)
761 goto kzalloc_fail;
762
763 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
764 controller->early_tx.function = cppi41_recheck_tx_req;
765 INIT_LIST_HEAD(&controller->early_tx_list);
766
767 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
768 controller->controller.channel_release = cppi41_dma_channel_release;
769 controller->controller.channel_program = cppi41_dma_channel_program;
770 controller->controller.channel_abort = cppi41_dma_channel_abort;
771 controller->controller.is_compatible = cppi41_is_compatible;
772 controller->controller.musb = musb;
773
774 if (musb->ops->quirks & MUSB_DA8XX) {
775 controller->tdown_reg = DA8XX_USB_TEARDOWN;
776 controller->autoreq_reg = DA8XX_USB_AUTOREQ;
777 controller->set_dma_mode = da8xx_set_dma_mode;
778 controller->num_channels = DA8XX_DMA_NUM_CHANNELS;
779 } else {
780 controller->tdown_reg = USB_TDOWN;
781 controller->autoreq_reg = USB_CTRL_AUTOREQ;
782 controller->set_dma_mode = cppi41_set_dma_mode;
783 controller->num_channels = MUSB_DMA_NUM_CHANNELS;
784 }
785
786 channel_size = controller->num_channels *
787 sizeof(struct cppi41_dma_channel);
788 controller->rx_channel = kzalloc(channel_size, GFP_KERNEL);
789 if (!controller->rx_channel)
790 goto rx_channel_alloc_fail;
791 controller->tx_channel = kzalloc(channel_size, GFP_KERNEL);
792 if (!controller->tx_channel)
793 goto tx_channel_alloc_fail;
794
795 ret = cppi41_dma_controller_start(controller);
796 if (ret)
797 goto plat_get_fail;
798 return &controller->controller;
799
800plat_get_fail:
801 kfree(controller->tx_channel);
802tx_channel_alloc_fail:
803 kfree(controller->rx_channel);
804rx_channel_alloc_fail:
805 kfree(controller);
806kzalloc_fail:
807 if (ret == -EPROBE_DEFER)
808 return ERR_PTR(ret);
809 return NULL;
810}
811EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);
1#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 "cppi_dma.h"
9#include "musb_core.h"
10#include "musb_trace.h"
11
12#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
13
14#define EP_MODE_AUTOREQ_NONE 0
15#define EP_MODE_AUTOREQ_ALL_NEOP 1
16#define EP_MODE_AUTOREQ_ALWAYS 3
17
18#define EP_MODE_DMA_TRANSPARENT 0
19#define EP_MODE_DMA_RNDIS 1
20#define EP_MODE_DMA_GEN_RNDIS 3
21
22#define USB_CTRL_TX_MODE 0x70
23#define USB_CTRL_RX_MODE 0x74
24#define USB_CTRL_AUTOREQ 0xd0
25#define USB_TDOWN 0xd8
26
27#define MUSB_DMA_NUM_CHANNELS 15
28
29struct cppi41_dma_controller {
30 struct dma_controller controller;
31 struct cppi41_dma_channel rx_channel[MUSB_DMA_NUM_CHANNELS];
32 struct cppi41_dma_channel tx_channel[MUSB_DMA_NUM_CHANNELS];
33 struct musb *musb;
34 struct hrtimer early_tx;
35 struct list_head early_tx_list;
36 u32 rx_mode;
37 u32 tx_mode;
38 u32 auto_req;
39};
40
41static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
42{
43 u16 csr;
44 u8 toggle;
45
46 if (cppi41_channel->is_tx)
47 return;
48 if (!is_host_active(cppi41_channel->controller->musb))
49 return;
50
51 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
52 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
53
54 cppi41_channel->usb_toggle = toggle;
55}
56
57static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
58{
59 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
60 struct musb *musb = hw_ep->musb;
61 u16 csr;
62 u8 toggle;
63
64 if (cppi41_channel->is_tx)
65 return;
66 if (!is_host_active(musb))
67 return;
68
69 musb_ep_select(musb->mregs, hw_ep->epnum);
70 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
71 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
72
73 /*
74 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
75 * data toggle may reset from DATA1 to DATA0 during receiving data from
76 * more than one endpoint.
77 */
78 if (!toggle && toggle == cppi41_channel->usb_toggle) {
79 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
80 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
81 musb_dbg(cppi41_channel->controller->musb,
82 "Restoring DATA1 toggle.");
83 }
84
85 cppi41_channel->usb_toggle = toggle;
86}
87
88static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
89{
90 u8 epnum = hw_ep->epnum;
91 struct musb *musb = hw_ep->musb;
92 void __iomem *epio = musb->endpoints[epnum].regs;
93 u16 csr;
94
95 musb_ep_select(musb->mregs, hw_ep->epnum);
96 csr = musb_readw(epio, MUSB_TXCSR);
97 if (csr & MUSB_TXCSR_TXPKTRDY)
98 return false;
99 return true;
100}
101
102static void cppi41_dma_callback(void *private_data);
103
104static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
105{
106 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
107 struct musb *musb = hw_ep->musb;
108 void __iomem *epio = hw_ep->regs;
109 u16 csr;
110
111 if (!cppi41_channel->prog_len ||
112 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
113
114 /* done, complete */
115 cppi41_channel->channel.actual_len =
116 cppi41_channel->transferred;
117 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
118 cppi41_channel->channel.rx_packet_done = true;
119
120 /*
121 * transmit ZLP using PIO mode for transfers which size is
122 * multiple of EP packet size.
123 */
124 if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
125 cppi41_channel->packet_sz) == 0) {
126 musb_ep_select(musb->mregs, hw_ep->epnum);
127 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
128 musb_writew(epio, MUSB_TXCSR, csr);
129 }
130
131 trace_musb_cppi41_done(cppi41_channel);
132 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
133 } else {
134 /* next iteration, reload */
135 struct dma_chan *dc = cppi41_channel->dc;
136 struct dma_async_tx_descriptor *dma_desc;
137 enum dma_transfer_direction direction;
138 u32 remain_bytes;
139
140 cppi41_channel->buf_addr += cppi41_channel->packet_sz;
141
142 remain_bytes = cppi41_channel->total_len;
143 remain_bytes -= cppi41_channel->transferred;
144 remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
145 cppi41_channel->prog_len = remain_bytes;
146
147 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
148 : DMA_DEV_TO_MEM;
149 dma_desc = dmaengine_prep_slave_single(dc,
150 cppi41_channel->buf_addr,
151 remain_bytes,
152 direction,
153 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
154 if (WARN_ON(!dma_desc))
155 return;
156
157 dma_desc->callback = cppi41_dma_callback;
158 dma_desc->callback_param = &cppi41_channel->channel;
159 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
160 trace_musb_cppi41_cont(cppi41_channel);
161 dma_async_issue_pending(dc);
162
163 if (!cppi41_channel->is_tx) {
164 musb_ep_select(musb->mregs, hw_ep->epnum);
165 csr = musb_readw(epio, MUSB_RXCSR);
166 csr |= MUSB_RXCSR_H_REQPKT;
167 musb_writew(epio, MUSB_RXCSR, csr);
168 }
169 }
170}
171
172static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
173{
174 struct cppi41_dma_controller *controller;
175 struct cppi41_dma_channel *cppi41_channel, *n;
176 struct musb *musb;
177 unsigned long flags;
178 enum hrtimer_restart ret = HRTIMER_NORESTART;
179
180 controller = container_of(timer, struct cppi41_dma_controller,
181 early_tx);
182 musb = controller->musb;
183
184 spin_lock_irqsave(&musb->lock, flags);
185 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
186 tx_check) {
187 bool empty;
188 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
189
190 empty = musb_is_tx_fifo_empty(hw_ep);
191 if (empty) {
192 list_del_init(&cppi41_channel->tx_check);
193 cppi41_trans_done(cppi41_channel);
194 }
195 }
196
197 if (!list_empty(&controller->early_tx_list) &&
198 !hrtimer_is_queued(&controller->early_tx)) {
199 ret = HRTIMER_RESTART;
200 hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
201 }
202
203 spin_unlock_irqrestore(&musb->lock, flags);
204 return ret;
205}
206
207static void cppi41_dma_callback(void *private_data)
208{
209 struct dma_channel *channel = private_data;
210 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
211 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
212 struct cppi41_dma_controller *controller;
213 struct musb *musb = hw_ep->musb;
214 unsigned long flags;
215 struct dma_tx_state txstate;
216 u32 transferred;
217 int is_hs = 0;
218 bool empty;
219
220 spin_lock_irqsave(&musb->lock, flags);
221
222 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
223 &txstate);
224 transferred = cppi41_channel->prog_len - txstate.residue;
225 cppi41_channel->transferred += transferred;
226
227 trace_musb_cppi41_gb(cppi41_channel);
228 update_rx_toggle(cppi41_channel);
229
230 if (cppi41_channel->transferred == cppi41_channel->total_len ||
231 transferred < cppi41_channel->packet_sz)
232 cppi41_channel->prog_len = 0;
233
234 if (cppi41_channel->is_tx) {
235 u8 type;
236
237 if (is_host_active(musb))
238 type = hw_ep->out_qh->type;
239 else
240 type = hw_ep->ep_in.type;
241
242 if (type == USB_ENDPOINT_XFER_ISOC)
243 /*
244 * Don't use the early-TX-interrupt workaround below
245 * for Isoch transfter. Since Isoch are periodic
246 * transfer, by the time the next transfer is
247 * scheduled, the current one should be done already.
248 *
249 * This avoids audio playback underrun issue.
250 */
251 empty = true;
252 else
253 empty = musb_is_tx_fifo_empty(hw_ep);
254 }
255
256 if (!cppi41_channel->is_tx || empty) {
257 cppi41_trans_done(cppi41_channel);
258 goto out;
259 }
260
261 /*
262 * On AM335x it has been observed that the TX interrupt fires
263 * too early that means the TXFIFO is not yet empty but the DMA
264 * engine says that it is done with the transfer. We don't
265 * receive a FIFO empty interrupt so the only thing we can do is
266 * to poll for the bit. On HS it usually takes 2us, on FS around
267 * 110us - 150us depending on the transfer size.
268 * We spin on HS (no longer than than 25us and setup a timer on
269 * FS to check for the bit and complete the transfer.
270 */
271 controller = cppi41_channel->controller;
272
273 if (is_host_active(musb)) {
274 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
275 is_hs = 1;
276 } else {
277 if (musb->g.speed == USB_SPEED_HIGH)
278 is_hs = 1;
279 }
280 if (is_hs) {
281 unsigned wait = 25;
282
283 do {
284 empty = musb_is_tx_fifo_empty(hw_ep);
285 if (empty) {
286 cppi41_trans_done(cppi41_channel);
287 goto out;
288 }
289 wait--;
290 if (!wait)
291 break;
292 cpu_relax();
293 } while (1);
294 }
295 list_add_tail(&cppi41_channel->tx_check,
296 &controller->early_tx_list);
297 if (!hrtimer_is_queued(&controller->early_tx)) {
298 unsigned long usecs = cppi41_channel->total_len / 10;
299
300 hrtimer_start_range_ns(&controller->early_tx,
301 usecs * NSEC_PER_USEC,
302 20 * NSEC_PER_USEC,
303 HRTIMER_MODE_REL);
304 }
305
306out:
307 spin_unlock_irqrestore(&musb->lock, flags);
308}
309
310static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
311{
312 unsigned shift;
313
314 shift = (ep - 1) * 2;
315 old &= ~(3 << shift);
316 old |= mode << shift;
317 return old;
318}
319
320static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
321 unsigned mode)
322{
323 struct cppi41_dma_controller *controller = cppi41_channel->controller;
324 u32 port;
325 u32 new_mode;
326 u32 old_mode;
327
328 if (cppi41_channel->is_tx)
329 old_mode = controller->tx_mode;
330 else
331 old_mode = controller->rx_mode;
332 port = cppi41_channel->port_num;
333 new_mode = update_ep_mode(port, mode, old_mode);
334
335 if (new_mode == old_mode)
336 return;
337 if (cppi41_channel->is_tx) {
338 controller->tx_mode = new_mode;
339 musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
340 new_mode);
341 } else {
342 controller->rx_mode = new_mode;
343 musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
344 new_mode);
345 }
346}
347
348static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
349 unsigned mode)
350{
351 struct cppi41_dma_controller *controller = cppi41_channel->controller;
352 u32 port;
353 u32 new_mode;
354 u32 old_mode;
355
356 old_mode = controller->auto_req;
357 port = cppi41_channel->port_num;
358 new_mode = update_ep_mode(port, mode, old_mode);
359
360 if (new_mode == old_mode)
361 return;
362 controller->auto_req = new_mode;
363 musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
364}
365
366static bool cppi41_configure_channel(struct dma_channel *channel,
367 u16 packet_sz, u8 mode,
368 dma_addr_t dma_addr, u32 len)
369{
370 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
371 struct dma_chan *dc = cppi41_channel->dc;
372 struct dma_async_tx_descriptor *dma_desc;
373 enum dma_transfer_direction direction;
374 struct musb *musb = cppi41_channel->controller->musb;
375 unsigned use_gen_rndis = 0;
376
377 cppi41_channel->buf_addr = dma_addr;
378 cppi41_channel->total_len = len;
379 cppi41_channel->transferred = 0;
380 cppi41_channel->packet_sz = packet_sz;
381 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
382
383 /*
384 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
385 * than max packet size at a time.
386 */
387 if (cppi41_channel->is_tx)
388 use_gen_rndis = 1;
389
390 if (use_gen_rndis) {
391 /* RNDIS mode */
392 if (len > packet_sz) {
393 musb_writel(musb->ctrl_base,
394 RNDIS_REG(cppi41_channel->port_num), len);
395 /* gen rndis */
396 cppi41_set_dma_mode(cppi41_channel,
397 EP_MODE_DMA_GEN_RNDIS);
398
399 /* auto req */
400 cppi41_set_autoreq_mode(cppi41_channel,
401 EP_MODE_AUTOREQ_ALL_NEOP);
402 } else {
403 musb_writel(musb->ctrl_base,
404 RNDIS_REG(cppi41_channel->port_num), 0);
405 cppi41_set_dma_mode(cppi41_channel,
406 EP_MODE_DMA_TRANSPARENT);
407 cppi41_set_autoreq_mode(cppi41_channel,
408 EP_MODE_AUTOREQ_NONE);
409 }
410 } else {
411 /* fallback mode */
412 cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
413 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
414 len = min_t(u32, packet_sz, len);
415 }
416 cppi41_channel->prog_len = len;
417 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
418 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
419 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
420 if (!dma_desc)
421 return false;
422
423 dma_desc->callback = cppi41_dma_callback;
424 dma_desc->callback_param = channel;
425 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
426 cppi41_channel->channel.rx_packet_done = false;
427
428 trace_musb_cppi41_config(cppi41_channel);
429
430 save_rx_toggle(cppi41_channel);
431 dma_async_issue_pending(dc);
432 return true;
433}
434
435static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
436 struct musb_hw_ep *hw_ep, u8 is_tx)
437{
438 struct cppi41_dma_controller *controller = container_of(c,
439 struct cppi41_dma_controller, controller);
440 struct cppi41_dma_channel *cppi41_channel = NULL;
441 u8 ch_num = hw_ep->epnum - 1;
442
443 if (ch_num >= MUSB_DMA_NUM_CHANNELS)
444 return NULL;
445
446 if (is_tx)
447 cppi41_channel = &controller->tx_channel[ch_num];
448 else
449 cppi41_channel = &controller->rx_channel[ch_num];
450
451 if (!cppi41_channel->dc)
452 return NULL;
453
454 if (cppi41_channel->is_allocated)
455 return NULL;
456
457 cppi41_channel->hw_ep = hw_ep;
458 cppi41_channel->is_allocated = 1;
459
460 trace_musb_cppi41_alloc(cppi41_channel);
461 return &cppi41_channel->channel;
462}
463
464static void cppi41_dma_channel_release(struct dma_channel *channel)
465{
466 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
467
468 trace_musb_cppi41_free(cppi41_channel);
469 if (cppi41_channel->is_allocated) {
470 cppi41_channel->is_allocated = 0;
471 channel->status = MUSB_DMA_STATUS_FREE;
472 channel->actual_len = 0;
473 }
474}
475
476static int cppi41_dma_channel_program(struct dma_channel *channel,
477 u16 packet_sz, u8 mode,
478 dma_addr_t dma_addr, u32 len)
479{
480 int ret;
481 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
482 int hb_mult = 0;
483
484 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
485 channel->status == MUSB_DMA_STATUS_BUSY);
486
487 if (is_host_active(cppi41_channel->controller->musb)) {
488 if (cppi41_channel->is_tx)
489 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
490 else
491 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
492 }
493
494 channel->status = MUSB_DMA_STATUS_BUSY;
495 channel->actual_len = 0;
496
497 if (hb_mult)
498 packet_sz = hb_mult * (packet_sz & 0x7FF);
499
500 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
501 if (!ret)
502 channel->status = MUSB_DMA_STATUS_FREE;
503
504 return ret;
505}
506
507static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
508 void *buf, u32 length)
509{
510 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
511 struct cppi41_dma_controller *controller = cppi41_channel->controller;
512 struct musb *musb = controller->musb;
513
514 if (is_host_active(musb)) {
515 WARN_ON(1);
516 return 1;
517 }
518 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
519 return 0;
520 if (cppi41_channel->is_tx)
521 return 1;
522 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
523 return 0;
524}
525
526static int cppi41_dma_channel_abort(struct dma_channel *channel)
527{
528 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
529 struct cppi41_dma_controller *controller = cppi41_channel->controller;
530 struct musb *musb = controller->musb;
531 void __iomem *epio = cppi41_channel->hw_ep->regs;
532 int tdbit;
533 int ret;
534 unsigned is_tx;
535 u16 csr;
536
537 is_tx = cppi41_channel->is_tx;
538 trace_musb_cppi41_abort(cppi41_channel);
539
540 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
541 return 0;
542
543 list_del_init(&cppi41_channel->tx_check);
544 if (is_tx) {
545 csr = musb_readw(epio, MUSB_TXCSR);
546 csr &= ~MUSB_TXCSR_DMAENAB;
547 musb_writew(epio, MUSB_TXCSR, csr);
548 } else {
549 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
550
551 /* delay to drain to cppi dma pipeline for isoch */
552 udelay(250);
553
554 csr = musb_readw(epio, MUSB_RXCSR);
555 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
556 musb_writew(epio, MUSB_RXCSR, csr);
557
558 /* wait to drain cppi dma pipe line */
559 udelay(50);
560
561 csr = musb_readw(epio, MUSB_RXCSR);
562 if (csr & MUSB_RXCSR_RXPKTRDY) {
563 csr |= MUSB_RXCSR_FLUSHFIFO;
564 musb_writew(epio, MUSB_RXCSR, csr);
565 musb_writew(epio, MUSB_RXCSR, csr);
566 }
567 }
568
569 tdbit = 1 << cppi41_channel->port_num;
570 if (is_tx)
571 tdbit <<= 16;
572
573 do {
574 if (is_tx)
575 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
576 ret = dmaengine_terminate_all(cppi41_channel->dc);
577 } while (ret == -EAGAIN);
578
579 if (is_tx) {
580 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
581
582 csr = musb_readw(epio, MUSB_TXCSR);
583 if (csr & MUSB_TXCSR_TXPKTRDY) {
584 csr |= MUSB_TXCSR_FLUSHFIFO;
585 musb_writew(epio, MUSB_TXCSR, csr);
586 }
587 }
588
589 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
590 return 0;
591}
592
593static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
594{
595 struct dma_chan *dc;
596 int i;
597
598 for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
599 dc = ctrl->tx_channel[i].dc;
600 if (dc)
601 dma_release_channel(dc);
602 dc = ctrl->rx_channel[i].dc;
603 if (dc)
604 dma_release_channel(dc);
605 }
606}
607
608static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
609{
610 cppi41_release_all_dma_chans(controller);
611}
612
613static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
614{
615 struct musb *musb = controller->musb;
616 struct device *dev = musb->controller;
617 struct device_node *np = dev->parent->of_node;
618 struct cppi41_dma_channel *cppi41_channel;
619 int count;
620 int i;
621 int ret;
622
623 count = of_property_count_strings(np, "dma-names");
624 if (count < 0)
625 return count;
626
627 for (i = 0; i < count; i++) {
628 struct dma_chan *dc;
629 struct dma_channel *musb_dma;
630 const char *str;
631 unsigned is_tx;
632 unsigned int port;
633
634 ret = of_property_read_string_index(np, "dma-names", i, &str);
635 if (ret)
636 goto err;
637 if (strstarts(str, "tx"))
638 is_tx = 1;
639 else if (strstarts(str, "rx"))
640 is_tx = 0;
641 else {
642 dev_err(dev, "Wrong dmatype %s\n", str);
643 goto err;
644 }
645 ret = kstrtouint(str + 2, 0, &port);
646 if (ret)
647 goto err;
648
649 ret = -EINVAL;
650 if (port > MUSB_DMA_NUM_CHANNELS || !port)
651 goto err;
652 if (is_tx)
653 cppi41_channel = &controller->tx_channel[port - 1];
654 else
655 cppi41_channel = &controller->rx_channel[port - 1];
656
657 cppi41_channel->controller = controller;
658 cppi41_channel->port_num = port;
659 cppi41_channel->is_tx = is_tx;
660 INIT_LIST_HEAD(&cppi41_channel->tx_check);
661
662 musb_dma = &cppi41_channel->channel;
663 musb_dma->private_data = cppi41_channel;
664 musb_dma->status = MUSB_DMA_STATUS_FREE;
665 musb_dma->max_len = SZ_4M;
666
667 dc = dma_request_slave_channel(dev->parent, str);
668 if (!dc) {
669 dev_err(dev, "Failed to request %s.\n", str);
670 ret = -EPROBE_DEFER;
671 goto err;
672 }
673 cppi41_channel->dc = dc;
674 }
675 return 0;
676err:
677 cppi41_release_all_dma_chans(controller);
678 return ret;
679}
680
681void cppi41_dma_controller_destroy(struct dma_controller *c)
682{
683 struct cppi41_dma_controller *controller = container_of(c,
684 struct cppi41_dma_controller, controller);
685
686 hrtimer_cancel(&controller->early_tx);
687 cppi41_dma_controller_stop(controller);
688 kfree(controller);
689}
690EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
691
692struct dma_controller *
693cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
694{
695 struct cppi41_dma_controller *controller;
696 int ret = 0;
697
698 if (!musb->controller->parent->of_node) {
699 dev_err(musb->controller, "Need DT for the DMA engine.\n");
700 return NULL;
701 }
702
703 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
704 if (!controller)
705 goto kzalloc_fail;
706
707 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
708 controller->early_tx.function = cppi41_recheck_tx_req;
709 INIT_LIST_HEAD(&controller->early_tx_list);
710 controller->musb = musb;
711
712 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
713 controller->controller.channel_release = cppi41_dma_channel_release;
714 controller->controller.channel_program = cppi41_dma_channel_program;
715 controller->controller.channel_abort = cppi41_dma_channel_abort;
716 controller->controller.is_compatible = cppi41_is_compatible;
717
718 ret = cppi41_dma_controller_start(controller);
719 if (ret)
720 goto plat_get_fail;
721 return &controller->controller;
722
723plat_get_fail:
724 kfree(controller);
725kzalloc_fail:
726 if (ret == -EPROBE_DEFER)
727 return ERR_PTR(ret);
728 return NULL;
729}
730EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);