blob: 4187ef1fab294b2636c91de3c4b40772636b40d8 [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
12#define EP_MODE_AUTOREG_NONE 0
13#define EP_MODE_AUTOREG_ALL_NEOP 1
14#define EP_MODE_AUTOREG_ALWAYS 3
15
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;
George Cherian1af54b72014-01-27 15:07:26 +053042 struct work_struct dma_completion;
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{
77 u16 csr;
78 u8 toggle;
79
80 if (cppi41_channel->is_tx)
81 return;
82 if (!is_host_active(cppi41_channel->controller->musb))
83 return;
84
85 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
86 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
87
88 /*
89 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
90 * data toggle may reset from DATA1 to DATA0 during receiving data from
91 * more than one endpoint.
92 */
93 if (!toggle && toggle == cppi41_channel->usb_toggle) {
94 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
95 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
96 dev_dbg(cppi41_channel->controller->musb->controller,
97 "Restoring DATA1 toggle.\n");
98 }
99
100 cppi41_channel->usb_toggle = toggle;
101}
102
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100103static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
104{
105 u8 epnum = hw_ep->epnum;
106 struct musb *musb = hw_ep->musb;
107 void __iomem *epio = musb->endpoints[epnum].regs;
108 u16 csr;
109
110 csr = musb_readw(epio, MUSB_TXCSR);
111 if (csr & MUSB_TXCSR_TXPKTRDY)
112 return false;
113 return true;
114}
115
George Cherian1af54b72014-01-27 15:07:26 +0530116static bool is_isoc(struct musb_hw_ep *hw_ep, bool in)
117{
118 if (in && hw_ep->in_qh) {
119 if (hw_ep->in_qh->type == USB_ENDPOINT_XFER_ISOC)
120 return true;
121 } else if (hw_ep->out_qh) {
122 if (hw_ep->out_qh->type == USB_ENDPOINT_XFER_ISOC)
123 return true;
124 }
125 return false;
126}
127
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100128static void cppi41_dma_callback(void *private_data);
129
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100130static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200131{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200132 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
133 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200134
George Cherianaecbc312014-02-27 10:44:41 +0530135 if (!cppi41_channel->prog_len ||
136 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200137
138 /* done, complete */
139 cppi41_channel->channel.actual_len =
140 cppi41_channel->transferred;
141 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200142 cppi41_channel->channel.rx_packet_done = true;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200143 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
144 } else {
145 /* next iteration, reload */
146 struct dma_chan *dc = cppi41_channel->dc;
147 struct dma_async_tx_descriptor *dma_desc;
148 enum dma_transfer_direction direction;
149 u16 csr;
150 u32 remain_bytes;
151 void __iomem *epio = cppi41_channel->hw_ep->regs;
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);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100167 if (WARN_ON(!dma_desc))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200168 return;
169
170 dma_desc->callback = cppi41_dma_callback;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100171 dma_desc->callback_param = &cppi41_channel->channel;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200172 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
173 dma_async_issue_pending(dc);
174
175 if (!cppi41_channel->is_tx) {
176 csr = musb_readw(epio, MUSB_RXCSR);
177 csr |= MUSB_RXCSR_H_REQPKT;
178 musb_writew(epio, MUSB_RXCSR, csr);
179 }
180 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100181}
182
George Cherian1af54b72014-01-27 15:07:26 +0530183static void cppi_trans_done_work(struct work_struct *work)
184{
185 unsigned long flags;
186 struct cppi41_dma_channel *cppi41_channel =
187 container_of(work, struct cppi41_dma_channel, dma_completion);
188 struct cppi41_dma_controller *controller = cppi41_channel->controller;
189 struct musb *musb = controller->musb;
190 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
191 bool empty;
192
193 if (!cppi41_channel->is_tx && is_isoc(hw_ep, 1)) {
194 spin_lock_irqsave(&musb->lock, flags);
195 cppi41_trans_done(cppi41_channel);
196 spin_unlock_irqrestore(&musb->lock, flags);
197 } else {
198 empty = musb_is_tx_fifo_empty(hw_ep);
199 if (empty) {
200 spin_lock_irqsave(&musb->lock, flags);
201 cppi41_trans_done(cppi41_channel);
202 spin_unlock_irqrestore(&musb->lock, flags);
203 } else {
204 schedule_work(&cppi41_channel->dma_completion);
205 }
206 }
207}
208
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100209static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
210{
211 struct cppi41_dma_controller *controller;
212 struct cppi41_dma_channel *cppi41_channel, *n;
213 struct musb *musb;
214 unsigned long flags;
215 enum hrtimer_restart ret = HRTIMER_NORESTART;
216
217 controller = container_of(timer, struct cppi41_dma_controller,
218 early_tx);
219 musb = controller->musb;
220
221 spin_lock_irqsave(&musb->lock, flags);
222 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
223 tx_check) {
224 bool empty;
225 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
226
227 empty = musb_is_tx_fifo_empty(hw_ep);
228 if (empty) {
229 list_del_init(&cppi41_channel->tx_check);
230 cppi41_trans_done(cppi41_channel);
231 }
232 }
233
234 if (!list_empty(&controller->early_tx_list)) {
235 ret = HRTIMER_RESTART;
236 hrtimer_forward_now(&controller->early_tx,
237 ktime_set(0, 150 * NSEC_PER_USEC));
238 }
239
240 spin_unlock_irqrestore(&musb->lock, flags);
241 return ret;
242}
243
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100244static void cppi41_dma_callback(void *private_data)
245{
246 struct dma_channel *channel = private_data;
247 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
248 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
249 struct musb *musb = hw_ep->musb;
250 unsigned long flags;
251 struct dma_tx_state txstate;
252 u32 transferred;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100253 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100254
255 spin_lock_irqsave(&musb->lock, flags);
256
257 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
258 &txstate);
259 transferred = cppi41_channel->prog_len - txstate.residue;
260 cppi41_channel->transferred += transferred;
261
262 dev_dbg(musb->controller, "DMA transfer done on hw_ep=%d bytes=%d/%d\n",
263 hw_ep->epnum, cppi41_channel->transferred,
264 cppi41_channel->total_len);
265
266 update_rx_toggle(cppi41_channel);
267
268 if (cppi41_channel->transferred == cppi41_channel->total_len ||
269 transferred < cppi41_channel->packet_sz)
270 cppi41_channel->prog_len = 0;
271
George Cherian1af54b72014-01-27 15:07:26 +0530272 if (!cppi41_channel->is_tx) {
273 if (is_isoc(hw_ep, 1))
274 schedule_work(&cppi41_channel->dma_completion);
275 else
276 cppi41_trans_done(cppi41_channel);
277 goto out;
278 }
279
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100280 empty = musb_is_tx_fifo_empty(hw_ep);
281 if (empty) {
282 cppi41_trans_done(cppi41_channel);
283 } else {
284 struct cppi41_dma_controller *controller;
285 /*
286 * On AM335x it has been observed that the TX interrupt fires
287 * too early that means the TXFIFO is not yet empty but the DMA
288 * engine says that it is done with the transfer. We don't
289 * receive a FIFO empty interrupt so the only thing we can do is
290 * to poll for the bit. On HS it usually takes 2us, on FS around
291 * 110us - 150us depending on the transfer size.
292 * We spin on HS (no longer than than 25us and setup a timer on
293 * FS to check for the bit and complete the transfer.
294 */
295 controller = cppi41_channel->controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100296
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100297 if (musb->g.speed == USB_SPEED_HIGH) {
298 unsigned wait = 25;
299
300 do {
301 empty = musb_is_tx_fifo_empty(hw_ep);
302 if (empty)
303 break;
304 wait--;
305 if (!wait)
306 break;
307 udelay(1);
308 } while (1);
309
310 empty = musb_is_tx_fifo_empty(hw_ep);
311 if (empty) {
312 cppi41_trans_done(cppi41_channel);
313 goto out;
314 }
315 }
George Cherian1af54b72014-01-27 15:07:26 +0530316 if (is_isoc(hw_ep, 0)) {
317 schedule_work(&cppi41_channel->dma_completion);
318 goto out;
319 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100320 list_add_tail(&cppi41_channel->tx_check,
321 &controller->early_tx_list);
322 if (!hrtimer_active(&controller->early_tx)) {
323 hrtimer_start_range_ns(&controller->early_tx,
324 ktime_set(0, 140 * NSEC_PER_USEC),
325 40 * NSEC_PER_USEC,
326 HRTIMER_MODE_REL);
327 }
328 }
329out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200330 spin_unlock_irqrestore(&musb->lock, flags);
331}
332
333static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
334{
335 unsigned shift;
336
337 shift = (ep - 1) * 2;
338 old &= ~(3 << shift);
339 old |= mode << shift;
340 return old;
341}
342
343static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
344 unsigned mode)
345{
346 struct cppi41_dma_controller *controller = cppi41_channel->controller;
347 u32 port;
348 u32 new_mode;
349 u32 old_mode;
350
351 if (cppi41_channel->is_tx)
352 old_mode = controller->tx_mode;
353 else
354 old_mode = controller->rx_mode;
355 port = cppi41_channel->port_num;
356 new_mode = update_ep_mode(port, mode, old_mode);
357
358 if (new_mode == old_mode)
359 return;
360 if (cppi41_channel->is_tx) {
361 controller->tx_mode = new_mode;
362 musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
363 new_mode);
364 } else {
365 controller->rx_mode = new_mode;
366 musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
367 new_mode);
368 }
369}
370
371static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
372 unsigned mode)
373{
374 struct cppi41_dma_controller *controller = cppi41_channel->controller;
375 u32 port;
376 u32 new_mode;
377 u32 old_mode;
378
379 old_mode = controller->auto_req;
380 port = cppi41_channel->port_num;
381 new_mode = update_ep_mode(port, mode, old_mode);
382
383 if (new_mode == old_mode)
384 return;
385 controller->auto_req = new_mode;
386 musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
387}
388
389static bool cppi41_configure_channel(struct dma_channel *channel,
390 u16 packet_sz, u8 mode,
391 dma_addr_t dma_addr, u32 len)
392{
393 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
394 struct dma_chan *dc = cppi41_channel->dc;
395 struct dma_async_tx_descriptor *dma_desc;
396 enum dma_transfer_direction direction;
397 struct musb *musb = cppi41_channel->controller->musb;
398 unsigned use_gen_rndis = 0;
399
400 dev_dbg(musb->controller,
401 "configure ep%d/%x packet_sz=%d, mode=%d, dma_addr=0x%llx, len=%d is_tx=%d\n",
402 cppi41_channel->port_num, RNDIS_REG(cppi41_channel->port_num),
403 packet_sz, mode, (unsigned long long) dma_addr,
404 len, cppi41_channel->is_tx);
405
406 cppi41_channel->buf_addr = dma_addr;
407 cppi41_channel->total_len = len;
408 cppi41_channel->transferred = 0;
409 cppi41_channel->packet_sz = packet_sz;
410
411 /*
412 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
413 * than max packet size at a time.
414 */
415 if (cppi41_channel->is_tx)
416 use_gen_rndis = 1;
417
418 if (use_gen_rndis) {
419 /* RNDIS mode */
420 if (len > packet_sz) {
421 musb_writel(musb->ctrl_base,
422 RNDIS_REG(cppi41_channel->port_num), len);
423 /* gen rndis */
424 cppi41_set_dma_mode(cppi41_channel,
425 EP_MODE_DMA_GEN_RNDIS);
426
427 /* auto req */
428 cppi41_set_autoreq_mode(cppi41_channel,
429 EP_MODE_AUTOREG_ALL_NEOP);
430 } else {
431 musb_writel(musb->ctrl_base,
432 RNDIS_REG(cppi41_channel->port_num), 0);
433 cppi41_set_dma_mode(cppi41_channel,
434 EP_MODE_DMA_TRANSPARENT);
435 cppi41_set_autoreq_mode(cppi41_channel,
436 EP_MODE_AUTOREG_NONE);
437 }
438 } else {
439 /* fallback mode */
440 cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
441 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREG_NONE);
442 len = min_t(u32, packet_sz, len);
443 }
444 cppi41_channel->prog_len = len;
445 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
446 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
447 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
448 if (!dma_desc)
449 return false;
450
451 dma_desc->callback = cppi41_dma_callback;
452 dma_desc->callback_param = channel;
453 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200454 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200455
456 save_rx_toggle(cppi41_channel);
457 dma_async_issue_pending(dc);
458 return true;
459}
460
461static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
462 struct musb_hw_ep *hw_ep, u8 is_tx)
463{
464 struct cppi41_dma_controller *controller = container_of(c,
465 struct cppi41_dma_controller, controller);
466 struct cppi41_dma_channel *cppi41_channel = NULL;
467 u8 ch_num = hw_ep->epnum - 1;
468
469 if (ch_num >= MUSB_DMA_NUM_CHANNELS)
470 return NULL;
471
472 if (is_tx)
473 cppi41_channel = &controller->tx_channel[ch_num];
474 else
475 cppi41_channel = &controller->rx_channel[ch_num];
476
477 if (!cppi41_channel->dc)
478 return NULL;
479
480 if (cppi41_channel->is_allocated)
481 return NULL;
482
483 cppi41_channel->hw_ep = hw_ep;
484 cppi41_channel->is_allocated = 1;
485
486 return &cppi41_channel->channel;
487}
488
489static void cppi41_dma_channel_release(struct dma_channel *channel)
490{
491 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
492
493 if (cppi41_channel->is_allocated) {
494 cppi41_channel->is_allocated = 0;
495 channel->status = MUSB_DMA_STATUS_FREE;
496 channel->actual_len = 0;
497 }
498}
499
500static int cppi41_dma_channel_program(struct dma_channel *channel,
501 u16 packet_sz, u8 mode,
502 dma_addr_t dma_addr, u32 len)
503{
504 int ret;
George Cherianf82503f2014-01-27 15:07:25 +0530505 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
506 int hb_mult = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200507
508 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
509 channel->status == MUSB_DMA_STATUS_BUSY);
510
George Cherianf82503f2014-01-27 15:07:25 +0530511 if (is_host_active(cppi41_channel->controller->musb)) {
512 if (cppi41_channel->is_tx)
513 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
514 else
515 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
516 }
517
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200518 channel->status = MUSB_DMA_STATUS_BUSY;
519 channel->actual_len = 0;
George Cherianf82503f2014-01-27 15:07:25 +0530520
521 if (hb_mult)
522 packet_sz = hb_mult * (packet_sz & 0x7FF);
523
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200524 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
525 if (!ret)
526 channel->status = MUSB_DMA_STATUS_FREE;
527
528 return ret;
529}
530
531static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
532 void *buf, u32 length)
533{
534 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
535 struct cppi41_dma_controller *controller = cppi41_channel->controller;
536 struct musb *musb = controller->musb;
537
538 if (is_host_active(musb)) {
539 WARN_ON(1);
540 return 1;
541 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100542 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
543 return 0;
Sebastian Andrzej Siewior13266fe2013-08-13 19:38:24 +0200544 if (cppi41_channel->is_tx)
545 return 1;
546 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200547 return 0;
548}
549
550static int cppi41_dma_channel_abort(struct dma_channel *channel)
551{
552 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
553 struct cppi41_dma_controller *controller = cppi41_channel->controller;
554 struct musb *musb = controller->musb;
555 void __iomem *epio = cppi41_channel->hw_ep->regs;
556 int tdbit;
557 int ret;
558 unsigned is_tx;
559 u16 csr;
560
561 is_tx = cppi41_channel->is_tx;
562 dev_dbg(musb->controller, "abort channel=%d, is_tx=%d\n",
563 cppi41_channel->port_num, is_tx);
564
565 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
566 return 0;
567
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100568 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200569 if (is_tx) {
570 csr = musb_readw(epio, MUSB_TXCSR);
571 csr &= ~MUSB_TXCSR_DMAENAB;
572 musb_writew(epio, MUSB_TXCSR, csr);
573 } else {
574 csr = musb_readw(epio, MUSB_RXCSR);
575 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
576 musb_writew(epio, MUSB_RXCSR, csr);
577
578 csr = musb_readw(epio, MUSB_RXCSR);
579 if (csr & MUSB_RXCSR_RXPKTRDY) {
580 csr |= MUSB_RXCSR_FLUSHFIFO;
581 musb_writew(epio, MUSB_RXCSR, csr);
582 musb_writew(epio, MUSB_RXCSR, csr);
583 }
584 }
585
586 tdbit = 1 << cppi41_channel->port_num;
587 if (is_tx)
588 tdbit <<= 16;
589
590 do {
591 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
592 ret = dmaengine_terminate_all(cppi41_channel->dc);
593 } while (ret == -EAGAIN);
594
595 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
596
597 if (is_tx) {
598 csr = musb_readw(epio, MUSB_TXCSR);
599 if (csr & MUSB_TXCSR_TXPKTRDY) {
600 csr |= MUSB_TXCSR_FLUSHFIFO;
601 musb_writew(epio, MUSB_TXCSR, csr);
602 }
603 }
604
605 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
606 return 0;
607}
608
609static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
610{
611 struct dma_chan *dc;
612 int i;
613
614 for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
615 dc = ctrl->tx_channel[i].dc;
616 if (dc)
617 dma_release_channel(dc);
618 dc = ctrl->rx_channel[i].dc;
619 if (dc)
620 dma_release_channel(dc);
621 }
622}
623
624static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
625{
626 cppi41_release_all_dma_chans(controller);
627}
628
629static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
630{
631 struct musb *musb = controller->musb;
632 struct device *dev = musb->controller;
633 struct device_node *np = dev->of_node;
634 struct cppi41_dma_channel *cppi41_channel;
635 int count;
636 int i;
637 int ret;
638
639 count = of_property_count_strings(np, "dma-names");
640 if (count < 0)
641 return count;
642
643 for (i = 0; i < count; i++) {
644 struct dma_chan *dc;
645 struct dma_channel *musb_dma;
646 const char *str;
647 unsigned is_tx;
648 unsigned int port;
649
650 ret = of_property_read_string_index(np, "dma-names", i, &str);
651 if (ret)
652 goto err;
653 if (!strncmp(str, "tx", 2))
654 is_tx = 1;
655 else if (!strncmp(str, "rx", 2))
656 is_tx = 0;
657 else {
658 dev_err(dev, "Wrong dmatype %s\n", str);
659 goto err;
660 }
661 ret = kstrtouint(str + 2, 0, &port);
662 if (ret)
663 goto err;
664
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200665 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200666 if (port > MUSB_DMA_NUM_CHANNELS || !port)
667 goto err;
668 if (is_tx)
669 cppi41_channel = &controller->tx_channel[port - 1];
670 else
671 cppi41_channel = &controller->rx_channel[port - 1];
672
673 cppi41_channel->controller = controller;
674 cppi41_channel->port_num = port;
675 cppi41_channel->is_tx = is_tx;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100676 INIT_LIST_HEAD(&cppi41_channel->tx_check);
George Cherian1af54b72014-01-27 15:07:26 +0530677 INIT_WORK(&cppi41_channel->dma_completion,
678 cppi_trans_done_work);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200679
680 musb_dma = &cppi41_channel->channel;
681 musb_dma->private_data = cppi41_channel;
682 musb_dma->status = MUSB_DMA_STATUS_FREE;
683 musb_dma->max_len = SZ_4M;
684
685 dc = dma_request_slave_channel(dev, str);
686 if (!dc) {
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +0530687 dev_err(dev, "Failed to request %s.\n", str);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200688 ret = -EPROBE_DEFER;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200689 goto err;
690 }
691 cppi41_channel->dc = dc;
692 }
693 return 0;
694err:
695 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200696 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200697}
698
699void dma_controller_destroy(struct dma_controller *c)
700{
701 struct cppi41_dma_controller *controller = container_of(c,
702 struct cppi41_dma_controller, controller);
703
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100704 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200705 cppi41_dma_controller_stop(controller);
706 kfree(controller);
707}
708
709struct dma_controller *dma_controller_create(struct musb *musb,
710 void __iomem *base)
711{
712 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200713 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200714
715 if (!musb->controller->of_node) {
716 dev_err(musb->controller, "Need DT for the DMA engine.\n");
717 return NULL;
718 }
719
720 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
721 if (!controller)
722 goto kzalloc_fail;
723
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100724 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
725 controller->early_tx.function = cppi41_recheck_tx_req;
726 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200727 controller->musb = musb;
728
729 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
730 controller->controller.channel_release = cppi41_dma_channel_release;
731 controller->controller.channel_program = cppi41_dma_channel_program;
732 controller->controller.channel_abort = cppi41_dma_channel_abort;
733 controller->controller.is_compatible = cppi41_is_compatible;
734
735 ret = cppi41_dma_controller_start(controller);
736 if (ret)
737 goto plat_get_fail;
738 return &controller->controller;
739
740plat_get_fail:
741 kfree(controller);
742kzalloc_fail:
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200743 if (ret == -EPROBE_DEFER)
744 return ERR_PTR(ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200745 return NULL;
746}