/*
 * Copyright 2011 Allied Telesis Labs
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */


#include <common.h>
#include <asm/io.h>
#include <usb.h>
#include "ehci.h"
#include "ehci-core.h"

#define BCM53000_USB20_0_BASE	0xb8004000
#define BCM53000_USB_IOCTRL		0xb8104408
#define BCM53000_USB_RESETCTRL	0xb8104800

/*
 * USB 2.0 Setup
 */
static void usb_bcm53000_setup(void)
{
	__u32 tmp;

	/* enable the USB clock */
	*(volatile __u32 *)BCM53000_USB_IOCTRL = 0x01;
	tmp = *(volatile __u32 *)BCM53000_USB_IOCTRL;

	/* take the USB core out of reset */
	*(volatile __u32 *)BCM53000_USB_RESETCTRL = 0;
	udelay(2);

	/* enable the USB clock */
	*(volatile __u32 *)BCM53000_USB_IOCTRL = 0x01;
	udelay(20);

	/* write to the magic undocumented register */
	*(volatile __u32 *)(BCM53000_USB20_0_BASE + 0x200) = 0x7ff;
	udelay(100);

	tmp = *(volatile __u32 *)(BCM53000_USB20_0_BASE + 0x304);
	tmp &= ~0x100;
	*(volatile __u32 *)(BCM53000_USB20_0_BASE + 0x304) = tmp;
	udelay(20);

}

/*
 * Create the appropriate control structures to manage
 * a new EHCI host controller.
 */
int ehci_hcd_init(void)
{
	usb_bcm53000_setup();

	hccr = (struct ehci_hccr *)(BCM53000_USB20_0_BASE);
	hcor = (struct ehci_hcor *)((uint32_t) hccr
			+ HC_LENGTH(ehci_readl(&hccr->cr_capbase)));

	debug("bcm53000-ehci: init hccr %x and hcor %x hc_length %d\n",
		(uint32_t)hccr, (uint32_t)hcor,
		(uint32_t)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));

	return 0;
}

/*
 * Destroy the appropriate control structures corresponding
 * the the EHCI host controller.
 */
int ehci_hcd_stop(void)
{
	/* disable the USB clock */
	*(volatile __u32 *)BCM53000_USB_IOCTRL = 0x00;
	udelay(20);

	/* put the USB core into reset */
	*(volatile __u32 *)BCM53000_USB_RESETCTRL = 1;
	udelay(2);

	return 0;
}

