xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision 25c8ebdfcd9b2fca08ccf6bcc37de9ba388ad458)
12731b9a8SJean-Christophe PLAGNIOL-VILLARD /*-
22731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2007-2008, Juniper Networks, Inc.
32731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Excito Elektronik i Skåne AB
42731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
52731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
62731b9a8SJean-Christophe PLAGNIOL-VILLARD  * All rights reserved.
72731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
82731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is free software; you can redistribute it and/or
92731b9a8SJean-Christophe PLAGNIOL-VILLARD  * modify it under the terms of the GNU General Public License as
102731b9a8SJean-Christophe PLAGNIOL-VILLARD  * published by the Free Software Foundation version 2 of
112731b9a8SJean-Christophe PLAGNIOL-VILLARD  * the License.
122731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
132731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is distributed in the hope that it will be useful,
142731b9a8SJean-Christophe PLAGNIOL-VILLARD  * but WITHOUT ANY WARRANTY; without even the implied warranty of
152731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162731b9a8SJean-Christophe PLAGNIOL-VILLARD  * GNU General Public License for more details.
172731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
182731b9a8SJean-Christophe PLAGNIOL-VILLARD  * You should have received a copy of the GNU General Public License
192731b9a8SJean-Christophe PLAGNIOL-VILLARD  * along with this program; if not, write to the Free Software
202731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
212731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MA 02111-1307 USA
222731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
232731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
2446b01797SSimon Glass #include <dm.h>
258f62ca64SPatrick Georgi #include <errno.h>
262731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/byteorder.h>
2793ad908cSLucas Stach #include <asm/unaligned.h>
282731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
292731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
302731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <malloc.h>
3167333f76SStefan Roese #include <watchdog.h>
328f62ca64SPatrick Georgi #include <linux/compiler.h>
332731b9a8SJean-Christophe PLAGNIOL-VILLARD 
342731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
352731b9a8SJean-Christophe PLAGNIOL-VILLARD 
36676ae068SLucas Stach #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
37676ae068SLucas Stach #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
38676ae068SLucas Stach #endif
392731b9a8SJean-Christophe PLAGNIOL-VILLARD 
405077f96fSJulius Werner /*
415077f96fSJulius Werner  * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
425077f96fSJulius Werner  * Let's time out after 8 to have a little safety margin on top of that.
435077f96fSJulius Werner  */
445077f96fSJulius Werner #define HCHALT_TIMEOUT (8 * 1000)
455077f96fSJulius Werner 
4646b01797SSimon Glass #ifndef CONFIG_DM_USB
47b959655fSMarek Vasut static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
4846b01797SSimon Glass #endif
4971c5de4fSTom Rini 
5071c5de4fSTom Rini #define ALIGN_END_ADDR(type, ptr, size)			\
5198ae840aSRob Herring 	((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
532731b9a8SJean-Christophe PLAGNIOL-VILLARD static struct descriptor {
542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_hub_descriptor hub;
552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_device_descriptor device;
562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_config_descriptor config;
572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_linux_interface_descriptor interface;
582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	struct usb_endpoint_descriptor endpoint;
592731b9a8SJean-Christophe PLAGNIOL-VILLARD }  __attribute__ ((packed)) descriptor = {
602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x8,		/* bDescLength */
622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x29,		/* bDescriptorType: hub descriptor */
632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bNrPorts -- runtime modified */
642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* wHubCharacteristics */
655f4b4f2fSVincent Palatin 		10,		/* bPwrOn2PwrGood */
662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bHubCntrCurrent */
672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{},		/* Device removable */
682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		{}		/* at most 7 ports! XXX */
692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x12,		/* bLength */
722731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDescriptorType: UDESC_DEVICE */
736d313c84SSergei Shtylyov 		cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bDeviceClass: UDCLASS_HUB */
752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bDeviceSubClass: UDSUBCLASS_HUB */
762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bDeviceProtocol: UDPROTO_HSHUBSTT */
772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		64,		/* bMaxPacketSize: 64 bytes */
782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idVendor */
792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x0000,		/* idProduct */
806d313c84SSergei Shtylyov 		cpu_to_le16(0x0100), /* bcdDevice */
812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* iManufacturer */
822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* iProduct */
832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iSerialNumber */
842731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1		/* bNumConfigurations: 1 */
852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,
882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		2,		/* bDescriptorType: UDESC_CONFIG */
892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cpu_to_le16(0x19),
902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumInterface */
912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bConfigurationValue */
922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* iConfiguration */
932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x40,		/* bmAttributes: UC_SELF_POWER */
942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* bMaxPower */
952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x9,		/* bLength */
982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		4,		/* bDescriptorType: UDESC_INTERFACE */
992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceNumber */
1002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bAlternateSetting */
1012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		1,		/* bNumEndpoints */
1022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		9,		/* bInterfaceClass: UICLASS_HUB */
1032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceSubClass: UISUBCLASS_HUB */
1042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0,		/* bInterfaceProtocol: UIPROTO_HSHUBSTT */
1052731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0		/* iInterface */
1062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	{
1082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x7,		/* bLength */
1092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		5,		/* bDescriptorType: UDESC_ENDPOINT */
1102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		0x81,		/* bEndpointAddress:
1112731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * UE_DIR_IN | EHCI_INTR_ENDPT
1122731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
1132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		3,		/* bmAttributes: UE_INTERRUPT */
1148f8bd565STom Rix 		8,		/* wMaxPacketSize */
1152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		255		/* bInterval */
1162731b9a8SJean-Christophe PLAGNIOL-VILLARD 	},
1172731b9a8SJean-Christophe PLAGNIOL-VILLARD };
1182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1192731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_IS_TDI)
1202731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(1)
1212731b9a8SJean-Christophe PLAGNIOL-VILLARD #else
1222731b9a8SJean-Christophe PLAGNIOL-VILLARD #define ehci_is_TDI()	(0)
1232731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
1242731b9a8SJean-Christophe PLAGNIOL-VILLARD 
12524ed894fSSimon Glass static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
12624ed894fSSimon Glass {
12746b01797SSimon Glass #ifdef CONFIG_DM_USB
128*25c8ebdfSHans de Goede 	return dev_get_priv(usb_get_bus(udev->dev));
12946b01797SSimon Glass #else
13024ed894fSSimon Glass 	return udev->controller;
13146b01797SSimon Glass #endif
13224ed894fSSimon Glass }
13324ed894fSSimon Glass 
134deb8508cSSimon Glass static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
135b068deb3SJim Lin {
136b068deb3SJim Lin 	return PORTSC_PSPD(reg);
137b068deb3SJim Lin }
138b068deb3SJim Lin 
139deb8508cSSimon Glass static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
140b068deb3SJim Lin {
141b068deb3SJim Lin 	uint32_t tmp;
142b068deb3SJim Lin 	uint32_t *reg_ptr;
143b068deb3SJim Lin 
14411d18a19SSimon Glass 	reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
145b068deb3SJim Lin 	tmp = ehci_readl(reg_ptr);
146b068deb3SJim Lin 	tmp |= USBMODE_CM_HC;
147b068deb3SJim Lin #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
148b068deb3SJim Lin 	tmp |= USBMODE_BE;
149b068deb3SJim Lin #endif
150b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
151b068deb3SJim Lin }
152b068deb3SJim Lin 
153deb8508cSSimon Glass static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
154727fce36SSimon Glass 			       uint32_t *reg)
1553874b6d6SMarek Vasut {
1563874b6d6SMarek Vasut 	mdelay(50);
1573874b6d6SMarek Vasut }
1583874b6d6SMarek Vasut 
159deb8508cSSimon Glass static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
160aac064f7SSimon Glass {
161aac064f7SSimon Glass 	if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
162aac064f7SSimon Glass 		/* Printing the message would cause a scan failure! */
163aac064f7SSimon Glass 		debug("The request port(%u) is not configured\n", port);
164aac064f7SSimon Glass 		return NULL;
165aac064f7SSimon Glass 	}
166aac064f7SSimon Glass 
1676a1a8162SSimon Glass 	return (uint32_t *)&ctrl->hcor->or_portsc[port];
168aac064f7SSimon Glass }
169aac064f7SSimon Glass 
1702731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1712731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
17509c83a45SWolfgang Denk 		udelay(5);
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1792731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1802731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1842731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
186aeca43e3SSimon Glass static int ehci_reset(struct ehci_ctrl *ctrl)
1872731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1902731b9a8SJean-Christophe PLAGNIOL-VILLARD 
191aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
192273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
193aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
194aeca43e3SSimon Glass 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
195676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2002731b9a8SJean-Christophe PLAGNIOL-VILLARD 
201b068deb3SJim Lin 	if (ehci_is_TDI())
202aeca43e3SSimon Glass 		ctrl->ops.set_usb_mode(ctrl);
2039ab4ce22SSimon Glass 
2049ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
205aeca43e3SSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
20614eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
2079ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
208aeca43e3SSimon Glass 	ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
2099ab4ce22SSimon Glass #endif
2102731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
2112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
2122731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2132731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2145077f96fSJulius Werner static int ehci_shutdown(struct ehci_ctrl *ctrl)
2155077f96fSJulius Werner {
2165077f96fSJulius Werner 	int i, ret = 0;
2175077f96fSJulius Werner 	uint32_t cmd, reg;
2185077f96fSJulius Werner 
2191e1be6d4SMarek Vasut 	if (!ctrl || !ctrl->hcor)
2201e1be6d4SMarek Vasut 		return -EINVAL;
2211e1be6d4SMarek Vasut 
2225077f96fSJulius Werner 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
2235077f96fSJulius Werner 	cmd &= ~(CMD_PSE | CMD_ASE);
2245077f96fSJulius Werner 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2255077f96fSJulius Werner 	ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
2265077f96fSJulius Werner 		100 * 1000);
2275077f96fSJulius Werner 
2285077f96fSJulius Werner 	if (!ret) {
2295077f96fSJulius Werner 		for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
2305077f96fSJulius Werner 			reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
2315077f96fSJulius Werner 			reg |= EHCI_PS_SUSP;
2325077f96fSJulius Werner 			ehci_writel(&ctrl->hcor->or_portsc[i], reg);
2335077f96fSJulius Werner 		}
2345077f96fSJulius Werner 
2355077f96fSJulius Werner 		cmd &= ~CMD_RUN;
2365077f96fSJulius Werner 		ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
2375077f96fSJulius Werner 		ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
2385077f96fSJulius Werner 			HCHALT_TIMEOUT);
2395077f96fSJulius Werner 	}
2405077f96fSJulius Werner 
2415077f96fSJulius Werner 	if (ret)
2425077f96fSJulius Werner 		puts("EHCI failed to shut down host controller.\n");
2435077f96fSJulius Werner 
2445077f96fSJulius Werner 	return ret;
2455077f96fSJulius Werner }
2465077f96fSJulius Werner 
2472731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2482731b9a8SJean-Christophe PLAGNIOL-VILLARD {
249b8adb120SMarek Vasut 	uint32_t delta, next;
25098ae840aSRob Herring 	uint32_t addr = (unsigned long)buf;
2512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
253189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
254b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
255b8adb120SMarek Vasut 
256189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
257189a6956SIlya Yanok 
2582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
259cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
2602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		td->qt_buffer[idx] = cpu_to_hc32(addr);
2613ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
26214eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2692731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
271cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
27298ae840aSRob Herring 		printf("out of buffer pointers (%zu bytes left)\n", sz);
2732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2772731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
279c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
280c60795f4SIlya Yanok {
281c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
282c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
283c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
284c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
285c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
286c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
287c60795f4SIlya Yanok 		return QH_LOW_SPEED;
288c60795f4SIlya Yanok 	return QH_FULL_SPEED;
289c60795f4SIlya Yanok }
290c60795f4SIlya Yanok 
29146b01797SSimon Glass static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
2924e2c4ad3SHans de Goede 					  struct QH *qh)
2934e2c4ad3SHans de Goede {
2944e2c4ad3SHans de Goede 	struct usb_device *ttdev;
29546b01797SSimon Glass 	int parent_devnum;
2964e2c4ad3SHans de Goede 
29746b01797SSimon Glass 	if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
2984e2c4ad3SHans de Goede 		return;
2994e2c4ad3SHans de Goede 
3004e2c4ad3SHans de Goede 	/*
3014e2c4ad3SHans de Goede 	 * For full / low speed devices we need to get the devnum and portnr of
3024e2c4ad3SHans de Goede 	 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
3034e2c4ad3SHans de Goede 	 * in the tree before that one!
3044e2c4ad3SHans de Goede 	 */
30546b01797SSimon Glass #ifdef CONFIG_DM_USB
30646b01797SSimon Glass 	struct udevice *parent;
30746b01797SSimon Glass 
30846b01797SSimon Glass 	for (ttdev = udev; ; ) {
30946b01797SSimon Glass 		struct udevice *dev = ttdev->dev;
31046b01797SSimon Glass 
31146b01797SSimon Glass 		if (dev->parent &&
31246b01797SSimon Glass 		    device_get_uclass_id(dev->parent) == UCLASS_USB_HUB)
31346b01797SSimon Glass 			parent = dev->parent;
31446b01797SSimon Glass 		else
31546b01797SSimon Glass 			parent = NULL;
31646b01797SSimon Glass 		if (!parent)
31746b01797SSimon Glass 			return;
31846b01797SSimon Glass 		ttdev = dev_get_parentdata(parent);
31946b01797SSimon Glass 		if (!ttdev->speed != USB_SPEED_HIGH)
32046b01797SSimon Glass 			break;
32146b01797SSimon Glass 	}
32246b01797SSimon Glass 	parent_devnum = ttdev->devnum;
32346b01797SSimon Glass #else
32446b01797SSimon Glass 	ttdev = udev;
3254e2c4ad3SHans de Goede 	while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
3264e2c4ad3SHans de Goede 		ttdev = ttdev->parent;
3274e2c4ad3SHans de Goede 	if (!ttdev->parent)
3284e2c4ad3SHans de Goede 		return;
32946b01797SSimon Glass 	parent_devnum = ttdev->parent->devnum;
33046b01797SSimon Glass #endif
3314e2c4ad3SHans de Goede 
3324e2c4ad3SHans de Goede 	qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
33346b01797SSimon Glass 				     QH_ENDPT2_HUBADDR(parent_devnum));
3344e2c4ad3SHans de Goede }
3354e2c4ad3SHans de Goede 
3362731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
3372731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
3382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
3392731b9a8SJean-Christophe PLAGNIOL-VILLARD {
34071c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
3415cec214eSBenoît Thébaudeau 	struct qTD *qtd;
3425cec214eSBenoît Thébaudeau 	int qtd_count = 0;
343de98e8b2SMarek Vasut 	int qtd_counter = 0;
3442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
3452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
3462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
347db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
3482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
3492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
35096820a35SSimon Glass 	int timeout;
3512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
35224ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
3532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
3552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
3562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
3572731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
3582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
3592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
3602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
3612731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
3622731b9a8SJean-Christophe PLAGNIOL-VILLARD 
363db191346SBenoît Thébaudeau #define PKT_ALIGN	512
3645cec214eSBenoît Thébaudeau 	/*
3655cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
3665cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
3675cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
3685cec214eSBenoît Thébaudeau 	 *
3695cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
3705cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
3715cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
3725cec214eSBenoît Thébaudeau 	 *
3735cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
3745cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
3755cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
3765cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
3775cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
3785cec214eSBenoît Thébaudeau 	 */
3795cec214eSBenoît Thébaudeau 	if (req != NULL)
3805cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
3815cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
3825cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
3835cec214eSBenoît Thébaudeau 		/*
3845cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
385db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
386db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
387db191346SBenoît Thébaudeau 		 * shorter).
3885cec214eSBenoît Thébaudeau 		 *
3895cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
390db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
391db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
392db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
3935cec214eSBenoît Thébaudeau 		 *
394db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
3955cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
3965cec214eSBenoît Thébaudeau 		 */
3975cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
3985cec214eSBenoît Thébaudeau 		/*
399db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
400db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
4015cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
4025cec214eSBenoît Thébaudeau 		 */
40398ae840aSRob Herring 		if ((unsigned long)buffer & (PKT_ALIGN - 1))
4045cec214eSBenoît Thébaudeau 			xfr_sz--;
4055cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
4065cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
4075cec214eSBenoît Thébaudeau 		/*
408db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
409db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
410db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
411db191346SBenoît Thébaudeau 		 * bytes.
4125cec214eSBenoît Thébaudeau 		 */
413db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
4145cec214eSBenoît Thébaudeau 	}
4155cec214eSBenoît Thébaudeau /*
416db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
417db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
4185cec214eSBenoît Thébaudeau  */
419db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
4205cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
4215cec214eSBenoît Thébaudeau #endif
4225cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
4235cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
4245cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
4255cec214eSBenoît Thébaudeau 		return -1;
4265cec214eSBenoît Thébaudeau 	}
4275cec214eSBenoît Thébaudeau 
42871c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
4295cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
430de98e8b2SMarek Vasut 
431b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
432b8adb120SMarek Vasut 
43341b1f0acSMarek Vasut 	/*
43441b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
43541b1f0acSMarek Vasut 	 *
43641b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
43741b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
43841b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
43941b1f0acSMarek Vasut 	 * - qh_curtd
44041b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
44141b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
44241b1f0acSMarek Vasut 	 */
44398ae840aSRob Herring 	qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
444c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
445db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
44614eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
447db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
44814eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
449c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
45014eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
45114eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
45271c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
4534e2c4ad3SHans de Goede 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
45471c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
4554e2c4ad3SHans de Goede 	ehci_update_endpt2_dev_n_port(dev, qh);
45671c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
4572456b97fSStephen Warren 	qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
4582731b9a8SJean-Christophe PLAGNIOL-VILLARD 
45971c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
4602731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
46241b1f0acSMarek Vasut 		/*
46341b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
46441b1f0acSMarek Vasut 		 *
46541b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
46641b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
46741b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
46841b1f0acSMarek Vasut 		 *
46941b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
47041b1f0acSMarek Vasut 		 */
471de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
472de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
47314eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
47414eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
47514eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
47614eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
477de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
47814eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
47914eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
4802731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
4812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
48241b1f0acSMarek Vasut 		/* Update previous qTD! */
48398ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
484de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
4862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4872731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
4895cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
4905cec214eSBenoît Thébaudeau 		int left_length = length;
4915cec214eSBenoît Thébaudeau 
4925cec214eSBenoît Thébaudeau 		do {
4935cec214eSBenoît Thébaudeau 			/*
4945cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
4955cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
4965cec214eSBenoît Thébaudeau 			 */
4975cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
4985cec214eSBenoît Thébaudeau 			/*
4995cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
5005cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
5015cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
5025cec214eSBenoît Thébaudeau 			 */
50398ae840aSRob Herring 			xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
5045cec214eSBenoît Thébaudeau 			/*
5055cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
506db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
5075cec214eSBenoît Thébaudeau 			 */
508db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
5095cec214eSBenoît Thébaudeau 			/*
5105cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
5115cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
5125cec214eSBenoît Thébaudeau 			 */
5135cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
5145cec214eSBenoît Thébaudeau 
51541b1f0acSMarek Vasut 			/*
51641b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
51741b1f0acSMarek Vasut 			 *
51841b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
51941b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
52041b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
52141b1f0acSMarek Vasut 			 *
52241b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
52341b1f0acSMarek Vasut 			 */
5245cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
5255cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5265cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
5275cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
5285cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
5295cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
53014eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
5315cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
5325cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
53314eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
53414eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
535de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
5365cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
5375cec214eSBenoît Thébaudeau 						xfr_bytes)) {
53814eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
5392731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
5402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
54141b1f0acSMarek Vasut 			/* Update previous qTD! */
54298ae840aSRob Herring 			*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
543de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
544db191346SBenoît Thébaudeau 			/*
545db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
546db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
547db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
548db191346SBenoît Thébaudeau 			 */
549db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
550db191346SBenoît Thébaudeau 				toggle ^= 1;
5515cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
5525cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
5535cec214eSBenoît Thébaudeau 		} while (left_length > 0);
5542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5552731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
55741b1f0acSMarek Vasut 		/*
55841b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
55941b1f0acSMarek Vasut 		 *
56041b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
56141b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
56241b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
56341b1f0acSMarek Vasut 		 */
564de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
565de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
566db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
56714eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
56814eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
56914eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
57014eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
571de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
57241b1f0acSMarek Vasut 		/* Update previous qTD! */
57398ae840aSRob Herring 		*tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
574de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
5752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5762731b9a8SJean-Christophe PLAGNIOL-VILLARD 
57798ae840aSRob Herring 	ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
5782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5792731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
58098ae840aSRob Herring 	flush_dcache_range((unsigned long)&ctrl->qh_list,
581676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
58298ae840aSRob Herring 	flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
58398ae840aSRob Herring 	flush_dcache_range((unsigned long)qtd,
5845cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
5852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
586c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
58798ae840aSRob Herring 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
588c7701af5SIlya Yanok 
589676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
590676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
5912731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
593676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5942731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
595676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5962731b9a8SJean-Christophe PLAGNIOL-VILLARD 
597676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
5982731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
60014eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
6012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6032731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
6052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
606de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
60796820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
6082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
6092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
61098ae840aSRob Herring 		invalidate_dcache_range((unsigned long)&ctrl->qh_list,
611676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
61298ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qh,
61371c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
61498ae840aSRob Herring 		invalidate_dcache_range((unsigned long)qtd,
6155cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
616b8adb120SMarek Vasut 
6172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
61814eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
6192731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
62067333f76SStefan Roese 		WATCHDOG_RESET();
62196820a35SSimon Glass 	} while (get_timer(ts) < timeout);
62296820a35SSimon Glass 
623189a6956SIlya Yanok 	/*
624189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
625189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
626189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
627189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
628189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
629189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
630189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
631189a6956SIlya Yanok 	 */
63298ae840aSRob Herring 	invalidate_dcache_range((unsigned long)buffer,
63398ae840aSRob Herring 		ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
634b8adb120SMarek Vasut 
63596820a35SSimon Glass 	/* Check that the TD processing happened */
63614eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
63796820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
6382731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6392731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
640676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
6412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
642676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
6432731b9a8SJean-Christophe PLAGNIOL-VILLARD 
644676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
6452731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
6462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
64714eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
6482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
6492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6502731b9a8SJean-Christophe PLAGNIOL-VILLARD 
65171c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
65214eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
6532731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
65414eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
65514eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
6562731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
65714eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
6582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
6592731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
6602731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
6612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66214eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
6632731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
6642731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66514eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
66614eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
6672731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
6682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
66914eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
67014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
6712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
6722731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6742731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
67514eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
676222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
6772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
67914eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
6802731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
6812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
682e82a316dSKuo-Jung Su #ifndef CONFIG_USB_EHCI_FARADAY
6832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
684676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
685676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
686676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
687e82a316dSKuo-Jung Su #endif
6882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6905cec214eSBenoît Thébaudeau 	free(qtd);
6912731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
6922731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6932731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
6945cec214eSBenoît Thébaudeau 	free(qtd);
6952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
6962731b9a8SJean-Christophe PLAGNIOL-VILLARD }
6972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
69824ed894fSSimon Glass static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
69924ed894fSSimon Glass 			    void *buffer, int length, struct devrequest *req)
7002731b9a8SJean-Christophe PLAGNIOL-VILLARD {
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
7022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
7042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
7052731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
7062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
7077d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
70824ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
7092731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
7112731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
7132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
7152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
71744259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
7182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
71944259bb9SPrafulla Wadaskar 	switch (typeReq) {
7209c6a9d7cSKuo-Jung Su 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7219c6a9d7cSKuo-Jung Su 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
7229c6a9d7cSKuo-Jung Su 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
723deb8508cSSimon Glass 		status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
7241dde1423SKuo-Jung Su 		if (!status_reg)
7259c6a9d7cSKuo-Jung Su 			return -1;
7269c6a9d7cSKuo-Jung Su 		break;
7279c6a9d7cSKuo-Jung Su 	default:
7289c6a9d7cSKuo-Jung Su 		status_reg = NULL;
7299c6a9d7cSKuo-Jung Su 		break;
7309c6a9d7cSKuo-Jung Su 	}
7319c6a9d7cSKuo-Jung Su 
7329c6a9d7cSKuo-Jung Su 	switch (typeReq) {
7332731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
7342731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
7362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
7372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
73814eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
7392731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
7412731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
7422731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
74314eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
74414eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
74514eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
7462731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
7482731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
7532731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7542731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
7552731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
7662731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7722731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
7762731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
7782731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
78014eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
7812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
7832731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
7842731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
7852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7872731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
7882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
789676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
7902731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7912731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
7922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
7932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
7942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
7982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
8002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
8022731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
8032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
8052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
8082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
8102731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
812c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
8162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
818deb8508cSSimon Glass 			switch (ctrl->ops.get_port_speed(ctrl, reg)) {
81914eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
8202731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
82114eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
8222731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
8232731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
82414eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
8252731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
8282731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8292731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
8312731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
8342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
8352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
8362731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
8372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
8397d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
8442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8462731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
8472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
8482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
8512731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
8522731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8532731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
854676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
8552731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
8562731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8572731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8592731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
8602731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
8612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
8622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
8632731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
8642731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
8657d9aa8fdSJulius Werner 				      port - 1);
8662731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
8672731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8682731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
8692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
870c8b2d1dcSSergei Shtylyov 				int ret;
871c8b2d1dcSSergei Shtylyov 
8722731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
8732731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
8742731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
8752731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
8762731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
8772731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
8782731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
8792731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
880deb8508cSSimon Glass 				ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
8813874b6d6SMarek Vasut 
882b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
883c8b2d1dcSSergei Shtylyov 				/*
884c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
885c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
886c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
887c8b2d1dcSSergei Shtylyov 				 */
888c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
889c8b2d1dcSSergei Shtylyov 						2 * 1000);
890c8b2d1dcSSergei Shtylyov 				if (!ret)
8917d9aa8fdSJulius Werner 					ctrl->portreset |= 1 << port;
892c8b2d1dcSSergei Shtylyov 				else
893c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
8947d9aa8fdSJulius Werner 					       port - 1);
8952731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
8962731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8977d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
8985077f96fSJulius Werner 			ehci_shutdown(ctrl);
8997d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
9007d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
9017d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
9027d9aa8fdSJulius Werner 			break;
9032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9042731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
908676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
9112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
912ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
9132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
9142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
9152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
9162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
918ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
9192731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9202731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
921676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
922ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
923ed10e66aSSimon Glass 			break;
9242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
925ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
9262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
928ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
9292731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9302731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
9317d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
9322731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
9332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
9342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
9352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
9362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
9372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
9382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
939676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
9402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
9412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
9422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
9432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
9442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9452731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9465b84dd67SMike Frysinger 	mdelay(1);
947b4141195SMasahiro Yamada 	len = min3(srclen, (int)le16_to_cpu(req->length), length);
9482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
9492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
9502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
9512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
9522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
9542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
9552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9562731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9572731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
9582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
9592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
9602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
9612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
9632731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
9642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
9652731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
967deb8508cSSimon Glass const struct ehci_ops default_ehci_ops = {
968deb8508cSSimon Glass 	.set_usb_mode		= ehci_set_usbmode,
969deb8508cSSimon Glass 	.get_port_speed		= ehci_get_port_speed,
970deb8508cSSimon Glass 	.powerup_fixup		= ehci_powerup_fixup,
971deb8508cSSimon Glass 	.get_portsc_register	= ehci_get_portsc_register,
972deb8508cSSimon Glass };
973deb8508cSSimon Glass 
974deb8508cSSimon Glass static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
975c4a3141dSSimon Glass {
976deb8508cSSimon Glass 	if (!ops) {
977deb8508cSSimon Glass 		ctrl->ops = default_ehci_ops;
978deb8508cSSimon Glass 	} else {
979deb8508cSSimon Glass 		ctrl->ops = *ops;
980deb8508cSSimon Glass 		if (!ctrl->ops.set_usb_mode)
981deb8508cSSimon Glass 			ctrl->ops.set_usb_mode = ehci_set_usbmode;
982deb8508cSSimon Glass 		if (!ctrl->ops.get_port_speed)
983deb8508cSSimon Glass 			ctrl->ops.get_port_speed = ehci_get_port_speed;
984deb8508cSSimon Glass 		if (!ctrl->ops.powerup_fixup)
985deb8508cSSimon Glass 			ctrl->ops.powerup_fixup = ehci_powerup_fixup;
986deb8508cSSimon Glass 		if (!ctrl->ops.get_portsc_register)
987deb8508cSSimon Glass 			ctrl->ops.get_portsc_register =
988deb8508cSSimon Glass 					ehci_get_portsc_register;
989deb8508cSSimon Glass 	}
990deb8508cSSimon Glass }
991deb8508cSSimon Glass 
99246b01797SSimon Glass #ifndef CONFIG_DM_USB
993deb8508cSSimon Glass void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
994deb8508cSSimon Glass {
995deb8508cSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
996deb8508cSSimon Glass 
997deb8508cSSimon Glass 	ctrl->priv = priv;
998deb8508cSSimon Glass 	ehci_setup_ops(ctrl, ops);
999c4a3141dSSimon Glass }
1000c4a3141dSSimon Glass 
1001c4a3141dSSimon Glass void *ehci_get_controller_priv(int index)
1002c4a3141dSSimon Glass {
1003c4a3141dSSimon Glass 	return ehcic[index].priv;
1004c4a3141dSSimon Glass }
100546b01797SSimon Glass #endif
1006c4a3141dSSimon Glass 
10077372b5bdSSimon Glass static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
10082731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1009676ae068SLucas Stach 	struct QH *qh_list;
10108f62ca64SPatrick Georgi 	struct QH *periodic;
10117372b5bdSSimon Glass 	uint32_t reg;
10127372b5bdSSimon Glass 	uint32_t cmd;
10138f62ca64SPatrick Georgi 	int i;
10142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10152982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
10167372b5bdSSimon Glass 	if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
10177372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
10182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10197372b5bdSSimon Glass 	qh_list = &ctrl->qh_list;
1020676ae068SLucas Stach 
10212731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
102271c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
102398ae840aSRob Herring 	qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
102414eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
102514eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
102671c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
102771c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
102814eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
102914eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
10302731b9a8SJean-Christophe PLAGNIOL-VILLARD 
103198ae840aSRob Herring 	flush_dcache_range((unsigned long)qh_list,
1032d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, qh_list, 1));
1033d3e07478SStephen Warren 
10348f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
10357372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)qh_list);
10368f62ca64SPatrick Georgi 
10378f62ca64SPatrick Georgi 	/*
10388f62ca64SPatrick Georgi 	 * Set up periodic list
10398f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
10408f62ca64SPatrick Georgi 	 */
10417372b5bdSSimon Glass 	ctrl->periodic_schedules = 0;
10427372b5bdSSimon Glass 	periodic = &ctrl->periodic_queue;
10438f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
10448f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
10458f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
10468f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
10478f62ca64SPatrick Georgi 
104898ae840aSRob Herring 	flush_dcache_range((unsigned long)periodic,
1049d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, periodic, 1));
1050d3e07478SStephen Warren 
10518f62ca64SPatrick Georgi 	/*
10528f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
10538f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
10548f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
10558f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
10568f62ca64SPatrick Georgi 	 *
10578f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
10588f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
10598f62ca64SPatrick Georgi 	 */
10607372b5bdSSimon Glass 	if (ctrl->periodic_list == NULL)
10617372b5bdSSimon Glass 		ctrl->periodic_list = memalign(4096, 1024 * 4);
10628bc36036SNikita Kiryanov 
10637372b5bdSSimon Glass 	if (!ctrl->periodic_list)
10648f62ca64SPatrick Georgi 		return -ENOMEM;
10658f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
10667372b5bdSSimon Glass 		ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
1067ea427775SAdrian Cox 						| QH_LINK_TYPE_QH);
10688f62ca64SPatrick Georgi 	}
10698f62ca64SPatrick Georgi 
10707372b5bdSSimon Glass 	flush_dcache_range((unsigned long)ctrl->periodic_list,
10717372b5bdSSimon Glass 			   ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
1072d3e07478SStephen Warren 					  1024));
1073d3e07478SStephen Warren 
10748f62ca64SPatrick Georgi 	/* Set periodic list base address */
10757372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_periodiclistbase,
10767372b5bdSSimon Glass 		(unsigned long)ctrl->periodic_list);
10778f62ca64SPatrick Georgi 
10787372b5bdSSimon Glass 	reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
10792731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
10807a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
10812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
10822731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
108393ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
108493ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
10852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
10862731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
108793ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
108893ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
10892731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
10917372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
10922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
10932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
10942731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
10952731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
10962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
10972731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
10987372b5bdSSimon Glass 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
10992731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11007372b5bdSSimon Glass 	if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
11012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* take control over the ports */
11027372b5bdSSimon Glass 		cmd = ehci_readl(&ctrl->hcor->or_configflag);
11032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		cmd |= FLAG_CF;
11047372b5bdSSimon Glass 		ehci_writel(&ctrl->hcor->or_configflag, cmd);
11057372b5bdSSimon Glass 	}
1106e82a316dSKuo-Jung Su 
11072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
11087372b5bdSSimon Glass 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
11095b84dd67SMike Frysinger 	mdelay(5);
11107372b5bdSSimon Glass 	reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
11112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
11122731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11137372b5bdSSimon Glass 	return 0;
11147372b5bdSSimon Glass }
11157372b5bdSSimon Glass 
111646b01797SSimon Glass #ifndef CONFIG_DM_USB
11177372b5bdSSimon Glass int usb_lowlevel_stop(int index)
11187372b5bdSSimon Glass {
11197372b5bdSSimon Glass 	ehci_shutdown(&ehcic[index]);
11207372b5bdSSimon Glass 	return ehci_hcd_stop(index);
11217372b5bdSSimon Glass }
11227372b5bdSSimon Glass 
11237372b5bdSSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
11247372b5bdSSimon Glass {
11257372b5bdSSimon Glass 	struct ehci_ctrl *ctrl = &ehcic[index];
11267372b5bdSSimon Glass 	uint tweaks = 0;
11277372b5bdSSimon Glass 	int rc;
11287372b5bdSSimon Glass 
1129deb8508cSSimon Glass 	/**
1130deb8508cSSimon Glass 	 * Set ops to default_ehci_ops, ehci_hcd_init should call
1131deb8508cSSimon Glass 	 * ehci_set_controller_priv to change any of these function pointers.
1132deb8508cSSimon Glass 	 */
1133deb8508cSSimon Glass 	ctrl->ops = default_ehci_ops;
1134deb8508cSSimon Glass 
11357372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11367372b5bdSSimon Glass 	if (rc)
11377372b5bdSSimon Glass 		return rc;
11387372b5bdSSimon Glass 	if (init == USB_INIT_DEVICE)
11397372b5bdSSimon Glass 		goto done;
11407372b5bdSSimon Glass 
11417372b5bdSSimon Glass 	/* EHCI spec section 4.1 */
1142aeca43e3SSimon Glass 	if (ehci_reset(ctrl))
11437372b5bdSSimon Glass 		return -1;
11447372b5bdSSimon Glass 
11457372b5bdSSimon Glass #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
11467372b5bdSSimon Glass 	rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
11477372b5bdSSimon Glass 	if (rc)
11487372b5bdSSimon Glass 		return rc;
11497372b5bdSSimon Glass #endif
11507372b5bdSSimon Glass #ifdef CONFIG_USB_EHCI_FARADAY
11517372b5bdSSimon Glass 	tweaks |= EHCI_TWEAK_NO_INIT_CF;
11527372b5bdSSimon Glass #endif
11537372b5bdSSimon Glass 	rc = ehci_common_init(ctrl, tweaks);
11547372b5bdSSimon Glass 	if (rc)
11557372b5bdSSimon Glass 		return rc;
11567372b5bdSSimon Glass 
11577372b5bdSSimon Glass 	ctrl->rootdev = 0;
1158127efc4fSTroy Kisky done:
1159676ae068SLucas Stach 	*controller = &ehcic[index];
11602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
11612731b9a8SJean-Christophe PLAGNIOL-VILLARD }
116246b01797SSimon Glass #endif
11632731b9a8SJean-Christophe PLAGNIOL-VILLARD 
116424ed894fSSimon Glass static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
116524ed894fSSimon Glass 				 void *buffer, int length)
11662731b9a8SJean-Christophe PLAGNIOL-VILLARD {
11672731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
11692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
11732731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11742731b9a8SJean-Christophe PLAGNIOL-VILLARD 
117524ed894fSSimon Glass static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
117624ed894fSSimon Glass 				    void *buffer, int length,
117724ed894fSSimon Glass 				    struct devrequest *setup)
11782731b9a8SJean-Christophe PLAGNIOL-VILLARD {
117924ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
11802731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11812731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
11822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
11832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
11842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11852731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1186676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1187676ae068SLucas Stach 		if (!ctrl->rootdev)
11882731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
11892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
11902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
11912731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
11922731b9a8SJean-Christophe PLAGNIOL-VILLARD }
11932731b9a8SJean-Christophe PLAGNIOL-VILLARD 
11948f62ca64SPatrick Georgi struct int_queue {
11958aa26b8eSHans de Goede 	int elementsize;
11968f62ca64SPatrick Georgi 	struct QH *first;
11978f62ca64SPatrick Georgi 	struct QH *current;
11988f62ca64SPatrick Georgi 	struct QH *last;
11998f62ca64SPatrick Georgi 	struct qTD *tds;
12008f62ca64SPatrick Georgi };
12018f62ca64SPatrick Georgi 
120298ae840aSRob Herring #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
12038f62ca64SPatrick Georgi 
12048f62ca64SPatrick Georgi static int
12058f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
12068f62ca64SPatrick Georgi {
12078f62ca64SPatrick Georgi 	uint32_t cmd;
12088f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12098f62ca64SPatrick Georgi 	int ret;
12108f62ca64SPatrick Georgi 
12118f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12128f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
12138f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12148f62ca64SPatrick Georgi 
12158f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12168f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
12178f62ca64SPatrick Georgi 	if (ret < 0) {
12188f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
12198f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12208f62ca64SPatrick Georgi 	}
12218f62ca64SPatrick Georgi 	udelay(1000);
12228f62ca64SPatrick Georgi 	return 0;
12238f62ca64SPatrick Georgi }
12248f62ca64SPatrick Georgi 
12258f62ca64SPatrick Georgi static int
12268f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
12278f62ca64SPatrick Georgi {
12288f62ca64SPatrick Georgi 	uint32_t cmd;
12298f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
12308f62ca64SPatrick Georgi 	int ret;
12318f62ca64SPatrick Georgi 
12328f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
12338f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
12348f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
12358f62ca64SPatrick Georgi 
12368f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
12378f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
12388f62ca64SPatrick Georgi 	if (ret < 0) {
12398f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
12408f62ca64SPatrick Georgi 		return -ETIMEDOUT;
12418f62ca64SPatrick Georgi 	}
12428f62ca64SPatrick Georgi 	return 0;
12438f62ca64SPatrick Georgi }
12448f62ca64SPatrick Georgi 
12458f62ca64SPatrick Georgi struct int_queue *
12468f62ca64SPatrick Georgi create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
12478bb6c1d1SHans de Goede 		 int elementsize, void *buffer, int interval)
12488f62ca64SPatrick Georgi {
124924ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
12508f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
12518f62ca64SPatrick Georgi 	int i;
12528f62ca64SPatrick Georgi 
1253bd818d81SHans de Goede 	/*
1254bd818d81SHans de Goede 	 * Interrupt transfers requiring several transactions are not supported
1255bd818d81SHans de Goede 	 * because bInterval is ignored.
1256bd818d81SHans de Goede 	 *
1257bd818d81SHans de Goede 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1258bd818d81SHans de Goede 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1259bd818d81SHans de Goede 	 * specification does not constrain this for interrupt transfers. That
1260bd818d81SHans de Goede 	 * means that ehci_submit_async() would support interrupt transfers
1261bd818d81SHans de Goede 	 * requiring several transactions only as long as the transfer size does
1262bd818d81SHans de Goede 	 * not require more than a single qTD.
1263bd818d81SHans de Goede 	 */
1264bd818d81SHans de Goede 	if (elementsize > usb_maxpacket(dev, pipe)) {
1265bd818d81SHans de Goede 		printf("%s: xfers requiring several transactions are not supported.\n",
1266bd818d81SHans de Goede 		       __func__);
1267bd818d81SHans de Goede 		return NULL;
1268bd818d81SHans de Goede 	}
1269bd818d81SHans de Goede 
12708f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
12718f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
12728f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
12738f62ca64SPatrick Georgi 		return NULL;
12748f62ca64SPatrick Georgi 	}
12758f62ca64SPatrick Georgi 
12768f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
12778f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
12788f62ca64SPatrick Georgi 	 * no matter the alignment
12798f62ca64SPatrick Georgi 	 */
12808f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
12818f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
12828f62ca64SPatrick Georgi 		return NULL;
12838f62ca64SPatrick Georgi 	}
12848f62ca64SPatrick Georgi 
12858f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
12868f62ca64SPatrick Georgi 	if (!result) {
12878f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12888f62ca64SPatrick Georgi 		goto fail1;
12898f62ca64SPatrick Georgi 	}
12908aa26b8eSHans de Goede 	result->elementsize = elementsize;
12918165e34bSStephen Warren 	result->first = memalign(USB_DMA_MINALIGN,
12928165e34bSStephen Warren 				 sizeof(struct QH) * queuesize);
12938f62ca64SPatrick Georgi 	if (!result->first) {
12948f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
12958f62ca64SPatrick Georgi 		goto fail2;
12968f62ca64SPatrick Georgi 	}
12978f62ca64SPatrick Georgi 	result->current = result->first;
12988f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
12998165e34bSStephen Warren 	result->tds = memalign(USB_DMA_MINALIGN,
13008165e34bSStephen Warren 			       sizeof(struct qTD) * queuesize);
13018f62ca64SPatrick Georgi 	if (!result->tds) {
13028f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
13038f62ca64SPatrick Georgi 		goto fail3;
13048f62ca64SPatrick Georgi 	}
13058f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
13068f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
13078f62ca64SPatrick Georgi 
13088f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
13098f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
13108f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
13118f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
13128f62ca64SPatrick Georgi 
131398ae840aSRob Herring 		qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
13148f62ca64SPatrick Georgi 		if (i == queuesize - 1)
1315ea427775SAdrian Cox 			qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
13168f62ca64SPatrick Georgi 
131798ae840aSRob Herring 		qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
1318ea427775SAdrian Cox 		qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1319ea427775SAdrian Cox 		qh->qh_endpt1 =
1320ea427775SAdrian Cox 			cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
13218f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
13228f62ca64SPatrick Georgi 			(1 << 14) |
13238f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
13248f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1325ea427775SAdrian Cox 			(usb_pipedevice(pipe) << 0));
1326ea427775SAdrian Cox 		qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1327ea427775SAdrian Cox 			(1 << 0)); /* S-mask: microframe 0 */
13288f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
13298f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
13304e2c4ad3SHans de Goede 			/* C-mask: microframes 2-4 */
13314e2c4ad3SHans de Goede 			qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
13328f62ca64SPatrick Georgi 		}
13334e2c4ad3SHans de Goede 		ehci_update_endpt2_dev_n_port(dev, qh);
13348f62ca64SPatrick Georgi 
1335ea427775SAdrian Cox 		td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1336ea427775SAdrian Cox 		td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
13378f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
13388f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
1339ea427775SAdrian Cox 		td->qt_token = cpu_to_hc32((elementsize << 16) |
13408f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1341ea427775SAdrian Cox 			0x80); /* active */
1342ea427775SAdrian Cox 		td->qt_buffer[0] =
134398ae840aSRob Herring 		    cpu_to_hc32((unsigned long)buffer + i * elementsize);
1344ea427775SAdrian Cox 		td->qt_buffer[1] =
1345ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1346ea427775SAdrian Cox 		td->qt_buffer[2] =
1347ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1348ea427775SAdrian Cox 		td->qt_buffer[3] =
1349ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1350ea427775SAdrian Cox 		td->qt_buffer[4] =
1351ea427775SAdrian Cox 		    cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
13528f62ca64SPatrick Georgi 
13538f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
13548f62ca64SPatrick Georgi 	}
13558f62ca64SPatrick Georgi 
135698ae840aSRob Herring 	flush_dcache_range((unsigned long)buffer,
1357d3e07478SStephen Warren 			   ALIGN_END_ADDR(char, buffer,
1358d3e07478SStephen Warren 					  queuesize * elementsize));
135998ae840aSRob Herring 	flush_dcache_range((unsigned long)result->first,
1360d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->first,
1361d3e07478SStephen Warren 					  queuesize));
136298ae840aSRob Herring 	flush_dcache_range((unsigned long)result->tds,
1363d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct qTD, result->tds,
1364d3e07478SStephen Warren 					  queuesize));
1365d3e07478SStephen Warren 
136632f2eac1SHans de Goede 	if (ctrl->periodic_schedules > 0) {
13678f62ca64SPatrick Georgi 		if (disable_periodic(ctrl) < 0) {
13688f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
13698f62ca64SPatrick Georgi 			goto fail3;
13708f62ca64SPatrick Georgi 		}
137132f2eac1SHans de Goede 	}
13728f62ca64SPatrick Georgi 
13738f62ca64SPatrick Georgi 	/* hook up to periodic list */
13748f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
13758f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
137698ae840aSRob Herring 	list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
13778f62ca64SPatrick Georgi 
137898ae840aSRob Herring 	flush_dcache_range((unsigned long)result->last,
1379d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, result->last, 1));
138098ae840aSRob Herring 	flush_dcache_range((unsigned long)list,
1381d3e07478SStephen Warren 			   ALIGN_END_ADDR(struct QH, list, 1));
1382d3e07478SStephen Warren 
13838f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
13848f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
13858f62ca64SPatrick Georgi 		goto fail3;
13868f62ca64SPatrick Georgi 	}
138736b73109SHans de Goede 	ctrl->periodic_schedules++;
13888f62ca64SPatrick Georgi 
13898f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
13908f62ca64SPatrick Georgi 	return result;
13918f62ca64SPatrick Georgi fail3:
13928f62ca64SPatrick Georgi 	if (result->tds)
13938f62ca64SPatrick Georgi 		free(result->tds);
13948f62ca64SPatrick Georgi fail2:
13958f62ca64SPatrick Georgi 	if (result->first)
13968f62ca64SPatrick Georgi 		free(result->first);
13978f62ca64SPatrick Georgi 	if (result)
13988f62ca64SPatrick Georgi 		free(result);
13998f62ca64SPatrick Georgi fail1:
14008f62ca64SPatrick Georgi 	return NULL;
14018f62ca64SPatrick Georgi }
14028f62ca64SPatrick Georgi 
14038f62ca64SPatrick Georgi void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
14048f62ca64SPatrick Georgi {
14058f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
1406415548d8SHans de Goede 	struct qTD *cur_td;
14078f62ca64SPatrick Georgi 
14088f62ca64SPatrick Georgi 	/* depleted queue */
14098f62ca64SPatrick Georgi 	if (cur == NULL) {
14108f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
14118f62ca64SPatrick Georgi 		return NULL;
14128f62ca64SPatrick Georgi 	}
14138f62ca64SPatrick Georgi 	/* still active */
1414415548d8SHans de Goede 	cur_td = &queue->tds[queue->current - queue->first];
141598ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur_td,
1416415548d8SHans de Goede 				ALIGN_END_ADDR(struct qTD, cur_td, 1));
1417415548d8SHans de Goede 	if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1418415548d8SHans de Goede 			QT_TOKEN_STATUS_ACTIVE) {
1419415548d8SHans de Goede 		debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1420415548d8SHans de Goede 		      hc32_to_cpu(cur_td->qt_token));
14218f62ca64SPatrick Georgi 		return NULL;
14228f62ca64SPatrick Georgi 	}
14238f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
14248f62ca64SPatrick Georgi 		queue->current++;
14258f62ca64SPatrick Georgi 	else
14268f62ca64SPatrick Georgi 		queue->current = NULL;
14278aa26b8eSHans de Goede 
142898ae840aSRob Herring 	invalidate_dcache_range((unsigned long)cur->buffer,
14298aa26b8eSHans de Goede 				ALIGN_END_ADDR(char, cur->buffer,
14308aa26b8eSHans de Goede 					       queue->elementsize));
14318aa26b8eSHans de Goede 
1432415548d8SHans de Goede 	debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1433415548d8SHans de Goede 	      hc32_to_cpu(cur_td->qt_token), cur, queue->first);
14348f62ca64SPatrick Georgi 	return cur->buffer;
14358f62ca64SPatrick Georgi }
14368f62ca64SPatrick Georgi 
14378f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
14388460b89aSHans de Goede int
14398f62ca64SPatrick Georgi destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
14408f62ca64SPatrick Georgi {
144124ed894fSSimon Glass 	struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
14428f62ca64SPatrick Georgi 	int result = -1;
14438f62ca64SPatrick Georgi 	unsigned long timeout;
14448f62ca64SPatrick Georgi 
14458f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
14468f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
14478f62ca64SPatrick Georgi 		goto out;
14488f62ca64SPatrick Georgi 	}
144936b73109SHans de Goede 	ctrl->periodic_schedules--;
14508f62ca64SPatrick Georgi 
14518f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
14528f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
1453ea427775SAdrian Cox 	while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
14548f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
14558f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
14568f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
14578f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
145898ae840aSRob Herring 			flush_dcache_range((unsigned long)cur,
1459ea7b30c5SHans de Goede 					   ALIGN_END_ADDR(struct QH, cur, 1));
14608f62ca64SPatrick Georgi 			result = 0;
14618f62ca64SPatrick Georgi 			break;
14628f62ca64SPatrick Georgi 		}
14638f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
14648f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
14658f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
14668f62ca64SPatrick Georgi 			result = -1;
14678f62ca64SPatrick Georgi 			goto out;
14688f62ca64SPatrick Georgi 		}
14698f62ca64SPatrick Georgi 	}
14708f62ca64SPatrick Georgi 
147136b73109SHans de Goede 	if (ctrl->periodic_schedules > 0) {
14728f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
14738f62ca64SPatrick Georgi 		if (result < 0)
14748f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
14758f62ca64SPatrick Georgi 	}
14768f62ca64SPatrick Georgi 
14778f62ca64SPatrick Georgi out:
14788f62ca64SPatrick Georgi 	free(queue->tds);
14798f62ca64SPatrick Georgi 	free(queue->first);
14808f62ca64SPatrick Georgi 	free(queue);
14818f62ca64SPatrick Georgi 
14828f62ca64SPatrick Georgi 	return result;
14838f62ca64SPatrick Georgi }
14848f62ca64SPatrick Georgi 
148524ed894fSSimon Glass static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
148624ed894fSSimon Glass 				void *buffer, int length, int interval)
14872731b9a8SJean-Christophe PLAGNIOL-VILLARD {
14888f62ca64SPatrick Georgi 	void *backbuffer;
14898f62ca64SPatrick Georgi 	struct int_queue *queue;
14908f62ca64SPatrick Georgi 	unsigned long timeout;
14918f62ca64SPatrick Georgi 	int result = 0, ret;
14928f62ca64SPatrick Georgi 
14932731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
14942731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
149544ae0be7SBenoît Thébaudeau 
14968bb6c1d1SHans de Goede 	queue = create_int_queue(dev, pipe, 1, length, buffer, interval);
1497bd818d81SHans de Goede 	if (!queue)
1498bd818d81SHans de Goede 		return -1;
14998f62ca64SPatrick Georgi 
15008f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
15018f62ca64SPatrick Georgi 	while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
15028f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
15038f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
15048f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
15058f62ca64SPatrick Georgi 			break;
15068f62ca64SPatrick Georgi 		}
15078f62ca64SPatrick Georgi 
15088f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
150998ae840aSRob Herring 		debug("got wrong buffer back (%p instead of %p)\n",
151098ae840aSRob Herring 		      backbuffer, buffer);
15118f62ca64SPatrick Georgi 		return -EINVAL;
15128f62ca64SPatrick Georgi 	}
15138f62ca64SPatrick Georgi 
15148f62ca64SPatrick Georgi 	ret = destroy_int_queue(dev, queue);
15158f62ca64SPatrick Georgi 	if (ret < 0)
15168f62ca64SPatrick Georgi 		return ret;
15178f62ca64SPatrick Georgi 
15188f62ca64SPatrick Georgi 	/* everything worked out fine */
15198f62ca64SPatrick Georgi 	return result;
15202731b9a8SJean-Christophe PLAGNIOL-VILLARD }
152124ed894fSSimon Glass 
152246b01797SSimon Glass #ifndef CONFIG_DM_USB
152324ed894fSSimon Glass int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
152424ed894fSSimon Glass 			    void *buffer, int length)
152524ed894fSSimon Glass {
152624ed894fSSimon Glass 	return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
152724ed894fSSimon Glass }
152824ed894fSSimon Glass 
152924ed894fSSimon Glass int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
153024ed894fSSimon Glass 		   int length, struct devrequest *setup)
153124ed894fSSimon Glass {
153224ed894fSSimon Glass 	return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
153324ed894fSSimon Glass }
153424ed894fSSimon Glass 
153524ed894fSSimon Glass int submit_int_msg(struct usb_device *dev, unsigned long pipe,
153624ed894fSSimon Glass 		   void *buffer, int length, int interval)
153724ed894fSSimon Glass {
153824ed894fSSimon Glass 	return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
153924ed894fSSimon Glass }
154046b01797SSimon Glass #endif
154146b01797SSimon Glass 
154246b01797SSimon Glass #ifdef CONFIG_DM_USB
154346b01797SSimon Glass static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
154446b01797SSimon Glass 				   unsigned long pipe, void *buffer, int length,
154546b01797SSimon Glass 				   struct devrequest *setup)
154646b01797SSimon Glass {
154746b01797SSimon Glass 	debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
154846b01797SSimon Glass 	      dev->name, udev, udev->dev->name, udev->portnr);
154946b01797SSimon Glass 
155046b01797SSimon Glass 	return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
155146b01797SSimon Glass }
155246b01797SSimon Glass 
155346b01797SSimon Glass static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
155446b01797SSimon Glass 				unsigned long pipe, void *buffer, int length)
155546b01797SSimon Glass {
155646b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
155746b01797SSimon Glass 	return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
155846b01797SSimon Glass }
155946b01797SSimon Glass 
156046b01797SSimon Glass static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
156146b01797SSimon Glass 			       unsigned long pipe, void *buffer, int length,
156246b01797SSimon Glass 			       int interval)
156346b01797SSimon Glass {
156446b01797SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
156546b01797SSimon Glass 	return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
156646b01797SSimon Glass }
156746b01797SSimon Glass 
156846b01797SSimon Glass int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
156946b01797SSimon Glass 		  struct ehci_hcor *hcor, const struct ehci_ops *ops,
157046b01797SSimon Glass 		  uint tweaks, enum usb_init_type init)
157146b01797SSimon Glass {
157246b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
157346b01797SSimon Glass 	int ret;
157446b01797SSimon Glass 
157546b01797SSimon Glass 	debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
157646b01797SSimon Glass 	      dev->name, ctrl, hccr, hcor, init);
157746b01797SSimon Glass 
157846b01797SSimon Glass 	ehci_setup_ops(ctrl, ops);
157946b01797SSimon Glass 	ctrl->hccr = hccr;
158046b01797SSimon Glass 	ctrl->hcor = hcor;
158146b01797SSimon Glass 	ctrl->priv = ctrl;
158246b01797SSimon Glass 
158346b01797SSimon Glass 	if (init == USB_INIT_DEVICE)
158446b01797SSimon Glass 		goto done;
158546b01797SSimon Glass 	ret = ehci_reset(ctrl);
158646b01797SSimon Glass 	if (ret)
158746b01797SSimon Glass 		goto err;
158846b01797SSimon Glass 
158946b01797SSimon Glass 	ret = ehci_common_init(ctrl, tweaks);
159046b01797SSimon Glass 	if (ret)
159146b01797SSimon Glass 		goto err;
159246b01797SSimon Glass done:
159346b01797SSimon Glass 	return 0;
159446b01797SSimon Glass err:
159546b01797SSimon Glass 	free(ctrl);
159646b01797SSimon Glass 	debug("%s: failed, ret=%d\n", __func__, ret);
159746b01797SSimon Glass 	return ret;
159846b01797SSimon Glass }
159946b01797SSimon Glass 
160046b01797SSimon Glass int ehci_deregister(struct udevice *dev)
160146b01797SSimon Glass {
160246b01797SSimon Glass 	struct ehci_ctrl *ctrl = dev_get_priv(dev);
160346b01797SSimon Glass 
160446b01797SSimon Glass 	ehci_shutdown(ctrl);
160546b01797SSimon Glass 
160646b01797SSimon Glass 	return 0;
160746b01797SSimon Glass }
160846b01797SSimon Glass 
160946b01797SSimon Glass struct dm_usb_ops ehci_usb_ops = {
161046b01797SSimon Glass 	.control = ehci_submit_control_msg,
161146b01797SSimon Glass 	.bulk = ehci_submit_bulk_msg,
161246b01797SSimon Glass 	.interrupt = ehci_submit_int_msg,
161346b01797SSimon Glass };
161446b01797SSimon Glass 
161546b01797SSimon Glass #endif
1616