15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29b3452d1SSebastian Andrzej Siewior #include <linux/device.h>
39b3452d1SSebastian Andrzej Siewior #include <linux/dma-mapping.h>
49b3452d1SSebastian Andrzej Siewior #include <linux/dmaengine.h>
59b3452d1SSebastian Andrzej Siewior #include <linux/sizes.h>
69b3452d1SSebastian Andrzej Siewior #include <linux/platform_device.h>
79b3452d1SSebastian Andrzej Siewior #include <linux/of.h>
89b3452d1SSebastian Andrzej Siewior
9239d2218SBin Liu #include "cppi_dma.h"
109b3452d1SSebastian Andrzej Siewior #include "musb_core.h"
118ccb49ddSBin Liu #include "musb_trace.h"
129b3452d1SSebastian Andrzej Siewior
139b3452d1SSebastian Andrzej Siewior #define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
149b3452d1SSebastian Andrzej Siewior
150149b07aSBin Liu #define EP_MODE_AUTOREQ_NONE 0
160149b07aSBin Liu #define EP_MODE_AUTOREQ_ALL_NEOP 1
170149b07aSBin Liu #define EP_MODE_AUTOREQ_ALWAYS 3
189b3452d1SSebastian Andrzej Siewior
199b3452d1SSebastian Andrzej Siewior #define EP_MODE_DMA_TRANSPARENT 0
209b3452d1SSebastian Andrzej Siewior #define EP_MODE_DMA_RNDIS 1
219b3452d1SSebastian Andrzej Siewior #define EP_MODE_DMA_GEN_RNDIS 3
229b3452d1SSebastian Andrzej Siewior
239b3452d1SSebastian Andrzej Siewior #define USB_CTRL_TX_MODE 0x70
249b3452d1SSebastian Andrzej Siewior #define USB_CTRL_RX_MODE 0x74
259b3452d1SSebastian Andrzej Siewior #define USB_CTRL_AUTOREQ 0xd0
269b3452d1SSebastian Andrzej Siewior #define USB_TDOWN 0xd8
279b3452d1SSebastian Andrzej Siewior
289b3452d1SSebastian Andrzej Siewior #define MUSB_DMA_NUM_CHANNELS 15
299b3452d1SSebastian Andrzej Siewior
30e10c5b0cSAlexandre Bailon #define DA8XX_USB_MODE 0x10
31bfa53e0eSAlexandre Bailon #define DA8XX_USB_AUTOREQ 0x14
32bfa53e0eSAlexandre Bailon #define DA8XX_USB_TEARDOWN 0x1c
33bfa53e0eSAlexandre Bailon
34297d7fe9SAlexandre Bailon #define DA8XX_DMA_NUM_CHANNELS 4
35297d7fe9SAlexandre Bailon
369b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller {
379b3452d1SSebastian Andrzej Siewior struct dma_controller controller;
38297d7fe9SAlexandre Bailon struct cppi41_dma_channel *rx_channel;
39297d7fe9SAlexandre Bailon struct cppi41_dma_channel *tx_channel;
40a655f481SSebastian Andrzej Siewior struct hrtimer early_tx;
41a655f481SSebastian Andrzej Siewior struct list_head early_tx_list;
429b3452d1SSebastian Andrzej Siewior u32 rx_mode;
439b3452d1SSebastian Andrzej Siewior u32 tx_mode;
449b3452d1SSebastian Andrzej Siewior u32 auto_req;
45bfa53e0eSAlexandre Bailon
46bfa53e0eSAlexandre Bailon u32 tdown_reg;
47bfa53e0eSAlexandre Bailon u32 autoreq_reg;
48e10c5b0cSAlexandre Bailon
49e10c5b0cSAlexandre Bailon void (*set_dma_mode)(struct cppi41_dma_channel *cppi41_channel,
50e10c5b0cSAlexandre Bailon unsigned int mode);
51297d7fe9SAlexandre Bailon u8 num_channels;
529b3452d1SSebastian Andrzej Siewior };
539b3452d1SSebastian Andrzej Siewior
save_rx_toggle(struct cppi41_dma_channel * cppi41_channel)549b3452d1SSebastian Andrzej Siewior static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
559b3452d1SSebastian Andrzej Siewior {
569b3452d1SSebastian Andrzej Siewior u16 csr;
579b3452d1SSebastian Andrzej Siewior u8 toggle;
589b3452d1SSebastian Andrzej Siewior
599b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_tx)
609b3452d1SSebastian Andrzej Siewior return;
61995ee0eaSAlexandre Bailon if (!is_host_active(cppi41_channel->controller->controller.musb))
629b3452d1SSebastian Andrzej Siewior return;
639b3452d1SSebastian Andrzej Siewior
649b3452d1SSebastian Andrzej Siewior csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
659b3452d1SSebastian Andrzej Siewior toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
669b3452d1SSebastian Andrzej Siewior
679b3452d1SSebastian Andrzej Siewior cppi41_channel->usb_toggle = toggle;
689b3452d1SSebastian Andrzej Siewior }
699b3452d1SSebastian Andrzej Siewior
update_rx_toggle(struct cppi41_dma_channel * cppi41_channel)709b3452d1SSebastian Andrzej Siewior static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
719b3452d1SSebastian Andrzej Siewior {
72f50e6785SDaniel Mack struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
73f50e6785SDaniel Mack struct musb *musb = hw_ep->musb;
749b3452d1SSebastian Andrzej Siewior u16 csr;
759b3452d1SSebastian Andrzej Siewior u8 toggle;
769b3452d1SSebastian Andrzej Siewior
779b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_tx)
789b3452d1SSebastian Andrzej Siewior return;
79f50e6785SDaniel Mack if (!is_host_active(musb))
809b3452d1SSebastian Andrzej Siewior return;
819b3452d1SSebastian Andrzej Siewior
82f50e6785SDaniel Mack musb_ep_select(musb->mregs, hw_ep->epnum);
83f50e6785SDaniel Mack csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
849b3452d1SSebastian Andrzej Siewior toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
859b3452d1SSebastian Andrzej Siewior
869b3452d1SSebastian Andrzej Siewior /*
879b3452d1SSebastian Andrzej Siewior * AM335x Advisory 1.0.13: Due to internal synchronisation error the
889b3452d1SSebastian Andrzej Siewior * data toggle may reset from DATA1 to DATA0 during receiving data from
899b3452d1SSebastian Andrzej Siewior * more than one endpoint.
909b3452d1SSebastian Andrzej Siewior */
919b3452d1SSebastian Andrzej Siewior if (!toggle && toggle == cppi41_channel->usb_toggle) {
929b3452d1SSebastian Andrzej Siewior csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
939b3452d1SSebastian Andrzej Siewior musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
94995ee0eaSAlexandre Bailon musb_dbg(musb, "Restoring DATA1 toggle.");
959b3452d1SSebastian Andrzej Siewior }
969b3452d1SSebastian Andrzej Siewior
979b3452d1SSebastian Andrzej Siewior cppi41_channel->usb_toggle = toggle;
989b3452d1SSebastian Andrzej Siewior }
999b3452d1SSebastian Andrzej Siewior
musb_is_tx_fifo_empty(struct musb_hw_ep * hw_ep)100a655f481SSebastian Andrzej Siewior static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
101a655f481SSebastian Andrzej Siewior {
102a655f481SSebastian Andrzej Siewior u8 epnum = hw_ep->epnum;
103a655f481SSebastian Andrzej Siewior struct musb *musb = hw_ep->musb;
104a655f481SSebastian Andrzej Siewior void __iomem *epio = musb->endpoints[epnum].regs;
105a655f481SSebastian Andrzej Siewior u16 csr;
106a655f481SSebastian Andrzej Siewior
107f50e6785SDaniel Mack musb_ep_select(musb->mregs, hw_ep->epnum);
108a655f481SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_TXCSR);
109a655f481SSebastian Andrzej Siewior if (csr & MUSB_TXCSR_TXPKTRDY)
110a655f481SSebastian Andrzej Siewior return false;
111a655f481SSebastian Andrzej Siewior return true;
112a655f481SSebastian Andrzej Siewior }
113a655f481SSebastian Andrzej Siewior
114ed232c0bSAlexandre Bailon static void cppi41_dma_callback(void *private_data,
115ed232c0bSAlexandre Bailon const struct dmaengine_result *result);
116d373a853SSebastian Andrzej Siewior
cppi41_trans_done(struct cppi41_dma_channel * cppi41_channel)117a655f481SSebastian Andrzej Siewior static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
1189b3452d1SSebastian Andrzej Siewior {
1199b3452d1SSebastian Andrzej Siewior struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
1209b3452d1SSebastian Andrzej Siewior struct musb *musb = hw_ep->musb;
1219267edafSBin Liu void __iomem *epio = hw_ep->regs;
1229267edafSBin Liu u16 csr;
1239b3452d1SSebastian Andrzej Siewior
124aecbc31dSGeorge Cherian if (!cppi41_channel->prog_len ||
125aecbc31dSGeorge Cherian (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
1269b3452d1SSebastian Andrzej Siewior
1279b3452d1SSebastian Andrzej Siewior /* done, complete */
1289b3452d1SSebastian Andrzej Siewior cppi41_channel->channel.actual_len =
1299b3452d1SSebastian Andrzej Siewior cppi41_channel->transferred;
1309b3452d1SSebastian Andrzej Siewior cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
131ff3fcac9SDaniel Mack cppi41_channel->channel.rx_packet_done = true;
1329267edafSBin Liu
1339267edafSBin Liu /*
1349267edafSBin Liu * transmit ZLP using PIO mode for transfers which size is
1359267edafSBin Liu * multiple of EP packet size.
1369267edafSBin Liu */
1379267edafSBin Liu if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
1389267edafSBin Liu cppi41_channel->packet_sz) == 0) {
1399267edafSBin Liu musb_ep_select(musb->mregs, hw_ep->epnum);
1409267edafSBin Liu csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
1419267edafSBin Liu musb_writew(epio, MUSB_TXCSR, csr);
1429267edafSBin Liu }
1438ccb49ddSBin Liu
1448ccb49ddSBin Liu trace_musb_cppi41_done(cppi41_channel);
1459b3452d1SSebastian Andrzej Siewior musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
1469b3452d1SSebastian Andrzej Siewior } else {
1479b3452d1SSebastian Andrzej Siewior /* next iteration, reload */
1489b3452d1SSebastian Andrzej Siewior struct dma_chan *dc = cppi41_channel->dc;
1499b3452d1SSebastian Andrzej Siewior struct dma_async_tx_descriptor *dma_desc;
1509b3452d1SSebastian Andrzej Siewior enum dma_transfer_direction direction;
1519b3452d1SSebastian Andrzej Siewior u32 remain_bytes;
1529b3452d1SSebastian Andrzej Siewior
1539b3452d1SSebastian Andrzej Siewior cppi41_channel->buf_addr += cppi41_channel->packet_sz;
1549b3452d1SSebastian Andrzej Siewior
1559b3452d1SSebastian Andrzej Siewior remain_bytes = cppi41_channel->total_len;
1569b3452d1SSebastian Andrzej Siewior remain_bytes -= cppi41_channel->transferred;
1579b3452d1SSebastian Andrzej Siewior remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
1589b3452d1SSebastian Andrzej Siewior cppi41_channel->prog_len = remain_bytes;
1599b3452d1SSebastian Andrzej Siewior
1609b3452d1SSebastian Andrzej Siewior direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
1619b3452d1SSebastian Andrzej Siewior : DMA_DEV_TO_MEM;
1629b3452d1SSebastian Andrzej Siewior dma_desc = dmaengine_prep_slave_single(dc,
1639b3452d1SSebastian Andrzej Siewior cppi41_channel->buf_addr,
1649b3452d1SSebastian Andrzej Siewior remain_bytes,
1659b3452d1SSebastian Andrzej Siewior direction,
1669b3452d1SSebastian Andrzej Siewior DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
167d373a853SSebastian Andrzej Siewior if (WARN_ON(!dma_desc))
1689b3452d1SSebastian Andrzej Siewior return;
1699b3452d1SSebastian Andrzej Siewior
170ed232c0bSAlexandre Bailon dma_desc->callback_result = cppi41_dma_callback;
171a655f481SSebastian Andrzej Siewior dma_desc->callback_param = &cppi41_channel->channel;
1729b3452d1SSebastian Andrzej Siewior cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
1738ccb49ddSBin Liu trace_musb_cppi41_cont(cppi41_channel);
1749b3452d1SSebastian Andrzej Siewior dma_async_issue_pending(dc);
1759b3452d1SSebastian Andrzej Siewior
1769b3452d1SSebastian Andrzej Siewior if (!cppi41_channel->is_tx) {
177f50e6785SDaniel Mack musb_ep_select(musb->mregs, hw_ep->epnum);
1789b3452d1SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_RXCSR);
1799b3452d1SSebastian Andrzej Siewior csr |= MUSB_RXCSR_H_REQPKT;
1809b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_RXCSR, csr);
1819b3452d1SSebastian Andrzej Siewior }
1829b3452d1SSebastian Andrzej Siewior }
183d373a853SSebastian Andrzej Siewior }
184d373a853SSebastian Andrzej Siewior
cppi41_recheck_tx_req(struct hrtimer * timer)185a655f481SSebastian Andrzej Siewior static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
186a655f481SSebastian Andrzej Siewior {
187a655f481SSebastian Andrzej Siewior struct cppi41_dma_controller *controller;
188a655f481SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel, *n;
189a655f481SSebastian Andrzej Siewior struct musb *musb;
190a655f481SSebastian Andrzej Siewior unsigned long flags;
191a655f481SSebastian Andrzej Siewior enum hrtimer_restart ret = HRTIMER_NORESTART;
192a655f481SSebastian Andrzej Siewior
193a655f481SSebastian Andrzej Siewior controller = container_of(timer, struct cppi41_dma_controller,
194a655f481SSebastian Andrzej Siewior early_tx);
195995ee0eaSAlexandre Bailon musb = controller->controller.musb;
196a655f481SSebastian Andrzej Siewior
197a655f481SSebastian Andrzej Siewior spin_lock_irqsave(&musb->lock, flags);
198a655f481SSebastian Andrzej Siewior list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
199a655f481SSebastian Andrzej Siewior tx_check) {
200a655f481SSebastian Andrzej Siewior bool empty;
201a655f481SSebastian Andrzej Siewior struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
202a655f481SSebastian Andrzej Siewior
203a655f481SSebastian Andrzej Siewior empty = musb_is_tx_fifo_empty(hw_ep);
204a655f481SSebastian Andrzej Siewior if (empty) {
205a655f481SSebastian Andrzej Siewior list_del_init(&cppi41_channel->tx_check);
206a655f481SSebastian Andrzej Siewior cppi41_trans_done(cppi41_channel);
207a655f481SSebastian Andrzej Siewior }
208a655f481SSebastian Andrzej Siewior }
209a655f481SSebastian Andrzej Siewior
210d2e6d62cSThomas Gleixner if (!list_empty(&controller->early_tx_list) &&
211d2e6d62cSThomas Gleixner !hrtimer_is_queued(&controller->early_tx)) {
212a655f481SSebastian Andrzej Siewior ret = HRTIMER_RESTART;
2138b0e1953SThomas Gleixner hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
214a655f481SSebastian Andrzej Siewior }
215a655f481SSebastian Andrzej Siewior
216a655f481SSebastian Andrzej Siewior spin_unlock_irqrestore(&musb->lock, flags);
217a655f481SSebastian Andrzej Siewior return ret;
218a655f481SSebastian Andrzej Siewior }
219a655f481SSebastian Andrzej Siewior
cppi41_dma_callback(void * private_data,const struct dmaengine_result * result)220ed232c0bSAlexandre Bailon static void cppi41_dma_callback(void *private_data,
221ed232c0bSAlexandre Bailon const struct dmaengine_result *result)
222d373a853SSebastian Andrzej Siewior {
223d373a853SSebastian Andrzej Siewior struct dma_channel *channel = private_data;
224d373a853SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = channel->private_data;
225d373a853SSebastian Andrzej Siewior struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
2261b61625fSFelipe Balbi struct cppi41_dma_controller *controller;
227d373a853SSebastian Andrzej Siewior struct musb *musb = hw_ep->musb;
228d373a853SSebastian Andrzej Siewior unsigned long flags;
229d373a853SSebastian Andrzej Siewior struct dma_tx_state txstate;
230d373a853SSebastian Andrzej Siewior u32 transferred;
2311b61625fSFelipe Balbi int is_hs = 0;
232a655f481SSebastian Andrzej Siewior bool empty;
233d373a853SSebastian Andrzej Siewior
234050dc900SAlexandre Bailon controller = cppi41_channel->controller;
235050dc900SAlexandre Bailon if (controller->controller.dma_callback)
236050dc900SAlexandre Bailon controller->controller.dma_callback(&controller->controller);
237050dc900SAlexandre Bailon
238ed232c0bSAlexandre Bailon if (result->result == DMA_TRANS_ABORTED)
239ed232c0bSAlexandre Bailon return;
240ed232c0bSAlexandre Bailon
241d373a853SSebastian Andrzej Siewior spin_lock_irqsave(&musb->lock, flags);
242d373a853SSebastian Andrzej Siewior
243d373a853SSebastian Andrzej Siewior dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
244d373a853SSebastian Andrzej Siewior &txstate);
245d373a853SSebastian Andrzej Siewior transferred = cppi41_channel->prog_len - txstate.residue;
246d373a853SSebastian Andrzej Siewior cppi41_channel->transferred += transferred;
247d373a853SSebastian Andrzej Siewior
2488ccb49ddSBin Liu trace_musb_cppi41_gb(cppi41_channel);
249d373a853SSebastian Andrzej Siewior update_rx_toggle(cppi41_channel);
250d373a853SSebastian Andrzej Siewior
251d373a853SSebastian Andrzej Siewior if (cppi41_channel->transferred == cppi41_channel->total_len ||
252d373a853SSebastian Andrzej Siewior transferred < cppi41_channel->packet_sz)
253d373a853SSebastian Andrzej Siewior cppi41_channel->prog_len = 0;
254d373a853SSebastian Andrzej Siewior
2550090114dSBin Liu if (cppi41_channel->is_tx) {
2560090114dSBin Liu u8 type;
2570090114dSBin Liu
2580090114dSBin Liu if (is_host_active(musb))
2590090114dSBin Liu type = hw_ep->out_qh->type;
2600090114dSBin Liu else
2610090114dSBin Liu type = hw_ep->ep_in.type;
2620090114dSBin Liu
2630090114dSBin Liu if (type == USB_ENDPOINT_XFER_ISOC)
2640090114dSBin Liu /*
2650090114dSBin Liu * Don't use the early-TX-interrupt workaround below
2660090114dSBin Liu * for Isoch transfter. Since Isoch are periodic
2670090114dSBin Liu * transfer, by the time the next transfer is
2680090114dSBin Liu * scheduled, the current one should be done already.
2690090114dSBin Liu *
2700090114dSBin Liu * This avoids audio playback underrun issue.
2710090114dSBin Liu */
2720090114dSBin Liu empty = true;
2730090114dSBin Liu else
274a655f481SSebastian Andrzej Siewior empty = musb_is_tx_fifo_empty(hw_ep);
2750090114dSBin Liu }
27672a472d2STakeyoshi Kikuchi
27772a472d2STakeyoshi Kikuchi if (!cppi41_channel->is_tx || empty) {
278a655f481SSebastian Andrzej Siewior cppi41_trans_done(cppi41_channel);
2791b61625fSFelipe Balbi goto out;
2801b61625fSFelipe Balbi }
2811b61625fSFelipe Balbi
282a655f481SSebastian Andrzej Siewior /*
283a655f481SSebastian Andrzej Siewior * On AM335x it has been observed that the TX interrupt fires
284a655f481SSebastian Andrzej Siewior * too early that means the TXFIFO is not yet empty but the DMA
285a655f481SSebastian Andrzej Siewior * engine says that it is done with the transfer. We don't
286a655f481SSebastian Andrzej Siewior * receive a FIFO empty interrupt so the only thing we can do is
287a655f481SSebastian Andrzej Siewior * to poll for the bit. On HS it usually takes 2us, on FS around
288a655f481SSebastian Andrzej Siewior * 110us - 150us depending on the transfer size.
289d5851c24SJilin Yuan * We spin on HS (no longer than 25us and setup a timer on
290a655f481SSebastian Andrzej Siewior * FS to check for the bit and complete the transfer.
291a655f481SSebastian Andrzej Siewior */
2921eec34e9SSebastian Andrzej Siewior if (is_host_active(musb)) {
2931eec34e9SSebastian Andrzej Siewior if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
2941eec34e9SSebastian Andrzej Siewior is_hs = 1;
2951eec34e9SSebastian Andrzej Siewior } else {
2961eec34e9SSebastian Andrzej Siewior if (musb->g.speed == USB_SPEED_HIGH)
2971eec34e9SSebastian Andrzej Siewior is_hs = 1;
2981eec34e9SSebastian Andrzej Siewior }
2991eec34e9SSebastian Andrzej Siewior if (is_hs) {
300a655f481SSebastian Andrzej Siewior unsigned wait = 25;
301a655f481SSebastian Andrzej Siewior
302a655f481SSebastian Andrzej Siewior do {
303a655f481SSebastian Andrzej Siewior empty = musb_is_tx_fifo_empty(hw_ep);
304af63429cSFelipe Balbi if (empty) {
305af63429cSFelipe Balbi cppi41_trans_done(cppi41_channel);
306af63429cSFelipe Balbi goto out;
307af63429cSFelipe Balbi }
308a655f481SSebastian Andrzej Siewior wait--;
309a655f481SSebastian Andrzej Siewior if (!wait)
310a655f481SSebastian Andrzej Siewior break;
311043f5b75SFelipe Balbi cpu_relax();
312a655f481SSebastian Andrzej Siewior } while (1);
313a655f481SSebastian Andrzej Siewior }
314a655f481SSebastian Andrzej Siewior list_add_tail(&cppi41_channel->tx_check,
315a655f481SSebastian Andrzej Siewior &controller->early_tx_list);
316c58d80f5SThomas Gleixner if (!hrtimer_is_queued(&controller->early_tx)) {
31750aea6fcSDaniel Mack unsigned long usecs = cppi41_channel->total_len / 10;
31850aea6fcSDaniel Mack
319a655f481SSebastian Andrzej Siewior hrtimer_start_range_ns(&controller->early_tx,
3208b0e1953SThomas Gleixner usecs * NSEC_PER_USEC,
321a5e4aa4dSDaniel Mack 20 * NSEC_PER_USEC,
322a655f481SSebastian Andrzej Siewior HRTIMER_MODE_REL);
323a655f481SSebastian Andrzej Siewior }
3241b61625fSFelipe Balbi
325a655f481SSebastian Andrzej Siewior out:
3269b3452d1SSebastian Andrzej Siewior spin_unlock_irqrestore(&musb->lock, flags);
3279b3452d1SSebastian Andrzej Siewior }
3289b3452d1SSebastian Andrzej Siewior
update_ep_mode(unsigned ep,unsigned mode,u32 old)3299b3452d1SSebastian Andrzej Siewior static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
3309b3452d1SSebastian Andrzej Siewior {
3319b3452d1SSebastian Andrzej Siewior unsigned shift;
3329b3452d1SSebastian Andrzej Siewior
3339b3452d1SSebastian Andrzej Siewior shift = (ep - 1) * 2;
3349b3452d1SSebastian Andrzej Siewior old &= ~(3 << shift);
3359b3452d1SSebastian Andrzej Siewior old |= mode << shift;
3369b3452d1SSebastian Andrzej Siewior return old;
3379b3452d1SSebastian Andrzej Siewior }
3389b3452d1SSebastian Andrzej Siewior
cppi41_set_dma_mode(struct cppi41_dma_channel * cppi41_channel,unsigned mode)3399b3452d1SSebastian Andrzej Siewior static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
3409b3452d1SSebastian Andrzej Siewior unsigned mode)
3419b3452d1SSebastian Andrzej Siewior {
3429b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = cppi41_channel->controller;
343995ee0eaSAlexandre Bailon struct musb *musb = controller->controller.musb;
3449b3452d1SSebastian Andrzej Siewior u32 port;
3459b3452d1SSebastian Andrzej Siewior u32 new_mode;
3469b3452d1SSebastian Andrzej Siewior u32 old_mode;
3479b3452d1SSebastian Andrzej Siewior
3489b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_tx)
3499b3452d1SSebastian Andrzej Siewior old_mode = controller->tx_mode;
3509b3452d1SSebastian Andrzej Siewior else
3519b3452d1SSebastian Andrzej Siewior old_mode = controller->rx_mode;
3529b3452d1SSebastian Andrzej Siewior port = cppi41_channel->port_num;
3539b3452d1SSebastian Andrzej Siewior new_mode = update_ep_mode(port, mode, old_mode);
3549b3452d1SSebastian Andrzej Siewior
3559b3452d1SSebastian Andrzej Siewior if (new_mode == old_mode)
3569b3452d1SSebastian Andrzej Siewior return;
3579b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_tx) {
3589b3452d1SSebastian Andrzej Siewior controller->tx_mode = new_mode;
359995ee0eaSAlexandre Bailon musb_writel(musb->ctrl_base, USB_CTRL_TX_MODE, new_mode);
3609b3452d1SSebastian Andrzej Siewior } else {
3619b3452d1SSebastian Andrzej Siewior controller->rx_mode = new_mode;
362995ee0eaSAlexandre Bailon musb_writel(musb->ctrl_base, USB_CTRL_RX_MODE, new_mode);
3639b3452d1SSebastian Andrzej Siewior }
3649b3452d1SSebastian Andrzej Siewior }
3659b3452d1SSebastian Andrzej Siewior
da8xx_set_dma_mode(struct cppi41_dma_channel * cppi41_channel,unsigned int mode)366e10c5b0cSAlexandre Bailon static void da8xx_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
367e10c5b0cSAlexandre Bailon unsigned int mode)
368e10c5b0cSAlexandre Bailon {
369e10c5b0cSAlexandre Bailon struct cppi41_dma_controller *controller = cppi41_channel->controller;
370e10c5b0cSAlexandre Bailon struct musb *musb = controller->controller.musb;
371e10c5b0cSAlexandre Bailon unsigned int shift;
372e10c5b0cSAlexandre Bailon u32 port;
373e10c5b0cSAlexandre Bailon u32 new_mode;
374e10c5b0cSAlexandre Bailon u32 old_mode;
375e10c5b0cSAlexandre Bailon
376e10c5b0cSAlexandre Bailon old_mode = controller->tx_mode;
377e10c5b0cSAlexandre Bailon port = cppi41_channel->port_num;
378e10c5b0cSAlexandre Bailon
379e10c5b0cSAlexandre Bailon shift = (port - 1) * 4;
380e10c5b0cSAlexandre Bailon if (!cppi41_channel->is_tx)
381e10c5b0cSAlexandre Bailon shift += 16;
382e10c5b0cSAlexandre Bailon new_mode = old_mode & ~(3 << shift);
383e10c5b0cSAlexandre Bailon new_mode |= mode << shift;
384e10c5b0cSAlexandre Bailon
385e10c5b0cSAlexandre Bailon if (new_mode == old_mode)
386e10c5b0cSAlexandre Bailon return;
387e10c5b0cSAlexandre Bailon controller->tx_mode = new_mode;
388e10c5b0cSAlexandre Bailon musb_writel(musb->ctrl_base, DA8XX_USB_MODE, new_mode);
389e10c5b0cSAlexandre Bailon }
390e10c5b0cSAlexandre Bailon
391e10c5b0cSAlexandre Bailon
cppi41_set_autoreq_mode(struct cppi41_dma_channel * cppi41_channel,unsigned mode)3929b3452d1SSebastian Andrzej Siewior static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
3939b3452d1SSebastian Andrzej Siewior unsigned mode)
3949b3452d1SSebastian Andrzej Siewior {
3959b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = cppi41_channel->controller;
3969b3452d1SSebastian Andrzej Siewior u32 port;
3979b3452d1SSebastian Andrzej Siewior u32 new_mode;
3989b3452d1SSebastian Andrzej Siewior u32 old_mode;
3999b3452d1SSebastian Andrzej Siewior
4009b3452d1SSebastian Andrzej Siewior old_mode = controller->auto_req;
4019b3452d1SSebastian Andrzej Siewior port = cppi41_channel->port_num;
4029b3452d1SSebastian Andrzej Siewior new_mode = update_ep_mode(port, mode, old_mode);
4039b3452d1SSebastian Andrzej Siewior
4049b3452d1SSebastian Andrzej Siewior if (new_mode == old_mode)
4059b3452d1SSebastian Andrzej Siewior return;
4069b3452d1SSebastian Andrzej Siewior controller->auto_req = new_mode;
407bfa53e0eSAlexandre Bailon musb_writel(controller->controller.musb->ctrl_base,
408bfa53e0eSAlexandre Bailon controller->autoreq_reg, new_mode);
4099b3452d1SSebastian Andrzej Siewior }
4109b3452d1SSebastian Andrzej Siewior
cppi41_configure_channel(struct dma_channel * channel,u16 packet_sz,u8 mode,dma_addr_t dma_addr,u32 len)4119b3452d1SSebastian Andrzej Siewior static bool cppi41_configure_channel(struct dma_channel *channel,
4129b3452d1SSebastian Andrzej Siewior u16 packet_sz, u8 mode,
4139b3452d1SSebastian Andrzej Siewior dma_addr_t dma_addr, u32 len)
4149b3452d1SSebastian Andrzej Siewior {
4159b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = channel->private_data;
416e10c5b0cSAlexandre Bailon struct cppi41_dma_controller *controller = cppi41_channel->controller;
4179b3452d1SSebastian Andrzej Siewior struct dma_chan *dc = cppi41_channel->dc;
4189b3452d1SSebastian Andrzej Siewior struct dma_async_tx_descriptor *dma_desc;
4199b3452d1SSebastian Andrzej Siewior enum dma_transfer_direction direction;
420995ee0eaSAlexandre Bailon struct musb *musb = cppi41_channel->controller->controller.musb;
4219b3452d1SSebastian Andrzej Siewior unsigned use_gen_rndis = 0;
4229b3452d1SSebastian Andrzej Siewior
4239b3452d1SSebastian Andrzej Siewior cppi41_channel->buf_addr = dma_addr;
4249b3452d1SSebastian Andrzej Siewior cppi41_channel->total_len = len;
4259b3452d1SSebastian Andrzej Siewior cppi41_channel->transferred = 0;
4269b3452d1SSebastian Andrzej Siewior cppi41_channel->packet_sz = packet_sz;
4279267edafSBin Liu cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
4289b3452d1SSebastian Andrzej Siewior
4299b3452d1SSebastian Andrzej Siewior /*
4309b3452d1SSebastian Andrzej Siewior * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
4319b3452d1SSebastian Andrzej Siewior * than max packet size at a time.
4329b3452d1SSebastian Andrzej Siewior */
4339b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_tx)
4349b3452d1SSebastian Andrzej Siewior use_gen_rndis = 1;
4359b3452d1SSebastian Andrzej Siewior
4369b3452d1SSebastian Andrzej Siewior if (use_gen_rndis) {
4379b3452d1SSebastian Andrzej Siewior /* RNDIS mode */
4389b3452d1SSebastian Andrzej Siewior if (len > packet_sz) {
4399b3452d1SSebastian Andrzej Siewior musb_writel(musb->ctrl_base,
4409b3452d1SSebastian Andrzej Siewior RNDIS_REG(cppi41_channel->port_num), len);
4419b3452d1SSebastian Andrzej Siewior /* gen rndis */
442e10c5b0cSAlexandre Bailon controller->set_dma_mode(cppi41_channel,
4439b3452d1SSebastian Andrzej Siewior EP_MODE_DMA_GEN_RNDIS);
4449b3452d1SSebastian Andrzej Siewior
4459b3452d1SSebastian Andrzej Siewior /* auto req */
4469b3452d1SSebastian Andrzej Siewior cppi41_set_autoreq_mode(cppi41_channel,
4470149b07aSBin Liu EP_MODE_AUTOREQ_ALL_NEOP);
4489b3452d1SSebastian Andrzej Siewior } else {
4499b3452d1SSebastian Andrzej Siewior musb_writel(musb->ctrl_base,
4509b3452d1SSebastian Andrzej Siewior RNDIS_REG(cppi41_channel->port_num), 0);
451e10c5b0cSAlexandre Bailon controller->set_dma_mode(cppi41_channel,
4529b3452d1SSebastian Andrzej Siewior EP_MODE_DMA_TRANSPARENT);
4539b3452d1SSebastian Andrzej Siewior cppi41_set_autoreq_mode(cppi41_channel,
4540149b07aSBin Liu EP_MODE_AUTOREQ_NONE);
4559b3452d1SSebastian Andrzej Siewior }
4569b3452d1SSebastian Andrzej Siewior } else {
4579b3452d1SSebastian Andrzej Siewior /* fallback mode */
458e10c5b0cSAlexandre Bailon controller->set_dma_mode(cppi41_channel,
459e10c5b0cSAlexandre Bailon EP_MODE_DMA_TRANSPARENT);
4600149b07aSBin Liu cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
4619b3452d1SSebastian Andrzej Siewior len = min_t(u32, packet_sz, len);
4629b3452d1SSebastian Andrzej Siewior }
4639b3452d1SSebastian Andrzej Siewior cppi41_channel->prog_len = len;
4649b3452d1SSebastian Andrzej Siewior direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
4659b3452d1SSebastian Andrzej Siewior dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
4669b3452d1SSebastian Andrzej Siewior DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
4679b3452d1SSebastian Andrzej Siewior if (!dma_desc)
4689b3452d1SSebastian Andrzej Siewior return false;
4699b3452d1SSebastian Andrzej Siewior
470ed232c0bSAlexandre Bailon dma_desc->callback_result = cppi41_dma_callback;
4719b3452d1SSebastian Andrzej Siewior dma_desc->callback_param = channel;
4729b3452d1SSebastian Andrzej Siewior cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
473ff3fcac9SDaniel Mack cppi41_channel->channel.rx_packet_done = false;
4749b3452d1SSebastian Andrzej Siewior
4758ccb49ddSBin Liu trace_musb_cppi41_config(cppi41_channel);
4768ccb49ddSBin Liu
4779b3452d1SSebastian Andrzej Siewior save_rx_toggle(cppi41_channel);
4789b3452d1SSebastian Andrzej Siewior dma_async_issue_pending(dc);
4799b3452d1SSebastian Andrzej Siewior return true;
4809b3452d1SSebastian Andrzej Siewior }
4819b3452d1SSebastian Andrzej Siewior
cppi41_dma_channel_allocate(struct dma_controller * c,struct musb_hw_ep * hw_ep,u8 is_tx)4829b3452d1SSebastian Andrzej Siewior static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
4839b3452d1SSebastian Andrzej Siewior struct musb_hw_ep *hw_ep, u8 is_tx)
4849b3452d1SSebastian Andrzej Siewior {
4859b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = container_of(c,
4869b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller, controller);
4879b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = NULL;
4889b3452d1SSebastian Andrzej Siewior u8 ch_num = hw_ep->epnum - 1;
4899b3452d1SSebastian Andrzej Siewior
490297d7fe9SAlexandre Bailon if (ch_num >= controller->num_channels)
4919b3452d1SSebastian Andrzej Siewior return NULL;
4929b3452d1SSebastian Andrzej Siewior
4939b3452d1SSebastian Andrzej Siewior if (is_tx)
4949b3452d1SSebastian Andrzej Siewior cppi41_channel = &controller->tx_channel[ch_num];
4959b3452d1SSebastian Andrzej Siewior else
4969b3452d1SSebastian Andrzej Siewior cppi41_channel = &controller->rx_channel[ch_num];
4979b3452d1SSebastian Andrzej Siewior
4989b3452d1SSebastian Andrzej Siewior if (!cppi41_channel->dc)
4999b3452d1SSebastian Andrzej Siewior return NULL;
5009b3452d1SSebastian Andrzej Siewior
5019b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_allocated)
5029b3452d1SSebastian Andrzej Siewior return NULL;
5039b3452d1SSebastian Andrzej Siewior
5049b3452d1SSebastian Andrzej Siewior cppi41_channel->hw_ep = hw_ep;
5059b3452d1SSebastian Andrzej Siewior cppi41_channel->is_allocated = 1;
5069b3452d1SSebastian Andrzej Siewior
5078ccb49ddSBin Liu trace_musb_cppi41_alloc(cppi41_channel);
5089b3452d1SSebastian Andrzej Siewior return &cppi41_channel->channel;
5099b3452d1SSebastian Andrzej Siewior }
5109b3452d1SSebastian Andrzej Siewior
cppi41_dma_channel_release(struct dma_channel * channel)5119b3452d1SSebastian Andrzej Siewior static void cppi41_dma_channel_release(struct dma_channel *channel)
5129b3452d1SSebastian Andrzej Siewior {
5139b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5149b3452d1SSebastian Andrzej Siewior
5158ccb49ddSBin Liu trace_musb_cppi41_free(cppi41_channel);
5169b3452d1SSebastian Andrzej Siewior if (cppi41_channel->is_allocated) {
5179b3452d1SSebastian Andrzej Siewior cppi41_channel->is_allocated = 0;
5189b3452d1SSebastian Andrzej Siewior channel->status = MUSB_DMA_STATUS_FREE;
5199b3452d1SSebastian Andrzej Siewior channel->actual_len = 0;
5209b3452d1SSebastian Andrzej Siewior }
5219b3452d1SSebastian Andrzej Siewior }
5229b3452d1SSebastian Andrzej Siewior
cppi41_dma_channel_program(struct dma_channel * channel,u16 packet_sz,u8 mode,dma_addr_t dma_addr,u32 len)5239b3452d1SSebastian Andrzej Siewior static int cppi41_dma_channel_program(struct dma_channel *channel,
5249b3452d1SSebastian Andrzej Siewior u16 packet_sz, u8 mode,
5259b3452d1SSebastian Andrzej Siewior dma_addr_t dma_addr, u32 len)
5269b3452d1SSebastian Andrzej Siewior {
5279b3452d1SSebastian Andrzej Siewior int ret;
528f82503f5SGeorge Cherian struct cppi41_dma_channel *cppi41_channel = channel->private_data;
529f82503f5SGeorge Cherian int hb_mult = 0;
5309b3452d1SSebastian Andrzej Siewior
5319b3452d1SSebastian Andrzej Siewior BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
5329b3452d1SSebastian Andrzej Siewior channel->status == MUSB_DMA_STATUS_BUSY);
5339b3452d1SSebastian Andrzej Siewior
534995ee0eaSAlexandre Bailon if (is_host_active(cppi41_channel->controller->controller.musb)) {
535f82503f5SGeorge Cherian if (cppi41_channel->is_tx)
536f82503f5SGeorge Cherian hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
537f82503f5SGeorge Cherian else
538f82503f5SGeorge Cherian hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
539f82503f5SGeorge Cherian }
540f82503f5SGeorge Cherian
5419b3452d1SSebastian Andrzej Siewior channel->status = MUSB_DMA_STATUS_BUSY;
5429b3452d1SSebastian Andrzej Siewior channel->actual_len = 0;
543f82503f5SGeorge Cherian
544f82503f5SGeorge Cherian if (hb_mult)
545f82503f5SGeorge Cherian packet_sz = hb_mult * (packet_sz & 0x7FF);
546f82503f5SGeorge Cherian
5479b3452d1SSebastian Andrzej Siewior ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
5489b3452d1SSebastian Andrzej Siewior if (!ret)
5499b3452d1SSebastian Andrzej Siewior channel->status = MUSB_DMA_STATUS_FREE;
5509b3452d1SSebastian Andrzej Siewior
5519b3452d1SSebastian Andrzej Siewior return ret;
5529b3452d1SSebastian Andrzej Siewior }
5539b3452d1SSebastian Andrzej Siewior
cppi41_is_compatible(struct dma_channel * channel,u16 maxpacket,void * buf,u32 length)5549b3452d1SSebastian Andrzej Siewior static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
5559b3452d1SSebastian Andrzej Siewior void *buf, u32 length)
5569b3452d1SSebastian Andrzej Siewior {
5579b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5589b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = cppi41_channel->controller;
559995ee0eaSAlexandre Bailon struct musb *musb = controller->controller.musb;
5609b3452d1SSebastian Andrzej Siewior
5619b3452d1SSebastian Andrzej Siewior if (is_host_active(musb)) {
5629b3452d1SSebastian Andrzej Siewior WARN_ON(1);
5639b3452d1SSebastian Andrzej Siewior return 1;
5649b3452d1SSebastian Andrzej Siewior }
565a655f481SSebastian Andrzej Siewior if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
566a655f481SSebastian Andrzej Siewior return 0;
56713266feaSSebastian Andrzej Siewior if (cppi41_channel->is_tx)
56813266feaSSebastian Andrzej Siewior return 1;
56913266feaSSebastian Andrzej Siewior /* AM335x Advisory 1.0.13. No workaround for device RX mode */
5709b3452d1SSebastian Andrzej Siewior return 0;
5719b3452d1SSebastian Andrzej Siewior }
5729b3452d1SSebastian Andrzej Siewior
cppi41_dma_channel_abort(struct dma_channel * channel)5739b3452d1SSebastian Andrzej Siewior static int cppi41_dma_channel_abort(struct dma_channel *channel)
5749b3452d1SSebastian Andrzej Siewior {
5759b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5769b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = cppi41_channel->controller;
577995ee0eaSAlexandre Bailon struct musb *musb = controller->controller.musb;
5789b3452d1SSebastian Andrzej Siewior void __iomem *epio = cppi41_channel->hw_ep->regs;
5799b3452d1SSebastian Andrzej Siewior int tdbit;
5809b3452d1SSebastian Andrzej Siewior int ret;
5819b3452d1SSebastian Andrzej Siewior unsigned is_tx;
5829b3452d1SSebastian Andrzej Siewior u16 csr;
5839b3452d1SSebastian Andrzej Siewior
5849b3452d1SSebastian Andrzej Siewior is_tx = cppi41_channel->is_tx;
5858ccb49ddSBin Liu trace_musb_cppi41_abort(cppi41_channel);
5869b3452d1SSebastian Andrzej Siewior
5879b3452d1SSebastian Andrzej Siewior if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
5889b3452d1SSebastian Andrzej Siewior return 0;
5899b3452d1SSebastian Andrzej Siewior
590a655f481SSebastian Andrzej Siewior list_del_init(&cppi41_channel->tx_check);
5919b3452d1SSebastian Andrzej Siewior if (is_tx) {
5929b3452d1SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_TXCSR);
5939b3452d1SSebastian Andrzej Siewior csr &= ~MUSB_TXCSR_DMAENAB;
5949b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_TXCSR, csr);
5959b3452d1SSebastian Andrzej Siewior } else {
596cb83df77SBin Liu cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
597cb83df77SBin Liu
598b431ba88SBin Liu /* delay to drain to cppi dma pipeline for isoch */
599b431ba88SBin Liu udelay(250);
600b431ba88SBin Liu
6019b3452d1SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_RXCSR);
6029b3452d1SSebastian Andrzej Siewior csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
6039b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_RXCSR, csr);
6049b3452d1SSebastian Andrzej Siewior
605cb83df77SBin Liu /* wait to drain cppi dma pipe line */
606cb83df77SBin Liu udelay(50);
607cb83df77SBin Liu
6089b3452d1SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_RXCSR);
6099b3452d1SSebastian Andrzej Siewior if (csr & MUSB_RXCSR_RXPKTRDY) {
6109b3452d1SSebastian Andrzej Siewior csr |= MUSB_RXCSR_FLUSHFIFO;
6119b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_RXCSR, csr);
6129b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_RXCSR, csr);
6139b3452d1SSebastian Andrzej Siewior }
6149b3452d1SSebastian Andrzej Siewior }
6159b3452d1SSebastian Andrzej Siewior
616593bc462SAlexandre Bailon /* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
617dc8fca6cSBin Liu if (musb->ops->quirks & MUSB_DA8XX)
618593bc462SAlexandre Bailon mdelay(250);
619593bc462SAlexandre Bailon
6209b3452d1SSebastian Andrzej Siewior tdbit = 1 << cppi41_channel->port_num;
6219b3452d1SSebastian Andrzej Siewior if (is_tx)
6229b3452d1SSebastian Andrzej Siewior tdbit <<= 16;
6239b3452d1SSebastian Andrzej Siewior
6249b3452d1SSebastian Andrzej Siewior do {
625cb83df77SBin Liu if (is_tx)
626bfa53e0eSAlexandre Bailon musb_writel(musb->ctrl_base, controller->tdown_reg,
627bfa53e0eSAlexandre Bailon tdbit);
6289b3452d1SSebastian Andrzej Siewior ret = dmaengine_terminate_all(cppi41_channel->dc);
6299b3452d1SSebastian Andrzej Siewior } while (ret == -EAGAIN);
6309b3452d1SSebastian Andrzej Siewior
631cb83df77SBin Liu if (is_tx) {
632bfa53e0eSAlexandre Bailon musb_writel(musb->ctrl_base, controller->tdown_reg, tdbit);
6339b3452d1SSebastian Andrzej Siewior
6349b3452d1SSebastian Andrzej Siewior csr = musb_readw(epio, MUSB_TXCSR);
6359b3452d1SSebastian Andrzej Siewior if (csr & MUSB_TXCSR_TXPKTRDY) {
6369b3452d1SSebastian Andrzej Siewior csr |= MUSB_TXCSR_FLUSHFIFO;
6379b3452d1SSebastian Andrzej Siewior musb_writew(epio, MUSB_TXCSR, csr);
6389b3452d1SSebastian Andrzej Siewior }
6399b3452d1SSebastian Andrzej Siewior }
6409b3452d1SSebastian Andrzej Siewior
6419b3452d1SSebastian Andrzej Siewior cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
6429b3452d1SSebastian Andrzej Siewior return 0;
6439b3452d1SSebastian Andrzej Siewior }
6449b3452d1SSebastian Andrzej Siewior
cppi41_release_all_dma_chans(struct cppi41_dma_controller * ctrl)6459b3452d1SSebastian Andrzej Siewior static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
6469b3452d1SSebastian Andrzej Siewior {
6479b3452d1SSebastian Andrzej Siewior struct dma_chan *dc;
6489b3452d1SSebastian Andrzej Siewior int i;
6499b3452d1SSebastian Andrzej Siewior
650297d7fe9SAlexandre Bailon for (i = 0; i < ctrl->num_channels; i++) {
6519b3452d1SSebastian Andrzej Siewior dc = ctrl->tx_channel[i].dc;
6529b3452d1SSebastian Andrzej Siewior if (dc)
6539b3452d1SSebastian Andrzej Siewior dma_release_channel(dc);
6549b3452d1SSebastian Andrzej Siewior dc = ctrl->rx_channel[i].dc;
6559b3452d1SSebastian Andrzej Siewior if (dc)
6569b3452d1SSebastian Andrzej Siewior dma_release_channel(dc);
6579b3452d1SSebastian Andrzej Siewior }
6589b3452d1SSebastian Andrzej Siewior }
6599b3452d1SSebastian Andrzej Siewior
cppi41_dma_controller_stop(struct cppi41_dma_controller * controller)6609b3452d1SSebastian Andrzej Siewior static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
6619b3452d1SSebastian Andrzej Siewior {
6629b3452d1SSebastian Andrzej Siewior cppi41_release_all_dma_chans(controller);
6639b3452d1SSebastian Andrzej Siewior }
6649b3452d1SSebastian Andrzej Siewior
cppi41_dma_controller_start(struct cppi41_dma_controller * controller)6659b3452d1SSebastian Andrzej Siewior static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
6669b3452d1SSebastian Andrzej Siewior {
667995ee0eaSAlexandre Bailon struct musb *musb = controller->controller.musb;
6689b3452d1SSebastian Andrzej Siewior struct device *dev = musb->controller;
669b0a688ddSFelipe Balbi struct device_node *np = dev->parent->of_node;
6709b3452d1SSebastian Andrzej Siewior struct cppi41_dma_channel *cppi41_channel;
6719b3452d1SSebastian Andrzej Siewior int count;
6729b3452d1SSebastian Andrzej Siewior int i;
6739b3452d1SSebastian Andrzej Siewior int ret;
6749b3452d1SSebastian Andrzej Siewior
6759b3452d1SSebastian Andrzej Siewior count = of_property_count_strings(np, "dma-names");
6769b3452d1SSebastian Andrzej Siewior if (count < 0)
6779b3452d1SSebastian Andrzej Siewior return count;
6789b3452d1SSebastian Andrzej Siewior
6799b3452d1SSebastian Andrzej Siewior for (i = 0; i < count; i++) {
6809b3452d1SSebastian Andrzej Siewior struct dma_chan *dc;
6819b3452d1SSebastian Andrzej Siewior struct dma_channel *musb_dma;
6829b3452d1SSebastian Andrzej Siewior const char *str;
6839b3452d1SSebastian Andrzej Siewior unsigned is_tx;
6849b3452d1SSebastian Andrzej Siewior unsigned int port;
6859b3452d1SSebastian Andrzej Siewior
6869b3452d1SSebastian Andrzej Siewior ret = of_property_read_string_index(np, "dma-names", i, &str);
6879b3452d1SSebastian Andrzej Siewior if (ret)
6889b3452d1SSebastian Andrzej Siewior goto err;
689e87c3f80SRasmus Villemoes if (strstarts(str, "tx"))
6909b3452d1SSebastian Andrzej Siewior is_tx = 1;
691e87c3f80SRasmus Villemoes else if (strstarts(str, "rx"))
6929b3452d1SSebastian Andrzej Siewior is_tx = 0;
6939b3452d1SSebastian Andrzej Siewior else {
6949b3452d1SSebastian Andrzej Siewior dev_err(dev, "Wrong dmatype %s\n", str);
6959b3452d1SSebastian Andrzej Siewior goto err;
6969b3452d1SSebastian Andrzej Siewior }
6979b3452d1SSebastian Andrzej Siewior ret = kstrtouint(str + 2, 0, &port);
6989b3452d1SSebastian Andrzej Siewior if (ret)
6999b3452d1SSebastian Andrzej Siewior goto err;
7009b3452d1SSebastian Andrzej Siewior
70148054147SSebastian Andrzej Siewior ret = -EINVAL;
702297d7fe9SAlexandre Bailon if (port > controller->num_channels || !port)
7039b3452d1SSebastian Andrzej Siewior goto err;
7049b3452d1SSebastian Andrzej Siewior if (is_tx)
7059b3452d1SSebastian Andrzej Siewior cppi41_channel = &controller->tx_channel[port - 1];
7069b3452d1SSebastian Andrzej Siewior else
7079b3452d1SSebastian Andrzej Siewior cppi41_channel = &controller->rx_channel[port - 1];
7089b3452d1SSebastian Andrzej Siewior
7099b3452d1SSebastian Andrzej Siewior cppi41_channel->controller = controller;
7109b3452d1SSebastian Andrzej Siewior cppi41_channel->port_num = port;
7119b3452d1SSebastian Andrzej Siewior cppi41_channel->is_tx = is_tx;
712a655f481SSebastian Andrzej Siewior INIT_LIST_HEAD(&cppi41_channel->tx_check);
7139b3452d1SSebastian Andrzej Siewior
7149b3452d1SSebastian Andrzej Siewior musb_dma = &cppi41_channel->channel;
7159b3452d1SSebastian Andrzej Siewior musb_dma->private_data = cppi41_channel;
7169b3452d1SSebastian Andrzej Siewior musb_dma->status = MUSB_DMA_STATUS_FREE;
7179b3452d1SSebastian Andrzej Siewior musb_dma->max_len = SZ_4M;
7189b3452d1SSebastian Andrzej Siewior
719a70df146SAlexandre Bailon dc = dma_request_chan(dev->parent, str);
720a70df146SAlexandre Bailon if (IS_ERR(dc)) {
721*a806f67fSYang Yingliang ret = dev_err_probe(dev, PTR_ERR(dc),
722*a806f67fSYang Yingliang "Failed to request %s.\n", str);
7239b3452d1SSebastian Andrzej Siewior goto err;
7249b3452d1SSebastian Andrzej Siewior }
725a70df146SAlexandre Bailon
7269b3452d1SSebastian Andrzej Siewior cppi41_channel->dc = dc;
7279b3452d1SSebastian Andrzej Siewior }
7289b3452d1SSebastian Andrzej Siewior return 0;
7299b3452d1SSebastian Andrzej Siewior err:
7309b3452d1SSebastian Andrzej Siewior cppi41_release_all_dma_chans(controller);
73148054147SSebastian Andrzej Siewior return ret;
7329b3452d1SSebastian Andrzej Siewior }
7339b3452d1SSebastian Andrzej Siewior
cppi41_dma_controller_destroy(struct dma_controller * c)7347f6283edSTony Lindgren void cppi41_dma_controller_destroy(struct dma_controller *c)
7359b3452d1SSebastian Andrzej Siewior {
7369b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller = container_of(c,
7379b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller, controller);
7389b3452d1SSebastian Andrzej Siewior
739a655f481SSebastian Andrzej Siewior hrtimer_cancel(&controller->early_tx);
7409b3452d1SSebastian Andrzej Siewior cppi41_dma_controller_stop(controller);
741297d7fe9SAlexandre Bailon kfree(controller->rx_channel);
742297d7fe9SAlexandre Bailon kfree(controller->tx_channel);
7439b3452d1SSebastian Andrzej Siewior kfree(controller);
7449b3452d1SSebastian Andrzej Siewior }
7457f6283edSTony Lindgren EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
7469b3452d1SSebastian Andrzej Siewior
7477f6283edSTony Lindgren struct dma_controller *
cppi41_dma_controller_create(struct musb * musb,void __iomem * base)7487f6283edSTony Lindgren cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
7499b3452d1SSebastian Andrzej Siewior {
7509b3452d1SSebastian Andrzej Siewior struct cppi41_dma_controller *controller;
751297d7fe9SAlexandre Bailon int channel_size;
75248054147SSebastian Andrzej Siewior int ret = 0;
7539b3452d1SSebastian Andrzej Siewior
754b0a688ddSFelipe Balbi if (!musb->controller->parent->of_node) {
7559b3452d1SSebastian Andrzej Siewior dev_err(musb->controller, "Need DT for the DMA engine.\n");
7569b3452d1SSebastian Andrzej Siewior return NULL;
7579b3452d1SSebastian Andrzej Siewior }
7589b3452d1SSebastian Andrzej Siewior
7599b3452d1SSebastian Andrzej Siewior controller = kzalloc(sizeof(*controller), GFP_KERNEL);
7609b3452d1SSebastian Andrzej Siewior if (!controller)
7619b3452d1SSebastian Andrzej Siewior goto kzalloc_fail;
7629b3452d1SSebastian Andrzej Siewior
763a655f481SSebastian Andrzej Siewior hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
764a655f481SSebastian Andrzej Siewior controller->early_tx.function = cppi41_recheck_tx_req;
765a655f481SSebastian Andrzej Siewior INIT_LIST_HEAD(&controller->early_tx_list);
7669b3452d1SSebastian Andrzej Siewior
7679b3452d1SSebastian Andrzej Siewior controller->controller.channel_alloc = cppi41_dma_channel_allocate;
7689b3452d1SSebastian Andrzej Siewior controller->controller.channel_release = cppi41_dma_channel_release;
7699b3452d1SSebastian Andrzej Siewior controller->controller.channel_program = cppi41_dma_channel_program;
7709b3452d1SSebastian Andrzej Siewior controller->controller.channel_abort = cppi41_dma_channel_abort;
7719b3452d1SSebastian Andrzej Siewior controller->controller.is_compatible = cppi41_is_compatible;
772050dc900SAlexandre Bailon controller->controller.musb = musb;
7739b3452d1SSebastian Andrzej Siewior
774dc8fca6cSBin Liu if (musb->ops->quirks & MUSB_DA8XX) {
775bfa53e0eSAlexandre Bailon controller->tdown_reg = DA8XX_USB_TEARDOWN;
776bfa53e0eSAlexandre Bailon controller->autoreq_reg = DA8XX_USB_AUTOREQ;
777e10c5b0cSAlexandre Bailon controller->set_dma_mode = da8xx_set_dma_mode;
778297d7fe9SAlexandre Bailon controller->num_channels = DA8XX_DMA_NUM_CHANNELS;
779bfa53e0eSAlexandre Bailon } else {
780bfa53e0eSAlexandre Bailon controller->tdown_reg = USB_TDOWN;
781bfa53e0eSAlexandre Bailon controller->autoreq_reg = USB_CTRL_AUTOREQ;
782e10c5b0cSAlexandre Bailon controller->set_dma_mode = cppi41_set_dma_mode;
783297d7fe9SAlexandre Bailon controller->num_channels = MUSB_DMA_NUM_CHANNELS;
784bfa53e0eSAlexandre Bailon }
785bfa53e0eSAlexandre Bailon
786297d7fe9SAlexandre Bailon channel_size = controller->num_channels *
787297d7fe9SAlexandre Bailon sizeof(struct cppi41_dma_channel);
788297d7fe9SAlexandre Bailon controller->rx_channel = kzalloc(channel_size, GFP_KERNEL);
789297d7fe9SAlexandre Bailon if (!controller->rx_channel)
790297d7fe9SAlexandre Bailon goto rx_channel_alloc_fail;
791297d7fe9SAlexandre Bailon controller->tx_channel = kzalloc(channel_size, GFP_KERNEL);
792297d7fe9SAlexandre Bailon if (!controller->tx_channel)
793297d7fe9SAlexandre Bailon goto tx_channel_alloc_fail;
794297d7fe9SAlexandre Bailon
7959b3452d1SSebastian Andrzej Siewior ret = cppi41_dma_controller_start(controller);
7969b3452d1SSebastian Andrzej Siewior if (ret)
7979b3452d1SSebastian Andrzej Siewior goto plat_get_fail;
7989b3452d1SSebastian Andrzej Siewior return &controller->controller;
7999b3452d1SSebastian Andrzej Siewior
8009b3452d1SSebastian Andrzej Siewior plat_get_fail:
801297d7fe9SAlexandre Bailon kfree(controller->tx_channel);
802297d7fe9SAlexandre Bailon tx_channel_alloc_fail:
803297d7fe9SAlexandre Bailon kfree(controller->rx_channel);
804297d7fe9SAlexandre Bailon rx_channel_alloc_fail:
8059b3452d1SSebastian Andrzej Siewior kfree(controller);
8069b3452d1SSebastian Andrzej Siewior kzalloc_fail:
80748054147SSebastian Andrzej Siewior if (ret == -EPROBE_DEFER)
80848054147SSebastian Andrzej Siewior return ERR_PTR(ret);
8099b3452d1SSebastian Andrzej Siewior return NULL;
8109b3452d1SSebastian Andrzej Siewior }
8117f6283edSTony Lindgren EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);
812