/*
 * 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/emma3.h>

/* ftop registers */
#define EMMA_FLIP_REGBASE	(0xb0200100)
#define EMMA_FLIP_M0_CTR	(0x0)
#define EMMA_FLIP_USB_CTR	(0x20)

#define FLIP_OCI1_EN		(0x00100000UL)
#define FLIP_OCI2_EN		(0x00200000UL)
#define FLIP_PPON1_EN		(0x00400000UL)
#define FLIP_PPON2_EN		(0x00800000UL)
#define FLIP_OCI1_ACT		(0x00010000UL)
#define FLIP_OCI2_ACT		(0x00020000UL)
#define FLIP_PPON1_ACT		(0x00040000UL)
#define FLIP_PPON2_ACT		(0x00080000UL)
#define FLIP_ENDIAN_SWAP	(0x00000002UL)

/* ftop pseudo pci */
#define EHCI_CONFBASE		(0xb0200500UL)
#define OHCI_CONFBASE		(0xb0200200UL)
#define PCI_CONFIG		EHCI_CONFBASE
#define	PCI_CMD_MEM_ENABLE	(0x0002)
#define	PCI_CMD_MASTER_ENABLE	(0x0004)

#if defined(__LITTLE_ENDIAN)
#define	emma_ehci_in(x)	readl((void __iomem *) (x))
#define	emma_ehci_out(y, x)	writel((unsigned long)(y), (void __iomem *)(x))
#elif defined(__BIG_ENDIAN)
#define	emma_ehci_in(x)	emma3_in32((u32)(x))
#define	emma_ehci_out(y, x)	emma3_out32((u32)(x), (y))
#else
#error "No endian format defined!"
#endif

static int emma_flip_init(void)
{
	u32	d;

	emma_ehci_out(0x00fc0900,
		(void *)(EMMA_FLIP_REGBASE + EMMA_FLIP_USB_CTR));

	d = emma_ehci_in((void *)(EMMA_FLIP_REGBASE + EMMA_FLIP_M0_CTR));
	d |= FLIP_ENDIAN_SWAP;
	emma_ehci_out(d, (void *)(EMMA_FLIP_REGBASE + EMMA_FLIP_M0_CTR));

	return 0;
}

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_ehci_out(CPHYSADDR(res->start), (void *)(PCI_CONFIG + 0x10));

	/* PCI interrupt line */
	d = emma_ehci_in((void *)(PCI_CONFIG + 0x3c));
	d &= ~0xff;
	d |= irq;
	emma_ehci_out(d, (void *)(PCI_CONFIG + 0x3c));

	/* Fix HCD H/W bug on EMMA3 (Recomend method from Renesas) */
	/* Ref. IMB-NM1-000371 (2013/07/01) */
	emma_ehci_out(0x0801001d, (void *)(PCI_CONFIG + 0xf8));

	/* PCI command status */
	emma_ehci_out(PCI_CMD_MASTER_ENABLE|PCI_CMD_MEM_ENABLE,
			(void *)(PCI_CONFIG + 0x4));

}

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

	emma_flip_init();
	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 ehci_hcd *ehci = 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 *)((u32)hcd->rsrc_start);

	emma_start_ehc(dev);

	/* fix  some ehci registers */
	ehci = hcd_to_ehci(hcd);
	ehci->caps = hcd->regs;
	ehci->regs = hcd->regs + HC_LENGTH(emma_ehci_in(&ehci->caps->hc_capbase));

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

	retval = usb_add_hcd(hcd, dev->resource[1].start, 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);
}

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_init,
	.start = ehci_run,
	.stop = ehci_stop,

	/*
	 * 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
	.hub_suspend = ehci_hub_suspend,
	.hub_resume = ehci_hub_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;
}

MODULE_ALIAS("emma-ehci");

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