Linux Audio

Check our new training course

Loading...
v4.6
 
   1/*
   2 * core.c - DesignWare HS OTG Controller common routines
   3 *
   4 * Copyright (C) 2004-2013 Synopsys, Inc.
   5 *
   6 * Redistribution and use in source and binary forms, with or without
   7 * modification, are permitted provided that the following conditions
   8 * are met:
   9 * 1. Redistributions of source code must retain the above copyright
  10 *    notice, this list of conditions, and the following disclaimer,
  11 *    without modification.
  12 * 2. Redistributions in binary form must reproduce the above copyright
  13 *    notice, this list of conditions and the following disclaimer in the
  14 *    documentation and/or other materials provided with the distribution.
  15 * 3. The names of the above-listed copyright holders may not be used
  16 *    to endorse or promote products derived from this software without
  17 *    specific prior written permission.
  18 *
  19 * ALTERNATIVELY, this software may be distributed under the terms of the
  20 * GNU General Public License ("GPL") as published by the Free Software
  21 * Foundation; either version 2 of the License, or (at your option) any
  22 * later version.
  23 *
  24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35 */
  36
  37/*
  38 * The Core code provides basic services for accessing and managing the
  39 * DWC_otg hardware. These services are used by both the Host Controller
  40 * Driver and the Peripheral Controller Driver.
  41 */
  42#include <linux/kernel.h>
  43#include <linux/module.h>
  44#include <linux/moduleparam.h>
  45#include <linux/spinlock.h>
  46#include <linux/interrupt.h>
  47#include <linux/dma-mapping.h>
  48#include <linux/delay.h>
  49#include <linux/io.h>
  50#include <linux/slab.h>
  51#include <linux/usb.h>
  52
  53#include <linux/usb/hcd.h>
  54#include <linux/usb/ch11.h>
  55
  56#include "core.h"
  57#include "hcd.h"
  58
  59/**
  60 * dwc2_backup_global_registers() - Backup global controller registers.
  61 * When suspending usb bus, registers needs to be backuped
  62 * if controller power is disabled once suspended.
  63 *
  64 * @hsotg: Programming view of the DWC_otg controller
  65 */
  66static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  67{
  68	struct dwc2_gregs_backup *gr;
  69	int i;
 
  70
  71	/* Backup global regs */
  72	gr = &hsotg->gr_backup;
  73
  74	gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  75	gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  76	gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  77	gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  78	gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  79	gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  80	gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  81	gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  82	for (i = 0; i < MAX_EPS_CHANNELS; i++)
  83		gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
 
  84
  85	gr->valid = true;
  86	return 0;
  87}
  88
  89/**
  90 * dwc2_restore_global_registers() - Restore controller global registers.
  91 * When resuming usb bus, device registers needs to be restored
  92 * if controller power were disabled.
  93 *
  94 * @hsotg: Programming view of the DWC_otg controller
  95 */
  96static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  97{
  98	struct dwc2_gregs_backup *gr;
  99	int i;
 100
 101	dev_dbg(hsotg->dev, "%s\n", __func__);
 102
 103	/* Restore global regs */
 104	gr = &hsotg->gr_backup;
 105	if (!gr->valid) {
 106		dev_err(hsotg->dev, "%s: no global registers to restore\n",
 107				__func__);
 108		return -EINVAL;
 109	}
 110	gr->valid = false;
 111
 112	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
 113	dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
 114	dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
 115	dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
 116	dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
 117	dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
 118	dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
 119	dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
 120	dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
 121	for (i = 0; i < MAX_EPS_CHANNELS; i++)
 122		dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
 
 123
 124	return 0;
 125}
 126
 127/**
 128 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
 129 *
 130 * @hsotg: Programming view of the DWC_otg controller
 
 131 * @restore: Controller registers need to be restored
 132 */
 133int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 134{
 135	u32 pcgcctl;
 136	int ret = 0;
 
 
 137
 138	if (!hsotg->core_params->hibernation)
 139		return -ENOTSUPP;
 
 140
 141	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
 142	pcgcctl &= ~PCGCTL_STOPPCLK;
 143	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 144
 145	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
 146	pcgcctl &= ~PCGCTL_PWRCLMP;
 147	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 148
 149	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
 150	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
 151	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 152
 153	udelay(100);
 154	if (restore) {
 155		ret = dwc2_restore_global_registers(hsotg);
 156		if (ret) {
 157			dev_err(hsotg->dev, "%s: failed to restore registers\n",
 158					__func__);
 159			return ret;
 160		}
 161		if (dwc2_is_host_mode(hsotg)) {
 162			ret = dwc2_restore_host_registers(hsotg);
 163			if (ret) {
 164				dev_err(hsotg->dev, "%s: failed to restore host registers\n",
 165						__func__);
 166				return ret;
 167			}
 168		} else {
 169			ret = dwc2_restore_device_registers(hsotg);
 170			if (ret) {
 171				dev_err(hsotg->dev, "%s: failed to restore device registers\n",
 172						__func__);
 173				return ret;
 174			}
 175		}
 176	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 177
 178	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 179}
 180
 181/**
 182 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
 183 *
 184 * @hsotg: Programming view of the DWC_otg controller
 
 
 185 */
 186int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
 
 187{
 188	u32 pcgcctl;
 189	int ret = 0;
 190
 191	if (!hsotg->core_params->hibernation)
 192		return -ENOTSUPP;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 193
 194	/* Backup all registers */
 195	ret = dwc2_backup_global_registers(hsotg);
 196	if (ret) {
 197		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
 198				__func__);
 199		return ret;
 200	}
 201
 202	if (dwc2_is_host_mode(hsotg)) {
 203		ret = dwc2_backup_host_registers(hsotg);
 204		if (ret) {
 205			dev_err(hsotg->dev, "%s: failed to backup host registers\n",
 206					__func__);
 207			return ret;
 208		}
 209	} else {
 210		ret = dwc2_backup_device_registers(hsotg);
 211		if (ret) {
 212			dev_err(hsotg->dev, "%s: failed to backup device registers\n",
 213					__func__);
 214			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 215		}
 
 
 216	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 217
 218	/*
 219	 * Clear any pending interrupts since dwc2 will not be able to
 220	 * clear them after entering hibernation.
 221	 */
 222	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
 223
 224	/* Put the controller in low power state */
 225	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
 226
 227	pcgcctl |= PCGCTL_PWRCLMP;
 228	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 229	ndelay(20);
 230
 231	pcgcctl |= PCGCTL_RSTPDWNMODULE;
 232	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 233	ndelay(20);
 234
 235	pcgcctl |= PCGCTL_STOPPCLK;
 236	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 237
 238	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 239}
 240
 241/*
 242 * Do core a soft reset of the core.  Be careful with this because it
 243 * resets all the internal state machines of the core.
 244 */
 245int dwc2_core_reset(struct dwc2_hsotg *hsotg)
 246{
 247	u32 greset;
 248	int count = 0;
 249
 250	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 252	/* Core Soft Reset */
 253	greset = dwc2_readl(hsotg->regs + GRSTCTL);
 254	greset |= GRSTCTL_CSFTRST;
 255	dwc2_writel(greset, hsotg->regs + GRSTCTL);
 256	do {
 257		udelay(1);
 258		greset = dwc2_readl(hsotg->regs + GRSTCTL);
 259		if (++count > 50) {
 260			dev_warn(hsotg->dev,
 261				 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
 262				 __func__, greset);
 263			return -EBUSY;
 264		}
 265	} while (greset & GRSTCTL_CSFTRST);
 266
 267	/* Wait for AHB master IDLE state */
 268	count = 0;
 269	do {
 270		udelay(1);
 271		greset = dwc2_readl(hsotg->regs + GRSTCTL);
 272		if (++count > 50) {
 273			dev_warn(hsotg->dev,
 274				 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
 275				 __func__, greset);
 276			return -EBUSY;
 277		}
 278	} while (!(greset & GRSTCTL_AHBIDLE));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 279
 280	return 0;
 281}
 282
 283/*
 284 * Force the mode of the controller.
 285 *
 286 * Forcing the mode is needed for two cases:
 287 *
 288 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
 289 * controller to stay in a particular mode regardless of ID pin
 290 * changes. We do this usually after a core reset.
 291 *
 292 * 2) During probe we want to read reset values of the hw
 293 * configuration registers that are only available in either host or
 294 * device mode. We may need to force the mode if the current mode does
 295 * not allow us to access the register in the mode that we want.
 296 *
 297 * In either case it only makes sense to force the mode if the
 298 * controller hardware is OTG capable.
 299 *
 300 * Checks are done in this function to determine whether doing a force
 301 * would be valid or not.
 302 *
 303 * If a force is done, it requires a 25ms delay to take effect.
 
 
 304 *
 305 * Returns true if the mode was forced.
 
 306 */
 307static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
 308{
 309	u32 gusbcfg;
 310	u32 set;
 311	u32 clear;
 312
 313	dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
 314
 315	/*
 316	 * Force mode has no effect if the hardware is not OTG.
 317	 */
 318	if (!dwc2_hw_is_otg(hsotg))
 319		return false;
 320
 321	/*
 322	 * If dr_mode is either peripheral or host only, there is no
 323	 * need to ever force the mode to the opposite mode.
 324	 */
 325	if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
 326		return false;
 327
 328	if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
 329		return false;
 330
 331	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
 332
 333	set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
 334	clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
 335
 336	gusbcfg &= ~clear;
 337	gusbcfg |= set;
 338	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
 339
 340	msleep(25);
 341	return true;
 342}
 343
 344/*
 345 * Clears the force mode bits.
 
 
 
 
 
 
 
 
 346 */
 347static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
 348{
 349	u32 gusbcfg;
 350
 351	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
 
 
 
 
 
 352	gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
 353	gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
 354	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
 355
 356	/*
 357	 * NOTE: This long sleep is _very_ important, otherwise the core will
 358	 * not stay in host mode after a connector ID change!
 359	 */
 360	msleep(25);
 361}
 362
 363/*
 364 * Sets or clears force mode based on the dr_mode parameter.
 365 */
 366void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
 367{
 368	switch (hsotg->dr_mode) {
 369	case USB_DR_MODE_HOST:
 370		dwc2_force_mode(hsotg, true);
 
 
 
 
 
 
 371		break;
 372	case USB_DR_MODE_PERIPHERAL:
 373		dwc2_force_mode(hsotg, false);
 374		break;
 375	case USB_DR_MODE_OTG:
 376		dwc2_clear_force_mode(hsotg);
 377		break;
 378	default:
 379		dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
 380			 __func__, hsotg->dr_mode);
 381		break;
 382	}
 383
 384	/*
 385	 * NOTE: This is required for some rockchip soc based
 386	 * platforms.
 387	 */
 388	msleep(50);
 389}
 390
 391/*
 392 * Do core a soft reset of the core.  Be careful with this because it
 393 * resets all the internal state machines of the core.
 394 *
 395 * Additionally this will apply force mode as per the hsotg->dr_mode
 396 * parameter.
 397 */
 398int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
 399{
 400	int retval;
 
 401
 402	retval = dwc2_core_reset(hsotg);
 403	if (retval)
 404		return retval;
 405
 406	dwc2_force_dr_mode(hsotg);
 407	return 0;
 408}
 409
 410/**
 411 * dwc2_dump_host_registers() - Prints the host registers
 412 *
 413 * @hsotg: Programming view of DWC_otg controller
 414 *
 415 * NOTE: This function will be removed once the peripheral controller code
 416 * is integrated and the driver is stable
 417 */
 418void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
 419{
 420#ifdef DEBUG
 421	u32 __iomem *addr;
 422	int i;
 423
 424	dev_dbg(hsotg->dev, "Host Global Registers\n");
 425	addr = hsotg->regs + HCFG;
 426	dev_dbg(hsotg->dev, "HCFG	 @0x%08lX : 0x%08X\n",
 427		(unsigned long)addr, dwc2_readl(addr));
 428	addr = hsotg->regs + HFIR;
 429	dev_dbg(hsotg->dev, "HFIR	 @0x%08lX : 0x%08X\n",
 430		(unsigned long)addr, dwc2_readl(addr));
 431	addr = hsotg->regs + HFNUM;
 432	dev_dbg(hsotg->dev, "HFNUM	 @0x%08lX : 0x%08X\n",
 433		(unsigned long)addr, dwc2_readl(addr));
 434	addr = hsotg->regs + HPTXSTS;
 435	dev_dbg(hsotg->dev, "HPTXSTS	 @0x%08lX : 0x%08X\n",
 436		(unsigned long)addr, dwc2_readl(addr));
 437	addr = hsotg->regs + HAINT;
 438	dev_dbg(hsotg->dev, "HAINT	 @0x%08lX : 0x%08X\n",
 439		(unsigned long)addr, dwc2_readl(addr));
 440	addr = hsotg->regs + HAINTMSK;
 441	dev_dbg(hsotg->dev, "HAINTMSK	 @0x%08lX : 0x%08X\n",
 442		(unsigned long)addr, dwc2_readl(addr));
 443	if (hsotg->core_params->dma_desc_enable > 0) {
 444		addr = hsotg->regs + HFLBADDR;
 445		dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
 446			(unsigned long)addr, dwc2_readl(addr));
 447	}
 448
 449	addr = hsotg->regs + HPRT0;
 450	dev_dbg(hsotg->dev, "HPRT0	 @0x%08lX : 0x%08X\n",
 451		(unsigned long)addr, dwc2_readl(addr));
 452
 453	for (i = 0; i < hsotg->core_params->host_channels; i++) {
 454		dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
 455		addr = hsotg->regs + HCCHAR(i);
 456		dev_dbg(hsotg->dev, "HCCHAR	 @0x%08lX : 0x%08X\n",
 457			(unsigned long)addr, dwc2_readl(addr));
 458		addr = hsotg->regs + HCSPLT(i);
 459		dev_dbg(hsotg->dev, "HCSPLT	 @0x%08lX : 0x%08X\n",
 460			(unsigned long)addr, dwc2_readl(addr));
 461		addr = hsotg->regs + HCINT(i);
 462		dev_dbg(hsotg->dev, "HCINT	 @0x%08lX : 0x%08X\n",
 463			(unsigned long)addr, dwc2_readl(addr));
 464		addr = hsotg->regs + HCINTMSK(i);
 465		dev_dbg(hsotg->dev, "HCINTMSK	 @0x%08lX : 0x%08X\n",
 466			(unsigned long)addr, dwc2_readl(addr));
 467		addr = hsotg->regs + HCTSIZ(i);
 468		dev_dbg(hsotg->dev, "HCTSIZ	 @0x%08lX : 0x%08X\n",
 469			(unsigned long)addr, dwc2_readl(addr));
 470		addr = hsotg->regs + HCDMA(i);
 471		dev_dbg(hsotg->dev, "HCDMA	 @0x%08lX : 0x%08X\n",
 472			(unsigned long)addr, dwc2_readl(addr));
 473		if (hsotg->core_params->dma_desc_enable > 0) {
 474			addr = hsotg->regs + HCDMAB(i);
 475			dev_dbg(hsotg->dev, "HCDMAB	 @0x%08lX : 0x%08X\n",
 476				(unsigned long)addr, dwc2_readl(addr));
 
 477		}
 478	}
 479#endif
 480}
 481
 482/**
 483 * dwc2_dump_global_registers() - Prints the core global registers
 484 *
 485 * @hsotg: Programming view of DWC_otg controller
 486 *
 487 * NOTE: This function will be removed once the peripheral controller code
 488 * is integrated and the driver is stable
 489 */
 490void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
 491{
 492#ifdef DEBUG
 493	u32 __iomem *addr;
 494
 495	dev_dbg(hsotg->dev, "Core Global Registers\n");
 496	addr = hsotg->regs + GOTGCTL;
 497	dev_dbg(hsotg->dev, "GOTGCTL	 @0x%08lX : 0x%08X\n",
 498		(unsigned long)addr, dwc2_readl(addr));
 499	addr = hsotg->regs + GOTGINT;
 500	dev_dbg(hsotg->dev, "GOTGINT	 @0x%08lX : 0x%08X\n",
 501		(unsigned long)addr, dwc2_readl(addr));
 502	addr = hsotg->regs + GAHBCFG;
 503	dev_dbg(hsotg->dev, "GAHBCFG	 @0x%08lX : 0x%08X\n",
 504		(unsigned long)addr, dwc2_readl(addr));
 505	addr = hsotg->regs + GUSBCFG;
 506	dev_dbg(hsotg->dev, "GUSBCFG	 @0x%08lX : 0x%08X\n",
 507		(unsigned long)addr, dwc2_readl(addr));
 508	addr = hsotg->regs + GRSTCTL;
 509	dev_dbg(hsotg->dev, "GRSTCTL	 @0x%08lX : 0x%08X\n",
 510		(unsigned long)addr, dwc2_readl(addr));
 511	addr = hsotg->regs + GINTSTS;
 512	dev_dbg(hsotg->dev, "GINTSTS	 @0x%08lX : 0x%08X\n",
 513		(unsigned long)addr, dwc2_readl(addr));
 514	addr = hsotg->regs + GINTMSK;
 515	dev_dbg(hsotg->dev, "GINTMSK	 @0x%08lX : 0x%08X\n",
 516		(unsigned long)addr, dwc2_readl(addr));
 517	addr = hsotg->regs + GRXSTSR;
 518	dev_dbg(hsotg->dev, "GRXSTSR	 @0x%08lX : 0x%08X\n",
 519		(unsigned long)addr, dwc2_readl(addr));
 520	addr = hsotg->regs + GRXFSIZ;
 521	dev_dbg(hsotg->dev, "GRXFSIZ	 @0x%08lX : 0x%08X\n",
 522		(unsigned long)addr, dwc2_readl(addr));
 523	addr = hsotg->regs + GNPTXFSIZ;
 524	dev_dbg(hsotg->dev, "GNPTXFSIZ	 @0x%08lX : 0x%08X\n",
 525		(unsigned long)addr, dwc2_readl(addr));
 526	addr = hsotg->regs + GNPTXSTS;
 527	dev_dbg(hsotg->dev, "GNPTXSTS	 @0x%08lX : 0x%08X\n",
 528		(unsigned long)addr, dwc2_readl(addr));
 529	addr = hsotg->regs + GI2CCTL;
 530	dev_dbg(hsotg->dev, "GI2CCTL	 @0x%08lX : 0x%08X\n",
 531		(unsigned long)addr, dwc2_readl(addr));
 532	addr = hsotg->regs + GPVNDCTL;
 533	dev_dbg(hsotg->dev, "GPVNDCTL	 @0x%08lX : 0x%08X\n",
 534		(unsigned long)addr, dwc2_readl(addr));
 535	addr = hsotg->regs + GGPIO;
 536	dev_dbg(hsotg->dev, "GGPIO	 @0x%08lX : 0x%08X\n",
 537		(unsigned long)addr, dwc2_readl(addr));
 538	addr = hsotg->regs + GUID;
 539	dev_dbg(hsotg->dev, "GUID	 @0x%08lX : 0x%08X\n",
 540		(unsigned long)addr, dwc2_readl(addr));
 541	addr = hsotg->regs + GSNPSID;
 542	dev_dbg(hsotg->dev, "GSNPSID	 @0x%08lX : 0x%08X\n",
 543		(unsigned long)addr, dwc2_readl(addr));
 544	addr = hsotg->regs + GHWCFG1;
 545	dev_dbg(hsotg->dev, "GHWCFG1	 @0x%08lX : 0x%08X\n",
 546		(unsigned long)addr, dwc2_readl(addr));
 547	addr = hsotg->regs + GHWCFG2;
 548	dev_dbg(hsotg->dev, "GHWCFG2	 @0x%08lX : 0x%08X\n",
 549		(unsigned long)addr, dwc2_readl(addr));
 550	addr = hsotg->regs + GHWCFG3;
 551	dev_dbg(hsotg->dev, "GHWCFG3	 @0x%08lX : 0x%08X\n",
 552		(unsigned long)addr, dwc2_readl(addr));
 553	addr = hsotg->regs + GHWCFG4;
 554	dev_dbg(hsotg->dev, "GHWCFG4	 @0x%08lX : 0x%08X\n",
 555		(unsigned long)addr, dwc2_readl(addr));
 556	addr = hsotg->regs + GLPMCFG;
 557	dev_dbg(hsotg->dev, "GLPMCFG	 @0x%08lX : 0x%08X\n",
 558		(unsigned long)addr, dwc2_readl(addr));
 559	addr = hsotg->regs + GPWRDN;
 560	dev_dbg(hsotg->dev, "GPWRDN	 @0x%08lX : 0x%08X\n",
 561		(unsigned long)addr, dwc2_readl(addr));
 562	addr = hsotg->regs + GDFIFOCFG;
 563	dev_dbg(hsotg->dev, "GDFIFOCFG	 @0x%08lX : 0x%08X\n",
 564		(unsigned long)addr, dwc2_readl(addr));
 565	addr = hsotg->regs + HPTXFSIZ;
 566	dev_dbg(hsotg->dev, "HPTXFSIZ	 @0x%08lX : 0x%08X\n",
 567		(unsigned long)addr, dwc2_readl(addr));
 568
 569	addr = hsotg->regs + PCGCTL;
 570	dev_dbg(hsotg->dev, "PCGCTL	 @0x%08lX : 0x%08X\n",
 571		(unsigned long)addr, dwc2_readl(addr));
 572#endif
 573}
 574
 575/**
 576 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
 577 *
 578 * @hsotg: Programming view of DWC_otg controller
 579 * @num:   Tx FIFO to flush
 580 */
 581void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
 582{
 583	u32 greset;
 584	int count = 0;
 585
 586	dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
 587
 
 
 
 
 
 588	greset = GRSTCTL_TXFFLSH;
 589	greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
 590	dwc2_writel(greset, hsotg->regs + GRSTCTL);
 591
 592	do {
 593		greset = dwc2_readl(hsotg->regs + GRSTCTL);
 594		if (++count > 10000) {
 595			dev_warn(hsotg->dev,
 596				 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
 597				 __func__, greset,
 598				 dwc2_readl(hsotg->regs + GNPTXSTS));
 599			break;
 600		}
 601		udelay(1);
 602	} while (greset & GRSTCTL_TXFFLSH);
 603
 604	/* Wait for at least 3 PHY Clocks */
 605	udelay(1);
 606}
 607
 608/**
 609 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
 610 *
 611 * @hsotg: Programming view of DWC_otg controller
 612 */
 613void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
 614{
 615	u32 greset;
 616	int count = 0;
 617
 618	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 619
 
 
 
 
 
 620	greset = GRSTCTL_RXFFLSH;
 621	dwc2_writel(greset, hsotg->regs + GRSTCTL);
 622
 623	do {
 624		greset = dwc2_readl(hsotg->regs + GRSTCTL);
 625		if (++count > 10000) {
 626			dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
 627				 __func__, greset);
 628			break;
 629		}
 630		udelay(1);
 631	} while (greset & GRSTCTL_RXFFLSH);
 632
 633	/* Wait for at least 3 PHY Clocks */
 634	udelay(1);
 635}
 636
 637#define DWC2_OUT_OF_BOUNDS(a, b, c)	((a) < (b) || (a) > (c))
 638
 639/* Parameter access functions */
 640void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
 641{
 642	int valid = 1;
 643
 644	switch (val) {
 645	case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
 646		if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
 647			valid = 0;
 648		break;
 649	case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
 650		switch (hsotg->hw_params.op_mode) {
 651		case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
 652		case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
 653		case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
 654		case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
 655			break;
 656		default:
 657			valid = 0;
 658			break;
 659		}
 660		break;
 661	case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
 662		/* always valid */
 663		break;
 664	default:
 665		valid = 0;
 666		break;
 667	}
 668
 669	if (!valid) {
 670		if (val >= 0)
 671			dev_err(hsotg->dev,
 672				"%d invalid for otg_cap parameter. Check HW configuration.\n",
 673				val);
 674		switch (hsotg->hw_params.op_mode) {
 675		case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
 676			val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
 677			break;
 678		case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
 679		case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
 680		case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
 681			val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
 682			break;
 683		default:
 684			val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
 685			break;
 686		}
 687		dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
 688	}
 689
 690	hsotg->core_params->otg_cap = val;
 691}
 692
 693void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
 694{
 695	int valid = 1;
 696
 697	if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
 698		valid = 0;
 699	if (val < 0)
 700		valid = 0;
 701
 702	if (!valid) {
 703		if (val >= 0)
 704			dev_err(hsotg->dev,
 705				"%d invalid for dma_enable parameter. Check HW configuration.\n",
 706				val);
 707		val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
 708		dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
 709	}
 710
 711	hsotg->core_params->dma_enable = val;
 712}
 713
 714void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
 
 
 
 
 
 
 715{
 716	int valid = 1;
 717
 718	if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
 719			!hsotg->hw_params.dma_desc_enable))
 720		valid = 0;
 721	if (val < 0)
 722		valid = 0;
 723
 724	if (!valid) {
 725		if (val >= 0)
 726			dev_err(hsotg->dev,
 727				"%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
 728				val);
 729		val = (hsotg->core_params->dma_enable > 0 &&
 730			hsotg->hw_params.dma_desc_enable);
 731		dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
 732	}
 733
 734	hsotg->core_params->dma_desc_enable = val;
 
 735}
 736
 737void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
 
 
 
 
 
 
 738{
 739	int valid = 1;
 740
 741	if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
 742			!hsotg->hw_params.dma_desc_enable))
 743		valid = 0;
 744	if (val < 0)
 745		valid = 0;
 746
 747	if (!valid) {
 748		if (val >= 0)
 749			dev_err(hsotg->dev,
 750				"%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
 751				val);
 752		val = (hsotg->core_params->dma_enable > 0 &&
 753			hsotg->hw_params.dma_desc_enable);
 754	}
 755
 756	hsotg->core_params->dma_desc_fs_enable = val;
 757	dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
 758}
 759
 760void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
 761						 int val)
 762{
 763	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
 764		if (val >= 0) {
 765			dev_err(hsotg->dev,
 766				"Wrong value for host_support_fs_low_power\n");
 767			dev_err(hsotg->dev,
 768				"host_support_fs_low_power must be 0 or 1\n");
 769		}
 770		val = 0;
 771		dev_dbg(hsotg->dev,
 772			"Setting host_support_fs_low_power to %d\n", val);
 773	}
 774
 775	hsotg->core_params->host_support_fs_ls_low_power = val;
 
 776}
 777
 778void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
 
 779{
 780	int valid = 1;
 781
 782	if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
 783		valid = 0;
 784	if (val < 0)
 785		valid = 0;
 786
 787	if (!valid) {
 788		if (val >= 0)
 789			dev_err(hsotg->dev,
 790				"%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
 791				val);
 792		val = hsotg->hw_params.enable_dynamic_fifo;
 793		dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
 794	}
 795
 796	hsotg->core_params->enable_dynamic_fifo = val;
 
 
 797}
 798
 799void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
 
 800{
 801	int valid = 1;
 802
 803	if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
 804		valid = 0;
 805
 806	if (!valid) {
 807		if (val >= 0)
 808			dev_err(hsotg->dev,
 809				"%d invalid for host_rx_fifo_size. Check HW configuration.\n",
 810				val);
 811		val = hsotg->hw_params.host_rx_fifo_size;
 812		dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
 813	}
 814
 815	hsotg->core_params->host_rx_fifo_size = val;
 
 816}
 817
 818void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
 
 819{
 820	int valid = 1;
 821
 822	if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
 823		valid = 0;
 824
 825	if (!valid) {
 826		if (val >= 0)
 827			dev_err(hsotg->dev,
 828				"%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
 829				val);
 830		val = hsotg->hw_params.host_nperio_tx_fifo_size;
 831		dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
 832			val);
 833	}
 834
 835	hsotg->core_params->host_nperio_tx_fifo_size = val;
 
 836}
 837
 838void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
 839{
 840	int valid = 1;
 841
 842	if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
 843		valid = 0;
 844
 845	if (!valid) {
 846		if (val >= 0)
 847			dev_err(hsotg->dev,
 848				"%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
 849				val);
 850		val = hsotg->hw_params.host_perio_tx_fifo_size;
 851		dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
 852			val);
 
 
 
 853	}
 854
 855	hsotg->core_params->host_perio_tx_fifo_size = val;
 856}
 857
 858void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
 859{
 860	int valid = 1;
 861
 862	if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
 863		valid = 0;
 864
 865	if (!valid) {
 866		if (val >= 0)
 867			dev_err(hsotg->dev,
 868				"%d invalid for max_transfer_size. Check HW configuration.\n",
 869				val);
 870		val = hsotg->hw_params.max_transfer_size;
 871		dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
 
 
 
 
 872	}
 873
 874	hsotg->core_params->max_transfer_size = val;
 875}
 876
 877void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
 
 
 
 
 878{
 879	int valid = 1;
 880
 881	if (val < 15 || val > hsotg->hw_params.max_packet_count)
 882		valid = 0;
 883
 884	if (!valid) {
 885		if (val >= 0)
 886			dev_err(hsotg->dev,
 887				"%d invalid for max_packet_count. Check HW configuration.\n",
 888				val);
 889		val = hsotg->hw_params.max_packet_count;
 890		dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
 
 
 891	}
 892
 893	hsotg->core_params->max_packet_count = val;
 
 
 
 
 894}
 895
 896void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
 897{
 898	int valid = 1;
 899
 900	if (val < 1 || val > hsotg->hw_params.host_channels)
 901		valid = 0;
 902
 903	if (!valid) {
 904		if (val >= 0)
 905			dev_err(hsotg->dev,
 906				"%d invalid for host_channels. Check HW configuration.\n",
 907				val);
 908		val = hsotg->hw_params.host_channels;
 909		dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
 910	}
 911
 912	hsotg->core_params->host_channels = val;
 913}
 914
 915void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
 916{
 917	int valid = 0;
 918	u32 hs_phy_type, fs_phy_type;
 919
 920	if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
 921			       DWC2_PHY_TYPE_PARAM_ULPI)) {
 922		if (val >= 0) {
 923			dev_err(hsotg->dev, "Wrong value for phy_type\n");
 924			dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
 925		}
 
 926
 927		valid = 0;
 
 
 
 
 
 
 928	}
 929
 930	hs_phy_type = hsotg->hw_params.hs_phy_type;
 931	fs_phy_type = hsotg->hw_params.fs_phy_type;
 932	if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
 933	    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
 934	     hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
 935		valid = 1;
 936	else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
 937		 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
 938		  hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
 939		valid = 1;
 940	else if (val == DWC2_PHY_TYPE_PARAM_FS &&
 941		 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
 942		valid = 1;
 943
 944	if (!valid) {
 945		if (val >= 0)
 946			dev_err(hsotg->dev,
 947				"%d invalid for phy_type. Check HW configuration.\n",
 948				val);
 949		val = DWC2_PHY_TYPE_PARAM_FS;
 950		if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
 951			if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
 952			    hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
 953				val = DWC2_PHY_TYPE_PARAM_UTMI;
 954			else
 955				val = DWC2_PHY_TYPE_PARAM_ULPI;
 956		}
 957		dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
 958	}
 959
 960	hsotg->core_params->phy_type = val;
 
 
 
 961}
 962
 963static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
 964{
 965	return hsotg->core_params->phy_type;
 966}
 967
 968void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
 969{
 970	int valid = 1;
 
 
 
 971
 972	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
 973		if (val >= 0) {
 974			dev_err(hsotg->dev, "Wrong value for speed parameter\n");
 975			dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
 
 
 
 
 
 
 
 
 
 
 
 976		}
 977		valid = 0;
 978	}
 979
 980	if (val == DWC2_SPEED_PARAM_HIGH &&
 981	    dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
 982		valid = 0;
 983
 984	if (!valid) {
 985		if (val >= 0)
 986			dev_err(hsotg->dev,
 987				"%d invalid for speed parameter. Check HW configuration.\n",
 988				val);
 989		val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
 990				DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
 991		dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
 992	}
 993
 994	hsotg->core_params->speed = val;
 995}
 996
 997void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
 998{
 999	int valid = 1;
1000
1001	if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
1002			       DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
1003		if (val >= 0) {
1004			dev_err(hsotg->dev,
1005				"Wrong value for host_ls_low_power_phy_clk parameter\n");
1006			dev_err(hsotg->dev,
1007				"host_ls_low_power_phy_clk must be 0 or 1\n");
1008		}
1009		valid = 0;
1010	}
1011
1012	if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
1013	    dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
1014		valid = 0;
 
 
 
 
1015
1016	if (!valid) {
1017		if (val >= 0)
1018			dev_err(hsotg->dev,
1019				"%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
1020				val);
1021		val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
1022			? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
1023			: DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
1024		dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
1025			val);
1026	}
1027
1028	hsotg->core_params->host_ls_low_power_phy_clk = val;
1029}
 
 
1030
1031void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
1032{
1033	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1034		if (val >= 0) {
1035			dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
1036			dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
1037		}
1038		val = 0;
1039		dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
1040	}
1041
1042	hsotg->core_params->phy_ulpi_ddr = val;
1043}
1044
1045void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
1046{
1047	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1048		if (val >= 0) {
1049			dev_err(hsotg->dev,
1050				"Wrong value for phy_ulpi_ext_vbus\n");
1051			dev_err(hsotg->dev,
1052				"phy_ulpi_ext_vbus must be 0 or 1\n");
1053		}
1054		val = 0;
1055		dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
1056	}
1057
1058	hsotg->core_params->phy_ulpi_ext_vbus = val;
1059}
1060
1061void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
1062{
1063	int valid = 0;
1064
1065	switch (hsotg->hw_params.utmi_phy_data_width) {
1066	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
1067		valid = (val == 8);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1068		break;
1069	case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
1070		valid = (val == 16);
 
 
 
 
1071		break;
1072	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
1073		valid = (val == 8 || val == 16);
1074		break;
1075	}
1076
1077	if (!valid) {
1078		if (val >= 0) {
1079			dev_err(hsotg->dev,
1080				"%d invalid for phy_utmi_width. Check HW configuration.\n",
1081				val);
1082		}
1083		val = (hsotg->hw_params.utmi_phy_data_width ==
1084		       GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
1085		dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
1086	}
1087
1088	hsotg->core_params->phy_utmi_width = val;
1089}
1090
1091void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
1092{
1093	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1094		if (val >= 0) {
1095			dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
1096			dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
1097		}
1098		val = 0;
1099		dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
1100	}
1101
1102	hsotg->core_params->ulpi_fs_ls = val;
1103}
1104
1105void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
1106{
1107	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1108		if (val >= 0) {
1109			dev_err(hsotg->dev, "Wrong value for ts_dline\n");
1110			dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
1111		}
1112		val = 0;
1113		dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
1114	}
1115
1116	hsotg->core_params->ts_dline = val;
1117}
1118
1119void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
1120{
1121	int valid = 1;
1122
1123	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1124		if (val >= 0) {
1125			dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
1126			dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
1127		}
1128
1129		valid = 0;
1130	}
1131
1132	if (val == 1 && !(hsotg->hw_params.i2c_enable))
1133		valid = 0;
1134
1135	if (!valid) {
1136		if (val >= 0)
1137			dev_err(hsotg->dev,
1138				"%d invalid for i2c_enable. Check HW configuration.\n",
1139				val);
1140		val = hsotg->hw_params.i2c_enable;
1141		dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
1142	}
1143
1144	hsotg->core_params->i2c_enable = val;
1145}
1146
1147void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
1148{
1149	int valid = 1;
1150
1151	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1152		if (val >= 0) {
1153			dev_err(hsotg->dev,
1154				"Wrong value for en_multiple_tx_fifo,\n");
1155			dev_err(hsotg->dev,
1156				"en_multiple_tx_fifo must be 0 or 1\n");
 
1157		}
1158		valid = 0;
1159	}
1160
1161	if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
1162		valid = 0;
1163
1164	if (!valid) {
1165		if (val >= 0)
1166			dev_err(hsotg->dev,
1167				"%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
1168				val);
1169		val = hsotg->hw_params.en_multiple_tx_fifo;
1170		dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
1171	}
1172
1173	hsotg->core_params->en_multiple_tx_fifo = val;
1174}
1175
1176void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
1177{
1178	int valid = 1;
1179
1180	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1181		if (val >= 0) {
1182			dev_err(hsotg->dev,
1183				"'%d' invalid for parameter reload_ctl\n", val);
1184			dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
1185		}
1186		valid = 0;
1187	}
1188
1189	if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
1190		valid = 0;
1191
1192	if (!valid) {
1193		if (val >= 0)
1194			dev_err(hsotg->dev,
1195				"%d invalid for parameter reload_ctl. Check HW configuration.\n",
1196				val);
1197		val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
1198		dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
1199	}
1200
1201	hsotg->core_params->reload_ctl = val;
1202}
1203
1204void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
1205{
1206	if (val != -1)
1207		hsotg->core_params->ahbcfg = val;
1208	else
1209		hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
1210						GAHBCFG_HBSTLEN_SHIFT;
1211}
1212
1213void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
1214{
1215	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1216		if (val >= 0) {
1217			dev_err(hsotg->dev,
1218				"'%d' invalid for parameter otg_ver\n", val);
1219			dev_err(hsotg->dev,
1220				"otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
1221		}
1222		val = 0;
1223		dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
1224	}
1225
1226	hsotg->core_params->otg_ver = val;
1227}
1228
1229static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
1230{
1231	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1232		if (val >= 0) {
1233			dev_err(hsotg->dev,
1234				"'%d' invalid for parameter uframe_sched\n",
1235				val);
1236			dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
1237		}
1238		val = 1;
1239		dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
1240	}
1241
1242	hsotg->core_params->uframe_sched = val;
1243}
1244
1245static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
1246		int val)
1247{
1248	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1249		if (val >= 0) {
1250			dev_err(hsotg->dev,
1251				"'%d' invalid for parameter external_id_pin_ctl\n",
1252				val);
1253			dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
1254		}
1255		val = 0;
1256		dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1257	}
1258
1259	hsotg->core_params->external_id_pin_ctl = val;
1260}
1261
1262static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
1263		int val)
1264{
1265	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1266		if (val >= 0) {
1267			dev_err(hsotg->dev,
1268				"'%d' invalid for parameter hibernation\n",
1269				val);
1270			dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
1271		}
1272		val = 0;
1273		dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
1274	}
1275
1276	hsotg->core_params->hibernation = val;
1277}
1278
1279/*
1280 * This function is called during module intialization to pass module parameters
1281 * for the DWC_otg core.
1282 */
1283void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
1284			 const struct dwc2_core_params *params)
1285{
1286	dev_dbg(hsotg->dev, "%s()\n", __func__);
1287
1288	dwc2_set_param_otg_cap(hsotg, params->otg_cap);
1289	dwc2_set_param_dma_enable(hsotg, params->dma_enable);
1290	dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
1291	dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
1292	dwc2_set_param_host_support_fs_ls_low_power(hsotg,
1293			params->host_support_fs_ls_low_power);
1294	dwc2_set_param_enable_dynamic_fifo(hsotg,
1295			params->enable_dynamic_fifo);
1296	dwc2_set_param_host_rx_fifo_size(hsotg,
1297			params->host_rx_fifo_size);
1298	dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
1299			params->host_nperio_tx_fifo_size);
1300	dwc2_set_param_host_perio_tx_fifo_size(hsotg,
1301			params->host_perio_tx_fifo_size);
1302	dwc2_set_param_max_transfer_size(hsotg,
1303			params->max_transfer_size);
1304	dwc2_set_param_max_packet_count(hsotg,
1305			params->max_packet_count);
1306	dwc2_set_param_host_channels(hsotg, params->host_channels);
1307	dwc2_set_param_phy_type(hsotg, params->phy_type);
1308	dwc2_set_param_speed(hsotg, params->speed);
1309	dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
1310			params->host_ls_low_power_phy_clk);
1311	dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
1312	dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
1313			params->phy_ulpi_ext_vbus);
1314	dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
1315	dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
1316	dwc2_set_param_ts_dline(hsotg, params->ts_dline);
1317	dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
1318	dwc2_set_param_en_multiple_tx_fifo(hsotg,
1319			params->en_multiple_tx_fifo);
1320	dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
1321	dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
1322	dwc2_set_param_otg_ver(hsotg, params->otg_ver);
1323	dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
1324	dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
1325	dwc2_set_param_hibernation(hsotg, params->hibernation);
1326}
1327
1328/*
1329 * Forces either host or device mode if the controller is not
1330 * currently in that mode.
1331 *
1332 * Returns true if the mode was forced.
1333 */
1334static bool dwc2_force_mode_if_needed(struct dwc2_hsotg *hsotg, bool host)
1335{
1336	if (host && dwc2_is_host_mode(hsotg))
1337		return false;
1338	else if (!host && dwc2_is_device_mode(hsotg))
1339		return false;
1340
1341	return dwc2_force_mode(hsotg, host);
1342}
1343
1344/*
1345 * Gets host hardware parameters. Forces host mode if not currently in
1346 * host mode. Should be called immediately after a core soft reset in
1347 * order to get the reset values.
1348 */
1349static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
1350{
1351	struct dwc2_hw_params *hw = &hsotg->hw_params;
1352	u32 gnptxfsiz;
1353	u32 hptxfsiz;
1354	bool forced;
1355
1356	if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
1357		return;
1358
1359	forced = dwc2_force_mode_if_needed(hsotg, true);
1360
1361	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
1362	hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
1363	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
1364	dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
1365
1366	if (forced)
1367		dwc2_clear_force_mode(hsotg);
1368
1369	hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1370				       FIFOSIZE_DEPTH_SHIFT;
1371	hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1372				      FIFOSIZE_DEPTH_SHIFT;
1373}
1374
1375/*
1376 * Gets device hardware parameters. Forces device mode if not
1377 * currently in device mode. Should be called immediately after a core
1378 * soft reset in order to get the reset values.
1379 */
1380static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
1381{
1382	struct dwc2_hw_params *hw = &hsotg->hw_params;
1383	bool forced;
1384	u32 gnptxfsiz;
1385
1386	if (hsotg->dr_mode == USB_DR_MODE_HOST)
1387		return;
1388
1389	forced = dwc2_force_mode_if_needed(hsotg, false);
1390
1391	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
1392	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
1393
1394	if (forced)
1395		dwc2_clear_force_mode(hsotg);
1396
1397	hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1398				       FIFOSIZE_DEPTH_SHIFT;
1399}
1400
1401/**
1402 * During device initialization, read various hardware configuration
1403 * registers and interpret the contents.
1404 */
1405int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
1406{
1407	struct dwc2_hw_params *hw = &hsotg->hw_params;
1408	unsigned width;
1409	u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
1410	u32 grxfsiz;
1411
1412	/*
1413	 * Attempt to ensure this device is really a DWC_otg Controller.
1414	 * Read and verify the GSNPSID register contents. The value should be
1415	 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
1416	 * as in "OTG version 2.xx" or "OTG version 3.xx".
1417	 */
1418	hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
1419	if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
1420	    (hw->snpsid & 0xfffff000) != 0x4f543000) {
1421		dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
1422			hw->snpsid);
1423		return -ENODEV;
1424	}
1425
1426	dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
1427		hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
1428		hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
1429
1430	hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
1431	hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
1432	hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
1433	hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
1434	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
1435
1436	dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
1437	dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
1438	dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
1439	dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
1440	dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
1441
1442	/*
1443	 * Host specific hardware parameters. Reading these parameters
1444	 * requires the controller to be in host mode. The mode will
1445	 * be forced, if necessary, to read these values.
1446	 */
1447	dwc2_get_host_hwparams(hsotg);
1448	dwc2_get_dev_hwparams(hsotg);
1449
1450	/* hwcfg1 */
1451	hw->dev_ep_dirs = hwcfg1;
1452
1453	/* hwcfg2 */
1454	hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
1455		      GHWCFG2_OP_MODE_SHIFT;
1456	hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
1457		   GHWCFG2_ARCHITECTURE_SHIFT;
1458	hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
1459	hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
1460				GHWCFG2_NUM_HOST_CHAN_SHIFT);
1461	hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
1462			  GHWCFG2_HS_PHY_TYPE_SHIFT;
1463	hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
1464			  GHWCFG2_FS_PHY_TYPE_SHIFT;
1465	hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
1466			 GHWCFG2_NUM_DEV_EP_SHIFT;
1467	hw->nperio_tx_q_depth =
1468		(hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
1469		GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
1470	hw->host_perio_tx_q_depth =
1471		(hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
1472		GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
1473	hw->dev_token_q_depth =
1474		(hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
1475		GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
1476
1477	/* hwcfg3 */
1478	width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
1479		GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
1480	hw->max_transfer_size = (1 << (width + 11)) - 1;
1481	width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
1482		GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
1483	hw->max_packet_count = (1 << (width + 4)) - 1;
1484	hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
1485	hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
1486			      GHWCFG3_DFIFO_DEPTH_SHIFT;
1487
1488	/* hwcfg4 */
1489	hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
1490	hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
1491				  GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
1492	hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
1493	hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
1494	hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
1495				  GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
1496
1497	/* fifo sizes */
1498	hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
1499				GRXFSIZ_DEPTH_SHIFT;
1500
1501	dev_dbg(hsotg->dev, "Detected values from hardware:\n");
1502	dev_dbg(hsotg->dev, "  op_mode=%d\n",
1503		hw->op_mode);
1504	dev_dbg(hsotg->dev, "  arch=%d\n",
1505		hw->arch);
1506	dev_dbg(hsotg->dev, "  dma_desc_enable=%d\n",
1507		hw->dma_desc_enable);
1508	dev_dbg(hsotg->dev, "  power_optimized=%d\n",
1509		hw->power_optimized);
1510	dev_dbg(hsotg->dev, "  i2c_enable=%d\n",
1511		hw->i2c_enable);
1512	dev_dbg(hsotg->dev, "  hs_phy_type=%d\n",
1513		hw->hs_phy_type);
1514	dev_dbg(hsotg->dev, "  fs_phy_type=%d\n",
1515		hw->fs_phy_type);
1516	dev_dbg(hsotg->dev, "  utmi_phy_data_width=%d\n",
1517		hw->utmi_phy_data_width);
1518	dev_dbg(hsotg->dev, "  num_dev_ep=%d\n",
1519		hw->num_dev_ep);
1520	dev_dbg(hsotg->dev, "  num_dev_perio_in_ep=%d\n",
1521		hw->num_dev_perio_in_ep);
1522	dev_dbg(hsotg->dev, "  host_channels=%d\n",
1523		hw->host_channels);
1524	dev_dbg(hsotg->dev, "  max_transfer_size=%d\n",
1525		hw->max_transfer_size);
1526	dev_dbg(hsotg->dev, "  max_packet_count=%d\n",
1527		hw->max_packet_count);
1528	dev_dbg(hsotg->dev, "  nperio_tx_q_depth=0x%0x\n",
1529		hw->nperio_tx_q_depth);
1530	dev_dbg(hsotg->dev, "  host_perio_tx_q_depth=0x%0x\n",
1531		hw->host_perio_tx_q_depth);
1532	dev_dbg(hsotg->dev, "  dev_token_q_depth=0x%0x\n",
1533		hw->dev_token_q_depth);
1534	dev_dbg(hsotg->dev, "  enable_dynamic_fifo=%d\n",
1535		hw->enable_dynamic_fifo);
1536	dev_dbg(hsotg->dev, "  en_multiple_tx_fifo=%d\n",
1537		hw->en_multiple_tx_fifo);
1538	dev_dbg(hsotg->dev, "  total_fifo_size=%d\n",
1539		hw->total_fifo_size);
1540	dev_dbg(hsotg->dev, "  host_rx_fifo_size=%d\n",
1541		hw->host_rx_fifo_size);
1542	dev_dbg(hsotg->dev, "  host_nperio_tx_fifo_size=%d\n",
1543		hw->host_nperio_tx_fifo_size);
1544	dev_dbg(hsotg->dev, "  host_perio_tx_fifo_size=%d\n",
1545		hw->host_perio_tx_fifo_size);
1546	dev_dbg(hsotg->dev, "\n");
1547
1548	return 0;
1549}
1550
1551/*
1552 * Sets all parameters to the given value.
1553 *
1554 * Assumes that the dwc2_core_params struct contains only integers.
1555 */
1556void dwc2_set_all_params(struct dwc2_core_params *params, int value)
1557{
1558	int *p = (int *)params;
1559	size_t size = sizeof(*params) / sizeof(*p);
1560	int i;
1561
1562	for (i = 0; i < size; i++)
1563		p[i] = value;
1564}
1565
1566
1567u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
1568{
1569	return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
1570}
1571
1572bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
1573{
1574	if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
1575		return false;
1576	else
1577		return true;
1578}
1579
1580/**
1581 * dwc2_enable_global_interrupts() - Enables the controller's Global
1582 * Interrupt in the AHB Config register
1583 *
1584 * @hsotg: Programming view of DWC_otg controller
1585 */
1586void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
1587{
1588	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
1589
1590	ahbcfg |= GAHBCFG_GLBL_INTR_EN;
1591	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
1592}
1593
1594/**
1595 * dwc2_disable_global_interrupts() - Disables the controller's Global
1596 * Interrupt in the AHB Config register
1597 *
1598 * @hsotg: Programming view of DWC_otg controller
1599 */
1600void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
1601{
1602	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
1603
1604	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
1605	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
1606}
1607
1608/* Returns the controller's GHWCFG2.OTG_MODE. */
1609unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
1610{
1611	u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
1612
1613	return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
1614		GHWCFG2_OP_MODE_SHIFT;
1615}
1616
1617/* Returns true if the controller is capable of DRD. */
1618bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
1619{
1620	unsigned op_mode = dwc2_op_mode(hsotg);
1621
1622	return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
1623		(op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
1624		(op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
1625}
1626
1627/* Returns true if the controller is host-only. */
1628bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
1629{
1630	unsigned op_mode = dwc2_op_mode(hsotg);
1631
1632	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
1633		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
1634}
1635
1636/* Returns true if the controller is device-only. */
1637bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
1638{
1639	unsigned op_mode = dwc2_op_mode(hsotg);
1640
1641	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
1642		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
1643}
1644
1645MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
1646MODULE_AUTHOR("Synopsys, Inc.");
1647MODULE_LICENSE("Dual BSD/GPL");
v6.13.7
   1// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
   2/*
   3 * core.c - DesignWare HS OTG Controller common routines
   4 *
   5 * Copyright (C) 2004-2013 Synopsys, Inc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   6 */
   7
   8/*
   9 * The Core code provides basic services for accessing and managing the
  10 * DWC_otg hardware. These services are used by both the Host Controller
  11 * Driver and the Peripheral Controller Driver.
  12 */
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/moduleparam.h>
  16#include <linux/spinlock.h>
  17#include <linux/interrupt.h>
  18#include <linux/dma-mapping.h>
  19#include <linux/delay.h>
  20#include <linux/io.h>
  21#include <linux/slab.h>
  22#include <linux/usb.h>
  23
  24#include <linux/usb/hcd.h>
  25#include <linux/usb/ch11.h>
  26
  27#include "core.h"
  28#include "hcd.h"
  29
  30/**
  31 * dwc2_backup_global_registers() - Backup global controller registers.
  32 * When suspending usb bus, registers needs to be backuped
  33 * if controller power is disabled once suspended.
  34 *
  35 * @hsotg: Programming view of the DWC_otg controller
  36 */
  37int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  38{
  39	struct dwc2_gregs_backup *gr;
  40
  41	dev_dbg(hsotg->dev, "%s\n", __func__);
  42
  43	/* Backup global regs */
  44	gr = &hsotg->gr_backup;
  45
  46	gr->gotgctl = dwc2_readl(hsotg, GOTGCTL);
  47	gr->gintmsk = dwc2_readl(hsotg, GINTMSK);
  48	gr->gahbcfg = dwc2_readl(hsotg, GAHBCFG);
  49	gr->gusbcfg = dwc2_readl(hsotg, GUSBCFG);
  50	gr->grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  51	gr->gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
  52	gr->gdfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
  53	gr->pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
  54	gr->glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  55	gr->gi2cctl = dwc2_readl(hsotg, GI2CCTL);
  56	gr->pcgcctl = dwc2_readl(hsotg, PCGCTL);
  57
  58	gr->valid = true;
  59	return 0;
  60}
  61
  62/**
  63 * dwc2_restore_global_registers() - Restore controller global registers.
  64 * When resuming usb bus, device registers needs to be restored
  65 * if controller power were disabled.
  66 *
  67 * @hsotg: Programming view of the DWC_otg controller
  68 */
  69int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  70{
  71	struct dwc2_gregs_backup *gr;
 
  72
  73	dev_dbg(hsotg->dev, "%s\n", __func__);
  74
  75	/* Restore global regs */
  76	gr = &hsotg->gr_backup;
  77	if (!gr->valid) {
  78		dev_err(hsotg->dev, "%s: no global registers to restore\n",
  79			__func__);
  80		return -EINVAL;
  81	}
  82	gr->valid = false;
  83
  84	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
  85	dwc2_writel(hsotg, gr->gotgctl, GOTGCTL);
  86	dwc2_writel(hsotg, gr->gintmsk, GINTMSK);
  87	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
  88	dwc2_writel(hsotg, gr->gahbcfg, GAHBCFG);
  89	dwc2_writel(hsotg, gr->grxfsiz, GRXFSIZ);
  90	dwc2_writel(hsotg, gr->gnptxfsiz, GNPTXFSIZ);
  91	dwc2_writel(hsotg, gr->gdfifocfg, GDFIFOCFG);
  92	dwc2_writel(hsotg, gr->pcgcctl1, PCGCCTL1);
  93	dwc2_writel(hsotg, gr->glpmcfg, GLPMCFG);
  94	dwc2_writel(hsotg, gr->pcgcctl, PCGCTL);
  95	dwc2_writel(hsotg, gr->gi2cctl, GI2CCTL);
  96
  97	return 0;
  98}
  99
 100/**
 101 * dwc2_exit_partial_power_down() - Exit controller from Partial Power Down.
 102 *
 103 * @hsotg: Programming view of the DWC_otg controller
 104 * @rem_wakeup: indicates whether resume is initiated by Reset.
 105 * @restore: Controller registers need to be restored
 106 */
 107int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, int rem_wakeup,
 108				 bool restore)
 109{
 110	struct dwc2_gregs_backup *gr;
 111
 112	gr = &hsotg->gr_backup;
 113
 114	/*
 115	 * Restore host or device regisers with the same mode core enterted
 116	 * to partial power down by checking "GOTGCTL_CURMODE_HOST" backup
 117	 * value of the "gotgctl" register.
 118	 */
 119	if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
 120		return dwc2_host_exit_partial_power_down(hsotg, rem_wakeup,
 121							 restore);
 122	else
 123		return dwc2_gadget_exit_partial_power_down(hsotg, restore);
 124}
 125
 126/**
 127 * dwc2_enter_partial_power_down() - Put controller in Partial Power Down.
 128 *
 129 * @hsotg: Programming view of the DWC_otg controller
 130 */
 131int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg)
 132{
 133	if (dwc2_is_host_mode(hsotg))
 134		return dwc2_host_enter_partial_power_down(hsotg);
 135	else
 136		return dwc2_gadget_enter_partial_power_down(hsotg);
 137}
 138
 139/**
 140 * dwc2_restore_essential_regs() - Restore essiential regs of core.
 141 *
 142 * @hsotg: Programming view of the DWC_otg controller
 143 * @rmode: Restore mode, enabled in case of remote-wakeup.
 144 * @is_host: Host or device mode.
 145 */
 146static void dwc2_restore_essential_regs(struct dwc2_hsotg *hsotg, int rmode,
 147					int is_host)
 148{
 149	u32 pcgcctl;
 150	struct dwc2_gregs_backup *gr;
 151	struct dwc2_dregs_backup *dr;
 152	struct dwc2_hregs_backup *hr;
 153
 154	gr = &hsotg->gr_backup;
 155	dr = &hsotg->dr_backup;
 156	hr = &hsotg->hr_backup;
 157
 158	dev_dbg(hsotg->dev, "%s: restoring essential regs\n", __func__);
 159
 160	/* Load restore values for [31:14] bits */
 161	pcgcctl = (gr->pcgcctl & 0xffffc000);
 162	/* If High Speed */
 163	if (is_host) {
 164		if (!(pcgcctl & PCGCTL_P2HD_PRT_SPD_MASK))
 165			pcgcctl |= BIT(17);
 166	} else {
 167		if (!(pcgcctl & PCGCTL_P2HD_DEV_ENUM_SPD_MASK))
 168			pcgcctl |= BIT(17);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 169	}
 170	dwc2_writel(hsotg, pcgcctl, PCGCTL);
 171
 172	/* Umnask global Interrupt in GAHBCFG and restore it */
 173	dwc2_writel(hsotg, gr->gahbcfg | GAHBCFG_GLBL_INTR_EN, GAHBCFG);
 174
 175	/* Clear all pending interupts */
 176	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
 177
 178	/* Unmask restore done interrupt */
 179	dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTMSK);
 180
 181	/* Restore GUSBCFG and HCFG/DCFG */
 182	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
 183
 184	if (is_host) {
 185		dwc2_writel(hsotg, hr->hcfg, HCFG);
 186		if (rmode)
 187			pcgcctl |= PCGCTL_RESTOREMODE;
 188		dwc2_writel(hsotg, pcgcctl, PCGCTL);
 189		udelay(10);
 190
 191		pcgcctl |= PCGCTL_ESS_REG_RESTORED;
 192		dwc2_writel(hsotg, pcgcctl, PCGCTL);
 193		udelay(10);
 194	} else {
 195		dwc2_writel(hsotg, dr->dcfg, DCFG);
 196		if (!rmode)
 197			pcgcctl |= PCGCTL_RESTOREMODE | PCGCTL_RSTPDWNMODULE;
 198		dwc2_writel(hsotg, pcgcctl, PCGCTL);
 199		udelay(10);
 200
 201		pcgcctl |= PCGCTL_ESS_REG_RESTORED;
 202		dwc2_writel(hsotg, pcgcctl, PCGCTL);
 203		udelay(10);
 204	}
 205}
 206
 207/**
 208 * dwc2_hib_restore_common() - Common part of restore routine.
 209 *
 210 * @hsotg: Programming view of the DWC_otg controller
 211 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
 212 * @is_host: Host or device mode.
 213 */
 214void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
 215			     int is_host)
 216{
 217	u32 gpwrdn;
 
 218
 219	/* Switch-on voltage to the core */
 220	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 221	gpwrdn &= ~GPWRDN_PWRDNSWTCH;
 222	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 223	udelay(10);
 224
 225	/* Reset core */
 226	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 227	gpwrdn &= ~GPWRDN_PWRDNRSTN;
 228	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 229	udelay(10);
 230
 231	/* Enable restore from PMU */
 232	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 233	gpwrdn |= GPWRDN_RESTORE;
 234	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 235	udelay(10);
 236
 237	/* Disable Power Down Clamp */
 238	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 239	gpwrdn &= ~GPWRDN_PWRDNCLMP;
 240	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 241	udelay(50);
 242
 243	if (!is_host && rem_wakeup)
 244		udelay(70);
 245
 246	/* Deassert reset core */
 247	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 248	gpwrdn |= GPWRDN_PWRDNRSTN;
 249	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 250	udelay(10);
 251
 252	/* Reset ULPI latch */
 253	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 254	gpwrdn &= ~GPWRDN_ULPI_LATCH_EN_DURING_HIB_ENTRY;
 255	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 256
 257	/* Disable PMU interrupt */
 258	gpwrdn = dwc2_readl(hsotg, GPWRDN);
 259	gpwrdn &= ~GPWRDN_PMUINTSEL;
 260	dwc2_writel(hsotg, gpwrdn, GPWRDN);
 261	udelay(10);
 262
 263	/* Set Restore Essential Regs bit in PCGCCTL register */
 264	dwc2_restore_essential_regs(hsotg, rem_wakeup, is_host);
 265
 266	/*
 267	 * Wait For Restore_done Interrupt. This mechanism of polling the
 268	 * interrupt is introduced to avoid any possible race conditions
 269	 */
 270	if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, GINTSTS_RESTOREDONE,
 271				    20000)) {
 272		dev_dbg(hsotg->dev,
 273			"%s: Restore Done wasn't generated here\n",
 274			__func__);
 
 
 
 275	} else {
 276		dev_dbg(hsotg->dev, "restore done  generated here\n");
 277
 278		/*
 279		 * To avoid restore done interrupt storm after restore is
 280		 * generated clear GINTSTS_RESTOREDONE bit.
 281		 */
 282		dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTSTS);
 283	}
 284}
 285
 286/**
 287 * dwc2_wait_for_mode() - Waits for the controller mode.
 288 * @hsotg:	Programming view of the DWC_otg controller.
 289 * @host_mode:	If true, waits for host mode, otherwise device mode.
 290 */
 291static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
 292			       bool host_mode)
 293{
 294	ktime_t start;
 295	ktime_t end;
 296	unsigned int timeout = 110;
 297
 298	dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
 299		 host_mode ? "host" : "device");
 300
 301	start = ktime_get();
 302
 303	while (1) {
 304		s64 ms;
 305
 306		if (dwc2_is_host_mode(hsotg) == host_mode) {
 307			dev_vdbg(hsotg->dev, "%s mode set\n",
 308				 host_mode ? "Host" : "Device");
 309			break;
 310		}
 311
 312		end = ktime_get();
 313		ms = ktime_to_ms(ktime_sub(end, start));
 314
 315		if (ms >= (s64)timeout) {
 316			dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
 317				 __func__, host_mode ? "host" : "device");
 318			break;
 319		}
 320
 321		usleep_range(1000, 2000);
 322	}
 323}
 324
 325/**
 326 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
 327 * filter is enabled.
 328 *
 329 * @hsotg: Programming view of DWC_otg controller
 330 */
 331static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
 332{
 333	u32 gsnpsid;
 334	u32 ghwcfg4;
 335
 336	if (!dwc2_hw_is_otg(hsotg))
 337		return false;
 338
 339	/* Check if core configuration includes the IDDIG filter. */
 340	ghwcfg4 = dwc2_readl(hsotg, GHWCFG4);
 341	if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
 342		return false;
 343
 344	/*
 345	 * Check if the IDDIG debounce filter is bypassed. Available
 346	 * in core version >= 3.10a.
 347	 */
 348	gsnpsid = dwc2_readl(hsotg, GSNPSID);
 349	if (gsnpsid >= DWC2_CORE_REV_3_10a) {
 350		u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
 
 351
 352		if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
 353			return false;
 354	}
 355
 356	return true;
 357}
 
 358
 359/*
 360 * dwc2_enter_hibernation() - Common function to enter hibernation.
 361 *
 362 * @hsotg: Programming view of the DWC_otg controller
 363 * @is_host: True if core is in host mode.
 364 *
 365 * Return: 0 if successful, negative error code otherwise
 366 */
 367int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host)
 368{
 369	if (is_host)
 370		return dwc2_host_enter_hibernation(hsotg);
 371	else
 372		return dwc2_gadget_enter_hibernation(hsotg);
 373}
 374
 375/*
 376 * dwc2_exit_hibernation() - Common function to exit from hibernation.
 377 *
 378 * @hsotg: Programming view of the DWC_otg controller
 379 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
 380 * @reset: Enabled in case of restore with reset.
 381 * @is_host: True if core is in host mode.
 382 *
 383 * Return: 0 if successful, negative error code otherwise
 384 */
 385int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
 386			  int reset, int is_host)
 387{
 388	if (is_host)
 389		return dwc2_host_exit_hibernation(hsotg, rem_wakeup, reset);
 390	else
 391		return dwc2_gadget_exit_hibernation(hsotg, rem_wakeup, reset);
 392}
 393
 394/*
 395 * Do core a soft reset of the core.  Be careful with this because it
 396 * resets all the internal state machines of the core.
 397 */
 398int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
 399{
 400	u32 greset;
 401	bool wait_for_host_mode = false;
 402
 403	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 404
 405	/*
 406	 * If the current mode is host, either due to the force mode
 407	 * bit being set (which persists after core reset) or the
 408	 * connector id pin, a core soft reset will temporarily reset
 409	 * the mode to device. A delay from the IDDIG debounce filter
 410	 * will occur before going back to host mode.
 411	 *
 412	 * Determine whether we will go back into host mode after a
 413	 * reset and account for this delay after the reset.
 414	 */
 415	if (dwc2_iddig_filter_enabled(hsotg)) {
 416		u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
 417		u32 gusbcfg = dwc2_readl(hsotg, GUSBCFG);
 418
 419		if (!(gotgctl & GOTGCTL_CONID_B) ||
 420		    (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
 421			wait_for_host_mode = true;
 422		}
 423	}
 424
 425	/* Core Soft Reset */
 426	greset = dwc2_readl(hsotg, GRSTCTL);
 427	greset |= GRSTCTL_CSFTRST;
 428	dwc2_writel(hsotg, greset, GRSTCTL);
 429
 430	if ((hsotg->hw_params.snpsid & DWC2_CORE_REV_MASK) <
 431		(DWC2_CORE_REV_4_20a & DWC2_CORE_REV_MASK)) {
 432		if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL,
 433					      GRSTCTL_CSFTRST, 10000)) {
 434			dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST\n",
 435				 __func__);
 436			return -EBUSY;
 437		}
 438	} else {
 439		if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
 440					    GRSTCTL_CSFTRST_DONE, 10000)) {
 441			dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST_DONE\n",
 442				 __func__);
 
 
 
 
 
 
 443			return -EBUSY;
 444		}
 445		greset = dwc2_readl(hsotg, GRSTCTL);
 446		greset &= ~GRSTCTL_CSFTRST;
 447		greset |= GRSTCTL_CSFTRST_DONE;
 448		dwc2_writel(hsotg, greset, GRSTCTL);
 449	}
 450
 451	/*
 452	 * Switching from device mode to host mode by disconnecting
 453	 * device cable core enters and exits form hibernation.
 454	 * However, the fifo map remains not cleared. It results
 455	 * to a WARNING (WARNING: CPU: 5 PID: 0 at drivers/usb/dwc2/
 456	 * gadget.c:307 dwc2_hsotg_init_fifo+0x12/0x152 [dwc2])
 457	 * if in host mode we disconnect the micro a to b host
 458	 * cable. Because core reset occurs.
 459	 * To avoid the WARNING, fifo_map should be cleared
 460	 * in dwc2_core_reset() function by taking into account configs.
 461	 * fifo_map must be cleared only if driver is configured in
 462	 * "CONFIG_USB_DWC2_PERIPHERAL" or "CONFIG_USB_DWC2_DUAL_ROLE"
 463	 * mode.
 464	 */
 465	dwc2_clear_fifo_map(hsotg);
 466
 467	/* Wait for AHB master IDLE state */
 468	if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000)) {
 469		dev_warn(hsotg->dev, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
 470			 __func__);
 471		return -EBUSY;
 472	}
 473
 474	if (wait_for_host_mode && !skip_wait)
 475		dwc2_wait_for_mode(hsotg, true);
 476
 477	return 0;
 478}
 479
 480/**
 481 * dwc2_force_mode() - Force the mode of the controller.
 482 *
 483 * Forcing the mode is needed for two cases:
 484 *
 485 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
 486 * controller to stay in a particular mode regardless of ID pin
 487 * changes. We do this once during probe.
 488 *
 489 * 2) During probe we want to read reset values of the hw
 490 * configuration registers that are only available in either host or
 491 * device mode. We may need to force the mode if the current mode does
 492 * not allow us to access the register in the mode that we want.
 493 *
 494 * In either case it only makes sense to force the mode if the
 495 * controller hardware is OTG capable.
 496 *
 497 * Checks are done in this function to determine whether doing a force
 498 * would be valid or not.
 499 *
 500 * If a force is done, it requires a IDDIG debounce filter delay if
 501 * the filter is configured and enabled. We poll the current mode of
 502 * the controller to account for this delay.
 503 *
 504 * @hsotg: Programming view of DWC_otg controller
 505 * @host: Host mode flag
 506 */
 507void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
 508{
 509	u32 gusbcfg;
 510	u32 set;
 511	u32 clear;
 512
 513	dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
 514
 515	/*
 516	 * Force mode has no effect if the hardware is not OTG.
 517	 */
 518	if (!dwc2_hw_is_otg(hsotg))
 519		return;
 520
 521	/*
 522	 * If dr_mode is either peripheral or host only, there is no
 523	 * need to ever force the mode to the opposite mode.
 524	 */
 525	if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
 526		return;
 527
 528	if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
 529		return;
 530
 531	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
 532
 533	set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
 534	clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
 535
 536	gusbcfg &= ~clear;
 537	gusbcfg |= set;
 538	dwc2_writel(hsotg, gusbcfg, GUSBCFG);
 539
 540	dwc2_wait_for_mode(hsotg, host);
 541	return;
 542}
 543
 544/**
 545 * dwc2_clear_force_mode() - Clears the force mode bits.
 546 *
 547 * After clearing the bits, wait up to 100 ms to account for any
 548 * potential IDDIG filter delay. We can't know if we expect this delay
 549 * or not because the value of the connector ID status is affected by
 550 * the force mode. We only need to call this once during probe if
 551 * dr_mode == OTG.
 552 *
 553 * @hsotg: Programming view of DWC_otg controller
 554 */
 555static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
 556{
 557	u32 gusbcfg;
 558
 559	if (!dwc2_hw_is_otg(hsotg))
 560		return;
 561
 562	dev_dbg(hsotg->dev, "Clearing force mode bits\n");
 563
 564	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
 565	gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
 566	gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
 567	dwc2_writel(hsotg, gusbcfg, GUSBCFG);
 568
 569	if (dwc2_iddig_filter_enabled(hsotg))
 570		msleep(100);
 
 
 
 571}
 572
 573/*
 574 * Sets or clears force mode based on the dr_mode parameter.
 575 */
 576void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
 577{
 578	switch (hsotg->dr_mode) {
 579	case USB_DR_MODE_HOST:
 580		/*
 581		 * NOTE: This is required for some rockchip soc based
 582		 * platforms on their host-only dwc2.
 583		 */
 584		if (!dwc2_hw_is_otg(hsotg))
 585			msleep(50);
 586
 587		break;
 588	case USB_DR_MODE_PERIPHERAL:
 589		dwc2_force_mode(hsotg, false);
 590		break;
 591	case USB_DR_MODE_OTG:
 592		dwc2_clear_force_mode(hsotg);
 593		break;
 594	default:
 595		dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
 596			 __func__, hsotg->dr_mode);
 597		break;
 598	}
 
 
 
 
 
 
 599}
 600
 601/*
 602 * dwc2_enable_acg - enable active clock gating feature
 
 
 
 
 603 */
 604void dwc2_enable_acg(struct dwc2_hsotg *hsotg)
 605{
 606	if (hsotg->params.acg_enable) {
 607		u32 pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
 608
 609		dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
 610		pcgcctl1 |= PCGCCTL1_GATEEN;
 611		dwc2_writel(hsotg, pcgcctl1, PCGCCTL1);
 612	}
 
 
 613}
 614
 615/**
 616 * dwc2_dump_host_registers() - Prints the host registers
 617 *
 618 * @hsotg: Programming view of DWC_otg controller
 619 *
 620 * NOTE: This function will be removed once the peripheral controller code
 621 * is integrated and the driver is stable
 622 */
 623void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
 624{
 625#ifdef DEBUG
 626	u32 __iomem *addr;
 627	int i;
 628
 629	dev_dbg(hsotg->dev, "Host Global Registers\n");
 630	addr = hsotg->regs + HCFG;
 631	dev_dbg(hsotg->dev, "HCFG	 @0x%08lX : 0x%08X\n",
 632		(unsigned long)addr, dwc2_readl(hsotg, HCFG));
 633	addr = hsotg->regs + HFIR;
 634	dev_dbg(hsotg->dev, "HFIR	 @0x%08lX : 0x%08X\n",
 635		(unsigned long)addr, dwc2_readl(hsotg, HFIR));
 636	addr = hsotg->regs + HFNUM;
 637	dev_dbg(hsotg->dev, "HFNUM	 @0x%08lX : 0x%08X\n",
 638		(unsigned long)addr, dwc2_readl(hsotg, HFNUM));
 639	addr = hsotg->regs + HPTXSTS;
 640	dev_dbg(hsotg->dev, "HPTXSTS	 @0x%08lX : 0x%08X\n",
 641		(unsigned long)addr, dwc2_readl(hsotg, HPTXSTS));
 642	addr = hsotg->regs + HAINT;
 643	dev_dbg(hsotg->dev, "HAINT	 @0x%08lX : 0x%08X\n",
 644		(unsigned long)addr, dwc2_readl(hsotg, HAINT));
 645	addr = hsotg->regs + HAINTMSK;
 646	dev_dbg(hsotg->dev, "HAINTMSK	 @0x%08lX : 0x%08X\n",
 647		(unsigned long)addr, dwc2_readl(hsotg, HAINTMSK));
 648	if (hsotg->params.dma_desc_enable) {
 649		addr = hsotg->regs + HFLBADDR;
 650		dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
 651			(unsigned long)addr, dwc2_readl(hsotg, HFLBADDR));
 652	}
 653
 654	addr = hsotg->regs + HPRT0;
 655	dev_dbg(hsotg->dev, "HPRT0	 @0x%08lX : 0x%08X\n",
 656		(unsigned long)addr, dwc2_readl(hsotg, HPRT0));
 657
 658	for (i = 0; i < hsotg->params.host_channels; i++) {
 659		dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
 660		addr = hsotg->regs + HCCHAR(i);
 661		dev_dbg(hsotg->dev, "HCCHAR	 @0x%08lX : 0x%08X\n",
 662			(unsigned long)addr, dwc2_readl(hsotg, HCCHAR(i)));
 663		addr = hsotg->regs + HCSPLT(i);
 664		dev_dbg(hsotg->dev, "HCSPLT	 @0x%08lX : 0x%08X\n",
 665			(unsigned long)addr, dwc2_readl(hsotg, HCSPLT(i)));
 666		addr = hsotg->regs + HCINT(i);
 667		dev_dbg(hsotg->dev, "HCINT	 @0x%08lX : 0x%08X\n",
 668			(unsigned long)addr, dwc2_readl(hsotg, HCINT(i)));
 669		addr = hsotg->regs + HCINTMSK(i);
 670		dev_dbg(hsotg->dev, "HCINTMSK	 @0x%08lX : 0x%08X\n",
 671			(unsigned long)addr, dwc2_readl(hsotg, HCINTMSK(i)));
 672		addr = hsotg->regs + HCTSIZ(i);
 673		dev_dbg(hsotg->dev, "HCTSIZ	 @0x%08lX : 0x%08X\n",
 674			(unsigned long)addr, dwc2_readl(hsotg, HCTSIZ(i)));
 675		addr = hsotg->regs + HCDMA(i);
 676		dev_dbg(hsotg->dev, "HCDMA	 @0x%08lX : 0x%08X\n",
 677			(unsigned long)addr, dwc2_readl(hsotg, HCDMA(i)));
 678		if (hsotg->params.dma_desc_enable) {
 679			addr = hsotg->regs + HCDMAB(i);
 680			dev_dbg(hsotg->dev, "HCDMAB	 @0x%08lX : 0x%08X\n",
 681				(unsigned long)addr, dwc2_readl(hsotg,
 682								HCDMAB(i)));
 683		}
 684	}
 685#endif
 686}
 687
 688/**
 689 * dwc2_dump_global_registers() - Prints the core global registers
 690 *
 691 * @hsotg: Programming view of DWC_otg controller
 692 *
 693 * NOTE: This function will be removed once the peripheral controller code
 694 * is integrated and the driver is stable
 695 */
 696void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
 697{
 698#ifdef DEBUG
 699	u32 __iomem *addr;
 700
 701	dev_dbg(hsotg->dev, "Core Global Registers\n");
 702	addr = hsotg->regs + GOTGCTL;
 703	dev_dbg(hsotg->dev, "GOTGCTL	 @0x%08lX : 0x%08X\n",
 704		(unsigned long)addr, dwc2_readl(hsotg, GOTGCTL));
 705	addr = hsotg->regs + GOTGINT;
 706	dev_dbg(hsotg->dev, "GOTGINT	 @0x%08lX : 0x%08X\n",
 707		(unsigned long)addr, dwc2_readl(hsotg, GOTGINT));
 708	addr = hsotg->regs + GAHBCFG;
 709	dev_dbg(hsotg->dev, "GAHBCFG	 @0x%08lX : 0x%08X\n",
 710		(unsigned long)addr, dwc2_readl(hsotg, GAHBCFG));
 711	addr = hsotg->regs + GUSBCFG;
 712	dev_dbg(hsotg->dev, "GUSBCFG	 @0x%08lX : 0x%08X\n",
 713		(unsigned long)addr, dwc2_readl(hsotg, GUSBCFG));
 714	addr = hsotg->regs + GRSTCTL;
 715	dev_dbg(hsotg->dev, "GRSTCTL	 @0x%08lX : 0x%08X\n",
 716		(unsigned long)addr, dwc2_readl(hsotg, GRSTCTL));
 717	addr = hsotg->regs + GINTSTS;
 718	dev_dbg(hsotg->dev, "GINTSTS	 @0x%08lX : 0x%08X\n",
 719		(unsigned long)addr, dwc2_readl(hsotg, GINTSTS));
 720	addr = hsotg->regs + GINTMSK;
 721	dev_dbg(hsotg->dev, "GINTMSK	 @0x%08lX : 0x%08X\n",
 722		(unsigned long)addr, dwc2_readl(hsotg, GINTMSK));
 723	addr = hsotg->regs + GRXSTSR;
 724	dev_dbg(hsotg->dev, "GRXSTSR	 @0x%08lX : 0x%08X\n",
 725		(unsigned long)addr, dwc2_readl(hsotg, GRXSTSR));
 726	addr = hsotg->regs + GRXFSIZ;
 727	dev_dbg(hsotg->dev, "GRXFSIZ	 @0x%08lX : 0x%08X\n",
 728		(unsigned long)addr, dwc2_readl(hsotg, GRXFSIZ));
 729	addr = hsotg->regs + GNPTXFSIZ;
 730	dev_dbg(hsotg->dev, "GNPTXFSIZ	 @0x%08lX : 0x%08X\n",
 731		(unsigned long)addr, dwc2_readl(hsotg, GNPTXFSIZ));
 732	addr = hsotg->regs + GNPTXSTS;
 733	dev_dbg(hsotg->dev, "GNPTXSTS	 @0x%08lX : 0x%08X\n",
 734		(unsigned long)addr, dwc2_readl(hsotg, GNPTXSTS));
 735	addr = hsotg->regs + GI2CCTL;
 736	dev_dbg(hsotg->dev, "GI2CCTL	 @0x%08lX : 0x%08X\n",
 737		(unsigned long)addr, dwc2_readl(hsotg, GI2CCTL));
 738	addr = hsotg->regs + GPVNDCTL;
 739	dev_dbg(hsotg->dev, "GPVNDCTL	 @0x%08lX : 0x%08X\n",
 740		(unsigned long)addr, dwc2_readl(hsotg, GPVNDCTL));
 741	addr = hsotg->regs + GGPIO;
 742	dev_dbg(hsotg->dev, "GGPIO	 @0x%08lX : 0x%08X\n",
 743		(unsigned long)addr, dwc2_readl(hsotg, GGPIO));
 744	addr = hsotg->regs + GUID;
 745	dev_dbg(hsotg->dev, "GUID	 @0x%08lX : 0x%08X\n",
 746		(unsigned long)addr, dwc2_readl(hsotg, GUID));
 747	addr = hsotg->regs + GSNPSID;
 748	dev_dbg(hsotg->dev, "GSNPSID	 @0x%08lX : 0x%08X\n",
 749		(unsigned long)addr, dwc2_readl(hsotg, GSNPSID));
 750	addr = hsotg->regs + GHWCFG1;
 751	dev_dbg(hsotg->dev, "GHWCFG1	 @0x%08lX : 0x%08X\n",
 752		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG1));
 753	addr = hsotg->regs + GHWCFG2;
 754	dev_dbg(hsotg->dev, "GHWCFG2	 @0x%08lX : 0x%08X\n",
 755		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG2));
 756	addr = hsotg->regs + GHWCFG3;
 757	dev_dbg(hsotg->dev, "GHWCFG3	 @0x%08lX : 0x%08X\n",
 758		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG3));
 759	addr = hsotg->regs + GHWCFG4;
 760	dev_dbg(hsotg->dev, "GHWCFG4	 @0x%08lX : 0x%08X\n",
 761		(unsigned long)addr, dwc2_readl(hsotg, GHWCFG4));
 762	addr = hsotg->regs + GLPMCFG;
 763	dev_dbg(hsotg->dev, "GLPMCFG	 @0x%08lX : 0x%08X\n",
 764		(unsigned long)addr, dwc2_readl(hsotg, GLPMCFG));
 765	addr = hsotg->regs + GPWRDN;
 766	dev_dbg(hsotg->dev, "GPWRDN	 @0x%08lX : 0x%08X\n",
 767		(unsigned long)addr, dwc2_readl(hsotg, GPWRDN));
 768	addr = hsotg->regs + GDFIFOCFG;
 769	dev_dbg(hsotg->dev, "GDFIFOCFG	 @0x%08lX : 0x%08X\n",
 770		(unsigned long)addr, dwc2_readl(hsotg, GDFIFOCFG));
 771	addr = hsotg->regs + HPTXFSIZ;
 772	dev_dbg(hsotg->dev, "HPTXFSIZ	 @0x%08lX : 0x%08X\n",
 773		(unsigned long)addr, dwc2_readl(hsotg, HPTXFSIZ));
 774
 775	addr = hsotg->regs + PCGCTL;
 776	dev_dbg(hsotg->dev, "PCGCTL	 @0x%08lX : 0x%08X\n",
 777		(unsigned long)addr, dwc2_readl(hsotg, PCGCTL));
 778#endif
 779}
 780
 781/**
 782 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
 783 *
 784 * @hsotg: Programming view of DWC_otg controller
 785 * @num:   Tx FIFO to flush
 786 */
 787void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
 788{
 789	u32 greset;
 
 790
 791	dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
 792
 793	/* Wait for AHB master IDLE state */
 794	if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
 795		dev_warn(hsotg->dev, "%s:  HANG! AHB Idle GRSCTL\n",
 796			 __func__);
 797
 798	greset = GRSTCTL_TXFFLSH;
 799	greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
 800	dwc2_writel(hsotg, greset, GRSTCTL);
 801
 802	if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
 803		dev_warn(hsotg->dev, "%s:  HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
 804			 __func__);
 
 
 
 
 
 
 
 
 805
 806	/* Wait for at least 3 PHY Clocks */
 807	udelay(1);
 808}
 809
 810/**
 811 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
 812 *
 813 * @hsotg: Programming view of DWC_otg controller
 814 */
 815void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
 816{
 817	u32 greset;
 
 818
 819	dev_vdbg(hsotg->dev, "%s()\n", __func__);
 820
 821	/* Wait for AHB master IDLE state */
 822	if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
 823		dev_warn(hsotg->dev, "%s:  HANG! AHB Idle GRSCTL\n",
 824			 __func__);
 825
 826	greset = GRSTCTL_RXFFLSH;
 827	dwc2_writel(hsotg, greset, GRSTCTL);
 828
 829	/* Wait for RxFIFO flush done */
 830	if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
 831		dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
 832			 __func__);
 
 
 
 
 
 833
 834	/* Wait for at least 3 PHY Clocks */
 835	udelay(1);
 836}
 837
 838bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 839{
 840	if (dwc2_readl(hsotg, GSNPSID) == 0xffffffff)
 841		return false;
 842	else
 843		return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 844}
 845
 846/**
 847 * dwc2_enable_global_interrupts() - Enables the controller's Global
 848 * Interrupt in the AHB Config register
 849 *
 850 * @hsotg: Programming view of DWC_otg controller
 851 */
 852void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
 853{
 854	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 855
 856	ahbcfg |= GAHBCFG_GLBL_INTR_EN;
 857	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
 858}
 859
 860/**
 861 * dwc2_disable_global_interrupts() - Disables the controller's Global
 862 * Interrupt in the AHB Config register
 863 *
 864 * @hsotg: Programming view of DWC_otg controller
 865 */
 866void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
 867{
 868	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 869
 870	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
 871	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
 872}
 873
 874/* Returns the controller's GHWCFG2.OTG_MODE. */
 875unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
 876{
 877	u32 ghwcfg2 = dwc2_readl(hsotg, GHWCFG2);
 
 
 
 
 
 
 
 
 
 
 878
 879	return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
 880		GHWCFG2_OP_MODE_SHIFT;
 881}
 882
 883/* Returns true if the controller is capable of DRD. */
 884bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
 885{
 886	unsigned int op_mode = dwc2_op_mode(hsotg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 887
 888	return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
 889		(op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
 890		(op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
 891}
 892
 893/* Returns true if the controller is host-only. */
 894bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
 895{
 896	unsigned int op_mode = dwc2_op_mode(hsotg);
 
 
 
 
 
 
 
 
 
 
 
 
 897
 898	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
 899		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
 900}
 901
 902/* Returns true if the controller is device-only. */
 903bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
 904{
 905	unsigned int op_mode = dwc2_op_mode(hsotg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 906
 907	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
 908		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
 909}
 910
 911/**
 912 * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
 913 * @hsotg: Programming view of DWC_otg controller.
 914 * @offset: Register's offset where bit/bits must be set.
 915 * @mask: Mask of the bit/bits which must be set.
 916 * @timeout: Timeout to wait.
 917 *
 918 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
 919 */
 920int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
 921			    u32 timeout)
 922{
 923	u32 i;
 924
 925	for (i = 0; i < timeout; i++) {
 926		if (dwc2_readl(hsotg, offset) & mask)
 927			return 0;
 928		udelay(1);
 929	}
 930
 931	return -ETIMEDOUT;
 932}
 933
 934/**
 935 * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
 936 * @hsotg: Programming view of DWC_otg controller.
 937 * @offset: Register's offset where bit/bits must be set.
 938 * @mask: Mask of the bit/bits which must be set.
 939 * @timeout: Timeout to wait.
 940 *
 941 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
 942 */
 943int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
 944			      u32 timeout)
 945{
 946	u32 i;
 947
 948	for (i = 0; i < timeout; i++) {
 949		if (!(dwc2_readl(hsotg, offset) & mask))
 950			return 0;
 951		udelay(1);
 952	}
 953
 954	return -ETIMEDOUT;
 955}
 956
 957/*
 958 * Initializes the FSLSPClkSel field of the HCFG register depending on the
 959 * PHY type
 960 */
 961void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
 962{
 963	u32 hcfg, val;
 
 
 
 964
 965	if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
 966	     hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
 967	     hsotg->params.ulpi_fs_ls) ||
 968	    hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
 969		/* Full speed PHY */
 970		val = HCFG_FSLSPCLKSEL_48_MHZ;
 971	} else {
 972		/* High speed PHY running at full speed or high speed */
 973		val = HCFG_FSLSPCLKSEL_30_60_MHZ;
 974	}
 975
 976	dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
 977	hcfg = dwc2_readl(hsotg, HCFG);
 978	hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
 979	hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
 980	dwc2_writel(hsotg, hcfg, HCFG);
 981}
 982
 983static void dwc2_set_clock_switch_timer(struct dwc2_hsotg *hsotg)
 984{
 985	u32 grstctl, gsnpsid, val = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 986
 987	gsnpsid = dwc2_readl(hsotg, GSNPSID);
 
 
 
 988
 989	/*
 990	 * Applicable only to HSOTG core v5.00a or higher.
 991	 * Not applicable to HS/FS IOT devices.
 992	 */
 993	if ((gsnpsid & ~DWC2_CORE_REV_MASK) != DWC2_OTG_ID ||
 994	    gsnpsid < DWC2_CORE_REV_5_00a)
 995		return;
 996
 997	if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI &&
 998	     hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) ||
 999	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
1000	     hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) ||
1001	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED &&
1002	     hsotg->hw_params.fs_phy_type != GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED)) {
1003		val = GRSTCTL_CLOCK_SWITH_TIMER_VALUE_DIS;
1004	}
1005
1006	if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW &&
1007	    hsotg->hw_params.hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED &&
1008	    hsotg->hw_params.fs_phy_type != GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED) {
1009		val = GRSTCTL_CLOCK_SWITH_TIMER_VALUE_147;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1010	}
1011
1012	grstctl = dwc2_readl(hsotg, GRSTCTL);
1013	grstctl &= ~GRSTCTL_CLOCK_SWITH_TIMER_MASK;
1014	grstctl |= GRSTCTL_CLOCK_SWITH_TIMER(val);
1015	dwc2_writel(hsotg, grstctl, GRSTCTL);
1016}
1017
1018static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1019{
1020	u32 usbcfg, ggpio, i2cctl;
1021	int retval = 0;
1022
1023	/*
1024	 * core_init() is now called on every switch so only call the
1025	 * following for the first time through
1026	 */
1027	if (select_phy) {
1028		dev_dbg(hsotg->dev, "FS PHY selected\n");
1029
1030		usbcfg = dwc2_readl(hsotg, GUSBCFG);
1031		if (!(usbcfg & GUSBCFG_PHYSEL)) {
1032			usbcfg |= GUSBCFG_PHYSEL;
1033			dwc2_writel(hsotg, usbcfg, GUSBCFG);
1034
1035			dwc2_set_clock_switch_timer(hsotg);
1036
1037			/* Reset after a PHY select */
1038			retval = dwc2_core_reset(hsotg, false);
1039
1040			if (retval) {
1041				dev_err(hsotg->dev,
1042					"%s: Reset failed, aborting", __func__);
1043				return retval;
1044			}
1045		}
 
 
1046
1047		if (hsotg->params.activate_stm_fs_transceiver) {
1048			ggpio = dwc2_readl(hsotg, GGPIO);
1049			if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
1050				dev_dbg(hsotg->dev, "Activating transceiver\n");
1051				/*
1052				 * STM32F4x9 uses the GGPIO register as general
1053				 * core configuration register.
1054				 */
1055				ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
1056				dwc2_writel(hsotg, ggpio, GGPIO);
1057			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1058		}
 
1059	}
1060
1061	/*
1062	 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
1063	 * do this on HNP Dev/Host mode switches (done in dev_init and
1064	 * host_init).
1065	 */
1066	if (dwc2_is_host_mode(hsotg))
1067		dwc2_init_fs_ls_pclk_sel(hsotg);
1068
1069	if (hsotg->params.i2c_enable) {
1070		dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
 
 
 
 
 
 
 
 
 
1071
1072		/* Program GUSBCFG.OtgUtmiFsSel to I2C */
1073		usbcfg = dwc2_readl(hsotg, GUSBCFG);
1074		usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
1075		dwc2_writel(hsotg, usbcfg, GUSBCFG);
1076
1077		/* Program GI2CCTL.I2CEn */
1078		i2cctl = dwc2_readl(hsotg, GI2CCTL);
1079		i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
1080		i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
1081		i2cctl &= ~GI2CCTL_I2CEN;
1082		dwc2_writel(hsotg, i2cctl, GI2CCTL);
1083		i2cctl |= GI2CCTL_I2CEN;
1084		dwc2_writel(hsotg, i2cctl, GI2CCTL);
 
1085	}
1086
1087	return retval;
1088}
1089
1090static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1091{
1092	u32 usbcfg, usbcfg_old;
1093	int retval = 0;
 
 
 
 
 
 
 
 
1094
1095	if (!select_phy)
1096		return 0;
1097
1098	usbcfg = dwc2_readl(hsotg, GUSBCFG);
1099	usbcfg_old = usbcfg;
 
1100
1101	/*
1102	 * HS PHY parameters. These parameters are preserved during soft reset
1103	 * so only program the first time. Do a soft reset immediately after
1104	 * setting phyif.
1105	 */
1106	switch (hsotg->params.phy_type) {
1107	case DWC2_PHY_TYPE_PARAM_ULPI:
1108		/* ULPI interface */
1109		dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
1110		usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
1111		usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
1112		if (hsotg->params.phy_ulpi_ddr)
1113			usbcfg |= GUSBCFG_DDRSEL;
1114
1115		/* Set external VBUS indicator as needed. */
1116		if (hsotg->params.oc_disable)
1117			usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
1118				   GUSBCFG_INDICATORPASSTHROUGH);
1119		break;
1120	case DWC2_PHY_TYPE_PARAM_UTMI:
1121		/* UTMI+ interface */
1122		dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
1123		usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
1124		if (hsotg->params.phy_utmi_width == 16)
1125			usbcfg |= GUSBCFG_PHYIF16;
1126		break;
1127	default:
1128		dev_err(hsotg->dev, "FS PHY selected at HS!\n");
1129		break;
1130	}
1131
1132	if (usbcfg != usbcfg_old) {
1133		dwc2_writel(hsotg, usbcfg, GUSBCFG);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1134
1135		/* Reset after setting the PHY parameters */
1136		retval = dwc2_core_reset(hsotg, false);
1137		if (retval) {
 
1138			dev_err(hsotg->dev,
1139				"%s: Reset failed, aborting", __func__);
1140			return retval;
1141		}
 
 
 
 
 
 
 
 
 
 
 
 
 
1142	}
1143
1144	return retval;
1145}
1146
1147static void dwc2_set_turnaround_time(struct dwc2_hsotg *hsotg)
1148{
1149	u32 usbcfg;
1150
1151	if (hsotg->params.phy_type != DWC2_PHY_TYPE_PARAM_UTMI)
1152		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1153
1154	usbcfg = dwc2_readl(hsotg, GUSBCFG);
 
1155
1156	usbcfg &= ~GUSBCFG_USBTRDTIM_MASK;
1157	if (hsotg->params.phy_utmi_width == 16)
1158		usbcfg |= 5 << GUSBCFG_USBTRDTIM_SHIFT;
 
1159	else
1160		usbcfg |= 9 << GUSBCFG_USBTRDTIM_SHIFT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1161
1162	dwc2_writel(hsotg, usbcfg, GUSBCFG);
1163}
1164
1165int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1166{
1167	u32 usbcfg;
1168	u32 otgctl;
1169	int retval = 0;
 
 
 
 
 
 
 
 
 
 
1170
1171	if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
1172	     hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
1173	    hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
1174		/* If FS/LS mode with FS/LS PHY */
1175		retval = dwc2_fs_phy_init(hsotg, select_phy);
1176		if (retval)
1177			return retval;
1178	} else {
1179		/* High speed PHY */
1180		retval = dwc2_hs_phy_init(hsotg, select_phy);
1181		if (retval)
1182			return retval;
1183
1184		if (dwc2_is_device_mode(hsotg))
1185			dwc2_set_turnaround_time(hsotg);
1186	}
1187
1188	if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
1189	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
1190	    hsotg->params.ulpi_fs_ls) {
1191		dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
1192		usbcfg = dwc2_readl(hsotg, GUSBCFG);
1193		usbcfg |= GUSBCFG_ULPI_FS_LS;
1194		usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
1195		dwc2_writel(hsotg, usbcfg, GUSBCFG);
1196	} else {
1197		usbcfg = dwc2_readl(hsotg, GUSBCFG);
1198		usbcfg &= ~GUSBCFG_ULPI_FS_LS;
1199		usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
1200		dwc2_writel(hsotg, usbcfg, GUSBCFG);
1201	}
1202
1203	if (!hsotg->params.activate_ingenic_overcurrent_detection) {
1204		if (dwc2_is_host_mode(hsotg)) {
1205			otgctl = readl(hsotg->regs + GOTGCTL);
1206			otgctl |= GOTGCTL_VBVALOEN | GOTGCTL_VBVALOVAL;
1207			writel(otgctl, hsotg->regs + GOTGCTL);
 
 
 
 
 
 
 
1208		}
 
 
1209	}
1210
1211	return retval;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1212}
1213
1214MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
1215MODULE_AUTHOR("Synopsys, Inc.");
1216MODULE_LICENSE("Dual BSD/GPL");