ehci-fsl.c 19.3 KB
Newer Older
1
/*
2
 * Copyright 2005-2009 MontaVista Software, Inc.
3
 * Copyright 2008,2012      Freescale Semiconductor, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Ported to 834x by Randy Vinson <rvinson@mvista.com> using code provided
 * by Hunter Wu.
21 22 23
 * Power Management support by Dave Liu <daveliu@freescale.com>,
 * Jerry Huang <Chang-Ming.Huang@freescale.com> and
 * Anton Vorontsov <avorontsov@ru.mvista.com>.
24 25
 */

26 27 28 29
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/pm.h>
30
#include <linux/err.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <linux/platform_device.h>
#include <linux/fsl_devices.h>

#include "ehci-fsl.h"

/* configure so an HC device and id are always provided */
/* always called with process context; sleeping is OK */

/**
 * usb_hcd_fsl_probe - initialize FSL-based HCDs
 * @drvier: Driver to be used for this HCD
 * @pdev: USB Host Controller being probed
 * Context: !in_interrupt()
 *
 * Allocates basic resources for this USB host controller.
 *
 */
48 49
static int usb_hcd_fsl_probe(const struct hc_driver *driver,
			     struct platform_device *pdev)
50 51 52 53 54 55 56 57 58 59
{
	struct fsl_usb2_platform_data *pdata;
	struct usb_hcd *hcd;
	struct resource *res;
	int irq;
	int retval;

	pr_debug("initializing FSL-SOC USB Controller\n");

	/* Need platform data for setup */
J
Jingoo Han 已提交
60
	pdata = (struct fsl_usb2_platform_data *)dev_get_platdata(&pdev->dev);
61 62
	if (!pdata) {
		dev_err(&pdev->dev,
63
			"No platform data for %s.\n", dev_name(&pdev->dev));
64 65 66 67 68 69 70 71
		return -ENODEV;
	}

	/*
	 * This is a host mode driver, verify that we're supposed to be
	 * in host mode.
	 */
	if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
72 73
	      (pdata->operating_mode == FSL_USB2_MPH_HOST) ||
	      (pdata->operating_mode == FSL_USB2_DR_OTG))) {
74 75
		dev_err(&pdev->dev,
			"Non Host Mode configured for %s. Wrong driver linked.\n",
76
			dev_name(&pdev->dev));
77 78 79 80 81 82 83
		return -ENODEV;
	}

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		dev_err(&pdev->dev,
			"Found HC with no IRQ. Check %s setup!\n",
84
			dev_name(&pdev->dev));
85 86 87 88
		return -ENODEV;
	}
	irq = res->start;

89
	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
90 91 92 93 94 95 96 97 98
	if (!hcd) {
		retval = -ENOMEM;
		goto err1;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev,
			"Found HC with no register addr. Check %s setup!\n",
99
			dev_name(&pdev->dev));
100 101 102 103
		retval = -ENODEV;
		goto err2;
	}
	hcd->rsrc_start = res->start;
104
	hcd->rsrc_len = resource_size(res);
105 106 107 108 109 110 111 112 113 114 115 116 117 118
	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
				driver->description)) {
		dev_dbg(&pdev->dev, "controller already in use\n");
		retval = -EBUSY;
		goto err2;
	}
	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);

	if (hcd->regs == NULL) {
		dev_dbg(&pdev->dev, "error mapping memory\n");
		retval = -EFAULT;
		goto err3;
	}

119
	pdata->regs = hcd->regs;
120

121 122 123
	if (pdata->power_budget)
		hcd->power_budget = pdata->power_budget;

124 125 126 127 128
	/*
	 * do platform specific init: check the clock, grab/config pins, etc.
	 */
	if (pdata->init && pdata->init(pdev)) {
		retval = -ENODEV;
129
		goto err4;
130 131 132 133 134 135 136
	}

	/* Enable USB controller, 83xx or 8536 */
	if (pdata->have_sysif_regs)
		setbits32(hcd->regs + FSL_SOC_USB_CTRL, 0x4);

	/* Don't need to set host mode here. It will be done by tdi_reset() */
137

Y
Yong Zhang 已提交
138
	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
139 140
	if (retval != 0)
		goto err4;
141 142 143 144 145

#ifdef CONFIG_USB_OTG
	if (pdata->operating_mode == FSL_USB2_DR_OTG) {
		struct ehci_hcd *ehci = hcd_to_ehci(hcd);

146
		hcd->phy = usb_get_phy(USB_PHY_TYPE_USB2);
147 148
		dev_dbg(&pdev->dev, "hcd=0x%p  ehci=0x%p, phy=0x%p\n",
			hcd, ehci, hcd->phy);
149

150
		if (!IS_ERR_OR_NULL(hcd->phy)) {
151
			retval = otg_set_host(hcd->phy->otg,
152 153
					      &ehci_to_hcd(ehci)->self);
			if (retval) {
154
				usb_put_phy(hcd->phy);
155 156 157
				goto err4;
			}
		} else {
158
			dev_err(&pdev->dev, "can't find phy\n");
159 160 161 162 163
			retval = -ENODEV;
			goto err4;
		}
	}
#endif
164 165 166 167 168 169 170 171 172
	return retval;

      err4:
	iounmap(hcd->regs);
      err3:
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
      err2:
	usb_put_hcd(hcd);
      err1:
173
	dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
174 175
	if (pdata->exit)
		pdata->exit(pdev);
176 177 178 179 180 181 182 183 184 185 186 187 188 189
	return retval;
}

/* may be called without controller electrically present */
/* may be called with controller, bus, and devices active */

/**
 * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_hcd_fsl_probe().
 *
 */
190 191
static void usb_hcd_fsl_remove(struct usb_hcd *hcd,
			       struct platform_device *pdev)
192
{
J
Jingoo Han 已提交
193
	struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
194

195
	if (!IS_ERR_OR_NULL(hcd->phy)) {
196
		otg_set_host(hcd->phy->otg, NULL);
197
		usb_put_phy(hcd->phy);
198
	}
199

200
	usb_remove_hcd(hcd);
201 202 203 204 205 206 207

	/*
	 * do platform specific un-initialization:
	 * release iomux pins, disable clock, etc.
	 */
	if (pdata->exit)
		pdata->exit(pdev);
208 209 210 211 212
	iounmap(hcd->regs);
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
	usb_put_hcd(hcd);
}

213
static int ehci_fsl_setup_phy(struct usb_hcd *hcd,
214 215
			       enum fsl_usb2_phy_modes phy_mode,
			       unsigned int port_offset)
216
{
217
	u32 portsc;
218
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
219
	void __iomem *non_ehci = hcd->regs;
220
	struct device *dev = hcd->self.controller;
J
Jingoo Han 已提交
221
	struct fsl_usb2_platform_data *pdata = dev_get_platdata(dev);
222

223 224
	if (pdata->controller_ver < 0) {
		dev_warn(hcd->self.controller, "Could not get controller version\n");
225
		return -ENODEV;
226
	}
227 228 229 230

	portsc = ehci_readl(ehci, &ehci->regs->port_status[port_offset]);
	portsc &= ~(PORT_PTS_MSK | PORT_PTS_PTW);

231 232
	switch (phy_mode) {
	case FSL_USB2_PHY_ULPI:
233
		if (pdata->have_sysif_regs && pdata->controller_ver) {
234
			/* controller version 1.6 or above */
235 236 237 238 239 240 241 242 243
			setbits32(non_ehci + FSL_SOC_USB_CTRL,
					ULPI_PHY_CLK_SEL);
			/*
			 * Due to controller issue of PHY_CLK_VALID in ULPI
			 * mode, we set USB_CTRL_USB_EN before checking
			 * PHY_CLK_VALID, otherwise PHY_CLK_VALID doesn't work.
			 */
			clrsetbits_be32(non_ehci + FSL_SOC_USB_CTRL,
					UTMI_PHY_EN, USB_CTRL_USB_EN);
244
		}
245 246 247 248 249 250 251 252 253
		portsc |= PORT_PTS_ULPI;
		break;
	case FSL_USB2_PHY_SERIAL:
		portsc |= PORT_PTS_SERIAL;
		break;
	case FSL_USB2_PHY_UTMI_WIDE:
		portsc |= PORT_PTS_PTW;
		/* fall through */
	case FSL_USB2_PHY_UTMI:
254
		if (pdata->have_sysif_regs && pdata->controller_ver) {
255
			/* controller version 1.6 or above */
256
			setbits32(non_ehci + FSL_SOC_USB_CTRL, UTMI_PHY_EN);
257 258 259
			mdelay(FSL_UTMI_PHY_DLY);  /* Delay for UTMI PHY CLK to
						become stable - 10ms*/
		}
260
		/* enable UTMI PHY */
261 262 263
		if (pdata->have_sysif_regs)
			setbits32(non_ehci + FSL_SOC_USB_CTRL,
				  CTRL_UTMI_PHY_EN);
264 265 266 267 268
		portsc |= PORT_PTS_UTMI;
		break;
	case FSL_USB2_PHY_NONE:
		break;
	}
269

270 271
	if (pdata->have_sysif_regs && pdata->controller_ver &&
	    (phy_mode == FSL_USB2_PHY_ULPI)) {
272 273 274 275 276 277 278 279
		/* check PHY_CLK_VALID to get phy clk valid */
		if (!spin_event_timeout(in_be32(non_ehci + FSL_SOC_USB_CTRL) &
				PHY_CLK_VALID, FSL_USB_PHY_CLK_TIMEOUT, 0)) {
			printk(KERN_WARNING "fsl-ehci: USB PHY clock invalid\n");
			return -EINVAL;
		}
	}

280
	ehci_writel(ehci, portsc, &ehci->regs->port_status[port_offset]);
281

282
	if (phy_mode != FSL_USB2_PHY_ULPI && pdata->have_sysif_regs)
283 284 285
		setbits32(non_ehci + FSL_SOC_USB_CTRL, USB_CTRL_USB_EN);

	return 0;
286 287
}

288
static int ehci_fsl_usb_setup(struct ehci_hcd *ehci)
289
{
290
	struct usb_hcd *hcd = ehci_to_hcd(ehci);
291 292 293
	struct fsl_usb2_platform_data *pdata;
	void __iomem *non_ehci = hcd->regs;

J
Jingoo Han 已提交
294
	pdata = dev_get_platdata(hcd->self.controller);
295 296

	if (pdata->have_sysif_regs) {
297 298 299 300
		/*
		* Turn on cache snooping hardware, since some PowerPC platforms
		* wholly rely on hardware to deal with cache coherent
		*/
301

302 303 304 305 306 307
		/* Setup Snooping for all the 4GB space */
		/* SNOOP1 starts from 0x0, size 2G */
		out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0 | SNOOP_SIZE_2GB);
		/* SNOOP2 starts from 0x80000000, size 2G */
		out_be32(non_ehci + FSL_SOC_USB_SNOOP2, 0x80000000 | SNOOP_SIZE_2GB);
	}
308

309 310
	if ((pdata->operating_mode == FSL_USB2_DR_HOST) ||
			(pdata->operating_mode == FSL_USB2_DR_OTG))
311 312
		if (ehci_fsl_setup_phy(hcd, pdata->phy_mode, 0))
			return -EINVAL;
313 314

	if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
315 316 317 318 319 320 321 322 323 324
		unsigned int chip, rev, svr;

		svr = mfspr(SPRN_SVR);
		chip = svr >> 16;
		rev = (svr >> 4) & 0xf;

		/* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
		if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
			ehci->has_fsl_port_bug = 1;

325
		if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
326 327 328
			if (ehci_fsl_setup_phy(hcd, pdata->phy_mode, 0))
				return -EINVAL;

329
		if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
330 331
			if (ehci_fsl_setup_phy(hcd, pdata->phy_mode, 1))
				return -EINVAL;
332 333
	}

334
	if (pdata->have_sysif_regs) {
335
#ifdef CONFIG_FSL_SOC_BOOKE
336 337
		out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x00000008);
		out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000080);
338
#else
339 340
		out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
		out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
341
#endif
342 343
		out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
	}
344 345

	return 0;
346 347 348 349 350
}

/* called after powerup, by probe or system-pm "wakeup" */
static int ehci_fsl_reinit(struct ehci_hcd *ehci)
{
351 352
	if (ehci_fsl_usb_setup(ehci))
		return -EINVAL;
353 354 355 356 357 358 359 360 361

	return 0;
}

/* called during probe() after chip reset completes */
static int ehci_fsl_setup(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	int retval;
362
	struct fsl_usb2_platform_data *pdata;
363
	struct device *dev;
364

365
	dev = hcd->self.controller;
J
Jingoo Han 已提交
366
	pdata = dev_get_platdata(hcd->self.controller);
367 368
	ehci->big_endian_desc = pdata->big_endian_desc;
	ehci->big_endian_mmio = pdata->big_endian_mmio;
369 370 371 372

	/* EHCI registers start at offset 0x100 */
	ehci->caps = hcd->regs + 0x100;

373 374 375 376 377 378 379 380 381
#ifdef CONFIG_PPC_83xx
	/*
	 * Deal with MPC834X that need port power to be cycled after the power
	 * fault condition is removed. Otherwise the state machine does not
	 * reflect PORTSC[CSC] correctly.
	 */
	ehci->need_oc_pp_cycle = 1;
#endif

382 383
	hcd->has_tt = 1;

384
	retval = ehci_setup(hcd);
385 386 387
	if (retval)
		return retval;

388 389 390 391 392 393 394 395 396 397
	if (of_device_is_compatible(dev->parent->of_node,
				    "fsl,mpc5121-usb2-dr")) {
		/*
		 * set SBUSCFG:AHBBRST so that control msgs don't
		 * fail when doing heavy PATA writes.
		 */
		ehci_writel(ehci, SBUSCFG_INCR8,
			    hcd->regs + FSL_SOC_USB_SBUSCFG);
	}

398 399 400 401
	retval = ehci_fsl_reinit(ehci);
	return retval;
}

402 403 404 405 406 407 408 409 410 411 412
struct ehci_fsl {
	struct ehci_hcd	ehci;

#ifdef CONFIG_PM
	/* Saved USB PHY settings, need to restore after deep sleep. */
	u32 usb_ctrl;
#endif
};

#ifdef CONFIG_PM

413 414 415 416 417
#ifdef CONFIG_PPC_MPC512x
static int ehci_fsl_mpc512x_drv_suspend(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
J
Jingoo Han 已提交
418
	struct fsl_usb2_platform_data *pdata = dev_get_platdata(dev);
419 420
	u32 tmp;

421
#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	u32 mode = ehci_readl(ehci, hcd->regs + FSL_SOC_USB_USBMODE);
	mode &= USBMODE_CM_MASK;
	tmp = ehci_readl(ehci, hcd->regs + 0x140);	/* usbcmd */

	dev_dbg(dev, "suspend=%d already_suspended=%d "
		"mode=%d  usbcmd %08x\n", pdata->suspended,
		pdata->already_suspended, mode, tmp);
#endif

	/*
	 * If the controller is already suspended, then this must be a
	 * PM suspend.  Remember this fact, so that we will leave the
	 * controller suspended at PM resume time.
	 */
	if (pdata->suspended) {
		dev_dbg(dev, "already suspended, leaving early\n");
		pdata->already_suspended = 1;
		return 0;
	}

	dev_dbg(dev, "suspending...\n");

444
	ehci->rh_state = EHCI_RH_SUSPENDED;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	dev->power.power_state = PMSG_SUSPEND;

	/* ignore non-host interrupts */
	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);

	/* stop the controller */
	tmp = ehci_readl(ehci, &ehci->regs->command);
	tmp &= ~CMD_RUN;
	ehci_writel(ehci, tmp, &ehci->regs->command);

	/* save EHCI registers */
	pdata->pm_command = ehci_readl(ehci, &ehci->regs->command);
	pdata->pm_command &= ~CMD_RUN;
	pdata->pm_status  = ehci_readl(ehci, &ehci->regs->status);
	pdata->pm_intr_enable  = ehci_readl(ehci, &ehci->regs->intr_enable);
	pdata->pm_frame_index  = ehci_readl(ehci, &ehci->regs->frame_index);
	pdata->pm_segment  = ehci_readl(ehci, &ehci->regs->segment);
	pdata->pm_frame_list  = ehci_readl(ehci, &ehci->regs->frame_list);
	pdata->pm_async_next  = ehci_readl(ehci, &ehci->regs->async_next);
	pdata->pm_configured_flag  =
		ehci_readl(ehci, &ehci->regs->configured_flag);
	pdata->pm_portsc = ehci_readl(ehci, &ehci->regs->port_status[0]);
	pdata->pm_usbgenctrl = ehci_readl(ehci,
					  hcd->regs + FSL_SOC_USB_USBGENCTRL);

	/* clear the W1C bits */
	pdata->pm_portsc &= cpu_to_hc32(ehci, ~PORT_RWC_BITS);

	pdata->suspended = 1;

	/* clear PP to cut power to the port */
	tmp = ehci_readl(ehci, &ehci->regs->port_status[0]);
	tmp &= ~PORT_POWER;
	ehci_writel(ehci, tmp, &ehci->regs->port_status[0]);

	return 0;
}

static int ehci_fsl_mpc512x_drv_resume(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
J
Jingoo Han 已提交
487
	struct fsl_usb2_platform_data *pdata = dev_get_platdata(dev);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	u32 tmp;

	dev_dbg(dev, "suspend=%d already_suspended=%d\n",
		pdata->suspended, pdata->already_suspended);

	/*
	 * If the controller was already suspended at suspend time,
	 * then don't resume it now.
	 */
	if (pdata->already_suspended) {
		dev_dbg(dev, "already suspended, leaving early\n");
		pdata->already_suspended = 0;
		return 0;
	}

	if (!pdata->suspended) {
		dev_dbg(dev, "not suspended, leaving early\n");
		return 0;
	}

	pdata->suspended = 0;

	dev_dbg(dev, "resuming...\n");

	/* set host mode */
	tmp = USBMODE_CM_HOST | (pdata->es ? USBMODE_ES : 0);
	ehci_writel(ehci, tmp, hcd->regs + FSL_SOC_USB_USBMODE);

	ehci_writel(ehci, pdata->pm_usbgenctrl,
		    hcd->regs + FSL_SOC_USB_USBGENCTRL);
	ehci_writel(ehci, ISIPHYCTRL_PXE | ISIPHYCTRL_PHYE,
		    hcd->regs + FSL_SOC_USB_ISIPHYCTRL);

521 522
	ehci_writel(ehci, SBUSCFG_INCR8, hcd->regs + FSL_SOC_USB_SBUSCFG);

523 524 525 526 527 528 529 530 531 532 533 534
	/* restore EHCI registers */
	ehci_writel(ehci, pdata->pm_command, &ehci->regs->command);
	ehci_writel(ehci, pdata->pm_intr_enable, &ehci->regs->intr_enable);
	ehci_writel(ehci, pdata->pm_frame_index, &ehci->regs->frame_index);
	ehci_writel(ehci, pdata->pm_segment, &ehci->regs->segment);
	ehci_writel(ehci, pdata->pm_frame_list, &ehci->regs->frame_list);
	ehci_writel(ehci, pdata->pm_async_next, &ehci->regs->async_next);
	ehci_writel(ehci, pdata->pm_configured_flag,
		    &ehci->regs->configured_flag);
	ehci_writel(ehci, pdata->pm_portsc, &ehci->regs->port_status[0]);

	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
535
	ehci->rh_state = EHCI_RH_RUNNING;
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	dev->power.power_state = PMSG_ON;

	tmp = ehci_readl(ehci, &ehci->regs->command);
	tmp |= CMD_RUN;
	ehci_writel(ehci, tmp, &ehci->regs->command);

	usb_hcd_resume_root_hub(hcd);

	return 0;
}
#else
static inline int ehci_fsl_mpc512x_drv_suspend(struct device *dev)
{
	return 0;
}

static inline int ehci_fsl_mpc512x_drv_resume(struct device *dev)
{
	return 0;
}
#endif /* CONFIG_PPC_MPC512x */

558 559 560 561 562 563 564 565 566 567 568 569 570
static struct ehci_fsl *hcd_to_ehci_fsl(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);

	return container_of(ehci, struct ehci_fsl, ehci);
}

static int ehci_fsl_drv_suspend(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
	void __iomem *non_ehci = hcd->regs;

571 572 573 574 575
	if (of_device_is_compatible(dev->parent->of_node,
				    "fsl,mpc5121-usb2-dr")) {
		return ehci_fsl_mpc512x_drv_suspend(dev);
	}

576 577
	ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd),
			device_may_wakeup(dev));
578 579 580 581 582 583 584 585 586 587 588 589 590 591
	if (!fsl_deep_sleep())
		return 0;

	ehci_fsl->usb_ctrl = in_be32(non_ehci + FSL_SOC_USB_CTRL);
	return 0;
}

static int ehci_fsl_drv_resume(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	void __iomem *non_ehci = hcd->regs;

592 593 594 595 596
	if (of_device_is_compatible(dev->parent->of_node,
				    "fsl,mpc5121-usb2-dr")) {
		return ehci_fsl_mpc512x_drv_resume(dev);
	}

597
	ehci_prepare_ports_for_controller_resume(ehci);
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	if (!fsl_deep_sleep())
		return 0;

	usb_root_hub_lost_power(hcd->self.root_hub);

	/* Restore USB PHY settings and enable the controller. */
	out_be32(non_ehci + FSL_SOC_USB_CTRL, ehci_fsl->usb_ctrl);

	ehci_reset(ehci);
	ehci_fsl_reinit(ehci);

	return 0;
}

static int ehci_fsl_drv_restore(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);

	usb_root_hub_lost_power(hcd->self.root_hub);
	return 0;
}

static struct dev_pm_ops ehci_fsl_pm_ops = {
	.suspend = ehci_fsl_drv_suspend,
	.resume = ehci_fsl_drv_resume,
	.restore = ehci_fsl_drv_restore,
};

#define EHCI_FSL_PM_OPS		(&ehci_fsl_pm_ops)
#else
#define EHCI_FSL_PM_OPS		NULL
#endif /* CONFIG_PM */

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
#ifdef CONFIG_USB_OTG
static int ehci_start_port_reset(struct usb_hcd *hcd, unsigned port)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	u32 status;

	if (!port)
		return -EINVAL;

	port--;

	/* start port reset before HNP protocol time out */
	status = readl(&ehci->regs->port_status[port]);
	if (!(status & PORT_CONNECT))
		return -ENODEV;

	/* khubd will finish the reset later */
	if (ehci_is_TDI(ehci)) {
		writel(PORT_RESET |
		       (status & ~(PORT_CSC | PORT_PEC | PORT_OCC)),
		       &ehci->regs->port_status[port]);
	} else {
		writel(PORT_RESET, &ehci->regs->port_status[port]);
	}

	return 0;
}
#else
#define ehci_start_port_reset	NULL
#endif /* CONFIG_USB_OTG */


663 664 665
static const struct hc_driver ehci_fsl_hc_driver = {
	.description = hcd_name,
	.product_desc = "Freescale On-Chip EHCI Host Controller",
666
	.hcd_priv_size = sizeof(struct ehci_fsl),
667 668 669 670 671

	/*
	 * generic hardware linkage
	 */
	.irq = ehci_irq,
672
	.flags = HCD_USB2 | HCD_MEMORY,
673 674 675 676 677 678 679

	/*
	 * basic lifecycle operations
	 */
	.reset = ehci_fsl_setup,
	.start = ehci_run,
	.stop = ehci_stop,
680
	.shutdown = ehci_shutdown,
681 682 683 684 685 686 687

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue = ehci_urb_enqueue,
	.urb_dequeue = ehci_urb_dequeue,
	.endpoint_disable = ehci_endpoint_disable,
688
	.endpoint_reset = ehci_endpoint_reset,
689 690 691 692 693 694 695 696 697 698 699 700 701

	/*
	 * scheduling support
	 */
	.get_frame_number = ehci_get_frame,

	/*
	 * root hub support
	 */
	.hub_status_data = ehci_hub_status_data,
	.hub_control = ehci_hub_control,
	.bus_suspend = ehci_bus_suspend,
	.bus_resume = ehci_bus_resume,
702
	.start_port_reset = ehci_start_port_reset,
703
	.relinquish_port = ehci_relinquish_port,
704
	.port_handed_over = ehci_port_handed_over,
705 706

	.clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
707 708 709 710 711 712 713
};

static int ehci_fsl_drv_probe(struct platform_device *pdev)
{
	if (usb_disabled())
		return -ENODEV;

714
	/* FIXME we only want one one probe() not two */
715 716 717 718 719 720 721
	return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
}

static int ehci_fsl_drv_remove(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);

722
	/* FIXME we only want one one remove() not two */
723 724 725 726
	usb_hcd_fsl_remove(hcd, pdev);
	return 0;
}

727
MODULE_ALIAS("platform:fsl-ehci");
728

729
static struct platform_driver ehci_fsl_driver = {
730 731
	.probe = ehci_fsl_drv_probe,
	.remove = ehci_fsl_drv_remove,
732
	.shutdown = usb_hcd_platform_shutdown,
733
	.driver = {
734
		.name = "fsl-ehci",
735
		.owner	= THIS_MODULE,
736
		.pm = EHCI_FSL_PM_OPS,
737
	},
738
};