blob: 7fbb8a3071457a3d650eb588469104f7dbe7012b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +02002#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
Bin Liu239d2212016-06-30 12:12:29 -05009#include "cppi_dma.h"
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020010#include "musb_core.h"
Bin Liu8ccb49d2016-06-30 12:12:30 -050011#include "musb_trace.h"
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020012
13#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
14
Bin Liu0149b072015-01-26 16:22:06 -060015#define EP_MODE_AUTOREQ_NONE 0
16#define EP_MODE_AUTOREQ_ALL_NEOP 1
17#define EP_MODE_AUTOREQ_ALWAYS 3
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020018
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
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020028#define MUSB_DMA_NUM_CHANNELS 15
29
Alexandre Bailone10c5b02017-10-09 22:46:10 -050030#define DA8XX_USB_MODE 0x10
Alexandre Bailonbfa53e02017-10-09 22:46:09 -050031#define DA8XX_USB_AUTOREQ 0x14
32#define DA8XX_USB_TEARDOWN 0x1c
33
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050034#define DA8XX_DMA_NUM_CHANNELS 4
35
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020036struct cppi41_dma_controller {
37 struct dma_controller controller;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050038 struct cppi41_dma_channel *rx_channel;
39 struct cppi41_dma_channel *tx_channel;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010040 struct hrtimer early_tx;
41 struct list_head early_tx_list;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020042 u32 rx_mode;
43 u32 tx_mode;
44 u32 auto_req;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -050045
46 u32 tdown_reg;
47 u32 autoreq_reg;
Alexandre Bailone10c5b02017-10-09 22:46:10 -050048
49 void (*set_dma_mode)(struct cppi41_dma_channel *cppi41_channel,
50 unsigned int mode);
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050051 u8 num_channels;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020052};
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;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -060061 if (!is_host_active(cppi41_channel->controller->controller.musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020062 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{
Daniel Mackf50e6782014-05-26 14:52:39 +020072 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
73 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020074 u16 csr;
75 u8 toggle;
76
77 if (cppi41_channel->is_tx)
78 return;
Daniel Mackf50e6782014-05-26 14:52:39 +020079 if (!is_host_active(musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020080 return;
81
Daniel Mackf50e6782014-05-26 14:52:39 +020082 musb_ep_select(musb->mregs, hw_ep->epnum);
83 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020084 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);
Alexandre Bailon995ee0e2017-02-06 22:53:54 -060094 musb_dbg(musb, "Restoring DATA1 toggle.");
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020095 }
96
97 cppi41_channel->usb_toggle = toggle;
98}
99
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100100static 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
Daniel Mackf50e6782014-05-26 14:52:39 +0200107 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100108 csr = musb_readw(epio, MUSB_TXCSR);
109 if (csr & MUSB_TXCSR_TXPKTRDY)
110 return false;
111 return true;
112}
113
Alexandre Bailoned232c02017-02-06 22:53:52 -0600114static void cppi41_dma_callback(void *private_data,
115 const struct dmaengine_result *result);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100116
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100117static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200118{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200119 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
120 struct musb *musb = hw_ep->musb;
Bin Liu9267eda2014-08-12 14:18:43 -0500121 void __iomem *epio = hw_ep->regs;
122 u16 csr;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200123
George Cherianaecbc312014-02-27 10:44:41 +0530124 if (!cppi41_channel->prog_len ||
125 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200126
127 /* done, complete */
128 cppi41_channel->channel.actual_len =
129 cppi41_channel->transferred;
130 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200131 cppi41_channel->channel.rx_packet_done = true;
Bin Liu9267eda2014-08-12 14:18:43 -0500132
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 }
Bin Liu8ccb49d2016-06-30 12:12:30 -0500143
144 trace_musb_cppi41_done(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200145 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;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200151 u32 remain_bytes;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200152
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
Alexandre Bailoned232c02017-02-06 22:53:52 -0600170 dma_desc->callback_result = 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);
Bin Liu8ccb49d2016-06-30 12:12:30 -0500173 trace_musb_cppi41_cont(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200174 dma_async_issue_pending(dc);
175
176 if (!cppi41_channel->is_tx) {
Daniel Mackf50e6782014-05-26 14:52:39 +0200177 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200178 csr = musb_readw(epio, MUSB_RXCSR);
179 csr |= MUSB_RXCSR_H_REQPKT;
180 musb_writew(epio, MUSB_RXCSR, csr);
181 }
182 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100183}
184
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100185static 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);
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600195 musb = controller->controller.musb;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100196
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
Thomas Gleixnerd2e6d622014-10-02 17:32:16 +0200210 if (!list_empty(&controller->early_tx_list) &&
211 !hrtimer_is_queued(&controller->early_tx)) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100212 ret = HRTIMER_RESTART;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100213 hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100214 }
215
216 spin_unlock_irqrestore(&musb->lock, flags);
217 return ret;
218}
219
Alexandre Bailoned232c02017-02-06 22:53:52 -0600220static void cppi41_dma_callback(void *private_data,
221 const struct dmaengine_result *result)
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100222{
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;
Felipe Balbi1b616252015-02-27 13:19:39 -0600226 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100227 struct musb *musb = hw_ep->musb;
228 unsigned long flags;
229 struct dma_tx_state txstate;
230 u32 transferred;
Felipe Balbi1b616252015-02-27 13:19:39 -0600231 int is_hs = 0;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100232 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100233
Alexandre Bailon050dc902017-02-06 22:53:51 -0600234 controller = cppi41_channel->controller;
235 if (controller->controller.dma_callback)
236 controller->controller.dma_callback(&controller->controller);
237
Alexandre Bailoned232c02017-02-06 22:53:52 -0600238 if (result->result == DMA_TRANS_ABORTED)
239 return;
240
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100241 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
Bin Liu8ccb49d2016-06-30 12:12:30 -0500248 trace_musb_cppi41_gb(cppi41_channel);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100249 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
Bin Liu00901142017-03-10 14:43:35 -0600255 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 }
Takeyoshi Kikuchi72a472d2015-03-02 11:03:51 +0900276
277 if (!cppi41_channel->is_tx || empty) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100278 cppi41_trans_done(cppi41_channel);
Felipe Balbi1b616252015-02-27 13:19:39 -0600279 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 than 25us and setup a timer on
290 * FS to check for the bit and complete the transfer.
291 */
Felipe Balbi1b616252015-02-27 13:19:39 -0600292 if (is_host_active(musb)) {
293 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
294 is_hs = 1;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100295 } else {
Felipe Balbi1b616252015-02-27 13:19:39 -0600296 if (musb->g.speed == USB_SPEED_HIGH)
297 is_hs = 1;
298 }
299 if (is_hs) {
300 unsigned wait = 25;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100301
Felipe Balbi1b616252015-02-27 13:19:39 -0600302 do {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100303 empty = musb_is_tx_fifo_empty(hw_ep);
Felipe Balbiaf634292015-02-27 13:21:14 -0600304 if (empty) {
305 cppi41_trans_done(cppi41_channel);
306 goto out;
307 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600308 wait--;
309 if (!wait)
310 break;
Felipe Balbi043f5b72015-02-27 13:22:27 -0600311 cpu_relax();
Felipe Balbi1b616252015-02-27 13:19:39 -0600312 } while (1);
Felipe Balbi1b616252015-02-27 13:19:39 -0600313 }
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,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100320 usecs * NSEC_PER_USEC,
321 20 * NSEC_PER_USEC,
322 HRTIMER_MODE_REL);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100323 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600324
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100325out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200326 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;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600343 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200344 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;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600359 musb_writel(musb->ctrl_base, USB_CTRL_TX_MODE, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200360 } else {
361 controller->rx_mode = new_mode;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600362 musb_writel(musb->ctrl_base, USB_CTRL_RX_MODE, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200363 }
364}
365
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500366static 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
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200392static 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;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500407 musb_writel(controller->controller.musb->ctrl_base,
408 controller->autoreq_reg, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200409}
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;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500416 struct cppi41_dma_controller *controller = cppi41_channel->controller;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200417 struct dma_chan *dc = cppi41_channel->dc;
418 struct dma_async_tx_descriptor *dma_desc;
419 enum dma_transfer_direction direction;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600420 struct musb *musb = cppi41_channel->controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200421 unsigned use_gen_rndis = 0;
422
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200423 cppi41_channel->buf_addr = dma_addr;
424 cppi41_channel->total_len = len;
425 cppi41_channel->transferred = 0;
426 cppi41_channel->packet_sz = packet_sz;
Bin Liu9267eda2014-08-12 14:18:43 -0500427 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200428
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 */
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500442 controller->set_dma_mode(cppi41_channel,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200443 EP_MODE_DMA_GEN_RNDIS);
444
445 /* auto req */
446 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600447 EP_MODE_AUTOREQ_ALL_NEOP);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200448 } else {
449 musb_writel(musb->ctrl_base,
450 RNDIS_REG(cppi41_channel->port_num), 0);
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500451 controller->set_dma_mode(cppi41_channel,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200452 EP_MODE_DMA_TRANSPARENT);
453 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600454 EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200455 }
456 } else {
457 /* fallback mode */
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500458 controller->set_dma_mode(cppi41_channel,
459 EP_MODE_DMA_TRANSPARENT);
Bin Liu0149b072015-01-26 16:22:06 -0600460 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200461 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
Alexandre Bailoned232c02017-02-06 22:53:52 -0600470 dma_desc->callback_result = cppi41_dma_callback;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200471 dma_desc->callback_param = channel;
472 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200473 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200474
Bin Liu8ccb49d2016-06-30 12:12:30 -0500475 trace_musb_cppi41_config(cppi41_channel);
476
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200477 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
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500490 if (ch_num >= controller->num_channels)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200491 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
Bin Liu8ccb49d2016-06-30 12:12:30 -0500507 trace_musb_cppi41_alloc(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200508 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
Bin Liu8ccb49d2016-06-30 12:12:30 -0500515 trace_musb_cppi41_free(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200516 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;
George Cherianf82503f2014-01-27 15:07:25 +0530528 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
529 int hb_mult = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200530
531 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
532 channel->status == MUSB_DMA_STATUS_BUSY);
533
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600534 if (is_host_active(cppi41_channel->controller->controller.musb)) {
George Cherianf82503f2014-01-27 15:07:25 +0530535 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
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200541 channel->status = MUSB_DMA_STATUS_BUSY;
542 channel->actual_len = 0;
George Cherianf82503f2014-01-27 15:07:25 +0530543
544 if (hb_mult)
545 packet_sz = hb_mult * (packet_sz & 0x7FF);
546
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200547 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;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600559 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200560
561 if (is_host_active(musb)) {
562 WARN_ON(1);
563 return 1;
564 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100565 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
566 return 0;
Sebastian Andrzej Siewior13266fe2013-08-13 19:38:24 +0200567 if (cppi41_channel->is_tx)
568 return 1;
569 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200570 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;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600577 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200578 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;
Bin Liu8ccb49d2016-06-30 12:12:30 -0500585 trace_musb_cppi41_abort(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200586
587 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
588 return 0;
589
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100590 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200591 if (is_tx) {
592 csr = musb_readw(epio, MUSB_TXCSR);
593 csr &= ~MUSB_TXCSR_DMAENAB;
594 musb_writew(epio, MUSB_TXCSR, csr);
595 } else {
Bin Liucb83df72015-01-26 16:22:07 -0600596 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
597
Bin Liub431ba82015-08-24 15:28:37 -0500598 /* delay to drain to cppi dma pipeline for isoch */
599 udelay(250);
600
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200601 csr = musb_readw(epio, MUSB_RXCSR);
602 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
603 musb_writew(epio, MUSB_RXCSR, csr);
604
Bin Liucb83df72015-01-26 16:22:07 -0600605 /* wait to drain cppi dma pipe line */
606 udelay(50);
607
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200608 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
Alexandre Bailon593bc462017-04-16 23:21:18 -0500616 /* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
Bin Liudc8fca62018-05-21 08:42:13 -0500617 if (musb->ops->quirks & MUSB_DA8XX)
Alexandre Bailon593bc462017-04-16 23:21:18 -0500618 mdelay(250);
619
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200620 tdbit = 1 << cppi41_channel->port_num;
621 if (is_tx)
622 tdbit <<= 16;
623
624 do {
Bin Liucb83df72015-01-26 16:22:07 -0600625 if (is_tx)
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500626 musb_writel(musb->ctrl_base, controller->tdown_reg,
627 tdbit);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200628 ret = dmaengine_terminate_all(cppi41_channel->dc);
629 } while (ret == -EAGAIN);
630
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200631 if (is_tx) {
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500632 musb_writel(musb->ctrl_base, controller->tdown_reg, tdbit);
Bin Liucb83df72015-01-26 16:22:07 -0600633
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200634 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
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500650 for (i = 0; i < ctrl->num_channels; i++) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200651 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{
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600667 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200668 struct device *dev = musb->controller;
Felipe Balbib0a688d2015-08-06 10:51:29 -0500669 struct device_node *np = dev->parent->of_node;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200670 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;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100689 if (strstarts(str, "tx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200690 is_tx = 1;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100691 else if (strstarts(str, "rx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200692 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
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200701 ret = -EINVAL;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500702 if (port > controller->num_channels || !port)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200703 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;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100712 INIT_LIST_HEAD(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200713
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
Alexandre Bailona70df142017-06-16 10:40:54 -0500719 dc = dma_request_chan(dev->parent, str);
720 if (IS_ERR(dc)) {
721 ret = PTR_ERR(dc);
722 if (ret != -EPROBE_DEFER)
723 dev_err(dev, "Failed to request %s: %d.\n",
724 str, ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200725 goto err;
726 }
Alexandre Bailona70df142017-06-16 10:40:54 -0500727
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200728 cppi41_channel->dc = dc;
729 }
730 return 0;
731err:
732 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200733 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200734}
735
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700736void cppi41_dma_controller_destroy(struct dma_controller *c)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200737{
738 struct cppi41_dma_controller *controller = container_of(c,
739 struct cppi41_dma_controller, controller);
740
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100741 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200742 cppi41_dma_controller_stop(controller);
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500743 kfree(controller->rx_channel);
744 kfree(controller->tx_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200745 kfree(controller);
746}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700747EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200748
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700749struct dma_controller *
750cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200751{
752 struct cppi41_dma_controller *controller;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500753 int channel_size;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200754 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200755
Felipe Balbib0a688d2015-08-06 10:51:29 -0500756 if (!musb->controller->parent->of_node) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200757 dev_err(musb->controller, "Need DT for the DMA engine.\n");
758 return NULL;
759 }
760
761 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
762 if (!controller)
763 goto kzalloc_fail;
764
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100765 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
766 controller->early_tx.function = cppi41_recheck_tx_req;
767 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200768
769 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
770 controller->controller.channel_release = cppi41_dma_channel_release;
771 controller->controller.channel_program = cppi41_dma_channel_program;
772 controller->controller.channel_abort = cppi41_dma_channel_abort;
773 controller->controller.is_compatible = cppi41_is_compatible;
Alexandre Bailon050dc902017-02-06 22:53:51 -0600774 controller->controller.musb = musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200775
Bin Liudc8fca62018-05-21 08:42:13 -0500776 if (musb->ops->quirks & MUSB_DA8XX) {
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500777 controller->tdown_reg = DA8XX_USB_TEARDOWN;
778 controller->autoreq_reg = DA8XX_USB_AUTOREQ;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500779 controller->set_dma_mode = da8xx_set_dma_mode;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500780 controller->num_channels = DA8XX_DMA_NUM_CHANNELS;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500781 } else {
782 controller->tdown_reg = USB_TDOWN;
783 controller->autoreq_reg = USB_CTRL_AUTOREQ;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500784 controller->set_dma_mode = cppi41_set_dma_mode;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500785 controller->num_channels = MUSB_DMA_NUM_CHANNELS;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500786 }
787
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500788 channel_size = controller->num_channels *
789 sizeof(struct cppi41_dma_channel);
790 controller->rx_channel = kzalloc(channel_size, GFP_KERNEL);
791 if (!controller->rx_channel)
792 goto rx_channel_alloc_fail;
793 controller->tx_channel = kzalloc(channel_size, GFP_KERNEL);
794 if (!controller->tx_channel)
795 goto tx_channel_alloc_fail;
796
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200797 ret = cppi41_dma_controller_start(controller);
798 if (ret)
799 goto plat_get_fail;
800 return &controller->controller;
801
802plat_get_fail:
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500803 kfree(controller->tx_channel);
804tx_channel_alloc_fail:
805 kfree(controller->rx_channel);
806rx_channel_alloc_fail:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200807 kfree(controller);
808kzalloc_fail:
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200809 if (ret == -EPROBE_DEFER)
810 return ERR_PTR(ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200811 return NULL;
812}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700813EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);