18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/device.h>
38c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
48c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
58c2ecf20Sopenharmony_ci#include <linux/sizes.h>
68c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
78c2ecf20Sopenharmony_ci#include <linux/of.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "cppi_dma.h"
108c2ecf20Sopenharmony_ci#include "musb_core.h"
118c2ecf20Sopenharmony_ci#include "musb_trace.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define EP_MODE_AUTOREQ_NONE		0
168c2ecf20Sopenharmony_ci#define EP_MODE_AUTOREQ_ALL_NEOP	1
178c2ecf20Sopenharmony_ci#define EP_MODE_AUTOREQ_ALWAYS		3
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define EP_MODE_DMA_TRANSPARENT		0
208c2ecf20Sopenharmony_ci#define EP_MODE_DMA_RNDIS		1
218c2ecf20Sopenharmony_ci#define EP_MODE_DMA_GEN_RNDIS		3
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define USB_CTRL_TX_MODE	0x70
248c2ecf20Sopenharmony_ci#define USB_CTRL_RX_MODE	0x74
258c2ecf20Sopenharmony_ci#define USB_CTRL_AUTOREQ	0xd0
268c2ecf20Sopenharmony_ci#define USB_TDOWN		0xd8
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define MUSB_DMA_NUM_CHANNELS 15
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define DA8XX_USB_MODE		0x10
318c2ecf20Sopenharmony_ci#define DA8XX_USB_AUTOREQ	0x14
328c2ecf20Sopenharmony_ci#define DA8XX_USB_TEARDOWN	0x1c
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define DA8XX_DMA_NUM_CHANNELS 4
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct cppi41_dma_controller {
378c2ecf20Sopenharmony_ci	struct dma_controller controller;
388c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *rx_channel;
398c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *tx_channel;
408c2ecf20Sopenharmony_ci	struct hrtimer early_tx;
418c2ecf20Sopenharmony_ci	struct list_head early_tx_list;
428c2ecf20Sopenharmony_ci	u32 rx_mode;
438c2ecf20Sopenharmony_ci	u32 tx_mode;
448c2ecf20Sopenharmony_ci	u32 auto_req;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	u32 tdown_reg;
478c2ecf20Sopenharmony_ci	u32 autoreq_reg;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	void (*set_dma_mode)(struct cppi41_dma_channel *cppi41_channel,
508c2ecf20Sopenharmony_ci			     unsigned int mode);
518c2ecf20Sopenharmony_ci	u8 num_channels;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	u16 csr;
578c2ecf20Sopenharmony_ci	u8 toggle;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx)
608c2ecf20Sopenharmony_ci		return;
618c2ecf20Sopenharmony_ci	if (!is_host_active(cppi41_channel->controller->controller.musb))
628c2ecf20Sopenharmony_ci		return;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
658c2ecf20Sopenharmony_ci	toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	cppi41_channel->usb_toggle = toggle;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
738c2ecf20Sopenharmony_ci	struct musb *musb = hw_ep->musb;
748c2ecf20Sopenharmony_ci	u16 csr;
758c2ecf20Sopenharmony_ci	u8 toggle;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx)
788c2ecf20Sopenharmony_ci		return;
798c2ecf20Sopenharmony_ci	if (!is_host_active(musb))
808c2ecf20Sopenharmony_ci		return;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	musb_ep_select(musb->mregs, hw_ep->epnum);
838c2ecf20Sopenharmony_ci	csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
848c2ecf20Sopenharmony_ci	toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/*
878c2ecf20Sopenharmony_ci	 * AM335x Advisory 1.0.13: Due to internal synchronisation error the
888c2ecf20Sopenharmony_ci	 * data toggle may reset from DATA1 to DATA0 during receiving data from
898c2ecf20Sopenharmony_ci	 * more than one endpoint.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	if (!toggle && toggle == cppi41_channel->usb_toggle) {
928c2ecf20Sopenharmony_ci		csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
938c2ecf20Sopenharmony_ci		musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
948c2ecf20Sopenharmony_ci		musb_dbg(musb, "Restoring DATA1 toggle.");
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	cppi41_channel->usb_toggle = toggle;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	u8		epnum = hw_ep->epnum;
1038c2ecf20Sopenharmony_ci	struct musb	*musb = hw_ep->musb;
1048c2ecf20Sopenharmony_ci	void __iomem	*epio = musb->endpoints[epnum].regs;
1058c2ecf20Sopenharmony_ci	u16		csr;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	musb_ep_select(musb->mregs, hw_ep->epnum);
1088c2ecf20Sopenharmony_ci	csr = musb_readw(epio, MUSB_TXCSR);
1098c2ecf20Sopenharmony_ci	if (csr & MUSB_TXCSR_TXPKTRDY)
1108c2ecf20Sopenharmony_ci		return false;
1118c2ecf20Sopenharmony_ci	return true;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic void cppi41_dma_callback(void *private_data,
1158c2ecf20Sopenharmony_ci				const struct dmaengine_result *result);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
1208c2ecf20Sopenharmony_ci	struct musb *musb = hw_ep->musb;
1218c2ecf20Sopenharmony_ci	void __iomem *epio = hw_ep->regs;
1228c2ecf20Sopenharmony_ci	u16 csr;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!cppi41_channel->prog_len ||
1258c2ecf20Sopenharmony_ci	    (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci		/* done, complete */
1288c2ecf20Sopenharmony_ci		cppi41_channel->channel.actual_len =
1298c2ecf20Sopenharmony_ci			cppi41_channel->transferred;
1308c2ecf20Sopenharmony_ci		cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
1318c2ecf20Sopenharmony_ci		cppi41_channel->channel.rx_packet_done = true;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci		/*
1348c2ecf20Sopenharmony_ci		 * transmit ZLP using PIO mode for transfers which size is
1358c2ecf20Sopenharmony_ci		 * multiple of EP packet size.
1368c2ecf20Sopenharmony_ci		 */
1378c2ecf20Sopenharmony_ci		if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
1388c2ecf20Sopenharmony_ci					cppi41_channel->packet_sz) == 0) {
1398c2ecf20Sopenharmony_ci			musb_ep_select(musb->mregs, hw_ep->epnum);
1408c2ecf20Sopenharmony_ci			csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
1418c2ecf20Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR, csr);
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		trace_musb_cppi41_done(cppi41_channel);
1458c2ecf20Sopenharmony_ci		musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
1468c2ecf20Sopenharmony_ci	} else {
1478c2ecf20Sopenharmony_ci		/* next iteration, reload */
1488c2ecf20Sopenharmony_ci		struct dma_chan *dc = cppi41_channel->dc;
1498c2ecf20Sopenharmony_ci		struct dma_async_tx_descriptor *dma_desc;
1508c2ecf20Sopenharmony_ci		enum dma_transfer_direction direction;
1518c2ecf20Sopenharmony_ci		u32 remain_bytes;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		cppi41_channel->buf_addr += cppi41_channel->packet_sz;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		remain_bytes = cppi41_channel->total_len;
1568c2ecf20Sopenharmony_ci		remain_bytes -= cppi41_channel->transferred;
1578c2ecf20Sopenharmony_ci		remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
1588c2ecf20Sopenharmony_ci		cppi41_channel->prog_len = remain_bytes;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
1618c2ecf20Sopenharmony_ci			: DMA_DEV_TO_MEM;
1628c2ecf20Sopenharmony_ci		dma_desc = dmaengine_prep_slave_single(dc,
1638c2ecf20Sopenharmony_ci				cppi41_channel->buf_addr,
1648c2ecf20Sopenharmony_ci				remain_bytes,
1658c2ecf20Sopenharmony_ci				direction,
1668c2ecf20Sopenharmony_ci				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1678c2ecf20Sopenharmony_ci		if (WARN_ON(!dma_desc))
1688c2ecf20Sopenharmony_ci			return;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		dma_desc->callback_result = cppi41_dma_callback;
1718c2ecf20Sopenharmony_ci		dma_desc->callback_param = &cppi41_channel->channel;
1728c2ecf20Sopenharmony_ci		cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
1738c2ecf20Sopenharmony_ci		trace_musb_cppi41_cont(cppi41_channel);
1748c2ecf20Sopenharmony_ci		dma_async_issue_pending(dc);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci		if (!cppi41_channel->is_tx) {
1778c2ecf20Sopenharmony_ci			musb_ep_select(musb->mregs, hw_ep->epnum);
1788c2ecf20Sopenharmony_ci			csr = musb_readw(epio, MUSB_RXCSR);
1798c2ecf20Sopenharmony_ci			csr |= MUSB_RXCSR_H_REQPKT;
1808c2ecf20Sopenharmony_ci			musb_writew(epio, MUSB_RXCSR, csr);
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller;
1888c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel, *n;
1898c2ecf20Sopenharmony_ci	struct musb *musb;
1908c2ecf20Sopenharmony_ci	unsigned long flags;
1918c2ecf20Sopenharmony_ci	enum hrtimer_restart ret = HRTIMER_NORESTART;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	controller = container_of(timer, struct cppi41_dma_controller,
1948c2ecf20Sopenharmony_ci			early_tx);
1958c2ecf20Sopenharmony_ci	musb = controller->controller.musb;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
1988c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
1998c2ecf20Sopenharmony_ci			tx_check) {
2008c2ecf20Sopenharmony_ci		bool empty;
2018c2ecf20Sopenharmony_ci		struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		empty = musb_is_tx_fifo_empty(hw_ep);
2048c2ecf20Sopenharmony_ci		if (empty) {
2058c2ecf20Sopenharmony_ci			list_del_init(&cppi41_channel->tx_check);
2068c2ecf20Sopenharmony_ci			cppi41_trans_done(cppi41_channel);
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (!list_empty(&controller->early_tx_list) &&
2118c2ecf20Sopenharmony_ci	    !hrtimer_is_queued(&controller->early_tx)) {
2128c2ecf20Sopenharmony_ci		ret = HRTIMER_RESTART;
2138c2ecf20Sopenharmony_ci		hrtimer_forward_now(&controller->early_tx, 20 * NSEC_PER_USEC);
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
2178c2ecf20Sopenharmony_ci	return ret;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void cppi41_dma_callback(void *private_data,
2218c2ecf20Sopenharmony_ci				const struct dmaengine_result *result)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	struct dma_channel *channel = private_data;
2248c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
2258c2ecf20Sopenharmony_ci	struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
2268c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller;
2278c2ecf20Sopenharmony_ci	struct musb *musb = hw_ep->musb;
2288c2ecf20Sopenharmony_ci	unsigned long flags;
2298c2ecf20Sopenharmony_ci	struct dma_tx_state txstate;
2308c2ecf20Sopenharmony_ci	u32 transferred;
2318c2ecf20Sopenharmony_ci	int is_hs = 0;
2328c2ecf20Sopenharmony_ci	bool empty;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	controller = cppi41_channel->controller;
2358c2ecf20Sopenharmony_ci	if (controller->controller.dma_callback)
2368c2ecf20Sopenharmony_ci		controller->controller.dma_callback(&controller->controller);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	if (result->result == DMA_TRANS_ABORTED)
2398c2ecf20Sopenharmony_ci		return;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
2448c2ecf20Sopenharmony_ci			&txstate);
2458c2ecf20Sopenharmony_ci	transferred = cppi41_channel->prog_len - txstate.residue;
2468c2ecf20Sopenharmony_ci	cppi41_channel->transferred += transferred;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	trace_musb_cppi41_gb(cppi41_channel);
2498c2ecf20Sopenharmony_ci	update_rx_toggle(cppi41_channel);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (cppi41_channel->transferred == cppi41_channel->total_len ||
2528c2ecf20Sopenharmony_ci			transferred < cppi41_channel->packet_sz)
2538c2ecf20Sopenharmony_ci		cppi41_channel->prog_len = 0;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx) {
2568c2ecf20Sopenharmony_ci		u8 type;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		if (is_host_active(musb))
2598c2ecf20Sopenharmony_ci			type = hw_ep->out_qh->type;
2608c2ecf20Sopenharmony_ci		else
2618c2ecf20Sopenharmony_ci			type = hw_ep->ep_in.type;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		if (type == USB_ENDPOINT_XFER_ISOC)
2648c2ecf20Sopenharmony_ci			/*
2658c2ecf20Sopenharmony_ci			 * Don't use the early-TX-interrupt workaround below
2668c2ecf20Sopenharmony_ci			 * for Isoch transfter. Since Isoch are periodic
2678c2ecf20Sopenharmony_ci			 * transfer, by the time the next transfer is
2688c2ecf20Sopenharmony_ci			 * scheduled, the current one should be done already.
2698c2ecf20Sopenharmony_ci			 *
2708c2ecf20Sopenharmony_ci			 * This avoids audio playback underrun issue.
2718c2ecf20Sopenharmony_ci			 */
2728c2ecf20Sopenharmony_ci			empty = true;
2738c2ecf20Sopenharmony_ci		else
2748c2ecf20Sopenharmony_ci			empty = musb_is_tx_fifo_empty(hw_ep);
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (!cppi41_channel->is_tx || empty) {
2788c2ecf20Sopenharmony_ci		cppi41_trans_done(cppi41_channel);
2798c2ecf20Sopenharmony_ci		goto out;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/*
2838c2ecf20Sopenharmony_ci	 * On AM335x it has been observed that the TX interrupt fires
2848c2ecf20Sopenharmony_ci	 * too early that means the TXFIFO is not yet empty but the DMA
2858c2ecf20Sopenharmony_ci	 * engine says that it is done with the transfer. We don't
2868c2ecf20Sopenharmony_ci	 * receive a FIFO empty interrupt so the only thing we can do is
2878c2ecf20Sopenharmony_ci	 * to poll for the bit. On HS it usually takes 2us, on FS around
2888c2ecf20Sopenharmony_ci	 * 110us - 150us depending on the transfer size.
2898c2ecf20Sopenharmony_ci	 * We spin on HS (no longer than than 25us and setup a timer on
2908c2ecf20Sopenharmony_ci	 * FS to check for the bit and complete the transfer.
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	if (is_host_active(musb)) {
2938c2ecf20Sopenharmony_ci		if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
2948c2ecf20Sopenharmony_ci			is_hs = 1;
2958c2ecf20Sopenharmony_ci	} else {
2968c2ecf20Sopenharmony_ci		if (musb->g.speed == USB_SPEED_HIGH)
2978c2ecf20Sopenharmony_ci			is_hs = 1;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci	if (is_hs) {
3008c2ecf20Sopenharmony_ci		unsigned wait = 25;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		do {
3038c2ecf20Sopenharmony_ci			empty = musb_is_tx_fifo_empty(hw_ep);
3048c2ecf20Sopenharmony_ci			if (empty) {
3058c2ecf20Sopenharmony_ci				cppi41_trans_done(cppi41_channel);
3068c2ecf20Sopenharmony_ci				goto out;
3078c2ecf20Sopenharmony_ci			}
3088c2ecf20Sopenharmony_ci			wait--;
3098c2ecf20Sopenharmony_ci			if (!wait)
3108c2ecf20Sopenharmony_ci				break;
3118c2ecf20Sopenharmony_ci			cpu_relax();
3128c2ecf20Sopenharmony_ci		} while (1);
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci	list_add_tail(&cppi41_channel->tx_check,
3158c2ecf20Sopenharmony_ci			&controller->early_tx_list);
3168c2ecf20Sopenharmony_ci	if (!hrtimer_is_queued(&controller->early_tx)) {
3178c2ecf20Sopenharmony_ci		unsigned long usecs = cppi41_channel->total_len / 10;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci		hrtimer_start_range_ns(&controller->early_tx,
3208c2ecf20Sopenharmony_ci				       usecs * NSEC_PER_USEC,
3218c2ecf20Sopenharmony_ci				       20 * NSEC_PER_USEC,
3228c2ecf20Sopenharmony_ci				       HRTIMER_MODE_REL);
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ciout:
3268c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	unsigned shift;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	shift = (ep - 1) * 2;
3348c2ecf20Sopenharmony_ci	old &= ~(3 << shift);
3358c2ecf20Sopenharmony_ci	old |= mode << shift;
3368c2ecf20Sopenharmony_ci	return old;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
3408c2ecf20Sopenharmony_ci		unsigned mode)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
3438c2ecf20Sopenharmony_ci	struct musb *musb = controller->controller.musb;
3448c2ecf20Sopenharmony_ci	u32 port;
3458c2ecf20Sopenharmony_ci	u32 new_mode;
3468c2ecf20Sopenharmony_ci	u32 old_mode;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx)
3498c2ecf20Sopenharmony_ci		old_mode = controller->tx_mode;
3508c2ecf20Sopenharmony_ci	else
3518c2ecf20Sopenharmony_ci		old_mode = controller->rx_mode;
3528c2ecf20Sopenharmony_ci	port = cppi41_channel->port_num;
3538c2ecf20Sopenharmony_ci	new_mode = update_ep_mode(port, mode, old_mode);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (new_mode == old_mode)
3568c2ecf20Sopenharmony_ci		return;
3578c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx) {
3588c2ecf20Sopenharmony_ci		controller->tx_mode = new_mode;
3598c2ecf20Sopenharmony_ci		musb_writel(musb->ctrl_base, USB_CTRL_TX_MODE, new_mode);
3608c2ecf20Sopenharmony_ci	} else {
3618c2ecf20Sopenharmony_ci		controller->rx_mode = new_mode;
3628c2ecf20Sopenharmony_ci		musb_writel(musb->ctrl_base, USB_CTRL_RX_MODE, new_mode);
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void da8xx_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
3678c2ecf20Sopenharmony_ci		unsigned int mode)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
3708c2ecf20Sopenharmony_ci	struct musb *musb = controller->controller.musb;
3718c2ecf20Sopenharmony_ci	unsigned int shift;
3728c2ecf20Sopenharmony_ci	u32 port;
3738c2ecf20Sopenharmony_ci	u32 new_mode;
3748c2ecf20Sopenharmony_ci	u32 old_mode;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	old_mode = controller->tx_mode;
3778c2ecf20Sopenharmony_ci	port = cppi41_channel->port_num;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	shift = (port - 1) * 4;
3808c2ecf20Sopenharmony_ci	if (!cppi41_channel->is_tx)
3818c2ecf20Sopenharmony_ci		shift += 16;
3828c2ecf20Sopenharmony_ci	new_mode = old_mode & ~(3 << shift);
3838c2ecf20Sopenharmony_ci	new_mode |= mode << shift;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	if (new_mode == old_mode)
3868c2ecf20Sopenharmony_ci		return;
3878c2ecf20Sopenharmony_ci	controller->tx_mode = new_mode;
3888c2ecf20Sopenharmony_ci	musb_writel(musb->ctrl_base, DA8XX_USB_MODE, new_mode);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
3938c2ecf20Sopenharmony_ci		unsigned mode)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
3968c2ecf20Sopenharmony_ci	u32 port;
3978c2ecf20Sopenharmony_ci	u32 new_mode;
3988c2ecf20Sopenharmony_ci	u32 old_mode;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	old_mode = controller->auto_req;
4018c2ecf20Sopenharmony_ci	port = cppi41_channel->port_num;
4028c2ecf20Sopenharmony_ci	new_mode = update_ep_mode(port, mode, old_mode);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (new_mode == old_mode)
4058c2ecf20Sopenharmony_ci		return;
4068c2ecf20Sopenharmony_ci	controller->auto_req = new_mode;
4078c2ecf20Sopenharmony_ci	musb_writel(controller->controller.musb->ctrl_base,
4088c2ecf20Sopenharmony_ci		    controller->autoreq_reg, new_mode);
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic bool cppi41_configure_channel(struct dma_channel *channel,
4128c2ecf20Sopenharmony_ci				u16 packet_sz, u8 mode,
4138c2ecf20Sopenharmony_ci				dma_addr_t dma_addr, u32 len)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
4168c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
4178c2ecf20Sopenharmony_ci	struct dma_chan *dc = cppi41_channel->dc;
4188c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *dma_desc;
4198c2ecf20Sopenharmony_ci	enum dma_transfer_direction direction;
4208c2ecf20Sopenharmony_ci	struct musb *musb = cppi41_channel->controller->controller.musb;
4218c2ecf20Sopenharmony_ci	unsigned use_gen_rndis = 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	cppi41_channel->buf_addr = dma_addr;
4248c2ecf20Sopenharmony_ci	cppi41_channel->total_len = len;
4258c2ecf20Sopenharmony_ci	cppi41_channel->transferred = 0;
4268c2ecf20Sopenharmony_ci	cppi41_channel->packet_sz = packet_sz;
4278c2ecf20Sopenharmony_ci	cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	/*
4308c2ecf20Sopenharmony_ci	 * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
4318c2ecf20Sopenharmony_ci	 * than max packet size at a time.
4328c2ecf20Sopenharmony_ci	 */
4338c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx)
4348c2ecf20Sopenharmony_ci		use_gen_rndis = 1;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (use_gen_rndis) {
4378c2ecf20Sopenharmony_ci		/* RNDIS mode */
4388c2ecf20Sopenharmony_ci		if (len > packet_sz) {
4398c2ecf20Sopenharmony_ci			musb_writel(musb->ctrl_base,
4408c2ecf20Sopenharmony_ci				RNDIS_REG(cppi41_channel->port_num), len);
4418c2ecf20Sopenharmony_ci			/* gen rndis */
4428c2ecf20Sopenharmony_ci			controller->set_dma_mode(cppi41_channel,
4438c2ecf20Sopenharmony_ci					EP_MODE_DMA_GEN_RNDIS);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci			/* auto req */
4468c2ecf20Sopenharmony_ci			cppi41_set_autoreq_mode(cppi41_channel,
4478c2ecf20Sopenharmony_ci					EP_MODE_AUTOREQ_ALL_NEOP);
4488c2ecf20Sopenharmony_ci		} else {
4498c2ecf20Sopenharmony_ci			musb_writel(musb->ctrl_base,
4508c2ecf20Sopenharmony_ci					RNDIS_REG(cppi41_channel->port_num), 0);
4518c2ecf20Sopenharmony_ci			controller->set_dma_mode(cppi41_channel,
4528c2ecf20Sopenharmony_ci					EP_MODE_DMA_TRANSPARENT);
4538c2ecf20Sopenharmony_ci			cppi41_set_autoreq_mode(cppi41_channel,
4548c2ecf20Sopenharmony_ci					EP_MODE_AUTOREQ_NONE);
4558c2ecf20Sopenharmony_ci		}
4568c2ecf20Sopenharmony_ci	} else {
4578c2ecf20Sopenharmony_ci		/* fallback mode */
4588c2ecf20Sopenharmony_ci		controller->set_dma_mode(cppi41_channel,
4598c2ecf20Sopenharmony_ci				EP_MODE_DMA_TRANSPARENT);
4608c2ecf20Sopenharmony_ci		cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
4618c2ecf20Sopenharmony_ci		len = min_t(u32, packet_sz, len);
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci	cppi41_channel->prog_len = len;
4648c2ecf20Sopenharmony_ci	direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
4658c2ecf20Sopenharmony_ci	dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
4668c2ecf20Sopenharmony_ci			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
4678c2ecf20Sopenharmony_ci	if (!dma_desc)
4688c2ecf20Sopenharmony_ci		return false;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	dma_desc->callback_result = cppi41_dma_callback;
4718c2ecf20Sopenharmony_ci	dma_desc->callback_param = channel;
4728c2ecf20Sopenharmony_ci	cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
4738c2ecf20Sopenharmony_ci	cppi41_channel->channel.rx_packet_done = false;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	trace_musb_cppi41_config(cppi41_channel);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	save_rx_toggle(cppi41_channel);
4788c2ecf20Sopenharmony_ci	dma_async_issue_pending(dc);
4798c2ecf20Sopenharmony_ci	return true;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
4838c2ecf20Sopenharmony_ci				struct musb_hw_ep *hw_ep, u8 is_tx)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = container_of(c,
4868c2ecf20Sopenharmony_ci			struct cppi41_dma_controller, controller);
4878c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = NULL;
4888c2ecf20Sopenharmony_ci	u8 ch_num = hw_ep->epnum - 1;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (ch_num >= controller->num_channels)
4918c2ecf20Sopenharmony_ci		return NULL;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (is_tx)
4948c2ecf20Sopenharmony_ci		cppi41_channel = &controller->tx_channel[ch_num];
4958c2ecf20Sopenharmony_ci	else
4968c2ecf20Sopenharmony_ci		cppi41_channel = &controller->rx_channel[ch_num];
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	if (!cppi41_channel->dc)
4998c2ecf20Sopenharmony_ci		return NULL;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (cppi41_channel->is_allocated)
5028c2ecf20Sopenharmony_ci		return NULL;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	cppi41_channel->hw_ep = hw_ep;
5058c2ecf20Sopenharmony_ci	cppi41_channel->is_allocated = 1;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	trace_musb_cppi41_alloc(cppi41_channel);
5088c2ecf20Sopenharmony_ci	return &cppi41_channel->channel;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic void cppi41_dma_channel_release(struct dma_channel *channel)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	trace_musb_cppi41_free(cppi41_channel);
5168c2ecf20Sopenharmony_ci	if (cppi41_channel->is_allocated) {
5178c2ecf20Sopenharmony_ci		cppi41_channel->is_allocated = 0;
5188c2ecf20Sopenharmony_ci		channel->status = MUSB_DMA_STATUS_FREE;
5198c2ecf20Sopenharmony_ci		channel->actual_len = 0;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic int cppi41_dma_channel_program(struct dma_channel *channel,
5248c2ecf20Sopenharmony_ci				u16 packet_sz, u8 mode,
5258c2ecf20Sopenharmony_ci				dma_addr_t dma_addr, u32 len)
5268c2ecf20Sopenharmony_ci{
5278c2ecf20Sopenharmony_ci	int ret;
5288c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5298c2ecf20Sopenharmony_ci	int hb_mult = 0;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
5328c2ecf20Sopenharmony_ci		channel->status == MUSB_DMA_STATUS_BUSY);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (is_host_active(cppi41_channel->controller->controller.musb)) {
5358c2ecf20Sopenharmony_ci		if (cppi41_channel->is_tx)
5368c2ecf20Sopenharmony_ci			hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
5378c2ecf20Sopenharmony_ci		else
5388c2ecf20Sopenharmony_ci			hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	channel->status = MUSB_DMA_STATUS_BUSY;
5428c2ecf20Sopenharmony_ci	channel->actual_len = 0;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (hb_mult)
5458c2ecf20Sopenharmony_ci		packet_sz = hb_mult * (packet_sz & 0x7FF);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
5488c2ecf20Sopenharmony_ci	if (!ret)
5498c2ecf20Sopenharmony_ci		channel->status = MUSB_DMA_STATUS_FREE;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	return ret;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistatic int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
5558c2ecf20Sopenharmony_ci		void *buf, u32 length)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5588c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
5598c2ecf20Sopenharmony_ci	struct musb *musb = controller->controller.musb;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (is_host_active(musb)) {
5628c2ecf20Sopenharmony_ci		WARN_ON(1);
5638c2ecf20Sopenharmony_ci		return 1;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci	if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
5668c2ecf20Sopenharmony_ci		return 0;
5678c2ecf20Sopenharmony_ci	if (cppi41_channel->is_tx)
5688c2ecf20Sopenharmony_ci		return 1;
5698c2ecf20Sopenharmony_ci	/* AM335x Advisory 1.0.13. No workaround for device RX mode */
5708c2ecf20Sopenharmony_ci	return 0;
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic int cppi41_dma_channel_abort(struct dma_channel *channel)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel = channel->private_data;
5768c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = cppi41_channel->controller;
5778c2ecf20Sopenharmony_ci	struct musb *musb = controller->controller.musb;
5788c2ecf20Sopenharmony_ci	void __iomem *epio = cppi41_channel->hw_ep->regs;
5798c2ecf20Sopenharmony_ci	int tdbit;
5808c2ecf20Sopenharmony_ci	int ret;
5818c2ecf20Sopenharmony_ci	unsigned is_tx;
5828c2ecf20Sopenharmony_ci	u16 csr;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	is_tx = cppi41_channel->is_tx;
5858c2ecf20Sopenharmony_ci	trace_musb_cppi41_abort(cppi41_channel);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
5888c2ecf20Sopenharmony_ci		return 0;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	list_del_init(&cppi41_channel->tx_check);
5918c2ecf20Sopenharmony_ci	if (is_tx) {
5928c2ecf20Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
5938c2ecf20Sopenharmony_ci		csr &= ~MUSB_TXCSR_DMAENAB;
5948c2ecf20Sopenharmony_ci		musb_writew(epio, MUSB_TXCSR, csr);
5958c2ecf20Sopenharmony_ci	} else {
5968c2ecf20Sopenharmony_ci		cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		/* delay to drain to cppi dma pipeline for isoch */
5998c2ecf20Sopenharmony_ci		udelay(250);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci		csr = musb_readw(epio, MUSB_RXCSR);
6028c2ecf20Sopenharmony_ci		csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
6038c2ecf20Sopenharmony_ci		musb_writew(epio, MUSB_RXCSR, csr);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci		/* wait to drain cppi dma pipe line */
6068c2ecf20Sopenharmony_ci		udelay(50);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		csr = musb_readw(epio, MUSB_RXCSR);
6098c2ecf20Sopenharmony_ci		if (csr & MUSB_RXCSR_RXPKTRDY) {
6108c2ecf20Sopenharmony_ci			csr |= MUSB_RXCSR_FLUSHFIFO;
6118c2ecf20Sopenharmony_ci			musb_writew(epio, MUSB_RXCSR, csr);
6128c2ecf20Sopenharmony_ci			musb_writew(epio, MUSB_RXCSR, csr);
6138c2ecf20Sopenharmony_ci		}
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* DA8xx Advisory 2.3.27: wait 250 ms before to start the teardown */
6178c2ecf20Sopenharmony_ci	if (musb->ops->quirks & MUSB_DA8XX)
6188c2ecf20Sopenharmony_ci		mdelay(250);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	tdbit = 1 << cppi41_channel->port_num;
6218c2ecf20Sopenharmony_ci	if (is_tx)
6228c2ecf20Sopenharmony_ci		tdbit <<= 16;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	do {
6258c2ecf20Sopenharmony_ci		if (is_tx)
6268c2ecf20Sopenharmony_ci			musb_writel(musb->ctrl_base, controller->tdown_reg,
6278c2ecf20Sopenharmony_ci				    tdbit);
6288c2ecf20Sopenharmony_ci		ret = dmaengine_terminate_all(cppi41_channel->dc);
6298c2ecf20Sopenharmony_ci	} while (ret == -EAGAIN);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if (is_tx) {
6328c2ecf20Sopenharmony_ci		musb_writel(musb->ctrl_base, controller->tdown_reg, tdbit);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci		csr = musb_readw(epio, MUSB_TXCSR);
6358c2ecf20Sopenharmony_ci		if (csr & MUSB_TXCSR_TXPKTRDY) {
6368c2ecf20Sopenharmony_ci			csr |= MUSB_TXCSR_FLUSHFIFO;
6378c2ecf20Sopenharmony_ci			musb_writew(epio, MUSB_TXCSR, csr);
6388c2ecf20Sopenharmony_ci		}
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
6428c2ecf20Sopenharmony_ci	return 0;
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	struct dma_chan *dc;
6488c2ecf20Sopenharmony_ci	int i;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	for (i = 0; i < ctrl->num_channels; i++) {
6518c2ecf20Sopenharmony_ci		dc = ctrl->tx_channel[i].dc;
6528c2ecf20Sopenharmony_ci		if (dc)
6538c2ecf20Sopenharmony_ci			dma_release_channel(dc);
6548c2ecf20Sopenharmony_ci		dc = ctrl->rx_channel[i].dc;
6558c2ecf20Sopenharmony_ci		if (dc)
6568c2ecf20Sopenharmony_ci			dma_release_channel(dc);
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	cppi41_release_all_dma_chans(controller);
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	struct musb *musb = controller->controller.musb;
6688c2ecf20Sopenharmony_ci	struct device *dev = musb->controller;
6698c2ecf20Sopenharmony_ci	struct device_node *np = dev->parent->of_node;
6708c2ecf20Sopenharmony_ci	struct cppi41_dma_channel *cppi41_channel;
6718c2ecf20Sopenharmony_ci	int count;
6728c2ecf20Sopenharmony_ci	int i;
6738c2ecf20Sopenharmony_ci	int ret;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	count = of_property_count_strings(np, "dma-names");
6768c2ecf20Sopenharmony_ci	if (count < 0)
6778c2ecf20Sopenharmony_ci		return count;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
6808c2ecf20Sopenharmony_ci		struct dma_chan *dc;
6818c2ecf20Sopenharmony_ci		struct dma_channel *musb_dma;
6828c2ecf20Sopenharmony_ci		const char *str;
6838c2ecf20Sopenharmony_ci		unsigned is_tx;
6848c2ecf20Sopenharmony_ci		unsigned int port;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci		ret = of_property_read_string_index(np, "dma-names", i, &str);
6878c2ecf20Sopenharmony_ci		if (ret)
6888c2ecf20Sopenharmony_ci			goto err;
6898c2ecf20Sopenharmony_ci		if (strstarts(str, "tx"))
6908c2ecf20Sopenharmony_ci			is_tx = 1;
6918c2ecf20Sopenharmony_ci		else if (strstarts(str, "rx"))
6928c2ecf20Sopenharmony_ci			is_tx = 0;
6938c2ecf20Sopenharmony_ci		else {
6948c2ecf20Sopenharmony_ci			dev_err(dev, "Wrong dmatype %s\n", str);
6958c2ecf20Sopenharmony_ci			goto err;
6968c2ecf20Sopenharmony_ci		}
6978c2ecf20Sopenharmony_ci		ret = kstrtouint(str + 2, 0, &port);
6988c2ecf20Sopenharmony_ci		if (ret)
6998c2ecf20Sopenharmony_ci			goto err;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		ret = -EINVAL;
7028c2ecf20Sopenharmony_ci		if (port > controller->num_channels || !port)
7038c2ecf20Sopenharmony_ci			goto err;
7048c2ecf20Sopenharmony_ci		if (is_tx)
7058c2ecf20Sopenharmony_ci			cppi41_channel = &controller->tx_channel[port - 1];
7068c2ecf20Sopenharmony_ci		else
7078c2ecf20Sopenharmony_ci			cppi41_channel = &controller->rx_channel[port - 1];
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		cppi41_channel->controller = controller;
7108c2ecf20Sopenharmony_ci		cppi41_channel->port_num = port;
7118c2ecf20Sopenharmony_ci		cppi41_channel->is_tx = is_tx;
7128c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&cppi41_channel->tx_check);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci		musb_dma = &cppi41_channel->channel;
7158c2ecf20Sopenharmony_ci		musb_dma->private_data = cppi41_channel;
7168c2ecf20Sopenharmony_ci		musb_dma->status = MUSB_DMA_STATUS_FREE;
7178c2ecf20Sopenharmony_ci		musb_dma->max_len = SZ_4M;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		dc = dma_request_chan(dev->parent, str);
7208c2ecf20Sopenharmony_ci		if (IS_ERR(dc)) {
7218c2ecf20Sopenharmony_ci			ret = PTR_ERR(dc);
7228c2ecf20Sopenharmony_ci			if (ret != -EPROBE_DEFER)
7238c2ecf20Sopenharmony_ci				dev_err(dev, "Failed to request %s: %d.\n",
7248c2ecf20Sopenharmony_ci					str, ret);
7258c2ecf20Sopenharmony_ci			goto err;
7268c2ecf20Sopenharmony_ci		}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci		cppi41_channel->dc = dc;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci	return 0;
7318c2ecf20Sopenharmony_cierr:
7328c2ecf20Sopenharmony_ci	cppi41_release_all_dma_chans(controller);
7338c2ecf20Sopenharmony_ci	return ret;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_civoid cppi41_dma_controller_destroy(struct dma_controller *c)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller = container_of(c,
7398c2ecf20Sopenharmony_ci			struct cppi41_dma_controller, controller);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	hrtimer_cancel(&controller->early_tx);
7428c2ecf20Sopenharmony_ci	cppi41_dma_controller_stop(controller);
7438c2ecf20Sopenharmony_ci	kfree(controller->rx_channel);
7448c2ecf20Sopenharmony_ci	kfree(controller->tx_channel);
7458c2ecf20Sopenharmony_ci	kfree(controller);
7468c2ecf20Sopenharmony_ci}
7478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_cistruct dma_controller *
7508c2ecf20Sopenharmony_cicppi41_dma_controller_create(struct musb *musb, void __iomem *base)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	struct cppi41_dma_controller *controller;
7538c2ecf20Sopenharmony_ci	int channel_size;
7548c2ecf20Sopenharmony_ci	int ret = 0;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if (!musb->controller->parent->of_node) {
7578c2ecf20Sopenharmony_ci		dev_err(musb->controller, "Need DT for the DMA engine.\n");
7588c2ecf20Sopenharmony_ci		return NULL;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
7628c2ecf20Sopenharmony_ci	if (!controller)
7638c2ecf20Sopenharmony_ci		goto kzalloc_fail;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7668c2ecf20Sopenharmony_ci	controller->early_tx.function = cppi41_recheck_tx_req;
7678c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&controller->early_tx_list);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	controller->controller.channel_alloc = cppi41_dma_channel_allocate;
7708c2ecf20Sopenharmony_ci	controller->controller.channel_release = cppi41_dma_channel_release;
7718c2ecf20Sopenharmony_ci	controller->controller.channel_program = cppi41_dma_channel_program;
7728c2ecf20Sopenharmony_ci	controller->controller.channel_abort = cppi41_dma_channel_abort;
7738c2ecf20Sopenharmony_ci	controller->controller.is_compatible = cppi41_is_compatible;
7748c2ecf20Sopenharmony_ci	controller->controller.musb = musb;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	if (musb->ops->quirks & MUSB_DA8XX) {
7778c2ecf20Sopenharmony_ci		controller->tdown_reg = DA8XX_USB_TEARDOWN;
7788c2ecf20Sopenharmony_ci		controller->autoreq_reg = DA8XX_USB_AUTOREQ;
7798c2ecf20Sopenharmony_ci		controller->set_dma_mode = da8xx_set_dma_mode;
7808c2ecf20Sopenharmony_ci		controller->num_channels = DA8XX_DMA_NUM_CHANNELS;
7818c2ecf20Sopenharmony_ci	} else {
7828c2ecf20Sopenharmony_ci		controller->tdown_reg = USB_TDOWN;
7838c2ecf20Sopenharmony_ci		controller->autoreq_reg = USB_CTRL_AUTOREQ;
7848c2ecf20Sopenharmony_ci		controller->set_dma_mode = cppi41_set_dma_mode;
7858c2ecf20Sopenharmony_ci		controller->num_channels = MUSB_DMA_NUM_CHANNELS;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	channel_size = controller->num_channels *
7898c2ecf20Sopenharmony_ci			sizeof(struct cppi41_dma_channel);
7908c2ecf20Sopenharmony_ci	controller->rx_channel = kzalloc(channel_size, GFP_KERNEL);
7918c2ecf20Sopenharmony_ci	if (!controller->rx_channel)
7928c2ecf20Sopenharmony_ci		goto rx_channel_alloc_fail;
7938c2ecf20Sopenharmony_ci	controller->tx_channel = kzalloc(channel_size, GFP_KERNEL);
7948c2ecf20Sopenharmony_ci	if (!controller->tx_channel)
7958c2ecf20Sopenharmony_ci		goto tx_channel_alloc_fail;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	ret = cppi41_dma_controller_start(controller);
7988c2ecf20Sopenharmony_ci	if (ret)
7998c2ecf20Sopenharmony_ci		goto plat_get_fail;
8008c2ecf20Sopenharmony_ci	return &controller->controller;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ciplat_get_fail:
8038c2ecf20Sopenharmony_ci	kfree(controller->tx_channel);
8048c2ecf20Sopenharmony_citx_channel_alloc_fail:
8058c2ecf20Sopenharmony_ci	kfree(controller->rx_channel);
8068c2ecf20Sopenharmony_cirx_channel_alloc_fail:
8078c2ecf20Sopenharmony_ci	kfree(controller);
8088c2ecf20Sopenharmony_cikzalloc_fail:
8098c2ecf20Sopenharmony_ci	if (ret == -EPROBE_DEFER)
8108c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
8118c2ecf20Sopenharmony_ci	return NULL;
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cppi41_dma_controller_create);
814