xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision 1bee64cb36b365e298e6ae73ef4ea08f68a1e2c2)
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  *
8e62b5266SSimon Glass  * SPDX-License-Identifier:	GPL-2.0
92731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
102731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
1146b01797SSimon Glass #include <dm.h>
128f62ca64SPatrick Georgi #include <errno.h>
132731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/byteorder.h>
1493ad908cSLucas Stach #include <asm/unaligned.h>
152731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
162731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
172731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <malloc.h>
18cf92e05cSSimon Glass #include <memalign.h>
1967333f76SStefan Roese #include <watchdog.h>
208f62ca64SPatrick Georgi #include <linux/compiler.h>
212731b9a8SJean-Christophe PLAGNIOL-VILLARD 
222731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
232731b9a8SJean-Christophe PLAGNIOL-VILLARD 
24676ae068SLucas Stach #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
25676ae068SLucas Stach #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
26676ae068SLucas Stach #endif
272731b9a8SJean-Christophe PLAGNIOL-VILLARD 
285077f96fSJulius Werner /*
295077f96fSJulius Werner  * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
305077f96fSJulius Werner  * Let's time out after 8 to have a little safety margin on top of that.
315077f96fSJulius Werner  */
325077f96fSJulius Werner #define HCHALT_TIMEOUT (8 * 1000)
335077f96fSJulius Werner 
3446b01797SSimon Glass #ifndef CONFIG_DM_USB
35b959655fSMarek Vasut static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
3646b01797SSimon Glass #endif
3771c5de4fSTom Rini 
3871c5de4fSTom Rini #define ALIGN_END_ADDR(type, ptr, size)			\
3998ae840aSRob Herring 	((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
402731b9a8SJean-Christophe PLAGNIOL-VILLARD 
412731b9a8SJean-Christophe PLAGNIOL-VILLARD static struct descriptor {
422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_hub_descriptor hub;
432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_device_descriptor device;
442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_config_descriptor config;
452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_interface_descriptor interface;
462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_endpoint_descriptor endpoint;
472731b9a8SJean-Christophe PLAGNIOL-VILLARD }  __attribute__ ((packed)) descriptor = {
482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x8,		/* bDescLength */
502731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x29,		/* bDescriptorType: hub descriptor */
512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bNrPorts -- runtime modified */
522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* wHubCharacteristics */
535f4b4f2fSVincent Palatin 		10,		/* bPwrOn2PwrGood */
542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bHubCntrCurrent */
55337fc7e6SBin Meng 		{		/* Device removable */
56337fc7e6SBin Meng 		}		/* at most 7 ports! XXX */
572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x12,		/* bLength */
602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDescriptorType: UDESC_DEVICE */
616d313c84SSergei Shtylyov 		cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bDeviceClass: UDCLASS_HUB */
632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bDeviceSubClass: UDSUBCLASS_HUB */
642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDeviceProtocol: UDPROTO_HSHUBSTT */
652731b9a8SJean-Christophe PLAGNIOL-VILLARD 		64,		/* bMaxPacketSize: 64 bytes */
662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idVendor */
672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idProduct */
686d313c84SSergei Shtylyov 		cpu_to_le16(0x0100), /* bcdDevice */
692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* iManufacturer */
702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* iProduct */
712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iSerialNumber */
722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1		/* bNumConfigurations: 1 */
732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,
762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bDescriptorType: UDESC_CONFIG */
772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cpu_to_le16(0x19),
782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumInterface */
792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bConfigurationValue */
802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iConfiguration */
812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x40,		/* bmAttributes: UC_SELF_POWER */
822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* bMaxPower */
832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,		/* bLength */
862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		4,		/* bDescriptorType: UDESC_INTERFACE */
872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceNumber */
882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bAlternateSetting */
892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumEndpoints */
902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bInterfaceClass: UICLASS_HUB */
912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceSubClass: UISUBCLASS_HUB */
922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceProtocol: UIPROTO_HSHUBSTT */
932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* iInterface */
942731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x7,		/* bLength */
972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		5,		/* bDescriptorType: UDESC_ENDPOINT */
982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x81,		/* bEndpointAddress:
992731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * UE_DIR_IN | EHCI_INTR_ENDPT
1002731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
1012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		3,		/* bmAttributes: UE_INTERRUPT */
1028f8bd565STom Rix 		8,		/* wMaxPacketSize */
1032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		255		/* bInterval */
1042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1052731b9a8SJean-Christophe PLAGNIOL-VILLARD };
1062731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1072731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_IS_TDI)
1082731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(1)
1092731b9a8SJean-Christophe PLAGNIOL-VILLARD #else
1102731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(0)
1112731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
1122731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11324ed894fSSimon Glass static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
11424ed894fSSimon Glass {
11546b01797SSimon Glass #ifdef CONFIG_DM_USB
11625c8ebdfSHans de Goede 	return dev_get_priv(usb_get_bus(udev->dev));
11746b01797SSimon Glass #else
11824ed894fSSimon Glass 	return udev->controller;
11946b01797SSimon Glass #endif
12024ed894fSSimon Glass }
12124ed894fSSimon Glass 
122deb8508cSSimon Glass static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
123b068deb3SJim Lin {
124b068deb3SJim Lin 	return PORTSC_PSPD(reg);
125b068deb3SJim Lin }
126b068deb3SJim Lin 
127deb8508cSSimon Glass static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
128b068deb3SJim Lin {
129b068deb3SJim Lin 	uint32_t tmp;
130b068deb3SJim Lin 	uint32_t *reg_ptr;
131b068deb3SJim Lin 
13211d18a19SSimon Glass 	reg_ptr = (uint32_t *)((u8 *)&ctrl->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;
1377ab0d355SMarek Vasut #else
1387ab0d355SMarek Vasut 	tmp &= ~USBMODE_BE;
139b068deb3SJim Lin #endif
140b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
141b068deb3SJim Lin }
142b068deb3SJim Lin 
143deb8508cSSimon Glass static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
144727fce36SSimon Glass 			       uint32_t *reg)
1453874b6d6SMarek Vasut {
1463874b6d6SMarek Vasut 	mdelay(50);
1473874b6d6SMarek Vasut }
1483874b6d6SMarek Vasut 
149deb8508cSSimon Glass static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
150aac064f7SSimon Glass {
15199c22556SBin Meng 	int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
15299c22556SBin Meng 
15399c22556SBin Meng 	if (port < 0 || port >= max_ports) {
154aac064f7SSimon Glass 		/* Printing the message would cause a scan failure! */
15599c22556SBin Meng 		debug("The request port(%u) exceeds maximum port number\n",
15699c22556SBin Meng 		      port);
157aac064f7SSimon Glass 		return NULL;
158aac064f7SSimon Glass 	}
159aac064f7SSimon Glass 
1606a1a8162SSimon Glass 	return (uint32_t *)&ctrl->hcor->or_portsc[port];
161aac064f7SSimon Glass }
162aac064f7SSimon Glass 
1632731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1642731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
16809c83a45SWolfgang Denk 		udelay(5);
1692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1772731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
179aeca43e3SSimon Glass static int ehci_reset(struct ehci_ctrl *ctrl)
1802731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1832731b9a8SJean-Christophe PLAGNIOL-VILLARD 
184aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
185273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
186aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
187aeca43e3SSimon Glass 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
188676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
1932731b9a8SJean-Christophe PLAGNIOL-VILLARD 
194b068deb3SJim Lin 	if (ehci_is_TDI())
195aeca43e3SSimon Glass 		ctrl->ops.set_usb_mode(ctrl);
1969ab4ce22SSimon Glass 
1979ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
198aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
19914eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
2009ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
201aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
2029ab4ce22SSimon Glass #endif
2032731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
2042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
2052731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2062731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2075077f96fSJulius Werner static int ehci_shutdown(struct ehci_ctrl *ctrl)
2085077f96fSJulius Werner {
2095077f96fSJulius Werner 	int i, ret = 0;
2105077f96fSJulius Werner 	uint32_t cmd, reg;
21199c22556SBin Meng 	int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
2125077f96fSJulius Werner 
2135077f96fSJulius Werner 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
2141e6fb0e3SPeng Fan 	/* If not run, directly return */
2151e6fb0e3SPeng Fan 	if (!(cmd & CMD_RUN))
2161e6fb0e3SPeng Fan 		return 0;
2175077f96fSJulius Werner 	cmd &= ~(CMD_PSE | CMD_ASE);
2185077f96fSJulius Werner 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2195077f96fSJulius Werner 	ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
2205077f96fSJulius Werner 		100 * 1000);
2215077f96fSJulius Werner 
2225077f96fSJulius Werner 	if (!ret) {
22399c22556SBin Meng 		for (i = 0; i < max_ports; i++) {
2245077f96fSJulius Werner 			reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
2255077f96fSJulius Werner 			reg |= EHCI_PS_SUSP;
2265077f96fSJulius Werner 			ehci_writel(&ctrl->hcor->or_portsc[i], reg);
2275077f96fSJulius Werner 		}
2285077f96fSJulius Werner 
2295077f96fSJulius Werner 		cmd &= ~CMD_RUN;
2305077f96fSJulius Werner 		ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2315077f96fSJulius Werner 		ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
2325077f96fSJulius Werner 			HCHALT_TIMEOUT);
2335077f96fSJulius Werner 	}
2345077f96fSJulius Werner 
2355077f96fSJulius Werner 	if (ret)
2365077f96fSJulius Werner 		puts("EHCI failed to shut down host controller.\n");
2375077f96fSJulius Werner 
2385077f96fSJulius Werner 	return ret;
2395077f96fSJulius Werner }
2405077f96fSJulius Werner 
2412731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2422731b9a8SJean-Christophe PLAGNIOL-VILLARD {
243b8adb120SMarek Vasut 	uint32_t delta, next;
244abd702f4SMarek Vasut 	unsigned long addr = (unsigned long)buf;
2452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2462731b9a8SJean-Christophe PLAGNIOL-VILLARD 
247189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
248b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
249b8adb120SMarek Vasut 
250189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
251189a6956SIlya Yanok 
2522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
253cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
254cf7c93cdSMarek Vasut 		td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr));
2553ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
25614eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2632731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2642731b9a8SJean-Christophe PLAGNIOL-VILLARD 
265cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
26698ae840aSRob Herring 		printf("out of buffer pointers (%zu bytes left)\n", sz);
2672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2692731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2712731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2722731b9a8SJean-Christophe PLAGNIOL-VILLARD 
273c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
274c60795f4SIlya Yanok {
275c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
276c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
277c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
278c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
279c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
280c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
281c60795f4SIlya Yanok 		return QH_LOW_SPEED;
282c60795f4SIlya Yanok 	return QH_FULL_SPEED;
283c60795f4SIlya Yanok }
284c60795f4SIlya Yanok 
28546b01797SSimon Glass static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
2864e2c4ad3SHans de Goede 					  struct QH *qh)
2874e2c4ad3SHans de Goede {
288faa7db24SStefan Brüns 	uint8_t portnr = 0;
289faa7db24SStefan Brüns 	uint8_t hubaddr = 0;
2904e2c4ad3SHans de Goede 
29146b01797SSimon Glass 	if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
2924e2c4ad3SHans de Goede 		return;
2934e2c4ad3SHans de Goede 
294faa7db24SStefan Brüns 	usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr);
29546b01797SSimon Glass 
296faa7db24SStefan Brüns 	qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) |
297faa7db24SStefan Brüns 				     QH_ENDPT2_HUBADDR(hubaddr));
2984e2c4ad3SHans de Goede }
2994e2c4ad3SHans de Goede 
3002731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
3012731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
3022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
3032731b9a8SJean-Christophe PLAGNIOL-VILLARD {
30471c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
3055cec214eSBenoît Thébaudeau 	struct qTD *qtd;
3065cec214eSBenoît Thébaudeau 	int qtd_count = 0;
307de98e8b2SMarek Vasut 	int qtd_counter = 0;
3082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
3092731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
3102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
311db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
3122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
3132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
31496820a35SSimon Glass 	int timeout;
3152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
31624ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
3172731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3182731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
3192731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
3202731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
3212731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
3222731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
3232731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
3242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
3252731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
3262731b9a8SJean-Christophe PLAGNIOL-VILLARD 
327db191346SBenoît Thébaudeau #define PKT_ALIGN	512
3285cec214eSBenoît Thébaudeau 	/*
3295cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
3305cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
3315cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
3325cec214eSBenoît Thébaudeau 	 *
3335cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
3345cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
3355cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
3365cec214eSBenoît Thébaudeau 	 *
3375cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
3385cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
3395cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
3405cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
3415cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
3425cec214eSBenoît Thébaudeau 	 */
3435cec214eSBenoît Thébaudeau 	if (req != NULL)
3445cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
3455cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
3465cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
3475cec214eSBenoît Thébaudeau 		/*
3485cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
349db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
350db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
351db191346SBenoît Thébaudeau 		 * shorter).
3525cec214eSBenoît Thébaudeau 		 *
3535cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
354db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
355db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
356db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
3575cec214eSBenoît Thébaudeau 		 *
358db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
3595cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
3605cec214eSBenoît Thébaudeau 		 */
3615cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
3625cec214eSBenoît Thébaudeau 		/*
363db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
364db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
3655cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
3665cec214eSBenoît Thébaudeau 		 */
36798ae840aSRob Herring 		if ((unsigned long)buffer & (PKT_ALIGN - 1))
3685cec214eSBenoît Thébaudeau 			xfr_sz--;
3695cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
3705cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
3715cec214eSBenoît Thébaudeau 		/*
372db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
373db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
374db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
375db191346SBenoît Thébaudeau 		 * bytes.
3765cec214eSBenoît Thébaudeau 		 */
377db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
3785cec214eSBenoît Thébaudeau 	}
3795cec214eSBenoît Thébaudeau /*
380db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
381db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
3825cec214eSBenoît Thébaudeau  */
383db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
3845cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
3855cec214eSBenoît Thébaudeau #endif
3865cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
3875cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
3885cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
3895cec214eSBenoît Thébaudeau 		return -1;
3905cec214eSBenoît Thébaudeau 	}
3915cec214eSBenoît Thébaudeau 
39271c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
3935cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
394de98e8b2SMarek Vasut 
395b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
396b8adb120SMarek Vasut 
39741b1f0acSMarek Vasut 	/*
39841b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
39941b1f0acSMarek Vasut 	 *
40041b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
40141b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
40241b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
40341b1f0acSMarek Vasut 	 * - qh_curtd
40441b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
40541b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
40641b1f0acSMarek Vasut 	 */
407cf7c93cdSMarek Vasut 	qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
408c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
409db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
41014eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
411db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
41214eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
413c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
41414eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
41514eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
41671c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
4174e2c4ad3SHans de Goede 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
41871c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
4194e2c4ad3SHans de Goede 	ehci_update_endpt2_dev_n_port(dev, qh);
42071c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
4212456b97fSStephen Warren 	qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
4222731b9a8SJean-Christophe PLAGNIOL-VILLARD 
42371c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
4242731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
42541b1f0acSMarek Vasut 		/*
42641b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
42741b1f0acSMarek Vasut 		 *
42841b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
42941b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
43041b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
43141b1f0acSMarek Vasut 		 *
43241b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
43341b1f0acSMarek Vasut 		 */
434de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
435de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
43614eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
43714eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
43814eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
43914eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
440de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
44114eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
44214eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
4432731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
4442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
44541b1f0acSMarek Vasut 		/* Update previous qTD! */
446cf7c93cdSMarek Vasut 		*tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
447de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
4492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4502731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
4525cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
4535cec214eSBenoît Thébaudeau 		int left_length = length;
4545cec214eSBenoît Thébaudeau 
4555cec214eSBenoît Thébaudeau 		do {
4565cec214eSBenoît Thébaudeau 			/*
4575cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
4585cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
4595cec214eSBenoît Thébaudeau 			 */
4605cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
4615cec214eSBenoît Thébaudeau 			/*
4625cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
4635cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
4645cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
4655cec214eSBenoît Thébaudeau 			 */
46698ae840aSRob Herring 			xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
4675cec214eSBenoît Thébaudeau 			/*
4685cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
469db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
4705cec214eSBenoît Thébaudeau 			 */
471db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
4725cec214eSBenoît Thébaudeau 			/*
4735cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
4745cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
4755cec214eSBenoît Thébaudeau 			 */
4765cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
4775cec214eSBenoît Thébaudeau 
47841b1f0acSMarek Vasut 			/*
47941b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
48041b1f0acSMarek Vasut 			 *
48141b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
48241b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
48341b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
48441b1f0acSMarek Vasut 			 *
48541b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
48641b1f0acSMarek Vasut 			 */
4875cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
4885cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4895cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
4905cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4915cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
4925cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
49314eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
4945cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
4955cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
49614eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
49714eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
498de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
4995cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
5005cec214eSBenoît Thébaudeau 						xfr_bytes)) {
50114eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
5022731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
5032731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
50441b1f0acSMarek Vasut 			/* Update previous qTD! */
505cf7c93cdSMarek Vasut 			*tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
506de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
507db191346SBenoît Thébaudeau 			/*
508db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
509db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
510db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
511db191346SBenoît Thébaudeau 			 */
512db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
513db191346SBenoît Thébaudeau 				toggle ^= 1;
5145cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
5155cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
5165cec214eSBenoît Thébaudeau 		} while (left_length > 0);
5172731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5192731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
52041b1f0acSMarek Vasut 		/*
52141b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
52241b1f0acSMarek Vasut 		 *
52341b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
52441b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
52541b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
52641b1f0acSMarek Vasut 		 */
527de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
528de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
529db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
53014eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
53114eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
53214eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
53314eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
534de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
53541b1f0acSMarek Vasut 		/* Update previous qTD! */
536cf7c93cdSMarek Vasut 		*tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
537de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
5382731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
540cf7c93cdSMarek Vasut 	ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH);
5412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
54398ae840aSRob Herring 	flush_dcache_range((unsigned long)&ctrl->qh_list,
544676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
54598ae840aSRob Herring 	flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
54698ae840aSRob Herring 	flush_dcache_range((unsigned long)qtd,
5475cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
5482731b9a8SJean-Christophe PLAGNIOL-VILLARD 
549c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
550cf7c93cdSMarek Vasut 	ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(&ctrl->qh_list));
551c7701af5SIlya Yanok 
552676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
553676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
5542731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
556676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
558676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5592731b9a8SJean-Christophe PLAGNIOL-VILLARD 
560676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
5612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
56314eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
5642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
5652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
5682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
569de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
57096820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
5712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
5722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
57398ae840aSRob Herring 		invalidate_dcache_range((unsigned long)&ctrl->qh_list,
574676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
57598ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qh,
57671c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
57798ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qtd,
5785cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
579b8adb120SMarek Vasut 
5802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
58114eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
5822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
58367333f76SStefan Roese 		WATCHDOG_RESET();
58496820a35SSimon Glass 	} while (get_timer(ts) < timeout);
58596820a35SSimon Glass 
586189a6956SIlya Yanok 	/*
587189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
588189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
589189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
590189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
591189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
592189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
593189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
594189a6956SIlya Yanok 	 */
595*1bee64cbSDirk Behme 	if (buffer != NULL && length > 0)
59698ae840aSRob Herring 		invalidate_dcache_range((unsigned long)buffer,
59798ae840aSRob Herring 			ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
598b8adb120SMarek Vasut 
59996820a35SSimon Glass 	/* Check that the TD processing happened */
60014eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
60196820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
6022731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
604676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
6052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
606676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
6072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
608676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
6092731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
61114eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
6122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
61571c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
61614eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
6172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
61814eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
61914eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
6202731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
62114eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
6222731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
6232731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
6242731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
6252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
62614eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
6272731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
6282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
62914eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
63014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
6312731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
6322731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
63314eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
63414eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
6352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
6362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
63914eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
640222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
6412731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
64314eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
6442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
6452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
646e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
6472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
648676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
649676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
650676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
651e82a316dSKuo-Jung Su #endif
6522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6545cec214eSBenoît Thébaudeau 	free(qtd);
6552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
6562731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6572731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
6585cec214eSBenoît Thébaudeau 	free(qtd);
6592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
6602731b9a8SJean-Christophe PLAGNIOL-VILLARD }
6612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
66224ed894fSSimon Glass static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
66324ed894fSSimon Glass 			    void *buffer, int length, struct devrequest *req)
6642731b9a8SJean-Christophe PLAGNIOL-VILLARD {
6652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
6662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
6672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
6682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
6692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
6702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
6717d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
67224ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
6732731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
6752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
6772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
6792731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
6802731b9a8SJean-Christophe PLAGNIOL-VILLARD 
68144259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
6822731b9a8SJean-Christophe PLAGNIOL-VILLARD 
68344259bb9SPrafulla Wadaskar 	switch (typeReq) {
6849c6a9d7cSKuo-Jung Su 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
6859c6a9d7cSKuo-Jung Su 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
6869c6a9d7cSKuo-Jung Su 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
687deb8508cSSimon Glass 		status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
6881dde1423SKuo-Jung Su 		if (!status_reg)
6899c6a9d7cSKuo-Jung Su 			return -1;
6909c6a9d7cSKuo-Jung Su 		break;
6919c6a9d7cSKuo-Jung Su 	default:
6929c6a9d7cSKuo-Jung Su 		status_reg = NULL;
6939c6a9d7cSKuo-Jung Su 		break;
6949c6a9d7cSKuo-Jung Su 	}
6959c6a9d7cSKuo-Jung Su 
6969c6a9d7cSKuo-Jung Su 	switch (typeReq) {
6972731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
6982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
6992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
7002731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
70214eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
7052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
7062731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
70714eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
70814eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
70914eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
7102731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
7132731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
7152731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
7172731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7182731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
7192731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
7202731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
7212731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7222731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
7232731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
7242731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
7252731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
7262731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
7272731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7292731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
7302731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
7312731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
7322731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7332731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7342731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
7402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7412731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
7422731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
7432731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
74414eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
7452731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7482731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
753676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
7542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
7712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
7722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
776c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
7782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
7802731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
782deb8508cSSimon Glass 			switch (ctrl->ops.get_port_speed(ctrl, reg)) {
78314eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
7842731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
78514eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
7862731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
7872731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
78814eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
7892731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7902731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7912731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7922731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
7942731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
7982731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
7992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
8002731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
8012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
8022731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
8037d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
8052731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
8082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
8122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
8162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
818676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
8192731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
8202731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8212731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8222731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8232731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
8242731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
8252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
8282731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
8297d9aa8fdSJulius Werner 				      port - 1);
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
8312731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
83245b9ea1dSHans de Goede 				return -ENXIO;
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
834c8b2d1dcSSergei Shtylyov 				int ret;
835c8b2d1dcSSergei Shtylyov 
8362731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
8372731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8392731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
844deb8508cSSimon Glass 				ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
8453874b6d6SMarek Vasut 
846b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
847c8b2d1dcSSergei Shtylyov 				/*
848c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
849c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
850c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
851c8b2d1dcSSergei Shtylyov 				 */
852c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
853c8b2d1dcSSergei Shtylyov 						2 * 1000);
85471b94526SHans de Goede 				if (!ret) {
85571b94526SHans de Goede 					reg = ehci_readl(status_reg);
85671b94526SHans de Goede 					if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
85771b94526SHans de Goede 					    == EHCI_PS_CS && !ehci_is_TDI()) {
85871b94526SHans de Goede 						debug("port %d full speed --> companion\n", port - 1);
85971b94526SHans de Goede 						reg &= ~EHCI_PS_CLEAR;
86071b94526SHans de Goede 						reg |= EHCI_PS_PO;
86171b94526SHans de Goede 						ehci_writel(status_reg, reg);
86245b9ea1dSHans de Goede 						return -ENXIO;
86371b94526SHans de Goede 					} else {
8647d9aa8fdSJulius Werner 						ctrl->portreset |= 1 << port;
86571b94526SHans de Goede 					}
86671b94526SHans de Goede 				} else {
867c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
8687d9aa8fdSJulius Werner 					       port - 1);
8692731b9a8SJean-Christophe PLAGNIOL-VILLARD 				}
87071b94526SHans de Goede 			}
8712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8727d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
8735077f96fSJulius Werner 			ehci_shutdown(ctrl);
8747d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
8757d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
8767d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
8777d9aa8fdSJulius Werner 			break;
8782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
8792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
8802731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
8812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
883676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
8842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
887ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
8882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8902731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
8912731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
893ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
8942731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
896676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
897ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
898ed10e66aSSimon Glass 			break;
8992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
900ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
9012731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
903ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
9042731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9052731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
9067d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
9072731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9092731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9102731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
9132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
914676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
9172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
9182731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
9192731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9202731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9215b84dd67SMike Frysinger 	mdelay(1);
922b4141195SMasahiro Yamada 	len = min3(srclen, (int)le16_to_cpu(req->length), length);
9232731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
9242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
9252731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
9262731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
9272731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9282731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
9292731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
9302731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9312731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9322731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
9332731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
9342731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
9352731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
9362731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9372731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
9382731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
9392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
9402731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
942121a4d13SMasahiro Yamada static const struct ehci_ops default_ehci_ops = {
943deb8508cSSimon Glass 	.set_usb_mode		= ehci_set_usbmode,
944deb8508cSSimon Glass 	.get_port_speed		= ehci_get_port_speed,
945deb8508cSSimon Glass 	.powerup_fixup		= ehci_powerup_fixup,
946deb8508cSSimon Glass 	.get_portsc_register	= ehci_get_portsc_register,
947deb8508cSSimon Glass };
948deb8508cSSimon Glass 
949deb8508cSSimon Glass static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
950c4a3141dSSimon Glass {
951deb8508cSSimon Glass 	if (!ops) {
952deb8508cSSimon Glass 		ctrl->ops = default_ehci_ops;
953deb8508cSSimon Glass 	} else {
954deb8508cSSimon Glass 		ctrl->ops = *ops;
955deb8508cSSimon Glass 		if (!ctrl->ops.set_usb_mode)
956deb8508cSSimon Glass 			ctrl->ops.set_usb_mode = ehci_set_usbmode;
957deb8508cSSimon Glass 		if (!ctrl->ops.get_port_speed)
958deb8508cSSimon Glass 			ctrl->ops.get_port_speed = ehci_get_port_speed;
959deb8508cSSimon Glass 		if (!ctrl->ops.powerup_fixup)
960deb8508cSSimon Glass 			ctrl->ops.powerup_fixup = ehci_powerup_fixup;
961deb8508cSSimon Glass 		if (!ctrl->ops.get_portsc_register)
962deb8508cSSimon Glass 			ctrl->ops.get_portsc_register =
963deb8508cSSimon Glass 					ehci_get_portsc_register;
964deb8508cSSimon Glass 	}
965deb8508cSSimon Glass }
966deb8508cSSimon Glass 
96746b01797SSimon Glass #ifndef CONFIG_DM_USB
968deb8508cSSimon Glass void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
969deb8508cSSimon Glass {
970deb8508cSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
971deb8508cSSimon Glass 
972deb8508cSSimon Glass 	ctrl->priv = priv;
973deb8508cSSimon Glass 	ehci_setup_ops(ctrl, ops);
974c4a3141dSSimon Glass }
975c4a3141dSSimon Glass 
976c4a3141dSSimon Glass void *ehci_get_controller_priv(int index)
977c4a3141dSSimon Glass {
978c4a3141dSSimon Glass 	return ehcic[index].priv;
979c4a3141dSSimon Glass }
98046b01797SSimon Glass #endif
981c4a3141dSSimon Glass 
9827372b5bdSSimon Glass static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
9832731b9a8SJean-Christophe PLAGNIOL-VILLARD {
984676ae068SLucas Stach 	struct QH *qh_list;
9858f62ca64SPatrick Georgi 	struct QH *periodic;
9867372b5bdSSimon Glass 	uint32_t reg;
9877372b5bdSSimon Glass 	uint32_t cmd;
9888f62ca64SPatrick Georgi 	int i;
9892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9902982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
9917372b5bdSSimon Glass 	if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
9927372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
9932731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9947372b5bdSSimon Glass 	qh_list = &ctrl->qh_list;
995676ae068SLucas Stach 
9962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
99771c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
998cf7c93cdSMarek Vasut 	qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH);
99914eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
100014eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
100171c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
100271c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
100314eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
100414eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
10052731b9a8SJean-Christophe PLAGNIOL-VILLARD 
100698ae840aSRob Herring 	flush_dcache_range((unsigned long)qh_list,
1007d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, qh_list, 1));
1008d3e07478SStephen Warren 
10098f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
1010cf7c93cdSMarek Vasut 	ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list));
10118f62ca64SPatrick Georgi 
10128f62ca64SPatrick Georgi 	/*
10138f62ca64SPatrick Georgi 	 * Set up periodic list
10148f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
10158f62ca64SPatrick Georgi 	 */
10167372b5bdSSimon Glass 	ctrl->periodic_schedules = 0;
10177372b5bdSSimon Glass 	periodic = &ctrl->periodic_queue;
10188f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
10198f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
10208f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
10218f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
10228f62ca64SPatrick Georgi 
102398ae840aSRob Herring 	flush_dcache_range((unsigned long)periodic,
1024d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, periodic, 1));
1025d3e07478SStephen Warren 
10268f62ca64SPatrick Georgi 	/*
10278f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
10288f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
10298f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
10308f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
10318f62ca64SPatrick Georgi 	 *
10328f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
10338f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
10348f62ca64SPatrick Georgi 	 */
10357372b5bdSSimon Glass 	if (ctrl->periodic_list == NULL)
10367372b5bdSSimon Glass 		ctrl->periodic_list = memalign(4096, 1024 * 4);
10378bc36036SNikita Kiryanov 
10387372b5bdSSimon Glass 	if (!ctrl->periodic_list)
10398f62ca64SPatrick Georgi 		return -ENOMEM;
10408f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
10417372b5bdSSimon Glass 		ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
1042ea427775SAdrian Cox 						| QH_LINK_TYPE_QH);
10438f62ca64SPatrick Georgi 	}
10448f62ca64SPatrick Georgi 
10457372b5bdSSimon Glass 	flush_dcache_range((unsigned long)ctrl->periodic_list,
10467372b5bdSSimon Glass 			   ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
1047d3e07478SStephen Warren 					  1024));
1048d3e07478SStephen Warren 
10498f62ca64SPatrick Georgi 	/* Set periodic list base address */
10507372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_periodiclistbase,
10517372b5bdSSimon Glass 		(unsigned long)ctrl->periodic_list);
10528f62ca64SPatrick Georgi 
10537372b5bdSSimon Glass 	reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
10542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
10557a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
10562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
10572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
105893ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
105993ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
10602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
10612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
106293ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
106393ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
10642731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
10667372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
10672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
10682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
10692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
10702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
10712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
10722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
10737372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
10742731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10757372b5bdSSimon Glass 	if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
10762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* take control over the ports */
10777372b5bdSSimon Glass 		cmd = ehci_readl(&ctrl->hcor->or_configflag);
10782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cmd |= FLAG_CF;
10797372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_configflag, cmd);
10807372b5bdSSimon Glass 	}
1081e82a316dSKuo-Jung Su 
10822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
10837372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
10845b84dd67SMike Frysinger 	mdelay(5);
10857372b5bdSSimon Glass 	reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
10862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
10872731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10887372b5bdSSimon Glass 	return 0;
10897372b5bdSSimon Glass }
10907372b5bdSSimon Glass 
109146b01797SSimon Glass #ifndef CONFIG_DM_USB
10927372b5bdSSimon Glass int usb_lowlevel_stop(int index)
10937372b5bdSSimon Glass {
10947372b5bdSSimon Glass 	ehci_shutdown(&ehcic[index]);
10957372b5bdSSimon Glass 	return ehci_hcd_stop(index);
10967372b5bdSSimon Glass }
10977372b5bdSSimon Glass 
10987372b5bdSSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
10997372b5bdSSimon Glass {
11007372b5bdSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
11017372b5bdSSimon Glass 	uint tweaks = 0;
11027372b5bdSSimon Glass 	int rc;
11037372b5bdSSimon Glass 
1104deb8508cSSimon Glass 	/**
1105deb8508cSSimon Glass 	 * Set ops to default_ehci_ops, ehci_hcd_init should call
1106deb8508cSSimon Glass 	 * ehci_set_controller_priv to change any of these function pointers.
1107deb8508cSSimon Glass 	 */
1108deb8508cSSimon Glass 	ctrl->ops = default_ehci_ops;
1109deb8508cSSimon Glass 
11107372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11117372b5bdSSimon Glass 	if (rc)
11127372b5bdSSimon Glass 		return rc;
111385b3e780SHeinrich Schuchardt 	if (!ctrl->hccr || !ctrl->hcor)
111485b3e780SHeinrich Schuchardt 		return -1;
11157372b5bdSSimon Glass 	if (init == USB_INIT_DEVICE)
11167372b5bdSSimon Glass 		goto done;
11177372b5bdSSimon Glass 
11187372b5bdSSimon Glass 	/* EHCI spec section 4.1 */
1119aeca43e3SSimon Glass 	if (ehci_reset(ctrl))
11207372b5bdSSimon Glass 		return -1;
11217372b5bdSSimon Glass 
11227372b5bdSSimon Glass #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
11237372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11247372b5bdSSimon Glass 	if (rc)
11257372b5bdSSimon Glass 		return rc;
11267372b5bdSSimon Glass #endif
11277372b5bdSSimon Glass #ifdef CONFIG_USB_EHCI_FARADAY
11287372b5bdSSimon Glass 	tweaks |= EHCI_TWEAK_NO_INIT_CF;
11297372b5bdSSimon Glass #endif
11307372b5bdSSimon Glass 	rc = ehci_common_init(ctrl, tweaks);
11317372b5bdSSimon Glass 	if (rc)
11327372b5bdSSimon Glass 		return rc;
11337372b5bdSSimon Glass 
11347372b5bdSSimon Glass 	ctrl->rootdev = 0;
1135127efc4fSTroy Kisky done:
1136676ae068SLucas Stach 	*controller = &ehcic[index];
11372731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
11382731b9a8SJean-Christophe PLAGNIOL-VILLARD }
113946b01797SSimon Glass #endif
11402731b9a8SJean-Christophe PLAGNIOL-VILLARD 
114124ed894fSSimon Glass static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
114224ed894fSSimon Glass 				 void *buffer, int length)
11432731b9a8SJean-Christophe PLAGNIOL-VILLARD {
11442731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
11462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
11502731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
115224ed894fSSimon Glass static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
115324ed894fSSimon Glass 				    void *buffer, int length,
115424ed894fSSimon Glass 				    struct devrequest *setup)
11552731b9a8SJean-Christophe PLAGNIOL-VILLARD {
115624ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
11572731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
11592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
11602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11622731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1163676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1164676ae068SLucas Stach 		if (!ctrl->rootdev)
11652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
11662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
11672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
11692731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11718f62ca64SPatrick Georgi struct int_queue {
11728aa26b8eSHans de Goede 	int elementsize;
11737f59d16aSHans de Goede 	unsigned long pipe;
11748f62ca64SPatrick Georgi 	struct QH *first;
11758f62ca64SPatrick Georgi 	struct QH *current;
11768f62ca64SPatrick Georgi 	struct QH *last;
11778f62ca64SPatrick Georgi 	struct qTD *tds;
11788f62ca64SPatrick Georgi };
11798f62ca64SPatrick Georgi 
118098ae840aSRob Herring #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
11818f62ca64SPatrick Georgi 
11828f62ca64SPatrick Georgi static int
11838f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
11848f62ca64SPatrick Georgi {
11858f62ca64SPatrick Georgi 	uint32_t cmd;
11868f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
11878f62ca64SPatrick Georgi 	int ret;
11888f62ca64SPatrick Georgi 
11898f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
11908f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
11918f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
11928f62ca64SPatrick Georgi 
11938f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
11948f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
11958f62ca64SPatrick Georgi 	if (ret < 0) {
11968f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
11978f62ca64SPatrick Georgi 		return -ETIMEDOUT;
11988f62ca64SPatrick Georgi 	}
11998f62ca64SPatrick Georgi 	udelay(1000);
12008f62ca64SPatrick Georgi 	return 0;
12018f62ca64SPatrick Georgi }
12028f62ca64SPatrick Georgi 
12038f62ca64SPatrick Georgi static int
12048f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
12058f62ca64SPatrick Georgi {
12068f62ca64SPatrick Georgi 	uint32_t cmd;
12078f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12088f62ca64SPatrick Georgi 	int ret;
12098f62ca64SPatrick Georgi 
12108f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12118f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
12128f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12138f62ca64SPatrick Georgi 
12148f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12158f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
12168f62ca64SPatrick Georgi 	if (ret < 0) {
12178f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
12188f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12198f62ca64SPatrick Georgi 	}
12208f62ca64SPatrick Georgi 	return 0;
12218f62ca64SPatrick Georgi }
12228f62ca64SPatrick Georgi 
1223029fd8eaSHans de Goede static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1224029fd8eaSHans de Goede 			unsigned long pipe, int queuesize, int elementsize,
1225029fd8eaSHans de Goede 			void *buffer, int interval)
12268f62ca64SPatrick Georgi {
122724ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
12288f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
12297f59d16aSHans de Goede 	uint32_t i, toggle;
12308f62ca64SPatrick Georgi 
1231bd818d81SHans de Goede 	/*
1232bd818d81SHans de Goede 	 * Interrupt transfers requiring several transactions are not supported
1233bd818d81SHans de Goede 	 * because bInterval is ignored.
1234bd818d81SHans de Goede 	 *
1235bd818d81SHans de Goede 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1236bd818d81SHans de Goede 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1237bd818d81SHans de Goede 	 * specification does not constrain this for interrupt transfers. That
1238bd818d81SHans de Goede 	 * means that ehci_submit_async() would support interrupt transfers
1239bd818d81SHans de Goede 	 * requiring several transactions only as long as the transfer size does
1240bd818d81SHans de Goede 	 * not require more than a single qTD.
1241bd818d81SHans de Goede 	 */
1242bd818d81SHans de Goede 	if (elementsize > usb_maxpacket(dev, pipe)) {
1243bd818d81SHans de Goede 		printf("%s: xfers requiring several transactions are not supported.\n",
1244bd818d81SHans de Goede 		       __func__);
1245bd818d81SHans de Goede 		return NULL;
1246bd818d81SHans de Goede 	}
1247bd818d81SHans de Goede 
12488f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
12498f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
12508f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
12518f62ca64SPatrick Georgi 		return NULL;
12528f62ca64SPatrick Georgi 	}
12538f62ca64SPatrick Georgi 
12548f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
12558f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
12568f62ca64SPatrick Georgi 	 * no matter the alignment
12578f62ca64SPatrick Georgi 	 */
12588f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
12598f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
12608f62ca64SPatrick Georgi 		return NULL;
12618f62ca64SPatrick Georgi 	}
12628f62ca64SPatrick Georgi 
12638f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
12648f62ca64SPatrick Georgi 	if (!result) {
12658f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12668f62ca64SPatrick Georgi 		goto fail1;
12678f62ca64SPatrick Georgi 	}
12688aa26b8eSHans de Goede 	result->elementsize = elementsize;
12697f59d16aSHans de Goede 	result->pipe = pipe;
12708165e34bSStephen Warren 	result->first = memalign(USB_DMA_MINALIGN,
12718165e34bSStephen Warren 				 sizeof(struct QH) * queuesize);
12728f62ca64SPatrick Georgi 	if (!result->first) {
12738f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12748f62ca64SPatrick Georgi 		goto fail2;
12758f62ca64SPatrick Georgi 	}
12768f62ca64SPatrick Georgi 	result->current = result->first;
12778f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
12788165e34bSStephen Warren 	result->tds = memalign(USB_DMA_MINALIGN,
12798165e34bSStephen Warren 			       sizeof(struct qTD) * queuesize);
12808f62ca64SPatrick Georgi 	if (!result->tds) {
12818f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12828f62ca64SPatrick Georgi 		goto fail3;
12838f62ca64SPatrick Georgi 	}
12848f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
12858f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
12868f62ca64SPatrick Georgi 
12877f59d16aSHans de Goede 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
12887f59d16aSHans de Goede 
12898f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
12908f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
12918f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
12928f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
12938f62ca64SPatrick Georgi 
129498ae840aSRob Herring 		qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
12958f62ca64SPatrick Georgi 		if (i == queuesize - 1)
1296ea427775SAdrian Cox 			qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
12978f62ca64SPatrick Georgi 
129898ae840aSRob Herring 		qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
1299ea427775SAdrian Cox 		qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1300ea427775SAdrian Cox 		qh->qh_endpt1 =
1301ea427775SAdrian Cox 			cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
13028f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
13038f62ca64SPatrick Georgi 			(1 << 14) |
13048f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
13058f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1306ea427775SAdrian Cox 			(usb_pipedevice(pipe) << 0));
1307ea427775SAdrian Cox 		qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1308ea427775SAdrian Cox 			(1 << 0)); /* S-mask: microframe 0 */
13098f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
13108f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
13114e2c4ad3SHans de Goede 			/* C-mask: microframes 2-4 */
13124e2c4ad3SHans de Goede 			qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
13138f62ca64SPatrick Georgi 		}
13144e2c4ad3SHans de Goede 		ehci_update_endpt2_dev_n_port(dev, qh);
13158f62ca64SPatrick Georgi 
1316ea427775SAdrian Cox 		td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1317ea427775SAdrian Cox 		td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
13188f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
13198f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
13207f59d16aSHans de Goede 		td->qt_token = cpu_to_hc32(
13217f59d16aSHans de Goede 			QT_TOKEN_DT(toggle) |
13227f59d16aSHans de Goede 			(elementsize << 16) |
13238f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1324ea427775SAdrian Cox 			0x80); /* active */
1325ea427775SAdrian Cox 		td->qt_buffer[0] =
132698ae840aSRob Herring 		    cpu_to_hc32((unsigned long)buffer + i * elementsize);
1327ea427775SAdrian Cox 		td->qt_buffer[1] =
1328ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1329ea427775SAdrian Cox 		td->qt_buffer[2] =
1330ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1331ea427775SAdrian Cox 		td->qt_buffer[3] =
1332ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1333ea427775SAdrian Cox 		td->qt_buffer[4] =
1334ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
13358f62ca64SPatrick Georgi 
13368f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
13377f59d16aSHans de Goede 		toggle ^= 1;
13388f62ca64SPatrick Georgi 	}
13398f62ca64SPatrick Georgi 
134098ae840aSRob Herring 	flush_dcache_range((unsigned long)buffer,
1341d3e07478SStephen Warren 			   ALIGN_END_ADDR(char, buffer,
1342d3e07478SStephen Warren 					  queuesize * elementsize));
134398ae840aSRob Herring 	flush_dcache_range((unsigned long)result->first,
1344d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->first,
1345d3e07478SStephen Warren 					  queuesize));
134698ae840aSRob Herring 	flush_dcache_range((unsigned long)result->tds,
1347d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct qTD, result->tds,
1348d3e07478SStephen Warren 					  queuesize));
1349d3e07478SStephen Warren 
135032f2eac1SHans de Goede 	if (ctrl->periodic_schedules > 0) {
13518f62ca64SPatrick Georgi 		if (disable_periodic(ctrl) < 0) {
13528f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
13538f62ca64SPatrick Georgi 			goto fail3;
13548f62ca64SPatrick Georgi 		}
135532f2eac1SHans de Goede 	}
13568f62ca64SPatrick Georgi 
13578f62ca64SPatrick Georgi 	/* hook up to periodic list */
13588f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
13598f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
136098ae840aSRob Herring 	list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
13618f62ca64SPatrick Georgi 
136298ae840aSRob Herring 	flush_dcache_range((unsigned long)result->last,
1363d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->last, 1));
136498ae840aSRob Herring 	flush_dcache_range((unsigned long)list,
1365d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, list, 1));
1366d3e07478SStephen Warren 
13678f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
13688f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
13698f62ca64SPatrick Georgi 		goto fail3;
13708f62ca64SPatrick Georgi 	}
137136b73109SHans de Goede 	ctrl->periodic_schedules++;
13728f62ca64SPatrick Georgi 
13738f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
13748f62ca64SPatrick Georgi 	return result;
13758f62ca64SPatrick Georgi fail3:
13768f62ca64SPatrick Georgi 	if (result->tds)
13778f62ca64SPatrick Georgi 		free(result->tds);
13788f62ca64SPatrick Georgi fail2:
13798f62ca64SPatrick Georgi 	if (result->first)
13808f62ca64SPatrick Georgi 		free(result->first);
13818f62ca64SPatrick Georgi 	if (result)
13828f62ca64SPatrick Georgi 		free(result);
13838f62ca64SPatrick Georgi fail1:
13848f62ca64SPatrick Georgi 	return NULL;
13858f62ca64SPatrick Georgi }
13868f62ca64SPatrick Georgi 
1387029fd8eaSHans de Goede static void *_ehci_poll_int_queue(struct usb_device *dev,
1388029fd8eaSHans de Goede 				  struct int_queue *queue)
13898f62ca64SPatrick Georgi {
13908f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
1391415548d8SHans de Goede 	struct qTD *cur_td;
13927f59d16aSHans de Goede 	uint32_t token, toggle;
13937f59d16aSHans de Goede 	unsigned long pipe = queue->pipe;
13948f62ca64SPatrick Georgi 
13958f62ca64SPatrick Georgi 	/* depleted queue */
13968f62ca64SPatrick Georgi 	if (cur == NULL) {
13978f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
13988f62ca64SPatrick Georgi 		return NULL;
13998f62ca64SPatrick Georgi 	}
14008f62ca64SPatrick Georgi 	/* still active */
1401415548d8SHans de Goede 	cur_td = &queue->tds[queue->current - queue->first];
140298ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur_td,
1403415548d8SHans de Goede 				ALIGN_END_ADDR(struct qTD, cur_td, 1));
14047f59d16aSHans de Goede 	token = hc32_to_cpu(cur_td->qt_token);
14057f59d16aSHans de Goede 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
14067f59d16aSHans de Goede 		debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
14078f62ca64SPatrick Georgi 		return NULL;
14088f62ca64SPatrick Georgi 	}
14097f59d16aSHans de Goede 
14107f59d16aSHans de Goede 	toggle = QT_TOKEN_GET_DT(token);
14117f59d16aSHans de Goede 	usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
14127f59d16aSHans de Goede 
14138f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
14148f62ca64SPatrick Georgi 		queue->current++;
14158f62ca64SPatrick Georgi 	else
14168f62ca64SPatrick Georgi 		queue->current = NULL;
14178aa26b8eSHans de Goede 
141898ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur->buffer,
14198aa26b8eSHans de Goede 				ALIGN_END_ADDR(char, cur->buffer,
14208aa26b8eSHans de Goede 					       queue->elementsize));
14218aa26b8eSHans de Goede 
1422415548d8SHans de Goede 	debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
14237f59d16aSHans de Goede 	      token, cur, queue->first);
14248f62ca64SPatrick Georgi 	return cur->buffer;
14258f62ca64SPatrick Georgi }
14268f62ca64SPatrick Georgi 
14278f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
1428029fd8eaSHans de Goede static int _ehci_destroy_int_queue(struct usb_device *dev,
1429029fd8eaSHans de Goede 				   struct int_queue *queue)
14308f62ca64SPatrick Georgi {
143124ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
14328f62ca64SPatrick Georgi 	int result = -1;
14338f62ca64SPatrick Georgi 	unsigned long timeout;
14348f62ca64SPatrick Georgi 
14358f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
14368f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14378f62ca64SPatrick Georgi 		goto out;
14388f62ca64SPatrick Georgi 	}
143936b73109SHans de Goede 	ctrl->periodic_schedules--;
14408f62ca64SPatrick Georgi 
14418f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
14428f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
1443ea427775SAdrian Cox 	while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
14448f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
14458f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
14468f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
14478f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
144898ae840aSRob Herring 			flush_dcache_range((unsigned long)cur,
1449ea7b30c5SHans de Goede 					   ALIGN_END_ADDR(struct QH, cur, 1));
14508f62ca64SPatrick Georgi 			result = 0;
14518f62ca64SPatrick Georgi 			break;
14528f62ca64SPatrick Georgi 		}
14538f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
14548f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14558f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
14568f62ca64SPatrick Georgi 			result = -1;
14578f62ca64SPatrick Georgi 			goto out;
14588f62ca64SPatrick Georgi 		}
14598f62ca64SPatrick Georgi 	}
14608f62ca64SPatrick Georgi 
146136b73109SHans de Goede 	if (ctrl->periodic_schedules > 0) {
14628f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
14638f62ca64SPatrick Georgi 		if (result < 0)
14648f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
14658f62ca64SPatrick Georgi 	}
14668f62ca64SPatrick Georgi 
14678f62ca64SPatrick Georgi out:
14688f62ca64SPatrick Georgi 	free(queue->tds);
14698f62ca64SPatrick Georgi 	free(queue->first);
14708f62ca64SPatrick Georgi 	free(queue);
14718f62ca64SPatrick Georgi 
14728f62ca64SPatrick Georgi 	return result;
14738f62ca64SPatrick Georgi }
14748f62ca64SPatrick Georgi 
147524ed894fSSimon Glass static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
147624ed894fSSimon Glass 				void *buffer, int length, int interval)
14772731b9a8SJean-Christophe PLAGNIOL-VILLARD {
14788f62ca64SPatrick Georgi 	void *backbuffer;
14798f62ca64SPatrick Georgi 	struct int_queue *queue;
14808f62ca64SPatrick Georgi 	unsigned long timeout;
14818f62ca64SPatrick Georgi 	int result = 0, ret;
14828f62ca64SPatrick Georgi 
14832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
14842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
148544ae0be7SBenoît Thébaudeau 
1486029fd8eaSHans de Goede 	queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
1487bd818d81SHans de Goede 	if (!queue)
1488bd818d81SHans de Goede 		return -1;
14898f62ca64SPatrick Georgi 
14908f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1491029fd8eaSHans de Goede 	while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
14928f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14938f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
14948f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
14958f62ca64SPatrick Georgi 			break;
14968f62ca64SPatrick Georgi 		}
14978f62ca64SPatrick Georgi 
14988f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
149998ae840aSRob Herring 		debug("got wrong buffer back (%p instead of %p)\n",
150098ae840aSRob Herring 		      backbuffer, buffer);
15018f62ca64SPatrick Georgi 		return -EINVAL;
15028f62ca64SPatrick Georgi 	}
15038f62ca64SPatrick Georgi 
1504029fd8eaSHans de Goede 	ret = _ehci_destroy_int_queue(dev, queue);
15058f62ca64SPatrick Georgi 	if (ret < 0)
15068f62ca64SPatrick Georgi 		return ret;
15078f62ca64SPatrick Georgi 
15088f62ca64SPatrick Georgi 	/* everything worked out fine */
15098f62ca64SPatrick Georgi 	return result;
15102731b9a8SJean-Christophe PLAGNIOL-VILLARD }
151124ed894fSSimon Glass 
151246b01797SSimon Glass #ifndef CONFIG_DM_USB
151324ed894fSSimon Glass int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
151424ed894fSSimon Glass 			    void *buffer, int length)
151524ed894fSSimon Glass {
151624ed894fSSimon Glass 	return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
151724ed894fSSimon Glass }
151824ed894fSSimon Glass 
151924ed894fSSimon Glass int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
152024ed894fSSimon Glass 		   int length, struct devrequest *setup)
152124ed894fSSimon Glass {
152224ed894fSSimon Glass 	return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
152324ed894fSSimon Glass }
152424ed894fSSimon Glass 
152524ed894fSSimon Glass int submit_int_msg(struct usb_device *dev, unsigned long pipe,
152624ed894fSSimon Glass 		   void *buffer, int length, int interval)
152724ed894fSSimon Glass {
152824ed894fSSimon Glass 	return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
152924ed894fSSimon Glass }
1530029fd8eaSHans de Goede 
1531029fd8eaSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev,
1532029fd8eaSHans de Goede 		unsigned long pipe, int queuesize, int elementsize,
1533029fd8eaSHans de Goede 		void *buffer, int interval)
1534029fd8eaSHans de Goede {
1535029fd8eaSHans de Goede 	return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1536029fd8eaSHans de Goede 				      buffer, interval);
1537029fd8eaSHans de Goede }
1538029fd8eaSHans de Goede 
1539029fd8eaSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1540029fd8eaSHans de Goede {
1541029fd8eaSHans de Goede 	return _ehci_poll_int_queue(dev, queue);
1542029fd8eaSHans de Goede }
1543029fd8eaSHans de Goede 
1544029fd8eaSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1545029fd8eaSHans de Goede {
1546029fd8eaSHans de Goede 	return _ehci_destroy_int_queue(dev, queue);
1547029fd8eaSHans de Goede }
154846b01797SSimon Glass #endif
154946b01797SSimon Glass 
155046b01797SSimon Glass #ifdef CONFIG_DM_USB
155146b01797SSimon Glass static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
155246b01797SSimon Glass 				   unsigned long pipe, void *buffer, int length,
155346b01797SSimon Glass 				   struct devrequest *setup)
155446b01797SSimon Glass {
155546b01797SSimon Glass 	debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
155646b01797SSimon Glass 	      dev->name, udev, udev->dev->name, udev->portnr);
155746b01797SSimon Glass 
155846b01797SSimon Glass 	return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
155946b01797SSimon Glass }
156046b01797SSimon Glass 
156146b01797SSimon Glass static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
156246b01797SSimon Glass 				unsigned long pipe, void *buffer, int length)
156346b01797SSimon Glass {
156446b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
156546b01797SSimon Glass 	return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
156646b01797SSimon Glass }
156746b01797SSimon Glass 
156846b01797SSimon Glass static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
156946b01797SSimon Glass 			       unsigned long pipe, void *buffer, int length,
157046b01797SSimon Glass 			       int interval)
157146b01797SSimon Glass {
157246b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
157346b01797SSimon Glass 	return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
157446b01797SSimon Glass }
157546b01797SSimon Glass 
15768a5f0665SHans de Goede static struct int_queue *ehci_create_int_queue(struct udevice *dev,
15778a5f0665SHans de Goede 		struct usb_device *udev, unsigned long pipe, int queuesize,
15788a5f0665SHans de Goede 		int elementsize, void *buffer, int interval)
15798a5f0665SHans de Goede {
15808a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
15818a5f0665SHans de Goede 	return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
15828a5f0665SHans de Goede 				      buffer, interval);
15838a5f0665SHans de Goede }
15848a5f0665SHans de Goede 
15858a5f0665SHans de Goede static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
15868a5f0665SHans de Goede 				 struct int_queue *queue)
15878a5f0665SHans de Goede {
15888a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
15898a5f0665SHans de Goede 	return _ehci_poll_int_queue(udev, queue);
15908a5f0665SHans de Goede }
15918a5f0665SHans de Goede 
15928a5f0665SHans de Goede static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
15938a5f0665SHans de Goede 				  struct int_queue *queue)
15948a5f0665SHans de Goede {
15958a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
15968a5f0665SHans de Goede 	return _ehci_destroy_int_queue(udev, queue);
15978a5f0665SHans de Goede }
15988a5f0665SHans de Goede 
159950293ea8SBin Meng static int ehci_get_max_xfer_size(struct udevice *dev, size_t *size)
160050293ea8SBin Meng {
160150293ea8SBin Meng 	/*
160250293ea8SBin Meng 	 * EHCD can handle any transfer length as long as there is enough
160350293ea8SBin Meng 	 * free heap space left, hence set the theoretical max number here.
160450293ea8SBin Meng 	 */
160550293ea8SBin Meng 	*size = SIZE_MAX;
160650293ea8SBin Meng 
160750293ea8SBin Meng 	return 0;
160850293ea8SBin Meng }
160950293ea8SBin Meng 
161046b01797SSimon Glass int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
161146b01797SSimon Glass 		  struct ehci_hcor *hcor, const struct ehci_ops *ops,
161246b01797SSimon Glass 		  uint tweaks, enum usb_init_type init)
161346b01797SSimon Glass {
1614cb8a2c14SHans de Goede 	struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
161546b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
161685b3e780SHeinrich Schuchardt 	int ret = -1;
161746b01797SSimon Glass 
161846b01797SSimon Glass 	debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
161946b01797SSimon Glass 	      dev->name, ctrl, hccr, hcor, init);
162046b01797SSimon Glass 
162185b3e780SHeinrich Schuchardt 	if (!ctrl || !hccr || !hcor)
162285b3e780SHeinrich Schuchardt 		goto err;
162385b3e780SHeinrich Schuchardt 
1624cb8a2c14SHans de Goede 	priv->desc_before_addr = true;
1625cb8a2c14SHans de Goede 
162646b01797SSimon Glass 	ehci_setup_ops(ctrl, ops);
162746b01797SSimon Glass 	ctrl->hccr = hccr;
162846b01797SSimon Glass 	ctrl->hcor = hcor;
162946b01797SSimon Glass 	ctrl->priv = ctrl;
163046b01797SSimon Glass 
163149b4c5c7SStephen Warren 	ctrl->init = init;
163249b4c5c7SStephen Warren 	if (ctrl->init == USB_INIT_DEVICE)
163346b01797SSimon Glass 		goto done;
163449b4c5c7SStephen Warren 
163546b01797SSimon Glass 	ret = ehci_reset(ctrl);
163646b01797SSimon Glass 	if (ret)
163746b01797SSimon Glass 		goto err;
163846b01797SSimon Glass 
1639cfb3f1cdSMateusz Kulikowski 	if (ctrl->ops.init_after_reset) {
1640cfb3f1cdSMateusz Kulikowski 		ret = ctrl->ops.init_after_reset(ctrl);
16413f9f8a5bSMateusz Kulikowski 		if (ret)
16423f9f8a5bSMateusz Kulikowski 			goto err;
16433f9f8a5bSMateusz Kulikowski 	}
16443f9f8a5bSMateusz Kulikowski 
164546b01797SSimon Glass 	ret = ehci_common_init(ctrl, tweaks);
164646b01797SSimon Glass 	if (ret)
164746b01797SSimon Glass 		goto err;
164846b01797SSimon Glass done:
164946b01797SSimon Glass 	return 0;
165046b01797SSimon Glass err:
165146b01797SSimon Glass 	free(ctrl);
165246b01797SSimon Glass 	debug("%s: failed, ret=%d\n", __func__, ret);
165346b01797SSimon Glass 	return ret;
165446b01797SSimon Glass }
165546b01797SSimon Glass 
165646b01797SSimon Glass int ehci_deregister(struct udevice *dev)
165746b01797SSimon Glass {
165846b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
165946b01797SSimon Glass 
166049b4c5c7SStephen Warren 	if (ctrl->init == USB_INIT_DEVICE)
166149b4c5c7SStephen Warren 		return 0;
166249b4c5c7SStephen Warren 
166346b01797SSimon Glass 	ehci_shutdown(ctrl);
166446b01797SSimon Glass 
166546b01797SSimon Glass 	return 0;
166646b01797SSimon Glass }
166746b01797SSimon Glass 
166846b01797SSimon Glass struct dm_usb_ops ehci_usb_ops = {
166946b01797SSimon Glass 	.control = ehci_submit_control_msg,
167046b01797SSimon Glass 	.bulk = ehci_submit_bulk_msg,
167146b01797SSimon Glass 	.interrupt = ehci_submit_int_msg,
16728a5f0665SHans de Goede 	.create_int_queue = ehci_create_int_queue,
16738a5f0665SHans de Goede 	.poll_int_queue = ehci_poll_int_queue,
16748a5f0665SHans de Goede 	.destroy_int_queue = ehci_destroy_int_queue,
167550293ea8SBin Meng 	.get_max_xfer_size  = ehci_get_max_xfer_size,
167646b01797SSimon Glass };
167746b01797SSimon Glass 
167846b01797SSimon Glass #endif
1679