xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision 029fd8ea1fffe70bf0d2dab4c64cffa20bf3f62e)
12731b9a8SJean-Christophe PLAGNIOL-VILLARD /*-
22731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2007-2008, Juniper Networks, Inc.
32731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Excito Elektronik i Skåne AB
42731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
52731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
62731b9a8SJean-Christophe PLAGNIOL-VILLARD  * All rights reserved.
72731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
82731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is free software; you can redistribute it and/or
92731b9a8SJean-Christophe PLAGNIOL-VILLARD  * modify it under the terms of the GNU General Public License as
102731b9a8SJean-Christophe PLAGNIOL-VILLARD  * published by the Free Software Foundation version 2 of
112731b9a8SJean-Christophe PLAGNIOL-VILLARD  * the License.
122731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
132731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is distributed in the hope that it will be useful,
142731b9a8SJean-Christophe PLAGNIOL-VILLARD  * but WITHOUT ANY WARRANTY; without even the implied warranty of
152731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162731b9a8SJean-Christophe PLAGNIOL-VILLARD  * GNU General Public License for more details.
172731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
182731b9a8SJean-Christophe PLAGNIOL-VILLARD  * You should have received a copy of the GNU General Public License
192731b9a8SJean-Christophe PLAGNIOL-VILLARD  * along with this program; if not, write to the Free Software
202731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
212731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MA 02111-1307 USA
222731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
232731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
2446b01797SSimon Glass #include <dm.h>
258f62ca64SPatrick Georgi #include <errno.h>
262731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/byteorder.h>
2793ad908cSLucas Stach #include <asm/unaligned.h>
282731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
292731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
302731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <malloc.h>
3167333f76SStefan Roese #include <watchdog.h>
328f62ca64SPatrick Georgi #include <linux/compiler.h>
332731b9a8SJean-Christophe PLAGNIOL-VILLARD 
342731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
352731b9a8SJean-Christophe PLAGNIOL-VILLARD 
36676ae068SLucas Stach #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
37676ae068SLucas Stach #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
38676ae068SLucas Stach #endif
392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
405077f96fSJulius Werner /*
415077f96fSJulius Werner  * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
425077f96fSJulius Werner  * Let's time out after 8 to have a little safety margin on top of that.
435077f96fSJulius Werner  */
445077f96fSJulius Werner #define HCHALT_TIMEOUT (8 * 1000)
455077f96fSJulius Werner 
4646b01797SSimon Glass #ifndef CONFIG_DM_USB
47b959655fSMarek Vasut static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
4846b01797SSimon Glass #endif
4971c5de4fSTom Rini 
5071c5de4fSTom Rini #define ALIGN_END_ADDR(type, ptr, size)			\
5198ae840aSRob Herring 	((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
532731b9a8SJean-Christophe PLAGNIOL-VILLARD static struct descriptor {
542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_hub_descriptor hub;
552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_device_descriptor device;
562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_config_descriptor config;
572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_interface_descriptor interface;
582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_endpoint_descriptor endpoint;
592731b9a8SJean-Christophe PLAGNIOL-VILLARD }  __attribute__ ((packed)) descriptor = {
602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x8,		/* bDescLength */
622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x29,		/* bDescriptorType: hub descriptor */
632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bNrPorts -- runtime modified */
642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* wHubCharacteristics */
655f4b4f2fSVincent Palatin 		10,		/* bPwrOn2PwrGood */
662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bHubCntrCurrent */
672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{},		/* Device removable */
682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{}		/* at most 7 ports! XXX */
692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x12,		/* bLength */
722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDescriptorType: UDESC_DEVICE */
736d313c84SSergei Shtylyov 		cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bDeviceClass: UDCLASS_HUB */
752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bDeviceSubClass: UDSUBCLASS_HUB */
762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDeviceProtocol: UDPROTO_HSHUBSTT */
772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		64,		/* bMaxPacketSize: 64 bytes */
782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idVendor */
792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idProduct */
806d313c84SSergei Shtylyov 		cpu_to_le16(0x0100), /* bcdDevice */
812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* iManufacturer */
822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* iProduct */
832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iSerialNumber */
842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1		/* bNumConfigurations: 1 */
852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,
882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bDescriptorType: UDESC_CONFIG */
892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cpu_to_le16(0x19),
902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumInterface */
912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bConfigurationValue */
922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iConfiguration */
932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x40,		/* bmAttributes: UC_SELF_POWER */
942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* bMaxPower */
952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,		/* bLength */
982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		4,		/* bDescriptorType: UDESC_INTERFACE */
992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceNumber */
1002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bAlternateSetting */
1012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumEndpoints */
1022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bInterfaceClass: UICLASS_HUB */
1032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceSubClass: UISUBCLASS_HUB */
1042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceProtocol: UIPROTO_HSHUBSTT */
1052731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* iInterface */
1062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
1082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x7,		/* bLength */
1092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		5,		/* bDescriptorType: UDESC_ENDPOINT */
1102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x81,		/* bEndpointAddress:
1112731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * UE_DIR_IN | EHCI_INTR_ENDPT
1122731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
1132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		3,		/* bmAttributes: UE_INTERRUPT */
1148f8bd565STom Rix 		8,		/* wMaxPacketSize */
1152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		255		/* bInterval */
1162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1172731b9a8SJean-Christophe PLAGNIOL-VILLARD };
1182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1192731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_IS_TDI)
1202731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(1)
1212731b9a8SJean-Christophe PLAGNIOL-VILLARD #else
1222731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(0)
1232731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
1242731b9a8SJean-Christophe PLAGNIOL-VILLARD 
12524ed894fSSimon Glass static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
12624ed894fSSimon Glass {
12746b01797SSimon Glass #ifdef CONFIG_DM_USB
12825c8ebdfSHans de Goede 	return dev_get_priv(usb_get_bus(udev->dev));
12946b01797SSimon Glass #else
13024ed894fSSimon Glass 	return udev->controller;
13146b01797SSimon Glass #endif
13224ed894fSSimon Glass }
13324ed894fSSimon Glass 
134deb8508cSSimon Glass static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
135b068deb3SJim Lin {
136b068deb3SJim Lin 	return PORTSC_PSPD(reg);
137b068deb3SJim Lin }
138b068deb3SJim Lin 
139deb8508cSSimon Glass static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
140b068deb3SJim Lin {
141b068deb3SJim Lin 	uint32_t tmp;
142b068deb3SJim Lin 	uint32_t *reg_ptr;
143b068deb3SJim Lin 
14411d18a19SSimon Glass 	reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
145b068deb3SJim Lin 	tmp = ehci_readl(reg_ptr);
146b068deb3SJim Lin 	tmp |= USBMODE_CM_HC;
147b068deb3SJim Lin #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
148b068deb3SJim Lin 	tmp |= USBMODE_BE;
149b068deb3SJim Lin #endif
150b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
151b068deb3SJim Lin }
152b068deb3SJim Lin 
153deb8508cSSimon Glass static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
154727fce36SSimon Glass 			       uint32_t *reg)
1553874b6d6SMarek Vasut {
1563874b6d6SMarek Vasut 	mdelay(50);
1573874b6d6SMarek Vasut }
1583874b6d6SMarek Vasut 
159deb8508cSSimon Glass static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
160aac064f7SSimon Glass {
161aac064f7SSimon Glass 	if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
162aac064f7SSimon Glass 		/* Printing the message would cause a scan failure! */
163aac064f7SSimon Glass 		debug("The request port(%u) is not configured\n", port);
164aac064f7SSimon Glass 		return NULL;
165aac064f7SSimon Glass 	}
166aac064f7SSimon Glass 
1676a1a8162SSimon Glass 	return (uint32_t *)&ctrl->hcor->or_portsc[port];
168aac064f7SSimon Glass }
169aac064f7SSimon Glass 
1702731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1712731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
17509c83a45SWolfgang Denk 		udelay(5);
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1802731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1842731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
186aeca43e3SSimon Glass static int ehci_reset(struct ehci_ctrl *ctrl)
1872731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1902731b9a8SJean-Christophe PLAGNIOL-VILLARD 
191aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
192273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
193aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
194aeca43e3SSimon Glass 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
195676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2002731b9a8SJean-Christophe PLAGNIOL-VILLARD 
201b068deb3SJim Lin 	if (ehci_is_TDI())
202aeca43e3SSimon Glass 		ctrl->ops.set_usb_mode(ctrl);
2039ab4ce22SSimon Glass 
2049ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
205aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
20614eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
2079ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
208aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
2099ab4ce22SSimon Glass #endif
2102731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
2112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
2122731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2132731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2145077f96fSJulius Werner static int ehci_shutdown(struct ehci_ctrl *ctrl)
2155077f96fSJulius Werner {
2165077f96fSJulius Werner 	int i, ret = 0;
2175077f96fSJulius Werner 	uint32_t cmd, reg;
2185077f96fSJulius Werner 
2191e1be6d4SMarek Vasut 	if (!ctrl || !ctrl->hcor)
2201e1be6d4SMarek Vasut 		return -EINVAL;
2211e1be6d4SMarek Vasut 
2225077f96fSJulius Werner 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
2235077f96fSJulius Werner 	cmd &= ~(CMD_PSE | CMD_ASE);
2245077f96fSJulius Werner 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2255077f96fSJulius Werner 	ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
2265077f96fSJulius Werner 		100 * 1000);
2275077f96fSJulius Werner 
2285077f96fSJulius Werner 	if (!ret) {
2295077f96fSJulius Werner 		for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
2305077f96fSJulius Werner 			reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
2315077f96fSJulius Werner 			reg |= EHCI_PS_SUSP;
2325077f96fSJulius Werner 			ehci_writel(&ctrl->hcor->or_portsc[i], reg);
2335077f96fSJulius Werner 		}
2345077f96fSJulius Werner 
2355077f96fSJulius Werner 		cmd &= ~CMD_RUN;
2365077f96fSJulius Werner 		ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2375077f96fSJulius Werner 		ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
2385077f96fSJulius Werner 			HCHALT_TIMEOUT);
2395077f96fSJulius Werner 	}
2405077f96fSJulius Werner 
2415077f96fSJulius Werner 	if (ret)
2425077f96fSJulius Werner 		puts("EHCI failed to shut down host controller.\n");
2435077f96fSJulius Werner 
2445077f96fSJulius Werner 	return ret;
2455077f96fSJulius Werner }
2465077f96fSJulius Werner 
2472731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2482731b9a8SJean-Christophe PLAGNIOL-VILLARD {
249b8adb120SMarek Vasut 	uint32_t delta, next;
25098ae840aSRob Herring 	uint32_t addr = (unsigned long)buf;
2512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
253189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
254b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
255b8adb120SMarek Vasut 
256189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
257189a6956SIlya Yanok 
2582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
259cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
2602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		td->qt_buffer[idx] = cpu_to_hc32(addr);
2613ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
26214eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
271cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
27298ae840aSRob Herring 		printf("out of buffer pointers (%zu bytes left)\n", sz);
2732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2772731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
279c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
280c60795f4SIlya Yanok {
281c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
282c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
283c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
284c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
285c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
286c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
287c60795f4SIlya Yanok 		return QH_LOW_SPEED;
288c60795f4SIlya Yanok 	return QH_FULL_SPEED;
289c60795f4SIlya Yanok }
290c60795f4SIlya Yanok 
29146b01797SSimon Glass static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
2924e2c4ad3SHans de Goede 					  struct QH *qh)
2934e2c4ad3SHans de Goede {
2944e2c4ad3SHans de Goede 	struct usb_device *ttdev;
29546b01797SSimon Glass 	int parent_devnum;
2964e2c4ad3SHans de Goede 
29746b01797SSimon Glass 	if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
2984e2c4ad3SHans de Goede 		return;
2994e2c4ad3SHans de Goede 
3004e2c4ad3SHans de Goede 	/*
3014e2c4ad3SHans de Goede 	 * For full / low speed devices we need to get the devnum and portnr of
3024e2c4ad3SHans de Goede 	 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
3034e2c4ad3SHans de Goede 	 * in the tree before that one!
3044e2c4ad3SHans de Goede 	 */
30546b01797SSimon Glass #ifdef CONFIG_DM_USB
306fcdd8aaaSHans de Goede 	/*
307fcdd8aaaSHans de Goede 	 * When called from usb-uclass.c: usb_scan_device() udev->dev points
308fcdd8aaaSHans de Goede 	 * to the parent udevice, not the actual udevice belonging to the
309fcdd8aaaSHans de Goede 	 * udev as the device is not instantiated yet. So when searching
310fcdd8aaaSHans de Goede 	 * for the first usb-2 parent start with udev->dev not
311fcdd8aaaSHans de Goede 	 * udev->dev->parent .
312fcdd8aaaSHans de Goede 	 */
31346b01797SSimon Glass 	struct udevice *parent;
314fcdd8aaaSHans de Goede 	struct usb_device *uparent;
31546b01797SSimon Glass 
316fcdd8aaaSHans de Goede 	ttdev = udev;
317fcdd8aaaSHans de Goede 	parent = udev->dev;
318fcdd8aaaSHans de Goede 	uparent = dev_get_parentdata(parent);
31946b01797SSimon Glass 
320fcdd8aaaSHans de Goede 	while (uparent->speed != USB_SPEED_HIGH) {
321fcdd8aaaSHans de Goede 		struct udevice *dev = parent;
322fcdd8aaaSHans de Goede 
323fcdd8aaaSHans de Goede 		if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) {
324fcdd8aaaSHans de Goede 			printf("ehci: Error cannot find high speed parent of usb-1 device\n");
32546b01797SSimon Glass 			return;
32646b01797SSimon Glass 		}
327fcdd8aaaSHans de Goede 
328fcdd8aaaSHans de Goede 		ttdev = dev_get_parentdata(dev);
329fcdd8aaaSHans de Goede 		parent = dev->parent;
330fcdd8aaaSHans de Goede 		uparent = dev_get_parentdata(parent);
331fcdd8aaaSHans de Goede 	}
332fcdd8aaaSHans de Goede 	parent_devnum = uparent->devnum;
33346b01797SSimon Glass #else
33446b01797SSimon Glass 	ttdev = udev;
3354e2c4ad3SHans de Goede 	while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
3364e2c4ad3SHans de Goede 		ttdev = ttdev->parent;
3374e2c4ad3SHans de Goede 	if (!ttdev->parent)
3384e2c4ad3SHans de Goede 		return;
33946b01797SSimon Glass 	parent_devnum = ttdev->parent->devnum;
34046b01797SSimon Glass #endif
3414e2c4ad3SHans de Goede 
3424e2c4ad3SHans de Goede 	qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
34346b01797SSimon Glass 				     QH_ENDPT2_HUBADDR(parent_devnum));
3444e2c4ad3SHans de Goede }
3454e2c4ad3SHans de Goede 
3462731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
3472731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
3482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
3492731b9a8SJean-Christophe PLAGNIOL-VILLARD {
35071c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
3515cec214eSBenoît Thébaudeau 	struct qTD *qtd;
3525cec214eSBenoît Thébaudeau 	int qtd_count = 0;
353de98e8b2SMarek Vasut 	int qtd_counter = 0;
3542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
3552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
3562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
357db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
3582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
3592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
36096820a35SSimon Glass 	int timeout;
3612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
36224ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
3632731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
3652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
3662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
3672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
3682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
3692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
3702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
3712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
3722731b9a8SJean-Christophe PLAGNIOL-VILLARD 
373db191346SBenoît Thébaudeau #define PKT_ALIGN	512
3745cec214eSBenoît Thébaudeau 	/*
3755cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
3765cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
3775cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
3785cec214eSBenoît Thébaudeau 	 *
3795cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
3805cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
3815cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
3825cec214eSBenoît Thébaudeau 	 *
3835cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
3845cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
3855cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
3865cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
3875cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
3885cec214eSBenoît Thébaudeau 	 */
3895cec214eSBenoît Thébaudeau 	if (req != NULL)
3905cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
3915cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
3925cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
3935cec214eSBenoît Thébaudeau 		/*
3945cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
395db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
396db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
397db191346SBenoît Thébaudeau 		 * shorter).
3985cec214eSBenoît Thébaudeau 		 *
3995cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
400db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
401db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
402db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
4035cec214eSBenoît Thébaudeau 		 *
404db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
4055cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
4065cec214eSBenoît Thébaudeau 		 */
4075cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
4085cec214eSBenoît Thébaudeau 		/*
409db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
410db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
4115cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
4125cec214eSBenoît Thébaudeau 		 */
41398ae840aSRob Herring 		if ((unsigned long)buffer & (PKT_ALIGN - 1))
4145cec214eSBenoît Thébaudeau 			xfr_sz--;
4155cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
4165cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
4175cec214eSBenoît Thébaudeau 		/*
418db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
419db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
420db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
421db191346SBenoît Thébaudeau 		 * bytes.
4225cec214eSBenoît Thébaudeau 		 */
423db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
4245cec214eSBenoît Thébaudeau 	}
4255cec214eSBenoît Thébaudeau /*
426db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
427db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
4285cec214eSBenoît Thébaudeau  */
429db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
4305cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
4315cec214eSBenoît Thébaudeau #endif
4325cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
4335cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
4345cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
4355cec214eSBenoît Thébaudeau 		return -1;
4365cec214eSBenoît Thébaudeau 	}
4375cec214eSBenoît Thébaudeau 
43871c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
4395cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
440de98e8b2SMarek Vasut 
441b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
442b8adb120SMarek Vasut 
44341b1f0acSMarek Vasut 	/*
44441b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
44541b1f0acSMarek Vasut 	 *
44641b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
44741b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
44841b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
44941b1f0acSMarek Vasut 	 * - qh_curtd
45041b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
45141b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
45241b1f0acSMarek Vasut 	 */
45398ae840aSRob Herring 	qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
454c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
455db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
45614eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
457db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
45814eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
459c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
46014eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
46114eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
46271c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
4634e2c4ad3SHans de Goede 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
46471c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
4654e2c4ad3SHans de Goede 	ehci_update_endpt2_dev_n_port(dev, qh);
46671c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
4672456b97fSStephen Warren 	qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
4682731b9a8SJean-Christophe PLAGNIOL-VILLARD 
46971c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
4702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
47241b1f0acSMarek Vasut 		/*
47341b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
47441b1f0acSMarek Vasut 		 *
47541b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
47641b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
47741b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
47841b1f0acSMarek Vasut 		 *
47941b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
48041b1f0acSMarek Vasut 		 */
481de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
482de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
48314eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
48414eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
48514eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
48614eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
487de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
48814eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
48914eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
4902731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
4912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
49241b1f0acSMarek Vasut 		/* Update previous qTD! */
49398ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
494de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
4962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4982731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
4995cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
5005cec214eSBenoît Thébaudeau 		int left_length = length;
5015cec214eSBenoît Thébaudeau 
5025cec214eSBenoît Thébaudeau 		do {
5035cec214eSBenoît Thébaudeau 			/*
5045cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
5055cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
5065cec214eSBenoît Thébaudeau 			 */
5075cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
5085cec214eSBenoît Thébaudeau 			/*
5095cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
5105cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
5115cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
5125cec214eSBenoît Thébaudeau 			 */
51398ae840aSRob Herring 			xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
5145cec214eSBenoît Thébaudeau 			/*
5155cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
516db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
5175cec214eSBenoît Thébaudeau 			 */
518db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
5195cec214eSBenoît Thébaudeau 			/*
5205cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
5215cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
5225cec214eSBenoît Thébaudeau 			 */
5235cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
5245cec214eSBenoît Thébaudeau 
52541b1f0acSMarek Vasut 			/*
52641b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
52741b1f0acSMarek Vasut 			 *
52841b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
52941b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
53041b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
53141b1f0acSMarek Vasut 			 *
53241b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
53341b1f0acSMarek Vasut 			 */
5345cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
5355cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5365cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
5375cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5385cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
5395cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
54014eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
5415cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
5425cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
54314eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
54414eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
545de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
5465cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
5475cec214eSBenoît Thébaudeau 						xfr_bytes)) {
54814eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
5492731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
5502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
55141b1f0acSMarek Vasut 			/* Update previous qTD! */
55298ae840aSRob Herring 			*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
553de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
554db191346SBenoît Thébaudeau 			/*
555db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
556db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
557db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
558db191346SBenoît Thébaudeau 			 */
559db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
560db191346SBenoît Thébaudeau 				toggle ^= 1;
5615cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
5625cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
5635cec214eSBenoît Thébaudeau 		} while (left_length > 0);
5642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5652731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
56741b1f0acSMarek Vasut 		/*
56841b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
56941b1f0acSMarek Vasut 		 *
57041b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
57141b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
57241b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
57341b1f0acSMarek Vasut 		 */
574de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
575de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
576db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
57714eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
57814eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
57914eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
58014eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
581de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
58241b1f0acSMarek Vasut 		/* Update previous qTD! */
58398ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
584de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
5852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5862731b9a8SJean-Christophe PLAGNIOL-VILLARD 
58798ae840aSRob Herring 	ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
5882731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
59098ae840aSRob Herring 	flush_dcache_range((unsigned long)&ctrl->qh_list,
591676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
59298ae840aSRob Herring 	flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
59398ae840aSRob Herring 	flush_dcache_range((unsigned long)qtd,
5945cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
5952731b9a8SJean-Christophe PLAGNIOL-VILLARD 
596c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
59798ae840aSRob Herring 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
598c7701af5SIlya Yanok 
599676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
600676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
6012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
603676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
6042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
605676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
6062731b9a8SJean-Christophe PLAGNIOL-VILLARD 
607676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
6082731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6092731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
61014eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
6112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6132731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
6152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
616de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
61796820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
6182731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
6192731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
62098ae840aSRob Herring 		invalidate_dcache_range((unsigned long)&ctrl->qh_list,
621676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
62298ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qh,
62371c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
62498ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qtd,
6255cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
626b8adb120SMarek Vasut 
6272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
62814eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
6292731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
63067333f76SStefan Roese 		WATCHDOG_RESET();
63196820a35SSimon Glass 	} while (get_timer(ts) < timeout);
63296820a35SSimon Glass 
633189a6956SIlya Yanok 	/*
634189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
635189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
636189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
637189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
638189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
639189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
640189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
641189a6956SIlya Yanok 	 */
64298ae840aSRob Herring 	invalidate_dcache_range((unsigned long)buffer,
64398ae840aSRob Herring 		ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
644b8adb120SMarek Vasut 
64596820a35SSimon Glass 	/* Check that the TD processing happened */
64614eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
64796820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
6482731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
650676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
6512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
652676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
6532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
654676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
6552731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
65714eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
6582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6602731b9a8SJean-Christophe PLAGNIOL-VILLARD 
66171c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
66214eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
6632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
66414eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
66514eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
6662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
66714eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
6682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
6692731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
6702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
6712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
67214eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
6732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
6742731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
67514eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
67614eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
6772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
67914eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
68014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
6812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
6822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6842731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
68514eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
686222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
6872731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
68914eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
6902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
6912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
692e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
6932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
694676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
695676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
696676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
697e82a316dSKuo-Jung Su #endif
6982731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6992731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7005cec214eSBenoît Thébaudeau 	free(qtd);
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
7022731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
7045cec214eSBenoît Thébaudeau 	free(qtd);
7052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
7062731b9a8SJean-Christophe PLAGNIOL-VILLARD }
7072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
70824ed894fSSimon Glass static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
70924ed894fSSimon Glass 			    void *buffer, int length, struct devrequest *req)
7102731b9a8SJean-Christophe PLAGNIOL-VILLARD {
7112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
7132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
7152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
7177d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
71824ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
7192731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7202731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
7212731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7222731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
7232731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
7242731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
7252731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
7262731b9a8SJean-Christophe PLAGNIOL-VILLARD 
72744259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
7282731b9a8SJean-Christophe PLAGNIOL-VILLARD 
72944259bb9SPrafulla Wadaskar 	switch (typeReq) {
7309c6a9d7cSKuo-Jung Su 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7319c6a9d7cSKuo-Jung Su 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
7329c6a9d7cSKuo-Jung Su 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
733deb8508cSSimon Glass 		status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
7341dde1423SKuo-Jung Su 		if (!status_reg)
7359c6a9d7cSKuo-Jung Su 			return -1;
7369c6a9d7cSKuo-Jung Su 		break;
7379c6a9d7cSKuo-Jung Su 	default:
7389c6a9d7cSKuo-Jung Su 		status_reg = NULL;
7399c6a9d7cSKuo-Jung Su 		break;
7409c6a9d7cSKuo-Jung Su 	}
7419c6a9d7cSKuo-Jung Su 
7429c6a9d7cSKuo-Jung Su 	switch (typeReq) {
7432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
7442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
7462731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
74814eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
75314eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
75414eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
75514eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
7662731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
7712731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
7722731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
7762731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
7782731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
7862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
7882731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
7892731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
79014eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
7912731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7932731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7942731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
7982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
799676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
8002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
8022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
8032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
8082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
8102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
8122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
8162731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
8172731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
8182731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
8192731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
8202731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
8212731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
822c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
8232731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
8242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
8252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
828deb8508cSSimon Glass 			switch (ctrl->ops.get_port_speed(ctrl, reg)) {
82914eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
83114eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
8322731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
83414eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
8352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
8362731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8372731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8392731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
8442731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
8452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
8462731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
8472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
8482731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
8497d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
8502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
8512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8532731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
8542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
8582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8602731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
8612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
8622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
864676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
8652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
8662731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8672731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
8702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
8712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
8722731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
8732731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
8742731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
8757d9aa8fdSJulius Werner 				      port - 1);
8762731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
8772731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
87845b9ea1dSHans de Goede 				return -ENXIO;
8792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
880c8b2d1dcSSergei Shtylyov 				int ret;
881c8b2d1dcSSergei Shtylyov 
8822731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
8832731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
8842731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8852731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
8862731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
8872731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
8882731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
8892731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
890deb8508cSSimon Glass 				ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
8913874b6d6SMarek Vasut 
892b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
893c8b2d1dcSSergei Shtylyov 				/*
894c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
895c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
896c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
897c8b2d1dcSSergei Shtylyov 				 */
898c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
899c8b2d1dcSSergei Shtylyov 						2 * 1000);
90071b94526SHans de Goede 				if (!ret) {
90171b94526SHans de Goede 					reg = ehci_readl(status_reg);
90271b94526SHans de Goede 					if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
90371b94526SHans de Goede 					    == EHCI_PS_CS && !ehci_is_TDI()) {
90471b94526SHans de Goede 						debug("port %d full speed --> companion\n", port - 1);
90571b94526SHans de Goede 						reg &= ~EHCI_PS_CLEAR;
90671b94526SHans de Goede 						reg |= EHCI_PS_PO;
90771b94526SHans de Goede 						ehci_writel(status_reg, reg);
90845b9ea1dSHans de Goede 						return -ENXIO;
90971b94526SHans de Goede 					} else {
9107d9aa8fdSJulius Werner 						ctrl->portreset |= 1 << port;
91171b94526SHans de Goede 					}
91271b94526SHans de Goede 				} else {
913c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
9147d9aa8fdSJulius Werner 					       port - 1);
9152731b9a8SJean-Christophe PLAGNIOL-VILLARD 				}
91671b94526SHans de Goede 			}
9172731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9187d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
9195077f96fSJulius Werner 			ehci_shutdown(ctrl);
9207d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
9217d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
9227d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
9237d9aa8fdSJulius Werner 			break;
9242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9282731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
929676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9302731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9312731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
9322731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
933ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
9342731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
9352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
9362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
9372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
939ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
9402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9412731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
942676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
943ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
944ed10e66aSSimon Glass 			break;
9452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
946ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
9472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
949ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
9502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
9527d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
9532731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9552731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
9592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
960676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
9632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
9642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
9652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9675b84dd67SMike Frysinger 	mdelay(1);
968b4141195SMasahiro Yamada 	len = min3(srclen, (int)le16_to_cpu(req->length), length);
9692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
9702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
9712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
9722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
9732731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
9752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
9762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9772731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9782731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
9792731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
9802731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
9812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
9822731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
9842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
9852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
9862731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9872731b9a8SJean-Christophe PLAGNIOL-VILLARD 
988deb8508cSSimon Glass const struct ehci_ops default_ehci_ops = {
989deb8508cSSimon Glass 	.set_usb_mode		= ehci_set_usbmode,
990deb8508cSSimon Glass 	.get_port_speed		= ehci_get_port_speed,
991deb8508cSSimon Glass 	.powerup_fixup		= ehci_powerup_fixup,
992deb8508cSSimon Glass 	.get_portsc_register	= ehci_get_portsc_register,
993deb8508cSSimon Glass };
994deb8508cSSimon Glass 
995deb8508cSSimon Glass static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
996c4a3141dSSimon Glass {
997deb8508cSSimon Glass 	if (!ops) {
998deb8508cSSimon Glass 		ctrl->ops = default_ehci_ops;
999deb8508cSSimon Glass 	} else {
1000deb8508cSSimon Glass 		ctrl->ops = *ops;
1001deb8508cSSimon Glass 		if (!ctrl->ops.set_usb_mode)
1002deb8508cSSimon Glass 			ctrl->ops.set_usb_mode = ehci_set_usbmode;
1003deb8508cSSimon Glass 		if (!ctrl->ops.get_port_speed)
1004deb8508cSSimon Glass 			ctrl->ops.get_port_speed = ehci_get_port_speed;
1005deb8508cSSimon Glass 		if (!ctrl->ops.powerup_fixup)
1006deb8508cSSimon Glass 			ctrl->ops.powerup_fixup = ehci_powerup_fixup;
1007deb8508cSSimon Glass 		if (!ctrl->ops.get_portsc_register)
1008deb8508cSSimon Glass 			ctrl->ops.get_portsc_register =
1009deb8508cSSimon Glass 					ehci_get_portsc_register;
1010deb8508cSSimon Glass 	}
1011deb8508cSSimon Glass }
1012deb8508cSSimon Glass 
101346b01797SSimon Glass #ifndef CONFIG_DM_USB
1014deb8508cSSimon Glass void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
1015deb8508cSSimon Glass {
1016deb8508cSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
1017deb8508cSSimon Glass 
1018deb8508cSSimon Glass 	ctrl->priv = priv;
1019deb8508cSSimon Glass 	ehci_setup_ops(ctrl, ops);
1020c4a3141dSSimon Glass }
1021c4a3141dSSimon Glass 
1022c4a3141dSSimon Glass void *ehci_get_controller_priv(int index)
1023c4a3141dSSimon Glass {
1024c4a3141dSSimon Glass 	return ehcic[index].priv;
1025c4a3141dSSimon Glass }
102646b01797SSimon Glass #endif
1027c4a3141dSSimon Glass 
10287372b5bdSSimon Glass static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
10292731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1030676ae068SLucas Stach 	struct QH *qh_list;
10318f62ca64SPatrick Georgi 	struct QH *periodic;
10327372b5bdSSimon Glass 	uint32_t reg;
10337372b5bdSSimon Glass 	uint32_t cmd;
10348f62ca64SPatrick Georgi 	int i;
10352731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10362982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
10377372b5bdSSimon Glass 	if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
10387372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
10392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10407372b5bdSSimon Glass 	qh_list = &ctrl->qh_list;
1041676ae068SLucas Stach 
10422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
104371c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
104498ae840aSRob Herring 	qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
104514eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
104614eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
104771c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
104871c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
104914eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
105014eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
10512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
105298ae840aSRob Herring 	flush_dcache_range((unsigned long)qh_list,
1053d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, qh_list, 1));
1054d3e07478SStephen Warren 
10558f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
10567372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)qh_list);
10578f62ca64SPatrick Georgi 
10588f62ca64SPatrick Georgi 	/*
10598f62ca64SPatrick Georgi 	 * Set up periodic list
10608f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
10618f62ca64SPatrick Georgi 	 */
10627372b5bdSSimon Glass 	ctrl->periodic_schedules = 0;
10637372b5bdSSimon Glass 	periodic = &ctrl->periodic_queue;
10648f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
10658f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
10668f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
10678f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
10688f62ca64SPatrick Georgi 
106998ae840aSRob Herring 	flush_dcache_range((unsigned long)periodic,
1070d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, periodic, 1));
1071d3e07478SStephen Warren 
10728f62ca64SPatrick Georgi 	/*
10738f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
10748f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
10758f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
10768f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
10778f62ca64SPatrick Georgi 	 *
10788f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
10798f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
10808f62ca64SPatrick Georgi 	 */
10817372b5bdSSimon Glass 	if (ctrl->periodic_list == NULL)
10827372b5bdSSimon Glass 		ctrl->periodic_list = memalign(4096, 1024 * 4);
10838bc36036SNikita Kiryanov 
10847372b5bdSSimon Glass 	if (!ctrl->periodic_list)
10858f62ca64SPatrick Georgi 		return -ENOMEM;
10868f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
10877372b5bdSSimon Glass 		ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
1088ea427775SAdrian Cox 						| QH_LINK_TYPE_QH);
10898f62ca64SPatrick Georgi 	}
10908f62ca64SPatrick Georgi 
10917372b5bdSSimon Glass 	flush_dcache_range((unsigned long)ctrl->periodic_list,
10927372b5bdSSimon Glass 			   ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
1093d3e07478SStephen Warren 					  1024));
1094d3e07478SStephen Warren 
10958f62ca64SPatrick Georgi 	/* Set periodic list base address */
10967372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_periodiclistbase,
10977372b5bdSSimon Glass 		(unsigned long)ctrl->periodic_list);
10988f62ca64SPatrick Georgi 
10997372b5bdSSimon Glass 	reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
11002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
11017a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
11022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
11032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
110493ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
110593ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
11062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
11072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
110893ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
110993ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
11102731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
11127372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
11132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
11142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
11152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
11162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
11172731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
11182731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
11197372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
11202731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11217372b5bdSSimon Glass 	if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
11222731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* take control over the ports */
11237372b5bdSSimon Glass 		cmd = ehci_readl(&ctrl->hcor->or_configflag);
11242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cmd |= FLAG_CF;
11257372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_configflag, cmd);
11267372b5bdSSimon Glass 	}
1127e82a316dSKuo-Jung Su 
11282731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
11297372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
11305b84dd67SMike Frysinger 	mdelay(5);
11317372b5bdSSimon Glass 	reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
11322731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
11332731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11347372b5bdSSimon Glass 	return 0;
11357372b5bdSSimon Glass }
11367372b5bdSSimon Glass 
113746b01797SSimon Glass #ifndef CONFIG_DM_USB
11387372b5bdSSimon Glass int usb_lowlevel_stop(int index)
11397372b5bdSSimon Glass {
11407372b5bdSSimon Glass 	ehci_shutdown(&ehcic[index]);
11417372b5bdSSimon Glass 	return ehci_hcd_stop(index);
11427372b5bdSSimon Glass }
11437372b5bdSSimon Glass 
11447372b5bdSSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
11457372b5bdSSimon Glass {
11467372b5bdSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
11477372b5bdSSimon Glass 	uint tweaks = 0;
11487372b5bdSSimon Glass 	int rc;
11497372b5bdSSimon Glass 
1150deb8508cSSimon Glass 	/**
1151deb8508cSSimon Glass 	 * Set ops to default_ehci_ops, ehci_hcd_init should call
1152deb8508cSSimon Glass 	 * ehci_set_controller_priv to change any of these function pointers.
1153deb8508cSSimon Glass 	 */
1154deb8508cSSimon Glass 	ctrl->ops = default_ehci_ops;
1155deb8508cSSimon Glass 
11567372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11577372b5bdSSimon Glass 	if (rc)
11587372b5bdSSimon Glass 		return rc;
11597372b5bdSSimon Glass 	if (init == USB_INIT_DEVICE)
11607372b5bdSSimon Glass 		goto done;
11617372b5bdSSimon Glass 
11627372b5bdSSimon Glass 	/* EHCI spec section 4.1 */
1163aeca43e3SSimon Glass 	if (ehci_reset(ctrl))
11647372b5bdSSimon Glass 		return -1;
11657372b5bdSSimon Glass 
11667372b5bdSSimon Glass #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
11677372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11687372b5bdSSimon Glass 	if (rc)
11697372b5bdSSimon Glass 		return rc;
11707372b5bdSSimon Glass #endif
11717372b5bdSSimon Glass #ifdef CONFIG_USB_EHCI_FARADAY
11727372b5bdSSimon Glass 	tweaks |= EHCI_TWEAK_NO_INIT_CF;
11737372b5bdSSimon Glass #endif
11747372b5bdSSimon Glass 	rc = ehci_common_init(ctrl, tweaks);
11757372b5bdSSimon Glass 	if (rc)
11767372b5bdSSimon Glass 		return rc;
11777372b5bdSSimon Glass 
11787372b5bdSSimon Glass 	ctrl->rootdev = 0;
1179127efc4fSTroy Kisky done:
1180676ae068SLucas Stach 	*controller = &ehcic[index];
11812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
11822731b9a8SJean-Christophe PLAGNIOL-VILLARD }
118346b01797SSimon Glass #endif
11842731b9a8SJean-Christophe PLAGNIOL-VILLARD 
118524ed894fSSimon Glass static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
118624ed894fSSimon Glass 				 void *buffer, int length)
11872731b9a8SJean-Christophe PLAGNIOL-VILLARD {
11882731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
11902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
11942731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11952731b9a8SJean-Christophe PLAGNIOL-VILLARD 
119624ed894fSSimon Glass static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
119724ed894fSSimon Glass 				    void *buffer, int length,
119824ed894fSSimon Glass 				    struct devrequest *setup)
11992731b9a8SJean-Christophe PLAGNIOL-VILLARD {
120024ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
12012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
12022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
12032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
12042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
12052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
12062731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1207676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1208676ae068SLucas Stach 		if (!ctrl->rootdev)
12092731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
12102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
12112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
12122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
12132731b9a8SJean-Christophe PLAGNIOL-VILLARD }
12142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
12158f62ca64SPatrick Georgi struct int_queue {
12168aa26b8eSHans de Goede 	int elementsize;
12178f62ca64SPatrick Georgi 	struct QH *first;
12188f62ca64SPatrick Georgi 	struct QH *current;
12198f62ca64SPatrick Georgi 	struct QH *last;
12208f62ca64SPatrick Georgi 	struct qTD *tds;
12218f62ca64SPatrick Georgi };
12228f62ca64SPatrick Georgi 
122398ae840aSRob Herring #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
12248f62ca64SPatrick Georgi 
12258f62ca64SPatrick Georgi static int
12268f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
12278f62ca64SPatrick Georgi {
12288f62ca64SPatrick Georgi 	uint32_t cmd;
12298f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12308f62ca64SPatrick Georgi 	int ret;
12318f62ca64SPatrick Georgi 
12328f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12338f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
12348f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12358f62ca64SPatrick Georgi 
12368f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12378f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
12388f62ca64SPatrick Georgi 	if (ret < 0) {
12398f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
12408f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12418f62ca64SPatrick Georgi 	}
12428f62ca64SPatrick Georgi 	udelay(1000);
12438f62ca64SPatrick Georgi 	return 0;
12448f62ca64SPatrick Georgi }
12458f62ca64SPatrick Georgi 
12468f62ca64SPatrick Georgi static int
12478f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
12488f62ca64SPatrick Georgi {
12498f62ca64SPatrick Georgi 	uint32_t cmd;
12508f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12518f62ca64SPatrick Georgi 	int ret;
12528f62ca64SPatrick Georgi 
12538f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12548f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
12558f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12568f62ca64SPatrick Georgi 
12578f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12588f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
12598f62ca64SPatrick Georgi 	if (ret < 0) {
12608f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
12618f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12628f62ca64SPatrick Georgi 	}
12638f62ca64SPatrick Georgi 	return 0;
12648f62ca64SPatrick Georgi }
12658f62ca64SPatrick Georgi 
1266*029fd8eaSHans de Goede static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1267*029fd8eaSHans de Goede 			unsigned long pipe, int queuesize, int elementsize,
1268*029fd8eaSHans de Goede 			void *buffer, int interval)
12698f62ca64SPatrick Georgi {
127024ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
12718f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
12728f62ca64SPatrick Georgi 	int i;
12738f62ca64SPatrick Georgi 
1274bd818d81SHans de Goede 	/*
1275bd818d81SHans de Goede 	 * Interrupt transfers requiring several transactions are not supported
1276bd818d81SHans de Goede 	 * because bInterval is ignored.
1277bd818d81SHans de Goede 	 *
1278bd818d81SHans de Goede 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1279bd818d81SHans de Goede 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1280bd818d81SHans de Goede 	 * specification does not constrain this for interrupt transfers. That
1281bd818d81SHans de Goede 	 * means that ehci_submit_async() would support interrupt transfers
1282bd818d81SHans de Goede 	 * requiring several transactions only as long as the transfer size does
1283bd818d81SHans de Goede 	 * not require more than a single qTD.
1284bd818d81SHans de Goede 	 */
1285bd818d81SHans de Goede 	if (elementsize > usb_maxpacket(dev, pipe)) {
1286bd818d81SHans de Goede 		printf("%s: xfers requiring several transactions are not supported.\n",
1287bd818d81SHans de Goede 		       __func__);
1288bd818d81SHans de Goede 		return NULL;
1289bd818d81SHans de Goede 	}
1290bd818d81SHans de Goede 
12918f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
12928f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
12938f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
12948f62ca64SPatrick Georgi 		return NULL;
12958f62ca64SPatrick Georgi 	}
12968f62ca64SPatrick Georgi 
12978f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
12988f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
12998f62ca64SPatrick Georgi 	 * no matter the alignment
13008f62ca64SPatrick Georgi 	 */
13018f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
13028f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
13038f62ca64SPatrick Georgi 		return NULL;
13048f62ca64SPatrick Georgi 	}
13058f62ca64SPatrick Georgi 
13068f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
13078f62ca64SPatrick Georgi 	if (!result) {
13088f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13098f62ca64SPatrick Georgi 		goto fail1;
13108f62ca64SPatrick Georgi 	}
13118aa26b8eSHans de Goede 	result->elementsize = elementsize;
13128165e34bSStephen Warren 	result->first = memalign(USB_DMA_MINALIGN,
13138165e34bSStephen Warren 				 sizeof(struct QH) * queuesize);
13148f62ca64SPatrick Georgi 	if (!result->first) {
13158f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13168f62ca64SPatrick Georgi 		goto fail2;
13178f62ca64SPatrick Georgi 	}
13188f62ca64SPatrick Georgi 	result->current = result->first;
13198f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
13208165e34bSStephen Warren 	result->tds = memalign(USB_DMA_MINALIGN,
13218165e34bSStephen Warren 			       sizeof(struct qTD) * queuesize);
13228f62ca64SPatrick Georgi 	if (!result->tds) {
13238f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13248f62ca64SPatrick Georgi 		goto fail3;
13258f62ca64SPatrick Georgi 	}
13268f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
13278f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
13288f62ca64SPatrick Georgi 
13298f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
13308f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
13318f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
13328f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
13338f62ca64SPatrick Georgi 
133498ae840aSRob Herring 		qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
13358f62ca64SPatrick Georgi 		if (i == queuesize - 1)
1336ea427775SAdrian Cox 			qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
13378f62ca64SPatrick Georgi 
133898ae840aSRob Herring 		qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
1339ea427775SAdrian Cox 		qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1340ea427775SAdrian Cox 		qh->qh_endpt1 =
1341ea427775SAdrian Cox 			cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
13428f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
13438f62ca64SPatrick Georgi 			(1 << 14) |
13448f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
13458f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1346ea427775SAdrian Cox 			(usb_pipedevice(pipe) << 0));
1347ea427775SAdrian Cox 		qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1348ea427775SAdrian Cox 			(1 << 0)); /* S-mask: microframe 0 */
13498f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
13508f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
13514e2c4ad3SHans de Goede 			/* C-mask: microframes 2-4 */
13524e2c4ad3SHans de Goede 			qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
13538f62ca64SPatrick Georgi 		}
13544e2c4ad3SHans de Goede 		ehci_update_endpt2_dev_n_port(dev, qh);
13558f62ca64SPatrick Georgi 
1356ea427775SAdrian Cox 		td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1357ea427775SAdrian Cox 		td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
13588f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
13598f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
1360ea427775SAdrian Cox 		td->qt_token = cpu_to_hc32((elementsize << 16) |
13618f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1362ea427775SAdrian Cox 			0x80); /* active */
1363ea427775SAdrian Cox 		td->qt_buffer[0] =
136498ae840aSRob Herring 		    cpu_to_hc32((unsigned long)buffer + i * elementsize);
1365ea427775SAdrian Cox 		td->qt_buffer[1] =
1366ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1367ea427775SAdrian Cox 		td->qt_buffer[2] =
1368ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1369ea427775SAdrian Cox 		td->qt_buffer[3] =
1370ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1371ea427775SAdrian Cox 		td->qt_buffer[4] =
1372ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
13738f62ca64SPatrick Georgi 
13748f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
13758f62ca64SPatrick Georgi 	}
13768f62ca64SPatrick Georgi 
137798ae840aSRob Herring 	flush_dcache_range((unsigned long)buffer,
1378d3e07478SStephen Warren 			   ALIGN_END_ADDR(char, buffer,
1379d3e07478SStephen Warren 					  queuesize * elementsize));
138098ae840aSRob Herring 	flush_dcache_range((unsigned long)result->first,
1381d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->first,
1382d3e07478SStephen Warren 					  queuesize));
138398ae840aSRob Herring 	flush_dcache_range((unsigned long)result->tds,
1384d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct qTD, result->tds,
1385d3e07478SStephen Warren 					  queuesize));
1386d3e07478SStephen Warren 
138732f2eac1SHans de Goede 	if (ctrl->periodic_schedules > 0) {
13888f62ca64SPatrick Georgi 		if (disable_periodic(ctrl) < 0) {
13898f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
13908f62ca64SPatrick Georgi 			goto fail3;
13918f62ca64SPatrick Georgi 		}
139232f2eac1SHans de Goede 	}
13938f62ca64SPatrick Georgi 
13948f62ca64SPatrick Georgi 	/* hook up to periodic list */
13958f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
13968f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
139798ae840aSRob Herring 	list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
13988f62ca64SPatrick Georgi 
139998ae840aSRob Herring 	flush_dcache_range((unsigned long)result->last,
1400d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->last, 1));
140198ae840aSRob Herring 	flush_dcache_range((unsigned long)list,
1402d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, list, 1));
1403d3e07478SStephen Warren 
14048f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
14058f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14068f62ca64SPatrick Georgi 		goto fail3;
14078f62ca64SPatrick Georgi 	}
140836b73109SHans de Goede 	ctrl->periodic_schedules++;
14098f62ca64SPatrick Georgi 
14108f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
14118f62ca64SPatrick Georgi 	return result;
14128f62ca64SPatrick Georgi fail3:
14138f62ca64SPatrick Georgi 	if (result->tds)
14148f62ca64SPatrick Georgi 		free(result->tds);
14158f62ca64SPatrick Georgi fail2:
14168f62ca64SPatrick Georgi 	if (result->first)
14178f62ca64SPatrick Georgi 		free(result->first);
14188f62ca64SPatrick Georgi 	if (result)
14198f62ca64SPatrick Georgi 		free(result);
14208f62ca64SPatrick Georgi fail1:
14218f62ca64SPatrick Georgi 	return NULL;
14228f62ca64SPatrick Georgi }
14238f62ca64SPatrick Georgi 
1424*029fd8eaSHans de Goede static void *_ehci_poll_int_queue(struct usb_device *dev,
1425*029fd8eaSHans de Goede 				  struct int_queue *queue)
14268f62ca64SPatrick Georgi {
14278f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
1428415548d8SHans de Goede 	struct qTD *cur_td;
14298f62ca64SPatrick Georgi 
14308f62ca64SPatrick Georgi 	/* depleted queue */
14318f62ca64SPatrick Georgi 	if (cur == NULL) {
14328f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
14338f62ca64SPatrick Georgi 		return NULL;
14348f62ca64SPatrick Georgi 	}
14358f62ca64SPatrick Georgi 	/* still active */
1436415548d8SHans de Goede 	cur_td = &queue->tds[queue->current - queue->first];
143798ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur_td,
1438415548d8SHans de Goede 				ALIGN_END_ADDR(struct qTD, cur_td, 1));
1439415548d8SHans de Goede 	if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1440415548d8SHans de Goede 			QT_TOKEN_STATUS_ACTIVE) {
1441415548d8SHans de Goede 		debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1442415548d8SHans de Goede 		      hc32_to_cpu(cur_td->qt_token));
14438f62ca64SPatrick Georgi 		return NULL;
14448f62ca64SPatrick Georgi 	}
14458f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
14468f62ca64SPatrick Georgi 		queue->current++;
14478f62ca64SPatrick Georgi 	else
14488f62ca64SPatrick Georgi 		queue->current = NULL;
14498aa26b8eSHans de Goede 
145098ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur->buffer,
14518aa26b8eSHans de Goede 				ALIGN_END_ADDR(char, cur->buffer,
14528aa26b8eSHans de Goede 					       queue->elementsize));
14538aa26b8eSHans de Goede 
1454415548d8SHans de Goede 	debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1455415548d8SHans de Goede 	      hc32_to_cpu(cur_td->qt_token), cur, queue->first);
14568f62ca64SPatrick Georgi 	return cur->buffer;
14578f62ca64SPatrick Georgi }
14588f62ca64SPatrick Georgi 
14598f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
1460*029fd8eaSHans de Goede static int _ehci_destroy_int_queue(struct usb_device *dev,
1461*029fd8eaSHans de Goede 				   struct int_queue *queue)
14628f62ca64SPatrick Georgi {
146324ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
14648f62ca64SPatrick Georgi 	int result = -1;
14658f62ca64SPatrick Georgi 	unsigned long timeout;
14668f62ca64SPatrick Georgi 
14678f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
14688f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14698f62ca64SPatrick Georgi 		goto out;
14708f62ca64SPatrick Georgi 	}
147136b73109SHans de Goede 	ctrl->periodic_schedules--;
14728f62ca64SPatrick Georgi 
14738f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
14748f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
1475ea427775SAdrian Cox 	while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
14768f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
14778f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
14788f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
14798f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
148098ae840aSRob Herring 			flush_dcache_range((unsigned long)cur,
1481ea7b30c5SHans de Goede 					   ALIGN_END_ADDR(struct QH, cur, 1));
14828f62ca64SPatrick Georgi 			result = 0;
14838f62ca64SPatrick Georgi 			break;
14848f62ca64SPatrick Georgi 		}
14858f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
14868f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14878f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
14888f62ca64SPatrick Georgi 			result = -1;
14898f62ca64SPatrick Georgi 			goto out;
14908f62ca64SPatrick Georgi 		}
14918f62ca64SPatrick Georgi 	}
14928f62ca64SPatrick Georgi 
149336b73109SHans de Goede 	if (ctrl->periodic_schedules > 0) {
14948f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
14958f62ca64SPatrick Georgi 		if (result < 0)
14968f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
14978f62ca64SPatrick Georgi 	}
14988f62ca64SPatrick Georgi 
14998f62ca64SPatrick Georgi out:
15008f62ca64SPatrick Georgi 	free(queue->tds);
15018f62ca64SPatrick Georgi 	free(queue->first);
15028f62ca64SPatrick Georgi 	free(queue);
15038f62ca64SPatrick Georgi 
15048f62ca64SPatrick Georgi 	return result;
15058f62ca64SPatrick Georgi }
15068f62ca64SPatrick Georgi 
150724ed894fSSimon Glass static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
150824ed894fSSimon Glass 				void *buffer, int length, int interval)
15092731b9a8SJean-Christophe PLAGNIOL-VILLARD {
15108f62ca64SPatrick Georgi 	void *backbuffer;
15118f62ca64SPatrick Georgi 	struct int_queue *queue;
15128f62ca64SPatrick Georgi 	unsigned long timeout;
15138f62ca64SPatrick Georgi 	int result = 0, ret;
15148f62ca64SPatrick Georgi 
15152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
15162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
151744ae0be7SBenoît Thébaudeau 
1518*029fd8eaSHans de Goede 	queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
1519bd818d81SHans de Goede 	if (!queue)
1520bd818d81SHans de Goede 		return -1;
15218f62ca64SPatrick Georgi 
15228f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1523*029fd8eaSHans de Goede 	while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
15248f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
15258f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
15268f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
15278f62ca64SPatrick Georgi 			break;
15288f62ca64SPatrick Georgi 		}
15298f62ca64SPatrick Georgi 
15308f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
153198ae840aSRob Herring 		debug("got wrong buffer back (%p instead of %p)\n",
153298ae840aSRob Herring 		      backbuffer, buffer);
15338f62ca64SPatrick Georgi 		return -EINVAL;
15348f62ca64SPatrick Georgi 	}
15358f62ca64SPatrick Georgi 
1536*029fd8eaSHans de Goede 	ret = _ehci_destroy_int_queue(dev, queue);
15378f62ca64SPatrick Georgi 	if (ret < 0)
15388f62ca64SPatrick Georgi 		return ret;
15398f62ca64SPatrick Georgi 
15408f62ca64SPatrick Georgi 	/* everything worked out fine */
15418f62ca64SPatrick Georgi 	return result;
15422731b9a8SJean-Christophe PLAGNIOL-VILLARD }
154324ed894fSSimon Glass 
154446b01797SSimon Glass #ifndef CONFIG_DM_USB
154524ed894fSSimon Glass int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
154624ed894fSSimon Glass 			    void *buffer, int length)
154724ed894fSSimon Glass {
154824ed894fSSimon Glass 	return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
154924ed894fSSimon Glass }
155024ed894fSSimon Glass 
155124ed894fSSimon Glass int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
155224ed894fSSimon Glass 		   int length, struct devrequest *setup)
155324ed894fSSimon Glass {
155424ed894fSSimon Glass 	return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
155524ed894fSSimon Glass }
155624ed894fSSimon Glass 
155724ed894fSSimon Glass int submit_int_msg(struct usb_device *dev, unsigned long pipe,
155824ed894fSSimon Glass 		   void *buffer, int length, int interval)
155924ed894fSSimon Glass {
156024ed894fSSimon Glass 	return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
156124ed894fSSimon Glass }
1562*029fd8eaSHans de Goede 
1563*029fd8eaSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev,
1564*029fd8eaSHans de Goede 		unsigned long pipe, int queuesize, int elementsize,
1565*029fd8eaSHans de Goede 		void *buffer, int interval)
1566*029fd8eaSHans de Goede {
1567*029fd8eaSHans de Goede 	return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1568*029fd8eaSHans de Goede 				      buffer, interval);
1569*029fd8eaSHans de Goede }
1570*029fd8eaSHans de Goede 
1571*029fd8eaSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1572*029fd8eaSHans de Goede {
1573*029fd8eaSHans de Goede 	return _ehci_poll_int_queue(dev, queue);
1574*029fd8eaSHans de Goede }
1575*029fd8eaSHans de Goede 
1576*029fd8eaSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1577*029fd8eaSHans de Goede {
1578*029fd8eaSHans de Goede 	return _ehci_destroy_int_queue(dev, queue);
1579*029fd8eaSHans de Goede }
158046b01797SSimon Glass #endif
158146b01797SSimon Glass 
158246b01797SSimon Glass #ifdef CONFIG_DM_USB
158346b01797SSimon Glass static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
158446b01797SSimon Glass 				   unsigned long pipe, void *buffer, int length,
158546b01797SSimon Glass 				   struct devrequest *setup)
158646b01797SSimon Glass {
158746b01797SSimon Glass 	debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
158846b01797SSimon Glass 	      dev->name, udev, udev->dev->name, udev->portnr);
158946b01797SSimon Glass 
159046b01797SSimon Glass 	return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
159146b01797SSimon Glass }
159246b01797SSimon Glass 
159346b01797SSimon Glass static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
159446b01797SSimon Glass 				unsigned long pipe, void *buffer, int length)
159546b01797SSimon Glass {
159646b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
159746b01797SSimon Glass 	return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
159846b01797SSimon Glass }
159946b01797SSimon Glass 
160046b01797SSimon Glass static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
160146b01797SSimon Glass 			       unsigned long pipe, void *buffer, int length,
160246b01797SSimon Glass 			       int interval)
160346b01797SSimon Glass {
160446b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
160546b01797SSimon Glass 	return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
160646b01797SSimon Glass }
160746b01797SSimon Glass 
160846b01797SSimon Glass int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
160946b01797SSimon Glass 		  struct ehci_hcor *hcor, const struct ehci_ops *ops,
161046b01797SSimon Glass 		  uint tweaks, enum usb_init_type init)
161146b01797SSimon Glass {
1612cb8a2c14SHans de Goede 	struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
161346b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
161446b01797SSimon Glass 	int ret;
161546b01797SSimon Glass 
161646b01797SSimon Glass 	debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
161746b01797SSimon Glass 	      dev->name, ctrl, hccr, hcor, init);
161846b01797SSimon Glass 
1619cb8a2c14SHans de Goede 	priv->desc_before_addr = true;
1620cb8a2c14SHans de Goede 
162146b01797SSimon Glass 	ehci_setup_ops(ctrl, ops);
162246b01797SSimon Glass 	ctrl->hccr = hccr;
162346b01797SSimon Glass 	ctrl->hcor = hcor;
162446b01797SSimon Glass 	ctrl->priv = ctrl;
162546b01797SSimon Glass 
162646b01797SSimon Glass 	if (init == USB_INIT_DEVICE)
162746b01797SSimon Glass 		goto done;
162846b01797SSimon Glass 	ret = ehci_reset(ctrl);
162946b01797SSimon Glass 	if (ret)
163046b01797SSimon Glass 		goto err;
163146b01797SSimon Glass 
163246b01797SSimon Glass 	ret = ehci_common_init(ctrl, tweaks);
163346b01797SSimon Glass 	if (ret)
163446b01797SSimon Glass 		goto err;
163546b01797SSimon Glass done:
163646b01797SSimon Glass 	return 0;
163746b01797SSimon Glass err:
163846b01797SSimon Glass 	free(ctrl);
163946b01797SSimon Glass 	debug("%s: failed, ret=%d\n", __func__, ret);
164046b01797SSimon Glass 	return ret;
164146b01797SSimon Glass }
164246b01797SSimon Glass 
164346b01797SSimon Glass int ehci_deregister(struct udevice *dev)
164446b01797SSimon Glass {
164546b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
164646b01797SSimon Glass 
164746b01797SSimon Glass 	ehci_shutdown(ctrl);
164846b01797SSimon Glass 
164946b01797SSimon Glass 	return 0;
165046b01797SSimon Glass }
165146b01797SSimon Glass 
165246b01797SSimon Glass struct dm_usb_ops ehci_usb_ops = {
165346b01797SSimon Glass 	.control = ehci_submit_control_msg,
165446b01797SSimon Glass 	.bulk = ehci_submit_bulk_msg,
165546b01797SSimon Glass 	.interrupt = ehci_submit_int_msg,
165646b01797SSimon Glass };
165746b01797SSimon Glass 
165846b01797SSimon Glass #endif
1659