blob: 602a230af65504633bf42ed2ed5d34eb1b8f6547 [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
Bin Liu239d2212016-06-30 12:12:29 -05008#include "cppi_dma.h"
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02009#include "musb_core.h"
10
11#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
12
Bin Liu0149b072015-01-26 16:22:06 -060013#define EP_MODE_AUTOREQ_NONE 0
14#define EP_MODE_AUTOREQ_ALL_NEOP 1
15#define EP_MODE_AUTOREQ_ALWAYS 3
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020016
17#define EP_MODE_DMA_TRANSPARENT 0
18#define EP_MODE_DMA_RNDIS 1
19#define EP_MODE_DMA_GEN_RNDIS 3
20
21#define USB_CTRL_TX_MODE 0x70
22#define USB_CTRL_RX_MODE 0x74
23#define USB_CTRL_AUTOREQ 0xd0
24#define USB_TDOWN 0xd8
25
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020026#define MUSB_DMA_NUM_CHANNELS 15
27
28struct cppi41_dma_controller {
29 struct dma_controller controller;
30 struct cppi41_dma_channel rx_channel[MUSB_DMA_NUM_CHANNELS];
31 struct cppi41_dma_channel tx_channel[MUSB_DMA_NUM_CHANNELS];
32 struct musb *musb;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010033 struct hrtimer early_tx;
34 struct list_head early_tx_list;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020035 u32 rx_mode;
36 u32 tx_mode;
37 u32 auto_req;
38};
39
40static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
41{
42 u16 csr;
43 u8 toggle;
44
45 if (cppi41_channel->is_tx)
46 return;
47 if (!is_host_active(cppi41_channel->controller->musb))
48 return;
49
50 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
51 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
52
53 cppi41_channel->usb_toggle = toggle;
54}
55
56static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
57{
Daniel Mackf50e6782014-05-26 14:52:39 +020058 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
59 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020060 u16 csr;
61 u8 toggle;
62
63 if (cppi41_channel->is_tx)
64 return;
Daniel Mackf50e6782014-05-26 14:52:39 +020065 if (!is_host_active(musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020066 return;
67
Daniel Mackf50e6782014-05-26 14:52:39 +020068 musb_ep_select(musb->mregs, hw_ep->epnum);
69 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020070 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
71
72 /*
73 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
74 * data toggle may reset from DATA1 to DATA0 during receiving data from
75 * more than one endpoint.
76 */
77 if (!toggle && toggle == cppi41_channel->usb_toggle) {
78 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
79 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
Bin Liub99d3652016-06-30 12:12:22 -050080 musb_dbg(cppi41_channel->controller->musb,
81 "Restoring DATA1 toggle.");
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020082 }
83
84 cppi41_channel->usb_toggle = toggle;
85}
86
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010087static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
88{
89 u8 epnum = hw_ep->epnum;
90 struct musb *musb = hw_ep->musb;
91 void __iomem *epio = musb->endpoints[epnum].regs;
92 u16 csr;
93
Daniel Mackf50e6782014-05-26 14:52:39 +020094 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010095 csr = musb_readw(epio, MUSB_TXCSR);
96 if (csr & MUSB_TXCSR_TXPKTRDY)
97 return false;
98 return true;
99}
100
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100101static void cppi41_dma_callback(void *private_data);
102
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100103static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200104{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200105 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
106 struct musb *musb = hw_ep->musb;
Bin Liu9267eda2014-08-12 14:18:43 -0500107 void __iomem *epio = hw_ep->regs;
108 u16 csr;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200109
George Cherianaecbc312014-02-27 10:44:41 +0530110 if (!cppi41_channel->prog_len ||
111 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200112
113 /* done, complete */
114 cppi41_channel->channel.actual_len =
115 cppi41_channel->transferred;
116 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200117 cppi41_channel->channel.rx_packet_done = true;
Bin Liu9267eda2014-08-12 14:18:43 -0500118
119 /*
120 * transmit ZLP using PIO mode for transfers which size is
121 * multiple of EP packet size.
122 */
123 if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
124 cppi41_channel->packet_sz) == 0) {
125 musb_ep_select(musb->mregs, hw_ep->epnum);
126 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
127 musb_writew(epio, MUSB_TXCSR, csr);
128 }
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200129 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
130 } else {
131 /* next iteration, reload */
132 struct dma_chan *dc = cppi41_channel->dc;
133 struct dma_async_tx_descriptor *dma_desc;
134 enum dma_transfer_direction direction;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200135 u32 remain_bytes;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200136
137 cppi41_channel->buf_addr += cppi41_channel->packet_sz;
138
139 remain_bytes = cppi41_channel->total_len;
140 remain_bytes -= cppi41_channel->transferred;
141 remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
142 cppi41_channel->prog_len = remain_bytes;
143
144 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
145 : DMA_DEV_TO_MEM;
146 dma_desc = dmaengine_prep_slave_single(dc,
147 cppi41_channel->buf_addr,
148 remain_bytes,
149 direction,
150 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100151 if (WARN_ON(!dma_desc))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200152 return;
153
154 dma_desc->callback = cppi41_dma_callback;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100155 dma_desc->callback_param = &cppi41_channel->channel;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200156 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
157 dma_async_issue_pending(dc);
158
159 if (!cppi41_channel->is_tx) {
Daniel Mackf50e6782014-05-26 14:52:39 +0200160 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200161 csr = musb_readw(epio, MUSB_RXCSR);
162 csr |= MUSB_RXCSR_H_REQPKT;
163 musb_writew(epio, MUSB_RXCSR, csr);
164 }
165 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100166}
167
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100168static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
169{
170 struct cppi41_dma_controller *controller;
171 struct cppi41_dma_channel *cppi41_channel, *n;
172 struct musb *musb;
173 unsigned long flags;
174 enum hrtimer_restart ret = HRTIMER_NORESTART;
175
176 controller = container_of(timer, struct cppi41_dma_controller,
177 early_tx);
178 musb = controller->musb;
179
180 spin_lock_irqsave(&musb->lock, flags);
181 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
182 tx_check) {
183 bool empty;
184 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
185
186 empty = musb_is_tx_fifo_empty(hw_ep);
187 if (empty) {
188 list_del_init(&cppi41_channel->tx_check);
189 cppi41_trans_done(cppi41_channel);
190 }
191 }
192
Thomas Gleixnerd2e6d622014-10-02 17:32:16 +0200193 if (!list_empty(&controller->early_tx_list) &&
194 !hrtimer_is_queued(&controller->early_tx)) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100195 ret = HRTIMER_RESTART;
196 hrtimer_forward_now(&controller->early_tx,
Daniel Macka5e4aa42014-09-03 17:21:24 +0200197 ktime_set(0, 20 * NSEC_PER_USEC));
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100198 }
199
200 spin_unlock_irqrestore(&musb->lock, flags);
201 return ret;
202}
203
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100204static void cppi41_dma_callback(void *private_data)
205{
206 struct dma_channel *channel = private_data;
207 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
208 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
Felipe Balbi1b616252015-02-27 13:19:39 -0600209 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100210 struct musb *musb = hw_ep->musb;
211 unsigned long flags;
212 struct dma_tx_state txstate;
213 u32 transferred;
Felipe Balbi1b616252015-02-27 13:19:39 -0600214 int is_hs = 0;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100215 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100216
217 spin_lock_irqsave(&musb->lock, flags);
218
219 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
220 &txstate);
221 transferred = cppi41_channel->prog_len - txstate.residue;
222 cppi41_channel->transferred += transferred;
223
Bin Liub99d3652016-06-30 12:12:22 -0500224 musb_dbg(musb, "DMA transfer done on hw_ep=%d bytes=%d/%d",
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100225 hw_ep->epnum, cppi41_channel->transferred,
226 cppi41_channel->total_len);
227
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
Takeyoshi Kikuchi72a472d2015-03-02 11:03:51 +0900234 if (cppi41_channel->is_tx)
235 empty = musb_is_tx_fifo_empty(hw_ep);
236
237 if (!cppi41_channel->is_tx || empty) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100238 cppi41_trans_done(cppi41_channel);
Felipe Balbi1b616252015-02-27 13:19:39 -0600239 goto out;
240 }
241
242 /*
243 * On AM335x it has been observed that the TX interrupt fires
244 * too early that means the TXFIFO is not yet empty but the DMA
245 * engine says that it is done with the transfer. We don't
246 * receive a FIFO empty interrupt so the only thing we can do is
247 * to poll for the bit. On HS it usually takes 2us, on FS around
248 * 110us - 150us depending on the transfer size.
249 * We spin on HS (no longer than than 25us and setup a timer on
250 * FS to check for the bit and complete the transfer.
251 */
252 controller = cppi41_channel->controller;
253
254 if (is_host_active(musb)) {
255 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
256 is_hs = 1;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100257 } else {
Felipe Balbi1b616252015-02-27 13:19:39 -0600258 if (musb->g.speed == USB_SPEED_HIGH)
259 is_hs = 1;
260 }
261 if (is_hs) {
262 unsigned wait = 25;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100263
Felipe Balbi1b616252015-02-27 13:19:39 -0600264 do {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100265 empty = musb_is_tx_fifo_empty(hw_ep);
Felipe Balbiaf634292015-02-27 13:21:14 -0600266 if (empty) {
267 cppi41_trans_done(cppi41_channel);
268 goto out;
269 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600270 wait--;
271 if (!wait)
272 break;
Felipe Balbi043f5b72015-02-27 13:22:27 -0600273 cpu_relax();
Felipe Balbi1b616252015-02-27 13:19:39 -0600274 } while (1);
Felipe Balbi1b616252015-02-27 13:19:39 -0600275 }
276 list_add_tail(&cppi41_channel->tx_check,
277 &controller->early_tx_list);
278 if (!hrtimer_is_queued(&controller->early_tx)) {
279 unsigned long usecs = cppi41_channel->total_len / 10;
280
281 hrtimer_start_range_ns(&controller->early_tx,
Daniel Mack50aea6f2014-06-20 00:20:44 +0200282 ktime_set(0, usecs * NSEC_PER_USEC),
Daniel Macka5e4aa42014-09-03 17:21:24 +0200283 20 * NSEC_PER_USEC,
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100284 HRTIMER_MODE_REL);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100285 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600286
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100287out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200288 spin_unlock_irqrestore(&musb->lock, flags);
289}
290
291static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
292{
293 unsigned shift;
294
295 shift = (ep - 1) * 2;
296 old &= ~(3 << shift);
297 old |= mode << shift;
298 return old;
299}
300
301static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
302 unsigned mode)
303{
304 struct cppi41_dma_controller *controller = cppi41_channel->controller;
305 u32 port;
306 u32 new_mode;
307 u32 old_mode;
308
309 if (cppi41_channel->is_tx)
310 old_mode = controller->tx_mode;
311 else
312 old_mode = controller->rx_mode;
313 port = cppi41_channel->port_num;
314 new_mode = update_ep_mode(port, mode, old_mode);
315
316 if (new_mode == old_mode)
317 return;
318 if (cppi41_channel->is_tx) {
319 controller->tx_mode = new_mode;
320 musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
321 new_mode);
322 } else {
323 controller->rx_mode = new_mode;
324 musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
325 new_mode);
326 }
327}
328
329static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
330 unsigned mode)
331{
332 struct cppi41_dma_controller *controller = cppi41_channel->controller;
333 u32 port;
334 u32 new_mode;
335 u32 old_mode;
336
337 old_mode = controller->auto_req;
338 port = cppi41_channel->port_num;
339 new_mode = update_ep_mode(port, mode, old_mode);
340
341 if (new_mode == old_mode)
342 return;
343 controller->auto_req = new_mode;
344 musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
345}
346
347static bool cppi41_configure_channel(struct dma_channel *channel,
348 u16 packet_sz, u8 mode,
349 dma_addr_t dma_addr, u32 len)
350{
351 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
352 struct dma_chan *dc = cppi41_channel->dc;
353 struct dma_async_tx_descriptor *dma_desc;
354 enum dma_transfer_direction direction;
355 struct musb *musb = cppi41_channel->controller->musb;
356 unsigned use_gen_rndis = 0;
357
Bin Liub99d3652016-06-30 12:12:22 -0500358 musb_dbg(musb,
359 "configure ep%d/%x packet_sz=%d, mode=%d, dma_addr=0x%llx, len=%d is_tx=%d",
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200360 cppi41_channel->port_num, RNDIS_REG(cppi41_channel->port_num),
361 packet_sz, mode, (unsigned long long) dma_addr,
362 len, cppi41_channel->is_tx);
363
364 cppi41_channel->buf_addr = dma_addr;
365 cppi41_channel->total_len = len;
366 cppi41_channel->transferred = 0;
367 cppi41_channel->packet_sz = packet_sz;
Bin Liu9267eda2014-08-12 14:18:43 -0500368 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200369
370 /*
371 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
372 * than max packet size at a time.
373 */
374 if (cppi41_channel->is_tx)
375 use_gen_rndis = 1;
376
377 if (use_gen_rndis) {
378 /* RNDIS mode */
379 if (len > packet_sz) {
380 musb_writel(musb->ctrl_base,
381 RNDIS_REG(cppi41_channel->port_num), len);
382 /* gen rndis */
383 cppi41_set_dma_mode(cppi41_channel,
384 EP_MODE_DMA_GEN_RNDIS);
385
386 /* auto req */
387 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600388 EP_MODE_AUTOREQ_ALL_NEOP);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200389 } else {
390 musb_writel(musb->ctrl_base,
391 RNDIS_REG(cppi41_channel->port_num), 0);
392 cppi41_set_dma_mode(cppi41_channel,
393 EP_MODE_DMA_TRANSPARENT);
394 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600395 EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200396 }
397 } else {
398 /* fallback mode */
399 cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
Bin Liu0149b072015-01-26 16:22:06 -0600400 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200401 len = min_t(u32, packet_sz, len);
402 }
403 cppi41_channel->prog_len = len;
404 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
405 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
406 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
407 if (!dma_desc)
408 return false;
409
410 dma_desc->callback = cppi41_dma_callback;
411 dma_desc->callback_param = channel;
412 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200413 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200414
415 save_rx_toggle(cppi41_channel);
416 dma_async_issue_pending(dc);
417 return true;
418}
419
420static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
421 struct musb_hw_ep *hw_ep, u8 is_tx)
422{
423 struct cppi41_dma_controller *controller = container_of(c,
424 struct cppi41_dma_controller, controller);
425 struct cppi41_dma_channel *cppi41_channel = NULL;
426 u8 ch_num = hw_ep->epnum - 1;
427
428 if (ch_num >= MUSB_DMA_NUM_CHANNELS)
429 return NULL;
430
431 if (is_tx)
432 cppi41_channel = &controller->tx_channel[ch_num];
433 else
434 cppi41_channel = &controller->rx_channel[ch_num];
435
436 if (!cppi41_channel->dc)
437 return NULL;
438
439 if (cppi41_channel->is_allocated)
440 return NULL;
441
442 cppi41_channel->hw_ep = hw_ep;
443 cppi41_channel->is_allocated = 1;
444
445 return &cppi41_channel->channel;
446}
447
448static void cppi41_dma_channel_release(struct dma_channel *channel)
449{
450 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
451
452 if (cppi41_channel->is_allocated) {
453 cppi41_channel->is_allocated = 0;
454 channel->status = MUSB_DMA_STATUS_FREE;
455 channel->actual_len = 0;
456 }
457}
458
459static int cppi41_dma_channel_program(struct dma_channel *channel,
460 u16 packet_sz, u8 mode,
461 dma_addr_t dma_addr, u32 len)
462{
463 int ret;
George Cherianf82503f2014-01-27 15:07:25 +0530464 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
465 int hb_mult = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200466
467 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
468 channel->status == MUSB_DMA_STATUS_BUSY);
469
George Cherianf82503f2014-01-27 15:07:25 +0530470 if (is_host_active(cppi41_channel->controller->musb)) {
471 if (cppi41_channel->is_tx)
472 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
473 else
474 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
475 }
476
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200477 channel->status = MUSB_DMA_STATUS_BUSY;
478 channel->actual_len = 0;
George Cherianf82503f2014-01-27 15:07:25 +0530479
480 if (hb_mult)
481 packet_sz = hb_mult * (packet_sz & 0x7FF);
482
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200483 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
484 if (!ret)
485 channel->status = MUSB_DMA_STATUS_FREE;
486
487 return ret;
488}
489
490static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
491 void *buf, u32 length)
492{
493 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
494 struct cppi41_dma_controller *controller = cppi41_channel->controller;
495 struct musb *musb = controller->musb;
496
497 if (is_host_active(musb)) {
498 WARN_ON(1);
499 return 1;
500 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100501 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
502 return 0;
Sebastian Andrzej Siewior13266fe2013-08-13 19:38:24 +0200503 if (cppi41_channel->is_tx)
504 return 1;
505 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200506 return 0;
507}
508
509static int cppi41_dma_channel_abort(struct dma_channel *channel)
510{
511 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
512 struct cppi41_dma_controller *controller = cppi41_channel->controller;
513 struct musb *musb = controller->musb;
514 void __iomem *epio = cppi41_channel->hw_ep->regs;
515 int tdbit;
516 int ret;
517 unsigned is_tx;
518 u16 csr;
519
520 is_tx = cppi41_channel->is_tx;
Bin Liub99d3652016-06-30 12:12:22 -0500521 musb_dbg(musb, "abort channel=%d, is_tx=%d",
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200522 cppi41_channel->port_num, is_tx);
523
524 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
525 return 0;
526
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100527 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200528 if (is_tx) {
529 csr = musb_readw(epio, MUSB_TXCSR);
530 csr &= ~MUSB_TXCSR_DMAENAB;
531 musb_writew(epio, MUSB_TXCSR, csr);
532 } else {
Bin Liucb83df72015-01-26 16:22:07 -0600533 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
534
Bin Liub431ba82015-08-24 15:28:37 -0500535 /* delay to drain to cppi dma pipeline for isoch */
536 udelay(250);
537
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200538 csr = musb_readw(epio, MUSB_RXCSR);
539 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
540 musb_writew(epio, MUSB_RXCSR, csr);
541
Bin Liucb83df72015-01-26 16:22:07 -0600542 /* wait to drain cppi dma pipe line */
543 udelay(50);
544
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200545 csr = musb_readw(epio, MUSB_RXCSR);
546 if (csr & MUSB_RXCSR_RXPKTRDY) {
547 csr |= MUSB_RXCSR_FLUSHFIFO;
548 musb_writew(epio, MUSB_RXCSR, csr);
549 musb_writew(epio, MUSB_RXCSR, csr);
550 }
551 }
552
553 tdbit = 1 << cppi41_channel->port_num;
554 if (is_tx)
555 tdbit <<= 16;
556
557 do {
Bin Liucb83df72015-01-26 16:22:07 -0600558 if (is_tx)
559 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200560 ret = dmaengine_terminate_all(cppi41_channel->dc);
561 } while (ret == -EAGAIN);
562
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200563 if (is_tx) {
Bin Liucb83df72015-01-26 16:22:07 -0600564 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
565
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200566 csr = musb_readw(epio, MUSB_TXCSR);
567 if (csr & MUSB_TXCSR_TXPKTRDY) {
568 csr |= MUSB_TXCSR_FLUSHFIFO;
569 musb_writew(epio, MUSB_TXCSR, csr);
570 }
571 }
572
573 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
574 return 0;
575}
576
577static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
578{
579 struct dma_chan *dc;
580 int i;
581
582 for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
583 dc = ctrl->tx_channel[i].dc;
584 if (dc)
585 dma_release_channel(dc);
586 dc = ctrl->rx_channel[i].dc;
587 if (dc)
588 dma_release_channel(dc);
589 }
590}
591
592static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
593{
594 cppi41_release_all_dma_chans(controller);
595}
596
597static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
598{
599 struct musb *musb = controller->musb;
600 struct device *dev = musb->controller;
Felipe Balbib0a688d2015-08-06 10:51:29 -0500601 struct device_node *np = dev->parent->of_node;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200602 struct cppi41_dma_channel *cppi41_channel;
603 int count;
604 int i;
605 int ret;
606
607 count = of_property_count_strings(np, "dma-names");
608 if (count < 0)
609 return count;
610
611 for (i = 0; i < count; i++) {
612 struct dma_chan *dc;
613 struct dma_channel *musb_dma;
614 const char *str;
615 unsigned is_tx;
616 unsigned int port;
617
618 ret = of_property_read_string_index(np, "dma-names", i, &str);
619 if (ret)
620 goto err;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100621 if (strstarts(str, "tx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200622 is_tx = 1;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100623 else if (strstarts(str, "rx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200624 is_tx = 0;
625 else {
626 dev_err(dev, "Wrong dmatype %s\n", str);
627 goto err;
628 }
629 ret = kstrtouint(str + 2, 0, &port);
630 if (ret)
631 goto err;
632
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200633 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200634 if (port > MUSB_DMA_NUM_CHANNELS || !port)
635 goto err;
636 if (is_tx)
637 cppi41_channel = &controller->tx_channel[port - 1];
638 else
639 cppi41_channel = &controller->rx_channel[port - 1];
640
641 cppi41_channel->controller = controller;
642 cppi41_channel->port_num = port;
643 cppi41_channel->is_tx = is_tx;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100644 INIT_LIST_HEAD(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200645
646 musb_dma = &cppi41_channel->channel;
647 musb_dma->private_data = cppi41_channel;
648 musb_dma->status = MUSB_DMA_STATUS_FREE;
649 musb_dma->max_len = SZ_4M;
650
Felipe Balbib0a688d2015-08-06 10:51:29 -0500651 dc = dma_request_slave_channel(dev->parent, str);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200652 if (!dc) {
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +0530653 dev_err(dev, "Failed to request %s.\n", str);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200654 ret = -EPROBE_DEFER;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200655 goto err;
656 }
657 cppi41_channel->dc = dc;
658 }
659 return 0;
660err:
661 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200662 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200663}
664
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700665void cppi41_dma_controller_destroy(struct dma_controller *c)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200666{
667 struct cppi41_dma_controller *controller = container_of(c,
668 struct cppi41_dma_controller, controller);
669
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100670 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200671 cppi41_dma_controller_stop(controller);
672 kfree(controller);
673}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700674EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200675
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700676struct dma_controller *
677cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200678{
679 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200680 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200681
Felipe Balbib0a688d2015-08-06 10:51:29 -0500682 if (!musb->controller->parent->of_node) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200683 dev_err(musb->controller, "Need DT for the DMA engine.\n");
684 return NULL;
685 }
686
687 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
688 if (!controller)
689 goto kzalloc_fail;
690
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100691 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
692 controller->early_tx.function = cppi41_recheck_tx_req;
693 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200694 controller->musb = musb;
695
696 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
697 controller->controller.channel_release = cppi41_dma_channel_release;
698 controller->controller.channel_program = cppi41_dma_channel_program;
699 controller->controller.channel_abort = cppi41_dma_channel_abort;
700 controller->controller.is_compatible = cppi41_is_compatible;
701
702 ret = cppi41_dma_controller_start(controller);
703 if (ret)
704 goto plat_get_fail;
705 return &controller->controller;
706
707plat_get_fail:
708 kfree(controller);
709kzalloc_fail:
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200710 if (ret == -EPROBE_DEFER)
711 return ERR_PTR(ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200712 return NULL;
713}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700714EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);