blob: f7d3d27fd2c1ec9563f7e55dc1f0a2b4bc089ef3 [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"
Bin Liu8ccb49d2016-06-30 12:12:30 -050010#include "musb_trace.h"
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020011
12#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
13
Bin Liu0149b072015-01-26 16:22:06 -060014#define EP_MODE_AUTOREQ_NONE 0
15#define EP_MODE_AUTOREQ_ALL_NEOP 1
16#define EP_MODE_AUTOREQ_ALWAYS 3
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020017
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
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020027#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;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010034 struct hrtimer early_tx;
35 struct list_head early_tx_list;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020036 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{
Daniel Mackf50e6782014-05-26 14:52:39 +020059 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
60 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020061 u16 csr;
62 u8 toggle;
63
64 if (cppi41_channel->is_tx)
65 return;
Daniel Mackf50e6782014-05-26 14:52:39 +020066 if (!is_host_active(musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020067 return;
68
Daniel Mackf50e6782014-05-26 14:52:39 +020069 musb_ep_select(musb->mregs, hw_ep->epnum);
70 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020071 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);
Bin Liub99d3652016-06-30 12:12:22 -050081 musb_dbg(cppi41_channel->controller->musb,
82 "Restoring DATA1 toggle.");
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020083 }
84
85 cppi41_channel->usb_toggle = toggle;
86}
87
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010088static 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
Daniel Mackf50e6782014-05-26 14:52:39 +020095 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010096 csr = musb_readw(epio, MUSB_TXCSR);
97 if (csr & MUSB_TXCSR_TXPKTRDY)
98 return false;
99 return true;
100}
101
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100102static void cppi41_dma_callback(void *private_data);
103
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100104static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200105{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200106 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
107 struct musb *musb = hw_ep->musb;
Bin Liu9267eda2014-08-12 14:18:43 -0500108 void __iomem *epio = hw_ep->regs;
109 u16 csr;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200110
George Cherianaecbc312014-02-27 10:44:41 +0530111 if (!cppi41_channel->prog_len ||
112 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200113
114 /* done, complete */
115 cppi41_channel->channel.actual_len =
116 cppi41_channel->transferred;
117 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200118 cppi41_channel->channel.rx_packet_done = true;
Bin Liu9267eda2014-08-12 14:18:43 -0500119
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 }
Bin Liu8ccb49d2016-06-30 12:12:30 -0500130
131 trace_musb_cppi41_done(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200132 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;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200138 u32 remain_bytes;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200139
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);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100154 if (WARN_ON(!dma_desc))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200155 return;
156
157 dma_desc->callback = cppi41_dma_callback;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100158 dma_desc->callback_param = &cppi41_channel->channel;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200159 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Bin Liu8ccb49d2016-06-30 12:12:30 -0500160 trace_musb_cppi41_cont(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200161 dma_async_issue_pending(dc);
162
163 if (!cppi41_channel->is_tx) {
Daniel Mackf50e6782014-05-26 14:52:39 +0200164 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200165 csr = musb_readw(epio, MUSB_RXCSR);
166 csr |= MUSB_RXCSR_H_REQPKT;
167 musb_writew(epio, MUSB_RXCSR, csr);
168 }
169 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100170}
171
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100172static 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
Thomas Gleixnerd2e6d622014-10-02 17:32:16 +0200197 if (!list_empty(&controller->early_tx_list) &&
198 !hrtimer_is_queued(&controller->early_tx)) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100199 ret = HRTIMER_RESTART;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100200 hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100201 }
202
203 spin_unlock_irqrestore(&musb->lock, flags);
204 return ret;
205}
206
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100207static 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;
Felipe Balbi1b616252015-02-27 13:19:39 -0600212 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100213 struct musb *musb = hw_ep->musb;
214 unsigned long flags;
215 struct dma_tx_state txstate;
216 u32 transferred;
Felipe Balbi1b616252015-02-27 13:19:39 -0600217 int is_hs = 0;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100218 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100219
Alexandre Bailon050dc902017-02-06 22:53:51 -0600220 controller = cppi41_channel->controller;
221 if (controller->controller.dma_callback)
222 controller->controller.dma_callback(&controller->controller);
223
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100224 spin_lock_irqsave(&musb->lock, flags);
225
226 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
227 &txstate);
228 transferred = cppi41_channel->prog_len - txstate.residue;
229 cppi41_channel->transferred += transferred;
230
Bin Liu8ccb49d2016-06-30 12:12:30 -0500231 trace_musb_cppi41_gb(cppi41_channel);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100232 update_rx_toggle(cppi41_channel);
233
234 if (cppi41_channel->transferred == cppi41_channel->total_len ||
235 transferred < cppi41_channel->packet_sz)
236 cppi41_channel->prog_len = 0;
237
Takeyoshi Kikuchi72a472d2015-03-02 11:03:51 +0900238 if (cppi41_channel->is_tx)
239 empty = musb_is_tx_fifo_empty(hw_ep);
240
241 if (!cppi41_channel->is_tx || empty) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100242 cppi41_trans_done(cppi41_channel);
Felipe Balbi1b616252015-02-27 13:19:39 -0600243 goto out;
244 }
245
246 /*
247 * On AM335x it has been observed that the TX interrupt fires
248 * too early that means the TXFIFO is not yet empty but the DMA
249 * engine says that it is done with the transfer. We don't
250 * receive a FIFO empty interrupt so the only thing we can do is
251 * to poll for the bit. On HS it usually takes 2us, on FS around
252 * 110us - 150us depending on the transfer size.
253 * We spin on HS (no longer than than 25us and setup a timer on
254 * FS to check for the bit and complete the transfer.
255 */
Felipe Balbi1b616252015-02-27 13:19:39 -0600256 if (is_host_active(musb)) {
257 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
258 is_hs = 1;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100259 } else {
Felipe Balbi1b616252015-02-27 13:19:39 -0600260 if (musb->g.speed == USB_SPEED_HIGH)
261 is_hs = 1;
262 }
263 if (is_hs) {
264 unsigned wait = 25;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100265
Felipe Balbi1b616252015-02-27 13:19:39 -0600266 do {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100267 empty = musb_is_tx_fifo_empty(hw_ep);
Felipe Balbiaf634292015-02-27 13:21:14 -0600268 if (empty) {
269 cppi41_trans_done(cppi41_channel);
270 goto out;
271 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600272 wait--;
273 if (!wait)
274 break;
Felipe Balbi043f5b72015-02-27 13:22:27 -0600275 cpu_relax();
Felipe Balbi1b616252015-02-27 13:19:39 -0600276 } while (1);
Felipe Balbi1b616252015-02-27 13:19:39 -0600277 }
278 list_add_tail(&cppi41_channel->tx_check,
279 &controller->early_tx_list);
280 if (!hrtimer_is_queued(&controller->early_tx)) {
281 unsigned long usecs = cppi41_channel->total_len / 10;
282
283 hrtimer_start_range_ns(&controller->early_tx,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100284 usecs * NSEC_PER_USEC,
285 20 * NSEC_PER_USEC,
286 HRTIMER_MODE_REL);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100287 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600288
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100289out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200290 spin_unlock_irqrestore(&musb->lock, flags);
291}
292
293static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
294{
295 unsigned shift;
296
297 shift = (ep - 1) * 2;
298 old &= ~(3 << shift);
299 old |= mode << shift;
300 return old;
301}
302
303static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
304 unsigned mode)
305{
306 struct cppi41_dma_controller *controller = cppi41_channel->controller;
307 u32 port;
308 u32 new_mode;
309 u32 old_mode;
310
311 if (cppi41_channel->is_tx)
312 old_mode = controller->tx_mode;
313 else
314 old_mode = controller->rx_mode;
315 port = cppi41_channel->port_num;
316 new_mode = update_ep_mode(port, mode, old_mode);
317
318 if (new_mode == old_mode)
319 return;
320 if (cppi41_channel->is_tx) {
321 controller->tx_mode = new_mode;
322 musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
323 new_mode);
324 } else {
325 controller->rx_mode = new_mode;
326 musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
327 new_mode);
328 }
329}
330
331static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
332 unsigned mode)
333{
334 struct cppi41_dma_controller *controller = cppi41_channel->controller;
335 u32 port;
336 u32 new_mode;
337 u32 old_mode;
338
339 old_mode = controller->auto_req;
340 port = cppi41_channel->port_num;
341 new_mode = update_ep_mode(port, mode, old_mode);
342
343 if (new_mode == old_mode)
344 return;
345 controller->auto_req = new_mode;
346 musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
347}
348
349static bool cppi41_configure_channel(struct dma_channel *channel,
350 u16 packet_sz, u8 mode,
351 dma_addr_t dma_addr, u32 len)
352{
353 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
354 struct dma_chan *dc = cppi41_channel->dc;
355 struct dma_async_tx_descriptor *dma_desc;
356 enum dma_transfer_direction direction;
357 struct musb *musb = cppi41_channel->controller->musb;
358 unsigned use_gen_rndis = 0;
359
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200360 cppi41_channel->buf_addr = dma_addr;
361 cppi41_channel->total_len = len;
362 cppi41_channel->transferred = 0;
363 cppi41_channel->packet_sz = packet_sz;
Bin Liu9267eda2014-08-12 14:18:43 -0500364 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200365
366 /*
367 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
368 * than max packet size at a time.
369 */
370 if (cppi41_channel->is_tx)
371 use_gen_rndis = 1;
372
373 if (use_gen_rndis) {
374 /* RNDIS mode */
375 if (len > packet_sz) {
376 musb_writel(musb->ctrl_base,
377 RNDIS_REG(cppi41_channel->port_num), len);
378 /* gen rndis */
379 cppi41_set_dma_mode(cppi41_channel,
380 EP_MODE_DMA_GEN_RNDIS);
381
382 /* auto req */
383 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600384 EP_MODE_AUTOREQ_ALL_NEOP);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200385 } else {
386 musb_writel(musb->ctrl_base,
387 RNDIS_REG(cppi41_channel->port_num), 0);
388 cppi41_set_dma_mode(cppi41_channel,
389 EP_MODE_DMA_TRANSPARENT);
390 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600391 EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200392 }
393 } else {
394 /* fallback mode */
395 cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
Bin Liu0149b072015-01-26 16:22:06 -0600396 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200397 len = min_t(u32, packet_sz, len);
398 }
399 cppi41_channel->prog_len = len;
400 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
401 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
402 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
403 if (!dma_desc)
404 return false;
405
406 dma_desc->callback = cppi41_dma_callback;
407 dma_desc->callback_param = channel;
408 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200409 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200410
Bin Liu8ccb49d2016-06-30 12:12:30 -0500411 trace_musb_cppi41_config(cppi41_channel);
412
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200413 save_rx_toggle(cppi41_channel);
414 dma_async_issue_pending(dc);
415 return true;
416}
417
418static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
419 struct musb_hw_ep *hw_ep, u8 is_tx)
420{
421 struct cppi41_dma_controller *controller = container_of(c,
422 struct cppi41_dma_controller, controller);
423 struct cppi41_dma_channel *cppi41_channel = NULL;
424 u8 ch_num = hw_ep->epnum - 1;
425
426 if (ch_num >= MUSB_DMA_NUM_CHANNELS)
427 return NULL;
428
429 if (is_tx)
430 cppi41_channel = &controller->tx_channel[ch_num];
431 else
432 cppi41_channel = &controller->rx_channel[ch_num];
433
434 if (!cppi41_channel->dc)
435 return NULL;
436
437 if (cppi41_channel->is_allocated)
438 return NULL;
439
440 cppi41_channel->hw_ep = hw_ep;
441 cppi41_channel->is_allocated = 1;
442
Bin Liu8ccb49d2016-06-30 12:12:30 -0500443 trace_musb_cppi41_alloc(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200444 return &cppi41_channel->channel;
445}
446
447static void cppi41_dma_channel_release(struct dma_channel *channel)
448{
449 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
450
Bin Liu8ccb49d2016-06-30 12:12:30 -0500451 trace_musb_cppi41_free(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200452 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 Liu8ccb49d2016-06-30 12:12:30 -0500521 trace_musb_cppi41_abort(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200522
523 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
524 return 0;
525
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100526 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200527 if (is_tx) {
528 csr = musb_readw(epio, MUSB_TXCSR);
529 csr &= ~MUSB_TXCSR_DMAENAB;
530 musb_writew(epio, MUSB_TXCSR, csr);
531 } else {
Bin Liucb83df72015-01-26 16:22:07 -0600532 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
533
Bin Liub431ba82015-08-24 15:28:37 -0500534 /* delay to drain to cppi dma pipeline for isoch */
535 udelay(250);
536
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200537 csr = musb_readw(epio, MUSB_RXCSR);
538 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
539 musb_writew(epio, MUSB_RXCSR, csr);
540
Bin Liucb83df72015-01-26 16:22:07 -0600541 /* wait to drain cppi dma pipe line */
542 udelay(50);
543
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200544 csr = musb_readw(epio, MUSB_RXCSR);
545 if (csr & MUSB_RXCSR_RXPKTRDY) {
546 csr |= MUSB_RXCSR_FLUSHFIFO;
547 musb_writew(epio, MUSB_RXCSR, csr);
548 musb_writew(epio, MUSB_RXCSR, csr);
549 }
550 }
551
552 tdbit = 1 << cppi41_channel->port_num;
553 if (is_tx)
554 tdbit <<= 16;
555
556 do {
Bin Liucb83df72015-01-26 16:22:07 -0600557 if (is_tx)
558 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200559 ret = dmaengine_terminate_all(cppi41_channel->dc);
560 } while (ret == -EAGAIN);
561
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200562 if (is_tx) {
Bin Liucb83df72015-01-26 16:22:07 -0600563 musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
564
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200565 csr = musb_readw(epio, MUSB_TXCSR);
566 if (csr & MUSB_TXCSR_TXPKTRDY) {
567 csr |= MUSB_TXCSR_FLUSHFIFO;
568 musb_writew(epio, MUSB_TXCSR, csr);
569 }
570 }
571
572 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
573 return 0;
574}
575
576static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
577{
578 struct dma_chan *dc;
579 int i;
580
581 for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
582 dc = ctrl->tx_channel[i].dc;
583 if (dc)
584 dma_release_channel(dc);
585 dc = ctrl->rx_channel[i].dc;
586 if (dc)
587 dma_release_channel(dc);
588 }
589}
590
591static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
592{
593 cppi41_release_all_dma_chans(controller);
594}
595
596static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
597{
598 struct musb *musb = controller->musb;
599 struct device *dev = musb->controller;
Felipe Balbib0a688d2015-08-06 10:51:29 -0500600 struct device_node *np = dev->parent->of_node;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200601 struct cppi41_dma_channel *cppi41_channel;
602 int count;
603 int i;
604 int ret;
605
606 count = of_property_count_strings(np, "dma-names");
607 if (count < 0)
608 return count;
609
610 for (i = 0; i < count; i++) {
611 struct dma_chan *dc;
612 struct dma_channel *musb_dma;
613 const char *str;
614 unsigned is_tx;
615 unsigned int port;
616
617 ret = of_property_read_string_index(np, "dma-names", i, &str);
618 if (ret)
619 goto err;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100620 if (strstarts(str, "tx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200621 is_tx = 1;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100622 else if (strstarts(str, "rx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200623 is_tx = 0;
624 else {
625 dev_err(dev, "Wrong dmatype %s\n", str);
626 goto err;
627 }
628 ret = kstrtouint(str + 2, 0, &port);
629 if (ret)
630 goto err;
631
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200632 ret = -EINVAL;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200633 if (port > MUSB_DMA_NUM_CHANNELS || !port)
634 goto err;
635 if (is_tx)
636 cppi41_channel = &controller->tx_channel[port - 1];
637 else
638 cppi41_channel = &controller->rx_channel[port - 1];
639
640 cppi41_channel->controller = controller;
641 cppi41_channel->port_num = port;
642 cppi41_channel->is_tx = is_tx;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100643 INIT_LIST_HEAD(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200644
645 musb_dma = &cppi41_channel->channel;
646 musb_dma->private_data = cppi41_channel;
647 musb_dma->status = MUSB_DMA_STATUS_FREE;
648 musb_dma->max_len = SZ_4M;
649
Felipe Balbib0a688d2015-08-06 10:51:29 -0500650 dc = dma_request_slave_channel(dev->parent, str);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200651 if (!dc) {
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +0530652 dev_err(dev, "Failed to request %s.\n", str);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200653 ret = -EPROBE_DEFER;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200654 goto err;
655 }
656 cppi41_channel->dc = dc;
657 }
658 return 0;
659err:
660 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200661 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200662}
663
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700664void cppi41_dma_controller_destroy(struct dma_controller *c)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200665{
666 struct cppi41_dma_controller *controller = container_of(c,
667 struct cppi41_dma_controller, controller);
668
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100669 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200670 cppi41_dma_controller_stop(controller);
671 kfree(controller);
672}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700673EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200674
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700675struct dma_controller *
676cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200677{
678 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200679 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200680
Felipe Balbib0a688d2015-08-06 10:51:29 -0500681 if (!musb->controller->parent->of_node) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200682 dev_err(musb->controller, "Need DT for the DMA engine.\n");
683 return NULL;
684 }
685
686 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
687 if (!controller)
688 goto kzalloc_fail;
689
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100690 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
691 controller->early_tx.function = cppi41_recheck_tx_req;
692 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200693 controller->musb = musb;
694
695 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
696 controller->controller.channel_release = cppi41_dma_channel_release;
697 controller->controller.channel_program = cppi41_dma_channel_program;
698 controller->controller.channel_abort = cppi41_dma_channel_abort;
699 controller->controller.is_compatible = cppi41_is_compatible;
Alexandre Bailon050dc902017-02-06 22:53:51 -0600700 controller->controller.musb = musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200701
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);