blob: 1ec0a4947b6b887414afd633e540e68470650113 [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
Alexandre Bailone10c5b02017-10-09 22:46:10 -050029#define DA8XX_USB_MODE 0x10
Alexandre Bailonbfa53e02017-10-09 22:46:09 -050030#define DA8XX_USB_AUTOREQ 0x14
31#define DA8XX_USB_TEARDOWN 0x1c
32
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050033#define DA8XX_DMA_NUM_CHANNELS 4
34
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020035struct cppi41_dma_controller {
36 struct dma_controller controller;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050037 struct cppi41_dma_channel *rx_channel;
38 struct cppi41_dma_channel *tx_channel;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010039 struct hrtimer early_tx;
40 struct list_head early_tx_list;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020041 u32 rx_mode;
42 u32 tx_mode;
43 u32 auto_req;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -050044
45 u32 tdown_reg;
46 u32 autoreq_reg;
Alexandre Bailone10c5b02017-10-09 22:46:10 -050047
48 void (*set_dma_mode)(struct cppi41_dma_channel *cppi41_channel,
49 unsigned int mode);
Alexandre Bailon297d7fe2017-10-09 22:46:11 -050050 u8 num_channels;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020051};
52
53static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
54{
55 u16 csr;
56 u8 toggle;
57
58 if (cppi41_channel->is_tx)
59 return;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -060060 if (!is_host_active(cppi41_channel->controller->controller.musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020061 return;
62
63 csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
64 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
65
66 cppi41_channel->usb_toggle = toggle;
67}
68
69static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
70{
Daniel Mackf50e6782014-05-26 14:52:39 +020071 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
72 struct musb *musb = hw_ep->musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020073 u16 csr;
74 u8 toggle;
75
76 if (cppi41_channel->is_tx)
77 return;
Daniel Mackf50e6782014-05-26 14:52:39 +020078 if (!is_host_active(musb))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020079 return;
80
Daniel Mackf50e6782014-05-26 14:52:39 +020081 musb_ep_select(musb->mregs, hw_ep->epnum);
82 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020083 toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
84
85 /*
86 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
87 * data toggle may reset from DATA1 to DATA0 during receiving data from
88 * more than one endpoint.
89 */
90 if (!toggle && toggle == cppi41_channel->usb_toggle) {
91 csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
92 musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
Alexandre Bailon995ee0e2017-02-06 22:53:54 -060093 musb_dbg(musb, "Restoring DATA1 toggle.");
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +020094 }
95
96 cppi41_channel->usb_toggle = toggle;
97}
98
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +010099static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
100{
101 u8 epnum = hw_ep->epnum;
102 struct musb *musb = hw_ep->musb;
103 void __iomem *epio = musb->endpoints[epnum].regs;
104 u16 csr;
105
Daniel Mackf50e6782014-05-26 14:52:39 +0200106 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100107 csr = musb_readw(epio, MUSB_TXCSR);
108 if (csr & MUSB_TXCSR_TXPKTRDY)
109 return false;
110 return true;
111}
112
Alexandre Bailoned232c02017-02-06 22:53:52 -0600113static void cppi41_dma_callback(void *private_data,
114 const struct dmaengine_result *result);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100115
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100116static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200117{
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200118 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
119 struct musb *musb = hw_ep->musb;
Bin Liu9267eda2014-08-12 14:18:43 -0500120 void __iomem *epio = hw_ep->regs;
121 u16 csr;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200122
George Cherianaecbc312014-02-27 10:44:41 +0530123 if (!cppi41_channel->prog_len ||
124 (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200125
126 /* done, complete */
127 cppi41_channel->channel.actual_len =
128 cppi41_channel->transferred;
129 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
Daniel Mackff3fcac2014-05-26 14:52:38 +0200130 cppi41_channel->channel.rx_packet_done = true;
Bin Liu9267eda2014-08-12 14:18:43 -0500131
132 /*
133 * transmit ZLP using PIO mode for transfers which size is
134 * multiple of EP packet size.
135 */
136 if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
137 cppi41_channel->packet_sz) == 0) {
138 musb_ep_select(musb->mregs, hw_ep->epnum);
139 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
140 musb_writew(epio, MUSB_TXCSR, csr);
141 }
Bin Liu8ccb49d2016-06-30 12:12:30 -0500142
143 trace_musb_cppi41_done(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200144 musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
145 } else {
146 /* next iteration, reload */
147 struct dma_chan *dc = cppi41_channel->dc;
148 struct dma_async_tx_descriptor *dma_desc;
149 enum dma_transfer_direction direction;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200150 u32 remain_bytes;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200151
152 cppi41_channel->buf_addr += cppi41_channel->packet_sz;
153
154 remain_bytes = cppi41_channel->total_len;
155 remain_bytes -= cppi41_channel->transferred;
156 remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
157 cppi41_channel->prog_len = remain_bytes;
158
159 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
160 : DMA_DEV_TO_MEM;
161 dma_desc = dmaengine_prep_slave_single(dc,
162 cppi41_channel->buf_addr,
163 remain_bytes,
164 direction,
165 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100166 if (WARN_ON(!dma_desc))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200167 return;
168
Alexandre Bailoned232c02017-02-06 22:53:52 -0600169 dma_desc->callback_result = cppi41_dma_callback;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100170 dma_desc->callback_param = &cppi41_channel->channel;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200171 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Bin Liu8ccb49d2016-06-30 12:12:30 -0500172 trace_musb_cppi41_cont(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200173 dma_async_issue_pending(dc);
174
175 if (!cppi41_channel->is_tx) {
Daniel Mackf50e6782014-05-26 14:52:39 +0200176 musb_ep_select(musb->mregs, hw_ep->epnum);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200177 csr = musb_readw(epio, MUSB_RXCSR);
178 csr |= MUSB_RXCSR_H_REQPKT;
179 musb_writew(epio, MUSB_RXCSR, csr);
180 }
181 }
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100182}
183
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100184static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
185{
186 struct cppi41_dma_controller *controller;
187 struct cppi41_dma_channel *cppi41_channel, *n;
188 struct musb *musb;
189 unsigned long flags;
190 enum hrtimer_restart ret = HRTIMER_NORESTART;
191
192 controller = container_of(timer, struct cppi41_dma_controller,
193 early_tx);
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600194 musb = controller->controller.musb;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100195
196 spin_lock_irqsave(&musb->lock, flags);
197 list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
198 tx_check) {
199 bool empty;
200 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
201
202 empty = musb_is_tx_fifo_empty(hw_ep);
203 if (empty) {
204 list_del_init(&cppi41_channel->tx_check);
205 cppi41_trans_done(cppi41_channel);
206 }
207 }
208
Thomas Gleixnerd2e6d622014-10-02 17:32:16 +0200209 if (!list_empty(&controller->early_tx_list) &&
210 !hrtimer_is_queued(&controller->early_tx)) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100211 ret = HRTIMER_RESTART;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100212 hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100213 }
214
215 spin_unlock_irqrestore(&musb->lock, flags);
216 return ret;
217}
218
Alexandre Bailoned232c02017-02-06 22:53:52 -0600219static void cppi41_dma_callback(void *private_data,
220 const struct dmaengine_result *result)
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100221{
222 struct dma_channel *channel = private_data;
223 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
224 struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
Felipe Balbi1b616252015-02-27 13:19:39 -0600225 struct cppi41_dma_controller *controller;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100226 struct musb *musb = hw_ep->musb;
227 unsigned long flags;
228 struct dma_tx_state txstate;
229 u32 transferred;
Felipe Balbi1b616252015-02-27 13:19:39 -0600230 int is_hs = 0;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100231 bool empty;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100232
Alexandre Bailon050dc902017-02-06 22:53:51 -0600233 controller = cppi41_channel->controller;
234 if (controller->controller.dma_callback)
235 controller->controller.dma_callback(&controller->controller);
236
Alexandre Bailoned232c02017-02-06 22:53:52 -0600237 if (result->result == DMA_TRANS_ABORTED)
238 return;
239
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100240 spin_lock_irqsave(&musb->lock, flags);
241
242 dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
243 &txstate);
244 transferred = cppi41_channel->prog_len - txstate.residue;
245 cppi41_channel->transferred += transferred;
246
Bin Liu8ccb49d2016-06-30 12:12:30 -0500247 trace_musb_cppi41_gb(cppi41_channel);
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100248 update_rx_toggle(cppi41_channel);
249
250 if (cppi41_channel->transferred == cppi41_channel->total_len ||
251 transferred < cppi41_channel->packet_sz)
252 cppi41_channel->prog_len = 0;
253
Bin Liu00901142017-03-10 14:43:35 -0600254 if (cppi41_channel->is_tx) {
255 u8 type;
256
257 if (is_host_active(musb))
258 type = hw_ep->out_qh->type;
259 else
260 type = hw_ep->ep_in.type;
261
262 if (type == USB_ENDPOINT_XFER_ISOC)
263 /*
264 * Don't use the early-TX-interrupt workaround below
265 * for Isoch transfter. Since Isoch are periodic
266 * transfer, by the time the next transfer is
267 * scheduled, the current one should be done already.
268 *
269 * This avoids audio playback underrun issue.
270 */
271 empty = true;
272 else
273 empty = musb_is_tx_fifo_empty(hw_ep);
274 }
Takeyoshi Kikuchi72a472d2015-03-02 11:03:51 +0900275
276 if (!cppi41_channel->is_tx || empty) {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100277 cppi41_trans_done(cppi41_channel);
Felipe Balbi1b616252015-02-27 13:19:39 -0600278 goto out;
279 }
280
281 /*
282 * On AM335x it has been observed that the TX interrupt fires
283 * too early that means the TXFIFO is not yet empty but the DMA
284 * engine says that it is done with the transfer. We don't
285 * receive a FIFO empty interrupt so the only thing we can do is
286 * to poll for the bit. On HS it usually takes 2us, on FS around
287 * 110us - 150us depending on the transfer size.
288 * We spin on HS (no longer than than 25us and setup a timer on
289 * FS to check for the bit and complete the transfer.
290 */
Felipe Balbi1b616252015-02-27 13:19:39 -0600291 if (is_host_active(musb)) {
292 if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
293 is_hs = 1;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100294 } else {
Felipe Balbi1b616252015-02-27 13:19:39 -0600295 if (musb->g.speed == USB_SPEED_HIGH)
296 is_hs = 1;
297 }
298 if (is_hs) {
299 unsigned wait = 25;
Sebastian Andrzej Siewiord373a852013-11-12 16:37:46 +0100300
Felipe Balbi1b616252015-02-27 13:19:39 -0600301 do {
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100302 empty = musb_is_tx_fifo_empty(hw_ep);
Felipe Balbiaf634292015-02-27 13:21:14 -0600303 if (empty) {
304 cppi41_trans_done(cppi41_channel);
305 goto out;
306 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600307 wait--;
308 if (!wait)
309 break;
Felipe Balbi043f5b72015-02-27 13:22:27 -0600310 cpu_relax();
Felipe Balbi1b616252015-02-27 13:19:39 -0600311 } while (1);
Felipe Balbi1b616252015-02-27 13:19:39 -0600312 }
313 list_add_tail(&cppi41_channel->tx_check,
314 &controller->early_tx_list);
315 if (!hrtimer_is_queued(&controller->early_tx)) {
316 unsigned long usecs = cppi41_channel->total_len / 10;
317
318 hrtimer_start_range_ns(&controller->early_tx,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100319 usecs * NSEC_PER_USEC,
320 20 * NSEC_PER_USEC,
321 HRTIMER_MODE_REL);
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100322 }
Felipe Balbi1b616252015-02-27 13:19:39 -0600323
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100324out:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200325 spin_unlock_irqrestore(&musb->lock, flags);
326}
327
328static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
329{
330 unsigned shift;
331
332 shift = (ep - 1) * 2;
333 old &= ~(3 << shift);
334 old |= mode << shift;
335 return old;
336}
337
338static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
339 unsigned mode)
340{
341 struct cppi41_dma_controller *controller = cppi41_channel->controller;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600342 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200343 u32 port;
344 u32 new_mode;
345 u32 old_mode;
346
347 if (cppi41_channel->is_tx)
348 old_mode = controller->tx_mode;
349 else
350 old_mode = controller->rx_mode;
351 port = cppi41_channel->port_num;
352 new_mode = update_ep_mode(port, mode, old_mode);
353
354 if (new_mode == old_mode)
355 return;
356 if (cppi41_channel->is_tx) {
357 controller->tx_mode = new_mode;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600358 musb_writel(musb->ctrl_base, USB_CTRL_TX_MODE, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200359 } else {
360 controller->rx_mode = new_mode;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600361 musb_writel(musb->ctrl_base, USB_CTRL_RX_MODE, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200362 }
363}
364
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500365static void da8xx_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
366 unsigned int mode)
367{
368 struct cppi41_dma_controller *controller = cppi41_channel->controller;
369 struct musb *musb = controller->controller.musb;
370 unsigned int shift;
371 u32 port;
372 u32 new_mode;
373 u32 old_mode;
374
375 old_mode = controller->tx_mode;
376 port = cppi41_channel->port_num;
377
378 shift = (port - 1) * 4;
379 if (!cppi41_channel->is_tx)
380 shift += 16;
381 new_mode = old_mode & ~(3 << shift);
382 new_mode |= mode << shift;
383
384 if (new_mode == old_mode)
385 return;
386 controller->tx_mode = new_mode;
387 musb_writel(musb->ctrl_base, DA8XX_USB_MODE, new_mode);
388}
389
390
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200391static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
392 unsigned mode)
393{
394 struct cppi41_dma_controller *controller = cppi41_channel->controller;
395 u32 port;
396 u32 new_mode;
397 u32 old_mode;
398
399 old_mode = controller->auto_req;
400 port = cppi41_channel->port_num;
401 new_mode = update_ep_mode(port, mode, old_mode);
402
403 if (new_mode == old_mode)
404 return;
405 controller->auto_req = new_mode;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500406 musb_writel(controller->controller.musb->ctrl_base,
407 controller->autoreq_reg, new_mode);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200408}
409
410static bool cppi41_configure_channel(struct dma_channel *channel,
411 u16 packet_sz, u8 mode,
412 dma_addr_t dma_addr, u32 len)
413{
414 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500415 struct cppi41_dma_controller *controller = cppi41_channel->controller;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200416 struct dma_chan *dc = cppi41_channel->dc;
417 struct dma_async_tx_descriptor *dma_desc;
418 enum dma_transfer_direction direction;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600419 struct musb *musb = cppi41_channel->controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200420 unsigned use_gen_rndis = 0;
421
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200422 cppi41_channel->buf_addr = dma_addr;
423 cppi41_channel->total_len = len;
424 cppi41_channel->transferred = 0;
425 cppi41_channel->packet_sz = packet_sz;
Bin Liu9267eda2014-08-12 14:18:43 -0500426 cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200427
428 /*
429 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
430 * than max packet size at a time.
431 */
432 if (cppi41_channel->is_tx)
433 use_gen_rndis = 1;
434
435 if (use_gen_rndis) {
436 /* RNDIS mode */
437 if (len > packet_sz) {
438 musb_writel(musb->ctrl_base,
439 RNDIS_REG(cppi41_channel->port_num), len);
440 /* gen rndis */
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500441 controller->set_dma_mode(cppi41_channel,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200442 EP_MODE_DMA_GEN_RNDIS);
443
444 /* auto req */
445 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600446 EP_MODE_AUTOREQ_ALL_NEOP);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200447 } else {
448 musb_writel(musb->ctrl_base,
449 RNDIS_REG(cppi41_channel->port_num), 0);
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500450 controller->set_dma_mode(cppi41_channel,
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200451 EP_MODE_DMA_TRANSPARENT);
452 cppi41_set_autoreq_mode(cppi41_channel,
Bin Liu0149b072015-01-26 16:22:06 -0600453 EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200454 }
455 } else {
456 /* fallback mode */
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500457 controller->set_dma_mode(cppi41_channel,
458 EP_MODE_DMA_TRANSPARENT);
Bin Liu0149b072015-01-26 16:22:06 -0600459 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200460 len = min_t(u32, packet_sz, len);
461 }
462 cppi41_channel->prog_len = len;
463 direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
464 dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
465 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
466 if (!dma_desc)
467 return false;
468
Alexandre Bailoned232c02017-02-06 22:53:52 -0600469 dma_desc->callback_result = cppi41_dma_callback;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200470 dma_desc->callback_param = channel;
471 cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
Daniel Mackff3fcac2014-05-26 14:52:38 +0200472 cppi41_channel->channel.rx_packet_done = false;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200473
Bin Liu8ccb49d2016-06-30 12:12:30 -0500474 trace_musb_cppi41_config(cppi41_channel);
475
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200476 save_rx_toggle(cppi41_channel);
477 dma_async_issue_pending(dc);
478 return true;
479}
480
481static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
482 struct musb_hw_ep *hw_ep, u8 is_tx)
483{
484 struct cppi41_dma_controller *controller = container_of(c,
485 struct cppi41_dma_controller, controller);
486 struct cppi41_dma_channel *cppi41_channel = NULL;
487 u8 ch_num = hw_ep->epnum - 1;
488
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500489 if (ch_num >= controller->num_channels)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200490 return NULL;
491
492 if (is_tx)
493 cppi41_channel = &controller->tx_channel[ch_num];
494 else
495 cppi41_channel = &controller->rx_channel[ch_num];
496
497 if (!cppi41_channel->dc)
498 return NULL;
499
500 if (cppi41_channel->is_allocated)
501 return NULL;
502
503 cppi41_channel->hw_ep = hw_ep;
504 cppi41_channel->is_allocated = 1;
505
Bin Liu8ccb49d2016-06-30 12:12:30 -0500506 trace_musb_cppi41_alloc(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200507 return &cppi41_channel->channel;
508}
509
510static void cppi41_dma_channel_release(struct dma_channel *channel)
511{
512 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
513
Bin Liu8ccb49d2016-06-30 12:12:30 -0500514 trace_musb_cppi41_free(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200515 if (cppi41_channel->is_allocated) {
516 cppi41_channel->is_allocated = 0;
517 channel->status = MUSB_DMA_STATUS_FREE;
518 channel->actual_len = 0;
519 }
520}
521
522static int cppi41_dma_channel_program(struct dma_channel *channel,
523 u16 packet_sz, u8 mode,
524 dma_addr_t dma_addr, u32 len)
525{
526 int ret;
George Cherianf82503f2014-01-27 15:07:25 +0530527 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
528 int hb_mult = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200529
530 BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
531 channel->status == MUSB_DMA_STATUS_BUSY);
532
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600533 if (is_host_active(cppi41_channel->controller->controller.musb)) {
George Cherianf82503f2014-01-27 15:07:25 +0530534 if (cppi41_channel->is_tx)
535 hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
536 else
537 hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
538 }
539
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200540 channel->status = MUSB_DMA_STATUS_BUSY;
541 channel->actual_len = 0;
George Cherianf82503f2014-01-27 15:07:25 +0530542
543 if (hb_mult)
544 packet_sz = hb_mult * (packet_sz & 0x7FF);
545
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200546 ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
547 if (!ret)
548 channel->status = MUSB_DMA_STATUS_FREE;
549
550 return ret;
551}
552
553static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
554 void *buf, u32 length)
555{
556 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
557 struct cppi41_dma_controller *controller = cppi41_channel->controller;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600558 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200559
560 if (is_host_active(musb)) {
561 WARN_ON(1);
562 return 1;
563 }
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100564 if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
565 return 0;
Sebastian Andrzej Siewior13266fe2013-08-13 19:38:24 +0200566 if (cppi41_channel->is_tx)
567 return 1;
568 /* AM335x Advisory 1.0.13. No workaround for device RX mode */
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200569 return 0;
570}
571
572static int cppi41_dma_channel_abort(struct dma_channel *channel)
573{
574 struct cppi41_dma_channel *cppi41_channel = channel->private_data;
575 struct cppi41_dma_controller *controller = cppi41_channel->controller;
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600576 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200577 void __iomem *epio = cppi41_channel->hw_ep->regs;
578 int tdbit;
579 int ret;
580 unsigned is_tx;
581 u16 csr;
582
583 is_tx = cppi41_channel->is_tx;
Bin Liu8ccb49d2016-06-30 12:12:30 -0500584 trace_musb_cppi41_abort(cppi41_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200585
586 if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
587 return 0;
588
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100589 list_del_init(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200590 if (is_tx) {
591 csr = musb_readw(epio, MUSB_TXCSR);
592 csr &= ~MUSB_TXCSR_DMAENAB;
593 musb_writew(epio, MUSB_TXCSR, csr);
594 } else {
Bin Liucb83df72015-01-26 16:22:07 -0600595 cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
596
Bin Liub431ba82015-08-24 15:28:37 -0500597 /* delay to drain to cppi dma pipeline for isoch */
598 udelay(250);
599
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200600 csr = musb_readw(epio, MUSB_RXCSR);
601 csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
602 musb_writew(epio, MUSB_RXCSR, csr);
603
Bin Liucb83df72015-01-26 16:22:07 -0600604 /* wait to drain cppi dma pipe line */
605 udelay(50);
606
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200607 csr = musb_readw(epio, MUSB_RXCSR);
608 if (csr & MUSB_RXCSR_RXPKTRDY) {
609 csr |= MUSB_RXCSR_FLUSHFIFO;
610 musb_writew(epio, MUSB_RXCSR, csr);
611 musb_writew(epio, MUSB_RXCSR, csr);
612 }
613 }
614
Alexandre Bailon593bc462017-04-16 23:21:18 -0500615 /* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
616 if (musb->io.quirks & MUSB_DA8XX)
617 mdelay(250);
618
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200619 tdbit = 1 << cppi41_channel->port_num;
620 if (is_tx)
621 tdbit <<= 16;
622
623 do {
Bin Liucb83df72015-01-26 16:22:07 -0600624 if (is_tx)
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500625 musb_writel(musb->ctrl_base, controller->tdown_reg,
626 tdbit);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200627 ret = dmaengine_terminate_all(cppi41_channel->dc);
628 } while (ret == -EAGAIN);
629
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200630 if (is_tx) {
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500631 musb_writel(musb->ctrl_base, controller->tdown_reg, tdbit);
Bin Liucb83df72015-01-26 16:22:07 -0600632
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200633 csr = musb_readw(epio, MUSB_TXCSR);
634 if (csr & MUSB_TXCSR_TXPKTRDY) {
635 csr |= MUSB_TXCSR_FLUSHFIFO;
636 musb_writew(epio, MUSB_TXCSR, csr);
637 }
638 }
639
640 cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
641 return 0;
642}
643
644static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
645{
646 struct dma_chan *dc;
647 int i;
648
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500649 for (i = 0; i < ctrl->num_channels; i++) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200650 dc = ctrl->tx_channel[i].dc;
651 if (dc)
652 dma_release_channel(dc);
653 dc = ctrl->rx_channel[i].dc;
654 if (dc)
655 dma_release_channel(dc);
656 }
657}
658
659static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
660{
661 cppi41_release_all_dma_chans(controller);
662}
663
664static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
665{
Alexandre Bailon995ee0e2017-02-06 22:53:54 -0600666 struct musb *musb = controller->controller.musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200667 struct device *dev = musb->controller;
Felipe Balbib0a688d2015-08-06 10:51:29 -0500668 struct device_node *np = dev->parent->of_node;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200669 struct cppi41_dma_channel *cppi41_channel;
670 int count;
671 int i;
672 int ret;
673
674 count = of_property_count_strings(np, "dma-names");
675 if (count < 0)
676 return count;
677
678 for (i = 0; i < count; i++) {
679 struct dma_chan *dc;
680 struct dma_channel *musb_dma;
681 const char *str;
682 unsigned is_tx;
683 unsigned int port;
684
685 ret = of_property_read_string_index(np, "dma-names", i, &str);
686 if (ret)
687 goto err;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100688 if (strstarts(str, "tx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200689 is_tx = 1;
Rasmus Villemoese87c3f82014-11-27 22:25:45 +0100690 else if (strstarts(str, "rx"))
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200691 is_tx = 0;
692 else {
693 dev_err(dev, "Wrong dmatype %s\n", str);
694 goto err;
695 }
696 ret = kstrtouint(str + 2, 0, &port);
697 if (ret)
698 goto err;
699
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200700 ret = -EINVAL;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500701 if (port > controller->num_channels || !port)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200702 goto err;
703 if (is_tx)
704 cppi41_channel = &controller->tx_channel[port - 1];
705 else
706 cppi41_channel = &controller->rx_channel[port - 1];
707
708 cppi41_channel->controller = controller;
709 cppi41_channel->port_num = port;
710 cppi41_channel->is_tx = is_tx;
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100711 INIT_LIST_HEAD(&cppi41_channel->tx_check);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200712
713 musb_dma = &cppi41_channel->channel;
714 musb_dma->private_data = cppi41_channel;
715 musb_dma->status = MUSB_DMA_STATUS_FREE;
716 musb_dma->max_len = SZ_4M;
717
Alexandre Bailona70df142017-06-16 10:40:54 -0500718 dc = dma_request_chan(dev->parent, str);
719 if (IS_ERR(dc)) {
720 ret = PTR_ERR(dc);
721 if (ret != -EPROBE_DEFER)
722 dev_err(dev, "Failed to request %s: %d.\n",
723 str, ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200724 goto err;
725 }
Alexandre Bailona70df142017-06-16 10:40:54 -0500726
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200727 cppi41_channel->dc = dc;
728 }
729 return 0;
730err:
731 cppi41_release_all_dma_chans(controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200732 return ret;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200733}
734
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700735void cppi41_dma_controller_destroy(struct dma_controller *c)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200736{
737 struct cppi41_dma_controller *controller = container_of(c,
738 struct cppi41_dma_controller, controller);
739
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100740 hrtimer_cancel(&controller->early_tx);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200741 cppi41_dma_controller_stop(controller);
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500742 kfree(controller->rx_channel);
743 kfree(controller->tx_channel);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200744 kfree(controller);
745}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700746EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200747
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700748struct dma_controller *
749cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200750{
751 struct cppi41_dma_controller *controller;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500752 int channel_size;
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200753 int ret = 0;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200754
Felipe Balbib0a688d2015-08-06 10:51:29 -0500755 if (!musb->controller->parent->of_node) {
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200756 dev_err(musb->controller, "Need DT for the DMA engine.\n");
757 return NULL;
758 }
759
760 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
761 if (!controller)
762 goto kzalloc_fail;
763
Sebastian Andrzej Siewiora655f482013-11-12 16:37:47 +0100764 hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
765 controller->early_tx.function = cppi41_recheck_tx_req;
766 INIT_LIST_HEAD(&controller->early_tx_list);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200767
768 controller->controller.channel_alloc = cppi41_dma_channel_allocate;
769 controller->controller.channel_release = cppi41_dma_channel_release;
770 controller->controller.channel_program = cppi41_dma_channel_program;
771 controller->controller.channel_abort = cppi41_dma_channel_abort;
772 controller->controller.is_compatible = cppi41_is_compatible;
Alexandre Bailon050dc902017-02-06 22:53:51 -0600773 controller->controller.musb = musb;
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200774
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500775 if (musb->io.quirks & MUSB_DA8XX) {
776 controller->tdown_reg = DA8XX_USB_TEARDOWN;
777 controller->autoreq_reg = DA8XX_USB_AUTOREQ;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500778 controller->set_dma_mode = da8xx_set_dma_mode;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500779 controller->num_channels = DA8XX_DMA_NUM_CHANNELS;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500780 } else {
781 controller->tdown_reg = USB_TDOWN;
782 controller->autoreq_reg = USB_CTRL_AUTOREQ;
Alexandre Bailone10c5b02017-10-09 22:46:10 -0500783 controller->set_dma_mode = cppi41_set_dma_mode;
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500784 controller->num_channels = MUSB_DMA_NUM_CHANNELS;
Alexandre Bailonbfa53e02017-10-09 22:46:09 -0500785 }
786
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500787 channel_size = controller->num_channels *
788 sizeof(struct cppi41_dma_channel);
789 controller->rx_channel = kzalloc(channel_size, GFP_KERNEL);
790 if (!controller->rx_channel)
791 goto rx_channel_alloc_fail;
792 controller->tx_channel = kzalloc(channel_size, GFP_KERNEL);
793 if (!controller->tx_channel)
794 goto tx_channel_alloc_fail;
795
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200796 ret = cppi41_dma_controller_start(controller);
797 if (ret)
798 goto plat_get_fail;
799 return &controller->controller;
800
801plat_get_fail:
Alexandre Bailon297d7fe2017-10-09 22:46:11 -0500802 kfree(controller->tx_channel);
803tx_channel_alloc_fail:
804 kfree(controller->rx_channel);
805rx_channel_alloc_fail:
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200806 kfree(controller);
807kzalloc_fail:
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +0200808 if (ret == -EPROBE_DEFER)
809 return ERR_PTR(ret);
Sebastian Andrzej Siewior9b3452d2013-06-20 12:13:04 +0200810 return NULL;
811}
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700812EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);