/* 2008-08-12: File added and changed by Sony Corporation */
/*
 * EHCI HCD (Host Controller Driver) for USB.
 * Bus Glue for NEC EMMA3PF
 * Based on "ehci-au1xxx.c"
 *
 * Author: NEC Electronics Corporation
 *
 * Copyright (C) 2008 NEC Electronics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/platform_device.h>
#include <asm/emma3.h>

#define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
MODULE_DESCRIPTION (DRIVER_INFO);

extern int usb_disabled(void);

static void emma_pseudo_pci_init(struct platform_device *dev)
{
	struct resource *res;
	int		irq;
	u32		d;

	res = platform_get_resource_byname(dev, IORESOURCE_MEM, "ehci_mem");
	irq = platform_get_irq_byname(dev, "ehci_irq");

	/* PCI BAR */
	emma_out32(EMMA_EHCI_CONF_BASE + PCI_BASE_ADDRESS_0,
		   CPHYSADDR(res->start));

	/* PCI interrupt line */
	d = emma_in32(EMMA_EHCI_CONF_BASE + PCI_INTERRUPT_LINE);
	d &= ~0xff;
	d |= irq;
	emma_out32(EMMA_EHCI_CONF_BASE + PCI_INTERRUPT_LINE, d);

	/* PCI command status */
	emma_out32(EMMA_EHCI_CONF_BASE + PCI_COMMAND,
		   PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
}

static void emma_start_ehc(struct platform_device *dev)
{
	pr_debug(__FILE__ ": starting EMMA EHCI USB Controller\n");

	emma_pseudo_pci_init(dev);

	pr_debug(__FILE__ ": Clock to USB host has been enabled\n");
}

static void emma_stop_ehc(struct platform_device *dev)
{
	pr_debug(__FILE__ ": stopping EMMA EHCI USB Controller\n");
}

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

/**
 * usb_ehci_emma_probe - initialize EMMA-based HCDs
 * Context: !in_interrupt()
 *
 * Allocates basic resources for this USB host controller, and
 * then invokes the start() method for the HCD associated with it
 * through the hotplug entry's driver_data.
 *
 */
int usb_ehci_emma_probe(const struct hc_driver *driver,
				struct platform_device *dev)
{
	int retval;
	struct usb_hcd	*hcd = 0;
	struct resource *res;
	int		irq;

	res = platform_get_resource_byname(
			dev, IORESOURCE_MEM, "ehci_mem");
	if (!res) {
		pr_debug("Found EMMA_HC with no MEM. Check %s setup!\n",
			dev->dev.bus_id);
		retval = -ENODEV;
		goto err1;
	}

	irq = platform_get_irq_byname(dev, "ehci_irq");
	if (irq == -ENXIO) {
		pr_debug("Found EMMA_HC with no IRQ. Check %s setup!\n",
				dev->dev.bus_id);
		retval = -ENODEV;
		goto err1;
	}

	hcd = usb_create_hcd(driver, &dev->dev, "emma-ehci");
	if (!hcd)
		return -ENOMEM;
	hcd->rsrc_start = (u64)res->start;
	hcd->rsrc_len = (u64)(res->end - res->start + 1);

	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
		pr_debug("request_mem_region failed");
		retval = -EBUSY;
		goto err1;
	}

	hcd->regs = (void __iomem *)ioremap(hcd->rsrc_start, hcd->rsrc_len);

	emma_start_ehc(dev);

	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
	if (retval == 0)
		return retval;

	emma_stop_ehc(dev);
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
 err1:
	usb_put_hcd(hcd);
	return retval;
}

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

/**
 * usb_ehci_hcd_emma_remove - shutdown processing for EMMA HCDs
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_ehci_hcd_emma_probe(), first invoking
 * the HCD's stop() method.  It is always called from a thread
 * context, normally "rmmod", "apmd", or something similar.
 *
 */
void usb_ehci_emma_remove(struct usb_hcd *hcd, struct platform_device *dev)
{
	usb_remove_hcd(hcd);
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
	usb_put_hcd(hcd);
	emma_stop_ehc(dev);
}

/* called during probe() after chip reset completes */
static int ehci_hcd_emma_setup(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);

	int retval;

	ehci->caps = hcd->regs;
	ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
	dbg_hcs_params(ehci, "reset");
	dbg_hcc_params(ehci, "reset");

	/* cache this readonly data; minimize reads */
	ehci->hcs_params = readl(&ehci->caps->hcs_params);

	retval = ehci_halt(ehci);
	if (retval)
		return retval;

	/* data structure init */
	retval = ehci_init(hcd);
	if (retval)
		return retval;

	ehci_reset(ehci);

	ehci->sbrn = (emma_in32(EMMA_EHCI_CONF_BASE + 0x60) & 0xff);
	ehci_port_power(ehci, 0);

	return 0;
}

static const struct hc_driver ehci_emma_hc_driver = {
	.description = hcd_name,
	.product_desc = "EMMA EHCI",
	.hcd_priv_size = sizeof(struct ehci_hcd),

	/*
	 * generic hardware linkage
	 */
	.irq = ehci_irq,
	.flags = HCD_MEMORY | HCD_USB2,

	/*
	 * basic lifecycle operations
	 */
	.reset = ehci_hcd_emma_setup,
	.start = ehci_run,
	.stop = ehci_stop,
	.shutdown = ehci_shutdown,

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue = ehci_urb_enqueue,
	.urb_dequeue = ehci_urb_dequeue,
	.endpoint_disable = ehci_endpoint_disable,

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

	/*
	 * root hub support
	 */
	.hub_status_data = ehci_hub_status_data,
	.hub_control = ehci_hub_control,
#ifdef	CONFIG_PM
	.bus_suspend		= ehci_bus_suspend,
	.bus_resume		= ehci_bus_resume,
#endif
};

static int ehci_hcd_emma_drv_probe(struct platform_device *pdev)
{
	int ret;

	pr_debug("In ehci_hcd_emma_drv_probe\n");

	if (usb_disabled())
		return -ENODEV;

	ret = usb_ehci_emma_probe(&ehci_emma_hc_driver, pdev);
	return ret;
}

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

	usb_ehci_emma_remove(hcd, pdev);
	return 0;
}

#ifdef CONFIG_PM
static int ehci_hcd_emma_drv_suspend(struct platform_device *pdev, pm_message_t message)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
	unsigned long		flags;
	int			rc = 0;

	if (time_before(jiffies, ehci->next_statechange))
		msleep(10);

	spin_lock_irqsave (&ehci->lock, flags);
	if (hcd->state != HC_STATE_SUSPENDED) {
		rc = -EINVAL;
		goto bail;
	}

	pdev->dev.power.power_state = PMSG_SUSPEND;

	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
	(void)ehci_readl(ehci, &ehci->regs->intr_enable);

	/* make sure snapshot being resumed re-enumerates everything */
	if (message.event == PM_EVENT_PRETHAW) {
		ehci_halt(ehci);
		ehci_reset(ehci);
	}

	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
bail:
	spin_unlock_irqrestore (&ehci->lock, flags);

	return rc;
}

static int ehci_hcd_emma_drv_resume(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);

	emma_pseudo_pci_init(pdev);

	// maybe restore FLADJ

	if (time_before(jiffies, ehci->next_statechange))
		msleep(100);

	/* Mark hardware accessible again as we are out of D3 state by now */
	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);

	pdev->dev.power.power_state = PMSG_ON;

	/* If CF is still set, we maintained PCI Vaux power.
	 * Just undo the effect of ehci_pci_suspend().
	 */
	if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
		int	mask = INTR_MASK;

		if (!device_may_wakeup(&hcd->self.root_hub->dev))
			mask &= ~STS_PCD;
		ehci_writel(ehci, mask, &ehci->regs->intr_enable);
		ehci_readl(ehci, &ehci->regs->intr_enable);
		return 0;
	}

	ehci_dbg(ehci, "lost power, restarting\n");
	usb_root_hub_lost_power(hcd->self.root_hub);

	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);

	/* here we "know" root ports should always stay powered */
	ehci_port_power(ehci, 1);

	return 0;
}
#else
#define ehci_hcd_emma_drv_suspend NULL
#define ehci_hcd_emma_drv_resume NULL
#endif

MODULE_ALIAS("emma-ehci");

static struct platform_driver ehci_hcd_emma_driver = {
	.probe		= ehci_hcd_emma_drv_probe,
	.remove		= ehci_hcd_emma_drv_remove,
	.suspend	= ehci_hcd_emma_drv_suspend,
	.resume		= ehci_hcd_emma_drv_resume,
	.driver = {
		.name = "emma-ehci",
		.owner	= THIS_MODULE
	}
};
