xref: /kernel/linux/linux-5.10/drivers/usb/dwc2/hcd.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * hcd.c - DesignWare HS OTG Controller host-mode routines
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004-2013 Synopsys, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
88c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
98c2ecf20Sopenharmony_ci * are met:
108c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
118c2ecf20Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
128c2ecf20Sopenharmony_ci *    without modification.
138c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
148c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
158c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
168c2ecf20Sopenharmony_ci * 3. The names of the above-listed copyright holders may not be used
178c2ecf20Sopenharmony_ci *    to endorse or promote products derived from this software without
188c2ecf20Sopenharmony_ci *    specific prior written permission.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * ALTERNATIVELY, this software may be distributed under the terms of the
218c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") as published by the Free Software
228c2ecf20Sopenharmony_ci * Foundation; either version 2 of the License, or (at your option) any
238c2ecf20Sopenharmony_ci * later version.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
268c2ecf20Sopenharmony_ci * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
278c2ecf20Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
288c2ecf20Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
298c2ecf20Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
308c2ecf20Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
318c2ecf20Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
328c2ecf20Sopenharmony_ci * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
338c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
348c2ecf20Sopenharmony_ci * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
358c2ecf20Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * This file contains the core HCD code, and implements the Linux hc_driver
408c2ecf20Sopenharmony_ci * API
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#include <linux/kernel.h>
438c2ecf20Sopenharmony_ci#include <linux/module.h>
448c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
458c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
468c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
478c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
488c2ecf20Sopenharmony_ci#include <linux/delay.h>
498c2ecf20Sopenharmony_ci#include <linux/io.h>
508c2ecf20Sopenharmony_ci#include <linux/slab.h>
518c2ecf20Sopenharmony_ci#include <linux/usb.h>
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include <linux/usb/hcd.h>
548c2ecf20Sopenharmony_ci#include <linux/usb/ch11.h>
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#include "core.h"
578c2ecf20Sopenharmony_ci#include "hcd.h"
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic void dwc2_port_resume(struct dwc2_hsotg *hsotg);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * =========================================================================
638c2ecf20Sopenharmony_ci *  Host Core Layer Functions
648c2ecf20Sopenharmony_ci * =========================================================================
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
698c2ecf20Sopenharmony_ci * used in both device and host modes
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * @hsotg: Programming view of the DWC_otg controller
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistatic void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	u32 intmsk;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* Clear any pending OTG Interrupts */
788c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0xffffffff, GOTGINT);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* Clear any pending interrupts */
818c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Enable the interrupts in the GINTMSK */
848c2ecf20Sopenharmony_ci	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (!hsotg->params.host_dma)
878c2ecf20Sopenharmony_ci		intmsk |= GINTSTS_RXFLVL;
888c2ecf20Sopenharmony_ci	if (!hsotg->params.external_id_pin_ctl)
898c2ecf20Sopenharmony_ci		intmsk |= GINTSTS_CONIDSTSCHNG;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
928c2ecf20Sopenharmony_ci		  GINTSTS_SESSREQINT;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
958c2ecf20Sopenharmony_ci		intmsk |= GINTSTS_LPMTRANRCVD;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intmsk, GINTMSK);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	switch (hsotg->hw_params.arch) {
1058c2ecf20Sopenharmony_ci	case GHWCFG2_EXT_DMA_ARCH:
1068c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "External DMA Mode not supported\n");
1078c2ecf20Sopenharmony_ci		return -EINVAL;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	case GHWCFG2_INT_DMA_ARCH:
1108c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
1118c2ecf20Sopenharmony_ci		if (hsotg->params.ahbcfg != -1) {
1128c2ecf20Sopenharmony_ci			ahbcfg &= GAHBCFG_CTRL_MASK;
1138c2ecf20Sopenharmony_ci			ahbcfg |= hsotg->params.ahbcfg &
1148c2ecf20Sopenharmony_ci				  ~GAHBCFG_CTRL_MASK;
1158c2ecf20Sopenharmony_ci		}
1168c2ecf20Sopenharmony_ci		break;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	case GHWCFG2_SLAVE_ONLY_ARCH:
1198c2ecf20Sopenharmony_ci	default:
1208c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Slave Only Mode\n");
1218c2ecf20Sopenharmony_ci		break;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma)
1258c2ecf20Sopenharmony_ci		ahbcfg |= GAHBCFG_DMA_EN;
1268c2ecf20Sopenharmony_ci	else
1278c2ecf20Sopenharmony_ci		hsotg->params.dma_desc_enable = false;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return 0;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	u32 usbcfg;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	usbcfg = dwc2_readl(hsotg, GUSBCFG);
1398c2ecf20Sopenharmony_ci	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	switch (hsotg->hw_params.op_mode) {
1428c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
1438c2ecf20Sopenharmony_ci		if (hsotg->params.otg_cap ==
1448c2ecf20Sopenharmony_ci				DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
1458c2ecf20Sopenharmony_ci			usbcfg |= GUSBCFG_HNPCAP;
1468c2ecf20Sopenharmony_ci		if (hsotg->params.otg_cap !=
1478c2ecf20Sopenharmony_ci				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
1488c2ecf20Sopenharmony_ci			usbcfg |= GUSBCFG_SRPCAP;
1498c2ecf20Sopenharmony_ci		break;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
1528c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
1538c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
1548c2ecf20Sopenharmony_ci		if (hsotg->params.otg_cap !=
1558c2ecf20Sopenharmony_ci				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
1568c2ecf20Sopenharmony_ci			usbcfg |= GUSBCFG_SRPCAP;
1578c2ecf20Sopenharmony_ci		break;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
1608c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
1618c2ecf20Sopenharmony_ci	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
1628c2ecf20Sopenharmony_ci	default:
1638c2ecf20Sopenharmony_ci		break;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, usbcfg, GUSBCFG);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	if (hsotg->vbus_supply)
1728c2ecf20Sopenharmony_ci		return regulator_enable(hsotg->vbus_supply);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	if (hsotg->vbus_supply)
1808c2ecf20Sopenharmony_ci		return regulator_disable(hsotg->vbus_supply);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/**
1868c2ecf20Sopenharmony_ci * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_cistatic void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	u32 intmsk;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s()\n", __func__);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/* Disable all interrupts */
1978c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, GINTMSK);
1988c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, HAINTMSK);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Enable the common interrupts */
2018c2ecf20Sopenharmony_ci	dwc2_enable_common_interrupts(hsotg);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/* Enable host mode interrupts without disturbing common interrupts */
2048c2ecf20Sopenharmony_ci	intmsk = dwc2_readl(hsotg, GINTMSK);
2058c2ecf20Sopenharmony_ci	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
2068c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intmsk, GINTMSK);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/**
2108c2ecf20Sopenharmony_ci * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_cistatic void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	u32 intmsk = dwc2_readl(hsotg, GINTMSK);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* Disable host mode interrupts without disturbing common interrupts */
2198c2ecf20Sopenharmony_ci	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
2208c2ecf20Sopenharmony_ci		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
2218c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intmsk, GINTMSK);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
2268c2ecf20Sopenharmony_ci * For system that have a total fifo depth that is smaller than the default
2278c2ecf20Sopenharmony_ci * RX + TX fifo size.
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_cistatic void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct dwc2_core_params *params = &hsotg->params;
2348c2ecf20Sopenharmony_ci	struct dwc2_hw_params *hw = &hsotg->hw_params;
2358c2ecf20Sopenharmony_ci	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	total_fifo_size = hw->total_fifo_size;
2388c2ecf20Sopenharmony_ci	rxfsiz = params->host_rx_fifo_size;
2398c2ecf20Sopenharmony_ci	nptxfsiz = params->host_nperio_tx_fifo_size;
2408c2ecf20Sopenharmony_ci	ptxfsiz = params->host_perio_tx_fifo_size;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/*
2438c2ecf20Sopenharmony_ci	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
2448c2ecf20Sopenharmony_ci	 * allocation with support for high bandwidth endpoints. Synopsys
2458c2ecf20Sopenharmony_ci	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
2468c2ecf20Sopenharmony_ci	 * non-periodic as 512.
2478c2ecf20Sopenharmony_ci	 */
2488c2ecf20Sopenharmony_ci	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
2498c2ecf20Sopenharmony_ci		/*
2508c2ecf20Sopenharmony_ci		 * For Buffer DMA mode/Scatter Gather DMA mode
2518c2ecf20Sopenharmony_ci		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
2528c2ecf20Sopenharmony_ci		 * with n = number of host channel.
2538c2ecf20Sopenharmony_ci		 * 2 * ((1024/4) + 2) = 516
2548c2ecf20Sopenharmony_ci		 */
2558c2ecf20Sopenharmony_ci		rxfsiz = 516 + hw->host_channels;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		/*
2588c2ecf20Sopenharmony_ci		 * min non-periodic tx fifo depth
2598c2ecf20Sopenharmony_ci		 * 2 * (largest non-periodic USB packet used / 4)
2608c2ecf20Sopenharmony_ci		 * 2 * (512/4) = 256
2618c2ecf20Sopenharmony_ci		 */
2628c2ecf20Sopenharmony_ci		nptxfsiz = 256;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		/*
2658c2ecf20Sopenharmony_ci		 * min periodic tx fifo depth
2668c2ecf20Sopenharmony_ci		 * (largest packet size*MC)/4
2678c2ecf20Sopenharmony_ci		 * (1024 * 3)/4 = 768
2688c2ecf20Sopenharmony_ci		 */
2698c2ecf20Sopenharmony_ci		ptxfsiz = 768;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		params->host_rx_fifo_size = rxfsiz;
2728c2ecf20Sopenharmony_ci		params->host_nperio_tx_fifo_size = nptxfsiz;
2738c2ecf20Sopenharmony_ci		params->host_perio_tx_fifo_size = ptxfsiz;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/*
2778c2ecf20Sopenharmony_ci	 * If the summation of RX, NPTX and PTX fifo sizes is still
2788c2ecf20Sopenharmony_ci	 * bigger than the total_fifo_size, then we have a problem.
2798c2ecf20Sopenharmony_ci	 *
2808c2ecf20Sopenharmony_ci	 * We won't be able to allocate as many endpoints. Right now,
2818c2ecf20Sopenharmony_ci	 * we're just printing an error message, but ideally this FIFO
2828c2ecf20Sopenharmony_ci	 * allocation algorithm would be improved in the future.
2838c2ecf20Sopenharmony_ci	 *
2848c2ecf20Sopenharmony_ci	 * FIXME improve this FIFO allocation algorithm.
2858c2ecf20Sopenharmony_ci	 */
2868c2ecf20Sopenharmony_ci	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
2878c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "invalid fifo sizes\n");
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct dwc2_core_params *params = &hsotg->params;
2938c2ecf20Sopenharmony_ci	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (!params->enable_dynamic_fifo)
2968c2ecf20Sopenharmony_ci		return;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	dwc2_calculate_dynamic_fifo(hsotg);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* Rx FIFO */
3018c2ecf20Sopenharmony_ci	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
3028c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
3038c2ecf20Sopenharmony_ci	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
3048c2ecf20Sopenharmony_ci	grxfsiz |= params->host_rx_fifo_size <<
3058c2ecf20Sopenharmony_ci		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
3068c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
3078c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
3088c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, GRXFSIZ));
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Non-periodic Tx FIFO */
3118c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
3128c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, GNPTXFSIZ));
3138c2ecf20Sopenharmony_ci	nptxfsiz = params->host_nperio_tx_fifo_size <<
3148c2ecf20Sopenharmony_ci		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
3158c2ecf20Sopenharmony_ci	nptxfsiz |= params->host_rx_fifo_size <<
3168c2ecf20Sopenharmony_ci		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
3178c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
3188c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
3198c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, GNPTXFSIZ));
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* Periodic Tx FIFO */
3228c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
3238c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, HPTXFSIZ));
3248c2ecf20Sopenharmony_ci	hptxfsiz = params->host_perio_tx_fifo_size <<
3258c2ecf20Sopenharmony_ci		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
3268c2ecf20Sopenharmony_ci	hptxfsiz |= (params->host_rx_fifo_size +
3278c2ecf20Sopenharmony_ci		     params->host_nperio_tx_fifo_size) <<
3288c2ecf20Sopenharmony_ci		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
3298c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
3308c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
3318c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, HPTXFSIZ));
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (hsotg->params.en_multiple_tx_fifo &&
3348c2ecf20Sopenharmony_ci	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
3358c2ecf20Sopenharmony_ci		/*
3368c2ecf20Sopenharmony_ci		 * This feature was implemented in 2.91a version
3378c2ecf20Sopenharmony_ci		 * Global DFIFOCFG calculation for Host mode -
3388c2ecf20Sopenharmony_ci		 * include RxFIFO, NPTXFIFO and HPTXFIFO
3398c2ecf20Sopenharmony_ci		 */
3408c2ecf20Sopenharmony_ci		dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
3418c2ecf20Sopenharmony_ci		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
3428c2ecf20Sopenharmony_ci		dfifocfg |= (params->host_rx_fifo_size +
3438c2ecf20Sopenharmony_ci			     params->host_nperio_tx_fifo_size +
3448c2ecf20Sopenharmony_ci			     params->host_perio_tx_fifo_size) <<
3458c2ecf20Sopenharmony_ci			    GDFIFOCFG_EPINFOBASE_SHIFT &
3468c2ecf20Sopenharmony_ci			    GDFIFOCFG_EPINFOBASE_MASK;
3478c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci/**
3528c2ecf20Sopenharmony_ci * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
3538c2ecf20Sopenharmony_ci * the HFIR register according to PHY type and speed
3548c2ecf20Sopenharmony_ci *
3558c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
3568c2ecf20Sopenharmony_ci *
3578c2ecf20Sopenharmony_ci * NOTE: The caller can modify the value of the HFIR register only after the
3588c2ecf20Sopenharmony_ci * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
3598c2ecf20Sopenharmony_ci * has been set
3608c2ecf20Sopenharmony_ci */
3618c2ecf20Sopenharmony_ciu32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	u32 usbcfg;
3648c2ecf20Sopenharmony_ci	u32 hprt0;
3658c2ecf20Sopenharmony_ci	int clock = 60;	/* default value */
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	usbcfg = dwc2_readl(hsotg, GUSBCFG);
3688c2ecf20Sopenharmony_ci	hprt0 = dwc2_readl(hsotg, HPRT0);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
3718c2ecf20Sopenharmony_ci	    !(usbcfg & GUSBCFG_PHYIF16))
3728c2ecf20Sopenharmony_ci		clock = 60;
3738c2ecf20Sopenharmony_ci	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
3748c2ecf20Sopenharmony_ci	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
3758c2ecf20Sopenharmony_ci		clock = 48;
3768c2ecf20Sopenharmony_ci	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
3778c2ecf20Sopenharmony_ci	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
3788c2ecf20Sopenharmony_ci		clock = 30;
3798c2ecf20Sopenharmony_ci	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
3808c2ecf20Sopenharmony_ci	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
3818c2ecf20Sopenharmony_ci		clock = 60;
3828c2ecf20Sopenharmony_ci	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
3838c2ecf20Sopenharmony_ci	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
3848c2ecf20Sopenharmony_ci		clock = 48;
3858c2ecf20Sopenharmony_ci	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
3868c2ecf20Sopenharmony_ci	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
3878c2ecf20Sopenharmony_ci		clock = 48;
3888c2ecf20Sopenharmony_ci	if ((usbcfg & GUSBCFG_PHYSEL) &&
3898c2ecf20Sopenharmony_ci	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
3908c2ecf20Sopenharmony_ci		clock = 48;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
3938c2ecf20Sopenharmony_ci		/* High speed case */
3948c2ecf20Sopenharmony_ci		return 125 * clock - 1;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* FS/LS case */
3978c2ecf20Sopenharmony_ci	return 1000 * clock - 1;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/**
4018c2ecf20Sopenharmony_ci * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
4028c2ecf20Sopenharmony_ci * buffer
4038c2ecf20Sopenharmony_ci *
4048c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
4058c2ecf20Sopenharmony_ci * @dest:    Destination buffer for the packet
4068c2ecf20Sopenharmony_ci * @bytes:   Number of bytes to copy to the destination
4078c2ecf20Sopenharmony_ci */
4088c2ecf20Sopenharmony_civoid dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u32 *data_buf = (u32 *)dest;
4118c2ecf20Sopenharmony_ci	int word_count = (bytes + 3) / 4;
4128c2ecf20Sopenharmony_ci	int i;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/*
4158c2ecf20Sopenharmony_ci	 * Todo: Account for the case where dest is not dword aligned. This
4168c2ecf20Sopenharmony_ci	 * requires reading data from the FIFO into a u32 temp buffer, then
4178c2ecf20Sopenharmony_ci	 * moving it into the data buffer.
4188c2ecf20Sopenharmony_ci	 */
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	for (i = 0; i < word_count; i++, data_buf++)
4238c2ecf20Sopenharmony_ci		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/**
4278c2ecf20Sopenharmony_ci * dwc2_dump_channel_info() - Prints the state of a host channel
4288c2ecf20Sopenharmony_ci *
4298c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
4308c2ecf20Sopenharmony_ci * @chan:  Pointer to the channel to dump
4318c2ecf20Sopenharmony_ci *
4328c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
4338c2ecf20Sopenharmony_ci *
4348c2ecf20Sopenharmony_ci * NOTE: This function will be removed once the peripheral controller code
4358c2ecf20Sopenharmony_ci * is integrated and the driver is stable
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistatic void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
4388c2ecf20Sopenharmony_ci				   struct dwc2_host_chan *chan)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci#ifdef VERBOSE_DEBUG
4418c2ecf20Sopenharmony_ci	int num_channels = hsotg->params.host_channels;
4428c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
4438c2ecf20Sopenharmony_ci	u32 hcchar;
4448c2ecf20Sopenharmony_ci	u32 hcsplt;
4458c2ecf20Sopenharmony_ci	u32 hctsiz;
4468c2ecf20Sopenharmony_ci	u32 hc_dma;
4478c2ecf20Sopenharmony_ci	int i;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (!chan)
4508c2ecf20Sopenharmony_ci		return;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
4538c2ecf20Sopenharmony_ci	hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
4548c2ecf20Sopenharmony_ci	hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
4558c2ecf20Sopenharmony_ci	hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
4588c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
4598c2ecf20Sopenharmony_ci		hcchar, hcsplt);
4608c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
4618c2ecf20Sopenharmony_ci		hctsiz, hc_dma);
4628c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
4638c2ecf20Sopenharmony_ci		chan->dev_addr, chan->ep_num, chan->ep_is_in);
4648c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
4658c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
4668c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
4678c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
4688c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
4698c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
4708c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
4718c2ecf20Sopenharmony_ci		(unsigned long)chan->xfer_dma);
4728c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
4738c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
4748c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
4758c2ecf20Sopenharmony_ci	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
4768c2ecf20Sopenharmony_ci			    qh_list_entry)
4778c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    %p\n", qh);
4788c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
4798c2ecf20Sopenharmony_ci	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
4808c2ecf20Sopenharmony_ci			    qh_list_entry)
4818c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    %p\n", qh);
4828c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  NP active sched:\n");
4838c2ecf20Sopenharmony_ci	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
4848c2ecf20Sopenharmony_ci			    qh_list_entry)
4858c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    %p\n", qh);
4868c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  Channels:\n");
4878c2ecf20Sopenharmony_ci	for (i = 0; i < num_channels; i++) {
4888c2ecf20Sopenharmony_ci		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci#endif /* VERBOSE_DEBUG */
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic int _dwc2_hcd_start(struct usb_hcd *hcd);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic void dwc2_host_start(struct dwc2_hsotg *hsotg)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
5028c2ecf20Sopenharmony_ci	_dwc2_hcd_start(hcd);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	hcd->self.is_b_host = 0;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
5138c2ecf20Sopenharmony_ci			       int *hub_addr, int *hub_port)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	struct urb *urb = context;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (urb->dev->tt)
5188c2ecf20Sopenharmony_ci		*hub_addr = urb->dev->tt->hub->devnum;
5198c2ecf20Sopenharmony_ci	else
5208c2ecf20Sopenharmony_ci		*hub_addr = 0;
5218c2ecf20Sopenharmony_ci	*hub_port = urb->dev->ttport;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/*
5258c2ecf20Sopenharmony_ci * =========================================================================
5268c2ecf20Sopenharmony_ci *  Low Level Host Channel Access Functions
5278c2ecf20Sopenharmony_ci * =========================================================================
5288c2ecf20Sopenharmony_ci */
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_cistatic void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
5318c2ecf20Sopenharmony_ci				      struct dwc2_host_chan *chan)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	u32 hcintmsk = HCINTMSK_CHHLTD;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	switch (chan->ep_type) {
5368c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_CONTROL:
5378c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_BULK:
5388c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "control/bulk\n");
5398c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_XFERCOMPL;
5408c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_STALL;
5418c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_XACTERR;
5428c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_DATATGLERR;
5438c2ecf20Sopenharmony_ci		if (chan->ep_is_in) {
5448c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_BBLERR;
5458c2ecf20Sopenharmony_ci		} else {
5468c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_NAK;
5478c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_NYET;
5488c2ecf20Sopenharmony_ci			if (chan->do_ping)
5498c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_ACK;
5508c2ecf20Sopenharmony_ci		}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		if (chan->do_split) {
5538c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_NAK;
5548c2ecf20Sopenharmony_ci			if (chan->complete_split)
5558c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_NYET;
5568c2ecf20Sopenharmony_ci			else
5578c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_ACK;
5588c2ecf20Sopenharmony_ci		}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci		if (chan->error_state)
5618c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_ACK;
5628c2ecf20Sopenharmony_ci		break;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_INT:
5658c2ecf20Sopenharmony_ci		if (dbg_perio())
5668c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "intr\n");
5678c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_XFERCOMPL;
5688c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_NAK;
5698c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_STALL;
5708c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_XACTERR;
5718c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_DATATGLERR;
5728c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_FRMOVRUN;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		if (chan->ep_is_in)
5758c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_BBLERR;
5768c2ecf20Sopenharmony_ci		if (chan->error_state)
5778c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_ACK;
5788c2ecf20Sopenharmony_ci		if (chan->do_split) {
5798c2ecf20Sopenharmony_ci			if (chan->complete_split)
5808c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_NYET;
5818c2ecf20Sopenharmony_ci			else
5828c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_ACK;
5838c2ecf20Sopenharmony_ci		}
5848c2ecf20Sopenharmony_ci		break;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
5878c2ecf20Sopenharmony_ci		if (dbg_perio())
5888c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "isoc\n");
5898c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_XFERCOMPL;
5908c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_FRMOVRUN;
5918c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_ACK;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		if (chan->ep_is_in) {
5948c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_XACTERR;
5958c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_BBLERR;
5968c2ecf20Sopenharmony_ci		}
5978c2ecf20Sopenharmony_ci		break;
5988c2ecf20Sopenharmony_ci	default:
5998c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "## Unknown EP type ##\n");
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
6048c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
6058c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
6098c2ecf20Sopenharmony_ci				    struct dwc2_host_chan *chan)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	u32 hcintmsk = HCINTMSK_CHHLTD;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/*
6148c2ecf20Sopenharmony_ci	 * For Descriptor DMA mode core halts the channel on AHB error.
6158c2ecf20Sopenharmony_ci	 * Interrupt is not required.
6168c2ecf20Sopenharmony_ci	 */
6178c2ecf20Sopenharmony_ci	if (!hsotg->params.dma_desc_enable) {
6188c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
6198c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
6208c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_AHBERR;
6218c2ecf20Sopenharmony_ci	} else {
6228c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
6238c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
6248c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
6258c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_XFERCOMPL;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (chan->error_state && !chan->do_split &&
6298c2ecf20Sopenharmony_ci	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
6308c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
6318c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "setting ACK\n");
6328c2ecf20Sopenharmony_ci		hcintmsk |= HCINTMSK_ACK;
6338c2ecf20Sopenharmony_ci		if (chan->ep_is_in) {
6348c2ecf20Sopenharmony_ci			hcintmsk |= HCINTMSK_DATATGLERR;
6358c2ecf20Sopenharmony_ci			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
6368c2ecf20Sopenharmony_ci				hcintmsk |= HCINTMSK_NAK;
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
6418c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
6428c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
6468c2ecf20Sopenharmony_ci				struct dwc2_host_chan *chan)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	u32 intmsk;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma) {
6518c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
6528c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "DMA enabled\n");
6538c2ecf20Sopenharmony_ci		dwc2_hc_enable_dma_ints(hsotg, chan);
6548c2ecf20Sopenharmony_ci	} else {
6558c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
6568c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "DMA disabled\n");
6578c2ecf20Sopenharmony_ci		dwc2_hc_enable_slave_ints(hsotg, chan);
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	/* Enable the top level host channel interrupt */
6618c2ecf20Sopenharmony_ci	intmsk = dwc2_readl(hsotg, HAINTMSK);
6628c2ecf20Sopenharmony_ci	intmsk |= 1 << chan->hc_num;
6638c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intmsk, HAINTMSK);
6648c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
6658c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	/* Make sure host channel interrupts are enabled */
6688c2ecf20Sopenharmony_ci	intmsk = dwc2_readl(hsotg, GINTMSK);
6698c2ecf20Sopenharmony_ci	intmsk |= GINTSTS_HCHINT;
6708c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intmsk, GINTMSK);
6718c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
6728c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci/**
6768c2ecf20Sopenharmony_ci * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
6778c2ecf20Sopenharmony_ci * a specific endpoint
6788c2ecf20Sopenharmony_ci *
6798c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
6808c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel
6818c2ecf20Sopenharmony_ci *
6828c2ecf20Sopenharmony_ci * The HCCHARn register is set up with the characteristics specified in chan.
6838c2ecf20Sopenharmony_ci * Host channel interrupts that may need to be serviced while this transfer is
6848c2ecf20Sopenharmony_ci * in progress are enabled.
6858c2ecf20Sopenharmony_ci */
6868c2ecf20Sopenharmony_cistatic void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	u8 hc_num = chan->hc_num;
6898c2ecf20Sopenharmony_ci	u32 hcintmsk;
6908c2ecf20Sopenharmony_ci	u32 hcchar;
6918c2ecf20Sopenharmony_ci	u32 hcsplt = 0;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
6948c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s()\n", __func__);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* Clear old interrupt conditions for this host channel */
6978c2ecf20Sopenharmony_ci	hcintmsk = 0xffffffff;
6988c2ecf20Sopenharmony_ci	hcintmsk &= ~HCINTMSK_RESERVED14_31;
6998c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	/* Enable channel interrupts required for this transfer */
7028c2ecf20Sopenharmony_ci	dwc2_hc_enable_ints(hsotg, chan);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	/*
7058c2ecf20Sopenharmony_ci	 * Program the HCCHARn register with the endpoint characteristics for
7068c2ecf20Sopenharmony_ci	 * the current transfer
7078c2ecf20Sopenharmony_ci	 */
7088c2ecf20Sopenharmony_ci	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
7098c2ecf20Sopenharmony_ci	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
7108c2ecf20Sopenharmony_ci	if (chan->ep_is_in)
7118c2ecf20Sopenharmony_ci		hcchar |= HCCHAR_EPDIR;
7128c2ecf20Sopenharmony_ci	if (chan->speed == USB_SPEED_LOW)
7138c2ecf20Sopenharmony_ci		hcchar |= HCCHAR_LSPDDEV;
7148c2ecf20Sopenharmony_ci	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
7158c2ecf20Sopenharmony_ci	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
7168c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
7178c2ecf20Sopenharmony_ci	if (dbg_hc(chan)) {
7188c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
7198c2ecf20Sopenharmony_ci			 hc_num, hcchar);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
7228c2ecf20Sopenharmony_ci			 __func__, hc_num);
7238c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
7248c2ecf20Sopenharmony_ci			 chan->dev_addr);
7258c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
7268c2ecf20Sopenharmony_ci			 chan->ep_num);
7278c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
7288c2ecf20Sopenharmony_ci			 chan->ep_is_in);
7298c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
7308c2ecf20Sopenharmony_ci			 chan->speed == USB_SPEED_LOW);
7318c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
7328c2ecf20Sopenharmony_ci			 chan->ep_type);
7338c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
7348c2ecf20Sopenharmony_ci			 chan->max_packet);
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	/* Program the HCSPLT register for SPLITs */
7388c2ecf20Sopenharmony_ci	if (chan->do_split) {
7398c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
7408c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev,
7418c2ecf20Sopenharmony_ci				 "Programming HC %d with split --> %s\n",
7428c2ecf20Sopenharmony_ci				 hc_num,
7438c2ecf20Sopenharmony_ci				 chan->complete_split ? "CSPLIT" : "SSPLIT");
7448c2ecf20Sopenharmony_ci		if (chan->complete_split)
7458c2ecf20Sopenharmony_ci			hcsplt |= HCSPLT_COMPSPLT;
7468c2ecf20Sopenharmony_ci		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
7478c2ecf20Sopenharmony_ci			  HCSPLT_XACTPOS_MASK;
7488c2ecf20Sopenharmony_ci		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
7498c2ecf20Sopenharmony_ci			  HCSPLT_HUBADDR_MASK;
7508c2ecf20Sopenharmony_ci		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
7518c2ecf20Sopenharmony_ci			  HCSPLT_PRTADDR_MASK;
7528c2ecf20Sopenharmony_ci		if (dbg_hc(chan)) {
7538c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  comp split %d\n",
7548c2ecf20Sopenharmony_ci				 chan->complete_split);
7558c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
7568c2ecf20Sopenharmony_ci				 chan->xact_pos);
7578c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
7588c2ecf20Sopenharmony_ci				 chan->hub_addr);
7598c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  hub port %d\n",
7608c2ecf20Sopenharmony_ci				 chan->hub_port);
7618c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  is_in %d\n",
7628c2ecf20Sopenharmony_ci				 chan->ep_is_in);
7638c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
7648c2ecf20Sopenharmony_ci				 chan->max_packet);
7658c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
7668c2ecf20Sopenharmony_ci				 chan->xfer_len);
7678c2ecf20Sopenharmony_ci		}
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci/**
7748c2ecf20Sopenharmony_ci * dwc2_hc_halt() - Attempts to halt a host channel
7758c2ecf20Sopenharmony_ci *
7768c2ecf20Sopenharmony_ci * @hsotg:       Controller register interface
7778c2ecf20Sopenharmony_ci * @chan:        Host channel to halt
7788c2ecf20Sopenharmony_ci * @halt_status: Reason for halting the channel
7798c2ecf20Sopenharmony_ci *
7808c2ecf20Sopenharmony_ci * This function should only be called in Slave mode or to abort a transfer in
7818c2ecf20Sopenharmony_ci * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
7828c2ecf20Sopenharmony_ci * controller halts the channel when the transfer is complete or a condition
7838c2ecf20Sopenharmony_ci * occurs that requires application intervention.
7848c2ecf20Sopenharmony_ci *
7858c2ecf20Sopenharmony_ci * In slave mode, checks for a free request queue entry, then sets the Channel
7868c2ecf20Sopenharmony_ci * Enable and Channel Disable bits of the Host Channel Characteristics
7878c2ecf20Sopenharmony_ci * register of the specified channel to intiate the halt. If there is no free
7888c2ecf20Sopenharmony_ci * request queue entry, sets only the Channel Disable bit of the HCCHARn
7898c2ecf20Sopenharmony_ci * register to flush requests for this channel. In the latter case, sets a
7908c2ecf20Sopenharmony_ci * flag to indicate that the host channel needs to be halted when a request
7918c2ecf20Sopenharmony_ci * queue slot is open.
7928c2ecf20Sopenharmony_ci *
7938c2ecf20Sopenharmony_ci * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
7948c2ecf20Sopenharmony_ci * HCCHARn register. The controller ensures there is space in the request
7958c2ecf20Sopenharmony_ci * queue before submitting the halt request.
7968c2ecf20Sopenharmony_ci *
7978c2ecf20Sopenharmony_ci * Some time may elapse before the core flushes any posted requests for this
7988c2ecf20Sopenharmony_ci * host channel and halts. The Channel Halted interrupt handler completes the
7998c2ecf20Sopenharmony_ci * deactivation of the host channel.
8008c2ecf20Sopenharmony_ci */
8018c2ecf20Sopenharmony_civoid dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
8028c2ecf20Sopenharmony_ci		  enum dwc2_halt_status halt_status)
8038c2ecf20Sopenharmony_ci{
8048c2ecf20Sopenharmony_ci	u32 nptxsts, hptxsts, hcchar;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
8078c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s()\n", __func__);
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	/*
8108c2ecf20Sopenharmony_ci	 * In buffer DMA or external DMA mode channel can't be halted
8118c2ecf20Sopenharmony_ci	 * for non-split periodic channels. At the end of the next
8128c2ecf20Sopenharmony_ci	 * uframe/frame (in the worst case), the core generates a channel
8138c2ecf20Sopenharmony_ci	 * halted and disables the channel automatically.
8148c2ecf20Sopenharmony_ci	 */
8158c2ecf20Sopenharmony_ci	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
8168c2ecf20Sopenharmony_ci	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
8178c2ecf20Sopenharmony_ci		if (!chan->do_split &&
8188c2ecf20Sopenharmony_ci		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
8198c2ecf20Sopenharmony_ci		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
8208c2ecf20Sopenharmony_ci			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
8218c2ecf20Sopenharmony_ci				__func__);
8228c2ecf20Sopenharmony_ci			return;
8238c2ecf20Sopenharmony_ci		}
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
8278c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
8308c2ecf20Sopenharmony_ci	    halt_status == DWC2_HC_XFER_AHB_ERR) {
8318c2ecf20Sopenharmony_ci		/*
8328c2ecf20Sopenharmony_ci		 * Disable all channel interrupts except Ch Halted. The QTD
8338c2ecf20Sopenharmony_ci		 * and QH state associated with this transfer has been cleared
8348c2ecf20Sopenharmony_ci		 * (in the case of URB_DEQUEUE), so the channel needs to be
8358c2ecf20Sopenharmony_ci		 * shut down carefully to prevent crashes.
8368c2ecf20Sopenharmony_ci		 */
8378c2ecf20Sopenharmony_ci		u32 hcintmsk = HCINTMSK_CHHLTD;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "dequeue/error\n");
8408c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci		/*
8438c2ecf20Sopenharmony_ci		 * Make sure no other interrupts besides halt are currently
8448c2ecf20Sopenharmony_ci		 * pending. Handling another interrupt could cause a crash due
8458c2ecf20Sopenharmony_ci		 * to the QTD and QH state.
8468c2ecf20Sopenharmony_ci		 */
8478c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci		/*
8508c2ecf20Sopenharmony_ci		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
8518c2ecf20Sopenharmony_ci		 * even if the channel was already halted for some other
8528c2ecf20Sopenharmony_ci		 * reason
8538c2ecf20Sopenharmony_ci		 */
8548c2ecf20Sopenharmony_ci		chan->halt_status = halt_status;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci		hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
8578c2ecf20Sopenharmony_ci		if (!(hcchar & HCCHAR_CHENA)) {
8588c2ecf20Sopenharmony_ci			/*
8598c2ecf20Sopenharmony_ci			 * The channel is either already halted or it hasn't
8608c2ecf20Sopenharmony_ci			 * started yet. In DMA mode, the transfer may halt if
8618c2ecf20Sopenharmony_ci			 * it finishes normally or a condition occurs that
8628c2ecf20Sopenharmony_ci			 * requires driver intervention. Don't want to halt
8638c2ecf20Sopenharmony_ci			 * the channel again. In either Slave or DMA mode,
8648c2ecf20Sopenharmony_ci			 * it's possible that the transfer has been assigned
8658c2ecf20Sopenharmony_ci			 * to a channel, but not started yet when an URB is
8668c2ecf20Sopenharmony_ci			 * dequeued. Don't want to halt a channel that hasn't
8678c2ecf20Sopenharmony_ci			 * started yet.
8688c2ecf20Sopenharmony_ci			 */
8698c2ecf20Sopenharmony_ci			return;
8708c2ecf20Sopenharmony_ci		}
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci	if (chan->halt_pending) {
8738c2ecf20Sopenharmony_ci		/*
8748c2ecf20Sopenharmony_ci		 * A halt has already been issued for this channel. This might
8758c2ecf20Sopenharmony_ci		 * happen when a transfer is aborted by a higher level in
8768c2ecf20Sopenharmony_ci		 * the stack.
8778c2ecf20Sopenharmony_ci		 */
8788c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
8798c2ecf20Sopenharmony_ci			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
8808c2ecf20Sopenharmony_ci			 __func__, chan->hc_num);
8818c2ecf20Sopenharmony_ci		return;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	/* No need to set the bit in DDMA for disabling the channel */
8878c2ecf20Sopenharmony_ci	/* TODO check it everywhere channel is disabled */
8888c2ecf20Sopenharmony_ci	if (!hsotg->params.dma_desc_enable) {
8898c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
8908c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
8918c2ecf20Sopenharmony_ci		hcchar |= HCCHAR_CHENA;
8928c2ecf20Sopenharmony_ci	} else {
8938c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
8948c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "desc DMA enabled\n");
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci	hcchar |= HCCHAR_CHDIS;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (!hsotg->params.host_dma) {
8998c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
9008c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "DMA not enabled\n");
9018c2ecf20Sopenharmony_ci		hcchar |= HCCHAR_CHENA;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci		/* Check for space in the request queue to issue the halt */
9048c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
9058c2ecf20Sopenharmony_ci		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
9068c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "control/bulk\n");
9078c2ecf20Sopenharmony_ci			nptxsts = dwc2_readl(hsotg, GNPTXSTS);
9088c2ecf20Sopenharmony_ci			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
9098c2ecf20Sopenharmony_ci				dev_vdbg(hsotg->dev, "Disabling channel\n");
9108c2ecf20Sopenharmony_ci				hcchar &= ~HCCHAR_CHENA;
9118c2ecf20Sopenharmony_ci			}
9128c2ecf20Sopenharmony_ci		} else {
9138c2ecf20Sopenharmony_ci			if (dbg_perio())
9148c2ecf20Sopenharmony_ci				dev_vdbg(hsotg->dev, "isoc/intr\n");
9158c2ecf20Sopenharmony_ci			hptxsts = dwc2_readl(hsotg, HPTXSTS);
9168c2ecf20Sopenharmony_ci			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
9178c2ecf20Sopenharmony_ci			    hsotg->queuing_high_bandwidth) {
9188c2ecf20Sopenharmony_ci				if (dbg_perio())
9198c2ecf20Sopenharmony_ci					dev_vdbg(hsotg->dev, "Disabling channel\n");
9208c2ecf20Sopenharmony_ci				hcchar &= ~HCCHAR_CHENA;
9218c2ecf20Sopenharmony_ci			}
9228c2ecf20Sopenharmony_ci		}
9238c2ecf20Sopenharmony_ci	} else {
9248c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
9258c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "DMA enabled\n");
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
9298c2ecf20Sopenharmony_ci	chan->halt_status = halt_status;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	if (hcchar & HCCHAR_CHENA) {
9328c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
9338c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "Channel enabled\n");
9348c2ecf20Sopenharmony_ci		chan->halt_pending = 1;
9358c2ecf20Sopenharmony_ci		chan->halt_on_queue = 0;
9368c2ecf20Sopenharmony_ci	} else {
9378c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
9388c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "Channel disabled\n");
9398c2ecf20Sopenharmony_ci		chan->halt_on_queue = 1;
9408c2ecf20Sopenharmony_ci	}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (dbg_hc(chan)) {
9438c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
9448c2ecf20Sopenharmony_ci			 chan->hc_num);
9458c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
9468c2ecf20Sopenharmony_ci			 hcchar);
9478c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
9488c2ecf20Sopenharmony_ci			 chan->halt_pending);
9498c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
9508c2ecf20Sopenharmony_ci			 chan->halt_on_queue);
9518c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
9528c2ecf20Sopenharmony_ci			 chan->halt_status);
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci/**
9578c2ecf20Sopenharmony_ci * dwc2_hc_cleanup() - Clears the transfer state for a host channel
9588c2ecf20Sopenharmony_ci *
9598c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
9608c2ecf20Sopenharmony_ci * @chan:  Identifies the host channel to clean up
9618c2ecf20Sopenharmony_ci *
9628c2ecf20Sopenharmony_ci * This function is normally called after a transfer is done and the host
9638c2ecf20Sopenharmony_ci * channel is being released
9648c2ecf20Sopenharmony_ci */
9658c2ecf20Sopenharmony_civoid dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	u32 hcintmsk;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	chan->xfer_started = 0;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	list_del_init(&chan->split_order_list_entry);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	/*
9748c2ecf20Sopenharmony_ci	 * Clear channel interrupt enables and any unhandled channel interrupt
9758c2ecf20Sopenharmony_ci	 * conditions
9768c2ecf20Sopenharmony_ci	 */
9778c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
9788c2ecf20Sopenharmony_ci	hcintmsk = 0xffffffff;
9798c2ecf20Sopenharmony_ci	hcintmsk &= ~HCINTMSK_RESERVED14_31;
9808c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
9818c2ecf20Sopenharmony_ci}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci/**
9848c2ecf20Sopenharmony_ci * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
9858c2ecf20Sopenharmony_ci * which frame a periodic transfer should occur
9868c2ecf20Sopenharmony_ci *
9878c2ecf20Sopenharmony_ci * @hsotg:  Programming view of DWC_otg controller
9888c2ecf20Sopenharmony_ci * @chan:   Identifies the host channel to set up and its properties
9898c2ecf20Sopenharmony_ci * @hcchar: Current value of the HCCHAR register for the specified host channel
9908c2ecf20Sopenharmony_ci *
9918c2ecf20Sopenharmony_ci * This function has no effect on non-periodic transfers
9928c2ecf20Sopenharmony_ci */
9938c2ecf20Sopenharmony_cistatic void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
9948c2ecf20Sopenharmony_ci				       struct dwc2_host_chan *chan, u32 *hcchar)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
9978c2ecf20Sopenharmony_ci	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
9988c2ecf20Sopenharmony_ci		int host_speed;
9998c2ecf20Sopenharmony_ci		int xfer_ns;
10008c2ecf20Sopenharmony_ci		int xfer_us;
10018c2ecf20Sopenharmony_ci		int bytes_in_fifo;
10028c2ecf20Sopenharmony_ci		u16 fifo_space;
10038c2ecf20Sopenharmony_ci		u16 frame_number;
10048c2ecf20Sopenharmony_ci		u16 wire_frame;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci		/*
10078c2ecf20Sopenharmony_ci		 * Try to figure out if we're an even or odd frame. If we set
10088c2ecf20Sopenharmony_ci		 * even and the current frame number is even the the transfer
10098c2ecf20Sopenharmony_ci		 * will happen immediately.  Similar if both are odd. If one is
10108c2ecf20Sopenharmony_ci		 * even and the other is odd then the transfer will happen when
10118c2ecf20Sopenharmony_ci		 * the frame number ticks.
10128c2ecf20Sopenharmony_ci		 *
10138c2ecf20Sopenharmony_ci		 * There's a bit of a balancing act to get this right.
10148c2ecf20Sopenharmony_ci		 * Sometimes we may want to send data in the current frame (AK
10158c2ecf20Sopenharmony_ci		 * right away).  We might want to do this if the frame number
10168c2ecf20Sopenharmony_ci		 * _just_ ticked, but we might also want to do this in order
10178c2ecf20Sopenharmony_ci		 * to continue a split transaction that happened late in a
10188c2ecf20Sopenharmony_ci		 * microframe (so we didn't know to queue the next transfer
10198c2ecf20Sopenharmony_ci		 * until the frame number had ticked).  The problem is that we
10208c2ecf20Sopenharmony_ci		 * need a lot of knowledge to know if there's actually still
10218c2ecf20Sopenharmony_ci		 * time to send things or if it would be better to wait until
10228c2ecf20Sopenharmony_ci		 * the next frame.
10238c2ecf20Sopenharmony_ci		 *
10248c2ecf20Sopenharmony_ci		 * We can look at how much time is left in the current frame
10258c2ecf20Sopenharmony_ci		 * and make a guess about whether we'll have time to transfer.
10268c2ecf20Sopenharmony_ci		 * We'll do that.
10278c2ecf20Sopenharmony_ci		 */
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci		/* Get speed host is running at */
10308c2ecf20Sopenharmony_ci		host_speed = (chan->speed != USB_SPEED_HIGH &&
10318c2ecf20Sopenharmony_ci			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci		/* See how many bytes are in the periodic FIFO right now */
10348c2ecf20Sopenharmony_ci		fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
10358c2ecf20Sopenharmony_ci			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
10368c2ecf20Sopenharmony_ci		bytes_in_fifo = sizeof(u32) *
10378c2ecf20Sopenharmony_ci				(hsotg->params.host_perio_tx_fifo_size -
10388c2ecf20Sopenharmony_ci				 fifo_space);
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci		/*
10418c2ecf20Sopenharmony_ci		 * Roughly estimate bus time for everything in the periodic
10428c2ecf20Sopenharmony_ci		 * queue + our new transfer.  This is "rough" because we're
10438c2ecf20Sopenharmony_ci		 * using a function that makes takes into account IN/OUT
10448c2ecf20Sopenharmony_ci		 * and INT/ISO and we're just slamming in one value for all
10458c2ecf20Sopenharmony_ci		 * transfers.  This should be an over-estimate and that should
10468c2ecf20Sopenharmony_ci		 * be OK, but we can probably tighten it.
10478c2ecf20Sopenharmony_ci		 */
10488c2ecf20Sopenharmony_ci		xfer_ns = usb_calc_bus_time(host_speed, false, false,
10498c2ecf20Sopenharmony_ci					    chan->xfer_len + bytes_in_fifo);
10508c2ecf20Sopenharmony_ci		xfer_us = NS_TO_US(xfer_ns);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci		/* See what frame number we'll be at by the time we finish */
10538c2ecf20Sopenharmony_ci		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci		/* This is when we were scheduled to be on the wire */
10568c2ecf20Sopenharmony_ci		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci		/*
10598c2ecf20Sopenharmony_ci		 * If we'd finish _after_ the frame we're scheduled in then
10608c2ecf20Sopenharmony_ci		 * it's hopeless.  Just schedule right away and hope for the
10618c2ecf20Sopenharmony_ci		 * best.  Note that it _might_ be wise to call back into the
10628c2ecf20Sopenharmony_ci		 * scheduler to pick a better frame, but this is better than
10638c2ecf20Sopenharmony_ci		 * nothing.
10648c2ecf20Sopenharmony_ci		 */
10658c2ecf20Sopenharmony_ci		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
10668c2ecf20Sopenharmony_ci			dwc2_sch_vdbg(hsotg,
10678c2ecf20Sopenharmony_ci				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
10688c2ecf20Sopenharmony_ci				      chan->qh, wire_frame, frame_number,
10698c2ecf20Sopenharmony_ci				      dwc2_frame_num_dec(frame_number,
10708c2ecf20Sopenharmony_ci							 wire_frame));
10718c2ecf20Sopenharmony_ci			wire_frame = frame_number;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci			/*
10748c2ecf20Sopenharmony_ci			 * We picked a different frame number; communicate this
10758c2ecf20Sopenharmony_ci			 * back to the scheduler so it doesn't try to schedule
10768c2ecf20Sopenharmony_ci			 * another in the same frame.
10778c2ecf20Sopenharmony_ci			 *
10788c2ecf20Sopenharmony_ci			 * Remember that next_active_frame is 1 before the wire
10798c2ecf20Sopenharmony_ci			 * frame.
10808c2ecf20Sopenharmony_ci			 */
10818c2ecf20Sopenharmony_ci			chan->qh->next_active_frame =
10828c2ecf20Sopenharmony_ci				dwc2_frame_num_dec(frame_number, 1);
10838c2ecf20Sopenharmony_ci		}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci		if (wire_frame & 1)
10868c2ecf20Sopenharmony_ci			*hcchar |= HCCHAR_ODDFRM;
10878c2ecf20Sopenharmony_ci		else
10888c2ecf20Sopenharmony_ci			*hcchar &= ~HCCHAR_ODDFRM;
10898c2ecf20Sopenharmony_ci	}
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_cistatic void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
10938c2ecf20Sopenharmony_ci{
10948c2ecf20Sopenharmony_ci	/* Set up the initial PID for the transfer */
10958c2ecf20Sopenharmony_ci	if (chan->speed == USB_SPEED_HIGH) {
10968c2ecf20Sopenharmony_ci		if (chan->ep_is_in) {
10978c2ecf20Sopenharmony_ci			if (chan->multi_count == 1)
10988c2ecf20Sopenharmony_ci				chan->data_pid_start = DWC2_HC_PID_DATA0;
10998c2ecf20Sopenharmony_ci			else if (chan->multi_count == 2)
11008c2ecf20Sopenharmony_ci				chan->data_pid_start = DWC2_HC_PID_DATA1;
11018c2ecf20Sopenharmony_ci			else
11028c2ecf20Sopenharmony_ci				chan->data_pid_start = DWC2_HC_PID_DATA2;
11038c2ecf20Sopenharmony_ci		} else {
11048c2ecf20Sopenharmony_ci			if (chan->multi_count == 1)
11058c2ecf20Sopenharmony_ci				chan->data_pid_start = DWC2_HC_PID_DATA0;
11068c2ecf20Sopenharmony_ci			else
11078c2ecf20Sopenharmony_ci				chan->data_pid_start = DWC2_HC_PID_MDATA;
11088c2ecf20Sopenharmony_ci		}
11098c2ecf20Sopenharmony_ci	} else {
11108c2ecf20Sopenharmony_ci		chan->data_pid_start = DWC2_HC_PID_DATA0;
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci/**
11158c2ecf20Sopenharmony_ci * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
11168c2ecf20Sopenharmony_ci * the Host Channel
11178c2ecf20Sopenharmony_ci *
11188c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
11198c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel
11208c2ecf20Sopenharmony_ci *
11218c2ecf20Sopenharmony_ci * This function should only be called in Slave mode. For a channel associated
11228c2ecf20Sopenharmony_ci * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
11238c2ecf20Sopenharmony_ci * associated with a periodic EP, the periodic Tx FIFO is written.
11248c2ecf20Sopenharmony_ci *
11258c2ecf20Sopenharmony_ci * Upon return the xfer_buf and xfer_count fields in chan are incremented by
11268c2ecf20Sopenharmony_ci * the number of bytes written to the Tx FIFO.
11278c2ecf20Sopenharmony_ci */
11288c2ecf20Sopenharmony_cistatic void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
11298c2ecf20Sopenharmony_ci				 struct dwc2_host_chan *chan)
11308c2ecf20Sopenharmony_ci{
11318c2ecf20Sopenharmony_ci	u32 i;
11328c2ecf20Sopenharmony_ci	u32 remaining_count;
11338c2ecf20Sopenharmony_ci	u32 byte_count;
11348c2ecf20Sopenharmony_ci	u32 dword_count;
11358c2ecf20Sopenharmony_ci	u32 *data_buf = (u32 *)chan->xfer_buf;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
11388c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s()\n", __func__);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	remaining_count = chan->xfer_len - chan->xfer_count;
11418c2ecf20Sopenharmony_ci	if (remaining_count > chan->max_packet)
11428c2ecf20Sopenharmony_ci		byte_count = chan->max_packet;
11438c2ecf20Sopenharmony_ci	else
11448c2ecf20Sopenharmony_ci		byte_count = remaining_count;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	dword_count = (byte_count + 3) / 4;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	if (((unsigned long)data_buf & 0x3) == 0) {
11498c2ecf20Sopenharmony_ci		/* xfer_buf is DWORD aligned */
11508c2ecf20Sopenharmony_ci		for (i = 0; i < dword_count; i++, data_buf++)
11518c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
11528c2ecf20Sopenharmony_ci	} else {
11538c2ecf20Sopenharmony_ci		/* xfer_buf is not DWORD aligned */
11548c2ecf20Sopenharmony_ci		for (i = 0; i < dword_count; i++, data_buf++) {
11558c2ecf20Sopenharmony_ci			u32 data = data_buf[0] | data_buf[1] << 8 |
11568c2ecf20Sopenharmony_ci				   data_buf[2] << 16 | data_buf[3] << 24;
11578c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
11588c2ecf20Sopenharmony_ci		}
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	chan->xfer_count += byte_count;
11628c2ecf20Sopenharmony_ci	chan->xfer_buf += byte_count;
11638c2ecf20Sopenharmony_ci}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci/**
11668c2ecf20Sopenharmony_ci * dwc2_hc_do_ping() - Starts a PING transfer
11678c2ecf20Sopenharmony_ci *
11688c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
11698c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel
11708c2ecf20Sopenharmony_ci *
11718c2ecf20Sopenharmony_ci * This function should only be called in Slave mode. The Do Ping bit is set in
11728c2ecf20Sopenharmony_ci * the HCTSIZ register, then the channel is enabled.
11738c2ecf20Sopenharmony_ci */
11748c2ecf20Sopenharmony_cistatic void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
11758c2ecf20Sopenharmony_ci			    struct dwc2_host_chan *chan)
11768c2ecf20Sopenharmony_ci{
11778c2ecf20Sopenharmony_ci	u32 hcchar;
11788c2ecf20Sopenharmony_ci	u32 hctsiz;
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
11818c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
11828c2ecf20Sopenharmony_ci			 chan->hc_num);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	hctsiz = TSIZ_DOPNG;
11858c2ecf20Sopenharmony_ci	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
11868c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
11898c2ecf20Sopenharmony_ci	hcchar |= HCCHAR_CHENA;
11908c2ecf20Sopenharmony_ci	hcchar &= ~HCCHAR_CHDIS;
11918c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci/**
11958c2ecf20Sopenharmony_ci * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
11968c2ecf20Sopenharmony_ci * channel and starts the transfer
11978c2ecf20Sopenharmony_ci *
11988c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
11998c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel. The xfer_len value
12008c2ecf20Sopenharmony_ci *         may be reduced to accommodate the max widths of the XferSize and
12018c2ecf20Sopenharmony_ci *         PktCnt fields in the HCTSIZn register. The multi_count value may be
12028c2ecf20Sopenharmony_ci *         changed to reflect the final xfer_len value.
12038c2ecf20Sopenharmony_ci *
12048c2ecf20Sopenharmony_ci * This function may be called in either Slave mode or DMA mode. In Slave mode,
12058c2ecf20Sopenharmony_ci * the caller must ensure that there is sufficient space in the request queue
12068c2ecf20Sopenharmony_ci * and Tx Data FIFO.
12078c2ecf20Sopenharmony_ci *
12088c2ecf20Sopenharmony_ci * For an OUT transfer in Slave mode, it loads a data packet into the
12098c2ecf20Sopenharmony_ci * appropriate FIFO. If necessary, additional data packets are loaded in the
12108c2ecf20Sopenharmony_ci * Host ISR.
12118c2ecf20Sopenharmony_ci *
12128c2ecf20Sopenharmony_ci * For an IN transfer in Slave mode, a data packet is requested. The data
12138c2ecf20Sopenharmony_ci * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
12148c2ecf20Sopenharmony_ci * additional data packets are requested in the Host ISR.
12158c2ecf20Sopenharmony_ci *
12168c2ecf20Sopenharmony_ci * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
12178c2ecf20Sopenharmony_ci * register along with a packet count of 1 and the channel is enabled. This
12188c2ecf20Sopenharmony_ci * causes a single PING transaction to occur. Other fields in HCTSIZ are
12198c2ecf20Sopenharmony_ci * simply set to 0 since no data transfer occurs in this case.
12208c2ecf20Sopenharmony_ci *
12218c2ecf20Sopenharmony_ci * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
12228c2ecf20Sopenharmony_ci * all the information required to perform the subsequent data transfer. In
12238c2ecf20Sopenharmony_ci * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
12248c2ecf20Sopenharmony_ci * controller performs the entire PING protocol, then starts the data
12258c2ecf20Sopenharmony_ci * transfer.
12268c2ecf20Sopenharmony_ci */
12278c2ecf20Sopenharmony_cistatic void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
12288c2ecf20Sopenharmony_ci				   struct dwc2_host_chan *chan)
12298c2ecf20Sopenharmony_ci{
12308c2ecf20Sopenharmony_ci	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
12318c2ecf20Sopenharmony_ci	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
12328c2ecf20Sopenharmony_ci	u32 hcchar;
12338c2ecf20Sopenharmony_ci	u32 hctsiz = 0;
12348c2ecf20Sopenharmony_ci	u16 num_packets;
12358c2ecf20Sopenharmony_ci	u32 ec_mc;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
12388c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s()\n", __func__);
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	if (chan->do_ping) {
12418c2ecf20Sopenharmony_ci		if (!hsotg->params.host_dma) {
12428c2ecf20Sopenharmony_ci			if (dbg_hc(chan))
12438c2ecf20Sopenharmony_ci				dev_vdbg(hsotg->dev, "ping, no DMA\n");
12448c2ecf20Sopenharmony_ci			dwc2_hc_do_ping(hsotg, chan);
12458c2ecf20Sopenharmony_ci			chan->xfer_started = 1;
12468c2ecf20Sopenharmony_ci			return;
12478c2ecf20Sopenharmony_ci		}
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
12508c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "ping, DMA\n");
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci		hctsiz |= TSIZ_DOPNG;
12538c2ecf20Sopenharmony_ci	}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	if (chan->do_split) {
12568c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
12578c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "split\n");
12588c2ecf20Sopenharmony_ci		num_packets = 1;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci		if (chan->complete_split && !chan->ep_is_in)
12618c2ecf20Sopenharmony_ci			/*
12628c2ecf20Sopenharmony_ci			 * For CSPLIT OUT Transfer, set the size to 0 so the
12638c2ecf20Sopenharmony_ci			 * core doesn't expect any data written to the FIFO
12648c2ecf20Sopenharmony_ci			 */
12658c2ecf20Sopenharmony_ci			chan->xfer_len = 0;
12668c2ecf20Sopenharmony_ci		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
12678c2ecf20Sopenharmony_ci			chan->xfer_len = chan->max_packet;
12688c2ecf20Sopenharmony_ci		else if (!chan->ep_is_in && chan->xfer_len > 188)
12698c2ecf20Sopenharmony_ci			chan->xfer_len = 188;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
12728c2ecf20Sopenharmony_ci			  TSIZ_XFERSIZE_MASK;
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci		/* For split set ec_mc for immediate retries */
12758c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
12768c2ecf20Sopenharmony_ci		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
12778c2ecf20Sopenharmony_ci			ec_mc = 3;
12788c2ecf20Sopenharmony_ci		else
12798c2ecf20Sopenharmony_ci			ec_mc = 1;
12808c2ecf20Sopenharmony_ci	} else {
12818c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
12828c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "no split\n");
12838c2ecf20Sopenharmony_ci		/*
12848c2ecf20Sopenharmony_ci		 * Ensure that the transfer length and packet count will fit
12858c2ecf20Sopenharmony_ci		 * in the widths allocated for them in the HCTSIZn register
12868c2ecf20Sopenharmony_ci		 */
12878c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
12888c2ecf20Sopenharmony_ci		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
12898c2ecf20Sopenharmony_ci			/*
12908c2ecf20Sopenharmony_ci			 * Make sure the transfer size is no larger than one
12918c2ecf20Sopenharmony_ci			 * (micro)frame's worth of data. (A check was done
12928c2ecf20Sopenharmony_ci			 * when the periodic transfer was accepted to ensure
12938c2ecf20Sopenharmony_ci			 * that a (micro)frame's worth of data can be
12948c2ecf20Sopenharmony_ci			 * programmed into a channel.)
12958c2ecf20Sopenharmony_ci			 */
12968c2ecf20Sopenharmony_ci			u32 max_periodic_len =
12978c2ecf20Sopenharmony_ci				chan->multi_count * chan->max_packet;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci			if (chan->xfer_len > max_periodic_len)
13008c2ecf20Sopenharmony_ci				chan->xfer_len = max_periodic_len;
13018c2ecf20Sopenharmony_ci		} else if (chan->xfer_len > max_hc_xfer_size) {
13028c2ecf20Sopenharmony_ci			/*
13038c2ecf20Sopenharmony_ci			 * Make sure that xfer_len is a multiple of max packet
13048c2ecf20Sopenharmony_ci			 * size
13058c2ecf20Sopenharmony_ci			 */
13068c2ecf20Sopenharmony_ci			chan->xfer_len =
13078c2ecf20Sopenharmony_ci				max_hc_xfer_size - chan->max_packet + 1;
13088c2ecf20Sopenharmony_ci		}
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci		if (chan->xfer_len > 0) {
13118c2ecf20Sopenharmony_ci			num_packets = (chan->xfer_len + chan->max_packet - 1) /
13128c2ecf20Sopenharmony_ci					chan->max_packet;
13138c2ecf20Sopenharmony_ci			if (num_packets > max_hc_pkt_count) {
13148c2ecf20Sopenharmony_ci				num_packets = max_hc_pkt_count;
13158c2ecf20Sopenharmony_ci				chan->xfer_len = num_packets * chan->max_packet;
13168c2ecf20Sopenharmony_ci			} else if (chan->ep_is_in) {
13178c2ecf20Sopenharmony_ci				/*
13188c2ecf20Sopenharmony_ci				 * Always program an integral # of max packets
13198c2ecf20Sopenharmony_ci				 * for IN transfers.
13208c2ecf20Sopenharmony_ci				 * Note: This assumes that the input buffer is
13218c2ecf20Sopenharmony_ci				 * aligned and sized accordingly.
13228c2ecf20Sopenharmony_ci				 */
13238c2ecf20Sopenharmony_ci				chan->xfer_len = num_packets * chan->max_packet;
13248c2ecf20Sopenharmony_ci			}
13258c2ecf20Sopenharmony_ci		} else {
13268c2ecf20Sopenharmony_ci			/* Need 1 packet for transfer length of 0 */
13278c2ecf20Sopenharmony_ci			num_packets = 1;
13288c2ecf20Sopenharmony_ci		}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
13318c2ecf20Sopenharmony_ci		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
13328c2ecf20Sopenharmony_ci			/*
13338c2ecf20Sopenharmony_ci			 * Make sure that the multi_count field matches the
13348c2ecf20Sopenharmony_ci			 * actual transfer length
13358c2ecf20Sopenharmony_ci			 */
13368c2ecf20Sopenharmony_ci			chan->multi_count = num_packets;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
13398c2ecf20Sopenharmony_ci			dwc2_set_pid_isoc(chan);
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
13428c2ecf20Sopenharmony_ci			  TSIZ_XFERSIZE_MASK;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci		/* The ec_mc gets the multi_count for non-split */
13458c2ecf20Sopenharmony_ci		ec_mc = chan->multi_count;
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	chan->start_pkt_count = num_packets;
13498c2ecf20Sopenharmony_ci	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
13508c2ecf20Sopenharmony_ci	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
13518c2ecf20Sopenharmony_ci		  TSIZ_SC_MC_PID_MASK;
13528c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
13538c2ecf20Sopenharmony_ci	if (dbg_hc(chan)) {
13548c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
13558c2ecf20Sopenharmony_ci			 hctsiz, chan->hc_num);
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
13588c2ecf20Sopenharmony_ci			 chan->hc_num);
13598c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
13608c2ecf20Sopenharmony_ci			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
13618c2ecf20Sopenharmony_ci			 TSIZ_XFERSIZE_SHIFT);
13628c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
13638c2ecf20Sopenharmony_ci			 (hctsiz & TSIZ_PKTCNT_MASK) >>
13648c2ecf20Sopenharmony_ci			 TSIZ_PKTCNT_SHIFT);
13658c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
13668c2ecf20Sopenharmony_ci			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
13678c2ecf20Sopenharmony_ci			 TSIZ_SC_MC_PID_SHIFT);
13688c2ecf20Sopenharmony_ci	}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma) {
13718c2ecf20Sopenharmony_ci		dma_addr_t dma_addr;
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci		if (chan->align_buf) {
13748c2ecf20Sopenharmony_ci			if (dbg_hc(chan))
13758c2ecf20Sopenharmony_ci				dev_vdbg(hsotg->dev, "align_buf\n");
13768c2ecf20Sopenharmony_ci			dma_addr = chan->align_buf;
13778c2ecf20Sopenharmony_ci		} else {
13788c2ecf20Sopenharmony_ci			dma_addr = chan->xfer_dma;
13798c2ecf20Sopenharmony_ci		}
13808c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
13838c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
13848c2ecf20Sopenharmony_ci				 (unsigned long)dma_addr, chan->hc_num);
13858c2ecf20Sopenharmony_ci	}
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	/* Start the split */
13888c2ecf20Sopenharmony_ci	if (chan->do_split) {
13898c2ecf20Sopenharmony_ci		u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci		hcsplt |= HCSPLT_SPLTENA;
13928c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
13938c2ecf20Sopenharmony_ci	}
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
13968c2ecf20Sopenharmony_ci	hcchar &= ~HCCHAR_MULTICNT_MASK;
13978c2ecf20Sopenharmony_ci	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
13988c2ecf20Sopenharmony_ci	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	if (hcchar & HCCHAR_CHDIS)
14018c2ecf20Sopenharmony_ci		dev_warn(hsotg->dev,
14028c2ecf20Sopenharmony_ci			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
14038c2ecf20Sopenharmony_ci			 __func__, chan->hc_num, hcchar);
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	/* Set host channel enable after all other setup is complete */
14068c2ecf20Sopenharmony_ci	hcchar |= HCCHAR_CHENA;
14078c2ecf20Sopenharmony_ci	hcchar &= ~HCCHAR_CHDIS;
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
14108c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
14118c2ecf20Sopenharmony_ci			 (hcchar & HCCHAR_MULTICNT_MASK) >>
14128c2ecf20Sopenharmony_ci			 HCCHAR_MULTICNT_SHIFT);
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
14158c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
14168c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
14178c2ecf20Sopenharmony_ci			 chan->hc_num);
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	chan->xfer_started = 1;
14208c2ecf20Sopenharmony_ci	chan->requests++;
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	if (!hsotg->params.host_dma &&
14238c2ecf20Sopenharmony_ci	    !chan->ep_is_in && chan->xfer_len > 0)
14248c2ecf20Sopenharmony_ci		/* Load OUT packet into the appropriate Tx FIFO */
14258c2ecf20Sopenharmony_ci		dwc2_hc_write_packet(hsotg, chan);
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci/**
14298c2ecf20Sopenharmony_ci * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
14308c2ecf20Sopenharmony_ci * host channel and starts the transfer in Descriptor DMA mode
14318c2ecf20Sopenharmony_ci *
14328c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
14338c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel
14348c2ecf20Sopenharmony_ci *
14358c2ecf20Sopenharmony_ci * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
14368c2ecf20Sopenharmony_ci * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
14378c2ecf20Sopenharmony_ci * with micro-frame bitmap.
14388c2ecf20Sopenharmony_ci *
14398c2ecf20Sopenharmony_ci * Initializes HCDMA register with descriptor list address and CTD value then
14408c2ecf20Sopenharmony_ci * starts the transfer via enabling the channel.
14418c2ecf20Sopenharmony_ci */
14428c2ecf20Sopenharmony_civoid dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
14438c2ecf20Sopenharmony_ci				 struct dwc2_host_chan *chan)
14448c2ecf20Sopenharmony_ci{
14458c2ecf20Sopenharmony_ci	u32 hcchar;
14468c2ecf20Sopenharmony_ci	u32 hctsiz = 0;
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	if (chan->do_ping)
14498c2ecf20Sopenharmony_ci		hctsiz |= TSIZ_DOPNG;
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
14528c2ecf20Sopenharmony_ci		dwc2_set_pid_isoc(chan);
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
14558c2ecf20Sopenharmony_ci	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
14568c2ecf20Sopenharmony_ci		  TSIZ_SC_MC_PID_MASK;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
14598c2ecf20Sopenharmony_ci	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	/* Non-zero only for high-speed interrupt endpoints */
14628c2ecf20Sopenharmony_ci	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	if (dbg_hc(chan)) {
14658c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
14668c2ecf20Sopenharmony_ci			 chan->hc_num);
14678c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
14688c2ecf20Sopenharmony_ci			 chan->data_pid_start);
14698c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
14758c2ecf20Sopenharmony_ci				   chan->desc_list_sz, DMA_TO_DEVICE);
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
14808c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
14818c2ecf20Sopenharmony_ci			 &chan->desc_list_addr, chan->hc_num);
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
14848c2ecf20Sopenharmony_ci	hcchar &= ~HCCHAR_MULTICNT_MASK;
14858c2ecf20Sopenharmony_ci	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
14868c2ecf20Sopenharmony_ci		  HCCHAR_MULTICNT_MASK;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	if (hcchar & HCCHAR_CHDIS)
14898c2ecf20Sopenharmony_ci		dev_warn(hsotg->dev,
14908c2ecf20Sopenharmony_ci			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
14918c2ecf20Sopenharmony_ci			 __func__, chan->hc_num, hcchar);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	/* Set host channel enable after all other setup is complete */
14948c2ecf20Sopenharmony_ci	hcchar |= HCCHAR_CHENA;
14958c2ecf20Sopenharmony_ci	hcchar &= ~HCCHAR_CHDIS;
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
14988c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
14998c2ecf20Sopenharmony_ci			 (hcchar & HCCHAR_MULTICNT_MASK) >>
15008c2ecf20Sopenharmony_ci			 HCCHAR_MULTICNT_SHIFT);
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
15038c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
15048c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
15058c2ecf20Sopenharmony_ci			 chan->hc_num);
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	chan->xfer_started = 1;
15088c2ecf20Sopenharmony_ci	chan->requests++;
15098c2ecf20Sopenharmony_ci}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci/**
15128c2ecf20Sopenharmony_ci * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
15138c2ecf20Sopenharmony_ci * a previous call to dwc2_hc_start_transfer()
15148c2ecf20Sopenharmony_ci *
15158c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
15168c2ecf20Sopenharmony_ci * @chan:  Information needed to initialize the host channel
15178c2ecf20Sopenharmony_ci *
15188c2ecf20Sopenharmony_ci * The caller must ensure there is sufficient space in the request queue and Tx
15198c2ecf20Sopenharmony_ci * Data FIFO. This function should only be called in Slave mode. In DMA mode,
15208c2ecf20Sopenharmony_ci * the controller acts autonomously to complete transfers programmed to a host
15218c2ecf20Sopenharmony_ci * channel.
15228c2ecf20Sopenharmony_ci *
15238c2ecf20Sopenharmony_ci * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
15248c2ecf20Sopenharmony_ci * if there is any data remaining to be queued. For an IN transfer, another
15258c2ecf20Sopenharmony_ci * data packet is always requested. For the SETUP phase of a control transfer,
15268c2ecf20Sopenharmony_ci * this function does nothing.
15278c2ecf20Sopenharmony_ci *
15288c2ecf20Sopenharmony_ci * Return: 1 if a new request is queued, 0 if no more requests are required
15298c2ecf20Sopenharmony_ci * for this transfer
15308c2ecf20Sopenharmony_ci */
15318c2ecf20Sopenharmony_cistatic int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
15328c2ecf20Sopenharmony_ci				     struct dwc2_host_chan *chan)
15338c2ecf20Sopenharmony_ci{
15348c2ecf20Sopenharmony_ci	if (dbg_hc(chan))
15358c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
15368c2ecf20Sopenharmony_ci			 chan->hc_num);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	if (chan->do_split)
15398c2ecf20Sopenharmony_ci		/* SPLITs always queue just once per channel */
15408c2ecf20Sopenharmony_ci		return 0;
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
15438c2ecf20Sopenharmony_ci		/* SETUPs are queued only once since they can't be NAK'd */
15448c2ecf20Sopenharmony_ci		return 0;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	if (chan->ep_is_in) {
15478c2ecf20Sopenharmony_ci		/*
15488c2ecf20Sopenharmony_ci		 * Always queue another request for other IN transfers. If
15498c2ecf20Sopenharmony_ci		 * back-to-back INs are issued and NAKs are received for both,
15508c2ecf20Sopenharmony_ci		 * the driver may still be processing the first NAK when the
15518c2ecf20Sopenharmony_ci		 * second NAK is received. When the interrupt handler clears
15528c2ecf20Sopenharmony_ci		 * the NAK interrupt for the first NAK, the second NAK will
15538c2ecf20Sopenharmony_ci		 * not be seen. So we can't depend on the NAK interrupt
15548c2ecf20Sopenharmony_ci		 * handler to requeue a NAK'd request. Instead, IN requests
15558c2ecf20Sopenharmony_ci		 * are issued each time this function is called. When the
15568c2ecf20Sopenharmony_ci		 * transfer completes, the extra requests for the channel will
15578c2ecf20Sopenharmony_ci		 * be flushed.
15588c2ecf20Sopenharmony_ci		 */
15598c2ecf20Sopenharmony_ci		u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
15628c2ecf20Sopenharmony_ci		hcchar |= HCCHAR_CHENA;
15638c2ecf20Sopenharmony_ci		hcchar &= ~HCCHAR_CHDIS;
15648c2ecf20Sopenharmony_ci		if (dbg_hc(chan))
15658c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
15668c2ecf20Sopenharmony_ci				 hcchar);
15678c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
15688c2ecf20Sopenharmony_ci		chan->requests++;
15698c2ecf20Sopenharmony_ci		return 1;
15708c2ecf20Sopenharmony_ci	}
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	/* OUT transfers */
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	if (chan->xfer_count < chan->xfer_len) {
15758c2ecf20Sopenharmony_ci		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
15768c2ecf20Sopenharmony_ci		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
15778c2ecf20Sopenharmony_ci			u32 hcchar = dwc2_readl(hsotg,
15788c2ecf20Sopenharmony_ci						HCCHAR(chan->hc_num));
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci			dwc2_hc_set_even_odd_frame(hsotg, chan,
15818c2ecf20Sopenharmony_ci						   &hcchar);
15828c2ecf20Sopenharmony_ci		}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci		/* Load OUT packet into the appropriate Tx FIFO */
15858c2ecf20Sopenharmony_ci		dwc2_hc_write_packet(hsotg, chan);
15868c2ecf20Sopenharmony_ci		chan->requests++;
15878c2ecf20Sopenharmony_ci		return 1;
15888c2ecf20Sopenharmony_ci	}
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	return 0;
15918c2ecf20Sopenharmony_ci}
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci/*
15948c2ecf20Sopenharmony_ci * =========================================================================
15958c2ecf20Sopenharmony_ci *  HCD
15968c2ecf20Sopenharmony_ci * =========================================================================
15978c2ecf20Sopenharmony_ci */
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci/*
16008c2ecf20Sopenharmony_ci * Processes all the URBs in a single list of QHs. Completes them with
16018c2ecf20Sopenharmony_ci * -ETIMEDOUT and frees the QTD.
16028c2ecf20Sopenharmony_ci *
16038c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
16048c2ecf20Sopenharmony_ci */
16058c2ecf20Sopenharmony_cistatic void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
16068c2ecf20Sopenharmony_ci				      struct list_head *qh_list)
16078c2ecf20Sopenharmony_ci{
16088c2ecf20Sopenharmony_ci	struct dwc2_qh *qh, *qh_tmp;
16098c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd, *qtd_tmp;
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
16128c2ecf20Sopenharmony_ci		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
16138c2ecf20Sopenharmony_ci					 qtd_list_entry) {
16148c2ecf20Sopenharmony_ci			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
16158c2ecf20Sopenharmony_ci			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
16168c2ecf20Sopenharmony_ci		}
16178c2ecf20Sopenharmony_ci	}
16188c2ecf20Sopenharmony_ci}
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_cistatic void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
16218c2ecf20Sopenharmony_ci			      struct list_head *qh_list)
16228c2ecf20Sopenharmony_ci{
16238c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd, *qtd_tmp;
16248c2ecf20Sopenharmony_ci	struct dwc2_qh *qh, *qh_tmp;
16258c2ecf20Sopenharmony_ci	unsigned long flags;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	if (!qh_list->next)
16288c2ecf20Sopenharmony_ci		/* The list hasn't been initialized yet */
16298c2ecf20Sopenharmony_ci		return;
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	/* Ensure there are no QTDs or URBs left */
16348c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
16378c2ecf20Sopenharmony_ci		dwc2_hcd_qh_unlink(hsotg, qh);
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci		/* Free each QTD in the QH's QTD list */
16408c2ecf20Sopenharmony_ci		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
16418c2ecf20Sopenharmony_ci					 qtd_list_entry)
16428c2ecf20Sopenharmony_ci			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci		if (qh->channel && qh->channel->qh == qh)
16458c2ecf20Sopenharmony_ci			qh->channel->qh = NULL;
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
16488c2ecf20Sopenharmony_ci		dwc2_hcd_qh_free(hsotg, qh);
16498c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
16508c2ecf20Sopenharmony_ci	}
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
16538c2ecf20Sopenharmony_ci}
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci/*
16568c2ecf20Sopenharmony_ci * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
16578c2ecf20Sopenharmony_ci * and periodic schedules. The QTD associated with each URB is removed from
16588c2ecf20Sopenharmony_ci * the schedule and freed. This function may be called when a disconnect is
16598c2ecf20Sopenharmony_ci * detected or when the HCD is being stopped.
16608c2ecf20Sopenharmony_ci *
16618c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
16628c2ecf20Sopenharmony_ci */
16638c2ecf20Sopenharmony_cistatic void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
16648c2ecf20Sopenharmony_ci{
16658c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
16668c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
16678c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
16688c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
16698c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
16708c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
16718c2ecf20Sopenharmony_ci	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
16728c2ecf20Sopenharmony_ci}
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci/**
16758c2ecf20Sopenharmony_ci * dwc2_hcd_start() - Starts the HCD when switching to Host mode
16768c2ecf20Sopenharmony_ci *
16778c2ecf20Sopenharmony_ci * @hsotg: Pointer to struct dwc2_hsotg
16788c2ecf20Sopenharmony_ci */
16798c2ecf20Sopenharmony_civoid dwc2_hcd_start(struct dwc2_hsotg *hsotg)
16808c2ecf20Sopenharmony_ci{
16818c2ecf20Sopenharmony_ci	u32 hprt0;
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	if (hsotg->op_state == OTG_STATE_B_HOST) {
16848c2ecf20Sopenharmony_ci		/*
16858c2ecf20Sopenharmony_ci		 * Reset the port. During a HNP mode switch the reset
16868c2ecf20Sopenharmony_ci		 * needs to occur within 1ms and have a duration of at
16878c2ecf20Sopenharmony_ci		 * least 50ms.
16888c2ecf20Sopenharmony_ci		 */
16898c2ecf20Sopenharmony_ci		hprt0 = dwc2_read_hprt0(hsotg);
16908c2ecf20Sopenharmony_ci		hprt0 |= HPRT0_RST;
16918c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hprt0, HPRT0);
16928c2ecf20Sopenharmony_ci	}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
16958c2ecf20Sopenharmony_ci			   msecs_to_jiffies(50));
16968c2ecf20Sopenharmony_ci}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci/* Must be called with interrupt disabled and spinlock held */
16998c2ecf20Sopenharmony_cistatic void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	int num_channels = hsotg->params.host_channels;
17028c2ecf20Sopenharmony_ci	struct dwc2_host_chan *channel;
17038c2ecf20Sopenharmony_ci	u32 hcchar;
17048c2ecf20Sopenharmony_ci	int i;
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	if (!hsotg->params.host_dma) {
17078c2ecf20Sopenharmony_ci		/* Flush out any channel requests in slave mode */
17088c2ecf20Sopenharmony_ci		for (i = 0; i < num_channels; i++) {
17098c2ecf20Sopenharmony_ci			channel = hsotg->hc_ptr_array[i];
17108c2ecf20Sopenharmony_ci			if (!list_empty(&channel->hc_list_entry))
17118c2ecf20Sopenharmony_ci				continue;
17128c2ecf20Sopenharmony_ci			hcchar = dwc2_readl(hsotg, HCCHAR(i));
17138c2ecf20Sopenharmony_ci			if (hcchar & HCCHAR_CHENA) {
17148c2ecf20Sopenharmony_ci				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
17158c2ecf20Sopenharmony_ci				hcchar |= HCCHAR_CHDIS;
17168c2ecf20Sopenharmony_ci				dwc2_writel(hsotg, hcchar, HCCHAR(i));
17178c2ecf20Sopenharmony_ci			}
17188c2ecf20Sopenharmony_ci		}
17198c2ecf20Sopenharmony_ci	}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	for (i = 0; i < num_channels; i++) {
17228c2ecf20Sopenharmony_ci		channel = hsotg->hc_ptr_array[i];
17238c2ecf20Sopenharmony_ci		if (!list_empty(&channel->hc_list_entry))
17248c2ecf20Sopenharmony_ci			continue;
17258c2ecf20Sopenharmony_ci		hcchar = dwc2_readl(hsotg, HCCHAR(i));
17268c2ecf20Sopenharmony_ci		if (hcchar & HCCHAR_CHENA) {
17278c2ecf20Sopenharmony_ci			/* Halt the channel */
17288c2ecf20Sopenharmony_ci			hcchar |= HCCHAR_CHDIS;
17298c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hcchar, HCCHAR(i));
17308c2ecf20Sopenharmony_ci		}
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci		dwc2_hc_cleanup(hsotg, channel);
17338c2ecf20Sopenharmony_ci		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
17348c2ecf20Sopenharmony_ci		/*
17358c2ecf20Sopenharmony_ci		 * Added for Descriptor DMA to prevent channel double cleanup in
17368c2ecf20Sopenharmony_ci		 * release_channel_ddma(), which is called from ep_disable when
17378c2ecf20Sopenharmony_ci		 * device disconnects
17388c2ecf20Sopenharmony_ci		 */
17398c2ecf20Sopenharmony_ci		channel->qh = NULL;
17408c2ecf20Sopenharmony_ci	}
17418c2ecf20Sopenharmony_ci	/* All channels have been freed, mark them available */
17428c2ecf20Sopenharmony_ci	if (hsotg->params.uframe_sched) {
17438c2ecf20Sopenharmony_ci		hsotg->available_host_channels =
17448c2ecf20Sopenharmony_ci			hsotg->params.host_channels;
17458c2ecf20Sopenharmony_ci	} else {
17468c2ecf20Sopenharmony_ci		hsotg->non_periodic_channels = 0;
17478c2ecf20Sopenharmony_ci		hsotg->periodic_channels = 0;
17488c2ecf20Sopenharmony_ci	}
17498c2ecf20Sopenharmony_ci}
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci/**
17528c2ecf20Sopenharmony_ci * dwc2_hcd_connect() - Handles connect of the HCD
17538c2ecf20Sopenharmony_ci *
17548c2ecf20Sopenharmony_ci * @hsotg: Pointer to struct dwc2_hsotg
17558c2ecf20Sopenharmony_ci *
17568c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
17578c2ecf20Sopenharmony_ci */
17588c2ecf20Sopenharmony_civoid dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
17598c2ecf20Sopenharmony_ci{
17608c2ecf20Sopenharmony_ci	if (hsotg->lx_state != DWC2_L0)
17618c2ecf20Sopenharmony_ci		usb_hcd_resume_root_hub(hsotg->priv);
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	hsotg->flags.b.port_connect_status_change = 1;
17648c2ecf20Sopenharmony_ci	hsotg->flags.b.port_connect_status = 1;
17658c2ecf20Sopenharmony_ci}
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci/**
17688c2ecf20Sopenharmony_ci * dwc2_hcd_disconnect() - Handles disconnect of the HCD
17698c2ecf20Sopenharmony_ci *
17708c2ecf20Sopenharmony_ci * @hsotg: Pointer to struct dwc2_hsotg
17718c2ecf20Sopenharmony_ci * @force: If true, we won't try to reconnect even if we see device connected.
17728c2ecf20Sopenharmony_ci *
17738c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
17748c2ecf20Sopenharmony_ci */
17758c2ecf20Sopenharmony_civoid dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
17768c2ecf20Sopenharmony_ci{
17778c2ecf20Sopenharmony_ci	u32 intr;
17788c2ecf20Sopenharmony_ci	u32 hprt0;
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	/* Set status flags for the hub driver */
17818c2ecf20Sopenharmony_ci	hsotg->flags.b.port_connect_status_change = 1;
17828c2ecf20Sopenharmony_ci	hsotg->flags.b.port_connect_status = 0;
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci	/*
17858c2ecf20Sopenharmony_ci	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
17868c2ecf20Sopenharmony_ci	 * interrupt mask and status bits and disabling subsequent host
17878c2ecf20Sopenharmony_ci	 * channel interrupts.
17888c2ecf20Sopenharmony_ci	 */
17898c2ecf20Sopenharmony_ci	intr = dwc2_readl(hsotg, GINTMSK);
17908c2ecf20Sopenharmony_ci	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
17918c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intr, GINTMSK);
17928c2ecf20Sopenharmony_ci	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
17938c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, intr, GINTSTS);
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_ci	/*
17968c2ecf20Sopenharmony_ci	 * Turn off the vbus power only if the core has transitioned to device
17978c2ecf20Sopenharmony_ci	 * mode. If still in host mode, need to keep power on to detect a
17988c2ecf20Sopenharmony_ci	 * reconnection.
17998c2ecf20Sopenharmony_ci	 */
18008c2ecf20Sopenharmony_ci	if (dwc2_is_device_mode(hsotg)) {
18018c2ecf20Sopenharmony_ci		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
18028c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
18038c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, 0, HPRT0);
18048c2ecf20Sopenharmony_ci		}
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci		dwc2_disable_host_interrupts(hsotg);
18078c2ecf20Sopenharmony_ci	}
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	/* Respond with an error status to all URBs in the schedule */
18108c2ecf20Sopenharmony_ci	dwc2_kill_all_urbs(hsotg);
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	if (dwc2_is_host_mode(hsotg))
18138c2ecf20Sopenharmony_ci		/* Clean up any host channels that were in use */
18148c2ecf20Sopenharmony_ci		dwc2_hcd_cleanup_channels(hsotg);
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	dwc2_host_disconnect(hsotg);
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci	/*
18198c2ecf20Sopenharmony_ci	 * Add an extra check here to see if we're actually connected but
18208c2ecf20Sopenharmony_ci	 * we don't have a detection interrupt pending.  This can happen if:
18218c2ecf20Sopenharmony_ci	 *   1. hardware sees connect
18228c2ecf20Sopenharmony_ci	 *   2. hardware sees disconnect
18238c2ecf20Sopenharmony_ci	 *   3. hardware sees connect
18248c2ecf20Sopenharmony_ci	 *   4. dwc2_port_intr() - clears connect interrupt
18258c2ecf20Sopenharmony_ci	 *   5. dwc2_handle_common_intr() - calls here
18268c2ecf20Sopenharmony_ci	 *
18278c2ecf20Sopenharmony_ci	 * Without the extra check here we will end calling disconnect
18288c2ecf20Sopenharmony_ci	 * and won't get any future interrupts to handle the connect.
18298c2ecf20Sopenharmony_ci	 */
18308c2ecf20Sopenharmony_ci	if (!force) {
18318c2ecf20Sopenharmony_ci		hprt0 = dwc2_readl(hsotg, HPRT0);
18328c2ecf20Sopenharmony_ci		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
18338c2ecf20Sopenharmony_ci			dwc2_hcd_connect(hsotg);
18348c2ecf20Sopenharmony_ci	}
18358c2ecf20Sopenharmony_ci}
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci/**
18388c2ecf20Sopenharmony_ci * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
18398c2ecf20Sopenharmony_ci *
18408c2ecf20Sopenharmony_ci * @hsotg: Pointer to struct dwc2_hsotg
18418c2ecf20Sopenharmony_ci */
18428c2ecf20Sopenharmony_cistatic void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
18438c2ecf20Sopenharmony_ci{
18448c2ecf20Sopenharmony_ci	if (hsotg->bus_suspended) {
18458c2ecf20Sopenharmony_ci		hsotg->flags.b.port_suspend_change = 1;
18468c2ecf20Sopenharmony_ci		usb_hcd_resume_root_hub(hsotg->priv);
18478c2ecf20Sopenharmony_ci	}
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	if (hsotg->lx_state == DWC2_L1)
18508c2ecf20Sopenharmony_ci		hsotg->flags.b.port_l1_change = 1;
18518c2ecf20Sopenharmony_ci}
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci/**
18548c2ecf20Sopenharmony_ci * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
18558c2ecf20Sopenharmony_ci *
18568c2ecf20Sopenharmony_ci * @hsotg: Pointer to struct dwc2_hsotg
18578c2ecf20Sopenharmony_ci *
18588c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
18598c2ecf20Sopenharmony_ci */
18608c2ecf20Sopenharmony_civoid dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
18618c2ecf20Sopenharmony_ci{
18628c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
18638c2ecf20Sopenharmony_ci
18648c2ecf20Sopenharmony_ci	/*
18658c2ecf20Sopenharmony_ci	 * The root hub should be disconnected before this function is called.
18668c2ecf20Sopenharmony_ci	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
18678c2ecf20Sopenharmony_ci	 * and the QH lists (via ..._hcd_endpoint_disable).
18688c2ecf20Sopenharmony_ci	 */
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	/* Turn off all host-specific interrupts */
18718c2ecf20Sopenharmony_ci	dwc2_disable_host_interrupts(hsotg);
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	/* Turn off the vbus power */
18748c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "PortPower off\n");
18758c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, HPRT0);
18768c2ecf20Sopenharmony_ci}
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci/* Caller must hold driver lock */
18798c2ecf20Sopenharmony_cistatic int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
18808c2ecf20Sopenharmony_ci				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
18818c2ecf20Sopenharmony_ci				struct dwc2_qtd *qtd)
18828c2ecf20Sopenharmony_ci{
18838c2ecf20Sopenharmony_ci	u32 intr_mask;
18848c2ecf20Sopenharmony_ci	int retval;
18858c2ecf20Sopenharmony_ci	int dev_speed;
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci	if (!hsotg->flags.b.port_connect_status) {
18888c2ecf20Sopenharmony_ci		/* No longer connected */
18898c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "Not connected\n");
18908c2ecf20Sopenharmony_ci		return -ENODEV;
18918c2ecf20Sopenharmony_ci	}
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ci	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
18948c2ecf20Sopenharmony_ci
18958c2ecf20Sopenharmony_ci	/* Some configurations cannot support LS traffic on a FS root port */
18968c2ecf20Sopenharmony_ci	if ((dev_speed == USB_SPEED_LOW) &&
18978c2ecf20Sopenharmony_ci	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
18988c2ecf20Sopenharmony_ci	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
18998c2ecf20Sopenharmony_ci		u32 hprt0 = dwc2_readl(hsotg, HPRT0);
19008c2ecf20Sopenharmony_ci		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci		if (prtspd == HPRT0_SPD_FULL_SPEED)
19038c2ecf20Sopenharmony_ci			return -ENODEV;
19048c2ecf20Sopenharmony_ci	}
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci	if (!qtd)
19078c2ecf20Sopenharmony_ci		return -EINVAL;
19088c2ecf20Sopenharmony_ci
19098c2ecf20Sopenharmony_ci	dwc2_hcd_qtd_init(qtd, urb);
19108c2ecf20Sopenharmony_ci	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
19118c2ecf20Sopenharmony_ci	if (retval) {
19128c2ecf20Sopenharmony_ci		dev_err(hsotg->dev,
19138c2ecf20Sopenharmony_ci			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
19148c2ecf20Sopenharmony_ci			retval);
19158c2ecf20Sopenharmony_ci		return retval;
19168c2ecf20Sopenharmony_ci	}
19178c2ecf20Sopenharmony_ci
19188c2ecf20Sopenharmony_ci	intr_mask = dwc2_readl(hsotg, GINTMSK);
19198c2ecf20Sopenharmony_ci	if (!(intr_mask & GINTSTS_SOF)) {
19208c2ecf20Sopenharmony_ci		enum dwc2_transaction_type tr_type;
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
19238c2ecf20Sopenharmony_ci		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
19248c2ecf20Sopenharmony_ci			/*
19258c2ecf20Sopenharmony_ci			 * Do not schedule SG transactions until qtd has
19268c2ecf20Sopenharmony_ci			 * URB_GIVEBACK_ASAP set
19278c2ecf20Sopenharmony_ci			 */
19288c2ecf20Sopenharmony_ci			return 0;
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci		tr_type = dwc2_hcd_select_transactions(hsotg);
19318c2ecf20Sopenharmony_ci		if (tr_type != DWC2_TRANSACTION_NONE)
19328c2ecf20Sopenharmony_ci			dwc2_hcd_queue_transactions(hsotg, tr_type);
19338c2ecf20Sopenharmony_ci	}
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci	return 0;
19368c2ecf20Sopenharmony_ci}
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci/* Must be called with interrupt disabled and spinlock held */
19398c2ecf20Sopenharmony_cistatic int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
19408c2ecf20Sopenharmony_ci				struct dwc2_hcd_urb *urb)
19418c2ecf20Sopenharmony_ci{
19428c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
19438c2ecf20Sopenharmony_ci	struct dwc2_qtd *urb_qtd;
19448c2ecf20Sopenharmony_ci
19458c2ecf20Sopenharmony_ci	urb_qtd = urb->qtd;
19468c2ecf20Sopenharmony_ci	if (!urb_qtd) {
19478c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
19488c2ecf20Sopenharmony_ci		return -EINVAL;
19498c2ecf20Sopenharmony_ci	}
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ci	qh = urb_qtd->qh;
19528c2ecf20Sopenharmony_ci	if (!qh) {
19538c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
19548c2ecf20Sopenharmony_ci		return -EINVAL;
19558c2ecf20Sopenharmony_ci	}
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	urb->priv = NULL;
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	if (urb_qtd->in_process && qh->channel) {
19608c2ecf20Sopenharmony_ci		dwc2_dump_channel_info(hsotg, qh->channel);
19618c2ecf20Sopenharmony_ci
19628c2ecf20Sopenharmony_ci		/* The QTD is in process (it has been assigned to a channel) */
19638c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_connect_status)
19648c2ecf20Sopenharmony_ci			/*
19658c2ecf20Sopenharmony_ci			 * If still connected (i.e. in host mode), halt the
19668c2ecf20Sopenharmony_ci			 * channel so it can be used for other transfers. If
19678c2ecf20Sopenharmony_ci			 * no longer connected, the host registers can't be
19688c2ecf20Sopenharmony_ci			 * written to halt the channel since the core is in
19698c2ecf20Sopenharmony_ci			 * device mode.
19708c2ecf20Sopenharmony_ci			 */
19718c2ecf20Sopenharmony_ci			dwc2_hc_halt(hsotg, qh->channel,
19728c2ecf20Sopenharmony_ci				     DWC2_HC_XFER_URB_DEQUEUE);
19738c2ecf20Sopenharmony_ci	}
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci	/*
19768c2ecf20Sopenharmony_ci	 * Free the QTD and clean up the associated QH. Leave the QH in the
19778c2ecf20Sopenharmony_ci	 * schedule if it has any remaining QTDs.
19788c2ecf20Sopenharmony_ci	 */
19798c2ecf20Sopenharmony_ci	if (!hsotg->params.dma_desc_enable) {
19808c2ecf20Sopenharmony_ci		u8 in_process = urb_qtd->in_process;
19818c2ecf20Sopenharmony_ci
19828c2ecf20Sopenharmony_ci		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
19838c2ecf20Sopenharmony_ci		if (in_process) {
19848c2ecf20Sopenharmony_ci			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
19858c2ecf20Sopenharmony_ci			qh->channel = NULL;
19868c2ecf20Sopenharmony_ci		} else if (list_empty(&qh->qtd_list)) {
19878c2ecf20Sopenharmony_ci			dwc2_hcd_qh_unlink(hsotg, qh);
19888c2ecf20Sopenharmony_ci		}
19898c2ecf20Sopenharmony_ci	} else {
19908c2ecf20Sopenharmony_ci		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
19918c2ecf20Sopenharmony_ci	}
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci	return 0;
19948c2ecf20Sopenharmony_ci}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci/* Must NOT be called with interrupt disabled or spinlock held */
19978c2ecf20Sopenharmony_cistatic int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
19988c2ecf20Sopenharmony_ci				     struct usb_host_endpoint *ep, int retry)
19998c2ecf20Sopenharmony_ci{
20008c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd, *qtd_tmp;
20018c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
20028c2ecf20Sopenharmony_ci	unsigned long flags;
20038c2ecf20Sopenharmony_ci	int rc;
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	qh = ep->hcpriv;
20088c2ecf20Sopenharmony_ci	if (!qh) {
20098c2ecf20Sopenharmony_ci		rc = -EINVAL;
20108c2ecf20Sopenharmony_ci		goto err;
20118c2ecf20Sopenharmony_ci	}
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci	while (!list_empty(&qh->qtd_list) && retry--) {
20148c2ecf20Sopenharmony_ci		if (retry == 0) {
20158c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
20168c2ecf20Sopenharmony_ci				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
20178c2ecf20Sopenharmony_ci			rc = -EBUSY;
20188c2ecf20Sopenharmony_ci			goto err;
20198c2ecf20Sopenharmony_ci		}
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
20228c2ecf20Sopenharmony_ci		msleep(20);
20238c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
20248c2ecf20Sopenharmony_ci		qh = ep->hcpriv;
20258c2ecf20Sopenharmony_ci		if (!qh) {
20268c2ecf20Sopenharmony_ci			rc = -EINVAL;
20278c2ecf20Sopenharmony_ci			goto err;
20288c2ecf20Sopenharmony_ci		}
20298c2ecf20Sopenharmony_ci	}
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	dwc2_hcd_qh_unlink(hsotg, qh);
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	/* Free each QTD in the QH's QTD list */
20348c2ecf20Sopenharmony_ci	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
20358c2ecf20Sopenharmony_ci		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci	ep->hcpriv = NULL;
20388c2ecf20Sopenharmony_ci
20398c2ecf20Sopenharmony_ci	if (qh->channel && qh->channel->qh == qh)
20408c2ecf20Sopenharmony_ci		qh->channel->qh = NULL;
20418c2ecf20Sopenharmony_ci
20428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
20438c2ecf20Sopenharmony_ci
20448c2ecf20Sopenharmony_ci	dwc2_hcd_qh_free(hsotg, qh);
20458c2ecf20Sopenharmony_ci
20468c2ecf20Sopenharmony_ci	return 0;
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_cierr:
20498c2ecf20Sopenharmony_ci	ep->hcpriv = NULL;
20508c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	return rc;
20538c2ecf20Sopenharmony_ci}
20548c2ecf20Sopenharmony_ci
20558c2ecf20Sopenharmony_ci/* Must be called with interrupt disabled and spinlock held */
20568c2ecf20Sopenharmony_cistatic int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
20578c2ecf20Sopenharmony_ci				   struct usb_host_endpoint *ep)
20588c2ecf20Sopenharmony_ci{
20598c2ecf20Sopenharmony_ci	struct dwc2_qh *qh = ep->hcpriv;
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci	if (!qh)
20628c2ecf20Sopenharmony_ci		return -EINVAL;
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	qh->data_toggle = DWC2_HC_PID_DATA0;
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_ci	return 0;
20678c2ecf20Sopenharmony_ci}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci/**
20708c2ecf20Sopenharmony_ci * dwc2_core_init() - Initializes the DWC_otg controller registers and
20718c2ecf20Sopenharmony_ci * prepares the core for device mode or host mode operation
20728c2ecf20Sopenharmony_ci *
20738c2ecf20Sopenharmony_ci * @hsotg:         Programming view of the DWC_otg controller
20748c2ecf20Sopenharmony_ci * @initial_setup: If true then this is the first init for this instance.
20758c2ecf20Sopenharmony_ci */
20768c2ecf20Sopenharmony_ciint dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
20778c2ecf20Sopenharmony_ci{
20788c2ecf20Sopenharmony_ci	u32 usbcfg, otgctl;
20798c2ecf20Sopenharmony_ci	int retval;
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci	usbcfg = dwc2_readl(hsotg, GUSBCFG);
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci	/* Set ULPI External VBUS bit if needed */
20868c2ecf20Sopenharmony_ci	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
20878c2ecf20Sopenharmony_ci	if (hsotg->params.phy_ulpi_ext_vbus)
20888c2ecf20Sopenharmony_ci		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci	/* Set external TS Dline pulsing bit if needed */
20918c2ecf20Sopenharmony_ci	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
20928c2ecf20Sopenharmony_ci	if (hsotg->params.ts_dline)
20938c2ecf20Sopenharmony_ci		usbcfg |= GUSBCFG_TERMSELDLPULSE;
20948c2ecf20Sopenharmony_ci
20958c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, usbcfg, GUSBCFG);
20968c2ecf20Sopenharmony_ci
20978c2ecf20Sopenharmony_ci	/*
20988c2ecf20Sopenharmony_ci	 * Reset the Controller
20998c2ecf20Sopenharmony_ci	 *
21008c2ecf20Sopenharmony_ci	 * We only need to reset the controller if this is a re-init.
21018c2ecf20Sopenharmony_ci	 * For the first init we know for sure that earlier code reset us (it
21028c2ecf20Sopenharmony_ci	 * needed to in order to properly detect various parameters).
21038c2ecf20Sopenharmony_ci	 */
21048c2ecf20Sopenharmony_ci	if (!initial_setup) {
21058c2ecf20Sopenharmony_ci		retval = dwc2_core_reset(hsotg, false);
21068c2ecf20Sopenharmony_ci		if (retval) {
21078c2ecf20Sopenharmony_ci			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
21088c2ecf20Sopenharmony_ci				__func__);
21098c2ecf20Sopenharmony_ci			return retval;
21108c2ecf20Sopenharmony_ci		}
21118c2ecf20Sopenharmony_ci	}
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	/*
21148c2ecf20Sopenharmony_ci	 * This needs to happen in FS mode before any other programming occurs
21158c2ecf20Sopenharmony_ci	 */
21168c2ecf20Sopenharmony_ci	retval = dwc2_phy_init(hsotg, initial_setup);
21178c2ecf20Sopenharmony_ci	if (retval)
21188c2ecf20Sopenharmony_ci		return retval;
21198c2ecf20Sopenharmony_ci
21208c2ecf20Sopenharmony_ci	/* Program the GAHBCFG Register */
21218c2ecf20Sopenharmony_ci	retval = dwc2_gahbcfg_init(hsotg);
21228c2ecf20Sopenharmony_ci	if (retval)
21238c2ecf20Sopenharmony_ci		return retval;
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	/* Program the GUSBCFG register */
21268c2ecf20Sopenharmony_ci	dwc2_gusbcfg_init(hsotg);
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci	/* Program the GOTGCTL register */
21298c2ecf20Sopenharmony_ci	otgctl = dwc2_readl(hsotg, GOTGCTL);
21308c2ecf20Sopenharmony_ci	otgctl &= ~GOTGCTL_OTGVER;
21318c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, otgctl, GOTGCTL);
21328c2ecf20Sopenharmony_ci
21338c2ecf20Sopenharmony_ci	/* Clear the SRP success bit for FS-I2c */
21348c2ecf20Sopenharmony_ci	hsotg->srp_success = 0;
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci	/* Enable common interrupts */
21378c2ecf20Sopenharmony_ci	dwc2_enable_common_interrupts(hsotg);
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	/*
21408c2ecf20Sopenharmony_ci	 * Do device or host initialization based on mode during PCD and
21418c2ecf20Sopenharmony_ci	 * HCD initialization
21428c2ecf20Sopenharmony_ci	 */
21438c2ecf20Sopenharmony_ci	if (dwc2_is_host_mode(hsotg)) {
21448c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Host Mode\n");
21458c2ecf20Sopenharmony_ci		hsotg->op_state = OTG_STATE_A_HOST;
21468c2ecf20Sopenharmony_ci	} else {
21478c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Device Mode\n");
21488c2ecf20Sopenharmony_ci		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
21498c2ecf20Sopenharmony_ci	}
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci	return 0;
21528c2ecf20Sopenharmony_ci}
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_ci/**
21558c2ecf20Sopenharmony_ci * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
21568c2ecf20Sopenharmony_ci * Host mode
21578c2ecf20Sopenharmony_ci *
21588c2ecf20Sopenharmony_ci * @hsotg: Programming view of DWC_otg controller
21598c2ecf20Sopenharmony_ci *
21608c2ecf20Sopenharmony_ci * This function flushes the Tx and Rx FIFOs and flushes any entries in the
21618c2ecf20Sopenharmony_ci * request queues. Host channels are reset to ensure that they are ready for
21628c2ecf20Sopenharmony_ci * performing transfers.
21638c2ecf20Sopenharmony_ci */
21648c2ecf20Sopenharmony_cistatic void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
21658c2ecf20Sopenharmony_ci{
21668c2ecf20Sopenharmony_ci	u32 hcfg, hfir, otgctl, usbcfg;
21678c2ecf20Sopenharmony_ci
21688c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	/* Set HS/FS Timeout Calibration to 7 (max available value).
21718c2ecf20Sopenharmony_ci	 * The number of PHY clocks that the application programs in
21728c2ecf20Sopenharmony_ci	 * this field is added to the high/full speed interpacket timeout
21738c2ecf20Sopenharmony_ci	 * duration in the core to account for any additional delays
21748c2ecf20Sopenharmony_ci	 * introduced by the PHY. This can be required, because the delay
21758c2ecf20Sopenharmony_ci	 * introduced by the PHY in generating the linestate condition
21768c2ecf20Sopenharmony_ci	 * can vary from one PHY to another.
21778c2ecf20Sopenharmony_ci	 */
21788c2ecf20Sopenharmony_ci	usbcfg = dwc2_readl(hsotg, GUSBCFG);
21798c2ecf20Sopenharmony_ci	usbcfg |= GUSBCFG_TOUTCAL(7);
21808c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, usbcfg, GUSBCFG);
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	/* Restart the Phy Clock */
21838c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, PCGCTL);
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	/* Initialize Host Configuration Register */
21868c2ecf20Sopenharmony_ci	dwc2_init_fs_ls_pclk_sel(hsotg);
21878c2ecf20Sopenharmony_ci	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
21888c2ecf20Sopenharmony_ci	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
21898c2ecf20Sopenharmony_ci		hcfg = dwc2_readl(hsotg, HCFG);
21908c2ecf20Sopenharmony_ci		hcfg |= HCFG_FSLSSUPP;
21918c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hcfg, HCFG);
21928c2ecf20Sopenharmony_ci	}
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci	/*
21958c2ecf20Sopenharmony_ci	 * This bit allows dynamic reloading of the HFIR register during
21968c2ecf20Sopenharmony_ci	 * runtime. This bit needs to be programmed during initial configuration
21978c2ecf20Sopenharmony_ci	 * and its value must not be changed during runtime.
21988c2ecf20Sopenharmony_ci	 */
21998c2ecf20Sopenharmony_ci	if (hsotg->params.reload_ctl) {
22008c2ecf20Sopenharmony_ci		hfir = dwc2_readl(hsotg, HFIR);
22018c2ecf20Sopenharmony_ci		hfir |= HFIR_RLDCTRL;
22028c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hfir, HFIR);
22038c2ecf20Sopenharmony_ci	}
22048c2ecf20Sopenharmony_ci
22058c2ecf20Sopenharmony_ci	if (hsotg->params.dma_desc_enable) {
22068c2ecf20Sopenharmony_ci		u32 op_mode = hsotg->hw_params.op_mode;
22078c2ecf20Sopenharmony_ci
22088c2ecf20Sopenharmony_ci		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
22098c2ecf20Sopenharmony_ci		    !hsotg->hw_params.dma_desc_enable ||
22108c2ecf20Sopenharmony_ci		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
22118c2ecf20Sopenharmony_ci		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
22128c2ecf20Sopenharmony_ci		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
22138c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
22148c2ecf20Sopenharmony_ci				"Hardware does not support descriptor DMA mode -\n");
22158c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
22168c2ecf20Sopenharmony_ci				"falling back to buffer DMA mode.\n");
22178c2ecf20Sopenharmony_ci			hsotg->params.dma_desc_enable = false;
22188c2ecf20Sopenharmony_ci		} else {
22198c2ecf20Sopenharmony_ci			hcfg = dwc2_readl(hsotg, HCFG);
22208c2ecf20Sopenharmony_ci			hcfg |= HCFG_DESCDMA;
22218c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hcfg, HCFG);
22228c2ecf20Sopenharmony_ci		}
22238c2ecf20Sopenharmony_ci	}
22248c2ecf20Sopenharmony_ci
22258c2ecf20Sopenharmony_ci	/* Configure data FIFO sizes */
22268c2ecf20Sopenharmony_ci	dwc2_config_fifos(hsotg);
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci	/* TODO - check this */
22298c2ecf20Sopenharmony_ci	/* Clear Host Set HNP Enable in the OTG Control Register */
22308c2ecf20Sopenharmony_ci	otgctl = dwc2_readl(hsotg, GOTGCTL);
22318c2ecf20Sopenharmony_ci	otgctl &= ~GOTGCTL_HSTSETHNPEN;
22328c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, otgctl, GOTGCTL);
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	/* Make sure the FIFOs are flushed */
22358c2ecf20Sopenharmony_ci	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
22368c2ecf20Sopenharmony_ci	dwc2_flush_rx_fifo(hsotg);
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	/* Clear Host Set HNP Enable in the OTG Control Register */
22398c2ecf20Sopenharmony_ci	otgctl = dwc2_readl(hsotg, GOTGCTL);
22408c2ecf20Sopenharmony_ci	otgctl &= ~GOTGCTL_HSTSETHNPEN;
22418c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, otgctl, GOTGCTL);
22428c2ecf20Sopenharmony_ci
22438c2ecf20Sopenharmony_ci	if (!hsotg->params.dma_desc_enable) {
22448c2ecf20Sopenharmony_ci		int num_channels, i;
22458c2ecf20Sopenharmony_ci		u32 hcchar;
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci		/* Flush out any leftover queued requests */
22488c2ecf20Sopenharmony_ci		num_channels = hsotg->params.host_channels;
22498c2ecf20Sopenharmony_ci		for (i = 0; i < num_channels; i++) {
22508c2ecf20Sopenharmony_ci			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22518c2ecf20Sopenharmony_ci			if (hcchar & HCCHAR_CHENA) {
22528c2ecf20Sopenharmony_ci				hcchar &= ~HCCHAR_CHENA;
22538c2ecf20Sopenharmony_ci				hcchar |= HCCHAR_CHDIS;
22548c2ecf20Sopenharmony_ci				hcchar &= ~HCCHAR_EPDIR;
22558c2ecf20Sopenharmony_ci				dwc2_writel(hsotg, hcchar, HCCHAR(i));
22568c2ecf20Sopenharmony_ci			}
22578c2ecf20Sopenharmony_ci		}
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci		/* Halt all channels to put them into a known state */
22608c2ecf20Sopenharmony_ci		for (i = 0; i < num_channels; i++) {
22618c2ecf20Sopenharmony_ci			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22628c2ecf20Sopenharmony_ci			if (hcchar & HCCHAR_CHENA) {
22638c2ecf20Sopenharmony_ci				hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
22648c2ecf20Sopenharmony_ci				hcchar &= ~HCCHAR_EPDIR;
22658c2ecf20Sopenharmony_ci				dwc2_writel(hsotg, hcchar, HCCHAR(i));
22668c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
22678c2ecf20Sopenharmony_ci					__func__, i);
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci				if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
22708c2ecf20Sopenharmony_ci							      HCCHAR_CHENA,
22718c2ecf20Sopenharmony_ci							      1000)) {
22728c2ecf20Sopenharmony_ci					dev_warn(hsotg->dev,
22738c2ecf20Sopenharmony_ci						 "Unable to clear enable on channel %d\n",
22748c2ecf20Sopenharmony_ci						 i);
22758c2ecf20Sopenharmony_ci				}
22768c2ecf20Sopenharmony_ci			}
22778c2ecf20Sopenharmony_ci		}
22788c2ecf20Sopenharmony_ci	}
22798c2ecf20Sopenharmony_ci
22808c2ecf20Sopenharmony_ci	/* Enable ACG feature in host mode, if supported */
22818c2ecf20Sopenharmony_ci	dwc2_enable_acg(hsotg);
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	/* Turn on the vbus power */
22848c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
22858c2ecf20Sopenharmony_ci	if (hsotg->op_state == OTG_STATE_A_HOST) {
22868c2ecf20Sopenharmony_ci		u32 hprt0 = dwc2_read_hprt0(hsotg);
22878c2ecf20Sopenharmony_ci
22888c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
22898c2ecf20Sopenharmony_ci			!!(hprt0 & HPRT0_PWR));
22908c2ecf20Sopenharmony_ci		if (!(hprt0 & HPRT0_PWR)) {
22918c2ecf20Sopenharmony_ci			hprt0 |= HPRT0_PWR;
22928c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
22938c2ecf20Sopenharmony_ci		}
22948c2ecf20Sopenharmony_ci	}
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	dwc2_enable_host_interrupts(hsotg);
22978c2ecf20Sopenharmony_ci}
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci/*
23008c2ecf20Sopenharmony_ci * Initializes dynamic portions of the DWC_otg HCD state
23018c2ecf20Sopenharmony_ci *
23028c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
23038c2ecf20Sopenharmony_ci */
23048c2ecf20Sopenharmony_cistatic void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
23058c2ecf20Sopenharmony_ci{
23068c2ecf20Sopenharmony_ci	struct dwc2_host_chan *chan, *chan_tmp;
23078c2ecf20Sopenharmony_ci	int num_channels;
23088c2ecf20Sopenharmony_ci	int i;
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_ci	hsotg->flags.d32 = 0;
23118c2ecf20Sopenharmony_ci	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
23128c2ecf20Sopenharmony_ci
23138c2ecf20Sopenharmony_ci	if (hsotg->params.uframe_sched) {
23148c2ecf20Sopenharmony_ci		hsotg->available_host_channels =
23158c2ecf20Sopenharmony_ci			hsotg->params.host_channels;
23168c2ecf20Sopenharmony_ci	} else {
23178c2ecf20Sopenharmony_ci		hsotg->non_periodic_channels = 0;
23188c2ecf20Sopenharmony_ci		hsotg->periodic_channels = 0;
23198c2ecf20Sopenharmony_ci	}
23208c2ecf20Sopenharmony_ci
23218c2ecf20Sopenharmony_ci	/*
23228c2ecf20Sopenharmony_ci	 * Put all channels in the free channel list and clean up channel
23238c2ecf20Sopenharmony_ci	 * states
23248c2ecf20Sopenharmony_ci	 */
23258c2ecf20Sopenharmony_ci	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
23268c2ecf20Sopenharmony_ci				 hc_list_entry)
23278c2ecf20Sopenharmony_ci		list_del_init(&chan->hc_list_entry);
23288c2ecf20Sopenharmony_ci
23298c2ecf20Sopenharmony_ci	num_channels = hsotg->params.host_channels;
23308c2ecf20Sopenharmony_ci	for (i = 0; i < num_channels; i++) {
23318c2ecf20Sopenharmony_ci		chan = hsotg->hc_ptr_array[i];
23328c2ecf20Sopenharmony_ci		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
23338c2ecf20Sopenharmony_ci		dwc2_hc_cleanup(hsotg, chan);
23348c2ecf20Sopenharmony_ci	}
23358c2ecf20Sopenharmony_ci
23368c2ecf20Sopenharmony_ci	/* Initialize the DWC core for host mode operation */
23378c2ecf20Sopenharmony_ci	dwc2_core_host_init(hsotg);
23388c2ecf20Sopenharmony_ci}
23398c2ecf20Sopenharmony_ci
23408c2ecf20Sopenharmony_cistatic void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
23418c2ecf20Sopenharmony_ci			       struct dwc2_host_chan *chan,
23428c2ecf20Sopenharmony_ci			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
23438c2ecf20Sopenharmony_ci{
23448c2ecf20Sopenharmony_ci	int hub_addr, hub_port;
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci	chan->do_split = 1;
23478c2ecf20Sopenharmony_ci	chan->xact_pos = qtd->isoc_split_pos;
23488c2ecf20Sopenharmony_ci	chan->complete_split = qtd->complete_split;
23498c2ecf20Sopenharmony_ci	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
23508c2ecf20Sopenharmony_ci	chan->hub_addr = (u8)hub_addr;
23518c2ecf20Sopenharmony_ci	chan->hub_port = (u8)hub_port;
23528c2ecf20Sopenharmony_ci}
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_cistatic void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
23558c2ecf20Sopenharmony_ci			      struct dwc2_host_chan *chan,
23568c2ecf20Sopenharmony_ci			      struct dwc2_qtd *qtd)
23578c2ecf20Sopenharmony_ci{
23588c2ecf20Sopenharmony_ci	struct dwc2_hcd_urb *urb = qtd->urb;
23598c2ecf20Sopenharmony_ci	struct dwc2_hcd_iso_packet_desc *frame_desc;
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
23628c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_CONTROL:
23638c2ecf20Sopenharmony_ci		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci		switch (qtd->control_phase) {
23668c2ecf20Sopenharmony_ci		case DWC2_CONTROL_SETUP:
23678c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
23688c2ecf20Sopenharmony_ci			chan->do_ping = 0;
23698c2ecf20Sopenharmony_ci			chan->ep_is_in = 0;
23708c2ecf20Sopenharmony_ci			chan->data_pid_start = DWC2_HC_PID_SETUP;
23718c2ecf20Sopenharmony_ci			if (hsotg->params.host_dma)
23728c2ecf20Sopenharmony_ci				chan->xfer_dma = urb->setup_dma;
23738c2ecf20Sopenharmony_ci			else
23748c2ecf20Sopenharmony_ci				chan->xfer_buf = urb->setup_packet;
23758c2ecf20Sopenharmony_ci			chan->xfer_len = 8;
23768c2ecf20Sopenharmony_ci			break;
23778c2ecf20Sopenharmony_ci
23788c2ecf20Sopenharmony_ci		case DWC2_CONTROL_DATA:
23798c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "  Control data transaction\n");
23808c2ecf20Sopenharmony_ci			chan->data_pid_start = qtd->data_toggle;
23818c2ecf20Sopenharmony_ci			break;
23828c2ecf20Sopenharmony_ci
23838c2ecf20Sopenharmony_ci		case DWC2_CONTROL_STATUS:
23848c2ecf20Sopenharmony_ci			/*
23858c2ecf20Sopenharmony_ci			 * Direction is opposite of data direction or IN if no
23868c2ecf20Sopenharmony_ci			 * data
23878c2ecf20Sopenharmony_ci			 */
23888c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "  Control status transaction\n");
23898c2ecf20Sopenharmony_ci			if (urb->length == 0)
23908c2ecf20Sopenharmony_ci				chan->ep_is_in = 1;
23918c2ecf20Sopenharmony_ci			else
23928c2ecf20Sopenharmony_ci				chan->ep_is_in =
23938c2ecf20Sopenharmony_ci					dwc2_hcd_is_pipe_out(&urb->pipe_info);
23948c2ecf20Sopenharmony_ci			if (chan->ep_is_in)
23958c2ecf20Sopenharmony_ci				chan->do_ping = 0;
23968c2ecf20Sopenharmony_ci			chan->data_pid_start = DWC2_HC_PID_DATA1;
23978c2ecf20Sopenharmony_ci			chan->xfer_len = 0;
23988c2ecf20Sopenharmony_ci			if (hsotg->params.host_dma)
23998c2ecf20Sopenharmony_ci				chan->xfer_dma = hsotg->status_buf_dma;
24008c2ecf20Sopenharmony_ci			else
24018c2ecf20Sopenharmony_ci				chan->xfer_buf = hsotg->status_buf;
24028c2ecf20Sopenharmony_ci			break;
24038c2ecf20Sopenharmony_ci		}
24048c2ecf20Sopenharmony_ci		break;
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_BULK:
24078c2ecf20Sopenharmony_ci		chan->ep_type = USB_ENDPOINT_XFER_BULK;
24088c2ecf20Sopenharmony_ci		break;
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_INT:
24118c2ecf20Sopenharmony_ci		chan->ep_type = USB_ENDPOINT_XFER_INT;
24128c2ecf20Sopenharmony_ci		break;
24138c2ecf20Sopenharmony_ci
24148c2ecf20Sopenharmony_ci	case USB_ENDPOINT_XFER_ISOC:
24158c2ecf20Sopenharmony_ci		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
24168c2ecf20Sopenharmony_ci		if (hsotg->params.dma_desc_enable)
24178c2ecf20Sopenharmony_ci			break;
24188c2ecf20Sopenharmony_ci
24198c2ecf20Sopenharmony_ci		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
24208c2ecf20Sopenharmony_ci		frame_desc->status = 0;
24218c2ecf20Sopenharmony_ci
24228c2ecf20Sopenharmony_ci		if (hsotg->params.host_dma) {
24238c2ecf20Sopenharmony_ci			chan->xfer_dma = urb->dma;
24248c2ecf20Sopenharmony_ci			chan->xfer_dma += frame_desc->offset +
24258c2ecf20Sopenharmony_ci					qtd->isoc_split_offset;
24268c2ecf20Sopenharmony_ci		} else {
24278c2ecf20Sopenharmony_ci			chan->xfer_buf = urb->buf;
24288c2ecf20Sopenharmony_ci			chan->xfer_buf += frame_desc->offset +
24298c2ecf20Sopenharmony_ci					qtd->isoc_split_offset;
24308c2ecf20Sopenharmony_ci		}
24318c2ecf20Sopenharmony_ci
24328c2ecf20Sopenharmony_ci		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
24338c2ecf20Sopenharmony_ci
24348c2ecf20Sopenharmony_ci		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
24358c2ecf20Sopenharmony_ci			if (chan->xfer_len <= 188)
24368c2ecf20Sopenharmony_ci				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
24378c2ecf20Sopenharmony_ci			else
24388c2ecf20Sopenharmony_ci				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
24398c2ecf20Sopenharmony_ci		}
24408c2ecf20Sopenharmony_ci		break;
24418c2ecf20Sopenharmony_ci	}
24428c2ecf20Sopenharmony_ci}
24438c2ecf20Sopenharmony_ci
24448c2ecf20Sopenharmony_cistatic int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
24458c2ecf20Sopenharmony_ci					    struct dwc2_qh *qh,
24468c2ecf20Sopenharmony_ci					    struct dwc2_host_chan *chan)
24478c2ecf20Sopenharmony_ci{
24488c2ecf20Sopenharmony_ci	if (!hsotg->unaligned_cache ||
24498c2ecf20Sopenharmony_ci	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
24508c2ecf20Sopenharmony_ci		return -ENOMEM;
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci	if (!qh->dw_align_buf) {
24538c2ecf20Sopenharmony_ci		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
24548c2ecf20Sopenharmony_ci						    GFP_ATOMIC | GFP_DMA);
24558c2ecf20Sopenharmony_ci		if (!qh->dw_align_buf)
24568c2ecf20Sopenharmony_ci			return -ENOMEM;
24578c2ecf20Sopenharmony_ci	}
24588c2ecf20Sopenharmony_ci
24598c2ecf20Sopenharmony_ci	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
24608c2ecf20Sopenharmony_ci					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
24618c2ecf20Sopenharmony_ci					      DMA_FROM_DEVICE);
24628c2ecf20Sopenharmony_ci
24638c2ecf20Sopenharmony_ci	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
24648c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "can't map align_buf\n");
24658c2ecf20Sopenharmony_ci		chan->align_buf = 0;
24668c2ecf20Sopenharmony_ci		return -EINVAL;
24678c2ecf20Sopenharmony_ci	}
24688c2ecf20Sopenharmony_ci
24698c2ecf20Sopenharmony_ci	chan->align_buf = qh->dw_align_buf_dma;
24708c2ecf20Sopenharmony_ci	return 0;
24718c2ecf20Sopenharmony_ci}
24728c2ecf20Sopenharmony_ci
24738c2ecf20Sopenharmony_ci#define DWC2_USB_DMA_ALIGN 4
24748c2ecf20Sopenharmony_ci
24758c2ecf20Sopenharmony_cistatic void dwc2_free_dma_aligned_buffer(struct urb *urb)
24768c2ecf20Sopenharmony_ci{
24778c2ecf20Sopenharmony_ci	void *stored_xfer_buffer;
24788c2ecf20Sopenharmony_ci	size_t length;
24798c2ecf20Sopenharmony_ci
24808c2ecf20Sopenharmony_ci	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
24818c2ecf20Sopenharmony_ci		return;
24828c2ecf20Sopenharmony_ci
24838c2ecf20Sopenharmony_ci	/* Restore urb->transfer_buffer from the end of the allocated area */
24848c2ecf20Sopenharmony_ci	memcpy(&stored_xfer_buffer,
24858c2ecf20Sopenharmony_ci	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
24868c2ecf20Sopenharmony_ci			 dma_get_cache_alignment()),
24878c2ecf20Sopenharmony_ci	       sizeof(urb->transfer_buffer));
24888c2ecf20Sopenharmony_ci
24898c2ecf20Sopenharmony_ci	if (usb_urb_dir_in(urb)) {
24908c2ecf20Sopenharmony_ci		if (usb_pipeisoc(urb->pipe))
24918c2ecf20Sopenharmony_ci			length = urb->transfer_buffer_length;
24928c2ecf20Sopenharmony_ci		else
24938c2ecf20Sopenharmony_ci			length = urb->actual_length;
24948c2ecf20Sopenharmony_ci
24958c2ecf20Sopenharmony_ci		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
24968c2ecf20Sopenharmony_ci	}
24978c2ecf20Sopenharmony_ci	kfree(urb->transfer_buffer);
24988c2ecf20Sopenharmony_ci	urb->transfer_buffer = stored_xfer_buffer;
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_ci	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
25018c2ecf20Sopenharmony_ci}
25028c2ecf20Sopenharmony_ci
25038c2ecf20Sopenharmony_cistatic int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
25048c2ecf20Sopenharmony_ci{
25058c2ecf20Sopenharmony_ci	void *kmalloc_ptr;
25068c2ecf20Sopenharmony_ci	size_t kmalloc_size;
25078c2ecf20Sopenharmony_ci
25088c2ecf20Sopenharmony_ci	if (urb->num_sgs || urb->sg ||
25098c2ecf20Sopenharmony_ci	    urb->transfer_buffer_length == 0 ||
25108c2ecf20Sopenharmony_ci	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
25118c2ecf20Sopenharmony_ci		return 0;
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_ci	/*
25148c2ecf20Sopenharmony_ci	 * Allocate a buffer with enough padding for original transfer_buffer
25158c2ecf20Sopenharmony_ci	 * pointer. This allocation is guaranteed to be aligned properly for
25168c2ecf20Sopenharmony_ci	 * DMA
25178c2ecf20Sopenharmony_ci	 */
25188c2ecf20Sopenharmony_ci	kmalloc_size = urb->transfer_buffer_length +
25198c2ecf20Sopenharmony_ci		(dma_get_cache_alignment() - 1) +
25208c2ecf20Sopenharmony_ci		sizeof(urb->transfer_buffer);
25218c2ecf20Sopenharmony_ci
25228c2ecf20Sopenharmony_ci	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
25238c2ecf20Sopenharmony_ci	if (!kmalloc_ptr)
25248c2ecf20Sopenharmony_ci		return -ENOMEM;
25258c2ecf20Sopenharmony_ci
25268c2ecf20Sopenharmony_ci	/*
25278c2ecf20Sopenharmony_ci	 * Position value of original urb->transfer_buffer pointer to the end
25288c2ecf20Sopenharmony_ci	 * of allocation for later referencing
25298c2ecf20Sopenharmony_ci	 */
25308c2ecf20Sopenharmony_ci	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
25318c2ecf20Sopenharmony_ci			 dma_get_cache_alignment()),
25328c2ecf20Sopenharmony_ci	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ci	if (usb_urb_dir_out(urb))
25358c2ecf20Sopenharmony_ci		memcpy(kmalloc_ptr, urb->transfer_buffer,
25368c2ecf20Sopenharmony_ci		       urb->transfer_buffer_length);
25378c2ecf20Sopenharmony_ci	urb->transfer_buffer = kmalloc_ptr;
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
25408c2ecf20Sopenharmony_ci
25418c2ecf20Sopenharmony_ci	return 0;
25428c2ecf20Sopenharmony_ci}
25438c2ecf20Sopenharmony_ci
25448c2ecf20Sopenharmony_cistatic int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
25458c2ecf20Sopenharmony_ci				gfp_t mem_flags)
25468c2ecf20Sopenharmony_ci{
25478c2ecf20Sopenharmony_ci	int ret;
25488c2ecf20Sopenharmony_ci
25498c2ecf20Sopenharmony_ci	/* We assume setup_dma is always aligned; warn if not */
25508c2ecf20Sopenharmony_ci	WARN_ON_ONCE(urb->setup_dma &&
25518c2ecf20Sopenharmony_ci		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
25528c2ecf20Sopenharmony_ci
25538c2ecf20Sopenharmony_ci	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
25548c2ecf20Sopenharmony_ci	if (ret)
25558c2ecf20Sopenharmony_ci		return ret;
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_ci	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
25588c2ecf20Sopenharmony_ci	if (ret)
25598c2ecf20Sopenharmony_ci		dwc2_free_dma_aligned_buffer(urb);
25608c2ecf20Sopenharmony_ci
25618c2ecf20Sopenharmony_ci	return ret;
25628c2ecf20Sopenharmony_ci}
25638c2ecf20Sopenharmony_ci
25648c2ecf20Sopenharmony_cistatic void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
25658c2ecf20Sopenharmony_ci{
25668c2ecf20Sopenharmony_ci	usb_hcd_unmap_urb_for_dma(hcd, urb);
25678c2ecf20Sopenharmony_ci	dwc2_free_dma_aligned_buffer(urb);
25688c2ecf20Sopenharmony_ci}
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci/**
25718c2ecf20Sopenharmony_ci * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
25728c2ecf20Sopenharmony_ci * channel and initializes the host channel to perform the transactions. The
25738c2ecf20Sopenharmony_ci * host channel is removed from the free list.
25748c2ecf20Sopenharmony_ci *
25758c2ecf20Sopenharmony_ci * @hsotg: The HCD state structure
25768c2ecf20Sopenharmony_ci * @qh:    Transactions from the first QTD for this QH are selected and assigned
25778c2ecf20Sopenharmony_ci *         to a free host channel
25788c2ecf20Sopenharmony_ci */
25798c2ecf20Sopenharmony_cistatic int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
25808c2ecf20Sopenharmony_ci{
25818c2ecf20Sopenharmony_ci	struct dwc2_host_chan *chan;
25828c2ecf20Sopenharmony_ci	struct dwc2_hcd_urb *urb;
25838c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd;
25848c2ecf20Sopenharmony_ci
25858c2ecf20Sopenharmony_ci	if (dbg_qh(qh))
25868c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_ci	if (list_empty(&qh->qtd_list)) {
25898c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
25908c2ecf20Sopenharmony_ci		return -ENOMEM;
25918c2ecf20Sopenharmony_ci	}
25928c2ecf20Sopenharmony_ci
25938c2ecf20Sopenharmony_ci	if (list_empty(&hsotg->free_hc_list)) {
25948c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "No free channel to assign\n");
25958c2ecf20Sopenharmony_ci		return -ENOMEM;
25968c2ecf20Sopenharmony_ci	}
25978c2ecf20Sopenharmony_ci
25988c2ecf20Sopenharmony_ci	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
25998c2ecf20Sopenharmony_ci				hc_list_entry);
26008c2ecf20Sopenharmony_ci
26018c2ecf20Sopenharmony_ci	/* Remove host channel from free list */
26028c2ecf20Sopenharmony_ci	list_del_init(&chan->hc_list_entry);
26038c2ecf20Sopenharmony_ci
26048c2ecf20Sopenharmony_ci	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
26058c2ecf20Sopenharmony_ci	urb = qtd->urb;
26068c2ecf20Sopenharmony_ci	qh->channel = chan;
26078c2ecf20Sopenharmony_ci	qtd->in_process = 1;
26088c2ecf20Sopenharmony_ci
26098c2ecf20Sopenharmony_ci	/*
26108c2ecf20Sopenharmony_ci	 * Use usb_pipedevice to determine device address. This address is
26118c2ecf20Sopenharmony_ci	 * 0 before the SET_ADDRESS command and the correct address afterward.
26128c2ecf20Sopenharmony_ci	 */
26138c2ecf20Sopenharmony_ci	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
26148c2ecf20Sopenharmony_ci	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
26158c2ecf20Sopenharmony_ci	chan->speed = qh->dev_speed;
26168c2ecf20Sopenharmony_ci	chan->max_packet = qh->maxp;
26178c2ecf20Sopenharmony_ci
26188c2ecf20Sopenharmony_ci	chan->xfer_started = 0;
26198c2ecf20Sopenharmony_ci	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
26208c2ecf20Sopenharmony_ci	chan->error_state = (qtd->error_count > 0);
26218c2ecf20Sopenharmony_ci	chan->halt_on_queue = 0;
26228c2ecf20Sopenharmony_ci	chan->halt_pending = 0;
26238c2ecf20Sopenharmony_ci	chan->requests = 0;
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_ci	/*
26268c2ecf20Sopenharmony_ci	 * The following values may be modified in the transfer type section
26278c2ecf20Sopenharmony_ci	 * below. The xfer_len value may be reduced when the transfer is
26288c2ecf20Sopenharmony_ci	 * started to accommodate the max widths of the XferSize and PktCnt
26298c2ecf20Sopenharmony_ci	 * fields in the HCTSIZn register.
26308c2ecf20Sopenharmony_ci	 */
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
26338c2ecf20Sopenharmony_ci	if (chan->ep_is_in)
26348c2ecf20Sopenharmony_ci		chan->do_ping = 0;
26358c2ecf20Sopenharmony_ci	else
26368c2ecf20Sopenharmony_ci		chan->do_ping = qh->ping_state;
26378c2ecf20Sopenharmony_ci
26388c2ecf20Sopenharmony_ci	chan->data_pid_start = qh->data_toggle;
26398c2ecf20Sopenharmony_ci	chan->multi_count = 1;
26408c2ecf20Sopenharmony_ci
26418c2ecf20Sopenharmony_ci	if (urb->actual_length > urb->length &&
26428c2ecf20Sopenharmony_ci	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
26438c2ecf20Sopenharmony_ci		urb->actual_length = urb->length;
26448c2ecf20Sopenharmony_ci
26458c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma)
26468c2ecf20Sopenharmony_ci		chan->xfer_dma = urb->dma + urb->actual_length;
26478c2ecf20Sopenharmony_ci	else
26488c2ecf20Sopenharmony_ci		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
26498c2ecf20Sopenharmony_ci
26508c2ecf20Sopenharmony_ci	chan->xfer_len = urb->length - urb->actual_length;
26518c2ecf20Sopenharmony_ci	chan->xfer_count = 0;
26528c2ecf20Sopenharmony_ci
26538c2ecf20Sopenharmony_ci	/* Set the split attributes if required */
26548c2ecf20Sopenharmony_ci	if (qh->do_split)
26558c2ecf20Sopenharmony_ci		dwc2_hc_init_split(hsotg, chan, qtd, urb);
26568c2ecf20Sopenharmony_ci	else
26578c2ecf20Sopenharmony_ci		chan->do_split = 0;
26588c2ecf20Sopenharmony_ci
26598c2ecf20Sopenharmony_ci	/* Set the transfer attributes */
26608c2ecf20Sopenharmony_ci	dwc2_hc_init_xfer(hsotg, chan, qtd);
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci	/* For non-dword aligned buffers */
26638c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma && qh->do_split &&
26648c2ecf20Sopenharmony_ci	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
26658c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
26668c2ecf20Sopenharmony_ci		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
26678c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
26688c2ecf20Sopenharmony_ci				"Failed to allocate memory to handle non-aligned buffer\n");
26698c2ecf20Sopenharmony_ci			/* Add channel back to free list */
26708c2ecf20Sopenharmony_ci			chan->align_buf = 0;
26718c2ecf20Sopenharmony_ci			chan->multi_count = 0;
26728c2ecf20Sopenharmony_ci			list_add_tail(&chan->hc_list_entry,
26738c2ecf20Sopenharmony_ci				      &hsotg->free_hc_list);
26748c2ecf20Sopenharmony_ci			qtd->in_process = 0;
26758c2ecf20Sopenharmony_ci			qh->channel = NULL;
26768c2ecf20Sopenharmony_ci			return -ENOMEM;
26778c2ecf20Sopenharmony_ci		}
26788c2ecf20Sopenharmony_ci	} else {
26798c2ecf20Sopenharmony_ci		/*
26808c2ecf20Sopenharmony_ci		 * We assume that DMA is always aligned in non-split
26818c2ecf20Sopenharmony_ci		 * case or split out case. Warn if not.
26828c2ecf20Sopenharmony_ci		 */
26838c2ecf20Sopenharmony_ci		WARN_ON_ONCE(hsotg->params.host_dma &&
26848c2ecf20Sopenharmony_ci			     (chan->xfer_dma & 0x3));
26858c2ecf20Sopenharmony_ci		chan->align_buf = 0;
26868c2ecf20Sopenharmony_ci	}
26878c2ecf20Sopenharmony_ci
26888c2ecf20Sopenharmony_ci	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
26898c2ecf20Sopenharmony_ci	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
26908c2ecf20Sopenharmony_ci		/*
26918c2ecf20Sopenharmony_ci		 * This value may be modified when the transfer is started
26928c2ecf20Sopenharmony_ci		 * to reflect the actual transfer length
26938c2ecf20Sopenharmony_ci		 */
26948c2ecf20Sopenharmony_ci		chan->multi_count = qh->maxp_mult;
26958c2ecf20Sopenharmony_ci
26968c2ecf20Sopenharmony_ci	if (hsotg->params.dma_desc_enable) {
26978c2ecf20Sopenharmony_ci		chan->desc_list_addr = qh->desc_list_dma;
26988c2ecf20Sopenharmony_ci		chan->desc_list_sz = qh->desc_list_sz;
26998c2ecf20Sopenharmony_ci	}
27008c2ecf20Sopenharmony_ci
27018c2ecf20Sopenharmony_ci	dwc2_hc_init(hsotg, chan);
27028c2ecf20Sopenharmony_ci	chan->qh = qh;
27038c2ecf20Sopenharmony_ci
27048c2ecf20Sopenharmony_ci	return 0;
27058c2ecf20Sopenharmony_ci}
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_ci/**
27088c2ecf20Sopenharmony_ci * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
27098c2ecf20Sopenharmony_ci * schedule and assigns them to available host channels. Called from the HCD
27108c2ecf20Sopenharmony_ci * interrupt handler functions.
27118c2ecf20Sopenharmony_ci *
27128c2ecf20Sopenharmony_ci * @hsotg: The HCD state structure
27138c2ecf20Sopenharmony_ci *
27148c2ecf20Sopenharmony_ci * Return: The types of new transactions that were assigned to host channels
27158c2ecf20Sopenharmony_ci */
27168c2ecf20Sopenharmony_cienum dwc2_transaction_type dwc2_hcd_select_transactions(
27178c2ecf20Sopenharmony_ci		struct dwc2_hsotg *hsotg)
27188c2ecf20Sopenharmony_ci{
27198c2ecf20Sopenharmony_ci	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
27208c2ecf20Sopenharmony_ci	struct list_head *qh_ptr;
27218c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
27228c2ecf20Sopenharmony_ci	int num_channels;
27238c2ecf20Sopenharmony_ci
27248c2ecf20Sopenharmony_ci#ifdef DWC2_DEBUG_SOF
27258c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Select Transactions\n");
27268c2ecf20Sopenharmony_ci#endif
27278c2ecf20Sopenharmony_ci
27288c2ecf20Sopenharmony_ci	/* Process entries in the periodic ready list */
27298c2ecf20Sopenharmony_ci	qh_ptr = hsotg->periodic_sched_ready.next;
27308c2ecf20Sopenharmony_ci	while (qh_ptr != &hsotg->periodic_sched_ready) {
27318c2ecf20Sopenharmony_ci		if (list_empty(&hsotg->free_hc_list))
27328c2ecf20Sopenharmony_ci			break;
27338c2ecf20Sopenharmony_ci		if (hsotg->params.uframe_sched) {
27348c2ecf20Sopenharmony_ci			if (hsotg->available_host_channels <= 1)
27358c2ecf20Sopenharmony_ci				break;
27368c2ecf20Sopenharmony_ci			hsotg->available_host_channels--;
27378c2ecf20Sopenharmony_ci		}
27388c2ecf20Sopenharmony_ci		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
27398c2ecf20Sopenharmony_ci		if (dwc2_assign_and_init_hc(hsotg, qh))
27408c2ecf20Sopenharmony_ci			break;
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_ci		/*
27438c2ecf20Sopenharmony_ci		 * Move the QH from the periodic ready schedule to the
27448c2ecf20Sopenharmony_ci		 * periodic assigned schedule
27458c2ecf20Sopenharmony_ci		 */
27468c2ecf20Sopenharmony_ci		qh_ptr = qh_ptr->next;
27478c2ecf20Sopenharmony_ci		list_move_tail(&qh->qh_list_entry,
27488c2ecf20Sopenharmony_ci			       &hsotg->periodic_sched_assigned);
27498c2ecf20Sopenharmony_ci		ret_val = DWC2_TRANSACTION_PERIODIC;
27508c2ecf20Sopenharmony_ci	}
27518c2ecf20Sopenharmony_ci
27528c2ecf20Sopenharmony_ci	/*
27538c2ecf20Sopenharmony_ci	 * Process entries in the inactive portion of the non-periodic
27548c2ecf20Sopenharmony_ci	 * schedule. Some free host channels may not be used if they are
27558c2ecf20Sopenharmony_ci	 * reserved for periodic transfers.
27568c2ecf20Sopenharmony_ci	 */
27578c2ecf20Sopenharmony_ci	num_channels = hsotg->params.host_channels;
27588c2ecf20Sopenharmony_ci	qh_ptr = hsotg->non_periodic_sched_inactive.next;
27598c2ecf20Sopenharmony_ci	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
27608c2ecf20Sopenharmony_ci		if (!hsotg->params.uframe_sched &&
27618c2ecf20Sopenharmony_ci		    hsotg->non_periodic_channels >= num_channels -
27628c2ecf20Sopenharmony_ci						hsotg->periodic_channels)
27638c2ecf20Sopenharmony_ci			break;
27648c2ecf20Sopenharmony_ci		if (list_empty(&hsotg->free_hc_list))
27658c2ecf20Sopenharmony_ci			break;
27668c2ecf20Sopenharmony_ci		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
27678c2ecf20Sopenharmony_ci		if (hsotg->params.uframe_sched) {
27688c2ecf20Sopenharmony_ci			if (hsotg->available_host_channels < 1)
27698c2ecf20Sopenharmony_ci				break;
27708c2ecf20Sopenharmony_ci			hsotg->available_host_channels--;
27718c2ecf20Sopenharmony_ci		}
27728c2ecf20Sopenharmony_ci
27738c2ecf20Sopenharmony_ci		if (dwc2_assign_and_init_hc(hsotg, qh))
27748c2ecf20Sopenharmony_ci			break;
27758c2ecf20Sopenharmony_ci
27768c2ecf20Sopenharmony_ci		/*
27778c2ecf20Sopenharmony_ci		 * Move the QH from the non-periodic inactive schedule to the
27788c2ecf20Sopenharmony_ci		 * non-periodic active schedule
27798c2ecf20Sopenharmony_ci		 */
27808c2ecf20Sopenharmony_ci		qh_ptr = qh_ptr->next;
27818c2ecf20Sopenharmony_ci		list_move_tail(&qh->qh_list_entry,
27828c2ecf20Sopenharmony_ci			       &hsotg->non_periodic_sched_active);
27838c2ecf20Sopenharmony_ci
27848c2ecf20Sopenharmony_ci		if (ret_val == DWC2_TRANSACTION_NONE)
27858c2ecf20Sopenharmony_ci			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
27868c2ecf20Sopenharmony_ci		else
27878c2ecf20Sopenharmony_ci			ret_val = DWC2_TRANSACTION_ALL;
27888c2ecf20Sopenharmony_ci
27898c2ecf20Sopenharmony_ci		if (!hsotg->params.uframe_sched)
27908c2ecf20Sopenharmony_ci			hsotg->non_periodic_channels++;
27918c2ecf20Sopenharmony_ci	}
27928c2ecf20Sopenharmony_ci
27938c2ecf20Sopenharmony_ci	return ret_val;
27948c2ecf20Sopenharmony_ci}
27958c2ecf20Sopenharmony_ci
27968c2ecf20Sopenharmony_ci/**
27978c2ecf20Sopenharmony_ci * dwc2_queue_transaction() - Attempts to queue a single transaction request for
27988c2ecf20Sopenharmony_ci * a host channel associated with either a periodic or non-periodic transfer
27998c2ecf20Sopenharmony_ci *
28008c2ecf20Sopenharmony_ci * @hsotg: The HCD state structure
28018c2ecf20Sopenharmony_ci * @chan:  Host channel descriptor associated with either a periodic or
28028c2ecf20Sopenharmony_ci *         non-periodic transfer
28038c2ecf20Sopenharmony_ci * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
28048c2ecf20Sopenharmony_ci *                     for periodic transfers or the non-periodic Tx FIFO
28058c2ecf20Sopenharmony_ci *                     for non-periodic transfers
28068c2ecf20Sopenharmony_ci *
28078c2ecf20Sopenharmony_ci * Return: 1 if a request is queued and more requests may be needed to
28088c2ecf20Sopenharmony_ci * complete the transfer, 0 if no more requests are required for this
28098c2ecf20Sopenharmony_ci * transfer, -1 if there is insufficient space in the Tx FIFO
28108c2ecf20Sopenharmony_ci *
28118c2ecf20Sopenharmony_ci * This function assumes that there is space available in the appropriate
28128c2ecf20Sopenharmony_ci * request queue. For an OUT transfer or SETUP transaction in Slave mode,
28138c2ecf20Sopenharmony_ci * it checks whether space is available in the appropriate Tx FIFO.
28148c2ecf20Sopenharmony_ci *
28158c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
28168c2ecf20Sopenharmony_ci */
28178c2ecf20Sopenharmony_cistatic int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
28188c2ecf20Sopenharmony_ci				  struct dwc2_host_chan *chan,
28198c2ecf20Sopenharmony_ci				  u16 fifo_dwords_avail)
28208c2ecf20Sopenharmony_ci{
28218c2ecf20Sopenharmony_ci	int retval = 0;
28228c2ecf20Sopenharmony_ci
28238c2ecf20Sopenharmony_ci	if (chan->do_split)
28248c2ecf20Sopenharmony_ci		/* Put ourselves on the list to keep order straight */
28258c2ecf20Sopenharmony_ci		list_move_tail(&chan->split_order_list_entry,
28268c2ecf20Sopenharmony_ci			       &hsotg->split_order);
28278c2ecf20Sopenharmony_ci
28288c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma && chan->qh) {
28298c2ecf20Sopenharmony_ci		if (hsotg->params.dma_desc_enable) {
28308c2ecf20Sopenharmony_ci			if (!chan->xfer_started ||
28318c2ecf20Sopenharmony_ci			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
28328c2ecf20Sopenharmony_ci				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
28338c2ecf20Sopenharmony_ci				chan->qh->ping_state = 0;
28348c2ecf20Sopenharmony_ci			}
28358c2ecf20Sopenharmony_ci		} else if (!chan->xfer_started) {
28368c2ecf20Sopenharmony_ci			dwc2_hc_start_transfer(hsotg, chan);
28378c2ecf20Sopenharmony_ci			chan->qh->ping_state = 0;
28388c2ecf20Sopenharmony_ci		}
28398c2ecf20Sopenharmony_ci	} else if (chan->halt_pending) {
28408c2ecf20Sopenharmony_ci		/* Don't queue a request if the channel has been halted */
28418c2ecf20Sopenharmony_ci	} else if (chan->halt_on_queue) {
28428c2ecf20Sopenharmony_ci		dwc2_hc_halt(hsotg, chan, chan->halt_status);
28438c2ecf20Sopenharmony_ci	} else if (chan->do_ping) {
28448c2ecf20Sopenharmony_ci		if (!chan->xfer_started)
28458c2ecf20Sopenharmony_ci			dwc2_hc_start_transfer(hsotg, chan);
28468c2ecf20Sopenharmony_ci	} else if (!chan->ep_is_in ||
28478c2ecf20Sopenharmony_ci		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
28488c2ecf20Sopenharmony_ci		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
28498c2ecf20Sopenharmony_ci			if (!chan->xfer_started) {
28508c2ecf20Sopenharmony_ci				dwc2_hc_start_transfer(hsotg, chan);
28518c2ecf20Sopenharmony_ci				retval = 1;
28528c2ecf20Sopenharmony_ci			} else {
28538c2ecf20Sopenharmony_ci				retval = dwc2_hc_continue_transfer(hsotg, chan);
28548c2ecf20Sopenharmony_ci			}
28558c2ecf20Sopenharmony_ci		} else {
28568c2ecf20Sopenharmony_ci			retval = -1;
28578c2ecf20Sopenharmony_ci		}
28588c2ecf20Sopenharmony_ci	} else {
28598c2ecf20Sopenharmony_ci		if (!chan->xfer_started) {
28608c2ecf20Sopenharmony_ci			dwc2_hc_start_transfer(hsotg, chan);
28618c2ecf20Sopenharmony_ci			retval = 1;
28628c2ecf20Sopenharmony_ci		} else {
28638c2ecf20Sopenharmony_ci			retval = dwc2_hc_continue_transfer(hsotg, chan);
28648c2ecf20Sopenharmony_ci		}
28658c2ecf20Sopenharmony_ci	}
28668c2ecf20Sopenharmony_ci
28678c2ecf20Sopenharmony_ci	return retval;
28688c2ecf20Sopenharmony_ci}
28698c2ecf20Sopenharmony_ci
28708c2ecf20Sopenharmony_ci/*
28718c2ecf20Sopenharmony_ci * Processes periodic channels for the next frame and queues transactions for
28728c2ecf20Sopenharmony_ci * these channels to the DWC_otg controller. After queueing transactions, the
28738c2ecf20Sopenharmony_ci * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
28748c2ecf20Sopenharmony_ci * to queue as Periodic Tx FIFO or request queue space becomes available.
28758c2ecf20Sopenharmony_ci * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
28768c2ecf20Sopenharmony_ci *
28778c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
28788c2ecf20Sopenharmony_ci */
28798c2ecf20Sopenharmony_cistatic void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
28808c2ecf20Sopenharmony_ci{
28818c2ecf20Sopenharmony_ci	struct list_head *qh_ptr;
28828c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
28838c2ecf20Sopenharmony_ci	u32 tx_status;
28848c2ecf20Sopenharmony_ci	u32 fspcavail;
28858c2ecf20Sopenharmony_ci	u32 gintmsk;
28868c2ecf20Sopenharmony_ci	int status;
28878c2ecf20Sopenharmony_ci	bool no_queue_space = false;
28888c2ecf20Sopenharmony_ci	bool no_fifo_space = false;
28898c2ecf20Sopenharmony_ci	u32 qspcavail;
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_ci	/* If empty list then just adjust interrupt enables */
28928c2ecf20Sopenharmony_ci	if (list_empty(&hsotg->periodic_sched_assigned))
28938c2ecf20Sopenharmony_ci		goto exit;
28948c2ecf20Sopenharmony_ci
28958c2ecf20Sopenharmony_ci	if (dbg_perio())
28968c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
28978c2ecf20Sopenharmony_ci
28988c2ecf20Sopenharmony_ci	tx_status = dwc2_readl(hsotg, HPTXSTS);
28998c2ecf20Sopenharmony_ci	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
29008c2ecf20Sopenharmony_ci		    TXSTS_QSPCAVAIL_SHIFT;
29018c2ecf20Sopenharmony_ci	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
29028c2ecf20Sopenharmony_ci		    TXSTS_FSPCAVAIL_SHIFT;
29038c2ecf20Sopenharmony_ci
29048c2ecf20Sopenharmony_ci	if (dbg_perio()) {
29058c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
29068c2ecf20Sopenharmony_ci			 qspcavail);
29078c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
29088c2ecf20Sopenharmony_ci			 fspcavail);
29098c2ecf20Sopenharmony_ci	}
29108c2ecf20Sopenharmony_ci
29118c2ecf20Sopenharmony_ci	qh_ptr = hsotg->periodic_sched_assigned.next;
29128c2ecf20Sopenharmony_ci	while (qh_ptr != &hsotg->periodic_sched_assigned) {
29138c2ecf20Sopenharmony_ci		tx_status = dwc2_readl(hsotg, HPTXSTS);
29148c2ecf20Sopenharmony_ci		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
29158c2ecf20Sopenharmony_ci			    TXSTS_QSPCAVAIL_SHIFT;
29168c2ecf20Sopenharmony_ci		if (qspcavail == 0) {
29178c2ecf20Sopenharmony_ci			no_queue_space = true;
29188c2ecf20Sopenharmony_ci			break;
29198c2ecf20Sopenharmony_ci		}
29208c2ecf20Sopenharmony_ci
29218c2ecf20Sopenharmony_ci		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
29228c2ecf20Sopenharmony_ci		if (!qh->channel) {
29238c2ecf20Sopenharmony_ci			qh_ptr = qh_ptr->next;
29248c2ecf20Sopenharmony_ci			continue;
29258c2ecf20Sopenharmony_ci		}
29268c2ecf20Sopenharmony_ci
29278c2ecf20Sopenharmony_ci		/* Make sure EP's TT buffer is clean before queueing qtds */
29288c2ecf20Sopenharmony_ci		if (qh->tt_buffer_dirty) {
29298c2ecf20Sopenharmony_ci			qh_ptr = qh_ptr->next;
29308c2ecf20Sopenharmony_ci			continue;
29318c2ecf20Sopenharmony_ci		}
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci		/*
29348c2ecf20Sopenharmony_ci		 * Set a flag if we're queuing high-bandwidth in slave mode.
29358c2ecf20Sopenharmony_ci		 * The flag prevents any halts to get into the request queue in
29368c2ecf20Sopenharmony_ci		 * the middle of multiple high-bandwidth packets getting queued.
29378c2ecf20Sopenharmony_ci		 */
29388c2ecf20Sopenharmony_ci		if (!hsotg->params.host_dma &&
29398c2ecf20Sopenharmony_ci		    qh->channel->multi_count > 1)
29408c2ecf20Sopenharmony_ci			hsotg->queuing_high_bandwidth = 1;
29418c2ecf20Sopenharmony_ci
29428c2ecf20Sopenharmony_ci		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
29438c2ecf20Sopenharmony_ci			    TXSTS_FSPCAVAIL_SHIFT;
29448c2ecf20Sopenharmony_ci		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
29458c2ecf20Sopenharmony_ci		if (status < 0) {
29468c2ecf20Sopenharmony_ci			no_fifo_space = true;
29478c2ecf20Sopenharmony_ci			break;
29488c2ecf20Sopenharmony_ci		}
29498c2ecf20Sopenharmony_ci
29508c2ecf20Sopenharmony_ci		/*
29518c2ecf20Sopenharmony_ci		 * In Slave mode, stay on the current transfer until there is
29528c2ecf20Sopenharmony_ci		 * nothing more to do or the high-bandwidth request count is
29538c2ecf20Sopenharmony_ci		 * reached. In DMA mode, only need to queue one request. The
29548c2ecf20Sopenharmony_ci		 * controller automatically handles multiple packets for
29558c2ecf20Sopenharmony_ci		 * high-bandwidth transfers.
29568c2ecf20Sopenharmony_ci		 */
29578c2ecf20Sopenharmony_ci		if (hsotg->params.host_dma || status == 0 ||
29588c2ecf20Sopenharmony_ci		    qh->channel->requests == qh->channel->multi_count) {
29598c2ecf20Sopenharmony_ci			qh_ptr = qh_ptr->next;
29608c2ecf20Sopenharmony_ci			/*
29618c2ecf20Sopenharmony_ci			 * Move the QH from the periodic assigned schedule to
29628c2ecf20Sopenharmony_ci			 * the periodic queued schedule
29638c2ecf20Sopenharmony_ci			 */
29648c2ecf20Sopenharmony_ci			list_move_tail(&qh->qh_list_entry,
29658c2ecf20Sopenharmony_ci				       &hsotg->periodic_sched_queued);
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci			/* done queuing high bandwidth */
29688c2ecf20Sopenharmony_ci			hsotg->queuing_high_bandwidth = 0;
29698c2ecf20Sopenharmony_ci		}
29708c2ecf20Sopenharmony_ci	}
29718c2ecf20Sopenharmony_ci
29728c2ecf20Sopenharmony_ciexit:
29738c2ecf20Sopenharmony_ci	if (no_queue_space || no_fifo_space ||
29748c2ecf20Sopenharmony_ci	    (!hsotg->params.host_dma &&
29758c2ecf20Sopenharmony_ci	     !list_empty(&hsotg->periodic_sched_assigned))) {
29768c2ecf20Sopenharmony_ci		/*
29778c2ecf20Sopenharmony_ci		 * May need to queue more transactions as the request
29788c2ecf20Sopenharmony_ci		 * queue or Tx FIFO empties. Enable the periodic Tx
29798c2ecf20Sopenharmony_ci		 * FIFO empty interrupt. (Always use the half-empty
29808c2ecf20Sopenharmony_ci		 * level to ensure that new requests are loaded as
29818c2ecf20Sopenharmony_ci		 * soon as possible.)
29828c2ecf20Sopenharmony_ci		 */
29838c2ecf20Sopenharmony_ci		gintmsk = dwc2_readl(hsotg, GINTMSK);
29848c2ecf20Sopenharmony_ci		if (!(gintmsk & GINTSTS_PTXFEMP)) {
29858c2ecf20Sopenharmony_ci			gintmsk |= GINTSTS_PTXFEMP;
29868c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, gintmsk, GINTMSK);
29878c2ecf20Sopenharmony_ci		}
29888c2ecf20Sopenharmony_ci	} else {
29898c2ecf20Sopenharmony_ci		/*
29908c2ecf20Sopenharmony_ci		 * Disable the Tx FIFO empty interrupt since there are
29918c2ecf20Sopenharmony_ci		 * no more transactions that need to be queued right
29928c2ecf20Sopenharmony_ci		 * now. This function is called from interrupt
29938c2ecf20Sopenharmony_ci		 * handlers to queue more transactions as transfer
29948c2ecf20Sopenharmony_ci		 * states change.
29958c2ecf20Sopenharmony_ci		 */
29968c2ecf20Sopenharmony_ci		gintmsk = dwc2_readl(hsotg, GINTMSK);
29978c2ecf20Sopenharmony_ci		if (gintmsk & GINTSTS_PTXFEMP) {
29988c2ecf20Sopenharmony_ci			gintmsk &= ~GINTSTS_PTXFEMP;
29998c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, gintmsk, GINTMSK);
30008c2ecf20Sopenharmony_ci		}
30018c2ecf20Sopenharmony_ci	}
30028c2ecf20Sopenharmony_ci}
30038c2ecf20Sopenharmony_ci
30048c2ecf20Sopenharmony_ci/*
30058c2ecf20Sopenharmony_ci * Processes active non-periodic channels and queues transactions for these
30068c2ecf20Sopenharmony_ci * channels to the DWC_otg controller. After queueing transactions, the NP Tx
30078c2ecf20Sopenharmony_ci * FIFO Empty interrupt is enabled if there are more transactions to queue as
30088c2ecf20Sopenharmony_ci * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
30098c2ecf20Sopenharmony_ci * FIFO Empty interrupt is disabled.
30108c2ecf20Sopenharmony_ci *
30118c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
30128c2ecf20Sopenharmony_ci */
30138c2ecf20Sopenharmony_cistatic void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
30148c2ecf20Sopenharmony_ci{
30158c2ecf20Sopenharmony_ci	struct list_head *orig_qh_ptr;
30168c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
30178c2ecf20Sopenharmony_ci	u32 tx_status;
30188c2ecf20Sopenharmony_ci	u32 qspcavail;
30198c2ecf20Sopenharmony_ci	u32 fspcavail;
30208c2ecf20Sopenharmony_ci	u32 gintmsk;
30218c2ecf20Sopenharmony_ci	int status;
30228c2ecf20Sopenharmony_ci	int no_queue_space = 0;
30238c2ecf20Sopenharmony_ci	int no_fifo_space = 0;
30248c2ecf20Sopenharmony_ci	int more_to_do = 0;
30258c2ecf20Sopenharmony_ci
30268c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
30278c2ecf20Sopenharmony_ci
30288c2ecf20Sopenharmony_ci	tx_status = dwc2_readl(hsotg, GNPTXSTS);
30298c2ecf20Sopenharmony_ci	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
30308c2ecf20Sopenharmony_ci		    TXSTS_QSPCAVAIL_SHIFT;
30318c2ecf20Sopenharmony_ci	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
30328c2ecf20Sopenharmony_ci		    TXSTS_FSPCAVAIL_SHIFT;
30338c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
30348c2ecf20Sopenharmony_ci		 qspcavail);
30358c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
30368c2ecf20Sopenharmony_ci		 fspcavail);
30378c2ecf20Sopenharmony_ci
30388c2ecf20Sopenharmony_ci	/*
30398c2ecf20Sopenharmony_ci	 * Keep track of the starting point. Skip over the start-of-list
30408c2ecf20Sopenharmony_ci	 * entry.
30418c2ecf20Sopenharmony_ci	 */
30428c2ecf20Sopenharmony_ci	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
30438c2ecf20Sopenharmony_ci		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
30448c2ecf20Sopenharmony_ci	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
30458c2ecf20Sopenharmony_ci
30468c2ecf20Sopenharmony_ci	/*
30478c2ecf20Sopenharmony_ci	 * Process once through the active list or until no more space is
30488c2ecf20Sopenharmony_ci	 * available in the request queue or the Tx FIFO
30498c2ecf20Sopenharmony_ci	 */
30508c2ecf20Sopenharmony_ci	do {
30518c2ecf20Sopenharmony_ci		tx_status = dwc2_readl(hsotg, GNPTXSTS);
30528c2ecf20Sopenharmony_ci		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
30538c2ecf20Sopenharmony_ci			    TXSTS_QSPCAVAIL_SHIFT;
30548c2ecf20Sopenharmony_ci		if (!hsotg->params.host_dma && qspcavail == 0) {
30558c2ecf20Sopenharmony_ci			no_queue_space = 1;
30568c2ecf20Sopenharmony_ci			break;
30578c2ecf20Sopenharmony_ci		}
30588c2ecf20Sopenharmony_ci
30598c2ecf20Sopenharmony_ci		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
30608c2ecf20Sopenharmony_ci				qh_list_entry);
30618c2ecf20Sopenharmony_ci		if (!qh->channel)
30628c2ecf20Sopenharmony_ci			goto next;
30638c2ecf20Sopenharmony_ci
30648c2ecf20Sopenharmony_ci		/* Make sure EP's TT buffer is clean before queueing qtds */
30658c2ecf20Sopenharmony_ci		if (qh->tt_buffer_dirty)
30668c2ecf20Sopenharmony_ci			goto next;
30678c2ecf20Sopenharmony_ci
30688c2ecf20Sopenharmony_ci		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
30698c2ecf20Sopenharmony_ci			    TXSTS_FSPCAVAIL_SHIFT;
30708c2ecf20Sopenharmony_ci		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
30718c2ecf20Sopenharmony_ci
30728c2ecf20Sopenharmony_ci		if (status > 0) {
30738c2ecf20Sopenharmony_ci			more_to_do = 1;
30748c2ecf20Sopenharmony_ci		} else if (status < 0) {
30758c2ecf20Sopenharmony_ci			no_fifo_space = 1;
30768c2ecf20Sopenharmony_ci			break;
30778c2ecf20Sopenharmony_ci		}
30788c2ecf20Sopenharmony_cinext:
30798c2ecf20Sopenharmony_ci		/* Advance to next QH, skipping start-of-list entry */
30808c2ecf20Sopenharmony_ci		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
30818c2ecf20Sopenharmony_ci		if (hsotg->non_periodic_qh_ptr ==
30828c2ecf20Sopenharmony_ci				&hsotg->non_periodic_sched_active)
30838c2ecf20Sopenharmony_ci			hsotg->non_periodic_qh_ptr =
30848c2ecf20Sopenharmony_ci					hsotg->non_periodic_qh_ptr->next;
30858c2ecf20Sopenharmony_ci	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
30868c2ecf20Sopenharmony_ci
30878c2ecf20Sopenharmony_ci	if (!hsotg->params.host_dma) {
30888c2ecf20Sopenharmony_ci		tx_status = dwc2_readl(hsotg, GNPTXSTS);
30898c2ecf20Sopenharmony_ci		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
30908c2ecf20Sopenharmony_ci			    TXSTS_QSPCAVAIL_SHIFT;
30918c2ecf20Sopenharmony_ci		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
30928c2ecf20Sopenharmony_ci			    TXSTS_FSPCAVAIL_SHIFT;
30938c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
30948c2ecf20Sopenharmony_ci			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
30958c2ecf20Sopenharmony_ci			 qspcavail);
30968c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
30978c2ecf20Sopenharmony_ci			 "  NP Tx FIFO Space Avail (after queue): %d\n",
30988c2ecf20Sopenharmony_ci			 fspcavail);
30998c2ecf20Sopenharmony_ci
31008c2ecf20Sopenharmony_ci		if (more_to_do || no_queue_space || no_fifo_space) {
31018c2ecf20Sopenharmony_ci			/*
31028c2ecf20Sopenharmony_ci			 * May need to queue more transactions as the request
31038c2ecf20Sopenharmony_ci			 * queue or Tx FIFO empties. Enable the non-periodic
31048c2ecf20Sopenharmony_ci			 * Tx FIFO empty interrupt. (Always use the half-empty
31058c2ecf20Sopenharmony_ci			 * level to ensure that new requests are loaded as
31068c2ecf20Sopenharmony_ci			 * soon as possible.)
31078c2ecf20Sopenharmony_ci			 */
31088c2ecf20Sopenharmony_ci			gintmsk = dwc2_readl(hsotg, GINTMSK);
31098c2ecf20Sopenharmony_ci			gintmsk |= GINTSTS_NPTXFEMP;
31108c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, gintmsk, GINTMSK);
31118c2ecf20Sopenharmony_ci		} else {
31128c2ecf20Sopenharmony_ci			/*
31138c2ecf20Sopenharmony_ci			 * Disable the Tx FIFO empty interrupt since there are
31148c2ecf20Sopenharmony_ci			 * no more transactions that need to be queued right
31158c2ecf20Sopenharmony_ci			 * now. This function is called from interrupt
31168c2ecf20Sopenharmony_ci			 * handlers to queue more transactions as transfer
31178c2ecf20Sopenharmony_ci			 * states change.
31188c2ecf20Sopenharmony_ci			 */
31198c2ecf20Sopenharmony_ci			gintmsk = dwc2_readl(hsotg, GINTMSK);
31208c2ecf20Sopenharmony_ci			gintmsk &= ~GINTSTS_NPTXFEMP;
31218c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, gintmsk, GINTMSK);
31228c2ecf20Sopenharmony_ci		}
31238c2ecf20Sopenharmony_ci	}
31248c2ecf20Sopenharmony_ci}
31258c2ecf20Sopenharmony_ci
31268c2ecf20Sopenharmony_ci/**
31278c2ecf20Sopenharmony_ci * dwc2_hcd_queue_transactions() - Processes the currently active host channels
31288c2ecf20Sopenharmony_ci * and queues transactions for these channels to the DWC_otg controller. Called
31298c2ecf20Sopenharmony_ci * from the HCD interrupt handler functions.
31308c2ecf20Sopenharmony_ci *
31318c2ecf20Sopenharmony_ci * @hsotg:   The HCD state structure
31328c2ecf20Sopenharmony_ci * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
31338c2ecf20Sopenharmony_ci *           or both)
31348c2ecf20Sopenharmony_ci *
31358c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
31368c2ecf20Sopenharmony_ci */
31378c2ecf20Sopenharmony_civoid dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
31388c2ecf20Sopenharmony_ci				 enum dwc2_transaction_type tr_type)
31398c2ecf20Sopenharmony_ci{
31408c2ecf20Sopenharmony_ci#ifdef DWC2_DEBUG_SOF
31418c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "Queue Transactions\n");
31428c2ecf20Sopenharmony_ci#endif
31438c2ecf20Sopenharmony_ci	/* Process host channels associated with periodic transfers */
31448c2ecf20Sopenharmony_ci	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31458c2ecf20Sopenharmony_ci	    tr_type == DWC2_TRANSACTION_ALL)
31468c2ecf20Sopenharmony_ci		dwc2_process_periodic_channels(hsotg);
31478c2ecf20Sopenharmony_ci
31488c2ecf20Sopenharmony_ci	/* Process host channels associated with non-periodic transfers */
31498c2ecf20Sopenharmony_ci	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
31508c2ecf20Sopenharmony_ci	    tr_type == DWC2_TRANSACTION_ALL) {
31518c2ecf20Sopenharmony_ci		if (!list_empty(&hsotg->non_periodic_sched_active)) {
31528c2ecf20Sopenharmony_ci			dwc2_process_non_periodic_channels(hsotg);
31538c2ecf20Sopenharmony_ci		} else {
31548c2ecf20Sopenharmony_ci			/*
31558c2ecf20Sopenharmony_ci			 * Ensure NP Tx FIFO empty interrupt is disabled when
31568c2ecf20Sopenharmony_ci			 * there are no non-periodic transfers to process
31578c2ecf20Sopenharmony_ci			 */
31588c2ecf20Sopenharmony_ci			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
31598c2ecf20Sopenharmony_ci
31608c2ecf20Sopenharmony_ci			gintmsk &= ~GINTSTS_NPTXFEMP;
31618c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, gintmsk, GINTMSK);
31628c2ecf20Sopenharmony_ci		}
31638c2ecf20Sopenharmony_ci	}
31648c2ecf20Sopenharmony_ci}
31658c2ecf20Sopenharmony_ci
31668c2ecf20Sopenharmony_cistatic void dwc2_conn_id_status_change(struct work_struct *work)
31678c2ecf20Sopenharmony_ci{
31688c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
31698c2ecf20Sopenharmony_ci						wf_otg);
31708c2ecf20Sopenharmony_ci	u32 count = 0;
31718c2ecf20Sopenharmony_ci	u32 gotgctl;
31728c2ecf20Sopenharmony_ci	unsigned long flags;
31738c2ecf20Sopenharmony_ci
31748c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s()\n", __func__);
31758c2ecf20Sopenharmony_ci
31768c2ecf20Sopenharmony_ci	gotgctl = dwc2_readl(hsotg, GOTGCTL);
31778c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
31788c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
31798c2ecf20Sopenharmony_ci		!!(gotgctl & GOTGCTL_CONID_B));
31808c2ecf20Sopenharmony_ci
31818c2ecf20Sopenharmony_ci	/* B-Device connector (Device Mode) */
31828c2ecf20Sopenharmony_ci	if (gotgctl & GOTGCTL_CONID_B) {
31838c2ecf20Sopenharmony_ci		dwc2_vbus_supply_exit(hsotg);
31848c2ecf20Sopenharmony_ci		/* Wait for switch to device mode */
31858c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "connId B\n");
31868c2ecf20Sopenharmony_ci		if (hsotg->bus_suspended) {
31878c2ecf20Sopenharmony_ci			dev_info(hsotg->dev,
31888c2ecf20Sopenharmony_ci				 "Do port resume before switching to device mode\n");
31898c2ecf20Sopenharmony_ci			dwc2_port_resume(hsotg);
31908c2ecf20Sopenharmony_ci		}
31918c2ecf20Sopenharmony_ci		while (!dwc2_is_device_mode(hsotg)) {
31928c2ecf20Sopenharmony_ci			dev_info(hsotg->dev,
31938c2ecf20Sopenharmony_ci				 "Waiting for Peripheral Mode, Mode=%s\n",
31948c2ecf20Sopenharmony_ci				 dwc2_is_host_mode(hsotg) ? "Host" :
31958c2ecf20Sopenharmony_ci				 "Peripheral");
31968c2ecf20Sopenharmony_ci			msleep(20);
31978c2ecf20Sopenharmony_ci			/*
31988c2ecf20Sopenharmony_ci			 * Sometimes the initial GOTGCTRL read is wrong, so
31998c2ecf20Sopenharmony_ci			 * check it again and jump to host mode if that was
32008c2ecf20Sopenharmony_ci			 * the case.
32018c2ecf20Sopenharmony_ci			 */
32028c2ecf20Sopenharmony_ci			gotgctl = dwc2_readl(hsotg, GOTGCTL);
32038c2ecf20Sopenharmony_ci			if (!(gotgctl & GOTGCTL_CONID_B))
32048c2ecf20Sopenharmony_ci				goto host;
32058c2ecf20Sopenharmony_ci			if (++count > 250)
32068c2ecf20Sopenharmony_ci				break;
32078c2ecf20Sopenharmony_ci		}
32088c2ecf20Sopenharmony_ci		if (count > 250)
32098c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
32108c2ecf20Sopenharmony_ci				"Connection id status change timed out\n");
32118c2ecf20Sopenharmony_ci		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
32128c2ecf20Sopenharmony_ci		dwc2_core_init(hsotg, false);
32138c2ecf20Sopenharmony_ci		dwc2_enable_global_interrupts(hsotg);
32148c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
32158c2ecf20Sopenharmony_ci		dwc2_hsotg_core_init_disconnected(hsotg, false);
32168c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
32178c2ecf20Sopenharmony_ci		/* Enable ACG feature in device mode,if supported */
32188c2ecf20Sopenharmony_ci		dwc2_enable_acg(hsotg);
32198c2ecf20Sopenharmony_ci		dwc2_hsotg_core_connect(hsotg);
32208c2ecf20Sopenharmony_ci	} else {
32218c2ecf20Sopenharmony_cihost:
32228c2ecf20Sopenharmony_ci		/* A-Device connector (Host Mode) */
32238c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "connId A\n");
32248c2ecf20Sopenharmony_ci		while (!dwc2_is_host_mode(hsotg)) {
32258c2ecf20Sopenharmony_ci			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
32268c2ecf20Sopenharmony_ci				 dwc2_is_host_mode(hsotg) ?
32278c2ecf20Sopenharmony_ci				 "Host" : "Peripheral");
32288c2ecf20Sopenharmony_ci			msleep(20);
32298c2ecf20Sopenharmony_ci			if (++count > 250)
32308c2ecf20Sopenharmony_ci				break;
32318c2ecf20Sopenharmony_ci		}
32328c2ecf20Sopenharmony_ci		if (count > 250)
32338c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
32348c2ecf20Sopenharmony_ci				"Connection id status change timed out\n");
32358c2ecf20Sopenharmony_ci
32368c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
32378c2ecf20Sopenharmony_ci		dwc2_hsotg_disconnect(hsotg);
32388c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
32398c2ecf20Sopenharmony_ci
32408c2ecf20Sopenharmony_ci		hsotg->op_state = OTG_STATE_A_HOST;
32418c2ecf20Sopenharmony_ci		/* Initialize the Core for Host mode */
32428c2ecf20Sopenharmony_ci		dwc2_core_init(hsotg, false);
32438c2ecf20Sopenharmony_ci		dwc2_enable_global_interrupts(hsotg);
32448c2ecf20Sopenharmony_ci		dwc2_hcd_start(hsotg);
32458c2ecf20Sopenharmony_ci	}
32468c2ecf20Sopenharmony_ci}
32478c2ecf20Sopenharmony_ci
32488c2ecf20Sopenharmony_cistatic void dwc2_wakeup_detected(struct timer_list *t)
32498c2ecf20Sopenharmony_ci{
32508c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
32518c2ecf20Sopenharmony_ci	u32 hprt0;
32528c2ecf20Sopenharmony_ci
32538c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s()\n", __func__);
32548c2ecf20Sopenharmony_ci
32558c2ecf20Sopenharmony_ci	/*
32568c2ecf20Sopenharmony_ci	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
32578c2ecf20Sopenharmony_ci	 * so that OPT tests pass with all PHYs.)
32588c2ecf20Sopenharmony_ci	 */
32598c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
32608c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
32618c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_RES;
32628c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
32638c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
32648c2ecf20Sopenharmony_ci		dwc2_readl(hsotg, HPRT0));
32658c2ecf20Sopenharmony_ci
32668c2ecf20Sopenharmony_ci	dwc2_hcd_rem_wakeup(hsotg);
32678c2ecf20Sopenharmony_ci	hsotg->bus_suspended = false;
32688c2ecf20Sopenharmony_ci
32698c2ecf20Sopenharmony_ci	/* Change to L0 state */
32708c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L0;
32718c2ecf20Sopenharmony_ci}
32728c2ecf20Sopenharmony_ci
32738c2ecf20Sopenharmony_cistatic int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
32748c2ecf20Sopenharmony_ci{
32758c2ecf20Sopenharmony_ci	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
32768c2ecf20Sopenharmony_ci
32778c2ecf20Sopenharmony_ci	return hcd->self.b_hnp_enable;
32788c2ecf20Sopenharmony_ci}
32798c2ecf20Sopenharmony_ci
32808c2ecf20Sopenharmony_ci/* Must NOT be called with interrupt disabled or spinlock held */
32818c2ecf20Sopenharmony_cistatic void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
32828c2ecf20Sopenharmony_ci{
32838c2ecf20Sopenharmony_ci	unsigned long flags;
32848c2ecf20Sopenharmony_ci	u32 hprt0;
32858c2ecf20Sopenharmony_ci	u32 pcgctl;
32868c2ecf20Sopenharmony_ci	u32 gotgctl;
32878c2ecf20Sopenharmony_ci
32888c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s()\n", __func__);
32898c2ecf20Sopenharmony_ci
32908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
32918c2ecf20Sopenharmony_ci
32928c2ecf20Sopenharmony_ci	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
32938c2ecf20Sopenharmony_ci		gotgctl = dwc2_readl(hsotg, GOTGCTL);
32948c2ecf20Sopenharmony_ci		gotgctl |= GOTGCTL_HSTSETHNPEN;
32958c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gotgctl, GOTGCTL);
32968c2ecf20Sopenharmony_ci		hsotg->op_state = OTG_STATE_A_SUSPEND;
32978c2ecf20Sopenharmony_ci	}
32988c2ecf20Sopenharmony_ci
32998c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
33008c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_SUSP;
33018c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
33028c2ecf20Sopenharmony_ci
33038c2ecf20Sopenharmony_ci	hsotg->bus_suspended = true;
33048c2ecf20Sopenharmony_ci
33058c2ecf20Sopenharmony_ci	/*
33068c2ecf20Sopenharmony_ci	 * If power_down is supported, Phy clock will be suspended
33078c2ecf20Sopenharmony_ci	 * after registers are backuped.
33088c2ecf20Sopenharmony_ci	 */
33098c2ecf20Sopenharmony_ci	if (!hsotg->params.power_down) {
33108c2ecf20Sopenharmony_ci		/* Suspend the Phy Clock */
33118c2ecf20Sopenharmony_ci		pcgctl = dwc2_readl(hsotg, PCGCTL);
33128c2ecf20Sopenharmony_ci		pcgctl |= PCGCTL_STOPPCLK;
33138c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, pcgctl, PCGCTL);
33148c2ecf20Sopenharmony_ci		udelay(10);
33158c2ecf20Sopenharmony_ci	}
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_ci	/* For HNP the bus must be suspended for at least 200ms */
33188c2ecf20Sopenharmony_ci	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
33198c2ecf20Sopenharmony_ci		pcgctl = dwc2_readl(hsotg, PCGCTL);
33208c2ecf20Sopenharmony_ci		pcgctl &= ~PCGCTL_STOPPCLK;
33218c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, pcgctl, PCGCTL);
33228c2ecf20Sopenharmony_ci
33238c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
33248c2ecf20Sopenharmony_ci
33258c2ecf20Sopenharmony_ci		msleep(200);
33268c2ecf20Sopenharmony_ci	} else {
33278c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
33288c2ecf20Sopenharmony_ci	}
33298c2ecf20Sopenharmony_ci}
33308c2ecf20Sopenharmony_ci
33318c2ecf20Sopenharmony_ci/* Must NOT be called with interrupt disabled or spinlock held */
33328c2ecf20Sopenharmony_cistatic void dwc2_port_resume(struct dwc2_hsotg *hsotg)
33338c2ecf20Sopenharmony_ci{
33348c2ecf20Sopenharmony_ci	unsigned long flags;
33358c2ecf20Sopenharmony_ci	u32 hprt0;
33368c2ecf20Sopenharmony_ci	u32 pcgctl;
33378c2ecf20Sopenharmony_ci
33388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
33398c2ecf20Sopenharmony_ci
33408c2ecf20Sopenharmony_ci	/*
33418c2ecf20Sopenharmony_ci	 * If power_down is supported, Phy clock is already resumed
33428c2ecf20Sopenharmony_ci	 * after registers restore.
33438c2ecf20Sopenharmony_ci	 */
33448c2ecf20Sopenharmony_ci	if (!hsotg->params.power_down) {
33458c2ecf20Sopenharmony_ci		pcgctl = dwc2_readl(hsotg, PCGCTL);
33468c2ecf20Sopenharmony_ci		pcgctl &= ~PCGCTL_STOPPCLK;
33478c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, pcgctl, PCGCTL);
33488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
33498c2ecf20Sopenharmony_ci		msleep(20);
33508c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
33518c2ecf20Sopenharmony_ci	}
33528c2ecf20Sopenharmony_ci
33538c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
33548c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_RES;
33558c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_SUSP;
33568c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
33578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
33588c2ecf20Sopenharmony_ci
33598c2ecf20Sopenharmony_ci	msleep(USB_RESUME_TIMEOUT);
33608c2ecf20Sopenharmony_ci
33618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
33628c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
33638c2ecf20Sopenharmony_ci	hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
33648c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
33658c2ecf20Sopenharmony_ci	hsotg->bus_suspended = false;
33668c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
33678c2ecf20Sopenharmony_ci}
33688c2ecf20Sopenharmony_ci
33698c2ecf20Sopenharmony_ci/* Handles hub class-specific requests */
33708c2ecf20Sopenharmony_cistatic int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
33718c2ecf20Sopenharmony_ci				u16 wvalue, u16 windex, char *buf, u16 wlength)
33728c2ecf20Sopenharmony_ci{
33738c2ecf20Sopenharmony_ci	struct usb_hub_descriptor *hub_desc;
33748c2ecf20Sopenharmony_ci	int retval = 0;
33758c2ecf20Sopenharmony_ci	u32 hprt0;
33768c2ecf20Sopenharmony_ci	u32 port_status;
33778c2ecf20Sopenharmony_ci	u32 speed;
33788c2ecf20Sopenharmony_ci	u32 pcgctl;
33798c2ecf20Sopenharmony_ci	u32 pwr;
33808c2ecf20Sopenharmony_ci
33818c2ecf20Sopenharmony_ci	switch (typereq) {
33828c2ecf20Sopenharmony_ci	case ClearHubFeature:
33838c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
33848c2ecf20Sopenharmony_ci
33858c2ecf20Sopenharmony_ci		switch (wvalue) {
33868c2ecf20Sopenharmony_ci		case C_HUB_LOCAL_POWER:
33878c2ecf20Sopenharmony_ci		case C_HUB_OVER_CURRENT:
33888c2ecf20Sopenharmony_ci			/* Nothing required here */
33898c2ecf20Sopenharmony_ci			break;
33908c2ecf20Sopenharmony_ci
33918c2ecf20Sopenharmony_ci		default:
33928c2ecf20Sopenharmony_ci			retval = -EINVAL;
33938c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
33948c2ecf20Sopenharmony_ci				"ClearHubFeature request %1xh unknown\n",
33958c2ecf20Sopenharmony_ci				wvalue);
33968c2ecf20Sopenharmony_ci		}
33978c2ecf20Sopenharmony_ci		break;
33988c2ecf20Sopenharmony_ci
33998c2ecf20Sopenharmony_ci	case ClearPortFeature:
34008c2ecf20Sopenharmony_ci		if (wvalue != USB_PORT_FEAT_L1)
34018c2ecf20Sopenharmony_ci			if (!windex || windex > 1)
34028c2ecf20Sopenharmony_ci				goto error;
34038c2ecf20Sopenharmony_ci		switch (wvalue) {
34048c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_ENABLE:
34058c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34068c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
34078c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
34088c2ecf20Sopenharmony_ci			hprt0 |= HPRT0_ENA;
34098c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
34108c2ecf20Sopenharmony_ci			break;
34118c2ecf20Sopenharmony_ci
34128c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_SUSPEND:
34138c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34148c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
34158c2ecf20Sopenharmony_ci
34168c2ecf20Sopenharmony_ci			if (hsotg->bus_suspended) {
34178c2ecf20Sopenharmony_ci				if (hsotg->hibernated)
34188c2ecf20Sopenharmony_ci					dwc2_exit_hibernation(hsotg, 0, 0, 1);
34198c2ecf20Sopenharmony_ci				else
34208c2ecf20Sopenharmony_ci					dwc2_port_resume(hsotg);
34218c2ecf20Sopenharmony_ci			}
34228c2ecf20Sopenharmony_ci			break;
34238c2ecf20Sopenharmony_ci
34248c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_POWER:
34258c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34268c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_POWER\n");
34278c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
34288c2ecf20Sopenharmony_ci			pwr = hprt0 & HPRT0_PWR;
34298c2ecf20Sopenharmony_ci			hprt0 &= ~HPRT0_PWR;
34308c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
34318c2ecf20Sopenharmony_ci			if (pwr)
34328c2ecf20Sopenharmony_ci				dwc2_vbus_supply_exit(hsotg);
34338c2ecf20Sopenharmony_ci			break;
34348c2ecf20Sopenharmony_ci
34358c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_INDICATOR:
34368c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34378c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
34388c2ecf20Sopenharmony_ci			/* Port indicator not supported */
34398c2ecf20Sopenharmony_ci			break;
34408c2ecf20Sopenharmony_ci
34418c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_CONNECTION:
34428c2ecf20Sopenharmony_ci			/*
34438c2ecf20Sopenharmony_ci			 * Clears driver's internal Connect Status Change flag
34448c2ecf20Sopenharmony_ci			 */
34458c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34468c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
34478c2ecf20Sopenharmony_ci			hsotg->flags.b.port_connect_status_change = 0;
34488c2ecf20Sopenharmony_ci			break;
34498c2ecf20Sopenharmony_ci
34508c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_RESET:
34518c2ecf20Sopenharmony_ci			/* Clears driver's internal Port Reset Change flag */
34528c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34538c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
34548c2ecf20Sopenharmony_ci			hsotg->flags.b.port_reset_change = 0;
34558c2ecf20Sopenharmony_ci			break;
34568c2ecf20Sopenharmony_ci
34578c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_ENABLE:
34588c2ecf20Sopenharmony_ci			/*
34598c2ecf20Sopenharmony_ci			 * Clears the driver's internal Port Enable/Disable
34608c2ecf20Sopenharmony_ci			 * Change flag
34618c2ecf20Sopenharmony_ci			 */
34628c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34638c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
34648c2ecf20Sopenharmony_ci			hsotg->flags.b.port_enable_change = 0;
34658c2ecf20Sopenharmony_ci			break;
34668c2ecf20Sopenharmony_ci
34678c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_SUSPEND:
34688c2ecf20Sopenharmony_ci			/*
34698c2ecf20Sopenharmony_ci			 * Clears the driver's internal Port Suspend Change
34708c2ecf20Sopenharmony_ci			 * flag, which is set when resume signaling on the host
34718c2ecf20Sopenharmony_ci			 * port is complete
34728c2ecf20Sopenharmony_ci			 */
34738c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34748c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
34758c2ecf20Sopenharmony_ci			hsotg->flags.b.port_suspend_change = 0;
34768c2ecf20Sopenharmony_ci			break;
34778c2ecf20Sopenharmony_ci
34788c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_PORT_L1:
34798c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34808c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
34818c2ecf20Sopenharmony_ci			hsotg->flags.b.port_l1_change = 0;
34828c2ecf20Sopenharmony_ci			break;
34838c2ecf20Sopenharmony_ci
34848c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_C_OVER_CURRENT:
34858c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
34868c2ecf20Sopenharmony_ci				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
34878c2ecf20Sopenharmony_ci			hsotg->flags.b.port_over_current_change = 0;
34888c2ecf20Sopenharmony_ci			break;
34898c2ecf20Sopenharmony_ci
34908c2ecf20Sopenharmony_ci		default:
34918c2ecf20Sopenharmony_ci			retval = -EINVAL;
34928c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
34938c2ecf20Sopenharmony_ci				"ClearPortFeature request %1xh unknown or unsupported\n",
34948c2ecf20Sopenharmony_ci				wvalue);
34958c2ecf20Sopenharmony_ci		}
34968c2ecf20Sopenharmony_ci		break;
34978c2ecf20Sopenharmony_ci
34988c2ecf20Sopenharmony_ci	case GetHubDescriptor:
34998c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
35008c2ecf20Sopenharmony_ci		hub_desc = (struct usb_hub_descriptor *)buf;
35018c2ecf20Sopenharmony_ci		hub_desc->bDescLength = 9;
35028c2ecf20Sopenharmony_ci		hub_desc->bDescriptorType = USB_DT_HUB;
35038c2ecf20Sopenharmony_ci		hub_desc->bNbrPorts = 1;
35048c2ecf20Sopenharmony_ci		hub_desc->wHubCharacteristics =
35058c2ecf20Sopenharmony_ci			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35068c2ecf20Sopenharmony_ci				    HUB_CHAR_INDV_PORT_OCPM);
35078c2ecf20Sopenharmony_ci		hub_desc->bPwrOn2PwrGood = 1;
35088c2ecf20Sopenharmony_ci		hub_desc->bHubContrCurrent = 0;
35098c2ecf20Sopenharmony_ci		hub_desc->u.hs.DeviceRemovable[0] = 0;
35108c2ecf20Sopenharmony_ci		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
35118c2ecf20Sopenharmony_ci		break;
35128c2ecf20Sopenharmony_ci
35138c2ecf20Sopenharmony_ci	case GetHubStatus:
35148c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "GetHubStatus\n");
35158c2ecf20Sopenharmony_ci		memset(buf, 0, 4);
35168c2ecf20Sopenharmony_ci		break;
35178c2ecf20Sopenharmony_ci
35188c2ecf20Sopenharmony_ci	case GetPortStatus:
35198c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
35208c2ecf20Sopenharmony_ci			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
35218c2ecf20Sopenharmony_ci			 hsotg->flags.d32);
35228c2ecf20Sopenharmony_ci		if (!windex || windex > 1)
35238c2ecf20Sopenharmony_ci			goto error;
35248c2ecf20Sopenharmony_ci
35258c2ecf20Sopenharmony_ci		port_status = 0;
35268c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_connect_status_change)
35278c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
35288c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_enable_change)
35298c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_ENABLE << 16;
35308c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_suspend_change)
35318c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
35328c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_l1_change)
35338c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_L1 << 16;
35348c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_reset_change)
35358c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_RESET << 16;
35368c2ecf20Sopenharmony_ci		if (hsotg->flags.b.port_over_current_change) {
35378c2ecf20Sopenharmony_ci			dev_warn(hsotg->dev, "Overcurrent change detected\n");
35388c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
35398c2ecf20Sopenharmony_ci		}
35408c2ecf20Sopenharmony_ci
35418c2ecf20Sopenharmony_ci		if (!hsotg->flags.b.port_connect_status) {
35428c2ecf20Sopenharmony_ci			/*
35438c2ecf20Sopenharmony_ci			 * The port is disconnected, which means the core is
35448c2ecf20Sopenharmony_ci			 * either in device mode or it soon will be. Just
35458c2ecf20Sopenharmony_ci			 * return 0's for the remainder of the port status
35468c2ecf20Sopenharmony_ci			 * since the port register can't be read if the core
35478c2ecf20Sopenharmony_ci			 * is in device mode.
35488c2ecf20Sopenharmony_ci			 */
35498c2ecf20Sopenharmony_ci			*(__le32 *)buf = cpu_to_le32(port_status);
35508c2ecf20Sopenharmony_ci			break;
35518c2ecf20Sopenharmony_ci		}
35528c2ecf20Sopenharmony_ci
35538c2ecf20Sopenharmony_ci		hprt0 = dwc2_readl(hsotg, HPRT0);
35548c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
35558c2ecf20Sopenharmony_ci
35568c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_CONNSTS)
35578c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_CONNECTION;
35588c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_ENA)
35598c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_ENABLE;
35608c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_SUSP)
35618c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_SUSPEND;
35628c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_OVRCURRACT)
35638c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_OVERCURRENT;
35648c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_RST)
35658c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_RESET;
35668c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_PWR)
35678c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_POWER;
35688c2ecf20Sopenharmony_ci
35698c2ecf20Sopenharmony_ci		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
35708c2ecf20Sopenharmony_ci		if (speed == HPRT0_SPD_HIGH_SPEED)
35718c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_HIGH_SPEED;
35728c2ecf20Sopenharmony_ci		else if (speed == HPRT0_SPD_LOW_SPEED)
35738c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_LOW_SPEED;
35748c2ecf20Sopenharmony_ci
35758c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_TSTCTL_MASK)
35768c2ecf20Sopenharmony_ci			port_status |= USB_PORT_STAT_TEST;
35778c2ecf20Sopenharmony_ci		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
35788c2ecf20Sopenharmony_ci
35798c2ecf20Sopenharmony_ci		if (hsotg->params.dma_desc_fs_enable) {
35808c2ecf20Sopenharmony_ci			/*
35818c2ecf20Sopenharmony_ci			 * Enable descriptor DMA only if a full speed
35828c2ecf20Sopenharmony_ci			 * device is connected.
35838c2ecf20Sopenharmony_ci			 */
35848c2ecf20Sopenharmony_ci			if (hsotg->new_connection &&
35858c2ecf20Sopenharmony_ci			    ((port_status &
35868c2ecf20Sopenharmony_ci			      (USB_PORT_STAT_CONNECTION |
35878c2ecf20Sopenharmony_ci			       USB_PORT_STAT_HIGH_SPEED |
35888c2ecf20Sopenharmony_ci			       USB_PORT_STAT_LOW_SPEED)) ==
35898c2ecf20Sopenharmony_ci			       USB_PORT_STAT_CONNECTION)) {
35908c2ecf20Sopenharmony_ci				u32 hcfg;
35918c2ecf20Sopenharmony_ci
35928c2ecf20Sopenharmony_ci				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
35938c2ecf20Sopenharmony_ci				hsotg->params.dma_desc_enable = true;
35948c2ecf20Sopenharmony_ci				hcfg = dwc2_readl(hsotg, HCFG);
35958c2ecf20Sopenharmony_ci				hcfg |= HCFG_DESCDMA;
35968c2ecf20Sopenharmony_ci				dwc2_writel(hsotg, hcfg, HCFG);
35978c2ecf20Sopenharmony_ci				hsotg->new_connection = false;
35988c2ecf20Sopenharmony_ci			}
35998c2ecf20Sopenharmony_ci		}
36008c2ecf20Sopenharmony_ci
36018c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
36028c2ecf20Sopenharmony_ci		*(__le32 *)buf = cpu_to_le32(port_status);
36038c2ecf20Sopenharmony_ci		break;
36048c2ecf20Sopenharmony_ci
36058c2ecf20Sopenharmony_ci	case SetHubFeature:
36068c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "SetHubFeature\n");
36078c2ecf20Sopenharmony_ci		/* No HUB features supported */
36088c2ecf20Sopenharmony_ci		break;
36098c2ecf20Sopenharmony_ci
36108c2ecf20Sopenharmony_ci	case SetPortFeature:
36118c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "SetPortFeature\n");
36128c2ecf20Sopenharmony_ci		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
36138c2ecf20Sopenharmony_ci			goto error;
36148c2ecf20Sopenharmony_ci
36158c2ecf20Sopenharmony_ci		if (!hsotg->flags.b.port_connect_status) {
36168c2ecf20Sopenharmony_ci			/*
36178c2ecf20Sopenharmony_ci			 * The port is disconnected, which means the core is
36188c2ecf20Sopenharmony_ci			 * either in device mode or it soon will be. Just
36198c2ecf20Sopenharmony_ci			 * return without doing anything since the port
36208c2ecf20Sopenharmony_ci			 * register can't be written if the core is in device
36218c2ecf20Sopenharmony_ci			 * mode.
36228c2ecf20Sopenharmony_ci			 */
36238c2ecf20Sopenharmony_ci			break;
36248c2ecf20Sopenharmony_ci		}
36258c2ecf20Sopenharmony_ci
36268c2ecf20Sopenharmony_ci		switch (wvalue) {
36278c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_SUSPEND:
36288c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
36298c2ecf20Sopenharmony_ci				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
36308c2ecf20Sopenharmony_ci			if (windex != hsotg->otg_port)
36318c2ecf20Sopenharmony_ci				goto error;
36328c2ecf20Sopenharmony_ci			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_HIBERNATION)
36338c2ecf20Sopenharmony_ci				dwc2_enter_hibernation(hsotg, 1);
36348c2ecf20Sopenharmony_ci			else
36358c2ecf20Sopenharmony_ci				dwc2_port_suspend(hsotg, windex);
36368c2ecf20Sopenharmony_ci			break;
36378c2ecf20Sopenharmony_ci
36388c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_POWER:
36398c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
36408c2ecf20Sopenharmony_ci				"SetPortFeature - USB_PORT_FEAT_POWER\n");
36418c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
36428c2ecf20Sopenharmony_ci			pwr = hprt0 & HPRT0_PWR;
36438c2ecf20Sopenharmony_ci			hprt0 |= HPRT0_PWR;
36448c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
36458c2ecf20Sopenharmony_ci			if (!pwr)
36468c2ecf20Sopenharmony_ci				dwc2_vbus_supply_init(hsotg);
36478c2ecf20Sopenharmony_ci			break;
36488c2ecf20Sopenharmony_ci
36498c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_RESET:
36508c2ecf20Sopenharmony_ci			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_HIBERNATION &&
36518c2ecf20Sopenharmony_ci			    hsotg->hibernated)
36528c2ecf20Sopenharmony_ci				dwc2_exit_hibernation(hsotg, 0, 1, 1);
36538c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
36548c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
36558c2ecf20Sopenharmony_ci				"SetPortFeature - USB_PORT_FEAT_RESET\n");
36568c2ecf20Sopenharmony_ci			pcgctl = dwc2_readl(hsotg, PCGCTL);
36578c2ecf20Sopenharmony_ci			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
36588c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, pcgctl, PCGCTL);
36598c2ecf20Sopenharmony_ci			/* ??? Original driver does this */
36608c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, 0, PCGCTL);
36618c2ecf20Sopenharmony_ci
36628c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
36638c2ecf20Sopenharmony_ci			pwr = hprt0 & HPRT0_PWR;
36648c2ecf20Sopenharmony_ci			/* Clear suspend bit if resetting from suspend state */
36658c2ecf20Sopenharmony_ci			hprt0 &= ~HPRT0_SUSP;
36668c2ecf20Sopenharmony_ci
36678c2ecf20Sopenharmony_ci			/*
36688c2ecf20Sopenharmony_ci			 * When B-Host the Port reset bit is set in the Start
36698c2ecf20Sopenharmony_ci			 * HCD Callback function, so that the reset is started
36708c2ecf20Sopenharmony_ci			 * within 1ms of the HNP success interrupt
36718c2ecf20Sopenharmony_ci			 */
36728c2ecf20Sopenharmony_ci			if (!dwc2_hcd_is_b_host(hsotg)) {
36738c2ecf20Sopenharmony_ci				hprt0 |= HPRT0_PWR | HPRT0_RST;
36748c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
36758c2ecf20Sopenharmony_ci					"In host mode, hprt0=%08x\n", hprt0);
36768c2ecf20Sopenharmony_ci				dwc2_writel(hsotg, hprt0, HPRT0);
36778c2ecf20Sopenharmony_ci				if (!pwr)
36788c2ecf20Sopenharmony_ci					dwc2_vbus_supply_init(hsotg);
36798c2ecf20Sopenharmony_ci			}
36808c2ecf20Sopenharmony_ci
36818c2ecf20Sopenharmony_ci			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
36828c2ecf20Sopenharmony_ci			msleep(50);
36838c2ecf20Sopenharmony_ci			hprt0 &= ~HPRT0_RST;
36848c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
36858c2ecf20Sopenharmony_ci			hsotg->lx_state = DWC2_L0; /* Now back to On state */
36868c2ecf20Sopenharmony_ci			break;
36878c2ecf20Sopenharmony_ci
36888c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_INDICATOR:
36898c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
36908c2ecf20Sopenharmony_ci				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
36918c2ecf20Sopenharmony_ci			/* Not supported */
36928c2ecf20Sopenharmony_ci			break;
36938c2ecf20Sopenharmony_ci
36948c2ecf20Sopenharmony_ci		case USB_PORT_FEAT_TEST:
36958c2ecf20Sopenharmony_ci			hprt0 = dwc2_read_hprt0(hsotg);
36968c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev,
36978c2ecf20Sopenharmony_ci				"SetPortFeature - USB_PORT_FEAT_TEST\n");
36988c2ecf20Sopenharmony_ci			hprt0 &= ~HPRT0_TSTCTL_MASK;
36998c2ecf20Sopenharmony_ci			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
37008c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
37018c2ecf20Sopenharmony_ci			break;
37028c2ecf20Sopenharmony_ci
37038c2ecf20Sopenharmony_ci		default:
37048c2ecf20Sopenharmony_ci			retval = -EINVAL;
37058c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
37068c2ecf20Sopenharmony_ci				"SetPortFeature %1xh unknown or unsupported\n",
37078c2ecf20Sopenharmony_ci				wvalue);
37088c2ecf20Sopenharmony_ci			break;
37098c2ecf20Sopenharmony_ci		}
37108c2ecf20Sopenharmony_ci		break;
37118c2ecf20Sopenharmony_ci
37128c2ecf20Sopenharmony_ci	default:
37138c2ecf20Sopenharmony_cierror:
37148c2ecf20Sopenharmony_ci		retval = -EINVAL;
37158c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev,
37168c2ecf20Sopenharmony_ci			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
37178c2ecf20Sopenharmony_ci			typereq, windex, wvalue);
37188c2ecf20Sopenharmony_ci		break;
37198c2ecf20Sopenharmony_ci	}
37208c2ecf20Sopenharmony_ci
37218c2ecf20Sopenharmony_ci	return retval;
37228c2ecf20Sopenharmony_ci}
37238c2ecf20Sopenharmony_ci
37248c2ecf20Sopenharmony_cistatic int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
37258c2ecf20Sopenharmony_ci{
37268c2ecf20Sopenharmony_ci	int retval;
37278c2ecf20Sopenharmony_ci
37288c2ecf20Sopenharmony_ci	if (port != 1)
37298c2ecf20Sopenharmony_ci		return -EINVAL;
37308c2ecf20Sopenharmony_ci
37318c2ecf20Sopenharmony_ci	retval = (hsotg->flags.b.port_connect_status_change ||
37328c2ecf20Sopenharmony_ci		  hsotg->flags.b.port_reset_change ||
37338c2ecf20Sopenharmony_ci		  hsotg->flags.b.port_enable_change ||
37348c2ecf20Sopenharmony_ci		  hsotg->flags.b.port_suspend_change ||
37358c2ecf20Sopenharmony_ci		  hsotg->flags.b.port_over_current_change);
37368c2ecf20Sopenharmony_ci
37378c2ecf20Sopenharmony_ci	if (retval) {
37388c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev,
37398c2ecf20Sopenharmony_ci			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
37408c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
37418c2ecf20Sopenharmony_ci			hsotg->flags.b.port_connect_status_change);
37428c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
37438c2ecf20Sopenharmony_ci			hsotg->flags.b.port_reset_change);
37448c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
37458c2ecf20Sopenharmony_ci			hsotg->flags.b.port_enable_change);
37468c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
37478c2ecf20Sopenharmony_ci			hsotg->flags.b.port_suspend_change);
37488c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
37498c2ecf20Sopenharmony_ci			hsotg->flags.b.port_over_current_change);
37508c2ecf20Sopenharmony_ci	}
37518c2ecf20Sopenharmony_ci
37528c2ecf20Sopenharmony_ci	return retval;
37538c2ecf20Sopenharmony_ci}
37548c2ecf20Sopenharmony_ci
37558c2ecf20Sopenharmony_ciint dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
37568c2ecf20Sopenharmony_ci{
37578c2ecf20Sopenharmony_ci	u32 hfnum = dwc2_readl(hsotg, HFNUM);
37588c2ecf20Sopenharmony_ci
37598c2ecf20Sopenharmony_ci#ifdef DWC2_DEBUG_SOF
37608c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
37618c2ecf20Sopenharmony_ci		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
37628c2ecf20Sopenharmony_ci#endif
37638c2ecf20Sopenharmony_ci	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
37648c2ecf20Sopenharmony_ci}
37658c2ecf20Sopenharmony_ci
37668c2ecf20Sopenharmony_ciint dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
37678c2ecf20Sopenharmony_ci{
37688c2ecf20Sopenharmony_ci	u32 hprt = dwc2_readl(hsotg, HPRT0);
37698c2ecf20Sopenharmony_ci	u32 hfir = dwc2_readl(hsotg, HFIR);
37708c2ecf20Sopenharmony_ci	u32 hfnum = dwc2_readl(hsotg, HFNUM);
37718c2ecf20Sopenharmony_ci	unsigned int us_per_frame;
37728c2ecf20Sopenharmony_ci	unsigned int frame_number;
37738c2ecf20Sopenharmony_ci	unsigned int remaining;
37748c2ecf20Sopenharmony_ci	unsigned int interval;
37758c2ecf20Sopenharmony_ci	unsigned int phy_clks;
37768c2ecf20Sopenharmony_ci
37778c2ecf20Sopenharmony_ci	/* High speed has 125 us per (micro) frame; others are 1 ms per */
37788c2ecf20Sopenharmony_ci	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
37798c2ecf20Sopenharmony_ci
37808c2ecf20Sopenharmony_ci	/* Extract fields */
37818c2ecf20Sopenharmony_ci	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
37828c2ecf20Sopenharmony_ci	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
37838c2ecf20Sopenharmony_ci	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
37848c2ecf20Sopenharmony_ci
37858c2ecf20Sopenharmony_ci	/*
37868c2ecf20Sopenharmony_ci	 * Number of phy clocks since the last tick of the frame number after
37878c2ecf20Sopenharmony_ci	 * "us" has passed.
37888c2ecf20Sopenharmony_ci	 */
37898c2ecf20Sopenharmony_ci	phy_clks = (interval - remaining) +
37908c2ecf20Sopenharmony_ci		   DIV_ROUND_UP(interval * us, us_per_frame);
37918c2ecf20Sopenharmony_ci
37928c2ecf20Sopenharmony_ci	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
37938c2ecf20Sopenharmony_ci}
37948c2ecf20Sopenharmony_ci
37958c2ecf20Sopenharmony_ciint dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
37968c2ecf20Sopenharmony_ci{
37978c2ecf20Sopenharmony_ci	return hsotg->op_state == OTG_STATE_B_HOST;
37988c2ecf20Sopenharmony_ci}
37998c2ecf20Sopenharmony_ci
38008c2ecf20Sopenharmony_cistatic struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
38018c2ecf20Sopenharmony_ci					       int iso_desc_count,
38028c2ecf20Sopenharmony_ci					       gfp_t mem_flags)
38038c2ecf20Sopenharmony_ci{
38048c2ecf20Sopenharmony_ci	struct dwc2_hcd_urb *urb;
38058c2ecf20Sopenharmony_ci
38068c2ecf20Sopenharmony_ci	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
38078c2ecf20Sopenharmony_ci	if (urb)
38088c2ecf20Sopenharmony_ci		urb->packet_count = iso_desc_count;
38098c2ecf20Sopenharmony_ci	return urb;
38108c2ecf20Sopenharmony_ci}
38118c2ecf20Sopenharmony_ci
38128c2ecf20Sopenharmony_cistatic void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
38138c2ecf20Sopenharmony_ci				      struct dwc2_hcd_urb *urb, u8 dev_addr,
38148c2ecf20Sopenharmony_ci				      u8 ep_num, u8 ep_type, u8 ep_dir,
38158c2ecf20Sopenharmony_ci				      u16 maxp, u16 maxp_mult)
38168c2ecf20Sopenharmony_ci{
38178c2ecf20Sopenharmony_ci	if (dbg_perio() ||
38188c2ecf20Sopenharmony_ci	    ep_type == USB_ENDPOINT_XFER_BULK ||
38198c2ecf20Sopenharmony_ci	    ep_type == USB_ENDPOINT_XFER_CONTROL)
38208c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
38218c2ecf20Sopenharmony_ci			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
38228c2ecf20Sopenharmony_ci			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
38238c2ecf20Sopenharmony_ci	urb->pipe_info.dev_addr = dev_addr;
38248c2ecf20Sopenharmony_ci	urb->pipe_info.ep_num = ep_num;
38258c2ecf20Sopenharmony_ci	urb->pipe_info.pipe_type = ep_type;
38268c2ecf20Sopenharmony_ci	urb->pipe_info.pipe_dir = ep_dir;
38278c2ecf20Sopenharmony_ci	urb->pipe_info.maxp = maxp;
38288c2ecf20Sopenharmony_ci	urb->pipe_info.maxp_mult = maxp_mult;
38298c2ecf20Sopenharmony_ci}
38308c2ecf20Sopenharmony_ci
38318c2ecf20Sopenharmony_ci/*
38328c2ecf20Sopenharmony_ci * NOTE: This function will be removed once the peripheral controller code
38338c2ecf20Sopenharmony_ci * is integrated and the driver is stable
38348c2ecf20Sopenharmony_ci */
38358c2ecf20Sopenharmony_civoid dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
38368c2ecf20Sopenharmony_ci{
38378c2ecf20Sopenharmony_ci#ifdef DEBUG
38388c2ecf20Sopenharmony_ci	struct dwc2_host_chan *chan;
38398c2ecf20Sopenharmony_ci	struct dwc2_hcd_urb *urb;
38408c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd;
38418c2ecf20Sopenharmony_ci	int num_channels;
38428c2ecf20Sopenharmony_ci	u32 np_tx_status;
38438c2ecf20Sopenharmony_ci	u32 p_tx_status;
38448c2ecf20Sopenharmony_ci	int i;
38458c2ecf20Sopenharmony_ci
38468c2ecf20Sopenharmony_ci	num_channels = hsotg->params.host_channels;
38478c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "\n");
38488c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev,
38498c2ecf20Sopenharmony_ci		"************************************************************\n");
38508c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "HCD State:\n");
38518c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
38528c2ecf20Sopenharmony_ci
38538c2ecf20Sopenharmony_ci	for (i = 0; i < num_channels; i++) {
38548c2ecf20Sopenharmony_ci		chan = hsotg->hc_ptr_array[i];
38558c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
38568c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev,
38578c2ecf20Sopenharmony_ci			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
38588c2ecf20Sopenharmony_ci			chan->dev_addr, chan->ep_num, chan->ep_is_in);
38598c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
38608c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
38618c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
38628c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
38638c2ecf20Sopenharmony_ci			chan->data_pid_start);
38648c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
38658c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
38668c2ecf20Sopenharmony_ci			chan->xfer_started);
38678c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
38688c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
38698c2ecf20Sopenharmony_ci			(unsigned long)chan->xfer_dma);
38708c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
38718c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
38728c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
38738c2ecf20Sopenharmony_ci			chan->halt_on_queue);
38748c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
38758c2ecf20Sopenharmony_ci			chan->halt_pending);
38768c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
38778c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
38788c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    complete_split: %d\n",
38798c2ecf20Sopenharmony_ci			chan->complete_split);
38808c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
38818c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
38828c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
38838c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
38848c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
38858c2ecf20Sopenharmony_ci
38868c2ecf20Sopenharmony_ci		if (chan->xfer_started) {
38878c2ecf20Sopenharmony_ci			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
38888c2ecf20Sopenharmony_ci
38898c2ecf20Sopenharmony_ci			hfnum = dwc2_readl(hsotg, HFNUM);
38908c2ecf20Sopenharmony_ci			hcchar = dwc2_readl(hsotg, HCCHAR(i));
38918c2ecf20Sopenharmony_ci			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
38928c2ecf20Sopenharmony_ci			hcint = dwc2_readl(hsotg, HCINT(i));
38938c2ecf20Sopenharmony_ci			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
38948c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
38958c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
38968c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
38978c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
38988c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
38998c2ecf20Sopenharmony_ci		}
39008c2ecf20Sopenharmony_ci
39018c2ecf20Sopenharmony_ci		if (!(chan->xfer_started && chan->qh))
39028c2ecf20Sopenharmony_ci			continue;
39038c2ecf20Sopenharmony_ci
39048c2ecf20Sopenharmony_ci		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
39058c2ecf20Sopenharmony_ci			if (!qtd->in_process)
39068c2ecf20Sopenharmony_ci				break;
39078c2ecf20Sopenharmony_ci			urb = qtd->urb;
39088c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "    URB Info:\n");
39098c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
39108c2ecf20Sopenharmony_ci				qtd, urb);
39118c2ecf20Sopenharmony_ci			if (urb) {
39128c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
39138c2ecf20Sopenharmony_ci					"      Dev: %d, EP: %d %s\n",
39148c2ecf20Sopenharmony_ci					dwc2_hcd_get_dev_addr(&urb->pipe_info),
39158c2ecf20Sopenharmony_ci					dwc2_hcd_get_ep_num(&urb->pipe_info),
39168c2ecf20Sopenharmony_ci					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
39178c2ecf20Sopenharmony_ci					"IN" : "OUT");
39188c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
39198c2ecf20Sopenharmony_ci					"      Max packet size: %d (%d mult)\n",
39208c2ecf20Sopenharmony_ci					dwc2_hcd_get_maxp(&urb->pipe_info),
39218c2ecf20Sopenharmony_ci					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
39228c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
39238c2ecf20Sopenharmony_ci					"      transfer_buffer: %p\n",
39248c2ecf20Sopenharmony_ci					urb->buf);
39258c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
39268c2ecf20Sopenharmony_ci					"      transfer_dma: %08lx\n",
39278c2ecf20Sopenharmony_ci					(unsigned long)urb->dma);
39288c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev,
39298c2ecf20Sopenharmony_ci					"      transfer_buffer_length: %d\n",
39308c2ecf20Sopenharmony_ci					urb->length);
39318c2ecf20Sopenharmony_ci				dev_dbg(hsotg->dev, "      actual_length: %d\n",
39328c2ecf20Sopenharmony_ci					urb->actual_length);
39338c2ecf20Sopenharmony_ci			}
39348c2ecf20Sopenharmony_ci		}
39358c2ecf20Sopenharmony_ci	}
39368c2ecf20Sopenharmony_ci
39378c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
39388c2ecf20Sopenharmony_ci		hsotg->non_periodic_channels);
39398c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
39408c2ecf20Sopenharmony_ci		hsotg->periodic_channels);
39418c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
39428c2ecf20Sopenharmony_ci	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
39438c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
39448c2ecf20Sopenharmony_ci		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
39458c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
39468c2ecf20Sopenharmony_ci		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
39478c2ecf20Sopenharmony_ci	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
39488c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
39498c2ecf20Sopenharmony_ci		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
39508c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
39518c2ecf20Sopenharmony_ci		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
39528c2ecf20Sopenharmony_ci	dwc2_dump_global_registers(hsotg);
39538c2ecf20Sopenharmony_ci	dwc2_dump_host_registers(hsotg);
39548c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev,
39558c2ecf20Sopenharmony_ci		"************************************************************\n");
39568c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "\n");
39578c2ecf20Sopenharmony_ci#endif
39588c2ecf20Sopenharmony_ci}
39598c2ecf20Sopenharmony_ci
39608c2ecf20Sopenharmony_cistruct wrapper_priv_data {
39618c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg;
39628c2ecf20Sopenharmony_ci};
39638c2ecf20Sopenharmony_ci
39648c2ecf20Sopenharmony_ci/* Gets the dwc2_hsotg from a usb_hcd */
39658c2ecf20Sopenharmony_cistatic struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
39668c2ecf20Sopenharmony_ci{
39678c2ecf20Sopenharmony_ci	struct wrapper_priv_data *p;
39688c2ecf20Sopenharmony_ci
39698c2ecf20Sopenharmony_ci	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
39708c2ecf20Sopenharmony_ci	return p->hsotg;
39718c2ecf20Sopenharmony_ci}
39728c2ecf20Sopenharmony_ci
39738c2ecf20Sopenharmony_ci/**
39748c2ecf20Sopenharmony_ci * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
39758c2ecf20Sopenharmony_ci *
39768c2ecf20Sopenharmony_ci * This will get the dwc2_tt structure (and ttport) associated with the given
39778c2ecf20Sopenharmony_ci * context (which is really just a struct urb pointer).
39788c2ecf20Sopenharmony_ci *
39798c2ecf20Sopenharmony_ci * The first time this is called for a given TT we allocate memory for our
39808c2ecf20Sopenharmony_ci * structure.  When everyone is done and has called dwc2_host_put_tt_info()
39818c2ecf20Sopenharmony_ci * then the refcount for the structure will go to 0 and we'll free it.
39828c2ecf20Sopenharmony_ci *
39838c2ecf20Sopenharmony_ci * @hsotg:     The HCD state structure for the DWC OTG controller.
39848c2ecf20Sopenharmony_ci * @context:   The priv pointer from a struct dwc2_hcd_urb.
39858c2ecf20Sopenharmony_ci * @mem_flags: Flags for allocating memory.
39868c2ecf20Sopenharmony_ci * @ttport:    We'll return this device's port number here.  That's used to
39878c2ecf20Sopenharmony_ci *             reference into the bitmap if we're on a multi_tt hub.
39888c2ecf20Sopenharmony_ci *
39898c2ecf20Sopenharmony_ci * Return: a pointer to a struct dwc2_tt.  Don't forget to call
39908c2ecf20Sopenharmony_ci *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
39918c2ecf20Sopenharmony_ci */
39928c2ecf20Sopenharmony_ci
39938c2ecf20Sopenharmony_cistruct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
39948c2ecf20Sopenharmony_ci				      gfp_t mem_flags, int *ttport)
39958c2ecf20Sopenharmony_ci{
39968c2ecf20Sopenharmony_ci	struct urb *urb = context;
39978c2ecf20Sopenharmony_ci	struct dwc2_tt *dwc_tt = NULL;
39988c2ecf20Sopenharmony_ci
39998c2ecf20Sopenharmony_ci	if (urb->dev->tt) {
40008c2ecf20Sopenharmony_ci		*ttport = urb->dev->ttport;
40018c2ecf20Sopenharmony_ci
40028c2ecf20Sopenharmony_ci		dwc_tt = urb->dev->tt->hcpriv;
40038c2ecf20Sopenharmony_ci		if (!dwc_tt) {
40048c2ecf20Sopenharmony_ci			size_t bitmap_size;
40058c2ecf20Sopenharmony_ci
40068c2ecf20Sopenharmony_ci			/*
40078c2ecf20Sopenharmony_ci			 * For single_tt we need one schedule.  For multi_tt
40088c2ecf20Sopenharmony_ci			 * we need one per port.
40098c2ecf20Sopenharmony_ci			 */
40108c2ecf20Sopenharmony_ci			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40118c2ecf20Sopenharmony_ci				      sizeof(dwc_tt->periodic_bitmaps[0]);
40128c2ecf20Sopenharmony_ci			if (urb->dev->tt->multi)
40138c2ecf20Sopenharmony_ci				bitmap_size *= urb->dev->tt->hub->maxchild;
40148c2ecf20Sopenharmony_ci
40158c2ecf20Sopenharmony_ci			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40168c2ecf20Sopenharmony_ci					 mem_flags);
40178c2ecf20Sopenharmony_ci			if (!dwc_tt)
40188c2ecf20Sopenharmony_ci				return NULL;
40198c2ecf20Sopenharmony_ci
40208c2ecf20Sopenharmony_ci			dwc_tt->usb_tt = urb->dev->tt;
40218c2ecf20Sopenharmony_ci			dwc_tt->usb_tt->hcpriv = dwc_tt;
40228c2ecf20Sopenharmony_ci		}
40238c2ecf20Sopenharmony_ci
40248c2ecf20Sopenharmony_ci		dwc_tt->refcount++;
40258c2ecf20Sopenharmony_ci	}
40268c2ecf20Sopenharmony_ci
40278c2ecf20Sopenharmony_ci	return dwc_tt;
40288c2ecf20Sopenharmony_ci}
40298c2ecf20Sopenharmony_ci
40308c2ecf20Sopenharmony_ci/**
40318c2ecf20Sopenharmony_ci * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40328c2ecf20Sopenharmony_ci *
40338c2ecf20Sopenharmony_ci * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40348c2ecf20Sopenharmony_ci * of the structure are done.
40358c2ecf20Sopenharmony_ci *
40368c2ecf20Sopenharmony_ci * It's OK to call this with NULL.
40378c2ecf20Sopenharmony_ci *
40388c2ecf20Sopenharmony_ci * @hsotg:     The HCD state structure for the DWC OTG controller.
40398c2ecf20Sopenharmony_ci * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40408c2ecf20Sopenharmony_ci */
40418c2ecf20Sopenharmony_civoid dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40428c2ecf20Sopenharmony_ci{
40438c2ecf20Sopenharmony_ci	/* Model kfree and make put of NULL a no-op */
40448c2ecf20Sopenharmony_ci	if (!dwc_tt)
40458c2ecf20Sopenharmony_ci		return;
40468c2ecf20Sopenharmony_ci
40478c2ecf20Sopenharmony_ci	WARN_ON(dwc_tt->refcount < 1);
40488c2ecf20Sopenharmony_ci
40498c2ecf20Sopenharmony_ci	dwc_tt->refcount--;
40508c2ecf20Sopenharmony_ci	if (!dwc_tt->refcount) {
40518c2ecf20Sopenharmony_ci		dwc_tt->usb_tt->hcpriv = NULL;
40528c2ecf20Sopenharmony_ci		kfree(dwc_tt);
40538c2ecf20Sopenharmony_ci	}
40548c2ecf20Sopenharmony_ci}
40558c2ecf20Sopenharmony_ci
40568c2ecf20Sopenharmony_ciint dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
40578c2ecf20Sopenharmony_ci{
40588c2ecf20Sopenharmony_ci	struct urb *urb = context;
40598c2ecf20Sopenharmony_ci
40608c2ecf20Sopenharmony_ci	return urb->dev->speed;
40618c2ecf20Sopenharmony_ci}
40628c2ecf20Sopenharmony_ci
40638c2ecf20Sopenharmony_cistatic void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
40648c2ecf20Sopenharmony_ci					struct urb *urb)
40658c2ecf20Sopenharmony_ci{
40668c2ecf20Sopenharmony_ci	struct usb_bus *bus = hcd_to_bus(hcd);
40678c2ecf20Sopenharmony_ci
40688c2ecf20Sopenharmony_ci	if (urb->interval)
40698c2ecf20Sopenharmony_ci		bus->bandwidth_allocated += bw / urb->interval;
40708c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
40718c2ecf20Sopenharmony_ci		bus->bandwidth_isoc_reqs++;
40728c2ecf20Sopenharmony_ci	else
40738c2ecf20Sopenharmony_ci		bus->bandwidth_int_reqs++;
40748c2ecf20Sopenharmony_ci}
40758c2ecf20Sopenharmony_ci
40768c2ecf20Sopenharmony_cistatic void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
40778c2ecf20Sopenharmony_ci				    struct urb *urb)
40788c2ecf20Sopenharmony_ci{
40798c2ecf20Sopenharmony_ci	struct usb_bus *bus = hcd_to_bus(hcd);
40808c2ecf20Sopenharmony_ci
40818c2ecf20Sopenharmony_ci	if (urb->interval)
40828c2ecf20Sopenharmony_ci		bus->bandwidth_allocated -= bw / urb->interval;
40838c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
40848c2ecf20Sopenharmony_ci		bus->bandwidth_isoc_reqs--;
40858c2ecf20Sopenharmony_ci	else
40868c2ecf20Sopenharmony_ci		bus->bandwidth_int_reqs--;
40878c2ecf20Sopenharmony_ci}
40888c2ecf20Sopenharmony_ci
40898c2ecf20Sopenharmony_ci/*
40908c2ecf20Sopenharmony_ci * Sets the final status of an URB and returns it to the upper layer. Any
40918c2ecf20Sopenharmony_ci * required cleanup of the URB is performed.
40928c2ecf20Sopenharmony_ci *
40938c2ecf20Sopenharmony_ci * Must be called with interrupt disabled and spinlock held
40948c2ecf20Sopenharmony_ci */
40958c2ecf20Sopenharmony_civoid dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
40968c2ecf20Sopenharmony_ci			int status)
40978c2ecf20Sopenharmony_ci{
40988c2ecf20Sopenharmony_ci	struct urb *urb;
40998c2ecf20Sopenharmony_ci	int i;
41008c2ecf20Sopenharmony_ci
41018c2ecf20Sopenharmony_ci	if (!qtd) {
41028c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
41038c2ecf20Sopenharmony_ci		return;
41048c2ecf20Sopenharmony_ci	}
41058c2ecf20Sopenharmony_ci
41068c2ecf20Sopenharmony_ci	if (!qtd->urb) {
41078c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
41088c2ecf20Sopenharmony_ci		return;
41098c2ecf20Sopenharmony_ci	}
41108c2ecf20Sopenharmony_ci
41118c2ecf20Sopenharmony_ci	urb = qtd->urb->priv;
41128c2ecf20Sopenharmony_ci	if (!urb) {
41138c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
41148c2ecf20Sopenharmony_ci		return;
41158c2ecf20Sopenharmony_ci	}
41168c2ecf20Sopenharmony_ci
41178c2ecf20Sopenharmony_ci	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
41188c2ecf20Sopenharmony_ci
41198c2ecf20Sopenharmony_ci	if (dbg_urb(urb))
41208c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev,
41218c2ecf20Sopenharmony_ci			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
41228c2ecf20Sopenharmony_ci			 __func__, urb, usb_pipedevice(urb->pipe),
41238c2ecf20Sopenharmony_ci			 usb_pipeendpoint(urb->pipe),
41248c2ecf20Sopenharmony_ci			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
41258c2ecf20Sopenharmony_ci			 urb->actual_length);
41268c2ecf20Sopenharmony_ci
41278c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
41288c2ecf20Sopenharmony_ci		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
41298c2ecf20Sopenharmony_ci		for (i = 0; i < urb->number_of_packets; ++i) {
41308c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].actual_length =
41318c2ecf20Sopenharmony_ci				dwc2_hcd_urb_get_iso_desc_actual_length(
41328c2ecf20Sopenharmony_ci						qtd->urb, i);
41338c2ecf20Sopenharmony_ci			urb->iso_frame_desc[i].status =
41348c2ecf20Sopenharmony_ci				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
41358c2ecf20Sopenharmony_ci		}
41368c2ecf20Sopenharmony_ci	}
41378c2ecf20Sopenharmony_ci
41388c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
41398c2ecf20Sopenharmony_ci		for (i = 0; i < urb->number_of_packets; i++)
41408c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
41418c2ecf20Sopenharmony_ci				 i, urb->iso_frame_desc[i].status);
41428c2ecf20Sopenharmony_ci	}
41438c2ecf20Sopenharmony_ci
41448c2ecf20Sopenharmony_ci	urb->status = status;
41458c2ecf20Sopenharmony_ci	if (!status) {
41468c2ecf20Sopenharmony_ci		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
41478c2ecf20Sopenharmony_ci		    urb->actual_length < urb->transfer_buffer_length)
41488c2ecf20Sopenharmony_ci			urb->status = -EREMOTEIO;
41498c2ecf20Sopenharmony_ci	}
41508c2ecf20Sopenharmony_ci
41518c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
41528c2ecf20Sopenharmony_ci	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
41538c2ecf20Sopenharmony_ci		struct usb_host_endpoint *ep = urb->ep;
41548c2ecf20Sopenharmony_ci
41558c2ecf20Sopenharmony_ci		if (ep)
41568c2ecf20Sopenharmony_ci			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
41578c2ecf20Sopenharmony_ci					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
41588c2ecf20Sopenharmony_ci					urb);
41598c2ecf20Sopenharmony_ci	}
41608c2ecf20Sopenharmony_ci
41618c2ecf20Sopenharmony_ci	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
41628c2ecf20Sopenharmony_ci	urb->hcpriv = NULL;
41638c2ecf20Sopenharmony_ci	kfree(qtd->urb);
41648c2ecf20Sopenharmony_ci	qtd->urb = NULL;
41658c2ecf20Sopenharmony_ci
41668c2ecf20Sopenharmony_ci	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
41678c2ecf20Sopenharmony_ci}
41688c2ecf20Sopenharmony_ci
41698c2ecf20Sopenharmony_ci/*
41708c2ecf20Sopenharmony_ci * Work queue function for starting the HCD when A-Cable is connected
41718c2ecf20Sopenharmony_ci */
41728c2ecf20Sopenharmony_cistatic void dwc2_hcd_start_func(struct work_struct *work)
41738c2ecf20Sopenharmony_ci{
41748c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
41758c2ecf20Sopenharmony_ci						start_work.work);
41768c2ecf20Sopenharmony_ci
41778c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
41788c2ecf20Sopenharmony_ci	dwc2_host_start(hsotg);
41798c2ecf20Sopenharmony_ci}
41808c2ecf20Sopenharmony_ci
41818c2ecf20Sopenharmony_ci/*
41828c2ecf20Sopenharmony_ci * Reset work queue function
41838c2ecf20Sopenharmony_ci */
41848c2ecf20Sopenharmony_cistatic void dwc2_hcd_reset_func(struct work_struct *work)
41858c2ecf20Sopenharmony_ci{
41868c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
41878c2ecf20Sopenharmony_ci						reset_work.work);
41888c2ecf20Sopenharmony_ci	unsigned long flags;
41898c2ecf20Sopenharmony_ci	u32 hprt0;
41908c2ecf20Sopenharmony_ci
41918c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "USB RESET function called\n");
41928c2ecf20Sopenharmony_ci
41938c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
41948c2ecf20Sopenharmony_ci
41958c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
41968c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_RST;
41978c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
41988c2ecf20Sopenharmony_ci	hsotg->flags.b.port_reset_change = 1;
41998c2ecf20Sopenharmony_ci
42008c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
42018c2ecf20Sopenharmony_ci}
42028c2ecf20Sopenharmony_ci
42038c2ecf20Sopenharmony_cistatic void dwc2_hcd_phy_reset_func(struct work_struct *work)
42048c2ecf20Sopenharmony_ci{
42058c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
42068c2ecf20Sopenharmony_ci						phy_reset_work);
42078c2ecf20Sopenharmony_ci	int ret;
42088c2ecf20Sopenharmony_ci
42098c2ecf20Sopenharmony_ci	ret = phy_reset(hsotg->phy);
42108c2ecf20Sopenharmony_ci	if (ret)
42118c2ecf20Sopenharmony_ci		dev_warn(hsotg->dev, "PHY reset failed\n");
42128c2ecf20Sopenharmony_ci}
42138c2ecf20Sopenharmony_ci
42148c2ecf20Sopenharmony_ci/*
42158c2ecf20Sopenharmony_ci * =========================================================================
42168c2ecf20Sopenharmony_ci *  Linux HC Driver Functions
42178c2ecf20Sopenharmony_ci * =========================================================================
42188c2ecf20Sopenharmony_ci */
42198c2ecf20Sopenharmony_ci
42208c2ecf20Sopenharmony_ci/*
42218c2ecf20Sopenharmony_ci * Initializes the DWC_otg controller and its root hub and prepares it for host
42228c2ecf20Sopenharmony_ci * mode operation. Activates the root port. Returns 0 on success and a negative
42238c2ecf20Sopenharmony_ci * error code on failure.
42248c2ecf20Sopenharmony_ci */
42258c2ecf20Sopenharmony_cistatic int _dwc2_hcd_start(struct usb_hcd *hcd)
42268c2ecf20Sopenharmony_ci{
42278c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
42288c2ecf20Sopenharmony_ci	struct usb_bus *bus = hcd_to_bus(hcd);
42298c2ecf20Sopenharmony_ci	unsigned long flags;
42308c2ecf20Sopenharmony_ci	u32 hprt0;
42318c2ecf20Sopenharmony_ci	int ret;
42328c2ecf20Sopenharmony_ci
42338c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
42348c2ecf20Sopenharmony_ci
42358c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
42368c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L0;
42378c2ecf20Sopenharmony_ci	hcd->state = HC_STATE_RUNNING;
42388c2ecf20Sopenharmony_ci	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
42398c2ecf20Sopenharmony_ci
42408c2ecf20Sopenharmony_ci	if (dwc2_is_device_mode(hsotg)) {
42418c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
42428c2ecf20Sopenharmony_ci		return 0;	/* why 0 ?? */
42438c2ecf20Sopenharmony_ci	}
42448c2ecf20Sopenharmony_ci
42458c2ecf20Sopenharmony_ci	dwc2_hcd_reinit(hsotg);
42468c2ecf20Sopenharmony_ci
42478c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
42488c2ecf20Sopenharmony_ci	/* Has vbus power been turned on in dwc2_core_host_init ? */
42498c2ecf20Sopenharmony_ci	if (hprt0 & HPRT0_PWR) {
42508c2ecf20Sopenharmony_ci		/* Enable external vbus supply before resuming root hub */
42518c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
42528c2ecf20Sopenharmony_ci		ret = dwc2_vbus_supply_init(hsotg);
42538c2ecf20Sopenharmony_ci		if (ret)
42548c2ecf20Sopenharmony_ci			return ret;
42558c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
42568c2ecf20Sopenharmony_ci	}
42578c2ecf20Sopenharmony_ci
42588c2ecf20Sopenharmony_ci	/* Initialize and connect root hub if one is not already attached */
42598c2ecf20Sopenharmony_ci	if (bus->root_hub) {
42608c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
42618c2ecf20Sopenharmony_ci		/* Inform the HUB driver to resume */
42628c2ecf20Sopenharmony_ci		usb_hcd_resume_root_hub(hcd);
42638c2ecf20Sopenharmony_ci	}
42648c2ecf20Sopenharmony_ci
42658c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
42668c2ecf20Sopenharmony_ci
42678c2ecf20Sopenharmony_ci	return 0;
42688c2ecf20Sopenharmony_ci}
42698c2ecf20Sopenharmony_ci
42708c2ecf20Sopenharmony_ci/*
42718c2ecf20Sopenharmony_ci * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
42728c2ecf20Sopenharmony_ci * stopped.
42738c2ecf20Sopenharmony_ci */
42748c2ecf20Sopenharmony_cistatic void _dwc2_hcd_stop(struct usb_hcd *hcd)
42758c2ecf20Sopenharmony_ci{
42768c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
42778c2ecf20Sopenharmony_ci	unsigned long flags;
42788c2ecf20Sopenharmony_ci	u32 hprt0;
42798c2ecf20Sopenharmony_ci
42808c2ecf20Sopenharmony_ci	/* Turn off all host-specific interrupts */
42818c2ecf20Sopenharmony_ci	dwc2_disable_host_interrupts(hsotg);
42828c2ecf20Sopenharmony_ci
42838c2ecf20Sopenharmony_ci	/* Wait for interrupt processing to finish */
42848c2ecf20Sopenharmony_ci	synchronize_irq(hcd->irq);
42858c2ecf20Sopenharmony_ci
42868c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
42878c2ecf20Sopenharmony_ci	hprt0 = dwc2_read_hprt0(hsotg);
42888c2ecf20Sopenharmony_ci	/* Ensure hcd is disconnected */
42898c2ecf20Sopenharmony_ci	dwc2_hcd_disconnect(hsotg, true);
42908c2ecf20Sopenharmony_ci	dwc2_hcd_stop(hsotg);
42918c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L3;
42928c2ecf20Sopenharmony_ci	hcd->state = HC_STATE_HALT;
42938c2ecf20Sopenharmony_ci	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
42948c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
42958c2ecf20Sopenharmony_ci
42968c2ecf20Sopenharmony_ci	/* keep balanced supply init/exit by checking HPRT0_PWR */
42978c2ecf20Sopenharmony_ci	if (hprt0 & HPRT0_PWR)
42988c2ecf20Sopenharmony_ci		dwc2_vbus_supply_exit(hsotg);
42998c2ecf20Sopenharmony_ci
43008c2ecf20Sopenharmony_ci	usleep_range(1000, 3000);
43018c2ecf20Sopenharmony_ci}
43028c2ecf20Sopenharmony_ci
43038c2ecf20Sopenharmony_cistatic int _dwc2_hcd_suspend(struct usb_hcd *hcd)
43048c2ecf20Sopenharmony_ci{
43058c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
43068c2ecf20Sopenharmony_ci	unsigned long flags;
43078c2ecf20Sopenharmony_ci	int ret = 0;
43088c2ecf20Sopenharmony_ci	u32 hprt0;
43098c2ecf20Sopenharmony_ci	u32 pcgctl;
43108c2ecf20Sopenharmony_ci
43118c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
43128c2ecf20Sopenharmony_ci
43138c2ecf20Sopenharmony_ci	if (dwc2_is_device_mode(hsotg))
43148c2ecf20Sopenharmony_ci		goto unlock;
43158c2ecf20Sopenharmony_ci
43168c2ecf20Sopenharmony_ci	if (hsotg->lx_state != DWC2_L0)
43178c2ecf20Sopenharmony_ci		goto unlock;
43188c2ecf20Sopenharmony_ci
43198c2ecf20Sopenharmony_ci	if (!HCD_HW_ACCESSIBLE(hcd))
43208c2ecf20Sopenharmony_ci		goto unlock;
43218c2ecf20Sopenharmony_ci
43228c2ecf20Sopenharmony_ci	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
43238c2ecf20Sopenharmony_ci		goto unlock;
43248c2ecf20Sopenharmony_ci
43258c2ecf20Sopenharmony_ci	if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL ||
43268c2ecf20Sopenharmony_ci	    hsotg->flags.b.port_connect_status == 0)
43278c2ecf20Sopenharmony_ci		goto skip_power_saving;
43288c2ecf20Sopenharmony_ci
43298c2ecf20Sopenharmony_ci	/*
43308c2ecf20Sopenharmony_ci	 * Drive USB suspend and disable port Power
43318c2ecf20Sopenharmony_ci	 * if usb bus is not suspended.
43328c2ecf20Sopenharmony_ci	 */
43338c2ecf20Sopenharmony_ci	if (!hsotg->bus_suspended) {
43348c2ecf20Sopenharmony_ci		hprt0 = dwc2_read_hprt0(hsotg);
43358c2ecf20Sopenharmony_ci		if (hprt0 & HPRT0_CONNSTS) {
43368c2ecf20Sopenharmony_ci			hprt0 |= HPRT0_SUSP;
43378c2ecf20Sopenharmony_ci			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL)
43388c2ecf20Sopenharmony_ci				hprt0 &= ~HPRT0_PWR;
43398c2ecf20Sopenharmony_ci			dwc2_writel(hsotg, hprt0, HPRT0);
43408c2ecf20Sopenharmony_ci		}
43418c2ecf20Sopenharmony_ci		if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
43428c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hsotg->lock, flags);
43438c2ecf20Sopenharmony_ci			dwc2_vbus_supply_exit(hsotg);
43448c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hsotg->lock, flags);
43458c2ecf20Sopenharmony_ci		} else {
43468c2ecf20Sopenharmony_ci			pcgctl = readl(hsotg->regs + PCGCTL);
43478c2ecf20Sopenharmony_ci			pcgctl |= PCGCTL_STOPPCLK;
43488c2ecf20Sopenharmony_ci			writel(pcgctl, hsotg->regs + PCGCTL);
43498c2ecf20Sopenharmony_ci		}
43508c2ecf20Sopenharmony_ci	}
43518c2ecf20Sopenharmony_ci
43528c2ecf20Sopenharmony_ci	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
43538c2ecf20Sopenharmony_ci		/* Enter partial_power_down */
43548c2ecf20Sopenharmony_ci		ret = dwc2_enter_partial_power_down(hsotg);
43558c2ecf20Sopenharmony_ci		if (ret) {
43568c2ecf20Sopenharmony_ci			if (ret != -ENOTSUPP)
43578c2ecf20Sopenharmony_ci				dev_err(hsotg->dev,
43588c2ecf20Sopenharmony_ci					"enter partial_power_down failed\n");
43598c2ecf20Sopenharmony_ci			goto skip_power_saving;
43608c2ecf20Sopenharmony_ci		}
43618c2ecf20Sopenharmony_ci
43628c2ecf20Sopenharmony_ci		/* After entering partial_power_down, hardware is no more accessible */
43638c2ecf20Sopenharmony_ci		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
43648c2ecf20Sopenharmony_ci	}
43658c2ecf20Sopenharmony_ci
43668c2ecf20Sopenharmony_ci	/* Ask phy to be suspended */
43678c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
43688c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
43698c2ecf20Sopenharmony_ci		usb_phy_set_suspend(hsotg->uphy, true);
43708c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
43718c2ecf20Sopenharmony_ci	}
43728c2ecf20Sopenharmony_ci
43738c2ecf20Sopenharmony_ciskip_power_saving:
43748c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L2;
43758c2ecf20Sopenharmony_ciunlock:
43768c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
43778c2ecf20Sopenharmony_ci
43788c2ecf20Sopenharmony_ci	return ret;
43798c2ecf20Sopenharmony_ci}
43808c2ecf20Sopenharmony_ci
43818c2ecf20Sopenharmony_cistatic int _dwc2_hcd_resume(struct usb_hcd *hcd)
43828c2ecf20Sopenharmony_ci{
43838c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
43848c2ecf20Sopenharmony_ci	unsigned long flags;
43858c2ecf20Sopenharmony_ci	u32 pcgctl;
43868c2ecf20Sopenharmony_ci	int ret = 0;
43878c2ecf20Sopenharmony_ci
43888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
43898c2ecf20Sopenharmony_ci
43908c2ecf20Sopenharmony_ci	if (dwc2_is_device_mode(hsotg))
43918c2ecf20Sopenharmony_ci		goto unlock;
43928c2ecf20Sopenharmony_ci
43938c2ecf20Sopenharmony_ci	if (hsotg->lx_state != DWC2_L2)
43948c2ecf20Sopenharmony_ci		goto unlock;
43958c2ecf20Sopenharmony_ci
43968c2ecf20Sopenharmony_ci	if (hsotg->params.power_down > DWC2_POWER_DOWN_PARAM_PARTIAL) {
43978c2ecf20Sopenharmony_ci		hsotg->lx_state = DWC2_L0;
43988c2ecf20Sopenharmony_ci		goto unlock;
43998c2ecf20Sopenharmony_ci	}
44008c2ecf20Sopenharmony_ci
44018c2ecf20Sopenharmony_ci	/*
44028c2ecf20Sopenharmony_ci	 * Enable power if not already done.
44038c2ecf20Sopenharmony_ci	 * This must not be spinlocked since duration
44048c2ecf20Sopenharmony_ci	 * of this call is unknown.
44058c2ecf20Sopenharmony_ci	 */
44068c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
44078c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
44088c2ecf20Sopenharmony_ci		usb_phy_set_suspend(hsotg->uphy, false);
44098c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
44108c2ecf20Sopenharmony_ci	}
44118c2ecf20Sopenharmony_ci
44128c2ecf20Sopenharmony_ci	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
44138c2ecf20Sopenharmony_ci		/*
44148c2ecf20Sopenharmony_ci		 * Set HW accessible bit before powering on the controller
44158c2ecf20Sopenharmony_ci		 * since an interrupt may rise.
44168c2ecf20Sopenharmony_ci		 */
44178c2ecf20Sopenharmony_ci		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
44188c2ecf20Sopenharmony_ci
44198c2ecf20Sopenharmony_ci
44208c2ecf20Sopenharmony_ci		/* Exit partial_power_down */
44218c2ecf20Sopenharmony_ci		ret = dwc2_exit_partial_power_down(hsotg, true);
44228c2ecf20Sopenharmony_ci		if (ret && (ret != -ENOTSUPP))
44238c2ecf20Sopenharmony_ci			dev_err(hsotg->dev, "exit partial_power_down failed\n");
44248c2ecf20Sopenharmony_ci	} else {
44258c2ecf20Sopenharmony_ci		pcgctl = readl(hsotg->regs + PCGCTL);
44268c2ecf20Sopenharmony_ci		pcgctl &= ~PCGCTL_STOPPCLK;
44278c2ecf20Sopenharmony_ci		writel(pcgctl, hsotg->regs + PCGCTL);
44288c2ecf20Sopenharmony_ci	}
44298c2ecf20Sopenharmony_ci
44308c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L0;
44318c2ecf20Sopenharmony_ci
44328c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
44338c2ecf20Sopenharmony_ci
44348c2ecf20Sopenharmony_ci	if (hsotg->bus_suspended) {
44358c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
44368c2ecf20Sopenharmony_ci		hsotg->flags.b.port_suspend_change = 1;
44378c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
44388c2ecf20Sopenharmony_ci		dwc2_port_resume(hsotg);
44398c2ecf20Sopenharmony_ci	} else {
44408c2ecf20Sopenharmony_ci		if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
44418c2ecf20Sopenharmony_ci			dwc2_vbus_supply_init(hsotg);
44428c2ecf20Sopenharmony_ci
44438c2ecf20Sopenharmony_ci			/* Wait for controller to correctly update D+/D- level */
44448c2ecf20Sopenharmony_ci			usleep_range(3000, 5000);
44458c2ecf20Sopenharmony_ci		}
44468c2ecf20Sopenharmony_ci
44478c2ecf20Sopenharmony_ci		/*
44488c2ecf20Sopenharmony_ci		 * Clear Port Enable and Port Status changes.
44498c2ecf20Sopenharmony_ci		 * Enable Port Power.
44508c2ecf20Sopenharmony_ci		 */
44518c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
44528c2ecf20Sopenharmony_ci				HPRT0_ENACHG, HPRT0);
44538c2ecf20Sopenharmony_ci		/* Wait for controller to detect Port Connect */
44548c2ecf20Sopenharmony_ci		usleep_range(5000, 7000);
44558c2ecf20Sopenharmony_ci	}
44568c2ecf20Sopenharmony_ci
44578c2ecf20Sopenharmony_ci	return ret;
44588c2ecf20Sopenharmony_ciunlock:
44598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
44608c2ecf20Sopenharmony_ci
44618c2ecf20Sopenharmony_ci	return ret;
44628c2ecf20Sopenharmony_ci}
44638c2ecf20Sopenharmony_ci
44648c2ecf20Sopenharmony_ci/* Returns the current frame number */
44658c2ecf20Sopenharmony_cistatic int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
44668c2ecf20Sopenharmony_ci{
44678c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
44688c2ecf20Sopenharmony_ci
44698c2ecf20Sopenharmony_ci	return dwc2_hcd_get_frame_number(hsotg);
44708c2ecf20Sopenharmony_ci}
44718c2ecf20Sopenharmony_ci
44728c2ecf20Sopenharmony_cistatic void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
44738c2ecf20Sopenharmony_ci			       char *fn_name)
44748c2ecf20Sopenharmony_ci{
44758c2ecf20Sopenharmony_ci#ifdef VERBOSE_DEBUG
44768c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
44778c2ecf20Sopenharmony_ci	char *pipetype = NULL;
44788c2ecf20Sopenharmony_ci	char *speed = NULL;
44798c2ecf20Sopenharmony_ci
44808c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
44818c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Device address: %d\n",
44828c2ecf20Sopenharmony_ci		 usb_pipedevice(urb->pipe));
44838c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
44848c2ecf20Sopenharmony_ci		 usb_pipeendpoint(urb->pipe),
44858c2ecf20Sopenharmony_ci		 usb_pipein(urb->pipe) ? "IN" : "OUT");
44868c2ecf20Sopenharmony_ci
44878c2ecf20Sopenharmony_ci	switch (usb_pipetype(urb->pipe)) {
44888c2ecf20Sopenharmony_ci	case PIPE_CONTROL:
44898c2ecf20Sopenharmony_ci		pipetype = "CONTROL";
44908c2ecf20Sopenharmony_ci		break;
44918c2ecf20Sopenharmony_ci	case PIPE_BULK:
44928c2ecf20Sopenharmony_ci		pipetype = "BULK";
44938c2ecf20Sopenharmony_ci		break;
44948c2ecf20Sopenharmony_ci	case PIPE_INTERRUPT:
44958c2ecf20Sopenharmony_ci		pipetype = "INTERRUPT";
44968c2ecf20Sopenharmony_ci		break;
44978c2ecf20Sopenharmony_ci	case PIPE_ISOCHRONOUS:
44988c2ecf20Sopenharmony_ci		pipetype = "ISOCHRONOUS";
44998c2ecf20Sopenharmony_ci		break;
45008c2ecf20Sopenharmony_ci	}
45018c2ecf20Sopenharmony_ci
45028c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
45038c2ecf20Sopenharmony_ci		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
45048c2ecf20Sopenharmony_ci		 "IN" : "OUT");
45058c2ecf20Sopenharmony_ci
45068c2ecf20Sopenharmony_ci	switch (urb->dev->speed) {
45078c2ecf20Sopenharmony_ci	case USB_SPEED_HIGH:
45088c2ecf20Sopenharmony_ci		speed = "HIGH";
45098c2ecf20Sopenharmony_ci		break;
45108c2ecf20Sopenharmony_ci	case USB_SPEED_FULL:
45118c2ecf20Sopenharmony_ci		speed = "FULL";
45128c2ecf20Sopenharmony_ci		break;
45138c2ecf20Sopenharmony_ci	case USB_SPEED_LOW:
45148c2ecf20Sopenharmony_ci		speed = "LOW";
45158c2ecf20Sopenharmony_ci		break;
45168c2ecf20Sopenharmony_ci	default:
45178c2ecf20Sopenharmony_ci		speed = "UNKNOWN";
45188c2ecf20Sopenharmony_ci		break;
45198c2ecf20Sopenharmony_ci	}
45208c2ecf20Sopenharmony_ci
45218c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
45228c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
45238c2ecf20Sopenharmony_ci		 usb_endpoint_maxp(&urb->ep->desc),
45248c2ecf20Sopenharmony_ci		 usb_endpoint_maxp_mult(&urb->ep->desc));
45258c2ecf20Sopenharmony_ci
45268c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
45278c2ecf20Sopenharmony_ci		 urb->transfer_buffer_length);
45288c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
45298c2ecf20Sopenharmony_ci		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
45308c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
45318c2ecf20Sopenharmony_ci		 urb->setup_packet, (unsigned long)urb->setup_dma);
45328c2ecf20Sopenharmony_ci	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
45338c2ecf20Sopenharmony_ci
45348c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
45358c2ecf20Sopenharmony_ci		int i;
45368c2ecf20Sopenharmony_ci
45378c2ecf20Sopenharmony_ci		for (i = 0; i < urb->number_of_packets; i++) {
45388c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
45398c2ecf20Sopenharmony_ci			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
45408c2ecf20Sopenharmony_ci				 urb->iso_frame_desc[i].offset,
45418c2ecf20Sopenharmony_ci				 urb->iso_frame_desc[i].length);
45428c2ecf20Sopenharmony_ci		}
45438c2ecf20Sopenharmony_ci	}
45448c2ecf20Sopenharmony_ci#endif
45458c2ecf20Sopenharmony_ci}
45468c2ecf20Sopenharmony_ci
45478c2ecf20Sopenharmony_ci/*
45488c2ecf20Sopenharmony_ci * Starts processing a USB transfer request specified by a USB Request Block
45498c2ecf20Sopenharmony_ci * (URB). mem_flags indicates the type of memory allocation to use while
45508c2ecf20Sopenharmony_ci * processing this URB.
45518c2ecf20Sopenharmony_ci */
45528c2ecf20Sopenharmony_cistatic int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
45538c2ecf20Sopenharmony_ci				 gfp_t mem_flags)
45548c2ecf20Sopenharmony_ci{
45558c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
45568c2ecf20Sopenharmony_ci	struct usb_host_endpoint *ep = urb->ep;
45578c2ecf20Sopenharmony_ci	struct dwc2_hcd_urb *dwc2_urb;
45588c2ecf20Sopenharmony_ci	int i;
45598c2ecf20Sopenharmony_ci	int retval;
45608c2ecf20Sopenharmony_ci	int alloc_bandwidth = 0;
45618c2ecf20Sopenharmony_ci	u8 ep_type = 0;
45628c2ecf20Sopenharmony_ci	u32 tflags = 0;
45638c2ecf20Sopenharmony_ci	void *buf;
45648c2ecf20Sopenharmony_ci	unsigned long flags;
45658c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
45668c2ecf20Sopenharmony_ci	bool qh_allocated = false;
45678c2ecf20Sopenharmony_ci	struct dwc2_qtd *qtd;
45688c2ecf20Sopenharmony_ci
45698c2ecf20Sopenharmony_ci	if (dbg_urb(urb)) {
45708c2ecf20Sopenharmony_ci		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
45718c2ecf20Sopenharmony_ci		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
45728c2ecf20Sopenharmony_ci	}
45738c2ecf20Sopenharmony_ci
45748c2ecf20Sopenharmony_ci	if (!ep)
45758c2ecf20Sopenharmony_ci		return -EINVAL;
45768c2ecf20Sopenharmony_ci
45778c2ecf20Sopenharmony_ci	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
45788c2ecf20Sopenharmony_ci	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
45798c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hsotg->lock, flags);
45808c2ecf20Sopenharmony_ci		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
45818c2ecf20Sopenharmony_ci			alloc_bandwidth = 1;
45828c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hsotg->lock, flags);
45838c2ecf20Sopenharmony_ci	}
45848c2ecf20Sopenharmony_ci
45858c2ecf20Sopenharmony_ci	switch (usb_pipetype(urb->pipe)) {
45868c2ecf20Sopenharmony_ci	case PIPE_CONTROL:
45878c2ecf20Sopenharmony_ci		ep_type = USB_ENDPOINT_XFER_CONTROL;
45888c2ecf20Sopenharmony_ci		break;
45898c2ecf20Sopenharmony_ci	case PIPE_ISOCHRONOUS:
45908c2ecf20Sopenharmony_ci		ep_type = USB_ENDPOINT_XFER_ISOC;
45918c2ecf20Sopenharmony_ci		break;
45928c2ecf20Sopenharmony_ci	case PIPE_BULK:
45938c2ecf20Sopenharmony_ci		ep_type = USB_ENDPOINT_XFER_BULK;
45948c2ecf20Sopenharmony_ci		break;
45958c2ecf20Sopenharmony_ci	case PIPE_INTERRUPT:
45968c2ecf20Sopenharmony_ci		ep_type = USB_ENDPOINT_XFER_INT;
45978c2ecf20Sopenharmony_ci		break;
45988c2ecf20Sopenharmony_ci	}
45998c2ecf20Sopenharmony_ci
46008c2ecf20Sopenharmony_ci	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
46018c2ecf20Sopenharmony_ci				      mem_flags);
46028c2ecf20Sopenharmony_ci	if (!dwc2_urb)
46038c2ecf20Sopenharmony_ci		return -ENOMEM;
46048c2ecf20Sopenharmony_ci
46058c2ecf20Sopenharmony_ci	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
46068c2ecf20Sopenharmony_ci				  usb_pipeendpoint(urb->pipe), ep_type,
46078c2ecf20Sopenharmony_ci				  usb_pipein(urb->pipe),
46088c2ecf20Sopenharmony_ci				  usb_endpoint_maxp(&ep->desc),
46098c2ecf20Sopenharmony_ci				  usb_endpoint_maxp_mult(&ep->desc));
46108c2ecf20Sopenharmony_ci
46118c2ecf20Sopenharmony_ci	buf = urb->transfer_buffer;
46128c2ecf20Sopenharmony_ci
46138c2ecf20Sopenharmony_ci	if (hcd_uses_dma(hcd)) {
46148c2ecf20Sopenharmony_ci		if (!buf && (urb->transfer_dma & 3)) {
46158c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
46168c2ecf20Sopenharmony_ci				"%s: unaligned transfer with no transfer_buffer",
46178c2ecf20Sopenharmony_ci				__func__);
46188c2ecf20Sopenharmony_ci			retval = -EINVAL;
46198c2ecf20Sopenharmony_ci			goto fail0;
46208c2ecf20Sopenharmony_ci		}
46218c2ecf20Sopenharmony_ci	}
46228c2ecf20Sopenharmony_ci
46238c2ecf20Sopenharmony_ci	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
46248c2ecf20Sopenharmony_ci		tflags |= URB_GIVEBACK_ASAP;
46258c2ecf20Sopenharmony_ci	if (urb->transfer_flags & URB_ZERO_PACKET)
46268c2ecf20Sopenharmony_ci		tflags |= URB_SEND_ZERO_PACKET;
46278c2ecf20Sopenharmony_ci
46288c2ecf20Sopenharmony_ci	dwc2_urb->priv = urb;
46298c2ecf20Sopenharmony_ci	dwc2_urb->buf = buf;
46308c2ecf20Sopenharmony_ci	dwc2_urb->dma = urb->transfer_dma;
46318c2ecf20Sopenharmony_ci	dwc2_urb->length = urb->transfer_buffer_length;
46328c2ecf20Sopenharmony_ci	dwc2_urb->setup_packet = urb->setup_packet;
46338c2ecf20Sopenharmony_ci	dwc2_urb->setup_dma = urb->setup_dma;
46348c2ecf20Sopenharmony_ci	dwc2_urb->flags = tflags;
46358c2ecf20Sopenharmony_ci	dwc2_urb->interval = urb->interval;
46368c2ecf20Sopenharmony_ci	dwc2_urb->status = -EINPROGRESS;
46378c2ecf20Sopenharmony_ci
46388c2ecf20Sopenharmony_ci	for (i = 0; i < urb->number_of_packets; ++i)
46398c2ecf20Sopenharmony_ci		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
46408c2ecf20Sopenharmony_ci						 urb->iso_frame_desc[i].offset,
46418c2ecf20Sopenharmony_ci						 urb->iso_frame_desc[i].length);
46428c2ecf20Sopenharmony_ci
46438c2ecf20Sopenharmony_ci	urb->hcpriv = dwc2_urb;
46448c2ecf20Sopenharmony_ci	qh = (struct dwc2_qh *)ep->hcpriv;
46458c2ecf20Sopenharmony_ci	/* Create QH for the endpoint if it doesn't exist */
46468c2ecf20Sopenharmony_ci	if (!qh) {
46478c2ecf20Sopenharmony_ci		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
46488c2ecf20Sopenharmony_ci		if (!qh) {
46498c2ecf20Sopenharmony_ci			retval = -ENOMEM;
46508c2ecf20Sopenharmony_ci			goto fail0;
46518c2ecf20Sopenharmony_ci		}
46528c2ecf20Sopenharmony_ci		ep->hcpriv = qh;
46538c2ecf20Sopenharmony_ci		qh_allocated = true;
46548c2ecf20Sopenharmony_ci	}
46558c2ecf20Sopenharmony_ci
46568c2ecf20Sopenharmony_ci	qtd = kzalloc(sizeof(*qtd), mem_flags);
46578c2ecf20Sopenharmony_ci	if (!qtd) {
46588c2ecf20Sopenharmony_ci		retval = -ENOMEM;
46598c2ecf20Sopenharmony_ci		goto fail1;
46608c2ecf20Sopenharmony_ci	}
46618c2ecf20Sopenharmony_ci
46628c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
46638c2ecf20Sopenharmony_ci	retval = usb_hcd_link_urb_to_ep(hcd, urb);
46648c2ecf20Sopenharmony_ci	if (retval)
46658c2ecf20Sopenharmony_ci		goto fail2;
46668c2ecf20Sopenharmony_ci
46678c2ecf20Sopenharmony_ci	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
46688c2ecf20Sopenharmony_ci	if (retval)
46698c2ecf20Sopenharmony_ci		goto fail3;
46708c2ecf20Sopenharmony_ci
46718c2ecf20Sopenharmony_ci	if (alloc_bandwidth) {
46728c2ecf20Sopenharmony_ci		dwc2_allocate_bus_bandwidth(hcd,
46738c2ecf20Sopenharmony_ci				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
46748c2ecf20Sopenharmony_ci				urb);
46758c2ecf20Sopenharmony_ci	}
46768c2ecf20Sopenharmony_ci
46778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
46788c2ecf20Sopenharmony_ci
46798c2ecf20Sopenharmony_ci	return 0;
46808c2ecf20Sopenharmony_ci
46818c2ecf20Sopenharmony_cifail3:
46828c2ecf20Sopenharmony_ci	dwc2_urb->priv = NULL;
46838c2ecf20Sopenharmony_ci	usb_hcd_unlink_urb_from_ep(hcd, urb);
46848c2ecf20Sopenharmony_ci	if (qh_allocated && qh->channel && qh->channel->qh == qh)
46858c2ecf20Sopenharmony_ci		qh->channel->qh = NULL;
46868c2ecf20Sopenharmony_cifail2:
46878c2ecf20Sopenharmony_ci	urb->hcpriv = NULL;
46888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
46898c2ecf20Sopenharmony_ci	kfree(qtd);
46908c2ecf20Sopenharmony_cifail1:
46918c2ecf20Sopenharmony_ci	if (qh_allocated) {
46928c2ecf20Sopenharmony_ci		struct dwc2_qtd *qtd2, *qtd2_tmp;
46938c2ecf20Sopenharmony_ci
46948c2ecf20Sopenharmony_ci		ep->hcpriv = NULL;
46958c2ecf20Sopenharmony_ci		dwc2_hcd_qh_unlink(hsotg, qh);
46968c2ecf20Sopenharmony_ci		/* Free each QTD in the QH's QTD list */
46978c2ecf20Sopenharmony_ci		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
46988c2ecf20Sopenharmony_ci					 qtd_list_entry)
46998c2ecf20Sopenharmony_ci			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
47008c2ecf20Sopenharmony_ci		dwc2_hcd_qh_free(hsotg, qh);
47018c2ecf20Sopenharmony_ci	}
47028c2ecf20Sopenharmony_cifail0:
47038c2ecf20Sopenharmony_ci	kfree(dwc2_urb);
47048c2ecf20Sopenharmony_ci
47058c2ecf20Sopenharmony_ci	return retval;
47068c2ecf20Sopenharmony_ci}
47078c2ecf20Sopenharmony_ci
47088c2ecf20Sopenharmony_ci/*
47098c2ecf20Sopenharmony_ci * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
47108c2ecf20Sopenharmony_ci */
47118c2ecf20Sopenharmony_cistatic int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
47128c2ecf20Sopenharmony_ci				 int status)
47138c2ecf20Sopenharmony_ci{
47148c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
47158c2ecf20Sopenharmony_ci	int rc;
47168c2ecf20Sopenharmony_ci	unsigned long flags;
47178c2ecf20Sopenharmony_ci
47188c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
47198c2ecf20Sopenharmony_ci	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
47208c2ecf20Sopenharmony_ci
47218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
47228c2ecf20Sopenharmony_ci
47238c2ecf20Sopenharmony_ci	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
47248c2ecf20Sopenharmony_ci	if (rc)
47258c2ecf20Sopenharmony_ci		goto out;
47268c2ecf20Sopenharmony_ci
47278c2ecf20Sopenharmony_ci	if (!urb->hcpriv) {
47288c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
47298c2ecf20Sopenharmony_ci		goto out;
47308c2ecf20Sopenharmony_ci	}
47318c2ecf20Sopenharmony_ci
47328c2ecf20Sopenharmony_ci	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
47338c2ecf20Sopenharmony_ci
47348c2ecf20Sopenharmony_ci	usb_hcd_unlink_urb_from_ep(hcd, urb);
47358c2ecf20Sopenharmony_ci
47368c2ecf20Sopenharmony_ci	kfree(urb->hcpriv);
47378c2ecf20Sopenharmony_ci	urb->hcpriv = NULL;
47388c2ecf20Sopenharmony_ci
47398c2ecf20Sopenharmony_ci	/* Higher layer software sets URB status */
47408c2ecf20Sopenharmony_ci	spin_unlock(&hsotg->lock);
47418c2ecf20Sopenharmony_ci	usb_hcd_giveback_urb(hcd, urb, status);
47428c2ecf20Sopenharmony_ci	spin_lock(&hsotg->lock);
47438c2ecf20Sopenharmony_ci
47448c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
47458c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
47468c2ecf20Sopenharmony_ciout:
47478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
47488c2ecf20Sopenharmony_ci
47498c2ecf20Sopenharmony_ci	return rc;
47508c2ecf20Sopenharmony_ci}
47518c2ecf20Sopenharmony_ci
47528c2ecf20Sopenharmony_ci/*
47538c2ecf20Sopenharmony_ci * Frees resources in the DWC_otg controller related to a given endpoint. Also
47548c2ecf20Sopenharmony_ci * clears state in the HCD related to the endpoint. Any URBs for the endpoint
47558c2ecf20Sopenharmony_ci * must already be dequeued.
47568c2ecf20Sopenharmony_ci */
47578c2ecf20Sopenharmony_cistatic void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
47588c2ecf20Sopenharmony_ci				       struct usb_host_endpoint *ep)
47598c2ecf20Sopenharmony_ci{
47608c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
47618c2ecf20Sopenharmony_ci
47628c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev,
47638c2ecf20Sopenharmony_ci		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
47648c2ecf20Sopenharmony_ci		ep->desc.bEndpointAddress, ep->hcpriv);
47658c2ecf20Sopenharmony_ci	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
47668c2ecf20Sopenharmony_ci}
47678c2ecf20Sopenharmony_ci
47688c2ecf20Sopenharmony_ci/*
47698c2ecf20Sopenharmony_ci * Resets endpoint specific parameter values, in current version used to reset
47708c2ecf20Sopenharmony_ci * the data toggle (as a WA). This function can be called from usb_clear_halt
47718c2ecf20Sopenharmony_ci * routine.
47728c2ecf20Sopenharmony_ci */
47738c2ecf20Sopenharmony_cistatic void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
47748c2ecf20Sopenharmony_ci				     struct usb_host_endpoint *ep)
47758c2ecf20Sopenharmony_ci{
47768c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
47778c2ecf20Sopenharmony_ci	unsigned long flags;
47788c2ecf20Sopenharmony_ci
47798c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev,
47808c2ecf20Sopenharmony_ci		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
47818c2ecf20Sopenharmony_ci		ep->desc.bEndpointAddress);
47828c2ecf20Sopenharmony_ci
47838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
47848c2ecf20Sopenharmony_ci	dwc2_hcd_endpoint_reset(hsotg, ep);
47858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
47868c2ecf20Sopenharmony_ci}
47878c2ecf20Sopenharmony_ci
47888c2ecf20Sopenharmony_ci/*
47898c2ecf20Sopenharmony_ci * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
47908c2ecf20Sopenharmony_ci * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
47918c2ecf20Sopenharmony_ci * interrupt.
47928c2ecf20Sopenharmony_ci *
47938c2ecf20Sopenharmony_ci * This function is called by the USB core when an interrupt occurs
47948c2ecf20Sopenharmony_ci */
47958c2ecf20Sopenharmony_cistatic irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
47968c2ecf20Sopenharmony_ci{
47978c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
47988c2ecf20Sopenharmony_ci
47998c2ecf20Sopenharmony_ci	return dwc2_handle_hcd_intr(hsotg);
48008c2ecf20Sopenharmony_ci}
48018c2ecf20Sopenharmony_ci
48028c2ecf20Sopenharmony_ci/*
48038c2ecf20Sopenharmony_ci * Creates Status Change bitmap for the root hub and root port. The bitmap is
48048c2ecf20Sopenharmony_ci * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
48058c2ecf20Sopenharmony_ci * is the status change indicator for the single root port. Returns 1 if either
48068c2ecf20Sopenharmony_ci * change indicator is 1, otherwise returns 0.
48078c2ecf20Sopenharmony_ci */
48088c2ecf20Sopenharmony_cistatic int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
48098c2ecf20Sopenharmony_ci{
48108c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
48118c2ecf20Sopenharmony_ci
48128c2ecf20Sopenharmony_ci	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
48138c2ecf20Sopenharmony_ci	return buf[0] != 0;
48148c2ecf20Sopenharmony_ci}
48158c2ecf20Sopenharmony_ci
48168c2ecf20Sopenharmony_ci/* Handles hub class-specific requests */
48178c2ecf20Sopenharmony_cistatic int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
48188c2ecf20Sopenharmony_ci				 u16 windex, char *buf, u16 wlength)
48198c2ecf20Sopenharmony_ci{
48208c2ecf20Sopenharmony_ci	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
48218c2ecf20Sopenharmony_ci					  wvalue, windex, buf, wlength);
48228c2ecf20Sopenharmony_ci	return retval;
48238c2ecf20Sopenharmony_ci}
48248c2ecf20Sopenharmony_ci
48258c2ecf20Sopenharmony_ci/* Handles hub TT buffer clear completions */
48268c2ecf20Sopenharmony_cistatic void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
48278c2ecf20Sopenharmony_ci					       struct usb_host_endpoint *ep)
48288c2ecf20Sopenharmony_ci{
48298c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
48308c2ecf20Sopenharmony_ci	struct dwc2_qh *qh;
48318c2ecf20Sopenharmony_ci	unsigned long flags;
48328c2ecf20Sopenharmony_ci
48338c2ecf20Sopenharmony_ci	qh = ep->hcpriv;
48348c2ecf20Sopenharmony_ci	if (!qh)
48358c2ecf20Sopenharmony_ci		return;
48368c2ecf20Sopenharmony_ci
48378c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
48388c2ecf20Sopenharmony_ci	qh->tt_buffer_dirty = 0;
48398c2ecf20Sopenharmony_ci
48408c2ecf20Sopenharmony_ci	if (hsotg->flags.b.port_connect_status)
48418c2ecf20Sopenharmony_ci		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
48428c2ecf20Sopenharmony_ci
48438c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
48448c2ecf20Sopenharmony_ci}
48458c2ecf20Sopenharmony_ci
48468c2ecf20Sopenharmony_ci/*
48478c2ecf20Sopenharmony_ci * HPRT0_SPD_HIGH_SPEED: high speed
48488c2ecf20Sopenharmony_ci * HPRT0_SPD_FULL_SPEED: full speed
48498c2ecf20Sopenharmony_ci */
48508c2ecf20Sopenharmony_cistatic void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
48518c2ecf20Sopenharmony_ci{
48528c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
48538c2ecf20Sopenharmony_ci
48548c2ecf20Sopenharmony_ci	if (hsotg->params.speed == speed)
48558c2ecf20Sopenharmony_ci		return;
48568c2ecf20Sopenharmony_ci
48578c2ecf20Sopenharmony_ci	hsotg->params.speed = speed;
48588c2ecf20Sopenharmony_ci	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
48598c2ecf20Sopenharmony_ci}
48608c2ecf20Sopenharmony_ci
48618c2ecf20Sopenharmony_cistatic void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
48628c2ecf20Sopenharmony_ci{
48638c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
48648c2ecf20Sopenharmony_ci
48658c2ecf20Sopenharmony_ci	if (!hsotg->params.change_speed_quirk)
48668c2ecf20Sopenharmony_ci		return;
48678c2ecf20Sopenharmony_ci
48688c2ecf20Sopenharmony_ci	/*
48698c2ecf20Sopenharmony_ci	 * On removal, set speed to default high-speed.
48708c2ecf20Sopenharmony_ci	 */
48718c2ecf20Sopenharmony_ci	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
48728c2ecf20Sopenharmony_ci	    udev->parent->speed < USB_SPEED_HIGH) {
48738c2ecf20Sopenharmony_ci		dev_info(hsotg->dev, "Set speed to default high-speed\n");
48748c2ecf20Sopenharmony_ci		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
48758c2ecf20Sopenharmony_ci	}
48768c2ecf20Sopenharmony_ci}
48778c2ecf20Sopenharmony_ci
48788c2ecf20Sopenharmony_cistatic int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
48798c2ecf20Sopenharmony_ci{
48808c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
48818c2ecf20Sopenharmony_ci
48828c2ecf20Sopenharmony_ci	if (!hsotg->params.change_speed_quirk)
48838c2ecf20Sopenharmony_ci		return 0;
48848c2ecf20Sopenharmony_ci
48858c2ecf20Sopenharmony_ci	if (udev->speed == USB_SPEED_HIGH) {
48868c2ecf20Sopenharmony_ci		dev_info(hsotg->dev, "Set speed to high-speed\n");
48878c2ecf20Sopenharmony_ci		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
48888c2ecf20Sopenharmony_ci	} else if ((udev->speed == USB_SPEED_FULL ||
48898c2ecf20Sopenharmony_ci				udev->speed == USB_SPEED_LOW)) {
48908c2ecf20Sopenharmony_ci		/*
48918c2ecf20Sopenharmony_ci		 * Change speed setting to full-speed if there's
48928c2ecf20Sopenharmony_ci		 * a full-speed or low-speed device plugged in.
48938c2ecf20Sopenharmony_ci		 */
48948c2ecf20Sopenharmony_ci		dev_info(hsotg->dev, "Set speed to full-speed\n");
48958c2ecf20Sopenharmony_ci		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
48968c2ecf20Sopenharmony_ci	}
48978c2ecf20Sopenharmony_ci
48988c2ecf20Sopenharmony_ci	return 0;
48998c2ecf20Sopenharmony_ci}
49008c2ecf20Sopenharmony_ci
49018c2ecf20Sopenharmony_cistatic struct hc_driver dwc2_hc_driver = {
49028c2ecf20Sopenharmony_ci	.description = "dwc2_hsotg",
49038c2ecf20Sopenharmony_ci	.product_desc = "DWC OTG Controller",
49048c2ecf20Sopenharmony_ci	.hcd_priv_size = sizeof(struct wrapper_priv_data),
49058c2ecf20Sopenharmony_ci
49068c2ecf20Sopenharmony_ci	.irq = _dwc2_hcd_irq,
49078c2ecf20Sopenharmony_ci	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
49088c2ecf20Sopenharmony_ci
49098c2ecf20Sopenharmony_ci	.start = _dwc2_hcd_start,
49108c2ecf20Sopenharmony_ci	.stop = _dwc2_hcd_stop,
49118c2ecf20Sopenharmony_ci	.urb_enqueue = _dwc2_hcd_urb_enqueue,
49128c2ecf20Sopenharmony_ci	.urb_dequeue = _dwc2_hcd_urb_dequeue,
49138c2ecf20Sopenharmony_ci	.endpoint_disable = _dwc2_hcd_endpoint_disable,
49148c2ecf20Sopenharmony_ci	.endpoint_reset = _dwc2_hcd_endpoint_reset,
49158c2ecf20Sopenharmony_ci	.get_frame_number = _dwc2_hcd_get_frame_number,
49168c2ecf20Sopenharmony_ci
49178c2ecf20Sopenharmony_ci	.hub_status_data = _dwc2_hcd_hub_status_data,
49188c2ecf20Sopenharmony_ci	.hub_control = _dwc2_hcd_hub_control,
49198c2ecf20Sopenharmony_ci	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
49208c2ecf20Sopenharmony_ci
49218c2ecf20Sopenharmony_ci	.bus_suspend = _dwc2_hcd_suspend,
49228c2ecf20Sopenharmony_ci	.bus_resume = _dwc2_hcd_resume,
49238c2ecf20Sopenharmony_ci
49248c2ecf20Sopenharmony_ci	.map_urb_for_dma	= dwc2_map_urb_for_dma,
49258c2ecf20Sopenharmony_ci	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
49268c2ecf20Sopenharmony_ci};
49278c2ecf20Sopenharmony_ci
49288c2ecf20Sopenharmony_ci/*
49298c2ecf20Sopenharmony_ci * Frees secondary storage associated with the dwc2_hsotg structure contained
49308c2ecf20Sopenharmony_ci * in the struct usb_hcd field
49318c2ecf20Sopenharmony_ci */
49328c2ecf20Sopenharmony_cistatic void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
49338c2ecf20Sopenharmony_ci{
49348c2ecf20Sopenharmony_ci	u32 ahbcfg;
49358c2ecf20Sopenharmony_ci	u32 dctl;
49368c2ecf20Sopenharmony_ci	int i;
49378c2ecf20Sopenharmony_ci
49388c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
49398c2ecf20Sopenharmony_ci
49408c2ecf20Sopenharmony_ci	/* Free memory for QH/QTD lists */
49418c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
49428c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
49438c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
49448c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
49458c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
49468c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
49478c2ecf20Sopenharmony_ci	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
49488c2ecf20Sopenharmony_ci
49498c2ecf20Sopenharmony_ci	/* Free memory for the host channels */
49508c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49518c2ecf20Sopenharmony_ci		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
49528c2ecf20Sopenharmony_ci
49538c2ecf20Sopenharmony_ci		if (chan) {
49548c2ecf20Sopenharmony_ci			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
49558c2ecf20Sopenharmony_ci				i, chan);
49568c2ecf20Sopenharmony_ci			hsotg->hc_ptr_array[i] = NULL;
49578c2ecf20Sopenharmony_ci			kfree(chan);
49588c2ecf20Sopenharmony_ci		}
49598c2ecf20Sopenharmony_ci	}
49608c2ecf20Sopenharmony_ci
49618c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma) {
49628c2ecf20Sopenharmony_ci		if (hsotg->status_buf) {
49638c2ecf20Sopenharmony_ci			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
49648c2ecf20Sopenharmony_ci					  hsotg->status_buf,
49658c2ecf20Sopenharmony_ci					  hsotg->status_buf_dma);
49668c2ecf20Sopenharmony_ci			hsotg->status_buf = NULL;
49678c2ecf20Sopenharmony_ci		}
49688c2ecf20Sopenharmony_ci	} else {
49698c2ecf20Sopenharmony_ci		kfree(hsotg->status_buf);
49708c2ecf20Sopenharmony_ci		hsotg->status_buf = NULL;
49718c2ecf20Sopenharmony_ci	}
49728c2ecf20Sopenharmony_ci
49738c2ecf20Sopenharmony_ci	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
49748c2ecf20Sopenharmony_ci
49758c2ecf20Sopenharmony_ci	/* Disable all interrupts */
49768c2ecf20Sopenharmony_ci	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
49778c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
49788c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0, GINTMSK);
49798c2ecf20Sopenharmony_ci
49808c2ecf20Sopenharmony_ci	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
49818c2ecf20Sopenharmony_ci		dctl = dwc2_readl(hsotg, DCTL);
49828c2ecf20Sopenharmony_ci		dctl |= DCTL_SFTDISCON;
49838c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, dctl, DCTL);
49848c2ecf20Sopenharmony_ci	}
49858c2ecf20Sopenharmony_ci
49868c2ecf20Sopenharmony_ci	if (hsotg->wq_otg) {
49878c2ecf20Sopenharmony_ci		if (!cancel_work_sync(&hsotg->wf_otg))
49888c2ecf20Sopenharmony_ci			flush_workqueue(hsotg->wq_otg);
49898c2ecf20Sopenharmony_ci		destroy_workqueue(hsotg->wq_otg);
49908c2ecf20Sopenharmony_ci	}
49918c2ecf20Sopenharmony_ci
49928c2ecf20Sopenharmony_ci	cancel_work_sync(&hsotg->phy_reset_work);
49938c2ecf20Sopenharmony_ci
49948c2ecf20Sopenharmony_ci	del_timer(&hsotg->wkp_timer);
49958c2ecf20Sopenharmony_ci}
49968c2ecf20Sopenharmony_ci
49978c2ecf20Sopenharmony_cistatic void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
49988c2ecf20Sopenharmony_ci{
49998c2ecf20Sopenharmony_ci	/* Turn off all host-specific interrupts */
50008c2ecf20Sopenharmony_ci	dwc2_disable_host_interrupts(hsotg);
50018c2ecf20Sopenharmony_ci
50028c2ecf20Sopenharmony_ci	dwc2_hcd_free(hsotg);
50038c2ecf20Sopenharmony_ci}
50048c2ecf20Sopenharmony_ci
50058c2ecf20Sopenharmony_ci/*
50068c2ecf20Sopenharmony_ci * Initializes the HCD. This function allocates memory for and initializes the
50078c2ecf20Sopenharmony_ci * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
50088c2ecf20Sopenharmony_ci * USB bus with the core and calls the hc_driver->start() function. It returns
50098c2ecf20Sopenharmony_ci * a negative error on failure.
50108c2ecf20Sopenharmony_ci */
50118c2ecf20Sopenharmony_ciint dwc2_hcd_init(struct dwc2_hsotg *hsotg)
50128c2ecf20Sopenharmony_ci{
50138c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(hsotg->dev);
50148c2ecf20Sopenharmony_ci	struct resource *res;
50158c2ecf20Sopenharmony_ci	struct usb_hcd *hcd;
50168c2ecf20Sopenharmony_ci	struct dwc2_host_chan *channel;
50178c2ecf20Sopenharmony_ci	u32 hcfg;
50188c2ecf20Sopenharmony_ci	int i, num_channels;
50198c2ecf20Sopenharmony_ci	int retval;
50208c2ecf20Sopenharmony_ci
50218c2ecf20Sopenharmony_ci	if (usb_disabled())
50228c2ecf20Sopenharmony_ci		return -ENODEV;
50238c2ecf20Sopenharmony_ci
50248c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
50258c2ecf20Sopenharmony_ci
50268c2ecf20Sopenharmony_ci	retval = -ENOMEM;
50278c2ecf20Sopenharmony_ci
50288c2ecf20Sopenharmony_ci	hcfg = dwc2_readl(hsotg, HCFG);
50298c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
50308c2ecf20Sopenharmony_ci
50318c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
50328c2ecf20Sopenharmony_ci	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
50338c2ecf20Sopenharmony_ci					 sizeof(*hsotg->frame_num_array),
50348c2ecf20Sopenharmony_ci					 GFP_KERNEL);
50358c2ecf20Sopenharmony_ci	if (!hsotg->frame_num_array)
50368c2ecf20Sopenharmony_ci		goto error1;
50378c2ecf20Sopenharmony_ci	hsotg->last_frame_num_array =
50388c2ecf20Sopenharmony_ci		kcalloc(FRAME_NUM_ARRAY_SIZE,
50398c2ecf20Sopenharmony_ci			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
50408c2ecf20Sopenharmony_ci	if (!hsotg->last_frame_num_array)
50418c2ecf20Sopenharmony_ci		goto error1;
50428c2ecf20Sopenharmony_ci#endif
50438c2ecf20Sopenharmony_ci	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
50448c2ecf20Sopenharmony_ci
50458c2ecf20Sopenharmony_ci	/* Check if the bus driver or platform code has setup a dma_mask */
50468c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma &&
50478c2ecf20Sopenharmony_ci	    !hsotg->dev->dma_mask) {
50488c2ecf20Sopenharmony_ci		dev_warn(hsotg->dev,
50498c2ecf20Sopenharmony_ci			 "dma_mask not set, disabling DMA\n");
50508c2ecf20Sopenharmony_ci		hsotg->params.host_dma = false;
50518c2ecf20Sopenharmony_ci		hsotg->params.dma_desc_enable = false;
50528c2ecf20Sopenharmony_ci	}
50538c2ecf20Sopenharmony_ci
50548c2ecf20Sopenharmony_ci	/* Set device flags indicating whether the HCD supports DMA */
50558c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma) {
50568c2ecf20Sopenharmony_ci		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
50578c2ecf20Sopenharmony_ci			dev_warn(hsotg->dev, "can't set DMA mask\n");
50588c2ecf20Sopenharmony_ci		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
50598c2ecf20Sopenharmony_ci			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
50608c2ecf20Sopenharmony_ci	}
50618c2ecf20Sopenharmony_ci
50628c2ecf20Sopenharmony_ci	if (hsotg->params.change_speed_quirk) {
50638c2ecf20Sopenharmony_ci		dwc2_hc_driver.free_dev = dwc2_free_dev;
50648c2ecf20Sopenharmony_ci		dwc2_hc_driver.reset_device = dwc2_reset_device;
50658c2ecf20Sopenharmony_ci	}
50668c2ecf20Sopenharmony_ci
50678c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma)
50688c2ecf20Sopenharmony_ci		dwc2_hc_driver.flags |= HCD_DMA;
50698c2ecf20Sopenharmony_ci
50708c2ecf20Sopenharmony_ci	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
50718c2ecf20Sopenharmony_ci	if (!hcd)
50728c2ecf20Sopenharmony_ci		goto error1;
50738c2ecf20Sopenharmony_ci
50748c2ecf20Sopenharmony_ci	hcd->has_tt = 1;
50758c2ecf20Sopenharmony_ci
50768c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
50778c2ecf20Sopenharmony_ci	if (!res) {
50788c2ecf20Sopenharmony_ci		retval = -EINVAL;
50798c2ecf20Sopenharmony_ci		goto error2;
50808c2ecf20Sopenharmony_ci	}
50818c2ecf20Sopenharmony_ci	hcd->rsrc_start = res->start;
50828c2ecf20Sopenharmony_ci	hcd->rsrc_len = resource_size(res);
50838c2ecf20Sopenharmony_ci
50848c2ecf20Sopenharmony_ci	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
50858c2ecf20Sopenharmony_ci	hsotg->priv = hcd;
50868c2ecf20Sopenharmony_ci
50878c2ecf20Sopenharmony_ci	/*
50888c2ecf20Sopenharmony_ci	 * Disable the global interrupt until all the interrupt handlers are
50898c2ecf20Sopenharmony_ci	 * installed
50908c2ecf20Sopenharmony_ci	 */
50918c2ecf20Sopenharmony_ci	dwc2_disable_global_interrupts(hsotg);
50928c2ecf20Sopenharmony_ci
50938c2ecf20Sopenharmony_ci	/* Initialize the DWC_otg core, and select the Phy type */
50948c2ecf20Sopenharmony_ci	retval = dwc2_core_init(hsotg, true);
50958c2ecf20Sopenharmony_ci	if (retval)
50968c2ecf20Sopenharmony_ci		goto error2;
50978c2ecf20Sopenharmony_ci
50988c2ecf20Sopenharmony_ci	/* Create new workqueue and init work */
50998c2ecf20Sopenharmony_ci	retval = -ENOMEM;
51008c2ecf20Sopenharmony_ci	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
51018c2ecf20Sopenharmony_ci	if (!hsotg->wq_otg) {
51028c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "Failed to create workqueue\n");
51038c2ecf20Sopenharmony_ci		goto error2;
51048c2ecf20Sopenharmony_ci	}
51058c2ecf20Sopenharmony_ci	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
51068c2ecf20Sopenharmony_ci
51078c2ecf20Sopenharmony_ci	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
51088c2ecf20Sopenharmony_ci
51098c2ecf20Sopenharmony_ci	/* Initialize the non-periodic schedule */
51108c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
51118c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
51128c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
51138c2ecf20Sopenharmony_ci
51148c2ecf20Sopenharmony_ci	/* Initialize the periodic schedule */
51158c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
51168c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
51178c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
51188c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
51198c2ecf20Sopenharmony_ci
51208c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->split_order);
51218c2ecf20Sopenharmony_ci
51228c2ecf20Sopenharmony_ci	/*
51238c2ecf20Sopenharmony_ci	 * Create a host channel descriptor for each host channel implemented
51248c2ecf20Sopenharmony_ci	 * in the controller. Initialize the channel descriptor array.
51258c2ecf20Sopenharmony_ci	 */
51268c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hsotg->free_hc_list);
51278c2ecf20Sopenharmony_ci	num_channels = hsotg->params.host_channels;
51288c2ecf20Sopenharmony_ci	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
51298c2ecf20Sopenharmony_ci
51308c2ecf20Sopenharmony_ci	for (i = 0; i < num_channels; i++) {
51318c2ecf20Sopenharmony_ci		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
51328c2ecf20Sopenharmony_ci		if (!channel)
51338c2ecf20Sopenharmony_ci			goto error3;
51348c2ecf20Sopenharmony_ci		channel->hc_num = i;
51358c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&channel->split_order_list_entry);
51368c2ecf20Sopenharmony_ci		hsotg->hc_ptr_array[i] = channel;
51378c2ecf20Sopenharmony_ci	}
51388c2ecf20Sopenharmony_ci
51398c2ecf20Sopenharmony_ci	/* Initialize work */
51408c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
51418c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
51428c2ecf20Sopenharmony_ci	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
51438c2ecf20Sopenharmony_ci
51448c2ecf20Sopenharmony_ci	/*
51458c2ecf20Sopenharmony_ci	 * Allocate space for storing data on status transactions. Normally no
51468c2ecf20Sopenharmony_ci	 * data is sent, but this space acts as a bit bucket. This must be
51478c2ecf20Sopenharmony_ci	 * done after usb_add_hcd since that function allocates the DMA buffer
51488c2ecf20Sopenharmony_ci	 * pool.
51498c2ecf20Sopenharmony_ci	 */
51508c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma)
51518c2ecf20Sopenharmony_ci		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
51528c2ecf20Sopenharmony_ci					DWC2_HCD_STATUS_BUF_SIZE,
51538c2ecf20Sopenharmony_ci					&hsotg->status_buf_dma, GFP_KERNEL);
51548c2ecf20Sopenharmony_ci	else
51558c2ecf20Sopenharmony_ci		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
51568c2ecf20Sopenharmony_ci					  GFP_KERNEL);
51578c2ecf20Sopenharmony_ci
51588c2ecf20Sopenharmony_ci	if (!hsotg->status_buf)
51598c2ecf20Sopenharmony_ci		goto error3;
51608c2ecf20Sopenharmony_ci
51618c2ecf20Sopenharmony_ci	/*
51628c2ecf20Sopenharmony_ci	 * Create kmem caches to handle descriptor buffers in descriptor
51638c2ecf20Sopenharmony_ci	 * DMA mode.
51648c2ecf20Sopenharmony_ci	 * Alignment must be set to 512 bytes.
51658c2ecf20Sopenharmony_ci	 */
51668c2ecf20Sopenharmony_ci	if (hsotg->params.dma_desc_enable ||
51678c2ecf20Sopenharmony_ci	    hsotg->params.dma_desc_fs_enable) {
51688c2ecf20Sopenharmony_ci		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
51698c2ecf20Sopenharmony_ci				sizeof(struct dwc2_dma_desc) *
51708c2ecf20Sopenharmony_ci				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
51718c2ecf20Sopenharmony_ci				NULL);
51728c2ecf20Sopenharmony_ci		if (!hsotg->desc_gen_cache) {
51738c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
51748c2ecf20Sopenharmony_ci				"unable to create dwc2 generic desc cache\n");
51758c2ecf20Sopenharmony_ci
51768c2ecf20Sopenharmony_ci			/*
51778c2ecf20Sopenharmony_ci			 * Disable descriptor dma mode since it will not be
51788c2ecf20Sopenharmony_ci			 * usable.
51798c2ecf20Sopenharmony_ci			 */
51808c2ecf20Sopenharmony_ci			hsotg->params.dma_desc_enable = false;
51818c2ecf20Sopenharmony_ci			hsotg->params.dma_desc_fs_enable = false;
51828c2ecf20Sopenharmony_ci		}
51838c2ecf20Sopenharmony_ci
51848c2ecf20Sopenharmony_ci		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
51858c2ecf20Sopenharmony_ci				sizeof(struct dwc2_dma_desc) *
51868c2ecf20Sopenharmony_ci				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
51878c2ecf20Sopenharmony_ci		if (!hsotg->desc_hsisoc_cache) {
51888c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
51898c2ecf20Sopenharmony_ci				"unable to create dwc2 hs isoc desc cache\n");
51908c2ecf20Sopenharmony_ci
51918c2ecf20Sopenharmony_ci			kmem_cache_destroy(hsotg->desc_gen_cache);
51928c2ecf20Sopenharmony_ci
51938c2ecf20Sopenharmony_ci			/*
51948c2ecf20Sopenharmony_ci			 * Disable descriptor dma mode since it will not be
51958c2ecf20Sopenharmony_ci			 * usable.
51968c2ecf20Sopenharmony_ci			 */
51978c2ecf20Sopenharmony_ci			hsotg->params.dma_desc_enable = false;
51988c2ecf20Sopenharmony_ci			hsotg->params.dma_desc_fs_enable = false;
51998c2ecf20Sopenharmony_ci		}
52008c2ecf20Sopenharmony_ci	}
52018c2ecf20Sopenharmony_ci
52028c2ecf20Sopenharmony_ci	if (hsotg->params.host_dma) {
52038c2ecf20Sopenharmony_ci		/*
52048c2ecf20Sopenharmony_ci		 * Create kmem caches to handle non-aligned buffer
52058c2ecf20Sopenharmony_ci		 * in Buffer DMA mode.
52068c2ecf20Sopenharmony_ci		 */
52078c2ecf20Sopenharmony_ci		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
52088c2ecf20Sopenharmony_ci						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
52098c2ecf20Sopenharmony_ci						SLAB_CACHE_DMA, NULL);
52108c2ecf20Sopenharmony_ci		if (!hsotg->unaligned_cache)
52118c2ecf20Sopenharmony_ci			dev_err(hsotg->dev,
52128c2ecf20Sopenharmony_ci				"unable to create dwc2 unaligned cache\n");
52138c2ecf20Sopenharmony_ci	}
52148c2ecf20Sopenharmony_ci
52158c2ecf20Sopenharmony_ci	hsotg->otg_port = 1;
52168c2ecf20Sopenharmony_ci	hsotg->frame_list = NULL;
52178c2ecf20Sopenharmony_ci	hsotg->frame_list_dma = 0;
52188c2ecf20Sopenharmony_ci	hsotg->periodic_qh_count = 0;
52198c2ecf20Sopenharmony_ci
52208c2ecf20Sopenharmony_ci	/* Initiate lx_state to L3 disconnected state */
52218c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L3;
52228c2ecf20Sopenharmony_ci
52238c2ecf20Sopenharmony_ci	hcd->self.otg_port = hsotg->otg_port;
52248c2ecf20Sopenharmony_ci
52258c2ecf20Sopenharmony_ci	/* Don't support SG list at this point */
52268c2ecf20Sopenharmony_ci	hcd->self.sg_tablesize = 0;
52278c2ecf20Sopenharmony_ci
52288c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(hsotg->uphy))
52298c2ecf20Sopenharmony_ci		otg_set_host(hsotg->uphy->otg, &hcd->self);
52308c2ecf20Sopenharmony_ci
52318c2ecf20Sopenharmony_ci	/*
52328c2ecf20Sopenharmony_ci	 * Finish generic HCD initialization and start the HCD. This function
52338c2ecf20Sopenharmony_ci	 * allocates the DMA buffer pool, registers the USB bus, requests the
52348c2ecf20Sopenharmony_ci	 * IRQ line, and calls hcd_start method.
52358c2ecf20Sopenharmony_ci	 */
52368c2ecf20Sopenharmony_ci	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
52378c2ecf20Sopenharmony_ci	if (retval < 0)
52388c2ecf20Sopenharmony_ci		goto error4;
52398c2ecf20Sopenharmony_ci
52408c2ecf20Sopenharmony_ci	device_wakeup_enable(hcd->self.controller);
52418c2ecf20Sopenharmony_ci
52428c2ecf20Sopenharmony_ci	dwc2_hcd_dump_state(hsotg);
52438c2ecf20Sopenharmony_ci
52448c2ecf20Sopenharmony_ci	dwc2_enable_global_interrupts(hsotg);
52458c2ecf20Sopenharmony_ci
52468c2ecf20Sopenharmony_ci	return 0;
52478c2ecf20Sopenharmony_ci
52488c2ecf20Sopenharmony_cierror4:
52498c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->unaligned_cache);
52508c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
52518c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->desc_gen_cache);
52528c2ecf20Sopenharmony_cierror3:
52538c2ecf20Sopenharmony_ci	dwc2_hcd_release(hsotg);
52548c2ecf20Sopenharmony_cierror2:
52558c2ecf20Sopenharmony_ci	usb_put_hcd(hcd);
52568c2ecf20Sopenharmony_cierror1:
52578c2ecf20Sopenharmony_ci
52588c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
52598c2ecf20Sopenharmony_ci	kfree(hsotg->last_frame_num_array);
52608c2ecf20Sopenharmony_ci	kfree(hsotg->frame_num_array);
52618c2ecf20Sopenharmony_ci#endif
52628c2ecf20Sopenharmony_ci
52638c2ecf20Sopenharmony_ci	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
52648c2ecf20Sopenharmony_ci	return retval;
52658c2ecf20Sopenharmony_ci}
52668c2ecf20Sopenharmony_ci
52678c2ecf20Sopenharmony_ci/*
52688c2ecf20Sopenharmony_ci * Removes the HCD.
52698c2ecf20Sopenharmony_ci * Frees memory and resources associated with the HCD and deregisters the bus.
52708c2ecf20Sopenharmony_ci */
52718c2ecf20Sopenharmony_civoid dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
52728c2ecf20Sopenharmony_ci{
52738c2ecf20Sopenharmony_ci	struct usb_hcd *hcd;
52748c2ecf20Sopenharmony_ci
52758c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
52768c2ecf20Sopenharmony_ci
52778c2ecf20Sopenharmony_ci	hcd = dwc2_hsotg_to_hcd(hsotg);
52788c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
52798c2ecf20Sopenharmony_ci
52808c2ecf20Sopenharmony_ci	if (!hcd) {
52818c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
52828c2ecf20Sopenharmony_ci			__func__);
52838c2ecf20Sopenharmony_ci		return;
52848c2ecf20Sopenharmony_ci	}
52858c2ecf20Sopenharmony_ci
52868c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(hsotg->uphy))
52878c2ecf20Sopenharmony_ci		otg_set_host(hsotg->uphy->otg, NULL);
52888c2ecf20Sopenharmony_ci
52898c2ecf20Sopenharmony_ci	usb_remove_hcd(hcd);
52908c2ecf20Sopenharmony_ci	hsotg->priv = NULL;
52918c2ecf20Sopenharmony_ci
52928c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->unaligned_cache);
52938c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
52948c2ecf20Sopenharmony_ci	kmem_cache_destroy(hsotg->desc_gen_cache);
52958c2ecf20Sopenharmony_ci
52968c2ecf20Sopenharmony_ci	dwc2_hcd_release(hsotg);
52978c2ecf20Sopenharmony_ci	usb_put_hcd(hcd);
52988c2ecf20Sopenharmony_ci
52998c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
53008c2ecf20Sopenharmony_ci	kfree(hsotg->last_frame_num_array);
53018c2ecf20Sopenharmony_ci	kfree(hsotg->frame_num_array);
53028c2ecf20Sopenharmony_ci#endif
53038c2ecf20Sopenharmony_ci}
53048c2ecf20Sopenharmony_ci
53058c2ecf20Sopenharmony_ci/**
53068c2ecf20Sopenharmony_ci * dwc2_backup_host_registers() - Backup controller host registers.
53078c2ecf20Sopenharmony_ci * When suspending usb bus, registers needs to be backuped
53088c2ecf20Sopenharmony_ci * if controller power is disabled once suspended.
53098c2ecf20Sopenharmony_ci *
53108c2ecf20Sopenharmony_ci * @hsotg: Programming view of the DWC_otg controller
53118c2ecf20Sopenharmony_ci */
53128c2ecf20Sopenharmony_ciint dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
53138c2ecf20Sopenharmony_ci{
53148c2ecf20Sopenharmony_ci	struct dwc2_hregs_backup *hr;
53158c2ecf20Sopenharmony_ci	int i;
53168c2ecf20Sopenharmony_ci
53178c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s\n", __func__);
53188c2ecf20Sopenharmony_ci
53198c2ecf20Sopenharmony_ci	/* Backup Host regs */
53208c2ecf20Sopenharmony_ci	hr = &hsotg->hr_backup;
53218c2ecf20Sopenharmony_ci	hr->hcfg = dwc2_readl(hsotg, HCFG);
53228c2ecf20Sopenharmony_ci	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
53238c2ecf20Sopenharmony_ci	for (i = 0; i < hsotg->params.host_channels; ++i)
53248c2ecf20Sopenharmony_ci		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
53258c2ecf20Sopenharmony_ci
53268c2ecf20Sopenharmony_ci	hr->hprt0 = dwc2_read_hprt0(hsotg);
53278c2ecf20Sopenharmony_ci	hr->hfir = dwc2_readl(hsotg, HFIR);
53288c2ecf20Sopenharmony_ci	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
53298c2ecf20Sopenharmony_ci	hr->valid = true;
53308c2ecf20Sopenharmony_ci
53318c2ecf20Sopenharmony_ci	return 0;
53328c2ecf20Sopenharmony_ci}
53338c2ecf20Sopenharmony_ci
53348c2ecf20Sopenharmony_ci/**
53358c2ecf20Sopenharmony_ci * dwc2_restore_host_registers() - Restore controller host registers.
53368c2ecf20Sopenharmony_ci * When resuming usb bus, device registers needs to be restored
53378c2ecf20Sopenharmony_ci * if controller power were disabled.
53388c2ecf20Sopenharmony_ci *
53398c2ecf20Sopenharmony_ci * @hsotg: Programming view of the DWC_otg controller
53408c2ecf20Sopenharmony_ci */
53418c2ecf20Sopenharmony_ciint dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
53428c2ecf20Sopenharmony_ci{
53438c2ecf20Sopenharmony_ci	struct dwc2_hregs_backup *hr;
53448c2ecf20Sopenharmony_ci	int i;
53458c2ecf20Sopenharmony_ci
53468c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s\n", __func__);
53478c2ecf20Sopenharmony_ci
53488c2ecf20Sopenharmony_ci	/* Restore host regs */
53498c2ecf20Sopenharmony_ci	hr = &hsotg->hr_backup;
53508c2ecf20Sopenharmony_ci	if (!hr->valid) {
53518c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "%s: no host registers to restore\n",
53528c2ecf20Sopenharmony_ci			__func__);
53538c2ecf20Sopenharmony_ci		return -EINVAL;
53548c2ecf20Sopenharmony_ci	}
53558c2ecf20Sopenharmony_ci	hr->valid = false;
53568c2ecf20Sopenharmony_ci
53578c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->hcfg, HCFG);
53588c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
53598c2ecf20Sopenharmony_ci
53608c2ecf20Sopenharmony_ci	for (i = 0; i < hsotg->params.host_channels; ++i)
53618c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
53628c2ecf20Sopenharmony_ci
53638c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->hprt0, HPRT0);
53648c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->hfir, HFIR);
53658c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
53668c2ecf20Sopenharmony_ci	hsotg->frame_number = 0;
53678c2ecf20Sopenharmony_ci
53688c2ecf20Sopenharmony_ci	return 0;
53698c2ecf20Sopenharmony_ci}
53708c2ecf20Sopenharmony_ci
53718c2ecf20Sopenharmony_ci/**
53728c2ecf20Sopenharmony_ci * dwc2_host_enter_hibernation() - Put controller in Hibernation.
53738c2ecf20Sopenharmony_ci *
53748c2ecf20Sopenharmony_ci * @hsotg: Programming view of the DWC_otg controller
53758c2ecf20Sopenharmony_ci */
53768c2ecf20Sopenharmony_ciint dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
53778c2ecf20Sopenharmony_ci{
53788c2ecf20Sopenharmony_ci	unsigned long flags;
53798c2ecf20Sopenharmony_ci	int ret = 0;
53808c2ecf20Sopenharmony_ci	u32 hprt0;
53818c2ecf20Sopenharmony_ci	u32 pcgcctl;
53828c2ecf20Sopenharmony_ci	u32 gusbcfg;
53838c2ecf20Sopenharmony_ci	u32 gpwrdn;
53848c2ecf20Sopenharmony_ci
53858c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
53868c2ecf20Sopenharmony_ci	ret = dwc2_backup_global_registers(hsotg);
53878c2ecf20Sopenharmony_ci	if (ret) {
53888c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
53898c2ecf20Sopenharmony_ci			__func__);
53908c2ecf20Sopenharmony_ci		return ret;
53918c2ecf20Sopenharmony_ci	}
53928c2ecf20Sopenharmony_ci	ret = dwc2_backup_host_registers(hsotg);
53938c2ecf20Sopenharmony_ci	if (ret) {
53948c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
53958c2ecf20Sopenharmony_ci			__func__);
53968c2ecf20Sopenharmony_ci		return ret;
53978c2ecf20Sopenharmony_ci	}
53988c2ecf20Sopenharmony_ci
53998c2ecf20Sopenharmony_ci	/* Enter USB Suspend Mode */
54008c2ecf20Sopenharmony_ci	hprt0 = dwc2_readl(hsotg, HPRT0);
54018c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_SUSP;
54028c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_ENA;
54038c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
54048c2ecf20Sopenharmony_ci
54058c2ecf20Sopenharmony_ci	/* Wait for the HPRT0.PrtSusp register field to be set */
54068c2ecf20Sopenharmony_ci	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
54078c2ecf20Sopenharmony_ci		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
54088c2ecf20Sopenharmony_ci
54098c2ecf20Sopenharmony_ci	/*
54108c2ecf20Sopenharmony_ci	 * We need to disable interrupts to prevent servicing of any IRQ
54118c2ecf20Sopenharmony_ci	 * during going to hibernation
54128c2ecf20Sopenharmony_ci	 */
54138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
54148c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L2;
54158c2ecf20Sopenharmony_ci
54168c2ecf20Sopenharmony_ci	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
54178c2ecf20Sopenharmony_ci	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
54188c2ecf20Sopenharmony_ci		/* ULPI interface */
54198c2ecf20Sopenharmony_ci		/* Suspend the Phy Clock */
54208c2ecf20Sopenharmony_ci		pcgcctl = dwc2_readl(hsotg, PCGCTL);
54218c2ecf20Sopenharmony_ci		pcgcctl |= PCGCTL_STOPPCLK;
54228c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, pcgcctl, PCGCTL);
54238c2ecf20Sopenharmony_ci		udelay(10);
54248c2ecf20Sopenharmony_ci
54258c2ecf20Sopenharmony_ci		gpwrdn = dwc2_readl(hsotg, GPWRDN);
54268c2ecf20Sopenharmony_ci		gpwrdn |= GPWRDN_PMUACTV;
54278c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gpwrdn, GPWRDN);
54288c2ecf20Sopenharmony_ci		udelay(10);
54298c2ecf20Sopenharmony_ci	} else {
54308c2ecf20Sopenharmony_ci		/* UTMI+ Interface */
54318c2ecf20Sopenharmony_ci		gpwrdn = dwc2_readl(hsotg, GPWRDN);
54328c2ecf20Sopenharmony_ci		gpwrdn |= GPWRDN_PMUACTV;
54338c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gpwrdn, GPWRDN);
54348c2ecf20Sopenharmony_ci		udelay(10);
54358c2ecf20Sopenharmony_ci
54368c2ecf20Sopenharmony_ci		pcgcctl = dwc2_readl(hsotg, PCGCTL);
54378c2ecf20Sopenharmony_ci		pcgcctl |= PCGCTL_STOPPCLK;
54388c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, pcgcctl, PCGCTL);
54398c2ecf20Sopenharmony_ci		udelay(10);
54408c2ecf20Sopenharmony_ci	}
54418c2ecf20Sopenharmony_ci
54428c2ecf20Sopenharmony_ci	/* Enable interrupts from wake up logic */
54438c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
54448c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_PMUINTSEL;
54458c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
54468c2ecf20Sopenharmony_ci	udelay(10);
54478c2ecf20Sopenharmony_ci
54488c2ecf20Sopenharmony_ci	/* Unmask host mode interrupts in GPWRDN */
54498c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
54508c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
54518c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
54528c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
54538c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
54548c2ecf20Sopenharmony_ci	udelay(10);
54558c2ecf20Sopenharmony_ci
54568c2ecf20Sopenharmony_ci	/* Enable Power Down Clamp */
54578c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
54588c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_PWRDNCLMP;
54598c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
54608c2ecf20Sopenharmony_ci	udelay(10);
54618c2ecf20Sopenharmony_ci
54628c2ecf20Sopenharmony_ci	/* Switch off VDD */
54638c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
54648c2ecf20Sopenharmony_ci	gpwrdn |= GPWRDN_PWRDNSWTCH;
54658c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
54668c2ecf20Sopenharmony_ci
54678c2ecf20Sopenharmony_ci	hsotg->hibernated = 1;
54688c2ecf20Sopenharmony_ci	hsotg->bus_suspended = 1;
54698c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Host hibernation completed\n");
54708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
54718c2ecf20Sopenharmony_ci	return ret;
54728c2ecf20Sopenharmony_ci}
54738c2ecf20Sopenharmony_ci
54748c2ecf20Sopenharmony_ci/*
54758c2ecf20Sopenharmony_ci * dwc2_host_exit_hibernation()
54768c2ecf20Sopenharmony_ci *
54778c2ecf20Sopenharmony_ci * @hsotg: Programming view of the DWC_otg controller
54788c2ecf20Sopenharmony_ci * @rem_wakeup: indicates whether resume is initiated by Device or Host.
54798c2ecf20Sopenharmony_ci * @param reset: indicates whether resume is initiated by Reset.
54808c2ecf20Sopenharmony_ci *
54818c2ecf20Sopenharmony_ci * Return: non-zero if failed to enter to hibernation.
54828c2ecf20Sopenharmony_ci *
54838c2ecf20Sopenharmony_ci * This function is for exiting from Host mode hibernation by
54848c2ecf20Sopenharmony_ci * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
54858c2ecf20Sopenharmony_ci */
54868c2ecf20Sopenharmony_ciint dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
54878c2ecf20Sopenharmony_ci			       int reset)
54888c2ecf20Sopenharmony_ci{
54898c2ecf20Sopenharmony_ci	u32 gpwrdn;
54908c2ecf20Sopenharmony_ci	u32 hprt0;
54918c2ecf20Sopenharmony_ci	int ret = 0;
54928c2ecf20Sopenharmony_ci	struct dwc2_gregs_backup *gr;
54938c2ecf20Sopenharmony_ci	struct dwc2_hregs_backup *hr;
54948c2ecf20Sopenharmony_ci
54958c2ecf20Sopenharmony_ci	gr = &hsotg->gr_backup;
54968c2ecf20Sopenharmony_ci	hr = &hsotg->hr_backup;
54978c2ecf20Sopenharmony_ci
54988c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev,
54998c2ecf20Sopenharmony_ci		"%s: called with rem_wakeup = %d reset = %d\n",
55008c2ecf20Sopenharmony_ci		__func__, rem_wakeup, reset);
55018c2ecf20Sopenharmony_ci
55028c2ecf20Sopenharmony_ci	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
55038c2ecf20Sopenharmony_ci	hsotg->hibernated = 0;
55048c2ecf20Sopenharmony_ci
55058c2ecf20Sopenharmony_ci	/*
55068c2ecf20Sopenharmony_ci	 * This step is not described in functional spec but if not wait for
55078c2ecf20Sopenharmony_ci	 * this delay, mismatch interrupts occurred because just after restore
55088c2ecf20Sopenharmony_ci	 * core is in Device mode(gintsts.curmode == 0)
55098c2ecf20Sopenharmony_ci	 */
55108c2ecf20Sopenharmony_ci	mdelay(100);
55118c2ecf20Sopenharmony_ci
55128c2ecf20Sopenharmony_ci	/* Clear all pending interupts */
55138c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
55148c2ecf20Sopenharmony_ci
55158c2ecf20Sopenharmony_ci	/* De-assert Restore */
55168c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
55178c2ecf20Sopenharmony_ci	gpwrdn &= ~GPWRDN_RESTORE;
55188c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
55198c2ecf20Sopenharmony_ci	udelay(10);
55208c2ecf20Sopenharmony_ci
55218c2ecf20Sopenharmony_ci	/* Restore GUSBCFG, HCFG */
55228c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
55238c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hr->hcfg, HCFG);
55248c2ecf20Sopenharmony_ci
55258c2ecf20Sopenharmony_ci	/* De-assert Wakeup Logic */
55268c2ecf20Sopenharmony_ci	gpwrdn = dwc2_readl(hsotg, GPWRDN);
55278c2ecf20Sopenharmony_ci	gpwrdn &= ~GPWRDN_PMUACTV;
55288c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gpwrdn, GPWRDN);
55298c2ecf20Sopenharmony_ci	udelay(10);
55308c2ecf20Sopenharmony_ci
55318c2ecf20Sopenharmony_ci	hprt0 = hr->hprt0;
55328c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_PWR;
55338c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_ENA;
55348c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_SUSP;
55358c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
55368c2ecf20Sopenharmony_ci
55378c2ecf20Sopenharmony_ci	hprt0 = hr->hprt0;
55388c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_PWR;
55398c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_ENA;
55408c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_SUSP;
55418c2ecf20Sopenharmony_ci
55428c2ecf20Sopenharmony_ci	if (reset) {
55438c2ecf20Sopenharmony_ci		hprt0 |= HPRT0_RST;
55448c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hprt0, HPRT0);
55458c2ecf20Sopenharmony_ci
55468c2ecf20Sopenharmony_ci		/* Wait for Resume time and then program HPRT again */
55478c2ecf20Sopenharmony_ci		mdelay(60);
55488c2ecf20Sopenharmony_ci		hprt0 &= ~HPRT0_RST;
55498c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hprt0, HPRT0);
55508c2ecf20Sopenharmony_ci	} else {
55518c2ecf20Sopenharmony_ci		hprt0 |= HPRT0_RES;
55528c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hprt0, HPRT0);
55538c2ecf20Sopenharmony_ci
55548c2ecf20Sopenharmony_ci		/* Wait for Resume time and then program HPRT again */
55558c2ecf20Sopenharmony_ci		mdelay(100);
55568c2ecf20Sopenharmony_ci		hprt0 &= ~HPRT0_RES;
55578c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, hprt0, HPRT0);
55588c2ecf20Sopenharmony_ci	}
55598c2ecf20Sopenharmony_ci	/* Clear all interrupt status */
55608c2ecf20Sopenharmony_ci	hprt0 = dwc2_readl(hsotg, HPRT0);
55618c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_CONNDET;
55628c2ecf20Sopenharmony_ci	hprt0 |= HPRT0_ENACHG;
55638c2ecf20Sopenharmony_ci	hprt0 &= ~HPRT0_ENA;
55648c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, hprt0, HPRT0);
55658c2ecf20Sopenharmony_ci
55668c2ecf20Sopenharmony_ci	hprt0 = dwc2_readl(hsotg, HPRT0);
55678c2ecf20Sopenharmony_ci
55688c2ecf20Sopenharmony_ci	/* Clear all pending interupts */
55698c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
55708c2ecf20Sopenharmony_ci
55718c2ecf20Sopenharmony_ci	/* Restore global registers */
55728c2ecf20Sopenharmony_ci	ret = dwc2_restore_global_registers(hsotg);
55738c2ecf20Sopenharmony_ci	if (ret) {
55748c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "%s: failed to restore registers\n",
55758c2ecf20Sopenharmony_ci			__func__);
55768c2ecf20Sopenharmony_ci		return ret;
55778c2ecf20Sopenharmony_ci	}
55788c2ecf20Sopenharmony_ci
55798c2ecf20Sopenharmony_ci	/* Restore host registers */
55808c2ecf20Sopenharmony_ci	ret = dwc2_restore_host_registers(hsotg);
55818c2ecf20Sopenharmony_ci	if (ret) {
55828c2ecf20Sopenharmony_ci		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
55838c2ecf20Sopenharmony_ci			__func__);
55848c2ecf20Sopenharmony_ci		return ret;
55858c2ecf20Sopenharmony_ci	}
55868c2ecf20Sopenharmony_ci
55878c2ecf20Sopenharmony_ci	if (rem_wakeup) {
55888c2ecf20Sopenharmony_ci		dwc2_hcd_rem_wakeup(hsotg);
55898c2ecf20Sopenharmony_ci		/*
55908c2ecf20Sopenharmony_ci		 * Change "port_connect_status_change" flag to re-enumerate,
55918c2ecf20Sopenharmony_ci		 * because after exit from hibernation port connection status
55928c2ecf20Sopenharmony_ci		 * is not detected.
55938c2ecf20Sopenharmony_ci		 */
55948c2ecf20Sopenharmony_ci		hsotg->flags.b.port_connect_status_change = 1;
55958c2ecf20Sopenharmony_ci	}
55968c2ecf20Sopenharmony_ci
55978c2ecf20Sopenharmony_ci	hsotg->hibernated = 0;
55988c2ecf20Sopenharmony_ci	hsotg->bus_suspended = 0;
55998c2ecf20Sopenharmony_ci	hsotg->lx_state = DWC2_L0;
56008c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
56018c2ecf20Sopenharmony_ci	return ret;
56028c2ecf20Sopenharmony_ci}
56038c2ecf20Sopenharmony_ci
56048c2ecf20Sopenharmony_cibool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
56058c2ecf20Sopenharmony_ci{
56068c2ecf20Sopenharmony_ci	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
56078c2ecf20Sopenharmony_ci
56088c2ecf20Sopenharmony_ci	/* If the controller isn't allowed to wakeup then we can power off. */
56098c2ecf20Sopenharmony_ci	if (!device_may_wakeup(dwc2->dev))
56108c2ecf20Sopenharmony_ci		return true;
56118c2ecf20Sopenharmony_ci
56128c2ecf20Sopenharmony_ci	/*
56138c2ecf20Sopenharmony_ci	 * We don't want to power off the PHY if something under the
56148c2ecf20Sopenharmony_ci	 * root hub has wakeup enabled.
56158c2ecf20Sopenharmony_ci	 */
56168c2ecf20Sopenharmony_ci	if (usb_wakeup_enabled_descendants(root_hub))
56178c2ecf20Sopenharmony_ci		return false;
56188c2ecf20Sopenharmony_ci
56198c2ecf20Sopenharmony_ci	/* No reason to keep the PHY powered, so allow poweroff */
56208c2ecf20Sopenharmony_ci	return true;
56218c2ecf20Sopenharmony_ci}
5622