xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision 8aa26b8e48ba63cf0d4fd8e49658b87cc00a9eaf)
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>
248f62ca64SPatrick Georgi #include <errno.h>
252731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/byteorder.h>
2693ad908cSLucas Stach #include <asm/unaligned.h>
272731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
282731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
292731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <malloc.h>
3067333f76SStefan Roese #include <watchdog.h>
318f62ca64SPatrick Georgi #include <linux/compiler.h>
322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
332731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
342731b9a8SJean-Christophe PLAGNIOL-VILLARD 
35676ae068SLucas Stach #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
36676ae068SLucas Stach #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37676ae068SLucas Stach #endif
382731b9a8SJean-Christophe PLAGNIOL-VILLARD 
395077f96fSJulius Werner /*
405077f96fSJulius Werner  * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
415077f96fSJulius Werner  * Let's time out after 8 to have a little safety margin on top of that.
425077f96fSJulius Werner  */
435077f96fSJulius Werner #define HCHALT_TIMEOUT (8 * 1000)
445077f96fSJulius Werner 
45b959655fSMarek Vasut static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
4671c5de4fSTom Rini 
4771c5de4fSTom Rini #define ALIGN_END_ADDR(type, ptr, size)			\
4871c5de4fSTom Rini 	((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
492731b9a8SJean-Christophe PLAGNIOL-VILLARD 
502731b9a8SJean-Christophe PLAGNIOL-VILLARD static struct descriptor {
512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_hub_descriptor hub;
522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_device_descriptor device;
532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_config_descriptor config;
542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_interface_descriptor interface;
552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_endpoint_descriptor endpoint;
562731b9a8SJean-Christophe PLAGNIOL-VILLARD }  __attribute__ ((packed)) descriptor = {
572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x8,		/* bDescLength */
592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x29,		/* bDescriptorType: hub descriptor */
602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bNrPorts -- runtime modified */
612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* wHubCharacteristics */
625f4b4f2fSVincent Palatin 		10,		/* bPwrOn2PwrGood */
632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bHubCntrCurrent */
642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{},		/* Device removable */
652731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{}		/* at most 7 ports! XXX */
662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x12,		/* bLength */
692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDescriptorType: UDESC_DEVICE */
706d313c84SSergei Shtylyov 		cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bDeviceClass: UDCLASS_HUB */
722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bDeviceSubClass: UDSUBCLASS_HUB */
732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDeviceProtocol: UDPROTO_HSHUBSTT */
742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		64,		/* bMaxPacketSize: 64 bytes */
752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idVendor */
762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idProduct */
776d313c84SSergei Shtylyov 		cpu_to_le16(0x0100), /* bcdDevice */
782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* iManufacturer */
792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* iProduct */
802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iSerialNumber */
812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1		/* bNumConfigurations: 1 */
822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,
852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bDescriptorType: UDESC_CONFIG */
862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cpu_to_le16(0x19),
872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumInterface */
882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bConfigurationValue */
892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iConfiguration */
902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x40,		/* bmAttributes: UC_SELF_POWER */
912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* bMaxPower */
922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,		/* bLength */
952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		4,		/* bDescriptorType: UDESC_INTERFACE */
962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceNumber */
972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bAlternateSetting */
982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumEndpoints */
992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bInterfaceClass: UICLASS_HUB */
1002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceSubClass: UISUBCLASS_HUB */
1012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceProtocol: UIPROTO_HSHUBSTT */
1022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* iInterface */
1032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
1052731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x7,		/* bLength */
1062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		5,		/* bDescriptorType: UDESC_ENDPOINT */
1072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x81,		/* bEndpointAddress:
1082731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * UE_DIR_IN | EHCI_INTR_ENDPT
1092731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
1102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		3,		/* bmAttributes: UE_INTERRUPT */
1118f8bd565STom Rix 		8,		/* wMaxPacketSize */
1122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		255		/* bInterval */
1132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1142731b9a8SJean-Christophe PLAGNIOL-VILLARD };
1152731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1162731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_IS_TDI)
1172731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(1)
1182731b9a8SJean-Christophe PLAGNIOL-VILLARD #else
1192731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(0)
1202731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
1212731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1223dd80aaeSJeroen Hofstee __weak int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
123b068deb3SJim Lin {
124b068deb3SJim Lin 	return PORTSC_PSPD(reg);
125b068deb3SJim Lin }
126b068deb3SJim Lin 
1273dd80aaeSJeroen Hofstee __weak void ehci_set_usbmode(int index)
128b068deb3SJim Lin {
129b068deb3SJim Lin 	uint32_t tmp;
130b068deb3SJim Lin 	uint32_t *reg_ptr;
131b068deb3SJim Lin 
132b068deb3SJim Lin 	reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
133b068deb3SJim Lin 	tmp = ehci_readl(reg_ptr);
134b068deb3SJim Lin 	tmp |= USBMODE_CM_HC;
135b068deb3SJim Lin #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
136b068deb3SJim Lin 	tmp |= USBMODE_BE;
137b068deb3SJim Lin #endif
138b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
139b068deb3SJim Lin }
140b068deb3SJim Lin 
1413dd80aaeSJeroen Hofstee __weak void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
1423874b6d6SMarek Vasut {
1433874b6d6SMarek Vasut 	mdelay(50);
1443874b6d6SMarek Vasut }
1453874b6d6SMarek Vasut 
1462731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1472731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1502731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
15109c83a45SWolfgang Denk 		udelay(5);
1522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1532731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1552731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1602731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
162676ae068SLucas Stach static int ehci_reset(int index)
1632731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
167676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
168273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
169676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
170676ae068SLucas Stach 	ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
171676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 
177b068deb3SJim Lin 	if (ehci_is_TDI())
178b068deb3SJim Lin 		ehci_set_usbmode(index);
1799ab4ce22SSimon Glass 
1809ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
181676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
18214eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
1839ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
184676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
1859ab4ce22SSimon Glass #endif
1862731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
1872731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
1882731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1905077f96fSJulius Werner static int ehci_shutdown(struct ehci_ctrl *ctrl)
1915077f96fSJulius Werner {
1925077f96fSJulius Werner 	int i, ret = 0;
1935077f96fSJulius Werner 	uint32_t cmd, reg;
1945077f96fSJulius Werner 
1951e1be6d4SMarek Vasut 	if (!ctrl || !ctrl->hcor)
1961e1be6d4SMarek Vasut 		return -EINVAL;
1971e1be6d4SMarek Vasut 
1985077f96fSJulius Werner 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
1995077f96fSJulius Werner 	cmd &= ~(CMD_PSE | CMD_ASE);
2005077f96fSJulius Werner 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2015077f96fSJulius Werner 	ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
2025077f96fSJulius Werner 		100 * 1000);
2035077f96fSJulius Werner 
2045077f96fSJulius Werner 	if (!ret) {
2055077f96fSJulius Werner 		for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
2065077f96fSJulius Werner 			reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
2075077f96fSJulius Werner 			reg |= EHCI_PS_SUSP;
2085077f96fSJulius Werner 			ehci_writel(&ctrl->hcor->or_portsc[i], reg);
2095077f96fSJulius Werner 		}
2105077f96fSJulius Werner 
2115077f96fSJulius Werner 		cmd &= ~CMD_RUN;
2125077f96fSJulius Werner 		ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2135077f96fSJulius Werner 		ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
2145077f96fSJulius Werner 			HCHALT_TIMEOUT);
2155077f96fSJulius Werner 	}
2165077f96fSJulius Werner 
2175077f96fSJulius Werner 	if (ret)
2185077f96fSJulius Werner 		puts("EHCI failed to shut down host controller.\n");
2195077f96fSJulius Werner 
2205077f96fSJulius Werner 	return ret;
2215077f96fSJulius Werner }
2225077f96fSJulius Werner 
2232731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2242731b9a8SJean-Christophe PLAGNIOL-VILLARD {
225b8adb120SMarek Vasut 	uint32_t delta, next;
226b8adb120SMarek Vasut 	uint32_t addr = (uint32_t)buf;
2272731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2282731b9a8SJean-Christophe PLAGNIOL-VILLARD 
229189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
230b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
231b8adb120SMarek Vasut 
232189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
233189a6956SIlya Yanok 
2342731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
235cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
2362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		td->qt_buffer[idx] = cpu_to_hc32(addr);
2373ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
23814eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2392731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2412731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2462731b9a8SJean-Christophe PLAGNIOL-VILLARD 
247cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
2482af16f85SIlya Yanok 		printf("out of buffer pointers (%u bytes left)\n", sz);
2492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2532731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2542731b9a8SJean-Christophe PLAGNIOL-VILLARD 
255c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
256c60795f4SIlya Yanok {
257c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
258c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
259c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
260c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
261c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
262c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
263c60795f4SIlya Yanok 		return QH_LOW_SPEED;
264c60795f4SIlya Yanok 	return QH_FULL_SPEED;
265c60795f4SIlya Yanok }
266c60795f4SIlya Yanok 
2674e2c4ad3SHans de Goede static void ehci_update_endpt2_dev_n_port(struct usb_device *dev,
2684e2c4ad3SHans de Goede 					  struct QH *qh)
2694e2c4ad3SHans de Goede {
2704e2c4ad3SHans de Goede 	struct usb_device *ttdev;
2714e2c4ad3SHans de Goede 
2724e2c4ad3SHans de Goede 	if (dev->speed != USB_SPEED_LOW && dev->speed != USB_SPEED_FULL)
2734e2c4ad3SHans de Goede 		return;
2744e2c4ad3SHans de Goede 
2754e2c4ad3SHans de Goede 	/*
2764e2c4ad3SHans de Goede 	 * For full / low speed devices we need to get the devnum and portnr of
2774e2c4ad3SHans de Goede 	 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
2784e2c4ad3SHans de Goede 	 * in the tree before that one!
2794e2c4ad3SHans de Goede 	 */
2804e2c4ad3SHans de Goede 	ttdev = dev;
2814e2c4ad3SHans de Goede 	while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
2824e2c4ad3SHans de Goede 		ttdev = ttdev->parent;
2834e2c4ad3SHans de Goede 	if (!ttdev->parent)
2844e2c4ad3SHans de Goede 		return;
2854e2c4ad3SHans de Goede 
2864e2c4ad3SHans de Goede 	qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
2874e2c4ad3SHans de Goede 				     QH_ENDPT2_HUBADDR(ttdev->parent->devnum));
2884e2c4ad3SHans de Goede }
2894e2c4ad3SHans de Goede 
2902731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
2912731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
2922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
2932731b9a8SJean-Christophe PLAGNIOL-VILLARD {
29471c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
2955cec214eSBenoît Thébaudeau 	struct qTD *qtd;
2965cec214eSBenoît Thébaudeau 	int qtd_count = 0;
297de98e8b2SMarek Vasut 	int qtd_counter = 0;
2982731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
2992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
3002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
301db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
3022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
3032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
30496820a35SSimon Glass 	int timeout;
3052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
306676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
3072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
3092731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
3102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
3112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
3122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
3132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
3142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
3152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
3162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
317db191346SBenoît Thébaudeau #define PKT_ALIGN	512
3185cec214eSBenoît Thébaudeau 	/*
3195cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
3205cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
3215cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
3225cec214eSBenoît Thébaudeau 	 *
3235cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
3245cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
3255cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
3265cec214eSBenoît Thébaudeau 	 *
3275cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
3285cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
3295cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
3305cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
3315cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
3325cec214eSBenoît Thébaudeau 	 */
3335cec214eSBenoît Thébaudeau 	if (req != NULL)
3345cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
3355cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
3365cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
3375cec214eSBenoît Thébaudeau 		/*
3385cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
339db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
340db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
341db191346SBenoît Thébaudeau 		 * shorter).
3425cec214eSBenoît Thébaudeau 		 *
3435cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
344db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
345db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
346db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
3475cec214eSBenoît Thébaudeau 		 *
348db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
3495cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
3505cec214eSBenoît Thébaudeau 		 */
3515cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
3525cec214eSBenoît Thébaudeau 		/*
353db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
354db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
3555cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
3565cec214eSBenoît Thébaudeau 		 */
357db191346SBenoît Thébaudeau 		if ((uint32_t)buffer & (PKT_ALIGN - 1))
3585cec214eSBenoît Thébaudeau 			xfr_sz--;
3595cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
3605cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
3615cec214eSBenoît Thébaudeau 		/*
362db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
363db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
364db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
365db191346SBenoît Thébaudeau 		 * bytes.
3665cec214eSBenoît Thébaudeau 		 */
367db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
3685cec214eSBenoît Thébaudeau 	}
3695cec214eSBenoît Thébaudeau /*
370db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
371db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
3725cec214eSBenoît Thébaudeau  */
373db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
3745cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
3755cec214eSBenoît Thébaudeau #endif
3765cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
3775cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
3785cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
3795cec214eSBenoît Thébaudeau 		return -1;
3805cec214eSBenoît Thébaudeau 	}
3815cec214eSBenoît Thébaudeau 
38271c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
3835cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
384de98e8b2SMarek Vasut 
385b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
386b8adb120SMarek Vasut 
38741b1f0acSMarek Vasut 	/*
38841b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
38941b1f0acSMarek Vasut 	 *
39041b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
39141b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
39241b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
39341b1f0acSMarek Vasut 	 * - qh_curtd
39441b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
39541b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
39641b1f0acSMarek Vasut 	 */
397676ae068SLucas Stach 	qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
398c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
399db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
40014eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
401db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
40214eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
403c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
40414eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
40514eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
40671c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
4074e2c4ad3SHans de Goede 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
40871c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
4094e2c4ad3SHans de Goede 	ehci_update_endpt2_dev_n_port(dev, qh);
41071c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
4112456b97fSStephen Warren 	qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
4122731b9a8SJean-Christophe PLAGNIOL-VILLARD 
41371c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
4142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
41641b1f0acSMarek Vasut 		/*
41741b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
41841b1f0acSMarek Vasut 		 *
41941b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
42041b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
42141b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
42241b1f0acSMarek Vasut 		 *
42341b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
42441b1f0acSMarek Vasut 		 */
425de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
426de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
42714eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
42814eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
42914eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
43014eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
431de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
43214eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
43314eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
4342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
4352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
43641b1f0acSMarek Vasut 		/* Update previous qTD! */
437de98e8b2SMarek Vasut 		*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
438de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4392731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
4402731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
4435cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
4445cec214eSBenoît Thébaudeau 		int left_length = length;
4455cec214eSBenoît Thébaudeau 
4465cec214eSBenoît Thébaudeau 		do {
4475cec214eSBenoît Thébaudeau 			/*
4485cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
4495cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
4505cec214eSBenoît Thébaudeau 			 */
4515cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
4525cec214eSBenoît Thébaudeau 			/*
4535cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
4545cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
4555cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
4565cec214eSBenoît Thébaudeau 			 */
4575cec214eSBenoît Thébaudeau 			xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
4585cec214eSBenoît Thébaudeau 			/*
4595cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
460db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
4615cec214eSBenoît Thébaudeau 			 */
462db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
4635cec214eSBenoît Thébaudeau 			/*
4645cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
4655cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
4665cec214eSBenoît Thébaudeau 			 */
4675cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
4685cec214eSBenoît Thébaudeau 
46941b1f0acSMarek Vasut 			/*
47041b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
47141b1f0acSMarek Vasut 			 *
47241b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
47341b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
47441b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
47541b1f0acSMarek Vasut 			 *
47641b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
47741b1f0acSMarek Vasut 			 */
4785cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
4795cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4805cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
4815cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4825cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
4835cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
48414eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
4855cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
4865cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
48714eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
48814eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
489de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
4905cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
4915cec214eSBenoît Thébaudeau 						xfr_bytes)) {
49214eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
4932731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
4942731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
49541b1f0acSMarek Vasut 			/* Update previous qTD! */
496de98e8b2SMarek Vasut 			*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
497de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
498db191346SBenoît Thébaudeau 			/*
499db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
500db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
501db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
502db191346SBenoît Thébaudeau 			 */
503db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
504db191346SBenoît Thébaudeau 				toggle ^= 1;
5055cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
5065cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
5075cec214eSBenoît Thébaudeau 		} while (left_length > 0);
5082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5092731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
51141b1f0acSMarek Vasut 		/*
51241b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
51341b1f0acSMarek Vasut 		 *
51441b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
51541b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
51641b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
51741b1f0acSMarek Vasut 		 */
518de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
519de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
520db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
52114eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
52214eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
52314eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
52414eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
525de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
52641b1f0acSMarek Vasut 		/* Update previous qTD! */
527de98e8b2SMarek Vasut 		*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
528de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
5292731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5302731b9a8SJean-Christophe PLAGNIOL-VILLARD 
531676ae068SLucas Stach 	ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
5322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5332731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
534676ae068SLucas Stach 	flush_dcache_range((uint32_t)&ctrl->qh_list,
535676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
53671c5de4fSTom Rini 	flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
53714eb79b7SBenoît Thébaudeau 	flush_dcache_range((uint32_t)qtd,
5385cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
5392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
540c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
541676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
542c7701af5SIlya Yanok 
543676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
544676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
5452731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
547676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
549676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5502731b9a8SJean-Christophe PLAGNIOL-VILLARD 
551676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
5522731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
55414eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
5552731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
5562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5572731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
5592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
560de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
56196820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
5622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
5632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
564676ae068SLucas Stach 		invalidate_dcache_range((uint32_t)&ctrl->qh_list,
565676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
56671c5de4fSTom Rini 		invalidate_dcache_range((uint32_t)qh,
56771c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
568b8adb120SMarek Vasut 		invalidate_dcache_range((uint32_t)qtd,
5695cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
570b8adb120SMarek Vasut 
5712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
57214eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
5732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
57467333f76SStefan Roese 		WATCHDOG_RESET();
57596820a35SSimon Glass 	} while (get_timer(ts) < timeout);
57696820a35SSimon Glass 
577189a6956SIlya Yanok 	/*
578189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
579189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
580189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
581189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
582189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
583189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
584189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
585189a6956SIlya Yanok 	 */
586189a6956SIlya Yanok 	invalidate_dcache_range((uint32_t)buffer,
587189a6956SIlya Yanok 		ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
588b8adb120SMarek Vasut 
58996820a35SSimon Glass 	/* Check that the TD processing happened */
59014eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
59196820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
5922731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
594676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
596676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
598676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
5992731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
60114eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
6022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6042731b9a8SJean-Christophe PLAGNIOL-VILLARD 
60571c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
60614eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
6072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
60814eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
60914eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
6102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
61114eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
6122731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
6132731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
6142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
6152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
61614eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
6172731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
6182731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
61914eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
62014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
6212731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
6222731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
62314eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
62414eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
6252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
6262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
62914eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
630222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
6312731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6322731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
63314eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
6342731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
6352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
636e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
6372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
638676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
639676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
640676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
641e82a316dSKuo-Jung Su #endif
6422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6432731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6445cec214eSBenoît Thébaudeau 	free(qtd);
6452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
6462731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6472731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
6485cec214eSBenoît Thébaudeau 	free(qtd);
6492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
6502731b9a8SJean-Christophe PLAGNIOL-VILLARD }
6512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6521dde1423SKuo-Jung Su __weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
6531dde1423SKuo-Jung Su {
6541dde1423SKuo-Jung Su 	if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
6551dde1423SKuo-Jung Su 		/* Printing the message would cause a scan failure! */
6561dde1423SKuo-Jung Su 		debug("The request port(%u) is not configured\n", port);
6571dde1423SKuo-Jung Su 		return NULL;
6581dde1423SKuo-Jung Su 	}
6591dde1423SKuo-Jung Su 
6601dde1423SKuo-Jung Su 	return (uint32_t *)&hcor->or_portsc[port];
6611dde1423SKuo-Jung Su }
6621dde1423SKuo-Jung Su 
6632731b9a8SJean-Christophe PLAGNIOL-VILLARD int
6642731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
6652731b9a8SJean-Christophe PLAGNIOL-VILLARD 		 int length, struct devrequest *req)
6662731b9a8SJean-Christophe PLAGNIOL-VILLARD {
6672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
6682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
6692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
6702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
6712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
6722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
6737d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
674676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
6752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
6772731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
6792731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
6802731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
6812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
6822731b9a8SJean-Christophe PLAGNIOL-VILLARD 
68344259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
6842731b9a8SJean-Christophe PLAGNIOL-VILLARD 
68544259bb9SPrafulla Wadaskar 	switch (typeReq) {
6869c6a9d7cSKuo-Jung Su 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
6879c6a9d7cSKuo-Jung Su 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
6889c6a9d7cSKuo-Jung Su 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
6891dde1423SKuo-Jung Su 		status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
6901dde1423SKuo-Jung Su 		if (!status_reg)
6919c6a9d7cSKuo-Jung Su 			return -1;
6929c6a9d7cSKuo-Jung Su 		break;
6939c6a9d7cSKuo-Jung Su 	default:
6949c6a9d7cSKuo-Jung Su 		status_reg = NULL;
6959c6a9d7cSKuo-Jung Su 		break;
6969c6a9d7cSKuo-Jung Su 	}
6979c6a9d7cSKuo-Jung Su 
6989c6a9d7cSKuo-Jung Su 	switch (typeReq) {
6992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
7002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
7022731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
70414eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
7052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
7072731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
7082731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
70914eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
71014eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
71114eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
7152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
7172731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
7182731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
7192731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7202731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
7212731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
7222731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
7232731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7242731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
7252731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
7262731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
7272731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
7282731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
7292731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7302731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7312731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
7322731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
7332731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
7342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7392731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
7422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
7442731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
7452731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
74614eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
7542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
755676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
7662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
7712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
7722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
7762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
778c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
7802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
7812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
7822731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
784b068deb3SJim Lin 			switch (ehci_get_port_speed(ctrl->hcor, reg)) {
78514eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
7862731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
78714eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
7882731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
7892731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
79014eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
7912731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7922731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7932731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7942731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7982731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
8002731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
8012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
8022731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
8032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
8057d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
8102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
8172731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
8182731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8192731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
820676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
8212731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
8222731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8232731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8242731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8252731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
8282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
8292731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
8317d9aa8fdSJulius Werner 				      port - 1);
8322731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8342731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
8352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
836c8b2d1dcSSergei Shtylyov 				int ret;
837c8b2d1dcSSergei Shtylyov 
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
8392731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
8442731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
8452731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
8463874b6d6SMarek Vasut 				ehci_powerup_fixup(status_reg, &reg);
8473874b6d6SMarek Vasut 
848b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
849c8b2d1dcSSergei Shtylyov 				/*
850c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
851c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
852c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
853c8b2d1dcSSergei Shtylyov 				 */
854c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
855c8b2d1dcSSergei Shtylyov 						2 * 1000);
856c8b2d1dcSSergei Shtylyov 				if (!ret)
8577d9aa8fdSJulius Werner 					ctrl->portreset |= 1 << port;
858c8b2d1dcSSergei Shtylyov 				else
859c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
8607d9aa8fdSJulius Werner 					       port - 1);
8612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8637d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
8645077f96fSJulius Werner 			ehci_shutdown(ctrl);
8657d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
8667d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
8677d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
8687d9aa8fdSJulius Werner 			break;
8692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
8702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
8712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
8722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
874676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
8752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
878ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
8792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
8822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
884ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
8852731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
887676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
888ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
889ed10e66aSSimon Glass 			break;
8902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
891ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
8922731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
894ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
8952731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
8977d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
8982731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9002731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9012731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
9042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
905676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
9082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
9092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
9102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9112731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9125b84dd67SMike Frysinger 	mdelay(1);
9132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	len = min3(srclen, le16_to_cpu(req->length), length);
9142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
9152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
9162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
9172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
9182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9192731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
9202731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
9212731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9222731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9232731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
9242731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
9252731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
9262731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
9272731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9282731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
9292731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
9302731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
9312731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
933c7e3b2b5SLucas Stach int usb_lowlevel_stop(int index)
9342731b9a8SJean-Christophe PLAGNIOL-VILLARD {
9355077f96fSJulius Werner 	ehci_shutdown(&ehcic[index]);
936676ae068SLucas Stach 	return ehci_hcd_stop(index);
9372731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9382731b9a8SJean-Christophe PLAGNIOL-VILLARD 
93906d513ecSTroy Kisky int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
9402731b9a8SJean-Christophe PLAGNIOL-VILLARD {
9412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
9422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
943676ae068SLucas Stach 	struct QH *qh_list;
9448f62ca64SPatrick Georgi 	struct QH *periodic;
9458f62ca64SPatrick Georgi 	int i;
946127efc4fSTroy Kisky 	int rc;
9472731b9a8SJean-Christophe PLAGNIOL-VILLARD 
948127efc4fSTroy Kisky 	rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
949127efc4fSTroy Kisky 	if (rc)
950127efc4fSTroy Kisky 		return rc;
951127efc4fSTroy Kisky 	if (init == USB_INIT_DEVICE)
952127efc4fSTroy Kisky 		goto done;
9532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* EHCI spec section 4.1 */
955676ae068SLucas Stach 	if (ehci_reset(index))
9562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
9572731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9582731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
959127efc4fSTroy Kisky 	rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
960127efc4fSTroy Kisky 	if (rc)
961127efc4fSTroy Kisky 		return rc;
9622731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
9632982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
9642982837eSVincent Palatin 	if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
965eb63218bSMarek Vasut 		ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
9662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
967676ae068SLucas Stach 	qh_list = &ehcic[index].qh_list;
968676ae068SLucas Stach 
9692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
97071c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
97171c5de4fSTom Rini 	qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
97214eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
97314eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
97471c5de4fSTom Rini 	qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
97571c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
97671c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
97714eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
97814eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
9792731b9a8SJean-Christophe PLAGNIOL-VILLARD 
980d3e07478SStephen Warren 	flush_dcache_range((uint32_t)qh_list,
981d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, qh_list, 1));
982d3e07478SStephen Warren 
9838f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
9848f62ca64SPatrick Georgi 	ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
9858f62ca64SPatrick Georgi 
9868f62ca64SPatrick Georgi 	/*
9878f62ca64SPatrick Georgi 	 * Set up periodic list
9888f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
9898f62ca64SPatrick Georgi 	 */
99036b73109SHans de Goede 	ehcic[index].periodic_schedules = 0;
9918f62ca64SPatrick Georgi 	periodic = &ehcic[index].periodic_queue;
9928f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
9938f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
9948f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
9958f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
9968f62ca64SPatrick Georgi 
997d3e07478SStephen Warren 	flush_dcache_range((uint32_t)periodic,
998d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, periodic, 1));
999d3e07478SStephen Warren 
10008f62ca64SPatrick Georgi 	/*
10018f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
10028f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
10038f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
10048f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
10058f62ca64SPatrick Georgi 	 *
10068f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
10078f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
10088f62ca64SPatrick Georgi 	 */
10098bc36036SNikita Kiryanov 	if (ehcic[index].periodic_list == NULL)
10108f62ca64SPatrick Georgi 		ehcic[index].periodic_list = memalign(4096, 1024 * 4);
10118bc36036SNikita Kiryanov 
10128f62ca64SPatrick Georgi 	if (!ehcic[index].periodic_list)
10138f62ca64SPatrick Georgi 		return -ENOMEM;
10148f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
1015ea427775SAdrian Cox 		ehcic[index].periodic_list[i] = cpu_to_hc32((uint32_t)periodic
1016ea427775SAdrian Cox 						| QH_LINK_TYPE_QH);
10178f62ca64SPatrick Georgi 	}
10188f62ca64SPatrick Georgi 
1019d3e07478SStephen Warren 	flush_dcache_range((uint32_t)ehcic[index].periodic_list,
1020d3e07478SStephen Warren 			   ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
1021d3e07478SStephen Warren 					  1024));
1022d3e07478SStephen Warren 
10238f62ca64SPatrick Georgi 	/* Set periodic list base address */
10248f62ca64SPatrick Georgi 	ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
10258f62ca64SPatrick Georgi 		(uint32_t)ehcic[index].periodic_list);
10268f62ca64SPatrick Georgi 
1027676ae068SLucas Stach 	reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
10282731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
10297a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
10302731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
10312731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
103293ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
103393ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
10342731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
10352731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
103693ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
103793ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
10382731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
1040676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
10412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
10422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
10432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
10442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
10452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
10462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
1047676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
10482731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1049e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
10502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* take control over the ports */
1051676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
10522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= FLAG_CF;
1053676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
1054e82a316dSKuo-Jung Su #endif
1055e82a316dSKuo-Jung Su 
10562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
1057676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
10585b84dd67SMike Frysinger 	mdelay(5);
1059676ae068SLucas Stach 	reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
10602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
10612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1062676ae068SLucas Stach 	ehcic[index].rootdev = 0;
1063127efc4fSTroy Kisky done:
1064676ae068SLucas Stach 	*controller = &ehcic[index];
10652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
10662731b9a8SJean-Christophe PLAGNIOL-VILLARD }
10672731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10682731b9a8SJean-Christophe PLAGNIOL-VILLARD int
10692731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
10702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		int length)
10712731b9a8SJean-Christophe PLAGNIOL-VILLARD {
10722731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
10742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
10752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
10762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
10772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
10782731b9a8SJean-Christophe PLAGNIOL-VILLARD }
10792731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10802731b9a8SJean-Christophe PLAGNIOL-VILLARD int
10812731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
10822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *setup)
10832731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1084676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
10852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
10872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
10882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
10892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
10902731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1091676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1092676ae068SLucas Stach 		if (!ctrl->rootdev)
10932731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
10942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
10952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
10962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
10972731b9a8SJean-Christophe PLAGNIOL-VILLARD }
10982731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10998f62ca64SPatrick Georgi struct int_queue {
1100*8aa26b8eSHans de Goede 	int elementsize;
11018f62ca64SPatrick Georgi 	struct QH *first;
11028f62ca64SPatrick Georgi 	struct QH *current;
11038f62ca64SPatrick Georgi 	struct QH *last;
11048f62ca64SPatrick Georgi 	struct qTD *tds;
11058f62ca64SPatrick Georgi };
11068f62ca64SPatrick Georgi 
1107ea427775SAdrian Cox #define NEXT_QH(qh) (struct QH *)(hc32_to_cpu((qh)->qh_link) & ~0x1f)
11088f62ca64SPatrick Georgi 
11098f62ca64SPatrick Georgi static int
11108f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
11118f62ca64SPatrick Georgi {
11128f62ca64SPatrick Georgi 	uint32_t cmd;
11138f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
11148f62ca64SPatrick Georgi 	int ret;
11158f62ca64SPatrick Georgi 
11168f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
11178f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
11188f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
11198f62ca64SPatrick Georgi 
11208f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
11218f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
11228f62ca64SPatrick Georgi 	if (ret < 0) {
11238f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
11248f62ca64SPatrick Georgi 		return -ETIMEDOUT;
11258f62ca64SPatrick Georgi 	}
11268f62ca64SPatrick Georgi 	udelay(1000);
11278f62ca64SPatrick Georgi 	return 0;
11288f62ca64SPatrick Georgi }
11298f62ca64SPatrick Georgi 
11308f62ca64SPatrick Georgi static int
11318f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
11328f62ca64SPatrick Georgi {
11338f62ca64SPatrick Georgi 	uint32_t cmd;
11348f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
11358f62ca64SPatrick Georgi 	int ret;
11368f62ca64SPatrick Georgi 
11378f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
11388f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
11398f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
11408f62ca64SPatrick Georgi 
11418f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
11428f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
11438f62ca64SPatrick Georgi 	if (ret < 0) {
11448f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
11458f62ca64SPatrick Georgi 		return -ETIMEDOUT;
11468f62ca64SPatrick Georgi 	}
11478f62ca64SPatrick Georgi 	return 0;
11488f62ca64SPatrick Georgi }
11498f62ca64SPatrick Georgi 
11508f62ca64SPatrick Georgi struct int_queue *
11518f62ca64SPatrick Georgi create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
11528f62ca64SPatrick Georgi 		 int elementsize, void *buffer)
11538f62ca64SPatrick Georgi {
11548f62ca64SPatrick Georgi 	struct ehci_ctrl *ctrl = dev->controller;
11558f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
11568f62ca64SPatrick Georgi 	int i;
11578f62ca64SPatrick Georgi 
1158bd818d81SHans de Goede 	/*
1159bd818d81SHans de Goede 	 * Interrupt transfers requiring several transactions are not supported
1160bd818d81SHans de Goede 	 * because bInterval is ignored.
1161bd818d81SHans de Goede 	 *
1162bd818d81SHans de Goede 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1163bd818d81SHans de Goede 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1164bd818d81SHans de Goede 	 * specification does not constrain this for interrupt transfers. That
1165bd818d81SHans de Goede 	 * means that ehci_submit_async() would support interrupt transfers
1166bd818d81SHans de Goede 	 * requiring several transactions only as long as the transfer size does
1167bd818d81SHans de Goede 	 * not require more than a single qTD.
1168bd818d81SHans de Goede 	 */
1169bd818d81SHans de Goede 	if (elementsize > usb_maxpacket(dev, pipe)) {
1170bd818d81SHans de Goede 		printf("%s: xfers requiring several transactions are not supported.\n",
1171bd818d81SHans de Goede 		       __func__);
1172bd818d81SHans de Goede 		return NULL;
1173bd818d81SHans de Goede 	}
1174bd818d81SHans de Goede 
11758f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
11768f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
11778f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
11788f62ca64SPatrick Georgi 		return NULL;
11798f62ca64SPatrick Georgi 	}
11808f62ca64SPatrick Georgi 
11818f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
11828f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
11838f62ca64SPatrick Georgi 	 * no matter the alignment
11848f62ca64SPatrick Georgi 	 */
11858f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
11868f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
11878f62ca64SPatrick Georgi 		return NULL;
11888f62ca64SPatrick Georgi 	}
11898f62ca64SPatrick Georgi 
11908f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
11918f62ca64SPatrick Georgi 	if (!result) {
11928f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
11938f62ca64SPatrick Georgi 		goto fail1;
11948f62ca64SPatrick Georgi 	}
1195*8aa26b8eSHans de Goede 	result->elementsize = elementsize;
11968165e34bSStephen Warren 	result->first = memalign(USB_DMA_MINALIGN,
11978165e34bSStephen Warren 				 sizeof(struct QH) * queuesize);
11988f62ca64SPatrick Georgi 	if (!result->first) {
11998f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12008f62ca64SPatrick Georgi 		goto fail2;
12018f62ca64SPatrick Georgi 	}
12028f62ca64SPatrick Georgi 	result->current = result->first;
12038f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
12048165e34bSStephen Warren 	result->tds = memalign(USB_DMA_MINALIGN,
12058165e34bSStephen Warren 			       sizeof(struct qTD) * queuesize);
12068f62ca64SPatrick Georgi 	if (!result->tds) {
12078f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12088f62ca64SPatrick Georgi 		goto fail3;
12098f62ca64SPatrick Georgi 	}
12108f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
12118f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
12128f62ca64SPatrick Georgi 
12138f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
12148f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
12158f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
12168f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
12178f62ca64SPatrick Georgi 
1218ea427775SAdrian Cox 		qh->qh_link = cpu_to_hc32((uint32_t)(qh+1) | QH_LINK_TYPE_QH);
12198f62ca64SPatrick Georgi 		if (i == queuesize - 1)
1220ea427775SAdrian Cox 			qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
12218f62ca64SPatrick Georgi 
1222ea427775SAdrian Cox 		qh->qh_overlay.qt_next = cpu_to_hc32((uint32_t)td);
1223ea427775SAdrian Cox 		qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1224ea427775SAdrian Cox 		qh->qh_endpt1 =
1225ea427775SAdrian Cox 			cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
12268f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
12278f62ca64SPatrick Georgi 			(1 << 14) |
12288f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
12298f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1230ea427775SAdrian Cox 			(usb_pipedevice(pipe) << 0));
1231ea427775SAdrian Cox 		qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1232ea427775SAdrian Cox 			(1 << 0)); /* S-mask: microframe 0 */
12338f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
12348f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
12354e2c4ad3SHans de Goede 			/* C-mask: microframes 2-4 */
12364e2c4ad3SHans de Goede 			qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
12378f62ca64SPatrick Georgi 		}
12384e2c4ad3SHans de Goede 		ehci_update_endpt2_dev_n_port(dev, qh);
12398f62ca64SPatrick Georgi 
1240ea427775SAdrian Cox 		td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1241ea427775SAdrian Cox 		td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
12428f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
12438f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
1244ea427775SAdrian Cox 		td->qt_token = cpu_to_hc32((elementsize << 16) |
12458f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1246ea427775SAdrian Cox 			0x80); /* active */
1247ea427775SAdrian Cox 		td->qt_buffer[0] =
1248ea427775SAdrian Cox 		    cpu_to_hc32((uint32_t)buffer + i * elementsize);
1249ea427775SAdrian Cox 		td->qt_buffer[1] =
1250ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1251ea427775SAdrian Cox 		td->qt_buffer[2] =
1252ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1253ea427775SAdrian Cox 		td->qt_buffer[3] =
1254ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1255ea427775SAdrian Cox 		td->qt_buffer[4] =
1256ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
12578f62ca64SPatrick Georgi 
12588f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
12598f62ca64SPatrick Georgi 	}
12608f62ca64SPatrick Georgi 
1261d3e07478SStephen Warren 	flush_dcache_range((uint32_t)buffer,
1262d3e07478SStephen Warren 			   ALIGN_END_ADDR(char, buffer,
1263d3e07478SStephen Warren 					  queuesize * elementsize));
1264d3e07478SStephen Warren 	flush_dcache_range((uint32_t)result->first,
1265d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->first,
1266d3e07478SStephen Warren 					  queuesize));
1267d3e07478SStephen Warren 	flush_dcache_range((uint32_t)result->tds,
1268d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct qTD, result->tds,
1269d3e07478SStephen Warren 					  queuesize));
1270d3e07478SStephen Warren 
127132f2eac1SHans de Goede 	if (ctrl->periodic_schedules > 0) {
12728f62ca64SPatrick Georgi 		if (disable_periodic(ctrl) < 0) {
12738f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
12748f62ca64SPatrick Georgi 			goto fail3;
12758f62ca64SPatrick Georgi 		}
127632f2eac1SHans de Goede 	}
12778f62ca64SPatrick Georgi 
12788f62ca64SPatrick Georgi 	/* hook up to periodic list */
12798f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
12808f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
1281ea427775SAdrian Cox 	list->qh_link = cpu_to_hc32((uint32_t)result->first | QH_LINK_TYPE_QH);
12828f62ca64SPatrick Georgi 
1283d3e07478SStephen Warren 	flush_dcache_range((uint32_t)result->last,
1284d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->last, 1));
1285d3e07478SStephen Warren 	flush_dcache_range((uint32_t)list,
1286d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, list, 1));
1287d3e07478SStephen Warren 
12888f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
12898f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
12908f62ca64SPatrick Georgi 		goto fail3;
12918f62ca64SPatrick Georgi 	}
129236b73109SHans de Goede 	ctrl->periodic_schedules++;
12938f62ca64SPatrick Georgi 
12948f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
12958f62ca64SPatrick Georgi 	return result;
12968f62ca64SPatrick Georgi fail3:
12978f62ca64SPatrick Georgi 	if (result->tds)
12988f62ca64SPatrick Georgi 		free(result->tds);
12998f62ca64SPatrick Georgi fail2:
13008f62ca64SPatrick Georgi 	if (result->first)
13018f62ca64SPatrick Georgi 		free(result->first);
13028f62ca64SPatrick Georgi 	if (result)
13038f62ca64SPatrick Georgi 		free(result);
13048f62ca64SPatrick Georgi fail1:
13058f62ca64SPatrick Georgi 	return NULL;
13068f62ca64SPatrick Georgi }
13078f62ca64SPatrick Georgi 
13088f62ca64SPatrick Georgi void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
13098f62ca64SPatrick Georgi {
13108f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
1311415548d8SHans de Goede 	struct qTD *cur_td;
13128f62ca64SPatrick Georgi 
13138f62ca64SPatrick Georgi 	/* depleted queue */
13148f62ca64SPatrick Georgi 	if (cur == NULL) {
13158f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
13168f62ca64SPatrick Georgi 		return NULL;
13178f62ca64SPatrick Georgi 	}
13188f62ca64SPatrick Georgi 	/* still active */
1319415548d8SHans de Goede 	cur_td = &queue->tds[queue->current - queue->first];
1320415548d8SHans de Goede 	invalidate_dcache_range((uint32_t)cur_td,
1321415548d8SHans de Goede 				ALIGN_END_ADDR(struct qTD, cur_td, 1));
1322415548d8SHans de Goede 	if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1323415548d8SHans de Goede 			QT_TOKEN_STATUS_ACTIVE) {
1324415548d8SHans de Goede 		debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1325415548d8SHans de Goede 		      hc32_to_cpu(cur_td->qt_token));
13268f62ca64SPatrick Georgi 		return NULL;
13278f62ca64SPatrick Georgi 	}
13288f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
13298f62ca64SPatrick Georgi 		queue->current++;
13308f62ca64SPatrick Georgi 	else
13318f62ca64SPatrick Georgi 		queue->current = NULL;
1332*8aa26b8eSHans de Goede 
1333*8aa26b8eSHans de Goede 	invalidate_dcache_range((uint32_t)cur->buffer,
1334*8aa26b8eSHans de Goede 				ALIGN_END_ADDR(char, cur->buffer,
1335*8aa26b8eSHans de Goede 					       queue->elementsize));
1336*8aa26b8eSHans de Goede 
1337415548d8SHans de Goede 	debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1338415548d8SHans de Goede 	      hc32_to_cpu(cur_td->qt_token), cur, queue->first);
13398f62ca64SPatrick Georgi 	return cur->buffer;
13408f62ca64SPatrick Georgi }
13418f62ca64SPatrick Georgi 
13428f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
13432d17b489SJeroen Hofstee static int
13448f62ca64SPatrick Georgi destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
13458f62ca64SPatrick Georgi {
13468f62ca64SPatrick Georgi 	struct ehci_ctrl *ctrl = dev->controller;
13478f62ca64SPatrick Georgi 	int result = -1;
13488f62ca64SPatrick Georgi 	unsigned long timeout;
13498f62ca64SPatrick Georgi 
13508f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
13518f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
13528f62ca64SPatrick Georgi 		goto out;
13538f62ca64SPatrick Georgi 	}
135436b73109SHans de Goede 	ctrl->periodic_schedules--;
13558f62ca64SPatrick Georgi 
13568f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
13578f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
1358ea427775SAdrian Cox 	while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
13598f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
13608f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
13618f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
13628f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
1363ea7b30c5SHans de Goede 			flush_dcache_range((uint32_t)cur,
1364ea7b30c5SHans de Goede 					   ALIGN_END_ADDR(struct QH, cur, 1));
13658f62ca64SPatrick Georgi 			result = 0;
13668f62ca64SPatrick Georgi 			break;
13678f62ca64SPatrick Georgi 		}
13688f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
13698f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
13708f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
13718f62ca64SPatrick Georgi 			result = -1;
13728f62ca64SPatrick Georgi 			goto out;
13738f62ca64SPatrick Georgi 		}
13748f62ca64SPatrick Georgi 	}
13758f62ca64SPatrick Georgi 
137636b73109SHans de Goede 	if (ctrl->periodic_schedules > 0) {
13778f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
13788f62ca64SPatrick Georgi 		if (result < 0)
13798f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
13808f62ca64SPatrick Georgi 	}
13818f62ca64SPatrick Georgi 
13828f62ca64SPatrick Georgi out:
13838f62ca64SPatrick Georgi 	free(queue->tds);
13848f62ca64SPatrick Georgi 	free(queue->first);
13858f62ca64SPatrick Georgi 	free(queue);
13868f62ca64SPatrick Georgi 
13878f62ca64SPatrick Georgi 	return result;
13888f62ca64SPatrick Georgi }
13898f62ca64SPatrick Georgi 
13902731b9a8SJean-Christophe PLAGNIOL-VILLARD int
13912731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
13922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	       int length, int interval)
13932731b9a8SJean-Christophe PLAGNIOL-VILLARD {
13948f62ca64SPatrick Georgi 	void *backbuffer;
13958f62ca64SPatrick Georgi 	struct int_queue *queue;
13968f62ca64SPatrick Georgi 	unsigned long timeout;
13978f62ca64SPatrick Georgi 	int result = 0, ret;
13988f62ca64SPatrick Georgi 
13992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
14002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
140144ae0be7SBenoît Thébaudeau 
14028f62ca64SPatrick Georgi 	queue = create_int_queue(dev, pipe, 1, length, buffer);
1403bd818d81SHans de Goede 	if (!queue)
1404bd818d81SHans de Goede 		return -1;
14058f62ca64SPatrick Georgi 
14068f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
14078f62ca64SPatrick Georgi 	while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
14088f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14098f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
14108f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
14118f62ca64SPatrick Georgi 			break;
14128f62ca64SPatrick Georgi 		}
14138f62ca64SPatrick Georgi 
14148f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
14158f62ca64SPatrick Georgi 		debug("got wrong buffer back (%x instead of %x)\n",
14168f62ca64SPatrick Georgi 		      (uint32_t)backbuffer, (uint32_t)buffer);
14178f62ca64SPatrick Georgi 		return -EINVAL;
14188f62ca64SPatrick Georgi 	}
14198f62ca64SPatrick Georgi 
14208f62ca64SPatrick Georgi 	ret = destroy_int_queue(dev, queue);
14218f62ca64SPatrick Georgi 	if (ret < 0)
14228f62ca64SPatrick Georgi 		return ret;
14238f62ca64SPatrick Georgi 
14248f62ca64SPatrick Georgi 	/* everything worked out fine */
14258f62ca64SPatrick Georgi 	return result;
14262731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1427