18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-1.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Renesas USB driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2011 Renesas Solutions Corp.
68c2ecf20Sopenharmony_ci * Copyright (C) 2019 Renesas Electronics Corporation
78c2ecf20Sopenharmony_ci * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
128c2ecf20Sopenharmony_ci#include "common.h"
138c2ecf20Sopenharmony_ci#include "pipe.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define usbhsf_get_cfifo(p)	(&((p)->fifo_info.cfifo))
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define usbhsf_fifo_is_busy(f)	((f)->pipe) /* see usbhs_pipe_select_fifo */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci *		packet initialize
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_civoid usbhs_pkt_init(struct usbhs_pkt *pkt)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&pkt->node);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci *		packet control function
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_cistatic int usbhsf_null_handle(struct usbhs_pkt *pkt, int *is_done)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
338c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	dev_err(dev, "null handler\n");
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return -EINVAL;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const struct usbhs_pkt_handle usbhsf_null_handler = {
418c2ecf20Sopenharmony_ci	.prepare = usbhsf_null_handle,
428c2ecf20Sopenharmony_ci	.try_run = usbhsf_null_handle,
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid usbhs_pkt_push(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt,
468c2ecf20Sopenharmony_ci		    void (*done)(struct usbhs_priv *priv,
478c2ecf20Sopenharmony_ci				 struct usbhs_pkt *pkt),
488c2ecf20Sopenharmony_ci		    void *buf, int len, int zero, int sequence)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
518c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
528c2ecf20Sopenharmony_ci	unsigned long flags;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (!done) {
558c2ecf20Sopenharmony_ci		dev_err(dev, "no done function\n");
568c2ecf20Sopenharmony_ci		return;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/********************  spin lock ********************/
608c2ecf20Sopenharmony_ci	usbhs_lock(priv, flags);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (!pipe->handler) {
638c2ecf20Sopenharmony_ci		dev_err(dev, "no handler function\n");
648c2ecf20Sopenharmony_ci		pipe->handler = &usbhsf_null_handler;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	list_move_tail(&pkt->node, &pipe->list);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * each pkt must hold own handler.
718c2ecf20Sopenharmony_ci	 * because handler might be changed by its situation.
728c2ecf20Sopenharmony_ci	 * dma handler -> pio handler.
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	pkt->pipe	= pipe;
758c2ecf20Sopenharmony_ci	pkt->buf	= buf;
768c2ecf20Sopenharmony_ci	pkt->handler	= pipe->handler;
778c2ecf20Sopenharmony_ci	pkt->length	= len;
788c2ecf20Sopenharmony_ci	pkt->zero	= zero;
798c2ecf20Sopenharmony_ci	pkt->actual	= 0;
808c2ecf20Sopenharmony_ci	pkt->done	= done;
818c2ecf20Sopenharmony_ci	pkt->sequence	= sequence;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	usbhs_unlock(priv, flags);
848c2ecf20Sopenharmony_ci	/********************  spin unlock ******************/
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic void __usbhsf_pkt_del(struct usbhs_pkt *pkt)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	list_del_init(&pkt->node);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct usbhs_pkt *__usbhsf_pkt_get(struct usbhs_pipe *pipe)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return list_first_entry_or_null(&pipe->list, struct usbhs_pkt, node);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
988c2ecf20Sopenharmony_ci				 struct usbhs_fifo *fifo);
998c2ecf20Sopenharmony_cistatic struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
1008c2ecf20Sopenharmony_ci					    struct usbhs_pkt *pkt);
1018c2ecf20Sopenharmony_ci#define usbhsf_dma_map(p)	__usbhsf_dma_map_ctrl(p, 1)
1028c2ecf20Sopenharmony_ci#define usbhsf_dma_unmap(p)	__usbhsf_dma_map_ctrl(p, 0)
1038c2ecf20Sopenharmony_cistatic int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map);
1048c2ecf20Sopenharmony_cistatic void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
1058c2ecf20Sopenharmony_cistatic void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable);
1068c2ecf20Sopenharmony_cistruct usbhs_pkt *usbhs_pkt_pop(struct usbhs_pipe *pipe, struct usbhs_pkt *pkt)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1098c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
1108c2ecf20Sopenharmony_ci	unsigned long flags;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/********************  spin lock ********************/
1138c2ecf20Sopenharmony_ci	usbhs_lock(priv, flags);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	usbhs_pipe_disable(pipe);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (!pkt)
1188c2ecf20Sopenharmony_ci		pkt = __usbhsf_pkt_get(pipe);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (pkt) {
1218c2ecf20Sopenharmony_ci		struct dma_chan *chan = NULL;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		if (fifo)
1248c2ecf20Sopenharmony_ci			chan = usbhsf_dma_chan_get(fifo, pkt);
1258c2ecf20Sopenharmony_ci		if (chan) {
1268c2ecf20Sopenharmony_ci			dmaengine_terminate_all(chan);
1278c2ecf20Sopenharmony_ci			usbhsf_dma_unmap(pkt);
1288c2ecf20Sopenharmony_ci		} else {
1298c2ecf20Sopenharmony_ci			if (usbhs_pipe_is_dir_in(pipe))
1308c2ecf20Sopenharmony_ci				usbhsf_rx_irq_ctrl(pipe, 0);
1318c2ecf20Sopenharmony_ci			else
1328c2ecf20Sopenharmony_ci				usbhsf_tx_irq_ctrl(pipe, 0);
1338c2ecf20Sopenharmony_ci		}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		usbhs_pipe_clear_without_sequence(pipe, 0, 0);
1368c2ecf20Sopenharmony_ci		usbhs_pipe_running(pipe, 0);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		__usbhsf_pkt_del(pkt);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (fifo)
1428c2ecf20Sopenharmony_ci		usbhsf_fifo_unselect(pipe, fifo);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	usbhs_unlock(priv, flags);
1458c2ecf20Sopenharmony_ci	/********************  spin unlock ******************/
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return pkt;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cienum {
1518c2ecf20Sopenharmony_ci	USBHSF_PKT_PREPARE,
1528c2ecf20Sopenharmony_ci	USBHSF_PKT_TRY_RUN,
1538c2ecf20Sopenharmony_ci	USBHSF_PKT_DMA_DONE,
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int usbhsf_pkt_handler(struct usbhs_pipe *pipe, int type)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
1598c2ecf20Sopenharmony_ci	struct usbhs_pkt *pkt;
1608c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
1618c2ecf20Sopenharmony_ci	int (*func)(struct usbhs_pkt *pkt, int *is_done);
1628c2ecf20Sopenharmony_ci	unsigned long flags;
1638c2ecf20Sopenharmony_ci	int ret = 0;
1648c2ecf20Sopenharmony_ci	int is_done = 0;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	/********************  spin lock ********************/
1678c2ecf20Sopenharmony_ci	usbhs_lock(priv, flags);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	pkt = __usbhsf_pkt_get(pipe);
1708c2ecf20Sopenharmony_ci	if (!pkt)
1718c2ecf20Sopenharmony_ci		goto __usbhs_pkt_handler_end;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	switch (type) {
1748c2ecf20Sopenharmony_ci	case USBHSF_PKT_PREPARE:
1758c2ecf20Sopenharmony_ci		func = pkt->handler->prepare;
1768c2ecf20Sopenharmony_ci		break;
1778c2ecf20Sopenharmony_ci	case USBHSF_PKT_TRY_RUN:
1788c2ecf20Sopenharmony_ci		func = pkt->handler->try_run;
1798c2ecf20Sopenharmony_ci		break;
1808c2ecf20Sopenharmony_ci	case USBHSF_PKT_DMA_DONE:
1818c2ecf20Sopenharmony_ci		func = pkt->handler->dma_done;
1828c2ecf20Sopenharmony_ci		break;
1838c2ecf20Sopenharmony_ci	default:
1848c2ecf20Sopenharmony_ci		dev_err(dev, "unknown pkt handler\n");
1858c2ecf20Sopenharmony_ci		goto __usbhs_pkt_handler_end;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (likely(func))
1898c2ecf20Sopenharmony_ci		ret = func(pkt, &is_done);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (is_done)
1928c2ecf20Sopenharmony_ci		__usbhsf_pkt_del(pkt);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci__usbhs_pkt_handler_end:
1958c2ecf20Sopenharmony_ci	usbhs_unlock(priv, flags);
1968c2ecf20Sopenharmony_ci	/********************  spin unlock ******************/
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (is_done) {
1998c2ecf20Sopenharmony_ci		pkt->done(priv, pkt);
2008c2ecf20Sopenharmony_ci		usbhs_pkt_start(pipe);
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return ret;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_civoid usbhs_pkt_start(struct usbhs_pipe *pipe)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	usbhsf_pkt_handler(pipe, USBHSF_PKT_PREPARE);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/*
2128c2ecf20Sopenharmony_ci *		irq enable/disable function
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_ci#define usbhsf_irq_empty_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_bempsts, e)
2158c2ecf20Sopenharmony_ci#define usbhsf_irq_ready_ctrl(p, e) usbhsf_irq_callback_ctrl(p, irq_brdysts, e)
2168c2ecf20Sopenharmony_ci#define usbhsf_irq_callback_ctrl(pipe, status, enable)			\
2178c2ecf20Sopenharmony_ci	({								\
2188c2ecf20Sopenharmony_ci		struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);	\
2198c2ecf20Sopenharmony_ci		struct usbhs_mod *mod = usbhs_mod_get_current(priv);	\
2208c2ecf20Sopenharmony_ci		u16 status = (1 << usbhs_pipe_number(pipe));		\
2218c2ecf20Sopenharmony_ci		if (!mod)						\
2228c2ecf20Sopenharmony_ci			return;						\
2238c2ecf20Sopenharmony_ci		if (enable)						\
2248c2ecf20Sopenharmony_ci			mod->status |= status;				\
2258c2ecf20Sopenharmony_ci		else							\
2268c2ecf20Sopenharmony_ci			mod->status &= ~status;				\
2278c2ecf20Sopenharmony_ci		usbhs_irq_callback_update(priv, mod);			\
2288c2ecf20Sopenharmony_ci	})
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic void usbhsf_tx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	/*
2338c2ecf20Sopenharmony_ci	 * And DCP pipe can NOT use "ready interrupt" for "send"
2348c2ecf20Sopenharmony_ci	 * it should use "empty" interrupt.
2358c2ecf20Sopenharmony_ci	 * see
2368c2ecf20Sopenharmony_ci	 *   "Operation" - "Interrupt Function" - "BRDY Interrupt"
2378c2ecf20Sopenharmony_ci	 *
2388c2ecf20Sopenharmony_ci	 * on the other hand, normal pipe can use "ready interrupt" for "send"
2398c2ecf20Sopenharmony_ci	 * even though it is single/double buffer
2408c2ecf20Sopenharmony_ci	 */
2418c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_dcp(pipe))
2428c2ecf20Sopenharmony_ci		usbhsf_irq_empty_ctrl(pipe, enable);
2438c2ecf20Sopenharmony_ci	else
2448c2ecf20Sopenharmony_ci		usbhsf_irq_ready_ctrl(pipe, enable);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic void usbhsf_rx_irq_ctrl(struct usbhs_pipe *pipe, int enable)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	usbhsf_irq_ready_ctrl(pipe, enable);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/*
2538c2ecf20Sopenharmony_ci *		FIFO ctrl
2548c2ecf20Sopenharmony_ci */
2558c2ecf20Sopenharmony_cistatic void usbhsf_send_terminator(struct usbhs_pipe *pipe,
2568c2ecf20Sopenharmony_ci				   struct usbhs_fifo *fifo)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	usbhs_bset(priv, fifo->ctr, BVAL, BVAL);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic int usbhsf_fifo_barrier(struct usbhs_priv *priv,
2648c2ecf20Sopenharmony_ci			       struct usbhs_fifo *fifo)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	/* The FIFO port is accessible */
2678c2ecf20Sopenharmony_ci	if (usbhs_read(priv, fifo->ctr) & FRDY)
2688c2ecf20Sopenharmony_ci		return 0;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return -EBUSY;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void usbhsf_fifo_clear(struct usbhs_pipe *pipe,
2748c2ecf20Sopenharmony_ci			      struct usbhs_fifo *fifo)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
2778c2ecf20Sopenharmony_ci	int ret = 0;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (!usbhs_pipe_is_dcp(pipe)) {
2808c2ecf20Sopenharmony_ci		/*
2818c2ecf20Sopenharmony_ci		 * This driver checks the pipe condition first to avoid -EBUSY
2828c2ecf20Sopenharmony_ci		 * from usbhsf_fifo_barrier() if the pipe is RX direction and
2838c2ecf20Sopenharmony_ci		 * empty.
2848c2ecf20Sopenharmony_ci		 */
2858c2ecf20Sopenharmony_ci		if (usbhs_pipe_is_dir_in(pipe))
2868c2ecf20Sopenharmony_ci			ret = usbhs_pipe_is_accessible(pipe);
2878c2ecf20Sopenharmony_ci		if (!ret)
2888c2ecf20Sopenharmony_ci			ret = usbhsf_fifo_barrier(priv, fifo);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/*
2928c2ecf20Sopenharmony_ci	 * if non-DCP pipe, this driver should set BCLR when
2938c2ecf20Sopenharmony_ci	 * usbhsf_fifo_barrier() returns 0.
2948c2ecf20Sopenharmony_ci	 */
2958c2ecf20Sopenharmony_ci	if (!ret)
2968c2ecf20Sopenharmony_ci		usbhs_write(priv, fifo->ctr, BCLR);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int usbhsf_fifo_rcv_len(struct usbhs_priv *priv,
3008c2ecf20Sopenharmony_ci			       struct usbhs_fifo *fifo)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	return usbhs_read(priv, fifo->ctr) & DTLN_MASK;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic void usbhsf_fifo_unselect(struct usbhs_pipe *pipe,
3068c2ecf20Sopenharmony_ci				 struct usbhs_fifo *fifo)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	usbhs_pipe_select_fifo(pipe, NULL);
3118c2ecf20Sopenharmony_ci	usbhs_write(priv, fifo->sel, 0);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int usbhsf_fifo_select(struct usbhs_pipe *pipe,
3158c2ecf20Sopenharmony_ci			      struct usbhs_fifo *fifo,
3168c2ecf20Sopenharmony_ci			      int write)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
3198c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
3208c2ecf20Sopenharmony_ci	int timeout = 1024;
3218c2ecf20Sopenharmony_ci	u16 mask = ((1 << 5) | 0xF);		/* mask of ISEL | CURPIPE */
3228c2ecf20Sopenharmony_ci	u16 base = usbhs_pipe_number(pipe);	/* CURPIPE */
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe) ||
3258c2ecf20Sopenharmony_ci	    usbhsf_fifo_is_busy(fifo))
3268c2ecf20Sopenharmony_ci		return -EBUSY;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_dcp(pipe)) {
3298c2ecf20Sopenharmony_ci		base |= (1 == write) << 5;	/* ISEL */
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		if (usbhs_mod_is_host(priv))
3328c2ecf20Sopenharmony_ci			usbhs_dcp_dir_for_host(pipe, write);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* "base" will be used below  */
3368c2ecf20Sopenharmony_ci	usbhs_write(priv, fifo->sel, base | MBW_32);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	/* check ISEL and CURPIPE value */
3398c2ecf20Sopenharmony_ci	while (timeout--) {
3408c2ecf20Sopenharmony_ci		if (base == (mask & usbhs_read(priv, fifo->sel))) {
3418c2ecf20Sopenharmony_ci			usbhs_pipe_select_fifo(pipe, fifo);
3428c2ecf20Sopenharmony_ci			return 0;
3438c2ecf20Sopenharmony_ci		}
3448c2ecf20Sopenharmony_ci		udelay(10);
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	dev_err(dev, "fifo select error\n");
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return -EIO;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/*
3538c2ecf20Sopenharmony_ci *		DCP status stage
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_cistatic int usbhs_dcp_dir_switch_to_write(struct usbhs_pkt *pkt, int *is_done)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
3588c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
3598c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
3608c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
3618c2ecf20Sopenharmony_ci	int ret;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	usbhs_pipe_disable(pipe);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 1);
3668c2ecf20Sopenharmony_ci	if (ret < 0) {
3678c2ecf20Sopenharmony_ci		dev_err(dev, "%s() faile\n", __func__);
3688c2ecf20Sopenharmony_ci		return ret;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	usbhs_pipe_sequence_data1(pipe); /* DATA1 */
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
3748c2ecf20Sopenharmony_ci	usbhsf_send_terminator(pipe, fifo);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	usbhsf_tx_irq_ctrl(pipe, 1);
3798c2ecf20Sopenharmony_ci	usbhs_pipe_enable(pipe);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	return ret;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic int usbhs_dcp_dir_switch_to_read(struct usbhs_pkt *pkt, int *is_done)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
3878c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
3888c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
3898c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
3908c2ecf20Sopenharmony_ci	int ret;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	usbhs_pipe_disable(pipe);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 0);
3958c2ecf20Sopenharmony_ci	if (ret < 0) {
3968c2ecf20Sopenharmony_ci		dev_err(dev, "%s() fail\n", __func__);
3978c2ecf20Sopenharmony_ci		return ret;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	usbhs_pipe_sequence_data1(pipe); /* DATA1 */
4018c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	usbhsf_rx_irq_ctrl(pipe, 1);
4068c2ecf20Sopenharmony_ci	usbhs_pipe_enable(pipe);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	return ret;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int usbhs_dcp_dir_switch_done(struct usbhs_pkt *pkt, int *is_done)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (pkt->handler == &usbhs_dcp_status_stage_in_handler)
4178c2ecf20Sopenharmony_ci		usbhsf_tx_irq_ctrl(pipe, 0);
4188c2ecf20Sopenharmony_ci	else
4198c2ecf20Sopenharmony_ci		usbhsf_rx_irq_ctrl(pipe, 0);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	pkt->actual = pkt->length;
4228c2ecf20Sopenharmony_ci	*is_done = 1;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return 0;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_dcp_status_stage_in_handler = {
4288c2ecf20Sopenharmony_ci	.prepare = usbhs_dcp_dir_switch_to_write,
4298c2ecf20Sopenharmony_ci	.try_run = usbhs_dcp_dir_switch_done,
4308c2ecf20Sopenharmony_ci};
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_dcp_status_stage_out_handler = {
4338c2ecf20Sopenharmony_ci	.prepare = usbhs_dcp_dir_switch_to_read,
4348c2ecf20Sopenharmony_ci	.try_run = usbhs_dcp_dir_switch_done,
4358c2ecf20Sopenharmony_ci};
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci/*
4388c2ecf20Sopenharmony_ci *		DCP data stage (push)
4398c2ecf20Sopenharmony_ci */
4408c2ecf20Sopenharmony_cistatic int usbhsf_dcp_data_stage_try_push(struct usbhs_pkt *pkt, int *is_done)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	usbhs_pipe_sequence_data1(pipe); /* DATA1 */
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/*
4478c2ecf20Sopenharmony_ci	 * change handler to PIO push
4488c2ecf20Sopenharmony_ci	 */
4498c2ecf20Sopenharmony_ci	pkt->handler = &usbhs_fifo_pio_push_handler;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return pkt->handler->prepare(pkt, is_done);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_dcp_data_stage_out_handler = {
4558c2ecf20Sopenharmony_ci	.prepare = usbhsf_dcp_data_stage_try_push,
4568c2ecf20Sopenharmony_ci};
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/*
4598c2ecf20Sopenharmony_ci *		DCP data stage (pop)
4608c2ecf20Sopenharmony_ci */
4618c2ecf20Sopenharmony_cistatic int usbhsf_dcp_data_stage_prepare_pop(struct usbhs_pkt *pkt,
4628c2ecf20Sopenharmony_ci					     int *is_done)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
4658c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
4668c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe))
4698c2ecf20Sopenharmony_ci		return 0;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/*
4728c2ecf20Sopenharmony_ci	 * prepare pop for DCP should
4738c2ecf20Sopenharmony_ci	 *  - change DCP direction,
4748c2ecf20Sopenharmony_ci	 *  - clear fifo
4758c2ecf20Sopenharmony_ci	 *  - DATA1
4768c2ecf20Sopenharmony_ci	 */
4778c2ecf20Sopenharmony_ci	usbhs_pipe_disable(pipe);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	usbhs_pipe_sequence_data1(pipe); /* DATA1 */
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	usbhsf_fifo_select(pipe, fifo, 0);
4828c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
4838c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/*
4868c2ecf20Sopenharmony_ci	 * change handler to PIO pop
4878c2ecf20Sopenharmony_ci	 */
4888c2ecf20Sopenharmony_ci	pkt->handler = &usbhs_fifo_pio_pop_handler;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	return pkt->handler->prepare(pkt, is_done);
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_dcp_data_stage_in_handler = {
4948c2ecf20Sopenharmony_ci	.prepare = usbhsf_dcp_data_stage_prepare_pop,
4958c2ecf20Sopenharmony_ci};
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci/*
4988c2ecf20Sopenharmony_ci *		PIO push handler
4998c2ecf20Sopenharmony_ci */
5008c2ecf20Sopenharmony_cistatic int usbhsf_pio_try_push(struct usbhs_pkt *pkt, int *is_done)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
5038c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
5048c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
5058c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
5068c2ecf20Sopenharmony_ci	void __iomem *addr = priv->base + fifo->port;
5078c2ecf20Sopenharmony_ci	u8 *buf;
5088c2ecf20Sopenharmony_ci	int maxp = usbhs_pipe_get_maxpacket(pipe);
5098c2ecf20Sopenharmony_ci	int total_len;
5108c2ecf20Sopenharmony_ci	int i, ret, len;
5118c2ecf20Sopenharmony_ci	int is_short;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	usbhs_pipe_data_sequence(pipe, pkt->sequence);
5148c2ecf20Sopenharmony_ci	pkt->sequence = -1; /* -1 sequence will be ignored */
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 1);
5198c2ecf20Sopenharmony_ci	if (ret < 0)
5208c2ecf20Sopenharmony_ci		return 0;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	ret = usbhs_pipe_is_accessible(pipe);
5238c2ecf20Sopenharmony_ci	if (ret < 0) {
5248c2ecf20Sopenharmony_ci		/* inaccessible pipe is not an error */
5258c2ecf20Sopenharmony_ci		ret = 0;
5268c2ecf20Sopenharmony_ci		goto usbhs_fifo_write_busy;
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_barrier(priv, fifo);
5308c2ecf20Sopenharmony_ci	if (ret < 0)
5318c2ecf20Sopenharmony_ci		goto usbhs_fifo_write_busy;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	buf		= pkt->buf    + pkt->actual;
5348c2ecf20Sopenharmony_ci	len		= pkt->length - pkt->actual;
5358c2ecf20Sopenharmony_ci	len		= min(len, maxp);
5368c2ecf20Sopenharmony_ci	total_len	= len;
5378c2ecf20Sopenharmony_ci	is_short	= total_len < maxp;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/*
5408c2ecf20Sopenharmony_ci	 * FIXME
5418c2ecf20Sopenharmony_ci	 *
5428c2ecf20Sopenharmony_ci	 * 32-bit access only
5438c2ecf20Sopenharmony_ci	 */
5448c2ecf20Sopenharmony_ci	if (len >= 4 && !((unsigned long)buf & 0x03)) {
5458c2ecf20Sopenharmony_ci		iowrite32_rep(addr, buf, len / 4);
5468c2ecf20Sopenharmony_ci		len %= 4;
5478c2ecf20Sopenharmony_ci		buf += total_len - len;
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* the rest operation */
5518c2ecf20Sopenharmony_ci	if (usbhs_get_dparam(priv, cfifo_byte_addr)) {
5528c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++)
5538c2ecf20Sopenharmony_ci			iowrite8(buf[i], addr + (i & 0x03));
5548c2ecf20Sopenharmony_ci	} else {
5558c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++)
5568c2ecf20Sopenharmony_ci			iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
5578c2ecf20Sopenharmony_ci	}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/*
5608c2ecf20Sopenharmony_ci	 * variable update
5618c2ecf20Sopenharmony_ci	 */
5628c2ecf20Sopenharmony_ci	pkt->actual += total_len;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	if (pkt->actual < pkt->length)
5658c2ecf20Sopenharmony_ci		*is_done = 0;		/* there are remainder data */
5668c2ecf20Sopenharmony_ci	else if (is_short)
5678c2ecf20Sopenharmony_ci		*is_done = 1;		/* short packet */
5688c2ecf20Sopenharmony_ci	else
5698c2ecf20Sopenharmony_ci		*is_done = !pkt->zero;	/* send zero packet ? */
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	/*
5728c2ecf20Sopenharmony_ci	 * pipe/irq handling
5738c2ecf20Sopenharmony_ci	 */
5748c2ecf20Sopenharmony_ci	if (is_short)
5758c2ecf20Sopenharmony_ci		usbhsf_send_terminator(pipe, fifo);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	usbhsf_tx_irq_ctrl(pipe, !*is_done);
5788c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, !*is_done);
5798c2ecf20Sopenharmony_ci	usbhs_pipe_enable(pipe);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	dev_dbg(dev, "  send %d (%d/ %d/ %d/ %d)\n",
5828c2ecf20Sopenharmony_ci		usbhs_pipe_number(pipe),
5838c2ecf20Sopenharmony_ci		pkt->length, pkt->actual, *is_done, pkt->zero);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return 0;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ciusbhs_fifo_write_busy:
5908c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/*
5938c2ecf20Sopenharmony_ci	 * pipe is busy.
5948c2ecf20Sopenharmony_ci	 * retry in interrupt
5958c2ecf20Sopenharmony_ci	 */
5968c2ecf20Sopenharmony_ci	usbhsf_tx_irq_ctrl(pipe, 1);
5978c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, 1);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	return ret;
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic int usbhsf_pio_prepare_push(struct usbhs_pkt *pkt, int *is_done)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_running(pkt->pipe))
6058c2ecf20Sopenharmony_ci		return 0;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	return usbhsf_pio_try_push(pkt, is_done);
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_fifo_pio_push_handler = {
6118c2ecf20Sopenharmony_ci	.prepare = usbhsf_pio_prepare_push,
6128c2ecf20Sopenharmony_ci	.try_run = usbhsf_pio_try_push,
6138c2ecf20Sopenharmony_ci};
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci/*
6168c2ecf20Sopenharmony_ci *		PIO pop handler
6178c2ecf20Sopenharmony_ci */
6188c2ecf20Sopenharmony_cistatic int usbhsf_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
6198c2ecf20Sopenharmony_ci{
6208c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
6218c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
6228c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe))
6258c2ecf20Sopenharmony_ci		return 0;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_running(pipe))
6288c2ecf20Sopenharmony_ci		return 0;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/*
6318c2ecf20Sopenharmony_ci	 * pipe enable to prepare packet receive
6328c2ecf20Sopenharmony_ci	 */
6338c2ecf20Sopenharmony_ci	usbhs_pipe_data_sequence(pipe, pkt->sequence);
6348c2ecf20Sopenharmony_ci	pkt->sequence = -1; /* -1 sequence will be ignored */
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_dcp(pipe))
6378c2ecf20Sopenharmony_ci		usbhsf_fifo_clear(pipe, fifo);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->length);
6408c2ecf20Sopenharmony_ci	usbhs_pipe_enable(pipe);
6418c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, 1);
6428c2ecf20Sopenharmony_ci	usbhsf_rx_irq_ctrl(pipe, 1);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return 0;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int usbhsf_pio_try_pop(struct usbhs_pkt *pkt, int *is_done)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
6508c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
6518c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
6528c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
6538c2ecf20Sopenharmony_ci	void __iomem *addr = priv->base + fifo->port;
6548c2ecf20Sopenharmony_ci	u8 *buf;
6558c2ecf20Sopenharmony_ci	u32 data = 0;
6568c2ecf20Sopenharmony_ci	int maxp = usbhs_pipe_get_maxpacket(pipe);
6578c2ecf20Sopenharmony_ci	int rcv_len, len;
6588c2ecf20Sopenharmony_ci	int i, ret;
6598c2ecf20Sopenharmony_ci	int total_len = 0;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 0);
6628c2ecf20Sopenharmony_ci	if (ret < 0)
6638c2ecf20Sopenharmony_ci		return 0;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_barrier(priv, fifo);
6668c2ecf20Sopenharmony_ci	if (ret < 0)
6678c2ecf20Sopenharmony_ci		goto usbhs_fifo_read_busy;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	buf		= pkt->buf    + pkt->actual;
6728c2ecf20Sopenharmony_ci	len		= pkt->length - pkt->actual;
6738c2ecf20Sopenharmony_ci	len		= min(len, rcv_len);
6748c2ecf20Sopenharmony_ci	total_len	= len;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/*
6778c2ecf20Sopenharmony_ci	 * update actual length first here to decide disable pipe.
6788c2ecf20Sopenharmony_ci	 * if this pipe keeps BUF status and all data were popped,
6798c2ecf20Sopenharmony_ci	 * then, next interrupt/token will be issued again
6808c2ecf20Sopenharmony_ci	 */
6818c2ecf20Sopenharmony_ci	pkt->actual += total_len;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	if ((pkt->actual == pkt->length) ||	/* receive all data */
6848c2ecf20Sopenharmony_ci	    (total_len < maxp)) {		/* short packet */
6858c2ecf20Sopenharmony_ci		*is_done = 1;
6868c2ecf20Sopenharmony_ci		usbhsf_rx_irq_ctrl(pipe, 0);
6878c2ecf20Sopenharmony_ci		usbhs_pipe_running(pipe, 0);
6888c2ecf20Sopenharmony_ci		/*
6898c2ecf20Sopenharmony_ci		 * If function mode, since this controller is possible to enter
6908c2ecf20Sopenharmony_ci		 * Control Write status stage at this timing, this driver
6918c2ecf20Sopenharmony_ci		 * should not disable the pipe. If such a case happens, this
6928c2ecf20Sopenharmony_ci		 * controller is not able to complete the status stage.
6938c2ecf20Sopenharmony_ci		 */
6948c2ecf20Sopenharmony_ci		if (!usbhs_mod_is_host(priv) && !usbhs_pipe_is_dcp(pipe))
6958c2ecf20Sopenharmony_ci			usbhs_pipe_disable(pipe);	/* disable pipe first */
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	/*
6998c2ecf20Sopenharmony_ci	 * Buffer clear if Zero-Length packet
7008c2ecf20Sopenharmony_ci	 *
7018c2ecf20Sopenharmony_ci	 * see
7028c2ecf20Sopenharmony_ci	 * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
7038c2ecf20Sopenharmony_ci	 */
7048c2ecf20Sopenharmony_ci	if (0 == rcv_len) {
7058c2ecf20Sopenharmony_ci		pkt->zero = 1;
7068c2ecf20Sopenharmony_ci		usbhsf_fifo_clear(pipe, fifo);
7078c2ecf20Sopenharmony_ci		goto usbhs_fifo_read_end;
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/*
7118c2ecf20Sopenharmony_ci	 * FIXME
7128c2ecf20Sopenharmony_ci	 *
7138c2ecf20Sopenharmony_ci	 * 32-bit access only
7148c2ecf20Sopenharmony_ci	 */
7158c2ecf20Sopenharmony_ci	if (len >= 4 && !((unsigned long)buf & 0x03)) {
7168c2ecf20Sopenharmony_ci		ioread32_rep(addr, buf, len / 4);
7178c2ecf20Sopenharmony_ci		len %= 4;
7188c2ecf20Sopenharmony_ci		buf += total_len - len;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	/* the rest operation */
7228c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
7238c2ecf20Sopenharmony_ci		if (!(i & 0x03))
7248c2ecf20Sopenharmony_ci			data = ioread32(addr);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ciusbhs_fifo_read_end:
7308c2ecf20Sopenharmony_ci	dev_dbg(dev, "  recv %d (%d/ %d/ %d/ %d)\n",
7318c2ecf20Sopenharmony_ci		usbhs_pipe_number(pipe),
7328c2ecf20Sopenharmony_ci		pkt->length, pkt->actual, *is_done, pkt->zero);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ciusbhs_fifo_read_busy:
7358c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	return ret;
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_fifo_pio_pop_handler = {
7418c2ecf20Sopenharmony_ci	.prepare = usbhsf_prepare_pop,
7428c2ecf20Sopenharmony_ci	.try_run = usbhsf_pio_try_pop,
7438c2ecf20Sopenharmony_ci};
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci/*
7468c2ecf20Sopenharmony_ci *		DCP ctrol statge handler
7478c2ecf20Sopenharmony_ci */
7488c2ecf20Sopenharmony_cistatic int usbhsf_ctrl_stage_end(struct usbhs_pkt *pkt, int *is_done)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	usbhs_dcp_control_transfer_done(pkt->pipe);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	*is_done = 1;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	return 0;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_ctrl_stage_end_handler = {
7588c2ecf20Sopenharmony_ci	.prepare = usbhsf_ctrl_stage_end,
7598c2ecf20Sopenharmony_ci	.try_run = usbhsf_ctrl_stage_end,
7608c2ecf20Sopenharmony_ci};
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci/*
7638c2ecf20Sopenharmony_ci *		DMA fifo functions
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_cistatic struct dma_chan *usbhsf_dma_chan_get(struct usbhs_fifo *fifo,
7668c2ecf20Sopenharmony_ci					    struct usbhs_pkt *pkt)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	if (&usbhs_fifo_dma_push_handler == pkt->handler)
7698c2ecf20Sopenharmony_ci		return fifo->tx_chan;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	if (&usbhs_fifo_dma_pop_handler == pkt->handler)
7728c2ecf20Sopenharmony_ci		return fifo->rx_chan;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	return NULL;
7758c2ecf20Sopenharmony_ci}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_cistatic struct usbhs_fifo *usbhsf_get_dma_fifo(struct usbhs_priv *priv,
7788c2ecf20Sopenharmony_ci					      struct usbhs_pkt *pkt)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
7818c2ecf20Sopenharmony_ci	int i;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	usbhs_for_each_dfifo(priv, fifo, i) {
7848c2ecf20Sopenharmony_ci		if (usbhsf_dma_chan_get(fifo, pkt) &&
7858c2ecf20Sopenharmony_ci		    !usbhsf_fifo_is_busy(fifo))
7868c2ecf20Sopenharmony_ci			return fifo;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	return NULL;
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci#define usbhsf_dma_start(p, f)	__usbhsf_dma_ctrl(p, f, DREQE)
7938c2ecf20Sopenharmony_ci#define usbhsf_dma_stop(p, f)	__usbhsf_dma_ctrl(p, f, 0)
7948c2ecf20Sopenharmony_cistatic void __usbhsf_dma_ctrl(struct usbhs_pipe *pipe,
7958c2ecf20Sopenharmony_ci			      struct usbhs_fifo *fifo,
7968c2ecf20Sopenharmony_ci			      u16 dreqe)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	usbhs_bset(priv, fifo->sel, DREQE, dreqe);
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic int __usbhsf_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
8068c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
8078c2ecf20Sopenharmony_ci	struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
8088c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
8098c2ecf20Sopenharmony_ci	struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	return info->dma_map_ctrl(chan->device->dev, pkt, map);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic void usbhsf_dma_complete(void *arg,
8158c2ecf20Sopenharmony_ci				const struct dmaengine_result *result);
8168c2ecf20Sopenharmony_cistatic void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
8198c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
8208c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
8218c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *desc;
8228c2ecf20Sopenharmony_ci	struct dma_chan *chan;
8238c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
8248c2ecf20Sopenharmony_ci	enum dma_transfer_direction dir;
8258c2ecf20Sopenharmony_ci	dma_cookie_t cookie;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	fifo = usbhs_pipe_to_fifo(pipe);
8288c2ecf20Sopenharmony_ci	if (!fifo)
8298c2ecf20Sopenharmony_ci		return;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	chan = usbhsf_dma_chan_get(fifo, pkt);
8328c2ecf20Sopenharmony_ci	dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	desc = dmaengine_prep_slave_single(chan, pkt->dma + pkt->actual,
8358c2ecf20Sopenharmony_ci					pkt->trans, dir,
8368c2ecf20Sopenharmony_ci					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
8378c2ecf20Sopenharmony_ci	if (!desc)
8388c2ecf20Sopenharmony_ci		return;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	desc->callback_result	= usbhsf_dma_complete;
8418c2ecf20Sopenharmony_ci	desc->callback_param	= pkt;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	cookie = dmaengine_submit(desc);
8448c2ecf20Sopenharmony_ci	if (cookie < 0) {
8458c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to submit dma descriptor\n");
8468c2ecf20Sopenharmony_ci		return;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	dev_dbg(dev, "  %s %d (%d/ %d)\n",
8508c2ecf20Sopenharmony_ci		fifo->name, usbhs_pipe_number(pipe), pkt->length, pkt->zero);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, 1);
8538c2ecf20Sopenharmony_ci	usbhs_pipe_set_trans_count_if_bulk(pipe, pkt->trans);
8548c2ecf20Sopenharmony_ci	dma_async_issue_pending(chan);
8558c2ecf20Sopenharmony_ci	usbhsf_dma_start(pipe, fifo);
8568c2ecf20Sopenharmony_ci	usbhs_pipe_enable(pipe);
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic void xfer_work(struct work_struct *work)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work);
8628c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
8638c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
8648c2ecf20Sopenharmony_ci	unsigned long flags;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	usbhs_lock(priv, flags);
8678c2ecf20Sopenharmony_ci	usbhsf_dma_xfer_preparing(pkt);
8688c2ecf20Sopenharmony_ci	usbhs_unlock(priv, flags);
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci/*
8728c2ecf20Sopenharmony_ci *		DMA push handler
8738c2ecf20Sopenharmony_ci */
8748c2ecf20Sopenharmony_cistatic int usbhsf_dma_prepare_push(struct usbhs_pkt *pkt, int *is_done)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
8778c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
8788c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
8798c2ecf20Sopenharmony_ci	int len = pkt->length - pkt->actual;
8808c2ecf20Sopenharmony_ci	int ret;
8818c2ecf20Sopenharmony_ci	uintptr_t align_mask;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe))
8848c2ecf20Sopenharmony_ci		return 0;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	/* use PIO if packet is less than pio_dma_border or pipe is DCP */
8878c2ecf20Sopenharmony_ci	if ((len < usbhs_get_dparam(priv, pio_dma_border)) ||
8888c2ecf20Sopenharmony_ci	    usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
8898c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	/* check data length if this driver don't use USB-DMAC */
8928c2ecf20Sopenharmony_ci	if (!usbhs_get_dparam(priv, has_usb_dmac) && len & 0x7)
8938c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* check buffer alignment */
8968c2ecf20Sopenharmony_ci	align_mask = usbhs_get_dparam(priv, has_usb_dmac) ?
8978c2ecf20Sopenharmony_ci					USBHS_USB_DMAC_XFER_SIZE - 1 : 0x7;
8988c2ecf20Sopenharmony_ci	if ((uintptr_t)(pkt->buf + pkt->actual) & align_mask)
8998c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	/* return at this time if the pipe is running */
9028c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_running(pipe))
9038c2ecf20Sopenharmony_ci		return 0;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	/* get enable DMA fifo */
9068c2ecf20Sopenharmony_ci	fifo = usbhsf_get_dma_fifo(priv, pkt);
9078c2ecf20Sopenharmony_ci	if (!fifo)
9088c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 0);
9118c2ecf20Sopenharmony_ci	if (ret < 0)
9128c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	if (usbhsf_dma_map(pkt) < 0)
9158c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_push_unselect;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	pkt->trans = len;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	usbhsf_tx_irq_ctrl(pipe, 0);
9208c2ecf20Sopenharmony_ci	/* FIXME: Workaound for usb dmac that driver can be used in atomic */
9218c2ecf20Sopenharmony_ci	if (usbhs_get_dparam(priv, has_usb_dmac)) {
9228c2ecf20Sopenharmony_ci		usbhsf_dma_xfer_preparing(pkt);
9238c2ecf20Sopenharmony_ci	} else {
9248c2ecf20Sopenharmony_ci		INIT_WORK(&pkt->work, xfer_work);
9258c2ecf20Sopenharmony_ci		schedule_work(&pkt->work);
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	return 0;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ciusbhsf_pio_prepare_push_unselect:
9318c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
9328c2ecf20Sopenharmony_ciusbhsf_pio_prepare_push:
9338c2ecf20Sopenharmony_ci	/*
9348c2ecf20Sopenharmony_ci	 * change handler to PIO
9358c2ecf20Sopenharmony_ci	 */
9368c2ecf20Sopenharmony_ci	pkt->handler = &usbhs_fifo_pio_push_handler;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	return pkt->handler->prepare(pkt, is_done);
9398c2ecf20Sopenharmony_ci}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_cistatic int usbhsf_dma_push_done(struct usbhs_pkt *pkt, int *is_done)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
9448c2ecf20Sopenharmony_ci	int is_short = pkt->trans % usbhs_pipe_get_maxpacket(pipe);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	pkt->actual += pkt->trans;
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (pkt->actual < pkt->length)
9498c2ecf20Sopenharmony_ci		*is_done = 0;		/* there are remainder data */
9508c2ecf20Sopenharmony_ci	else if (is_short)
9518c2ecf20Sopenharmony_ci		*is_done = 1;		/* short packet */
9528c2ecf20Sopenharmony_ci	else
9538c2ecf20Sopenharmony_ci		*is_done = !pkt->zero;	/* send zero packet? */
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, !*is_done);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	usbhsf_dma_stop(pipe, pipe->fifo);
9588c2ecf20Sopenharmony_ci	usbhsf_dma_unmap(pkt);
9598c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, pipe->fifo);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	if (!*is_done) {
9628c2ecf20Sopenharmony_ci		/* change handler to PIO */
9638c2ecf20Sopenharmony_ci		pkt->handler = &usbhs_fifo_pio_push_handler;
9648c2ecf20Sopenharmony_ci		return pkt->handler->try_run(pkt, is_done);
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	return 0;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_fifo_dma_push_handler = {
9718c2ecf20Sopenharmony_ci	.prepare	= usbhsf_dma_prepare_push,
9728c2ecf20Sopenharmony_ci	.dma_done	= usbhsf_dma_push_done,
9738c2ecf20Sopenharmony_ci};
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci/*
9768c2ecf20Sopenharmony_ci *		DMA pop handler
9778c2ecf20Sopenharmony_ci */
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_cistatic int usbhsf_dma_prepare_pop_with_rx_irq(struct usbhs_pkt *pkt,
9808c2ecf20Sopenharmony_ci					      int *is_done)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	return usbhsf_prepare_pop(pkt, is_done);
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_cistatic int usbhsf_dma_prepare_pop_with_usb_dmac(struct usbhs_pkt *pkt,
9868c2ecf20Sopenharmony_ci						int *is_done)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
9898c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
9908c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
9918c2ecf20Sopenharmony_ci	int ret;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe))
9948c2ecf20Sopenharmony_ci		return 0;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	/* use PIO if packet is less than pio_dma_border or pipe is DCP */
9978c2ecf20Sopenharmony_ci	if ((pkt->length < usbhs_get_dparam(priv, pio_dma_border)) ||
9988c2ecf20Sopenharmony_ci	    usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
9998c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	fifo = usbhsf_get_dma_fifo(priv, pkt);
10028c2ecf20Sopenharmony_ci	if (!fifo)
10038c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	if ((uintptr_t)pkt->buf & (USBHS_USB_DMAC_XFER_SIZE - 1))
10068c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	/* return at this time if the pipe is running */
10098c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_running(pipe))
10108c2ecf20Sopenharmony_ci		return 0;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	usbhs_pipe_config_change_bfre(pipe, 1);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 0);
10158c2ecf20Sopenharmony_ci	if (ret < 0)
10168c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	if (usbhsf_dma_map(pkt) < 0)
10198c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop_unselect;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	/* DMA */
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	/*
10248c2ecf20Sopenharmony_ci	 * usbhs_fifo_dma_pop_handler :: prepare
10258c2ecf20Sopenharmony_ci	 * enabled irq to come here.
10268c2ecf20Sopenharmony_ci	 * but it is no longer needed for DMA. disable it.
10278c2ecf20Sopenharmony_ci	 */
10288c2ecf20Sopenharmony_ci	usbhsf_rx_irq_ctrl(pipe, 0);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	pkt->trans = pkt->length;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	usbhsf_dma_xfer_preparing(pkt);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	return 0;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ciusbhsf_pio_prepare_pop_unselect:
10378c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
10388c2ecf20Sopenharmony_ciusbhsf_pio_prepare_pop:
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	/*
10418c2ecf20Sopenharmony_ci	 * change handler to PIO
10428c2ecf20Sopenharmony_ci	 */
10438c2ecf20Sopenharmony_ci	pkt->handler = &usbhs_fifo_pio_pop_handler;
10448c2ecf20Sopenharmony_ci	usbhs_pipe_config_change_bfre(pipe, 0);
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	return pkt->handler->prepare(pkt, is_done);
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_cistatic int usbhsf_dma_prepare_pop(struct usbhs_pkt *pkt, int *is_done)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	if (usbhs_get_dparam(priv, has_usb_dmac))
10548c2ecf20Sopenharmony_ci		return usbhsf_dma_prepare_pop_with_usb_dmac(pkt, is_done);
10558c2ecf20Sopenharmony_ci	else
10568c2ecf20Sopenharmony_ci		return usbhsf_dma_prepare_pop_with_rx_irq(pkt, is_done);
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_cistatic int usbhsf_dma_try_pop_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
10608c2ecf20Sopenharmony_ci{
10618c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
10628c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
10638c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
10648c2ecf20Sopenharmony_ci	int len, ret;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_busy(pipe))
10678c2ecf20Sopenharmony_ci		return 0;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	if (usbhs_pipe_is_dcp(pipe))
10708c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	/* get enable DMA fifo */
10738c2ecf20Sopenharmony_ci	fifo = usbhsf_get_dma_fifo(priv, pkt);
10748c2ecf20Sopenharmony_ci	if (!fifo)
10758c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	if ((uintptr_t)(pkt->buf + pkt->actual) & 0x7) /* 8byte alignment */
10788c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_select(pipe, fifo, 0);
10818c2ecf20Sopenharmony_ci	if (ret < 0)
10828c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	/* use PIO if packet is less than pio_dma_border */
10858c2ecf20Sopenharmony_ci	len = usbhsf_fifo_rcv_len(priv, fifo);
10868c2ecf20Sopenharmony_ci	len = min(pkt->length - pkt->actual, len);
10878c2ecf20Sopenharmony_ci	if (len & 0x7) /* 8byte alignment */
10888c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop_unselect;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	if (len < usbhs_get_dparam(priv, pio_dma_border))
10918c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop_unselect;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	ret = usbhsf_fifo_barrier(priv, fifo);
10948c2ecf20Sopenharmony_ci	if (ret < 0)
10958c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop_unselect;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	if (usbhsf_dma_map(pkt) < 0)
10988c2ecf20Sopenharmony_ci		goto usbhsf_pio_prepare_pop_unselect;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	/* DMA */
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	/*
11038c2ecf20Sopenharmony_ci	 * usbhs_fifo_dma_pop_handler :: prepare
11048c2ecf20Sopenharmony_ci	 * enabled irq to come here.
11058c2ecf20Sopenharmony_ci	 * but it is no longer needed for DMA. disable it.
11068c2ecf20Sopenharmony_ci	 */
11078c2ecf20Sopenharmony_ci	usbhsf_rx_irq_ctrl(pipe, 0);
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	pkt->trans = len;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	INIT_WORK(&pkt->work, xfer_work);
11128c2ecf20Sopenharmony_ci	schedule_work(&pkt->work);
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	return 0;
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ciusbhsf_pio_prepare_pop_unselect:
11178c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
11188c2ecf20Sopenharmony_ciusbhsf_pio_prepare_pop:
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	/*
11218c2ecf20Sopenharmony_ci	 * change handler to PIO
11228c2ecf20Sopenharmony_ci	 */
11238c2ecf20Sopenharmony_ci	pkt->handler = &usbhs_fifo_pio_pop_handler;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	return pkt->handler->try_run(pkt, is_done);
11268c2ecf20Sopenharmony_ci}
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_cistatic int usbhsf_dma_try_pop(struct usbhs_pkt *pkt, int *is_done)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	BUG_ON(usbhs_get_dparam(priv, has_usb_dmac));
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	return usbhsf_dma_try_pop_with_rx_irq(pkt, is_done);
11358c2ecf20Sopenharmony_ci}
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_cistatic int usbhsf_dma_pop_done_with_rx_irq(struct usbhs_pkt *pkt, int *is_done)
11388c2ecf20Sopenharmony_ci{
11398c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
11408c2ecf20Sopenharmony_ci	int maxp = usbhs_pipe_get_maxpacket(pipe);
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	usbhsf_dma_stop(pipe, pipe->fifo);
11438c2ecf20Sopenharmony_ci	usbhsf_dma_unmap(pkt);
11448c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, pipe->fifo);
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	pkt->actual += pkt->trans;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	if ((pkt->actual == pkt->length) ||	/* receive all data */
11498c2ecf20Sopenharmony_ci	    (pkt->trans < maxp)) {		/* short packet */
11508c2ecf20Sopenharmony_ci		*is_done = 1;
11518c2ecf20Sopenharmony_ci		usbhs_pipe_running(pipe, 0);
11528c2ecf20Sopenharmony_ci	} else {
11538c2ecf20Sopenharmony_ci		/* re-enable */
11548c2ecf20Sopenharmony_ci		usbhs_pipe_running(pipe, 0);
11558c2ecf20Sopenharmony_ci		usbhsf_prepare_pop(pkt, is_done);
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	return 0;
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_cistatic size_t usbhs_dma_calc_received_size(struct usbhs_pkt *pkt,
11628c2ecf20Sopenharmony_ci					   struct dma_chan *chan, int dtln)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
11658c2ecf20Sopenharmony_ci	size_t received_size;
11668c2ecf20Sopenharmony_ci	int maxp = usbhs_pipe_get_maxpacket(pipe);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	received_size = pkt->length - pkt->dma_result->residue;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if (dtln) {
11718c2ecf20Sopenharmony_ci		received_size -= USBHS_USB_DMAC_XFER_SIZE;
11728c2ecf20Sopenharmony_ci		received_size &= ~(maxp - 1);
11738c2ecf20Sopenharmony_ci		received_size += dtln;
11748c2ecf20Sopenharmony_ci	}
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	return received_size;
11778c2ecf20Sopenharmony_ci}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_cistatic int usbhsf_dma_pop_done_with_usb_dmac(struct usbhs_pkt *pkt,
11808c2ecf20Sopenharmony_ci					     int *is_done)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
11838c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
11848c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhs_pipe_to_fifo(pipe);
11858c2ecf20Sopenharmony_ci	struct dma_chan *chan = usbhsf_dma_chan_get(fifo, pkt);
11868c2ecf20Sopenharmony_ci	int rcv_len;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	/*
11898c2ecf20Sopenharmony_ci	 * Since the driver disables rx_irq in DMA mode, the interrupt handler
11908c2ecf20Sopenharmony_ci	 * cannot the BRDYSTS. So, the function clears it here because the
11918c2ecf20Sopenharmony_ci	 * driver may use PIO mode next time.
11928c2ecf20Sopenharmony_ci	 */
11938c2ecf20Sopenharmony_ci	usbhs_xxxsts_clear(priv, BRDYSTS, usbhs_pipe_number(pipe));
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	rcv_len = usbhsf_fifo_rcv_len(priv, fifo);
11968c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
11978c2ecf20Sopenharmony_ci	pkt->actual = usbhs_dma_calc_received_size(pkt, chan, rcv_len);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	usbhs_pipe_running(pipe, 0);
12008c2ecf20Sopenharmony_ci	usbhsf_dma_stop(pipe, fifo);
12018c2ecf20Sopenharmony_ci	usbhsf_dma_unmap(pkt);
12028c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, pipe->fifo);
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	/* The driver can assume the rx transaction is always "done" */
12058c2ecf20Sopenharmony_ci	*is_done = 1;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	return 0;
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic int usbhsf_dma_pop_done(struct usbhs_pkt *pkt, int *is_done)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pkt->pipe);
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	if (usbhs_get_dparam(priv, has_usb_dmac))
12158c2ecf20Sopenharmony_ci		return usbhsf_dma_pop_done_with_usb_dmac(pkt, is_done);
12168c2ecf20Sopenharmony_ci	else
12178c2ecf20Sopenharmony_ci		return usbhsf_dma_pop_done_with_rx_irq(pkt, is_done);
12188c2ecf20Sopenharmony_ci}
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ciconst struct usbhs_pkt_handle usbhs_fifo_dma_pop_handler = {
12218c2ecf20Sopenharmony_ci	.prepare	= usbhsf_dma_prepare_pop,
12228c2ecf20Sopenharmony_ci	.try_run	= usbhsf_dma_try_pop,
12238c2ecf20Sopenharmony_ci	.dma_done	= usbhsf_dma_pop_done
12248c2ecf20Sopenharmony_ci};
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci/*
12278c2ecf20Sopenharmony_ci *		DMA setting
12288c2ecf20Sopenharmony_ci */
12298c2ecf20Sopenharmony_cistatic bool usbhsf_dma_filter(struct dma_chan *chan, void *param)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	struct sh_dmae_slave *slave = param;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	/*
12348c2ecf20Sopenharmony_ci	 * FIXME
12358c2ecf20Sopenharmony_ci	 *
12368c2ecf20Sopenharmony_ci	 * usbhs doesn't recognize id = 0 as valid DMA
12378c2ecf20Sopenharmony_ci	 */
12388c2ecf20Sopenharmony_ci	if (0 == slave->shdma_slave.slave_id)
12398c2ecf20Sopenharmony_ci		return false;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	chan->private = slave;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	return true;
12448c2ecf20Sopenharmony_ci}
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_cistatic void usbhsf_dma_quit(struct usbhs_priv *priv, struct usbhs_fifo *fifo)
12478c2ecf20Sopenharmony_ci{
12488c2ecf20Sopenharmony_ci	if (fifo->tx_chan)
12498c2ecf20Sopenharmony_ci		dma_release_channel(fifo->tx_chan);
12508c2ecf20Sopenharmony_ci	if (fifo->rx_chan)
12518c2ecf20Sopenharmony_ci		dma_release_channel(fifo->rx_chan);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	fifo->tx_chan = NULL;
12548c2ecf20Sopenharmony_ci	fifo->rx_chan = NULL;
12558c2ecf20Sopenharmony_ci}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_cistatic void usbhsf_dma_init_pdev(struct usbhs_fifo *fifo)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	dma_cap_mask_t mask;
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	dma_cap_zero(mask);
12628c2ecf20Sopenharmony_ci	dma_cap_set(DMA_SLAVE, mask);
12638c2ecf20Sopenharmony_ci	fifo->tx_chan = dma_request_channel(mask, usbhsf_dma_filter,
12648c2ecf20Sopenharmony_ci					    &fifo->tx_slave);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	dma_cap_zero(mask);
12678c2ecf20Sopenharmony_ci	dma_cap_set(DMA_SLAVE, mask);
12688c2ecf20Sopenharmony_ci	fifo->rx_chan = dma_request_channel(mask, usbhsf_dma_filter,
12698c2ecf20Sopenharmony_ci					    &fifo->rx_slave);
12708c2ecf20Sopenharmony_ci}
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_cistatic void usbhsf_dma_init_dt(struct device *dev, struct usbhs_fifo *fifo,
12738c2ecf20Sopenharmony_ci			       int channel)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	char name[16];
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	/*
12788c2ecf20Sopenharmony_ci	 * To avoid complex handing for DnFIFOs, the driver uses each
12798c2ecf20Sopenharmony_ci	 * DnFIFO as TX or RX direction (not bi-direction).
12808c2ecf20Sopenharmony_ci	 * So, the driver uses odd channels for TX, even channels for RX.
12818c2ecf20Sopenharmony_ci	 */
12828c2ecf20Sopenharmony_ci	snprintf(name, sizeof(name), "ch%d", channel);
12838c2ecf20Sopenharmony_ci	if (channel & 1) {
12848c2ecf20Sopenharmony_ci		fifo->tx_chan = dma_request_chan(dev, name);
12858c2ecf20Sopenharmony_ci		if (IS_ERR(fifo->tx_chan))
12868c2ecf20Sopenharmony_ci			fifo->tx_chan = NULL;
12878c2ecf20Sopenharmony_ci	} else {
12888c2ecf20Sopenharmony_ci		fifo->rx_chan = dma_request_chan(dev, name);
12898c2ecf20Sopenharmony_ci		if (IS_ERR(fifo->rx_chan))
12908c2ecf20Sopenharmony_ci			fifo->rx_chan = NULL;
12918c2ecf20Sopenharmony_ci	}
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_cistatic void usbhsf_dma_init(struct usbhs_priv *priv, struct usbhs_fifo *fifo,
12958c2ecf20Sopenharmony_ci			    int channel)
12968c2ecf20Sopenharmony_ci{
12978c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (dev_of_node(dev))
13008c2ecf20Sopenharmony_ci		usbhsf_dma_init_dt(dev, fifo, channel);
13018c2ecf20Sopenharmony_ci	else
13028c2ecf20Sopenharmony_ci		usbhsf_dma_init_pdev(fifo);
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	if (fifo->tx_chan || fifo->rx_chan)
13058c2ecf20Sopenharmony_ci		dev_dbg(dev, "enable DMAEngine (%s%s%s)\n",
13068c2ecf20Sopenharmony_ci			 fifo->name,
13078c2ecf20Sopenharmony_ci			 fifo->tx_chan ? "[TX]" : "    ",
13088c2ecf20Sopenharmony_ci			 fifo->rx_chan ? "[RX]" : "    ");
13098c2ecf20Sopenharmony_ci}
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci/*
13128c2ecf20Sopenharmony_ci *		irq functions
13138c2ecf20Sopenharmony_ci */
13148c2ecf20Sopenharmony_cistatic int usbhsf_irq_empty(struct usbhs_priv *priv,
13158c2ecf20Sopenharmony_ci			    struct usbhs_irq_state *irq_state)
13168c2ecf20Sopenharmony_ci{
13178c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe;
13188c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
13198c2ecf20Sopenharmony_ci	int i, ret;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	if (!irq_state->bempsts) {
13228c2ecf20Sopenharmony_ci		dev_err(dev, "debug %s !!\n", __func__);
13238c2ecf20Sopenharmony_ci		return -EIO;
13248c2ecf20Sopenharmony_ci	}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	/*
13298c2ecf20Sopenharmony_ci	 * search interrupted "pipe"
13308c2ecf20Sopenharmony_ci	 * not "uep".
13318c2ecf20Sopenharmony_ci	 */
13328c2ecf20Sopenharmony_ci	usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
13338c2ecf20Sopenharmony_ci		if (!(irq_state->bempsts & (1 << i)))
13348c2ecf20Sopenharmony_ci			continue;
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci		ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
13378c2ecf20Sopenharmony_ci		if (ret < 0)
13388c2ecf20Sopenharmony_ci			dev_err(dev, "irq_empty run_error %d : %d\n", i, ret);
13398c2ecf20Sopenharmony_ci	}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	return 0;
13428c2ecf20Sopenharmony_ci}
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_cistatic int usbhsf_irq_ready(struct usbhs_priv *priv,
13458c2ecf20Sopenharmony_ci			    struct usbhs_irq_state *irq_state)
13468c2ecf20Sopenharmony_ci{
13478c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe;
13488c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
13498c2ecf20Sopenharmony_ci	int i, ret;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	if (!irq_state->brdysts) {
13528c2ecf20Sopenharmony_ci		dev_err(dev, "debug %s !!\n", __func__);
13538c2ecf20Sopenharmony_ci		return -EIO;
13548c2ecf20Sopenharmony_ci	}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	/*
13598c2ecf20Sopenharmony_ci	 * search interrupted "pipe"
13608c2ecf20Sopenharmony_ci	 * not "uep".
13618c2ecf20Sopenharmony_ci	 */
13628c2ecf20Sopenharmony_ci	usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
13638c2ecf20Sopenharmony_ci		if (!(irq_state->brdysts & (1 << i)))
13648c2ecf20Sopenharmony_ci			continue;
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci		ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_TRY_RUN);
13678c2ecf20Sopenharmony_ci		if (ret < 0)
13688c2ecf20Sopenharmony_ci			dev_err(dev, "irq_ready run_error %d : %d\n", i, ret);
13698c2ecf20Sopenharmony_ci	}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	return 0;
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_cistatic void usbhsf_dma_complete(void *arg,
13758c2ecf20Sopenharmony_ci				const struct dmaengine_result *result)
13768c2ecf20Sopenharmony_ci{
13778c2ecf20Sopenharmony_ci	struct usbhs_pkt *pkt = arg;
13788c2ecf20Sopenharmony_ci	struct usbhs_pipe *pipe = pkt->pipe;
13798c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
13808c2ecf20Sopenharmony_ci	struct device *dev = usbhs_priv_to_dev(priv);
13818c2ecf20Sopenharmony_ci	int ret;
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	pkt->dma_result = result;
13848c2ecf20Sopenharmony_ci	ret = usbhsf_pkt_handler(pipe, USBHSF_PKT_DMA_DONE);
13858c2ecf20Sopenharmony_ci	if (ret < 0)
13868c2ecf20Sopenharmony_ci		dev_err(dev, "dma_complete run_error %d : %d\n",
13878c2ecf20Sopenharmony_ci			usbhs_pipe_number(pipe), ret);
13888c2ecf20Sopenharmony_ci}
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_civoid usbhs_fifo_clear_dcp(struct usbhs_pipe *pipe)
13918c2ecf20Sopenharmony_ci{
13928c2ecf20Sopenharmony_ci	struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
13938c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo = usbhsf_get_cfifo(priv); /* CFIFO */
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	/* clear DCP FIFO of transmission */
13968c2ecf20Sopenharmony_ci	if (usbhsf_fifo_select(pipe, fifo, 1) < 0)
13978c2ecf20Sopenharmony_ci		return;
13988c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
13998c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	/* clear DCP FIFO of reception */
14028c2ecf20Sopenharmony_ci	if (usbhsf_fifo_select(pipe, fifo, 0) < 0)
14038c2ecf20Sopenharmony_ci		return;
14048c2ecf20Sopenharmony_ci	usbhsf_fifo_clear(pipe, fifo);
14058c2ecf20Sopenharmony_ci	usbhsf_fifo_unselect(pipe, fifo);
14068c2ecf20Sopenharmony_ci}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci/*
14098c2ecf20Sopenharmony_ci *		fifo init
14108c2ecf20Sopenharmony_ci */
14118c2ecf20Sopenharmony_civoid usbhs_fifo_init(struct usbhs_priv *priv)
14128c2ecf20Sopenharmony_ci{
14138c2ecf20Sopenharmony_ci	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
14148c2ecf20Sopenharmony_ci	struct usbhs_fifo *cfifo = usbhsf_get_cfifo(priv);
14158c2ecf20Sopenharmony_ci	struct usbhs_fifo *dfifo;
14168c2ecf20Sopenharmony_ci	int i;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	mod->irq_empty		= usbhsf_irq_empty;
14198c2ecf20Sopenharmony_ci	mod->irq_ready		= usbhsf_irq_ready;
14208c2ecf20Sopenharmony_ci	mod->irq_bempsts	= 0;
14218c2ecf20Sopenharmony_ci	mod->irq_brdysts	= 0;
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	cfifo->pipe	= NULL;
14248c2ecf20Sopenharmony_ci	usbhs_for_each_dfifo(priv, dfifo, i)
14258c2ecf20Sopenharmony_ci		dfifo->pipe	= NULL;
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_civoid usbhs_fifo_quit(struct usbhs_priv *priv)
14298c2ecf20Sopenharmony_ci{
14308c2ecf20Sopenharmony_ci	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	mod->irq_empty		= NULL;
14338c2ecf20Sopenharmony_ci	mod->irq_ready		= NULL;
14348c2ecf20Sopenharmony_ci	mod->irq_bempsts	= 0;
14358c2ecf20Sopenharmony_ci	mod->irq_brdysts	= 0;
14368c2ecf20Sopenharmony_ci}
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci#define __USBHS_DFIFO_INIT(priv, fifo, channel, fifo_port)		\
14398c2ecf20Sopenharmony_cido {									\
14408c2ecf20Sopenharmony_ci	fifo = usbhsf_get_dnfifo(priv, channel);			\
14418c2ecf20Sopenharmony_ci	fifo->name	= "D"#channel"FIFO";				\
14428c2ecf20Sopenharmony_ci	fifo->port	= fifo_port;					\
14438c2ecf20Sopenharmony_ci	fifo->sel	= D##channel##FIFOSEL;				\
14448c2ecf20Sopenharmony_ci	fifo->ctr	= D##channel##FIFOCTR;				\
14458c2ecf20Sopenharmony_ci	fifo->tx_slave.shdma_slave.slave_id =				\
14468c2ecf20Sopenharmony_ci			usbhs_get_dparam(priv, d##channel##_tx_id);	\
14478c2ecf20Sopenharmony_ci	fifo->rx_slave.shdma_slave.slave_id =				\
14488c2ecf20Sopenharmony_ci			usbhs_get_dparam(priv, d##channel##_rx_id);	\
14498c2ecf20Sopenharmony_ci	usbhsf_dma_init(priv, fifo, channel);				\
14508c2ecf20Sopenharmony_ci} while (0)
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci#define USBHS_DFIFO_INIT(priv, fifo, channel)				\
14538c2ecf20Sopenharmony_ci		__USBHS_DFIFO_INIT(priv, fifo, channel, D##channel##FIFO)
14548c2ecf20Sopenharmony_ci#define USBHS_DFIFO_INIT_NO_PORT(priv, fifo, channel)			\
14558c2ecf20Sopenharmony_ci		__USBHS_DFIFO_INIT(priv, fifo, channel, 0)
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ciint usbhs_fifo_probe(struct usbhs_priv *priv)
14588c2ecf20Sopenharmony_ci{
14598c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	/* CFIFO */
14628c2ecf20Sopenharmony_ci	fifo = usbhsf_get_cfifo(priv);
14638c2ecf20Sopenharmony_ci	fifo->name	= "CFIFO";
14648c2ecf20Sopenharmony_ci	fifo->port	= CFIFO;
14658c2ecf20Sopenharmony_ci	fifo->sel	= CFIFOSEL;
14668c2ecf20Sopenharmony_ci	fifo->ctr	= CFIFOCTR;
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci	/* DFIFO */
14698c2ecf20Sopenharmony_ci	USBHS_DFIFO_INIT(priv, fifo, 0);
14708c2ecf20Sopenharmony_ci	USBHS_DFIFO_INIT(priv, fifo, 1);
14718c2ecf20Sopenharmony_ci	USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 2);
14728c2ecf20Sopenharmony_ci	USBHS_DFIFO_INIT_NO_PORT(priv, fifo, 3);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	return 0;
14758c2ecf20Sopenharmony_ci}
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_civoid usbhs_fifo_remove(struct usbhs_priv *priv)
14788c2ecf20Sopenharmony_ci{
14798c2ecf20Sopenharmony_ci	struct usbhs_fifo *fifo;
14808c2ecf20Sopenharmony_ci	int i;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	usbhs_for_each_dfifo(priv, fifo, i)
14838c2ecf20Sopenharmony_ci		usbhsf_dma_quit(priv, fifo);
14848c2ecf20Sopenharmony_ci}
1485