/*
 * (C) Copyright 2004
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * This code is based on linux driver for sl811hs chip, source at
 * drivers/usb/host/sl811.c:
 *
 * SL811 Host Controller Interface driver for USB.
 *
 * Copyright (c) 2003/06, Courage Co., Ltd.
 *
 * Based on:
 *	1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
 *	  Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
 *	  Adam Richter, Gregory P. Smith;
 *	2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
 *	3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <usb.h>
#include <asm/io.h>
#include "mb9bfxxx.h"

#ifdef MB9BFXXX_DEBUG
static int debug = 9;
#endif

static int root_hub_devnum = 0;
static struct usb_port_status rh_status = { 0 };/* root hub port status */

static int mb9bfxxx_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
			       void *data, int buf_len, struct devrequest *cmd);

static void inline mb9bfxxx_read_buf(__u8 *buf, int size)
{
	while (size > 0) {
		*buf++ = readb(EP1DTL);
		if (size > 1)
			*buf++ = readb(EP1DTH);
		size -= 2;
	}
}

static void inline mb9bfxxx_write_buf(__u8 *buf, int size)
{
	writew((readw(EP1C) & ~0x1ff) | size, EP1C);
	writew(readw(EP1S) & ~(1 << 15), EP1S);
	size++;
	while (size > 0) {
		writeb(buf[0], EP1DTL);
		if (size > 1)
			writeb(buf[1], EP1DTH);
		buf += 2;
		size -= 2;
	}
	writew(readw(EP1S) & ~(1 << 10), EP1S);
}

/*
 * This function resets MB9BFXXX controller and detects the speed of
 * the connecting device
 *
 * Return: 0 = no device attached; 1 = USB device attached
 */
static int mb9bfxxx_hc_reset(void)
{
	int status ;
	int i;

	writew(0x0080, UDCC);
	writew(0x0001, HCNT);

	for(i = 0; i < 10000; i++) {
		if((status = readb(HIRQ)) & HIRQ_CNNIRQ)
			break;
		udelay(10);
	}
	
	if (!(status & HIRQ_CNNIRQ)) {
		/* Device is not present */
		PDEBUG(0, "Device not present\n");
		rh_status.wPortStatus &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
		rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
		return 0;
	}

	status = readw(HSTATE);
	if (status & HSTATE_TMODE) {
		/* full speed device connect directly to root hub */
		PDEBUG (0, "Full speed Device attached\n");
		writew(readw(HSTATE) | HSTATE_CLKSEL, HSTATE);
		while(!(readw(HSTATE) & HSTATE_CLKSEL));
		writew(0x0000, UDCC);
		writew(readw(HCNT) | HCNT_URST, HCNT);
		while(!(readw(HIRQ) & HIRQ_URIRQ));
		while(!(readw(HIRQ) & HIRQ_CNNIRQ));
		writew(0x0001, UDCC);
		writew(readw(HSTATE) | HSTATE_CLKSEL, HSTATE);
		while(!(readw(HSTATE) & HSTATE_CLKSEL));
		rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION;
		rh_status.wPortStatus &= ~USB_PORT_STAT_LOW_SPEED;
	} else {
		/* slow speed device connect directly to root-hub */
		PDEBUG(0, "Low speed Device attached\n");
		writew(readw(HSTATE) & ~HSTATE_CLKSEL, HSTATE);
		while((readw(HSTATE) & HSTATE_CLKSEL));
		writew(0x0000, UDCC);
		writew(readw(HCNT) | HCNT_URST, HCNT);
		while(!(readw(HIRQ) & HIRQ_URIRQ));
		while(!(readw(HIRQ) & HIRQ_CNNIRQ));
		writew(0x0001, UDCC);
		writew(readw(HSTATE) & ~HSTATE_CLKSEL, HSTATE);
		while((readw(HSTATE) & HSTATE_CLKSEL));
		rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION | USB_PORT_STAT_LOW_SPEED;
	}
	writew(0xffff, HRTIMER);
	writeb(0x03, HRTIMER2);
	udelay(500);
	writeb(0, HADDR);
	writeb(TOKEN_SOF, HTOKEN);

	rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
	return 1;
}

int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
{
	root_hub_devnum = 0;
	mb9bfxxx_hc_reset();
	return 0;
}

int usb_lowlevel_stop(int index)
{
	mb9bfxxx_hc_reset();
	return 0;
}

static int mb9bfxxx_send_packet(struct usb_device *dev, unsigned long pipe, __u8 *buffer, int len)
{
	__u16 ctrl = 0;
	__u16 status = 0;
	__u8 token = 0;
	int err = 3, time_start = get_timer(0);

	if (len > 256)
		return -1;

	if (usb_pipeout(pipe)) {
		ctrl |= CTRL_EPEN | CTRL_DIR_OUT;
		if (usb_pipecontrol(pipe)) {
			token |= TOKEN_SETUP;
		} else {
			token |= TOKEN_OUT;
		}
	}
	if (usb_pipein(pipe)) {
		ctrl |= CTRL_EPEN | CTRL_DIR_IN | 0x100;
		token |= TOKEN_IN;
	}
	switch (usb_pipetype(pipe)) {
	case PIPE_INTERRUPT:
		ctrl |= CTRL_TYPE_INTERRUPT;
		break;
	case PIPE_BULK:
		ctrl |= CTRL_TYPE_BULK;
		break;
	}

	if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)))
		token |= TOKEN_TGGE;
	token |= usb_pipeendpoint(pipe);
	while(err > 0 ) {
		writew(ctrl, EP1C);
		writew(0x0400, EP1S);
		if (usb_pipeout(pipe) && len)
			mb9bfxxx_write_buf(buffer, len);
		udelay(1);
		writeb(token, HTOKEN);
		udelay(10);
		while (!(readb(HIRQ) & HIRQ_CMPIRQ)) {
			if (5*CONFIG_SYS_HZ < get_timer(time_start)) {
				printf("USB transmit timed out\n");
				return -USB_ST_CRC_ERR;
			}
		}
		udelay(10);
		writeb(0x00, HIRQ);

		status = readb(HERR);
		writeb(0x00, HERR);
		switch (status & HERR_HS) {
		case HERR_HS_ACK:
			if (usb_pipein(pipe) && len) {
				mb9bfxxx_read_buf(buffer, len);
				writew(readw(EP1S) & ~0x0400, EP1S);
				udelay(10);
			}
			writew(readw(EP1S) | 0x8000, EP1S);
			return len;
		case HERR_HS_NAK:
			if (status & HERR_TGERR) {
				token ^= TOKEN_TGGE;
				usb_dotoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
			}
			continue;
		case HERR_HS_STALL:
			if (status & HERR_ERR)
				err = 0;
			break;
		}
		err--;
	}

	PDEBUG(0, "usb transfer error %#x\n", (int)status);

	err = 0;

	if (status & (HERR_RERR | HERR_TGERR))
		err |= USB_ST_BUF_ERR;
	if (status & HERR_CRC)
		err |= USB_ST_CRC_ERR;
	if ((status & HERR_HS) == HERR_HS_STALL)
		err |= USB_ST_STALLED;

	return -err;
}

int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		    int len)
{
	int dir_out = usb_pipeout(pipe);
	int ep = usb_pipeendpoint(pipe);
	int max = usb_maxpacket(dev, pipe);
	int done = 0;

	PDEBUG(7, "dev = %ld ep = %ld buf = %p size = %d dir_out = %d\n",
	       usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);

	dev->status = 0;

	writeb(usb_pipedevice(pipe), HADDR);
	while (done < len) {
		int res = mb9bfxxx_send_packet(dev, pipe, (__u8*)buffer+done,
					    max > len - done ? len - done : max);
		if (res < 0) {
			dev->status = -res;
			return res;
		}

		if (!dir_out && res < max) /* short packet */
			break;

		usb_dotoggle(dev, ep, usb_pipeout(pipe));
		done += res;
	}

	dev->act_len = done;

	return 0;
}

int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		       int len,struct devrequest *setup)
{
	int done = 0;
	int devnum = usb_pipedevice(pipe);
	int ep = usb_pipeendpoint(pipe);

	dev->status = 0;

	if (devnum == root_hub_devnum)
		return mb9bfxxx_rh_submit_urb(dev, pipe, buffer, len, setup);

	PDEBUG(7, "dev = %d pipe = %08lx buf = %p size = %d rt = %#x req = %#x\n",
	       devnum, pipe, buffer, len, (int)setup->requesttype,
	       (int)setup->request);

	writeb(devnum, HADDR);
	/* setup phase */
	usb_settoggle(dev, ep, 1, 0);
	if (mb9bfxxx_send_packet(dev, usb_sndctrlpipe(dev, ep),
			      (__u8*)setup, sizeof(*setup)) == sizeof(*setup)) {
		int dir_in = usb_pipein(pipe);
		int max = usb_maxpacket(dev, pipe);

		/* data phase */
		usb_settoggle(dev, ep, usb_pipeout(pipe), 1);
		while (done < len) {
			int res;
			res = mb9bfxxx_send_packet(dev, pipe, (__u8*)buffer+done,
						    max > len - done ? len - done : max);
			if (res < 0) {
				PDEBUG(0, "status data failed!\n");
				dev->status = -res;
				return 0;
			}
			done += res;
			usb_dotoggle(dev, ep, usb_pipeout(pipe));
			if (dir_in && res < max) /* short packet */
				break;
		}

		/* status phase */
		usb_settoggle(dev, ep, !usb_pipeout(pipe), 1);
		if (mb9bfxxx_send_packet(dev,
				      !dir_in ? usb_rcvctrlpipe(dev, ep) :
				      usb_sndctrlpipe(dev, ep),
				      0, 0) < 0) {
			PDEBUG(0, "status phase failed!\n");
			dev->status = -1;
		}
	} else {
		PDEBUG(0, "setup phase failed!\n");
		dev->status = -1;
	}

	dev->act_len = done;

	return done;
}

int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		   int len, int interval)
{
	PDEBUG(0, "dev = %p pipe = %#lx buf = %p size = %d int = %d\n", dev, pipe,
	       buffer, len, interval);
	return -1;
}

/*
 * MB9BFXXX Virtual Root Hub
 */

/* Device descriptor */
static __u8 mb9bfxxx_rh_dev_des[] =
{
	0x12,	    /*	__u8  bLength; */
	0x01,	    /*	__u8  bDescriptorType; Device */
	0x10,	    /*	__u16 bcdUSB; v1.1 */
	0x01,
	0x09,	    /*	__u8  bDeviceClass; HUB_CLASSCODE */
	0x00,	    /*	__u8  bDeviceSubClass; */
	0x00,	    /*	__u8  bDeviceProtocol; */
	0x08,	    /*	__u8  bMaxPacketSize0; 8 Bytes */
	0x00,	    /*	__u16 idVendor; */
	0x00,
	0x00,	    /*	__u16 idProduct; */
	0x00,
	0x00,	    /*	__u16 bcdDevice; */
	0x00,
	0x00,	    /*	__u8  iManufacturer; */
	0x02,	    /*	__u8  iProduct; */
	0x01,	    /*	__u8  iSerialNumber; */
	0x01	    /*	__u8  bNumConfigurations; */
};

/* Configuration descriptor */
static __u8 mb9bfxxx_rh_config_des[] =
{
	0x09,	    /*	__u8  bLength; */
	0x02,	    /*	__u8  bDescriptorType; Configuration */
	0x19,	    /*	__u16 wTotalLength; */
	0x00,
	0x01,	    /*	__u8  bNumInterfaces; */
	0x01,	    /*	__u8  bConfigurationValue; */
	0x00,	    /*	__u8  iConfiguration; */
	0x40,	    /*	__u8  bmAttributes;
		    Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup,
		    4..0: resvd */
	0x00,	    /*	__u8  MaxPower; */

	/* interface */
	0x09,	    /*	__u8  if_bLength; */
	0x04,	    /*	__u8  if_bDescriptorType; Interface */
	0x00,	    /*	__u8  if_bInterfaceNumber; */
	0x00,	    /*	__u8  if_bAlternateSetting; */
	0x01,	    /*	__u8  if_bNumEndpoints; */
	0x09,	    /*	__u8  if_bInterfaceClass; HUB_CLASSCODE */
	0x00,	    /*	__u8  if_bInterfaceSubClass; */
	0x00,	    /*	__u8  if_bInterfaceProtocol; */
	0x00,	    /*	__u8  if_iInterface; */

	/* endpoint */
	0x07,	    /*	__u8  ep_bLength; */
	0x05,	    /*	__u8  ep_bDescriptorType; Endpoint */
	0x81,	    /*	__u8  ep_bEndpointAddress; IN Endpoint 1 */
	0x03,	    /*	__u8  ep_bmAttributes; Interrupt */
	0x08,	    /*	__u16 ep_wMaxPacketSize; */
	0x00,
	0xff	    /*	__u8  ep_bInterval; 255 ms */
};

/* root hub class descriptor*/
static __u8 mb9bfxxx_rh_hub_des[] =
{
	0x09,			/*  __u8  bLength; */
	0x29,			/*  __u8  bDescriptorType; Hub-descriptor */
	0x01,			/*  __u8  bNbrPorts; */
	0x00,			/* __u16  wHubCharacteristics; */
	0x00,
	0x50,			/*  __u8  bPwrOn2pwrGood; 2ms */
	0x00,			/*  __u8  bHubContrCurrent; 0 mA */
	0xfc,			/*  __u8  DeviceRemovable; *** 7 Ports max *** */
	0xff			/*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
};

/*
 * helper routine for returning string descriptors in UTF-16LE
 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
 */
static int ascii2utf (char *s, u8 *utf, int utfmax)
{
	int retval;

	for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
		*utf++ = *s++;
		*utf++ = 0;
	}
	return retval;
}

/*
 * root_hub_string is used by each host controller's root hub code,
 * so that they're identified consistently throughout the system.
 */
static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
{
	char buf [30];

	/* assert (len > (2 * (sizeof (buf) + 1)));
	   assert (strlen (type) <= 8);*/

	/* language ids */
	if (id == 0) {
		*data++ = 4; *data++ = 3;	/* 4 bytes data */
		*data++ = 0; *data++ = 0;	/* some language id */
		return 4;

	/* serial number */
	} else if (id == 1) {
		sprintf (buf, "%#x", serial);

	/* product description */
	} else if (id == 2) {
		sprintf (buf, "USB %s Root Hub", type);

	/* id 3 == vendor description */

	/* unsupported IDs --> "stall" */
	} else
	    return 0;

	ascii2utf (buf, data + 2, len - 2);
	data [0] = 2 + strlen(buf) * 2;
	data [1] = 3;
	return data [0];
}

/* helper macro */
#define OK(x)	len = (x); break

/*
 * This function handles all USB request to the the virtual root hub
 */
static int mb9bfxxx_rh_submit_urb(struct usb_device *usb_dev, unsigned long pipe,
			       void *data, int buf_len, struct devrequest *cmd)
{
	__u8 data_buf[16];
	__u8 *bufp = data_buf;
	int len = 0;
	int status = 0;
	__u16 bmRType_bReq;
	__u16 wValue  = le16_to_cpu (cmd->value);
	__u16 wLength = le16_to_cpu (cmd->length);
#ifdef MB9BFXXX_DEBUG
	__u16 wIndex  = le16_to_cpu (cmd->index);
#endif

	if (usb_pipeint(pipe)) {
		PDEBUG(0, "interrupt transfer unimplemented!\n");
		return 0;
	}

	bmRType_bReq  = cmd->requesttype | (cmd->request << 8);

	PDEBUG(5, "submit rh urb, req = %d(%x) val = %#x index = %#x len=%d\n",
	       bmRType_bReq, bmRType_bReq, wValue, wIndex, wLength);

	/* Request Destination:
		   without flags: Device,
		   USB_RECIP_INTERFACE: interface,
		   USB_RECIP_ENDPOINT: endpoint,
		   USB_TYPE_CLASS means HUB here,
		   USB_RECIP_OTHER | USB_TYPE_CLASS  almost ever means HUB_PORT here
	*/
	switch (bmRType_bReq) {
	case RH_GET_STATUS:
		*(__u16 *)bufp = cpu_to_le16(1);
		OK(2);

	case RH_GET_STATUS | USB_RECIP_INTERFACE:
		*(__u16 *)bufp = cpu_to_le16(0);
		OK(2);

	case RH_GET_STATUS | USB_RECIP_ENDPOINT:
		*(__u16 *)bufp = cpu_to_le16(0);
		OK(2);

	case RH_GET_STATUS | USB_TYPE_CLASS:
		*(__u32 *)bufp = cpu_to_le32(0);
		OK(4);

	case RH_GET_STATUS | USB_RECIP_OTHER | USB_TYPE_CLASS:
		*(__u32 *)bufp = cpu_to_le32(rh_status.wPortChange<<16 | rh_status.wPortStatus);
		OK(4);

	case RH_CLEAR_FEATURE | USB_RECIP_ENDPOINT:
		switch (wValue) {
		case 1:
			OK(0);
		}
		break;

	case RH_CLEAR_FEATURE | USB_TYPE_CLASS:
		switch (wValue) {
		case C_HUB_LOCAL_POWER:
			OK(0);

		case C_HUB_OVER_CURRENT:
			OK(0);
		}
		break;

	case RH_CLEAR_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			rh_status.wPortStatus &= ~USB_PORT_STAT_ENABLE;
			OK(0);

		case USB_PORT_FEAT_SUSPEND:
			rh_status.wPortStatus &= ~USB_PORT_STAT_SUSPEND;
			OK(0);

		case USB_PORT_FEAT_POWER:
			rh_status.wPortStatus &= ~USB_PORT_STAT_POWER;
			OK(0);

		case USB_PORT_FEAT_C_CONNECTION:
			rh_status.wPortChange &= ~USB_PORT_STAT_C_CONNECTION;
			OK(0);

		case USB_PORT_FEAT_C_ENABLE:
			rh_status.wPortChange &= ~USB_PORT_STAT_C_ENABLE;
			OK(0);

		case USB_PORT_FEAT_C_SUSPEND:
			rh_status.wPortChange &= ~USB_PORT_STAT_C_SUSPEND;
			OK(0);

		case USB_PORT_FEAT_C_OVER_CURRENT:
			rh_status.wPortChange &= ~USB_PORT_STAT_C_OVERCURRENT;
			OK(0);

		case USB_PORT_FEAT_C_RESET:
			rh_status.wPortChange &= ~USB_PORT_STAT_C_RESET;
			OK(0);
		}
		break;

	case RH_SET_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
			rh_status.wPortStatus |= USB_PORT_STAT_SUSPEND;
			OK(0);

		case USB_PORT_FEAT_RESET:
			rh_status.wPortStatus |= USB_PORT_STAT_RESET;
			rh_status.wPortChange = 0;
			rh_status.wPortChange |= USB_PORT_STAT_C_RESET;
			rh_status.wPortStatus &= ~USB_PORT_STAT_RESET;
			rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
			OK(0);

		case USB_PORT_FEAT_POWER:
			rh_status.wPortStatus |= USB_PORT_STAT_POWER;
			OK(0);

		case USB_PORT_FEAT_ENABLE:
			rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
			OK(0);
		}
		break;

	case RH_SET_ADDRESS:
		root_hub_devnum = wValue;
		OK(0);

	case RH_GET_DESCRIPTOR:
		switch ((wValue & 0xff00) >> 8) {
		case USB_DT_DEVICE:
			len = sizeof(mb9bfxxx_rh_dev_des);
			bufp = mb9bfxxx_rh_dev_des;
			OK(len);

		case USB_DT_CONFIG:
			len = sizeof(mb9bfxxx_rh_config_des);
			bufp = mb9bfxxx_rh_config_des;
			OK(len);

		case USB_DT_STRING:
			len = usb_root_hub_string(wValue & 0xff, (int)(long)0,	"MB9BFXXX", data, wLength);
			if (len > 0) {
				bufp = data;
				OK(len);
			}

		default:
			status = -32;
		}
		break;

	case RH_GET_DESCRIPTOR | USB_TYPE_CLASS:
		len = sizeof(mb9bfxxx_rh_hub_des);
		bufp = mb9bfxxx_rh_hub_des;
		OK(len);

	case RH_GET_CONFIGURATION:
		bufp[0] = 0x01;
		OK(1);

	case RH_SET_CONFIGURATION:
		OK(0);

	default:
		PDEBUG(1, "unsupported root hub command\n");
		status = -32;
	}

	len = min(len, buf_len);
	if (data != bufp)
		memcpy(data, bufp, len);

	PDEBUG(5, "len = %d, status = %d\n", len, status);

	usb_dev->status = status;
	usb_dev->act_len = len;

	return status == 0 ? len : status;
}
