xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision cf92e05c0135bc2b1a1b25a3218e31e6d79bad59)
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>
18*cf92e05cSSimon 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 */
552731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{},		/* Device removable */
562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{}		/* 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;
137b068deb3SJim Lin #endif
138b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
139b068deb3SJim Lin }
140b068deb3SJim Lin 
141deb8508cSSimon Glass static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
142727fce36SSimon Glass 			       uint32_t *reg)
1433874b6d6SMarek Vasut {
1443874b6d6SMarek Vasut 	mdelay(50);
1453874b6d6SMarek Vasut }
1463874b6d6SMarek Vasut 
147deb8508cSSimon Glass static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
148aac064f7SSimon Glass {
149aac064f7SSimon Glass 	if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
150aac064f7SSimon Glass 		/* Printing the message would cause a scan failure! */
151aac064f7SSimon Glass 		debug("The request port(%u) is not configured\n", port);
152aac064f7SSimon Glass 		return NULL;
153aac064f7SSimon Glass 	}
154aac064f7SSimon Glass 
1556a1a8162SSimon Glass 	return (uint32_t *)&ctrl->hcor->or_portsc[port];
156aac064f7SSimon Glass }
157aac064f7SSimon Glass 
1582731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1592731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
16309c83a45SWolfgang Denk 		udelay(5);
1642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 
174aeca43e3SSimon Glass static int ehci_reset(struct ehci_ctrl *ctrl)
1752731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
179aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
180273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
181aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
182aeca43e3SSimon Glass 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
183676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1872731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
1882731b9a8SJean-Christophe PLAGNIOL-VILLARD 
189b068deb3SJim Lin 	if (ehci_is_TDI())
190aeca43e3SSimon Glass 		ctrl->ops.set_usb_mode(ctrl);
1919ab4ce22SSimon Glass 
1929ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
193aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
19414eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
1959ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
196aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
1979ab4ce22SSimon Glass #endif
1982731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
1992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
2002731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2025077f96fSJulius Werner static int ehci_shutdown(struct ehci_ctrl *ctrl)
2035077f96fSJulius Werner {
2045077f96fSJulius Werner 	int i, ret = 0;
2055077f96fSJulius Werner 	uint32_t cmd, reg;
2065077f96fSJulius Werner 
2071e1be6d4SMarek Vasut 	if (!ctrl || !ctrl->hcor)
2081e1be6d4SMarek Vasut 		return -EINVAL;
2091e1be6d4SMarek Vasut 
2105077f96fSJulius Werner 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
2115077f96fSJulius Werner 	cmd &= ~(CMD_PSE | CMD_ASE);
2125077f96fSJulius Werner 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2135077f96fSJulius Werner 	ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
2145077f96fSJulius Werner 		100 * 1000);
2155077f96fSJulius Werner 
2165077f96fSJulius Werner 	if (!ret) {
2175077f96fSJulius Werner 		for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
2185077f96fSJulius Werner 			reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
2195077f96fSJulius Werner 			reg |= EHCI_PS_SUSP;
2205077f96fSJulius Werner 			ehci_writel(&ctrl->hcor->or_portsc[i], reg);
2215077f96fSJulius Werner 		}
2225077f96fSJulius Werner 
2235077f96fSJulius Werner 		cmd &= ~CMD_RUN;
2245077f96fSJulius Werner 		ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2255077f96fSJulius Werner 		ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
2265077f96fSJulius Werner 			HCHALT_TIMEOUT);
2275077f96fSJulius Werner 	}
2285077f96fSJulius Werner 
2295077f96fSJulius Werner 	if (ret)
2305077f96fSJulius Werner 		puts("EHCI failed to shut down host controller.\n");
2315077f96fSJulius Werner 
2325077f96fSJulius Werner 	return ret;
2335077f96fSJulius Werner }
2345077f96fSJulius Werner 
2352731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2362731b9a8SJean-Christophe PLAGNIOL-VILLARD {
237b8adb120SMarek Vasut 	uint32_t delta, next;
23898ae840aSRob Herring 	uint32_t addr = (unsigned long)buf;
2392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2402731b9a8SJean-Christophe PLAGNIOL-VILLARD 
241189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
242b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
243b8adb120SMarek Vasut 
244189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
245189a6956SIlya Yanok 
2462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
247cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
2482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		td->qt_buffer[idx] = cpu_to_hc32(addr);
2493ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
25014eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2532731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2552731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2582731b9a8SJean-Christophe PLAGNIOL-VILLARD 
259cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
26098ae840aSRob Herring 		printf("out of buffer pointers (%zu bytes left)\n", sz);
2612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2632731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2652731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
267c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
268c60795f4SIlya Yanok {
269c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
270c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
271c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
272c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
273c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
274c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
275c60795f4SIlya Yanok 		return QH_LOW_SPEED;
276c60795f4SIlya Yanok 	return QH_FULL_SPEED;
277c60795f4SIlya Yanok }
278c60795f4SIlya Yanok 
27946b01797SSimon Glass static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
2804e2c4ad3SHans de Goede 					  struct QH *qh)
2814e2c4ad3SHans de Goede {
2824e2c4ad3SHans de Goede 	struct usb_device *ttdev;
28346b01797SSimon Glass 	int parent_devnum;
2844e2c4ad3SHans de Goede 
28546b01797SSimon Glass 	if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
2864e2c4ad3SHans de Goede 		return;
2874e2c4ad3SHans de Goede 
2884e2c4ad3SHans de Goede 	/*
2894e2c4ad3SHans de Goede 	 * For full / low speed devices we need to get the devnum and portnr of
2904e2c4ad3SHans de Goede 	 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
2914e2c4ad3SHans de Goede 	 * in the tree before that one!
2924e2c4ad3SHans de Goede 	 */
29346b01797SSimon Glass #ifdef CONFIG_DM_USB
294fcdd8aaaSHans de Goede 	/*
295fcdd8aaaSHans de Goede 	 * When called from usb-uclass.c: usb_scan_device() udev->dev points
296fcdd8aaaSHans de Goede 	 * to the parent udevice, not the actual udevice belonging to the
297fcdd8aaaSHans de Goede 	 * udev as the device is not instantiated yet. So when searching
298fcdd8aaaSHans de Goede 	 * for the first usb-2 parent start with udev->dev not
299fcdd8aaaSHans de Goede 	 * udev->dev->parent .
300fcdd8aaaSHans de Goede 	 */
30146b01797SSimon Glass 	struct udevice *parent;
302fcdd8aaaSHans de Goede 	struct usb_device *uparent;
30346b01797SSimon Glass 
304fcdd8aaaSHans de Goede 	ttdev = udev;
305fcdd8aaaSHans de Goede 	parent = udev->dev;
306fcdd8aaaSHans de Goede 	uparent = dev_get_parentdata(parent);
30746b01797SSimon Glass 
308fcdd8aaaSHans de Goede 	while (uparent->speed != USB_SPEED_HIGH) {
309fcdd8aaaSHans de Goede 		struct udevice *dev = parent;
310fcdd8aaaSHans de Goede 
311fcdd8aaaSHans de Goede 		if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) {
31219df0bc1SSimon Glass 			printf("ehci: Error cannot find high-speed parent of usb-1 device\n");
31346b01797SSimon Glass 			return;
31446b01797SSimon Glass 		}
315fcdd8aaaSHans de Goede 
316fcdd8aaaSHans de Goede 		ttdev = dev_get_parentdata(dev);
317fcdd8aaaSHans de Goede 		parent = dev->parent;
318fcdd8aaaSHans de Goede 		uparent = dev_get_parentdata(parent);
319fcdd8aaaSHans de Goede 	}
320fcdd8aaaSHans de Goede 	parent_devnum = uparent->devnum;
32146b01797SSimon Glass #else
32246b01797SSimon Glass 	ttdev = udev;
3234e2c4ad3SHans de Goede 	while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
3244e2c4ad3SHans de Goede 		ttdev = ttdev->parent;
3254e2c4ad3SHans de Goede 	if (!ttdev->parent)
3264e2c4ad3SHans de Goede 		return;
32746b01797SSimon Glass 	parent_devnum = ttdev->parent->devnum;
32846b01797SSimon Glass #endif
3294e2c4ad3SHans de Goede 
3304e2c4ad3SHans de Goede 	qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
33146b01797SSimon Glass 				     QH_ENDPT2_HUBADDR(parent_devnum));
3324e2c4ad3SHans de Goede }
3334e2c4ad3SHans de Goede 
3342731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
3352731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
3362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
3372731b9a8SJean-Christophe PLAGNIOL-VILLARD {
33871c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
3395cec214eSBenoît Thébaudeau 	struct qTD *qtd;
3405cec214eSBenoît Thébaudeau 	int qtd_count = 0;
341de98e8b2SMarek Vasut 	int qtd_counter = 0;
3422731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
3432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
3442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
345db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
3462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
3472731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
34896820a35SSimon Glass 	int timeout;
3492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
35024ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
3512731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
3532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
3542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
3552731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
3562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
3572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
3582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
3592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
3602731b9a8SJean-Christophe PLAGNIOL-VILLARD 
361db191346SBenoît Thébaudeau #define PKT_ALIGN	512
3625cec214eSBenoît Thébaudeau 	/*
3635cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
3645cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
3655cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
3665cec214eSBenoît Thébaudeau 	 *
3675cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
3685cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
3695cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
3705cec214eSBenoît Thébaudeau 	 *
3715cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
3725cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
3735cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
3745cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
3755cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
3765cec214eSBenoît Thébaudeau 	 */
3775cec214eSBenoît Thébaudeau 	if (req != NULL)
3785cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
3795cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
3805cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
3815cec214eSBenoît Thébaudeau 		/*
3825cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
383db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
384db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
385db191346SBenoît Thébaudeau 		 * shorter).
3865cec214eSBenoît Thébaudeau 		 *
3875cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
388db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
389db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
390db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
3915cec214eSBenoît Thébaudeau 		 *
392db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
3935cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
3945cec214eSBenoît Thébaudeau 		 */
3955cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
3965cec214eSBenoît Thébaudeau 		/*
397db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
398db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
3995cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
4005cec214eSBenoît Thébaudeau 		 */
40198ae840aSRob Herring 		if ((unsigned long)buffer & (PKT_ALIGN - 1))
4025cec214eSBenoît Thébaudeau 			xfr_sz--;
4035cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
4045cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
4055cec214eSBenoît Thébaudeau 		/*
406db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
407db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
408db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
409db191346SBenoît Thébaudeau 		 * bytes.
4105cec214eSBenoît Thébaudeau 		 */
411db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
4125cec214eSBenoît Thébaudeau 	}
4135cec214eSBenoît Thébaudeau /*
414db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
415db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
4165cec214eSBenoît Thébaudeau  */
417db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
4185cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
4195cec214eSBenoît Thébaudeau #endif
4205cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
4215cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
4225cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
4235cec214eSBenoît Thébaudeau 		return -1;
4245cec214eSBenoît Thébaudeau 	}
4255cec214eSBenoît Thébaudeau 
42671c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
4275cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
428de98e8b2SMarek Vasut 
429b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
430b8adb120SMarek Vasut 
43141b1f0acSMarek Vasut 	/*
43241b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
43341b1f0acSMarek Vasut 	 *
43441b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
43541b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
43641b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
43741b1f0acSMarek Vasut 	 * - qh_curtd
43841b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
43941b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
44041b1f0acSMarek Vasut 	 */
44198ae840aSRob Herring 	qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
442c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
443db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
44414eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
445db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
44614eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
447c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
44814eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
44914eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
45071c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
4514e2c4ad3SHans de Goede 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
45271c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
4534e2c4ad3SHans de Goede 	ehci_update_endpt2_dev_n_port(dev, qh);
45471c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
4552456b97fSStephen Warren 	qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
4562731b9a8SJean-Christophe PLAGNIOL-VILLARD 
45771c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
4582731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
46041b1f0acSMarek Vasut 		/*
46141b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
46241b1f0acSMarek Vasut 		 *
46341b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
46441b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
46541b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
46641b1f0acSMarek Vasut 		 *
46741b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
46841b1f0acSMarek Vasut 		 */
469de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
470de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
47114eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
47214eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
47314eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
47414eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
475de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
47614eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
47714eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
4782731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
4792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
48041b1f0acSMarek Vasut 		/* Update previous qTD! */
48198ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
482de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
4842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
4875cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
4885cec214eSBenoît Thébaudeau 		int left_length = length;
4895cec214eSBenoît Thébaudeau 
4905cec214eSBenoît Thébaudeau 		do {
4915cec214eSBenoît Thébaudeau 			/*
4925cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
4935cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
4945cec214eSBenoît Thébaudeau 			 */
4955cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
4965cec214eSBenoît Thébaudeau 			/*
4975cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
4985cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
4995cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
5005cec214eSBenoît Thébaudeau 			 */
50198ae840aSRob Herring 			xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
5025cec214eSBenoît Thébaudeau 			/*
5035cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
504db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
5055cec214eSBenoît Thébaudeau 			 */
506db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
5075cec214eSBenoît Thébaudeau 			/*
5085cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
5095cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
5105cec214eSBenoît Thébaudeau 			 */
5115cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
5125cec214eSBenoît Thébaudeau 
51341b1f0acSMarek Vasut 			/*
51441b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
51541b1f0acSMarek Vasut 			 *
51641b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
51741b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
51841b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
51941b1f0acSMarek Vasut 			 *
52041b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
52141b1f0acSMarek Vasut 			 */
5225cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
5235cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5245cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
5255cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5265cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
5275cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
52814eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
5295cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
5305cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
53114eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
53214eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
533de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
5345cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
5355cec214eSBenoît Thébaudeau 						xfr_bytes)) {
53614eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
5372731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
5382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
53941b1f0acSMarek Vasut 			/* Update previous qTD! */
54098ae840aSRob Herring 			*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
541de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
542db191346SBenoît Thébaudeau 			/*
543db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
544db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
545db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
546db191346SBenoît Thébaudeau 			 */
547db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
548db191346SBenoît Thébaudeau 				toggle ^= 1;
5495cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
5505cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
5515cec214eSBenoît Thébaudeau 		} while (left_length > 0);
5522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
55541b1f0acSMarek Vasut 		/*
55641b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
55741b1f0acSMarek Vasut 		 *
55841b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
55941b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
56041b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
56141b1f0acSMarek Vasut 		 */
562de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
563de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
564db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
56514eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
56614eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
56714eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
56814eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
569de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
57041b1f0acSMarek Vasut 		/* Update previous qTD! */
57198ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
572de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
5732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5742731b9a8SJean-Christophe PLAGNIOL-VILLARD 
57598ae840aSRob Herring 	ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
5762731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
57898ae840aSRob Herring 	flush_dcache_range((unsigned long)&ctrl->qh_list,
579676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
58098ae840aSRob Herring 	flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
58198ae840aSRob Herring 	flush_dcache_range((unsigned long)qtd,
5825cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
5832731b9a8SJean-Christophe PLAGNIOL-VILLARD 
584c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
58598ae840aSRob Herring 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
586c7701af5SIlya Yanok 
587676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
588676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
5892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
591676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
593676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5942731b9a8SJean-Christophe PLAGNIOL-VILLARD 
595676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
5962731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5972731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
59814eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
5992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
6032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
604de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
60596820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
6062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
6072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
60898ae840aSRob Herring 		invalidate_dcache_range((unsigned long)&ctrl->qh_list,
609676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
61098ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qh,
61171c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
61298ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qtd,
6135cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
614b8adb120SMarek Vasut 
6152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
61614eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
6172731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
61867333f76SStefan Roese 		WATCHDOG_RESET();
61996820a35SSimon Glass 	} while (get_timer(ts) < timeout);
62096820a35SSimon Glass 
621189a6956SIlya Yanok 	/*
622189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
623189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
624189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
625189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
626189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
627189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
628189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
629189a6956SIlya Yanok 	 */
63098ae840aSRob Herring 	invalidate_dcache_range((unsigned long)buffer,
63198ae840aSRob Herring 		ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
632b8adb120SMarek Vasut 
63396820a35SSimon Glass 	/* Check that the TD processing happened */
63414eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
63596820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
6362731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6372731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
638676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
6392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
640676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
6412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
642676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
6432731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
64514eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
6462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6472731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6482731b9a8SJean-Christophe PLAGNIOL-VILLARD 
64971c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
65014eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
6512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
65214eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
65314eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
6542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
65514eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
6562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
6572731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
6582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
6592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
6612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
6622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66314eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
66414eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
6652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
6662731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66714eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
66814eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
6692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
6702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6722731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
67314eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
674222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
6752731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
67714eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
6792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
680e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
6812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
682676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
683676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
684676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
685e82a316dSKuo-Jung Su #endif
6862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6872731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6885cec214eSBenoît Thébaudeau 	free(qtd);
6892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
6902731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6912731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
6925cec214eSBenoît Thébaudeau 	free(qtd);
6932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
6942731b9a8SJean-Christophe PLAGNIOL-VILLARD }
6952731b9a8SJean-Christophe PLAGNIOL-VILLARD 
69624ed894fSSimon Glass static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
69724ed894fSSimon Glass 			    void *buffer, int length, struct devrequest *req)
6982731b9a8SJean-Christophe PLAGNIOL-VILLARD {
6992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
7002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
7022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
7042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
7057d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
70624ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
7072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
7092731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
7112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
7132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
71544259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
71744259bb9SPrafulla Wadaskar 	switch (typeReq) {
7189c6a9d7cSKuo-Jung Su 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7199c6a9d7cSKuo-Jung Su 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
7209c6a9d7cSKuo-Jung Su 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
721deb8508cSSimon Glass 		status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
7221dde1423SKuo-Jung Su 		if (!status_reg)
7239c6a9d7cSKuo-Jung Su 			return -1;
7249c6a9d7cSKuo-Jung Su 		break;
7259c6a9d7cSKuo-Jung Su 	default:
7269c6a9d7cSKuo-Jung Su 		status_reg = NULL;
7279c6a9d7cSKuo-Jung Su 		break;
7289c6a9d7cSKuo-Jung Su 	}
7299c6a9d7cSKuo-Jung Su 
7309c6a9d7cSKuo-Jung Su 	switch (typeReq) {
7312731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
7322731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
7342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
7352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
73614eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
7372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
7392731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
7402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
74114eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
74214eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
74314eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
7442731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
7462731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
7482731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
7532731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
7542731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
7552731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
7662731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
7762731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
77814eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7802731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
7862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
787676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
7882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
7902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
7912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
7922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
7942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
7952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
7982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
8002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
8012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
8032731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
8052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
8082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
810c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
8122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
816deb8508cSSimon Glass 			switch (ctrl->ops.get_port_speed(ctrl, reg)) {
81714eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
8182731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
81914eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
8202731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
8212731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
82214eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
8232731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
8242731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8252731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
8282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8292731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8312731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
8322731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
8342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
8352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
8362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
8377d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
8392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
8462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8482731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
8492731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
8502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
852676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
8532731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
8542731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8552731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8562731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
8582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
8592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
8602731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
8612731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
8622731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
8637d9aa8fdSJulius Werner 				      port - 1);
8642731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
8652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
86645b9ea1dSHans de Goede 				return -ENXIO;
8672731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
868c8b2d1dcSSergei Shtylyov 				int ret;
869c8b2d1dcSSergei Shtylyov 
8702731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
8712731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
8722731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8732731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
8742731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
8752731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
8762731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
8772731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
878deb8508cSSimon Glass 				ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
8793874b6d6SMarek Vasut 
880b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
881c8b2d1dcSSergei Shtylyov 				/*
882c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
883c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
884c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
885c8b2d1dcSSergei Shtylyov 				 */
886c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
887c8b2d1dcSSergei Shtylyov 						2 * 1000);
88871b94526SHans de Goede 				if (!ret) {
88971b94526SHans de Goede 					reg = ehci_readl(status_reg);
89071b94526SHans de Goede 					if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
89171b94526SHans de Goede 					    == EHCI_PS_CS && !ehci_is_TDI()) {
89271b94526SHans de Goede 						debug("port %d full speed --> companion\n", port - 1);
89371b94526SHans de Goede 						reg &= ~EHCI_PS_CLEAR;
89471b94526SHans de Goede 						reg |= EHCI_PS_PO;
89571b94526SHans de Goede 						ehci_writel(status_reg, reg);
89645b9ea1dSHans de Goede 						return -ENXIO;
89771b94526SHans de Goede 					} else {
8987d9aa8fdSJulius Werner 						ctrl->portreset |= 1 << port;
89971b94526SHans de Goede 					}
90071b94526SHans de Goede 				} else {
901c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
9027d9aa8fdSJulius Werner 					       port - 1);
9032731b9a8SJean-Christophe PLAGNIOL-VILLARD 				}
90471b94526SHans de Goede 			}
9052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9067d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
9075077f96fSJulius Werner 			ehci_shutdown(ctrl);
9087d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
9097d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
9107d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
9117d9aa8fdSJulius Werner 			break;
9122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9132731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9162731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
917676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9182731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9192731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
9202731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
921ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
9222731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
9232731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
9242731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
9252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9262731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
927ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
9282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9292731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
930676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
931ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
932ed10e66aSSimon Glass 			break;
9332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
934ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
9352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
937ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
9382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9392731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
9407d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
9412731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9432731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9442731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
9472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
948676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
9512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
9522731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
9532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9542731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9555b84dd67SMike Frysinger 	mdelay(1);
956b4141195SMasahiro Yamada 	len = min3(srclen, (int)le16_to_cpu(req->length), length);
9572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
9582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
9592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
9602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
9612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
9632731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
9642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9652731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9662731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
9672731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
9682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
9692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
9702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
9722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
9732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
9742731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
976deb8508cSSimon Glass const struct ehci_ops default_ehci_ops = {
977deb8508cSSimon Glass 	.set_usb_mode		= ehci_set_usbmode,
978deb8508cSSimon Glass 	.get_port_speed		= ehci_get_port_speed,
979deb8508cSSimon Glass 	.powerup_fixup		= ehci_powerup_fixup,
980deb8508cSSimon Glass 	.get_portsc_register	= ehci_get_portsc_register,
981deb8508cSSimon Glass };
982deb8508cSSimon Glass 
983deb8508cSSimon Glass static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
984c4a3141dSSimon Glass {
985deb8508cSSimon Glass 	if (!ops) {
986deb8508cSSimon Glass 		ctrl->ops = default_ehci_ops;
987deb8508cSSimon Glass 	} else {
988deb8508cSSimon Glass 		ctrl->ops = *ops;
989deb8508cSSimon Glass 		if (!ctrl->ops.set_usb_mode)
990deb8508cSSimon Glass 			ctrl->ops.set_usb_mode = ehci_set_usbmode;
991deb8508cSSimon Glass 		if (!ctrl->ops.get_port_speed)
992deb8508cSSimon Glass 			ctrl->ops.get_port_speed = ehci_get_port_speed;
993deb8508cSSimon Glass 		if (!ctrl->ops.powerup_fixup)
994deb8508cSSimon Glass 			ctrl->ops.powerup_fixup = ehci_powerup_fixup;
995deb8508cSSimon Glass 		if (!ctrl->ops.get_portsc_register)
996deb8508cSSimon Glass 			ctrl->ops.get_portsc_register =
997deb8508cSSimon Glass 					ehci_get_portsc_register;
998deb8508cSSimon Glass 	}
999deb8508cSSimon Glass }
1000deb8508cSSimon Glass 
100146b01797SSimon Glass #ifndef CONFIG_DM_USB
1002deb8508cSSimon Glass void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
1003deb8508cSSimon Glass {
1004deb8508cSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
1005deb8508cSSimon Glass 
1006deb8508cSSimon Glass 	ctrl->priv = priv;
1007deb8508cSSimon Glass 	ehci_setup_ops(ctrl, ops);
1008c4a3141dSSimon Glass }
1009c4a3141dSSimon Glass 
1010c4a3141dSSimon Glass void *ehci_get_controller_priv(int index)
1011c4a3141dSSimon Glass {
1012c4a3141dSSimon Glass 	return ehcic[index].priv;
1013c4a3141dSSimon Glass }
101446b01797SSimon Glass #endif
1015c4a3141dSSimon Glass 
10167372b5bdSSimon Glass static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
10172731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1018676ae068SLucas Stach 	struct QH *qh_list;
10198f62ca64SPatrick Georgi 	struct QH *periodic;
10207372b5bdSSimon Glass 	uint32_t reg;
10217372b5bdSSimon Glass 	uint32_t cmd;
10228f62ca64SPatrick Georgi 	int i;
10232731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10242982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
10257372b5bdSSimon Glass 	if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
10267372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
10272731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10287372b5bdSSimon Glass 	qh_list = &ctrl->qh_list;
1029676ae068SLucas Stach 
10302731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
103171c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
103298ae840aSRob Herring 	qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
103314eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
103414eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
103571c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
103671c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
103714eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
103814eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
10392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
104098ae840aSRob Herring 	flush_dcache_range((unsigned long)qh_list,
1041d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, qh_list, 1));
1042d3e07478SStephen Warren 
10438f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
10447372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)qh_list);
10458f62ca64SPatrick Georgi 
10468f62ca64SPatrick Georgi 	/*
10478f62ca64SPatrick Georgi 	 * Set up periodic list
10488f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
10498f62ca64SPatrick Georgi 	 */
10507372b5bdSSimon Glass 	ctrl->periodic_schedules = 0;
10517372b5bdSSimon Glass 	periodic = &ctrl->periodic_queue;
10528f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
10538f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
10548f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
10558f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
10568f62ca64SPatrick Georgi 
105798ae840aSRob Herring 	flush_dcache_range((unsigned long)periodic,
1058d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, periodic, 1));
1059d3e07478SStephen Warren 
10608f62ca64SPatrick Georgi 	/*
10618f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
10628f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
10638f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
10648f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
10658f62ca64SPatrick Georgi 	 *
10668f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
10678f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
10688f62ca64SPatrick Georgi 	 */
10697372b5bdSSimon Glass 	if (ctrl->periodic_list == NULL)
10707372b5bdSSimon Glass 		ctrl->periodic_list = memalign(4096, 1024 * 4);
10718bc36036SNikita Kiryanov 
10727372b5bdSSimon Glass 	if (!ctrl->periodic_list)
10738f62ca64SPatrick Georgi 		return -ENOMEM;
10748f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
10757372b5bdSSimon Glass 		ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
1076ea427775SAdrian Cox 						| QH_LINK_TYPE_QH);
10778f62ca64SPatrick Georgi 	}
10788f62ca64SPatrick Georgi 
10797372b5bdSSimon Glass 	flush_dcache_range((unsigned long)ctrl->periodic_list,
10807372b5bdSSimon Glass 			   ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
1081d3e07478SStephen Warren 					  1024));
1082d3e07478SStephen Warren 
10838f62ca64SPatrick Georgi 	/* Set periodic list base address */
10847372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_periodiclistbase,
10857372b5bdSSimon Glass 		(unsigned long)ctrl->periodic_list);
10868f62ca64SPatrick Georgi 
10877372b5bdSSimon Glass 	reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
10882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
10897a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
10902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
10912731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
109293ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
109393ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
10942731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
10952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
109693ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
109793ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
10982731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
11007372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
11012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
11022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
11032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
11042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
11052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
11062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
11077372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
11082731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11097372b5bdSSimon Glass 	if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
11102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* take control over the ports */
11117372b5bdSSimon Glass 		cmd = ehci_readl(&ctrl->hcor->or_configflag);
11122731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cmd |= FLAG_CF;
11137372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_configflag, cmd);
11147372b5bdSSimon Glass 	}
1115e82a316dSKuo-Jung Su 
11162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
11177372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
11185b84dd67SMike Frysinger 	mdelay(5);
11197372b5bdSSimon Glass 	reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
11202731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
11212731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11227372b5bdSSimon Glass 	return 0;
11237372b5bdSSimon Glass }
11247372b5bdSSimon Glass 
112546b01797SSimon Glass #ifndef CONFIG_DM_USB
11267372b5bdSSimon Glass int usb_lowlevel_stop(int index)
11277372b5bdSSimon Glass {
11287372b5bdSSimon Glass 	ehci_shutdown(&ehcic[index]);
11297372b5bdSSimon Glass 	return ehci_hcd_stop(index);
11307372b5bdSSimon Glass }
11317372b5bdSSimon Glass 
11327372b5bdSSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
11337372b5bdSSimon Glass {
11347372b5bdSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
11357372b5bdSSimon Glass 	uint tweaks = 0;
11367372b5bdSSimon Glass 	int rc;
11377372b5bdSSimon Glass 
1138deb8508cSSimon Glass 	/**
1139deb8508cSSimon Glass 	 * Set ops to default_ehci_ops, ehci_hcd_init should call
1140deb8508cSSimon Glass 	 * ehci_set_controller_priv to change any of these function pointers.
1141deb8508cSSimon Glass 	 */
1142deb8508cSSimon Glass 	ctrl->ops = default_ehci_ops;
1143deb8508cSSimon Glass 
11447372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11457372b5bdSSimon Glass 	if (rc)
11467372b5bdSSimon Glass 		return rc;
11477372b5bdSSimon Glass 	if (init == USB_INIT_DEVICE)
11487372b5bdSSimon Glass 		goto done;
11497372b5bdSSimon Glass 
11507372b5bdSSimon Glass 	/* EHCI spec section 4.1 */
1151aeca43e3SSimon Glass 	if (ehci_reset(ctrl))
11527372b5bdSSimon Glass 		return -1;
11537372b5bdSSimon Glass 
11547372b5bdSSimon Glass #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
11557372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11567372b5bdSSimon Glass 	if (rc)
11577372b5bdSSimon Glass 		return rc;
11587372b5bdSSimon Glass #endif
11597372b5bdSSimon Glass #ifdef CONFIG_USB_EHCI_FARADAY
11607372b5bdSSimon Glass 	tweaks |= EHCI_TWEAK_NO_INIT_CF;
11617372b5bdSSimon Glass #endif
11627372b5bdSSimon Glass 	rc = ehci_common_init(ctrl, tweaks);
11637372b5bdSSimon Glass 	if (rc)
11647372b5bdSSimon Glass 		return rc;
11657372b5bdSSimon Glass 
11667372b5bdSSimon Glass 	ctrl->rootdev = 0;
1167127efc4fSTroy Kisky done:
1168676ae068SLucas Stach 	*controller = &ehcic[index];
11692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
11702731b9a8SJean-Christophe PLAGNIOL-VILLARD }
117146b01797SSimon Glass #endif
11722731b9a8SJean-Christophe PLAGNIOL-VILLARD 
117324ed894fSSimon Glass static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
117424ed894fSSimon Glass 				 void *buffer, int length)
11752731b9a8SJean-Christophe PLAGNIOL-VILLARD {
11762731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
11782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11802731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
11822731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11832731b9a8SJean-Christophe PLAGNIOL-VILLARD 
118424ed894fSSimon Glass static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
118524ed894fSSimon Glass 				    void *buffer, int length,
118624ed894fSSimon Glass 				    struct devrequest *setup)
11872731b9a8SJean-Christophe PLAGNIOL-VILLARD {
118824ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
11892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
11912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
11922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11942731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1195676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1196676ae068SLucas Stach 		if (!ctrl->rootdev)
11972731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
11982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
11992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
12002731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
12012731b9a8SJean-Christophe PLAGNIOL-VILLARD }
12022731b9a8SJean-Christophe PLAGNIOL-VILLARD 
12038f62ca64SPatrick Georgi struct int_queue {
12048aa26b8eSHans de Goede 	int elementsize;
12057f59d16aSHans de Goede 	unsigned long pipe;
12068f62ca64SPatrick Georgi 	struct QH *first;
12078f62ca64SPatrick Georgi 	struct QH *current;
12088f62ca64SPatrick Georgi 	struct QH *last;
12098f62ca64SPatrick Georgi 	struct qTD *tds;
12108f62ca64SPatrick Georgi };
12118f62ca64SPatrick Georgi 
121298ae840aSRob Herring #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
12138f62ca64SPatrick Georgi 
12148f62ca64SPatrick Georgi static int
12158f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
12168f62ca64SPatrick Georgi {
12178f62ca64SPatrick Georgi 	uint32_t cmd;
12188f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12198f62ca64SPatrick Georgi 	int ret;
12208f62ca64SPatrick Georgi 
12218f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12228f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
12238f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12248f62ca64SPatrick Georgi 
12258f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12268f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
12278f62ca64SPatrick Georgi 	if (ret < 0) {
12288f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
12298f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12308f62ca64SPatrick Georgi 	}
12318f62ca64SPatrick Georgi 	udelay(1000);
12328f62ca64SPatrick Georgi 	return 0;
12338f62ca64SPatrick Georgi }
12348f62ca64SPatrick Georgi 
12358f62ca64SPatrick Georgi static int
12368f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
12378f62ca64SPatrick Georgi {
12388f62ca64SPatrick Georgi 	uint32_t cmd;
12398f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12408f62ca64SPatrick Georgi 	int ret;
12418f62ca64SPatrick Georgi 
12428f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12438f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
12448f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12458f62ca64SPatrick Georgi 
12468f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12478f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
12488f62ca64SPatrick Georgi 	if (ret < 0) {
12498f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
12508f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12518f62ca64SPatrick Georgi 	}
12528f62ca64SPatrick Georgi 	return 0;
12538f62ca64SPatrick Georgi }
12548f62ca64SPatrick Georgi 
1255029fd8eaSHans de Goede static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1256029fd8eaSHans de Goede 			unsigned long pipe, int queuesize, int elementsize,
1257029fd8eaSHans de Goede 			void *buffer, int interval)
12588f62ca64SPatrick Georgi {
125924ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
12608f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
12617f59d16aSHans de Goede 	uint32_t i, toggle;
12628f62ca64SPatrick Georgi 
1263bd818d81SHans de Goede 	/*
1264bd818d81SHans de Goede 	 * Interrupt transfers requiring several transactions are not supported
1265bd818d81SHans de Goede 	 * because bInterval is ignored.
1266bd818d81SHans de Goede 	 *
1267bd818d81SHans de Goede 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1268bd818d81SHans de Goede 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1269bd818d81SHans de Goede 	 * specification does not constrain this for interrupt transfers. That
1270bd818d81SHans de Goede 	 * means that ehci_submit_async() would support interrupt transfers
1271bd818d81SHans de Goede 	 * requiring several transactions only as long as the transfer size does
1272bd818d81SHans de Goede 	 * not require more than a single qTD.
1273bd818d81SHans de Goede 	 */
1274bd818d81SHans de Goede 	if (elementsize > usb_maxpacket(dev, pipe)) {
1275bd818d81SHans de Goede 		printf("%s: xfers requiring several transactions are not supported.\n",
1276bd818d81SHans de Goede 		       __func__);
1277bd818d81SHans de Goede 		return NULL;
1278bd818d81SHans de Goede 	}
1279bd818d81SHans de Goede 
12808f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
12818f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
12828f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
12838f62ca64SPatrick Georgi 		return NULL;
12848f62ca64SPatrick Georgi 	}
12858f62ca64SPatrick Georgi 
12868f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
12878f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
12888f62ca64SPatrick Georgi 	 * no matter the alignment
12898f62ca64SPatrick Georgi 	 */
12908f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
12918f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
12928f62ca64SPatrick Georgi 		return NULL;
12938f62ca64SPatrick Georgi 	}
12948f62ca64SPatrick Georgi 
12958f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
12968f62ca64SPatrick Georgi 	if (!result) {
12978f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12988f62ca64SPatrick Georgi 		goto fail1;
12998f62ca64SPatrick Georgi 	}
13008aa26b8eSHans de Goede 	result->elementsize = elementsize;
13017f59d16aSHans de Goede 	result->pipe = pipe;
13028165e34bSStephen Warren 	result->first = memalign(USB_DMA_MINALIGN,
13038165e34bSStephen Warren 				 sizeof(struct QH) * queuesize);
13048f62ca64SPatrick Georgi 	if (!result->first) {
13058f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13068f62ca64SPatrick Georgi 		goto fail2;
13078f62ca64SPatrick Georgi 	}
13088f62ca64SPatrick Georgi 	result->current = result->first;
13098f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
13108165e34bSStephen Warren 	result->tds = memalign(USB_DMA_MINALIGN,
13118165e34bSStephen Warren 			       sizeof(struct qTD) * queuesize);
13128f62ca64SPatrick Georgi 	if (!result->tds) {
13138f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13148f62ca64SPatrick Georgi 		goto fail3;
13158f62ca64SPatrick Georgi 	}
13168f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
13178f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
13188f62ca64SPatrick Georgi 
13197f59d16aSHans de Goede 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
13207f59d16aSHans de Goede 
13218f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
13228f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
13238f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
13248f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
13258f62ca64SPatrick Georgi 
132698ae840aSRob Herring 		qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
13278f62ca64SPatrick Georgi 		if (i == queuesize - 1)
1328ea427775SAdrian Cox 			qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
13298f62ca64SPatrick Georgi 
133098ae840aSRob Herring 		qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
1331ea427775SAdrian Cox 		qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1332ea427775SAdrian Cox 		qh->qh_endpt1 =
1333ea427775SAdrian Cox 			cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
13348f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
13358f62ca64SPatrick Georgi 			(1 << 14) |
13368f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
13378f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1338ea427775SAdrian Cox 			(usb_pipedevice(pipe) << 0));
1339ea427775SAdrian Cox 		qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1340ea427775SAdrian Cox 			(1 << 0)); /* S-mask: microframe 0 */
13418f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
13428f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
13434e2c4ad3SHans de Goede 			/* C-mask: microframes 2-4 */
13444e2c4ad3SHans de Goede 			qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
13458f62ca64SPatrick Georgi 		}
13464e2c4ad3SHans de Goede 		ehci_update_endpt2_dev_n_port(dev, qh);
13478f62ca64SPatrick Georgi 
1348ea427775SAdrian Cox 		td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1349ea427775SAdrian Cox 		td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
13508f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
13518f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
13527f59d16aSHans de Goede 		td->qt_token = cpu_to_hc32(
13537f59d16aSHans de Goede 			QT_TOKEN_DT(toggle) |
13547f59d16aSHans de Goede 			(elementsize << 16) |
13558f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1356ea427775SAdrian Cox 			0x80); /* active */
1357ea427775SAdrian Cox 		td->qt_buffer[0] =
135898ae840aSRob Herring 		    cpu_to_hc32((unsigned long)buffer + i * elementsize);
1359ea427775SAdrian Cox 		td->qt_buffer[1] =
1360ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1361ea427775SAdrian Cox 		td->qt_buffer[2] =
1362ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1363ea427775SAdrian Cox 		td->qt_buffer[3] =
1364ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1365ea427775SAdrian Cox 		td->qt_buffer[4] =
1366ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
13678f62ca64SPatrick Georgi 
13688f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
13697f59d16aSHans de Goede 		toggle ^= 1;
13708f62ca64SPatrick Georgi 	}
13718f62ca64SPatrick Georgi 
137298ae840aSRob Herring 	flush_dcache_range((unsigned long)buffer,
1373d3e07478SStephen Warren 			   ALIGN_END_ADDR(char, buffer,
1374d3e07478SStephen Warren 					  queuesize * elementsize));
137598ae840aSRob Herring 	flush_dcache_range((unsigned long)result->first,
1376d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->first,
1377d3e07478SStephen Warren 					  queuesize));
137898ae840aSRob Herring 	flush_dcache_range((unsigned long)result->tds,
1379d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct qTD, result->tds,
1380d3e07478SStephen Warren 					  queuesize));
1381d3e07478SStephen Warren 
138232f2eac1SHans de Goede 	if (ctrl->periodic_schedules > 0) {
13838f62ca64SPatrick Georgi 		if (disable_periodic(ctrl) < 0) {
13848f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
13858f62ca64SPatrick Georgi 			goto fail3;
13868f62ca64SPatrick Georgi 		}
138732f2eac1SHans de Goede 	}
13888f62ca64SPatrick Georgi 
13898f62ca64SPatrick Georgi 	/* hook up to periodic list */
13908f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
13918f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
139298ae840aSRob Herring 	list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
13938f62ca64SPatrick Georgi 
139498ae840aSRob Herring 	flush_dcache_range((unsigned long)result->last,
1395d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->last, 1));
139698ae840aSRob Herring 	flush_dcache_range((unsigned long)list,
1397d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, list, 1));
1398d3e07478SStephen Warren 
13998f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
14008f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14018f62ca64SPatrick Georgi 		goto fail3;
14028f62ca64SPatrick Georgi 	}
140336b73109SHans de Goede 	ctrl->periodic_schedules++;
14048f62ca64SPatrick Georgi 
14058f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
14068f62ca64SPatrick Georgi 	return result;
14078f62ca64SPatrick Georgi fail3:
14088f62ca64SPatrick Georgi 	if (result->tds)
14098f62ca64SPatrick Georgi 		free(result->tds);
14108f62ca64SPatrick Georgi fail2:
14118f62ca64SPatrick Georgi 	if (result->first)
14128f62ca64SPatrick Georgi 		free(result->first);
14138f62ca64SPatrick Georgi 	if (result)
14148f62ca64SPatrick Georgi 		free(result);
14158f62ca64SPatrick Georgi fail1:
14168f62ca64SPatrick Georgi 	return NULL;
14178f62ca64SPatrick Georgi }
14188f62ca64SPatrick Georgi 
1419029fd8eaSHans de Goede static void *_ehci_poll_int_queue(struct usb_device *dev,
1420029fd8eaSHans de Goede 				  struct int_queue *queue)
14218f62ca64SPatrick Georgi {
14228f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
1423415548d8SHans de Goede 	struct qTD *cur_td;
14247f59d16aSHans de Goede 	uint32_t token, toggle;
14257f59d16aSHans de Goede 	unsigned long pipe = queue->pipe;
14268f62ca64SPatrick Georgi 
14278f62ca64SPatrick Georgi 	/* depleted queue */
14288f62ca64SPatrick Georgi 	if (cur == NULL) {
14298f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
14308f62ca64SPatrick Georgi 		return NULL;
14318f62ca64SPatrick Georgi 	}
14328f62ca64SPatrick Georgi 	/* still active */
1433415548d8SHans de Goede 	cur_td = &queue->tds[queue->current - queue->first];
143498ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur_td,
1435415548d8SHans de Goede 				ALIGN_END_ADDR(struct qTD, cur_td, 1));
14367f59d16aSHans de Goede 	token = hc32_to_cpu(cur_td->qt_token);
14377f59d16aSHans de Goede 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
14387f59d16aSHans de Goede 		debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
14398f62ca64SPatrick Georgi 		return NULL;
14408f62ca64SPatrick Georgi 	}
14417f59d16aSHans de Goede 
14427f59d16aSHans de Goede 	toggle = QT_TOKEN_GET_DT(token);
14437f59d16aSHans de Goede 	usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
14447f59d16aSHans de Goede 
14458f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
14468f62ca64SPatrick Georgi 		queue->current++;
14478f62ca64SPatrick Georgi 	else
14488f62ca64SPatrick Georgi 		queue->current = NULL;
14498aa26b8eSHans de Goede 
145098ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur->buffer,
14518aa26b8eSHans de Goede 				ALIGN_END_ADDR(char, cur->buffer,
14528aa26b8eSHans de Goede 					       queue->elementsize));
14538aa26b8eSHans de Goede 
1454415548d8SHans de Goede 	debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
14557f59d16aSHans de Goede 	      token, cur, queue->first);
14568f62ca64SPatrick Georgi 	return cur->buffer;
14578f62ca64SPatrick Georgi }
14588f62ca64SPatrick Georgi 
14598f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
1460029fd8eaSHans de Goede static int _ehci_destroy_int_queue(struct usb_device *dev,
1461029fd8eaSHans de Goede 				   struct int_queue *queue)
14628f62ca64SPatrick Georgi {
146324ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
14648f62ca64SPatrick Georgi 	int result = -1;
14658f62ca64SPatrick Georgi 	unsigned long timeout;
14668f62ca64SPatrick Georgi 
14678f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
14688f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14698f62ca64SPatrick Georgi 		goto out;
14708f62ca64SPatrick Georgi 	}
147136b73109SHans de Goede 	ctrl->periodic_schedules--;
14728f62ca64SPatrick Georgi 
14738f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
14748f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
1475ea427775SAdrian Cox 	while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
14768f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
14778f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
14788f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
14798f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
148098ae840aSRob Herring 			flush_dcache_range((unsigned long)cur,
1481ea7b30c5SHans de Goede 					   ALIGN_END_ADDR(struct QH, cur, 1));
14828f62ca64SPatrick Georgi 			result = 0;
14838f62ca64SPatrick Georgi 			break;
14848f62ca64SPatrick Georgi 		}
14858f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
14868f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14878f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
14888f62ca64SPatrick Georgi 			result = -1;
14898f62ca64SPatrick Georgi 			goto out;
14908f62ca64SPatrick Georgi 		}
14918f62ca64SPatrick Georgi 	}
14928f62ca64SPatrick Georgi 
149336b73109SHans de Goede 	if (ctrl->periodic_schedules > 0) {
14948f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
14958f62ca64SPatrick Georgi 		if (result < 0)
14968f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
14978f62ca64SPatrick Georgi 	}
14988f62ca64SPatrick Georgi 
14998f62ca64SPatrick Georgi out:
15008f62ca64SPatrick Georgi 	free(queue->tds);
15018f62ca64SPatrick Georgi 	free(queue->first);
15028f62ca64SPatrick Georgi 	free(queue);
15038f62ca64SPatrick Georgi 
15048f62ca64SPatrick Georgi 	return result;
15058f62ca64SPatrick Georgi }
15068f62ca64SPatrick Georgi 
150724ed894fSSimon Glass static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
150824ed894fSSimon Glass 				void *buffer, int length, int interval)
15092731b9a8SJean-Christophe PLAGNIOL-VILLARD {
15108f62ca64SPatrick Georgi 	void *backbuffer;
15118f62ca64SPatrick Georgi 	struct int_queue *queue;
15128f62ca64SPatrick Georgi 	unsigned long timeout;
15138f62ca64SPatrick Georgi 	int result = 0, ret;
15148f62ca64SPatrick Georgi 
15152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
15162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
151744ae0be7SBenoît Thébaudeau 
1518029fd8eaSHans de Goede 	queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
1519bd818d81SHans de Goede 	if (!queue)
1520bd818d81SHans de Goede 		return -1;
15218f62ca64SPatrick Georgi 
15228f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1523029fd8eaSHans de Goede 	while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
15248f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
15258f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
15268f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
15278f62ca64SPatrick Georgi 			break;
15288f62ca64SPatrick Georgi 		}
15298f62ca64SPatrick Georgi 
15308f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
153198ae840aSRob Herring 		debug("got wrong buffer back (%p instead of %p)\n",
153298ae840aSRob Herring 		      backbuffer, buffer);
15338f62ca64SPatrick Georgi 		return -EINVAL;
15348f62ca64SPatrick Georgi 	}
15358f62ca64SPatrick Georgi 
1536029fd8eaSHans de Goede 	ret = _ehci_destroy_int_queue(dev, queue);
15378f62ca64SPatrick Georgi 	if (ret < 0)
15388f62ca64SPatrick Georgi 		return ret;
15398f62ca64SPatrick Georgi 
15408f62ca64SPatrick Georgi 	/* everything worked out fine */
15418f62ca64SPatrick Georgi 	return result;
15422731b9a8SJean-Christophe PLAGNIOL-VILLARD }
154324ed894fSSimon Glass 
154446b01797SSimon Glass #ifndef CONFIG_DM_USB
154524ed894fSSimon Glass int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
154624ed894fSSimon Glass 			    void *buffer, int length)
154724ed894fSSimon Glass {
154824ed894fSSimon Glass 	return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
154924ed894fSSimon Glass }
155024ed894fSSimon Glass 
155124ed894fSSimon Glass int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
155224ed894fSSimon Glass 		   int length, struct devrequest *setup)
155324ed894fSSimon Glass {
155424ed894fSSimon Glass 	return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
155524ed894fSSimon Glass }
155624ed894fSSimon Glass 
155724ed894fSSimon Glass int submit_int_msg(struct usb_device *dev, unsigned long pipe,
155824ed894fSSimon Glass 		   void *buffer, int length, int interval)
155924ed894fSSimon Glass {
156024ed894fSSimon Glass 	return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
156124ed894fSSimon Glass }
1562029fd8eaSHans de Goede 
1563029fd8eaSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev,
1564029fd8eaSHans de Goede 		unsigned long pipe, int queuesize, int elementsize,
1565029fd8eaSHans de Goede 		void *buffer, int interval)
1566029fd8eaSHans de Goede {
1567029fd8eaSHans de Goede 	return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1568029fd8eaSHans de Goede 				      buffer, interval);
1569029fd8eaSHans de Goede }
1570029fd8eaSHans de Goede 
1571029fd8eaSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1572029fd8eaSHans de Goede {
1573029fd8eaSHans de Goede 	return _ehci_poll_int_queue(dev, queue);
1574029fd8eaSHans de Goede }
1575029fd8eaSHans de Goede 
1576029fd8eaSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1577029fd8eaSHans de Goede {
1578029fd8eaSHans de Goede 	return _ehci_destroy_int_queue(dev, queue);
1579029fd8eaSHans de Goede }
158046b01797SSimon Glass #endif
158146b01797SSimon Glass 
158246b01797SSimon Glass #ifdef CONFIG_DM_USB
158346b01797SSimon Glass static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
158446b01797SSimon Glass 				   unsigned long pipe, void *buffer, int length,
158546b01797SSimon Glass 				   struct devrequest *setup)
158646b01797SSimon Glass {
158746b01797SSimon Glass 	debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
158846b01797SSimon Glass 	      dev->name, udev, udev->dev->name, udev->portnr);
158946b01797SSimon Glass 
159046b01797SSimon Glass 	return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
159146b01797SSimon Glass }
159246b01797SSimon Glass 
159346b01797SSimon Glass static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
159446b01797SSimon Glass 				unsigned long pipe, void *buffer, int length)
159546b01797SSimon Glass {
159646b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
159746b01797SSimon Glass 	return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
159846b01797SSimon Glass }
159946b01797SSimon Glass 
160046b01797SSimon Glass static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
160146b01797SSimon Glass 			       unsigned long pipe, void *buffer, int length,
160246b01797SSimon Glass 			       int interval)
160346b01797SSimon Glass {
160446b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
160546b01797SSimon Glass 	return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
160646b01797SSimon Glass }
160746b01797SSimon Glass 
16088a5f0665SHans de Goede static struct int_queue *ehci_create_int_queue(struct udevice *dev,
16098a5f0665SHans de Goede 		struct usb_device *udev, unsigned long pipe, int queuesize,
16108a5f0665SHans de Goede 		int elementsize, void *buffer, int interval)
16118a5f0665SHans de Goede {
16128a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
16138a5f0665SHans de Goede 	return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
16148a5f0665SHans de Goede 				      buffer, interval);
16158a5f0665SHans de Goede }
16168a5f0665SHans de Goede 
16178a5f0665SHans de Goede static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
16188a5f0665SHans de Goede 				 struct int_queue *queue)
16198a5f0665SHans de Goede {
16208a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
16218a5f0665SHans de Goede 	return _ehci_poll_int_queue(udev, queue);
16228a5f0665SHans de Goede }
16238a5f0665SHans de Goede 
16248a5f0665SHans de Goede static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
16258a5f0665SHans de Goede 				  struct int_queue *queue)
16268a5f0665SHans de Goede {
16278a5f0665SHans de Goede 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
16288a5f0665SHans de Goede 	return _ehci_destroy_int_queue(udev, queue);
16298a5f0665SHans de Goede }
16308a5f0665SHans de Goede 
163146b01797SSimon Glass int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
163246b01797SSimon Glass 		  struct ehci_hcor *hcor, const struct ehci_ops *ops,
163346b01797SSimon Glass 		  uint tweaks, enum usb_init_type init)
163446b01797SSimon Glass {
1635cb8a2c14SHans de Goede 	struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
163646b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
163746b01797SSimon Glass 	int ret;
163846b01797SSimon Glass 
163946b01797SSimon Glass 	debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
164046b01797SSimon Glass 	      dev->name, ctrl, hccr, hcor, init);
164146b01797SSimon Glass 
1642cb8a2c14SHans de Goede 	priv->desc_before_addr = true;
1643cb8a2c14SHans de Goede 
164446b01797SSimon Glass 	ehci_setup_ops(ctrl, ops);
164546b01797SSimon Glass 	ctrl->hccr = hccr;
164646b01797SSimon Glass 	ctrl->hcor = hcor;
164746b01797SSimon Glass 	ctrl->priv = ctrl;
164846b01797SSimon Glass 
164949b4c5c7SStephen Warren 	ctrl->init = init;
165049b4c5c7SStephen Warren 	if (ctrl->init == USB_INIT_DEVICE)
165146b01797SSimon Glass 		goto done;
165249b4c5c7SStephen Warren 
165346b01797SSimon Glass 	ret = ehci_reset(ctrl);
165446b01797SSimon Glass 	if (ret)
165546b01797SSimon Glass 		goto err;
165646b01797SSimon Glass 
165746b01797SSimon Glass 	ret = ehci_common_init(ctrl, tweaks);
165846b01797SSimon Glass 	if (ret)
165946b01797SSimon Glass 		goto err;
166046b01797SSimon Glass done:
166146b01797SSimon Glass 	return 0;
166246b01797SSimon Glass err:
166346b01797SSimon Glass 	free(ctrl);
166446b01797SSimon Glass 	debug("%s: failed, ret=%d\n", __func__, ret);
166546b01797SSimon Glass 	return ret;
166646b01797SSimon Glass }
166746b01797SSimon Glass 
166846b01797SSimon Glass int ehci_deregister(struct udevice *dev)
166946b01797SSimon Glass {
167046b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
167146b01797SSimon Glass 
167249b4c5c7SStephen Warren 	if (ctrl->init == USB_INIT_DEVICE)
167349b4c5c7SStephen Warren 		return 0;
167449b4c5c7SStephen Warren 
167546b01797SSimon Glass 	ehci_shutdown(ctrl);
167646b01797SSimon Glass 
167746b01797SSimon Glass 	return 0;
167846b01797SSimon Glass }
167946b01797SSimon Glass 
168046b01797SSimon Glass struct dm_usb_ops ehci_usb_ops = {
168146b01797SSimon Glass 	.control = ehci_submit_control_msg,
168246b01797SSimon Glass 	.bulk = ehci_submit_bulk_msg,
168346b01797SSimon Glass 	.interrupt = ehci_submit_int_msg,
16848a5f0665SHans de Goede 	.create_int_queue = ehci_create_int_queue,
16858a5f0665SHans de Goede 	.poll_int_queue = ehci_poll_int_queue,
16868a5f0665SHans de Goede 	.destroy_int_queue = ehci_destroy_int_queue,
168746b01797SSimon Glass };
168846b01797SSimon Glass 
168946b01797SSimon Glass #endif
1690