xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-hcd.c (revision ed10e66aba4d6a0151dc6dff2fa93d5fd19bc8a2)
12731b9a8SJean-Christophe PLAGNIOL-VILLARD /*-
22731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2007-2008, Juniper Networks, Inc.
32731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Excito Elektronik i Skåne AB
42731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
52731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
62731b9a8SJean-Christophe PLAGNIOL-VILLARD  * All rights reserved.
72731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
82731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is free software; you can redistribute it and/or
92731b9a8SJean-Christophe PLAGNIOL-VILLARD  * modify it under the terms of the GNU General Public License as
102731b9a8SJean-Christophe PLAGNIOL-VILLARD  * published by the Free Software Foundation version 2 of
112731b9a8SJean-Christophe PLAGNIOL-VILLARD  * the License.
122731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
132731b9a8SJean-Christophe PLAGNIOL-VILLARD  * This program is distributed in the hope that it will be useful,
142731b9a8SJean-Christophe PLAGNIOL-VILLARD  * but WITHOUT ANY WARRANTY; without even the implied warranty of
152731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162731b9a8SJean-Christophe PLAGNIOL-VILLARD  * GNU General Public License for more details.
172731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
182731b9a8SJean-Christophe PLAGNIOL-VILLARD  * You should have received a copy of the GNU General Public License
192731b9a8SJean-Christophe PLAGNIOL-VILLARD  * along with this program; if not, write to the Free Software
202731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
212731b9a8SJean-Christophe PLAGNIOL-VILLARD  * MA 02111-1307 USA
222731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
232731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
248f62ca64SPatrick Georgi #include <errno.h>
252731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/byteorder.h>
2693ad908cSLucas Stach #include <asm/unaligned.h>
272731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
282731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
292731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <malloc.h>
3067333f76SStefan Roese #include <watchdog.h>
318f62ca64SPatrick Georgi #include <linux/compiler.h>
322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
332731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
342731b9a8SJean-Christophe PLAGNIOL-VILLARD 
35676ae068SLucas Stach #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
36676ae068SLucas Stach #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37676ae068SLucas Stach #endif
382731b9a8SJean-Christophe PLAGNIOL-VILLARD 
39676ae068SLucas Stach static struct ehci_ctrl {
40676ae068SLucas Stach 	struct ehci_hccr *hccr;	/* R/O registers, not need for volatile */
41676ae068SLucas Stach 	struct ehci_hcor *hcor;
42676ae068SLucas Stach 	int rootdev;
43676ae068SLucas Stach 	uint16_t portreset;
448f62ca64SPatrick Georgi 	struct QH qh_list __aligned(USB_DMA_MINALIGN);
458f62ca64SPatrick Georgi 	struct QH periodic_queue __aligned(USB_DMA_MINALIGN);
468f62ca64SPatrick Georgi 	uint32_t *periodic_list;
478f62ca64SPatrick Georgi 	int ntds;
48676ae068SLucas Stach } ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
4971c5de4fSTom Rini 
5071c5de4fSTom Rini #define ALIGN_END_ADDR(type, ptr, size)			\
5171c5de4fSTom Rini 	((uint32_t)(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 
125b068deb3SJim Lin int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
126b068deb3SJim Lin {
127b068deb3SJim Lin 	return PORTSC_PSPD(reg);
128b068deb3SJim Lin }
129b068deb3SJim Lin 
130b068deb3SJim Lin int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
131b068deb3SJim Lin 	__attribute__((weak, alias("__ehci_get_port_speed")));
132b068deb3SJim Lin 
133b068deb3SJim Lin void __ehci_set_usbmode(int index)
134b068deb3SJim Lin {
135b068deb3SJim Lin 	uint32_t tmp;
136b068deb3SJim Lin 	uint32_t *reg_ptr;
137b068deb3SJim Lin 
138b068deb3SJim Lin 	reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
139b068deb3SJim Lin 	tmp = ehci_readl(reg_ptr);
140b068deb3SJim Lin 	tmp |= USBMODE_CM_HC;
141b068deb3SJim Lin #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
142b068deb3SJim Lin 	tmp |= USBMODE_BE;
143b068deb3SJim Lin #endif
144b068deb3SJim Lin 	ehci_writel(reg_ptr, tmp);
145b068deb3SJim Lin }
146b068deb3SJim Lin 
147b068deb3SJim Lin void ehci_set_usbmode(int index)
148b068deb3SJim Lin 	__attribute__((weak, alias("__ehci_set_usbmode")));
149b068deb3SJim Lin 
1503874b6d6SMarek Vasut void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
1513874b6d6SMarek Vasut {
1523874b6d6SMarek Vasut 	mdelay(50);
1533874b6d6SMarek Vasut }
1543874b6d6SMarek Vasut 
1553874b6d6SMarek Vasut void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
1563874b6d6SMarek Vasut 	__attribute__((weak, alias("__ehci_powerup_fixup")));
1573874b6d6SMarek Vasut 
1582731b9a8SJean-Christophe PLAGNIOL-VILLARD static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
1592731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t result;
1612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
1622731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result = ehci_readl(ptr);
16309c83a45SWolfgang Denk 		udelay(5);
1642731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == ~(uint32_t)0)
1652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return -1;
1662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		result &= mask;
1672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (result == done)
1682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			return 0;
1692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		usec--;
1702731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} while (usec > 0);
1712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
1722731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1732731b9a8SJean-Christophe PLAGNIOL-VILLARD 
174676ae068SLucas Stach static int ehci_reset(int index)
1752731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
1772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
1782731b9a8SJean-Christophe PLAGNIOL-VILLARD 
179676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
180273d7204SStefan Roese 	cmd = (cmd & ~CMD_RUN) | CMD_RESET;
181676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
182676ae068SLucas Stach 	ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
183676ae068SLucas Stach 			CMD_RESET, 0, 250 * 1000);
1842731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
1852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		printf("EHCI fail to reset\n");
1862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto out;
1872731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
1882731b9a8SJean-Christophe PLAGNIOL-VILLARD 
189b068deb3SJim Lin 	if (ehci_is_TDI())
190b068deb3SJim Lin 		ehci_set_usbmode(index);
1919ab4ce22SSimon Glass 
1929ab4ce22SSimon Glass #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
193676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
19414eb79b7SBenoît Thébaudeau 	cmd &= ~TXFIFO_THRESH_MASK;
1959ab4ce22SSimon Glass 	cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
196676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
1979ab4ce22SSimon Glass #endif
1982731b9a8SJean-Christophe PLAGNIOL-VILLARD out:
1992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ret;
2002731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2022731b9a8SJean-Christophe PLAGNIOL-VILLARD static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
2032731b9a8SJean-Christophe PLAGNIOL-VILLARD {
204b8adb120SMarek Vasut 	uint32_t delta, next;
205b8adb120SMarek Vasut 	uint32_t addr = (uint32_t)buf;
2062731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int idx;
2072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
208189a6956SIlya Yanok 	if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
209b8adb120SMarek Vasut 		debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
210b8adb120SMarek Vasut 
211189a6956SIlya Yanok 	flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
212189a6956SIlya Yanok 
2132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	idx = 0;
214cdeb9161SBenoît Thébaudeau 	while (idx < QT_BUFFER_CNT) {
2152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		td->qt_buffer[idx] = cpu_to_hc32(addr);
2163ed16071SWolfgang Denk 		td->qt_buffer_hi[idx] = 0;
21714eb79b7SBenoît Thébaudeau 		next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
2182731b9a8SJean-Christophe PLAGNIOL-VILLARD 		delta = next - addr;
2192731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (delta >= sz)
2202731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
2212731b9a8SJean-Christophe PLAGNIOL-VILLARD 		sz -= delta;
2222731b9a8SJean-Christophe PLAGNIOL-VILLARD 		addr = next;
2232731b9a8SJean-Christophe PLAGNIOL-VILLARD 		idx++;
2242731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2252731b9a8SJean-Christophe PLAGNIOL-VILLARD 
226cdeb9161SBenoît Thébaudeau 	if (idx == QT_BUFFER_CNT) {
2272af16f85SIlya Yanok 		printf("out of buffer pointers (%u bytes left)\n", sz);
2282731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
2292731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
2302731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2312731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
2322731b9a8SJean-Christophe PLAGNIOL-VILLARD }
2332731b9a8SJean-Christophe PLAGNIOL-VILLARD 
234c60795f4SIlya Yanok static inline u8 ehci_encode_speed(enum usb_device_speed speed)
235c60795f4SIlya Yanok {
236c60795f4SIlya Yanok 	#define QH_HIGH_SPEED	2
237c60795f4SIlya Yanok 	#define QH_FULL_SPEED	0
238c60795f4SIlya Yanok 	#define QH_LOW_SPEED	1
239c60795f4SIlya Yanok 	if (speed == USB_SPEED_HIGH)
240c60795f4SIlya Yanok 		return QH_HIGH_SPEED;
241c60795f4SIlya Yanok 	if (speed == USB_SPEED_LOW)
242c60795f4SIlya Yanok 		return QH_LOW_SPEED;
243c60795f4SIlya Yanok 	return QH_FULL_SPEED;
244c60795f4SIlya Yanok }
245c60795f4SIlya Yanok 
2462731b9a8SJean-Christophe PLAGNIOL-VILLARD static int
2472731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
2482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *req)
2492731b9a8SJean-Christophe PLAGNIOL-VILLARD {
25071c5de4fSTom Rini 	ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
2515cec214eSBenoît Thébaudeau 	struct qTD *qtd;
2525cec214eSBenoît Thébaudeau 	int qtd_count = 0;
253de98e8b2SMarek Vasut 	int qtd_counter = 0;
2542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	volatile struct qTD *vtd;
2552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	unsigned long ts;
2562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *tdp;
257db191346SBenoît Thébaudeau 	uint32_t endpt, maxpacket, token, usbsts;
2582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t c, toggle;
2592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
26096820a35SSimon Glass 	int timeout;
2612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int ret = 0;
262676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
2632731b9a8SJean-Christophe PLAGNIOL-VILLARD 
2642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
2652731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      buffer, length, req);
2662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL)
2672731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
2682731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->request, req->request,
2692731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      req->requesttype, req->requesttype,
2702731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->value), le16_to_cpu(req->value),
2712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		      le16_to_cpu(req->index));
2722731b9a8SJean-Christophe PLAGNIOL-VILLARD 
273db191346SBenoît Thébaudeau #define PKT_ALIGN	512
2745cec214eSBenoît Thébaudeau 	/*
2755cec214eSBenoît Thébaudeau 	 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
2765cec214eSBenoît Thébaudeau 	 * described by a transfer descriptor (the qTD). The qTDs form a linked
2775cec214eSBenoît Thébaudeau 	 * list with a queue head (QH).
2785cec214eSBenoît Thébaudeau 	 *
2795cec214eSBenoît Thébaudeau 	 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
2805cec214eSBenoît Thébaudeau 	 * have its beginning in a qTD transfer and its end in the following
2815cec214eSBenoît Thébaudeau 	 * one, so the qTD transfer lengths have to be chosen accordingly.
2825cec214eSBenoît Thébaudeau 	 *
2835cec214eSBenoît Thébaudeau 	 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
2845cec214eSBenoît Thébaudeau 	 * single pages. The first data buffer can start at any offset within a
2855cec214eSBenoît Thébaudeau 	 * page (not considering the cache-line alignment issues), while the
2865cec214eSBenoît Thébaudeau 	 * following buffers must be page-aligned. There is no alignment
2875cec214eSBenoît Thébaudeau 	 * constraint on the size of a qTD transfer.
2885cec214eSBenoît Thébaudeau 	 */
2895cec214eSBenoît Thébaudeau 	if (req != NULL)
2905cec214eSBenoît Thébaudeau 		/* 1 qTD will be needed for SETUP, and 1 for ACK. */
2915cec214eSBenoît Thébaudeau 		qtd_count += 1 + 1;
2925cec214eSBenoît Thébaudeau 	if (length > 0 || req == NULL) {
2935cec214eSBenoît Thébaudeau 		/*
2945cec214eSBenoît Thébaudeau 		 * Determine the qTD transfer size that will be used for the
295db191346SBenoît Thébaudeau 		 * data payload (not considering the first qTD transfer, which
296db191346SBenoît Thébaudeau 		 * may be longer or shorter, and the final one, which may be
297db191346SBenoît Thébaudeau 		 * shorter).
2985cec214eSBenoît Thébaudeau 		 *
2995cec214eSBenoît Thébaudeau 		 * In order to keep each packet within a qTD transfer, the qTD
300db191346SBenoît Thébaudeau 		 * transfer size is aligned to PKT_ALIGN, which is a multiple of
301db191346SBenoît Thébaudeau 		 * wMaxPacketSize (except in some cases for interrupt transfers,
302db191346SBenoît Thébaudeau 		 * see comment in submit_int_msg()).
3035cec214eSBenoît Thébaudeau 		 *
304db191346SBenoît Thébaudeau 		 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
3055cec214eSBenoît Thébaudeau 		 * QT_BUFFER_CNT full pages will be used.
3065cec214eSBenoît Thébaudeau 		 */
3075cec214eSBenoît Thébaudeau 		int xfr_sz = QT_BUFFER_CNT;
3085cec214eSBenoît Thébaudeau 		/*
309db191346SBenoît Thébaudeau 		 * However, if the input buffer is not aligned to PKT_ALIGN, the
310db191346SBenoît Thébaudeau 		 * qTD transfer size will be one page shorter, and the first qTD
3115cec214eSBenoît Thébaudeau 		 * data buffer of each transfer will be page-unaligned.
3125cec214eSBenoît Thébaudeau 		 */
313db191346SBenoît Thébaudeau 		if ((uint32_t)buffer & (PKT_ALIGN - 1))
3145cec214eSBenoît Thébaudeau 			xfr_sz--;
3155cec214eSBenoît Thébaudeau 		/* Convert the qTD transfer size to bytes. */
3165cec214eSBenoît Thébaudeau 		xfr_sz *= EHCI_PAGE_SIZE;
3175cec214eSBenoît Thébaudeau 		/*
318db191346SBenoît Thébaudeau 		 * Approximate by excess the number of qTDs that will be
319db191346SBenoît Thébaudeau 		 * required for the data payload. The exact formula is way more
320db191346SBenoît Thébaudeau 		 * complicated and saves at most 2 qTDs, i.e. a total of 128
321db191346SBenoît Thébaudeau 		 * bytes.
3225cec214eSBenoît Thébaudeau 		 */
323db191346SBenoît Thébaudeau 		qtd_count += 2 + length / xfr_sz;
3245cec214eSBenoît Thébaudeau 	}
3255cec214eSBenoît Thébaudeau /*
326db191346SBenoît Thébaudeau  * Threshold value based on the worst-case total size of the allocated qTDs for
327db191346SBenoît Thébaudeau  * a mass-storage transfer of 65535 blocks of 512 bytes.
3285cec214eSBenoît Thébaudeau  */
329db191346SBenoît Thébaudeau #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
3305cec214eSBenoît Thébaudeau #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
3315cec214eSBenoît Thébaudeau #endif
3325cec214eSBenoît Thébaudeau 	qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
3335cec214eSBenoît Thébaudeau 	if (qtd == NULL) {
3345cec214eSBenoît Thébaudeau 		printf("unable to allocate TDs\n");
3355cec214eSBenoît Thébaudeau 		return -1;
3365cec214eSBenoît Thébaudeau 	}
3375cec214eSBenoît Thébaudeau 
33871c5de4fSTom Rini 	memset(qh, 0, sizeof(struct QH));
3395cec214eSBenoît Thébaudeau 	memset(qtd, 0, qtd_count * sizeof(*qtd));
340de98e8b2SMarek Vasut 
341b8adb120SMarek Vasut 	toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
342b8adb120SMarek Vasut 
34341b1f0acSMarek Vasut 	/*
34441b1f0acSMarek Vasut 	 * Setup QH (3.6 in ehci-r10.pdf)
34541b1f0acSMarek Vasut 	 *
34641b1f0acSMarek Vasut 	 *   qh_link ................. 03-00 H
34741b1f0acSMarek Vasut 	 *   qh_endpt1 ............... 07-04 H
34841b1f0acSMarek Vasut 	 *   qh_endpt2 ............... 0B-08 H
34941b1f0acSMarek Vasut 	 * - qh_curtd
35041b1f0acSMarek Vasut 	 *   qh_overlay.qt_next ...... 13-10 H
35141b1f0acSMarek Vasut 	 * - qh_overlay.qt_altnext
35241b1f0acSMarek Vasut 	 */
353676ae068SLucas Stach 	qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
354c60795f4SIlya Yanok 	c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
355db191346SBenoît Thébaudeau 	maxpacket = usb_maxpacket(dev, pipe);
35614eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
357db191346SBenoît Thébaudeau 		QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
35814eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
359c60795f4SIlya Yanok 		QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
36014eb79b7SBenoît Thébaudeau 		QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
36114eb79b7SBenoît Thébaudeau 		QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
36271c5de4fSTom Rini 	qh->qh_endpt1 = cpu_to_hc32(endpt);
36314eb79b7SBenoît Thébaudeau 	endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
36414eb79b7SBenoît Thébaudeau 		QH_ENDPT2_HUBADDR(dev->parent->devnum) |
36514eb79b7SBenoît Thébaudeau 		QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
36671c5de4fSTom Rini 	qh->qh_endpt2 = cpu_to_hc32(endpt);
36771c5de4fSTom Rini 	qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
3682731b9a8SJean-Christophe PLAGNIOL-VILLARD 
36971c5de4fSTom Rini 	tdp = &qh->qh_overlay.qt_next;
3702731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3712731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
37241b1f0acSMarek Vasut 		/*
37341b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
37441b1f0acSMarek Vasut 		 *
37541b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
37641b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
37741b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
37841b1f0acSMarek Vasut 		 *
37941b1f0acSMarek Vasut 		 *   [ buffer, buffer_hi ] loaded with "req".
38041b1f0acSMarek Vasut 		 */
381de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
382de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
38314eb79b7SBenoît Thébaudeau 		token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
38414eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
38514eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
38614eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
387de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
38814eb79b7SBenoît Thébaudeau 		if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
38914eb79b7SBenoît Thébaudeau 			printf("unable to construct SETUP TD\n");
3902731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto fail;
3912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
39241b1f0acSMarek Vasut 		/* Update previous qTD! */
393de98e8b2SMarek Vasut 		*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
394de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
3952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		toggle = 1;
3962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
3972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3982731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (length > 0 || req == NULL) {
3995cec214eSBenoît Thébaudeau 		uint8_t *buf_ptr = buffer;
4005cec214eSBenoît Thébaudeau 		int left_length = length;
4015cec214eSBenoît Thébaudeau 
4025cec214eSBenoît Thébaudeau 		do {
4035cec214eSBenoît Thébaudeau 			/*
4045cec214eSBenoît Thébaudeau 			 * Determine the size of this qTD transfer. By default,
4055cec214eSBenoît Thébaudeau 			 * QT_BUFFER_CNT full pages can be used.
4065cec214eSBenoît Thébaudeau 			 */
4075cec214eSBenoît Thébaudeau 			int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
4085cec214eSBenoît Thébaudeau 			/*
4095cec214eSBenoît Thébaudeau 			 * However, if the input buffer is not page-aligned, the
4105cec214eSBenoît Thébaudeau 			 * portion of the first page before the buffer start
4115cec214eSBenoît Thébaudeau 			 * offset within that page is unusable.
4125cec214eSBenoît Thébaudeau 			 */
4135cec214eSBenoît Thébaudeau 			xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
4145cec214eSBenoît Thébaudeau 			/*
4155cec214eSBenoît Thébaudeau 			 * In order to keep each packet within a qTD transfer,
416db191346SBenoît Thébaudeau 			 * align the qTD transfer size to PKT_ALIGN.
4175cec214eSBenoît Thébaudeau 			 */
418db191346SBenoît Thébaudeau 			xfr_bytes &= ~(PKT_ALIGN - 1);
4195cec214eSBenoît Thébaudeau 			/*
4205cec214eSBenoît Thébaudeau 			 * This transfer may be shorter than the available qTD
4215cec214eSBenoît Thébaudeau 			 * transfer size that has just been computed.
4225cec214eSBenoît Thébaudeau 			 */
4235cec214eSBenoît Thébaudeau 			xfr_bytes = min(xfr_bytes, left_length);
4245cec214eSBenoît Thébaudeau 
42541b1f0acSMarek Vasut 			/*
42641b1f0acSMarek Vasut 			 * Setup request qTD (3.5 in ehci-r10.pdf)
42741b1f0acSMarek Vasut 			 *
42841b1f0acSMarek Vasut 			 *   qt_next ................ 03-00 H
42941b1f0acSMarek Vasut 			 *   qt_altnext ............. 07-04 H
43041b1f0acSMarek Vasut 			 *   qt_token ............... 0B-08 H
43141b1f0acSMarek Vasut 			 *
43241b1f0acSMarek Vasut 			 *   [ buffer, buffer_hi ] loaded with "buffer".
43341b1f0acSMarek Vasut 			 */
4345cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_next =
4355cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4365cec214eSBenoît Thébaudeau 			qtd[qtd_counter].qt_altnext =
4375cec214eSBenoît Thébaudeau 					cpu_to_hc32(QT_NEXT_TERMINATE);
4385cec214eSBenoît Thébaudeau 			token = QT_TOKEN_DT(toggle) |
4395cec214eSBenoît Thébaudeau 				QT_TOKEN_TOTALBYTES(xfr_bytes) |
44014eb79b7SBenoît Thébaudeau 				QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
4415cec214eSBenoît Thébaudeau 				QT_TOKEN_CERR(3) |
4425cec214eSBenoît Thébaudeau 				QT_TOKEN_PID(usb_pipein(pipe) ?
44314eb79b7SBenoît Thébaudeau 					QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
44414eb79b7SBenoît Thébaudeau 				QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
445de98e8b2SMarek Vasut 			qtd[qtd_counter].qt_token = cpu_to_hc32(token);
4465cec214eSBenoît Thébaudeau 			if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
4475cec214eSBenoît Thébaudeau 						xfr_bytes)) {
44814eb79b7SBenoît Thébaudeau 				printf("unable to construct DATA TD\n");
4492731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto fail;
4502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
45141b1f0acSMarek Vasut 			/* Update previous qTD! */
452de98e8b2SMarek Vasut 			*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
453de98e8b2SMarek Vasut 			tdp = &qtd[qtd_counter++].qt_next;
454db191346SBenoît Thébaudeau 			/*
455db191346SBenoît Thébaudeau 			 * Data toggle has to be adjusted since the qTD transfer
456db191346SBenoît Thébaudeau 			 * size is not always an even multiple of
457db191346SBenoît Thébaudeau 			 * wMaxPacketSize.
458db191346SBenoît Thébaudeau 			 */
459db191346SBenoît Thébaudeau 			if ((xfr_bytes / maxpacket) & 1)
460db191346SBenoît Thébaudeau 				toggle ^= 1;
4615cec214eSBenoît Thébaudeau 			buf_ptr += xfr_bytes;
4625cec214eSBenoît Thébaudeau 			left_length -= xfr_bytes;
4635cec214eSBenoît Thébaudeau 		} while (left_length > 0);
4642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4652731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (req != NULL) {
46741b1f0acSMarek Vasut 		/*
46841b1f0acSMarek Vasut 		 * Setup request qTD (3.5 in ehci-r10.pdf)
46941b1f0acSMarek Vasut 		 *
47041b1f0acSMarek Vasut 		 *   qt_next ................ 03-00 H
47141b1f0acSMarek Vasut 		 *   qt_altnext ............. 07-04 H
47241b1f0acSMarek Vasut 		 *   qt_token ............... 0B-08 H
47341b1f0acSMarek Vasut 		 */
474de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
475de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
476db191346SBenoît Thébaudeau 		token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
47714eb79b7SBenoît Thébaudeau 			QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
47814eb79b7SBenoît Thébaudeau 			QT_TOKEN_PID(usb_pipein(pipe) ?
47914eb79b7SBenoît Thébaudeau 				QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
48014eb79b7SBenoît Thébaudeau 			QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
481de98e8b2SMarek Vasut 		qtd[qtd_counter].qt_token = cpu_to_hc32(token);
48241b1f0acSMarek Vasut 		/* Update previous qTD! */
483de98e8b2SMarek Vasut 		*tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
484de98e8b2SMarek Vasut 		tdp = &qtd[qtd_counter++].qt_next;
4852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
4862731b9a8SJean-Christophe PLAGNIOL-VILLARD 
487676ae068SLucas Stach 	ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
4882731b9a8SJean-Christophe PLAGNIOL-VILLARD 
4892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Flush dcache */
490676ae068SLucas Stach 	flush_dcache_range((uint32_t)&ctrl->qh_list,
491676ae068SLucas Stach 		ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
49271c5de4fSTom Rini 	flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
49314eb79b7SBenoît Thébaudeau 	flush_dcache_range((uint32_t)qtd,
4945cec214eSBenoît Thébaudeau 			   ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
4952731b9a8SJean-Christophe PLAGNIOL-VILLARD 
496c7701af5SIlya Yanok 	/* Set async. queue head pointer. */
497676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
498c7701af5SIlya Yanok 
499676ae068SLucas Stach 	usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
500676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
5012731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Enable async. schedule. */
503676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5042731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_ASE;
505676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5062731b9a8SJean-Christophe PLAGNIOL-VILLARD 
507676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
5082731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5092731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
51014eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS set\n");
5112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
5122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5132731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Wait for TDs to be processed. */
5152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	ts = get_timer(0);
516de98e8b2SMarek Vasut 	vtd = &qtd[qtd_counter - 1];
51796820a35SSimon Glass 	timeout = USB_TIMEOUT_MS(pipe);
5182731b9a8SJean-Christophe PLAGNIOL-VILLARD 	do {
5192731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Invalidate dcache */
520676ae068SLucas Stach 		invalidate_dcache_range((uint32_t)&ctrl->qh_list,
521676ae068SLucas Stach 			ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
52271c5de4fSTom Rini 		invalidate_dcache_range((uint32_t)qh,
52371c5de4fSTom Rini 			ALIGN_END_ADDR(struct QH, qh, 1));
524b8adb120SMarek Vasut 		invalidate_dcache_range((uint32_t)qtd,
5255cec214eSBenoît Thébaudeau 			ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
526b8adb120SMarek Vasut 
5272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		token = hc32_to_cpu(vtd->qt_token);
52814eb79b7SBenoît Thébaudeau 		if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
5292731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
53067333f76SStefan Roese 		WATCHDOG_RESET();
53196820a35SSimon Glass 	} while (get_timer(ts) < timeout);
53296820a35SSimon Glass 
533189a6956SIlya Yanok 	/*
534189a6956SIlya Yanok 	 * Invalidate the memory area occupied by buffer
535189a6956SIlya Yanok 	 * Don't try to fix the buffer alignment, if it isn't properly
536189a6956SIlya Yanok 	 * aligned it's upper layer's fault so let invalidate_dcache_range()
537189a6956SIlya Yanok 	 * vow about it. But we have to fix the length as it's actual
538189a6956SIlya Yanok 	 * transfer length and can be unaligned. This is potentially
539189a6956SIlya Yanok 	 * dangerous operation, it's responsibility of the calling
540189a6956SIlya Yanok 	 * code to make sure enough space is reserved.
541189a6956SIlya Yanok 	 */
542189a6956SIlya Yanok 	invalidate_dcache_range((uint32_t)buffer,
543189a6956SIlya Yanok 		ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
544b8adb120SMarek Vasut 
54596820a35SSimon Glass 	/* Check that the TD processing happened */
54614eb79b7SBenoît Thébaudeau 	if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
54796820a35SSimon Glass 		printf("EHCI timed out on TD - token=%#x\n", token);
5482731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Disable async schedule. */
550676ae068SLucas Stach 	cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
5512731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~CMD_ASE;
552676ae068SLucas Stach 	ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
5532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
554676ae068SLucas Stach 	ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
5552731b9a8SJean-Christophe PLAGNIOL-VILLARD 			100 * 1000);
5562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (ret < 0) {
55714eb79b7SBenoît Thébaudeau 		printf("EHCI fail timeout STS_ASS reset\n");
5582731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto fail;
5592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5602731b9a8SJean-Christophe PLAGNIOL-VILLARD 
56171c5de4fSTom Rini 	token = hc32_to_cpu(qh->qh_overlay.qt_token);
56214eb79b7SBenoît Thébaudeau 	if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
5632731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("TOKEN=%#x\n", token);
56414eb79b7SBenoît Thébaudeau 		switch (QT_TOKEN_GET_STATUS(token) &
56514eb79b7SBenoît Thébaudeau 			~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
5662731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case 0:
56714eb79b7SBenoît Thébaudeau 			toggle = QT_TOKEN_GET_DT(token);
5682731b9a8SJean-Christophe PLAGNIOL-VILLARD 			usb_settoggle(dev, usb_pipeendpoint(pipe),
5692731b9a8SJean-Christophe PLAGNIOL-VILLARD 				       usb_pipeout(pipe), toggle);
5702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = 0;
5712731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
57214eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED:
5732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_STALLED;
5742731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
57514eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
57614eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_DATBUFERR:
5772731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BUF_ERR;
5782731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
57914eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
58014eb79b7SBenoît Thébaudeau 		case QT_TOKEN_STATUS_BABBLEDET:
5812731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_BABBLE_DET;
5822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
5832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
5842731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->status = USB_ST_CRC_ERR;
58514eb79b7SBenoît Thébaudeau 			if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
586222d6dffSAnatolij Gustschin 				dev->status |= USB_ST_STALLED;
5872731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
5882731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
58914eb79b7SBenoît Thébaudeau 		dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
5902731b9a8SJean-Christophe PLAGNIOL-VILLARD 	} else {
5912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		dev->act_len = 0;
5922731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
593676ae068SLucas Stach 		      dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
594676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[0]),
595676ae068SLucas Stach 		      ehci_readl(&ctrl->hcor->or_portsc[1]));
5962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
5972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
5985cec214eSBenoît Thébaudeau 	free(qtd);
5992731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
6002731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6012731b9a8SJean-Christophe PLAGNIOL-VILLARD fail:
6025cec214eSBenoît Thébaudeau 	free(qtd);
6032731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
6042731b9a8SJean-Christophe PLAGNIOL-VILLARD }
6052731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6062731b9a8SJean-Christophe PLAGNIOL-VILLARD int
6072731b9a8SJean-Christophe PLAGNIOL-VILLARD ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
6082731b9a8SJean-Christophe PLAGNIOL-VILLARD 		 int length, struct devrequest *req)
6092731b9a8SJean-Christophe PLAGNIOL-VILLARD {
6102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint8_t tmpbuf[4];
6112731b9a8SJean-Christophe PLAGNIOL-VILLARD 	u16 typeReq;
6122731b9a8SJean-Christophe PLAGNIOL-VILLARD 	void *srcptr = NULL;
6132731b9a8SJean-Christophe PLAGNIOL-VILLARD 	int len, srclen;
6142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
6152731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t *status_reg;
6167d9aa8fdSJulius Werner 	int port = le16_to_cpu(req->index) & 0xff;
617676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
6182731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6197d9aa8fdSJulius Werner 	if (port > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
6207d9aa8fdSJulius Werner 		printf("The request port(%d) is not configured\n", port - 1);
6212731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
6222731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
6237d9aa8fdSJulius Werner 	status_reg = (uint32_t *)&ctrl->hcor->or_portsc[port - 1];
6242731b9a8SJean-Christophe PLAGNIOL-VILLARD 	srclen = 0;
6252731b9a8SJean-Christophe PLAGNIOL-VILLARD 
6262731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
6272731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->request, req->request,
6282731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->requesttype,
6292731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->value), le16_to_cpu(req->index));
6302731b9a8SJean-Christophe PLAGNIOL-VILLARD 
63144259bb9SPrafulla Wadaskar 	typeReq = req->request | req->requesttype << 8;
6322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
63344259bb9SPrafulla Wadaskar 	switch (typeReq) {
6342731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
6352731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
6362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_DEVICE:
6372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_DEVICE request\n");
6382731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.device;
63914eb79b7SBenoît Thébaudeau 			srclen = descriptor.device.bLength;
6402731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6412731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_CONFIG:
6422731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_CONFIG config\n");
6432731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.config;
64414eb79b7SBenoît Thébaudeau 			srclen = descriptor.config.bLength +
64514eb79b7SBenoît Thébaudeau 					descriptor.interface.bLength +
64614eb79b7SBenoît Thébaudeau 					descriptor.endpoint.bLength;
6472731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_STRING:
6492731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_STRING config\n");
6502731b9a8SJean-Christophe PLAGNIOL-VILLARD 			switch (le16_to_cpu(req->value) & 0xff) {
6512731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 0:	/* Language */
6522731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\4\3\1\0";
6532731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 4;
6542731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
6552731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 1:	/* Vendor */
6562731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
6572731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 14;
6582731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
6592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			case 2:	/* Product */
6602731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srcptr = "\52\3E\0H\0C\0I\0 "
6612731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0H\0o\0s\0t\0 "
6622731b9a8SJean-Christophe PLAGNIOL-VILLARD 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
6632731b9a8SJean-Christophe PLAGNIOL-VILLARD 				srclen = 42;
6642731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
6652731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
6662731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("unknown value DT_STRING %x\n",
6672731b9a8SJean-Christophe PLAGNIOL-VILLARD 					le16_to_cpu(req->value));
6682731b9a8SJean-Christophe PLAGNIOL-VILLARD 				goto unknown;
6692731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
6702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6712731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6722731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
6732731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
6742731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
6752731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
6762731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
6772731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value) >> 8) {
6782731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_DT_HUB:
6792731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("USB_DT_HUB config\n");
6802731b9a8SJean-Christophe PLAGNIOL-VILLARD 			srcptr = &descriptor.hub;
68114eb79b7SBenoît Thébaudeau 			srclen = descriptor.hub.bLength;
6822731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
6832731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
6842731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown value %x\n", le16_to_cpu(req->value));
6852731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
6862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
6872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
6882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
6892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_ADDRESS\n");
690676ae068SLucas Stach 		ctrl->rootdev = le16_to_cpu(req->value);
6912731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
6922731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
6932731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("USB_REQ_SET_CONFIGURATION\n");
6942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* Nothing to do */
6952731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
6962731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
6972731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
6982731b9a8SJean-Christophe PLAGNIOL-VILLARD 		tmpbuf[1] = 0;
6992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 2;
7012731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7022731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
7032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memset(tmpbuf, 0, 4);
7042731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
7052731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CS)
7062731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
7072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PE)
7082731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
7092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_SUSP)
7102731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
7112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCA)
7122731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
713c8b2d1dcSSergei Shtylyov 		if (reg & EHCI_PS_PR)
7142731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[0] |= USB_PORT_STAT_RESET;
7152731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PP)
7162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
7172731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7182731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (ehci_is_TDI()) {
719b068deb3SJim Lin 			switch (ehci_get_port_speed(ctrl->hcor, reg)) {
72014eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_FS:
7212731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
72214eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_LS:
7232731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
7242731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
72514eb79b7SBenoît Thébaudeau 			case PORTSC_PSPD_HS:
7262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			default:
7272731b9a8SJean-Christophe PLAGNIOL-VILLARD 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7282731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7292731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7302731b9a8SJean-Christophe PLAGNIOL-VILLARD 		} else {
7312731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
7322731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
7332731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7342731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_CSC)
7352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
7362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_PEC)
7372731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
7382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		if (reg & EHCI_PS_OCC)
7392731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
7407d9aa8fdSJulius Werner 		if (ctrl->portreset & (1 << port))
7412731b9a8SJean-Christophe PLAGNIOL-VILLARD 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
7422731b9a8SJean-Christophe PLAGNIOL-VILLARD 
7432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srcptr = tmpbuf;
7442731b9a8SJean-Christophe PLAGNIOL-VILLARD 		srclen = 4;
7452731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
7462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
7472731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
7482731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg &= ~EHCI_PS_CLEAR;
7492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
7502731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
7512731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg |= EHCI_PS_PE;
7522731b9a8SJean-Christophe PLAGNIOL-VILLARD 			ehci_writel(status_reg, reg);
7532731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7542731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
755676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
7562731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PP;
7572731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
7582731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7592731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7602731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_RESET:
7612731b9a8SJean-Christophe PLAGNIOL-VILLARD 			if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
7622731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    !ehci_is_TDI() &&
7632731b9a8SJean-Christophe PLAGNIOL-VILLARD 			    EHCI_PS_IS_LOWSPEED(reg)) {
7642731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/* Low speed device, give up ownership. */
7652731b9a8SJean-Christophe PLAGNIOL-VILLARD 				debug("port %d low speed --> companion\n",
7667d9aa8fdSJulius Werner 				      port - 1);
7672731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PO;
7682731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
7692731b9a8SJean-Christophe PLAGNIOL-VILLARD 				break;
7702731b9a8SJean-Christophe PLAGNIOL-VILLARD 			} else {
771c8b2d1dcSSergei Shtylyov 				int ret;
772c8b2d1dcSSergei Shtylyov 
7732731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg |= EHCI_PS_PR;
7742731b9a8SJean-Christophe PLAGNIOL-VILLARD 				reg &= ~EHCI_PS_PE;
7752731b9a8SJean-Christophe PLAGNIOL-VILLARD 				ehci_writel(status_reg, reg);
7762731b9a8SJean-Christophe PLAGNIOL-VILLARD 				/*
7772731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * caller must wait, then call GetPortStatus
7782731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * usb 2.0 specification say 50 ms resets on
7792731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 * root
7802731b9a8SJean-Christophe PLAGNIOL-VILLARD 				 */
7813874b6d6SMarek Vasut 				ehci_powerup_fixup(status_reg, &reg);
7823874b6d6SMarek Vasut 
783b416191aSChris Zhang 				ehci_writel(status_reg, reg & ~EHCI_PS_PR);
784c8b2d1dcSSergei Shtylyov 				/*
785c8b2d1dcSSergei Shtylyov 				 * A host controller must terminate the reset
786c8b2d1dcSSergei Shtylyov 				 * and stabilize the state of the port within
787c8b2d1dcSSergei Shtylyov 				 * 2 milliseconds
788c8b2d1dcSSergei Shtylyov 				 */
789c8b2d1dcSSergei Shtylyov 				ret = handshake(status_reg, EHCI_PS_PR, 0,
790c8b2d1dcSSergei Shtylyov 						2 * 1000);
791c8b2d1dcSSergei Shtylyov 				if (!ret)
7927d9aa8fdSJulius Werner 					ctrl->portreset |= 1 << port;
793c8b2d1dcSSergei Shtylyov 				else
794c8b2d1dcSSergei Shtylyov 					printf("port(%d) reset error\n",
7957d9aa8fdSJulius Werner 					       port - 1);
7962731b9a8SJean-Christophe PLAGNIOL-VILLARD 			}
7972731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
7987d9aa8fdSJulius Werner 		case USB_PORT_FEAT_TEST:
7997d9aa8fdSJulius Werner 			reg &= ~(0xf << 16);
8007d9aa8fdSJulius Werner 			reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
8017d9aa8fdSJulius Werner 			ehci_writel(status_reg, reg);
8027d9aa8fdSJulius Werner 			break;
8032731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
8042731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
8052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
8062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8072731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted writes */
808676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
8092731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8102731b9a8SJean-Christophe PLAGNIOL-VILLARD 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
8112731b9a8SJean-Christophe PLAGNIOL-VILLARD 		reg = ehci_readl(status_reg);
812*ed10e66aSSimon Glass 		reg &= ~EHCI_PS_CLEAR;
8132731b9a8SJean-Christophe PLAGNIOL-VILLARD 		switch (le16_to_cpu(req->value)) {
8142731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_ENABLE:
8152731b9a8SJean-Christophe PLAGNIOL-VILLARD 			reg &= ~EHCI_PS_PE;
8162731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8172731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_ENABLE:
818*ed10e66aSSimon Glass 			reg |= EHCI_PS_PE;
8192731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8202731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_POWER:
821676ae068SLucas Stach 			if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
822*ed10e66aSSimon Glass 				reg &= ~EHCI_PS_PP;
823*ed10e66aSSimon Glass 			break;
8242731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_CONNECTION:
825*ed10e66aSSimon Glass 			reg |= EHCI_PS_CSC;
8262731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8272731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_OVER_CURRENT:
828*ed10e66aSSimon Glass 			reg |= EHCI_PS_OCC;
8292731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8302731b9a8SJean-Christophe PLAGNIOL-VILLARD 		case USB_PORT_FEAT_C_RESET:
8317d9aa8fdSJulius Werner 			ctrl->portreset &= ~(1 << port);
8322731b9a8SJean-Christophe PLAGNIOL-VILLARD 			break;
8332731b9a8SJean-Christophe PLAGNIOL-VILLARD 		default:
8342731b9a8SJean-Christophe PLAGNIOL-VILLARD 			debug("unknown feature %x\n", le16_to_cpu(req->value));
8352731b9a8SJean-Christophe PLAGNIOL-VILLARD 			goto unknown;
8362731b9a8SJean-Christophe PLAGNIOL-VILLARD 		}
8372731b9a8SJean-Christophe PLAGNIOL-VILLARD 		ehci_writel(status_reg, reg);
8382731b9a8SJean-Christophe PLAGNIOL-VILLARD 		/* unblock posted write */
839676ae068SLucas Stach 		(void) ehci_readl(&ctrl->hcor->or_usbcmd);
8402731b9a8SJean-Christophe PLAGNIOL-VILLARD 		break;
8412731b9a8SJean-Christophe PLAGNIOL-VILLARD 	default:
8422731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Unknown request\n");
8432731b9a8SJean-Christophe PLAGNIOL-VILLARD 		goto unknown;
8442731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
8452731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8465b84dd67SMike Frysinger 	mdelay(1);
8472731b9a8SJean-Christophe PLAGNIOL-VILLARD 	len = min3(srclen, le16_to_cpu(req->length), length);
8482731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (srcptr != NULL && len > 0)
8492731b9a8SJean-Christophe PLAGNIOL-VILLARD 		memcpy(buffer, srcptr, len);
8502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	else
8512731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("Len is 0\n");
8522731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8532731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = len;
8542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = 0;
8552731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
8562731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8572731b9a8SJean-Christophe PLAGNIOL-VILLARD unknown:
8582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
8592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      req->requesttype, req->request, le16_to_cpu(req->value),
8602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      le16_to_cpu(req->index), le16_to_cpu(req->length));
8612731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8622731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->act_len = 0;
8632731b9a8SJean-Christophe PLAGNIOL-VILLARD 	dev->status = USB_ST_STALLED;
8642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return -1;
8652731b9a8SJean-Christophe PLAGNIOL-VILLARD }
8662731b9a8SJean-Christophe PLAGNIOL-VILLARD 
867c7e3b2b5SLucas Stach int usb_lowlevel_stop(int index)
8682731b9a8SJean-Christophe PLAGNIOL-VILLARD {
869676ae068SLucas Stach 	return ehci_hcd_stop(index);
8702731b9a8SJean-Christophe PLAGNIOL-VILLARD }
8712731b9a8SJean-Christophe PLAGNIOL-VILLARD 
872c7e3b2b5SLucas Stach int usb_lowlevel_init(int index, void **controller)
8732731b9a8SJean-Christophe PLAGNIOL-VILLARD {
8742731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t reg;
8752731b9a8SJean-Christophe PLAGNIOL-VILLARD 	uint32_t cmd;
876676ae068SLucas Stach 	struct QH *qh_list;
8778f62ca64SPatrick Georgi 	struct QH *periodic;
8788f62ca64SPatrick Georgi 	int i;
8792731b9a8SJean-Christophe PLAGNIOL-VILLARD 
880676ae068SLucas Stach 	if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
8812731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
8822731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8832731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* EHCI spec section 4.1 */
884676ae068SLucas Stach 	if (ehci_reset(index))
8852731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
8862731b9a8SJean-Christophe PLAGNIOL-VILLARD 
8872731b9a8SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
888676ae068SLucas Stach 	if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
8892731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
8902731b9a8SJean-Christophe PLAGNIOL-VILLARD #endif
8912982837eSVincent Palatin 	/* Set the high address word (aka segment) for 64-bit controller */
8922982837eSVincent Palatin 	if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
8932982837eSVincent Palatin 		ehci_writel(ehcic[index].hcor->or_ctrldssegment, 0);
8942731b9a8SJean-Christophe PLAGNIOL-VILLARD 
895676ae068SLucas Stach 	qh_list = &ehcic[index].qh_list;
896676ae068SLucas Stach 
8972731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Set head of reclaim list */
89871c5de4fSTom Rini 	memset(qh_list, 0, sizeof(*qh_list));
89971c5de4fSTom Rini 	qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
90014eb79b7SBenoît Thébaudeau 	qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
90114eb79b7SBenoît Thébaudeau 						QH_ENDPT1_EPS(USB_SPEED_HIGH));
90271c5de4fSTom Rini 	qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
90371c5de4fSTom Rini 	qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
90471c5de4fSTom Rini 	qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
90514eb79b7SBenoît Thébaudeau 	qh_list->qh_overlay.qt_token =
90614eb79b7SBenoît Thébaudeau 			cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
9072731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9088f62ca64SPatrick Georgi 	/* Set async. queue head pointer. */
9098f62ca64SPatrick Georgi 	ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
9108f62ca64SPatrick Georgi 
9118f62ca64SPatrick Georgi 	/*
9128f62ca64SPatrick Georgi 	 * Set up periodic list
9138f62ca64SPatrick Georgi 	 * Step 1: Parent QH for all periodic transfers.
9148f62ca64SPatrick Georgi 	 */
9158f62ca64SPatrick Georgi 	periodic = &ehcic[index].periodic_queue;
9168f62ca64SPatrick Georgi 	memset(periodic, 0, sizeof(*periodic));
9178f62ca64SPatrick Georgi 	periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
9188f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
9198f62ca64SPatrick Georgi 	periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
9208f62ca64SPatrick Georgi 
9218f62ca64SPatrick Georgi 	/*
9228f62ca64SPatrick Georgi 	 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
9238f62ca64SPatrick Georgi 	 *         In particular, device specifications on polling frequency
9248f62ca64SPatrick Georgi 	 *         are disregarded. Keyboards seem to send NAK/NYet reliably
9258f62ca64SPatrick Georgi 	 *         when polled with an empty buffer.
9268f62ca64SPatrick Georgi 	 *
9278f62ca64SPatrick Georgi 	 *         Split Transactions will be spread across microframes using
9288f62ca64SPatrick Georgi 	 *         S-mask and C-mask.
9298f62ca64SPatrick Georgi 	 */
9308f62ca64SPatrick Georgi 	ehcic[index].periodic_list = memalign(4096, 1024*4);
9318f62ca64SPatrick Georgi 	if (!ehcic[index].periodic_list)
9328f62ca64SPatrick Georgi 		return -ENOMEM;
9338f62ca64SPatrick Georgi 	for (i = 0; i < 1024; i++) {
9348f62ca64SPatrick Georgi 		ehcic[index].periodic_list[i] = (uint32_t)periodic
9358f62ca64SPatrick Georgi 						| QH_LINK_TYPE_QH;
9368f62ca64SPatrick Georgi 	}
9378f62ca64SPatrick Georgi 
9388f62ca64SPatrick Georgi 	/* Set periodic list base address */
9398f62ca64SPatrick Georgi 	ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
9408f62ca64SPatrick Georgi 		(uint32_t)ehcic[index].periodic_list);
9418f62ca64SPatrick Georgi 
942676ae068SLucas Stach 	reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
9432731b9a8SJean-Christophe PLAGNIOL-VILLARD 	descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
9447a46b2c7SLucas Stach 	debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
9452731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Indicators */
9462731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_INDICATOR(reg))
94793ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
94893ad908cSLucas Stach 				| 0x80, &descriptor.hub.wHubCharacteristics);
9492731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Port Power Control */
9502731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (HCS_PPC(reg))
95193ad908cSLucas Stach 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
95293ad908cSLucas Stach 				| 0x01, &descriptor.hub.wHubCharacteristics);
9532731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9542731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* Start the host controller. */
955676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
9562731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/*
9572731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * Philips, Intel, and maybe others need CMD_RUN before the
9582731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 * root hub will detect new devices (why?); NEC doesn't
9592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	 */
9602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
9612731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= CMD_RUN;
962676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
9632731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9642731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* take control over the ports */
965676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
9662731b9a8SJean-Christophe PLAGNIOL-VILLARD 	cmd |= FLAG_CF;
967676ae068SLucas Stach 	ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
9682731b9a8SJean-Christophe PLAGNIOL-VILLARD 	/* unblock posted write */
969676ae068SLucas Stach 	cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
9705b84dd67SMike Frysinger 	mdelay(5);
971676ae068SLucas Stach 	reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
9722731b9a8SJean-Christophe PLAGNIOL-VILLARD 	printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
9732731b9a8SJean-Christophe PLAGNIOL-VILLARD 
974676ae068SLucas Stach 	ehcic[index].rootdev = 0;
9752731b9a8SJean-Christophe PLAGNIOL-VILLARD 
976676ae068SLucas Stach 	*controller = &ehcic[index];
9772731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
9782731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9792731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9802731b9a8SJean-Christophe PLAGNIOL-VILLARD int
9812731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
9822731b9a8SJean-Christophe PLAGNIOL-VILLARD 		int length)
9832731b9a8SJean-Christophe PLAGNIOL-VILLARD {
9842731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9852731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_BULK) {
9862731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
9872731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
9882731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
9892731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, NULL);
9902731b9a8SJean-Christophe PLAGNIOL-VILLARD }
9912731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9922731b9a8SJean-Christophe PLAGNIOL-VILLARD int
9932731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
9942731b9a8SJean-Christophe PLAGNIOL-VILLARD 		   int length, struct devrequest *setup)
9952731b9a8SJean-Christophe PLAGNIOL-VILLARD {
996676ae068SLucas Stach 	struct ehci_ctrl *ctrl = dev->controller;
9972731b9a8SJean-Christophe PLAGNIOL-VILLARD 
9982731b9a8SJean-Christophe PLAGNIOL-VILLARD 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
9992731b9a8SJean-Christophe PLAGNIOL-VILLARD 		debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
10002731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return -1;
10012731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
10022731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1003676ae068SLucas Stach 	if (usb_pipedevice(pipe) == ctrl->rootdev) {
1004676ae068SLucas Stach 		if (!ctrl->rootdev)
10052731b9a8SJean-Christophe PLAGNIOL-VILLARD 			dev->speed = USB_SPEED_HIGH;
10062731b9a8SJean-Christophe PLAGNIOL-VILLARD 		return ehci_submit_root(dev, pipe, buffer, length, setup);
10072731b9a8SJean-Christophe PLAGNIOL-VILLARD 	}
10082731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return ehci_submit_async(dev, pipe, buffer, length, setup);
10092731b9a8SJean-Christophe PLAGNIOL-VILLARD }
10102731b9a8SJean-Christophe PLAGNIOL-VILLARD 
10118f62ca64SPatrick Georgi struct int_queue {
10128f62ca64SPatrick Georgi 	struct QH *first;
10138f62ca64SPatrick Georgi 	struct QH *current;
10148f62ca64SPatrick Georgi 	struct QH *last;
10158f62ca64SPatrick Georgi 	struct qTD *tds;
10168f62ca64SPatrick Georgi };
10178f62ca64SPatrick Georgi 
10188f62ca64SPatrick Georgi #define NEXT_QH(qh) (struct QH *)((qh)->qh_link & ~0x1f)
10198f62ca64SPatrick Georgi 
10208f62ca64SPatrick Georgi static int
10218f62ca64SPatrick Georgi enable_periodic(struct ehci_ctrl *ctrl)
10228f62ca64SPatrick Georgi {
10238f62ca64SPatrick Georgi 	uint32_t cmd;
10248f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
10258f62ca64SPatrick Georgi 	int ret;
10268f62ca64SPatrick Georgi 
10278f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
10288f62ca64SPatrick Georgi 	cmd |= CMD_PSE;
10298f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
10308f62ca64SPatrick Georgi 
10318f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
10328f62ca64SPatrick Georgi 			STS_PSS, STS_PSS, 100 * 1000);
10338f62ca64SPatrick Georgi 	if (ret < 0) {
10348f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when enabling periodic list\n");
10358f62ca64SPatrick Georgi 		return -ETIMEDOUT;
10368f62ca64SPatrick Georgi 	}
10378f62ca64SPatrick Georgi 	udelay(1000);
10388f62ca64SPatrick Georgi 	return 0;
10398f62ca64SPatrick Georgi }
10408f62ca64SPatrick Georgi 
10418f62ca64SPatrick Georgi static int
10428f62ca64SPatrick Georgi disable_periodic(struct ehci_ctrl *ctrl)
10438f62ca64SPatrick Georgi {
10448f62ca64SPatrick Georgi 	uint32_t cmd;
10458f62ca64SPatrick Georgi 	struct ehci_hcor *hcor = ctrl->hcor;
10468f62ca64SPatrick Georgi 	int ret;
10478f62ca64SPatrick Georgi 
10488f62ca64SPatrick Georgi 	cmd = ehci_readl(&hcor->or_usbcmd);
10498f62ca64SPatrick Georgi 	cmd &= ~CMD_PSE;
10508f62ca64SPatrick Georgi 	ehci_writel(&hcor->or_usbcmd, cmd);
10518f62ca64SPatrick Georgi 
10528f62ca64SPatrick Georgi 	ret = handshake((uint32_t *)&hcor->or_usbsts,
10538f62ca64SPatrick Georgi 			STS_PSS, 0, 100 * 1000);
10548f62ca64SPatrick Georgi 	if (ret < 0) {
10558f62ca64SPatrick Georgi 		printf("EHCI failed: timeout when disabling periodic list\n");
10568f62ca64SPatrick Georgi 		return -ETIMEDOUT;
10578f62ca64SPatrick Georgi 	}
10588f62ca64SPatrick Georgi 	return 0;
10598f62ca64SPatrick Georgi }
10608f62ca64SPatrick Georgi 
10618f62ca64SPatrick Georgi static int periodic_schedules;
10628f62ca64SPatrick Georgi 
10638f62ca64SPatrick Georgi struct int_queue *
10648f62ca64SPatrick Georgi create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
10658f62ca64SPatrick Georgi 		 int elementsize, void *buffer)
10668f62ca64SPatrick Georgi {
10678f62ca64SPatrick Georgi 	struct ehci_ctrl *ctrl = dev->controller;
10688f62ca64SPatrick Georgi 	struct int_queue *result = NULL;
10698f62ca64SPatrick Georgi 	int i;
10708f62ca64SPatrick Georgi 
10718f62ca64SPatrick Georgi 	debug("Enter create_int_queue\n");
10728f62ca64SPatrick Georgi 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
10738f62ca64SPatrick Georgi 		debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
10748f62ca64SPatrick Georgi 		return NULL;
10758f62ca64SPatrick Georgi 	}
10768f62ca64SPatrick Georgi 
10778f62ca64SPatrick Georgi 	/* limit to 4 full pages worth of data -
10788f62ca64SPatrick Georgi 	 * we can safely fit them in a single TD,
10798f62ca64SPatrick Georgi 	 * no matter the alignment
10808f62ca64SPatrick Georgi 	 */
10818f62ca64SPatrick Georgi 	if (elementsize >= 16384) {
10828f62ca64SPatrick Georgi 		debug("too large elements for interrupt transfers\n");
10838f62ca64SPatrick Georgi 		return NULL;
10848f62ca64SPatrick Georgi 	}
10858f62ca64SPatrick Georgi 
10868f62ca64SPatrick Georgi 	result = malloc(sizeof(*result));
10878f62ca64SPatrick Georgi 	if (!result) {
10888f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
10898f62ca64SPatrick Georgi 		goto fail1;
10908f62ca64SPatrick Georgi 	}
10918f62ca64SPatrick Georgi 	result->first = memalign(32, sizeof(struct QH) * queuesize);
10928f62ca64SPatrick Georgi 	if (!result->first) {
10938f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
10948f62ca64SPatrick Georgi 		goto fail2;
10958f62ca64SPatrick Georgi 	}
10968f62ca64SPatrick Georgi 	result->current = result->first;
10978f62ca64SPatrick Georgi 	result->last = result->first + queuesize - 1;
10988f62ca64SPatrick Georgi 	result->tds = memalign(32, sizeof(struct qTD) * queuesize);
10998f62ca64SPatrick Georgi 	if (!result->tds) {
11008f62ca64SPatrick Georgi 		debug("ehci intr queue: out of memory\n");
11018f62ca64SPatrick Georgi 		goto fail3;
11028f62ca64SPatrick Georgi 	}
11038f62ca64SPatrick Georgi 	memset(result->first, 0, sizeof(struct QH) * queuesize);
11048f62ca64SPatrick Georgi 	memset(result->tds, 0, sizeof(struct qTD) * queuesize);
11058f62ca64SPatrick Georgi 
11068f62ca64SPatrick Georgi 	for (i = 0; i < queuesize; i++) {
11078f62ca64SPatrick Georgi 		struct QH *qh = result->first + i;
11088f62ca64SPatrick Georgi 		struct qTD *td = result->tds + i;
11098f62ca64SPatrick Georgi 		void **buf = &qh->buffer;
11108f62ca64SPatrick Georgi 
11118f62ca64SPatrick Georgi 		qh->qh_link = (uint32_t)(qh+1) | QH_LINK_TYPE_QH;
11128f62ca64SPatrick Georgi 		if (i == queuesize - 1)
11138f62ca64SPatrick Georgi 			qh->qh_link = QH_LINK_TERMINATE;
11148f62ca64SPatrick Georgi 
11158f62ca64SPatrick Georgi 		qh->qh_overlay.qt_next = (uint32_t)td;
11168f62ca64SPatrick Georgi 		qh->qh_endpt1 = (0 << 28) | /* No NAK reload (ehci 4.9) */
11178f62ca64SPatrick Georgi 			(usb_maxpacket(dev, pipe) << 16) | /* MPS */
11188f62ca64SPatrick Georgi 			(1 << 14) |
11198f62ca64SPatrick Georgi 			QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
11208f62ca64SPatrick Georgi 			(usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
11218f62ca64SPatrick Georgi 			(usb_pipedevice(pipe) << 0);
11228f62ca64SPatrick Georgi 		qh->qh_endpt2 = (1 << 30) | /* 1 Tx per mframe */
11238f62ca64SPatrick Georgi 			(1 << 0); /* S-mask: microframe 0 */
11248f62ca64SPatrick Georgi 		if (dev->speed == USB_SPEED_LOW ||
11258f62ca64SPatrick Georgi 				dev->speed == USB_SPEED_FULL) {
11268f62ca64SPatrick Georgi 			debug("TT: port: %d, hub address: %d\n",
11278f62ca64SPatrick Georgi 				dev->portnr, dev->parent->devnum);
11288f62ca64SPatrick Georgi 			qh->qh_endpt2 |= (dev->portnr << 23) |
11298f62ca64SPatrick Georgi 				(dev->parent->devnum << 16) |
11308f62ca64SPatrick Georgi 				(0x1c << 8); /* C-mask: microframes 2-4 */
11318f62ca64SPatrick Georgi 		}
11328f62ca64SPatrick Georgi 
11338f62ca64SPatrick Georgi 		td->qt_next = QT_NEXT_TERMINATE;
11348f62ca64SPatrick Georgi 		td->qt_altnext = QT_NEXT_TERMINATE;
11358f62ca64SPatrick Georgi 		debug("communication direction is '%s'\n",
11368f62ca64SPatrick Georgi 		      usb_pipein(pipe) ? "in" : "out");
11378f62ca64SPatrick Georgi 		td->qt_token = (elementsize << 16) |
11388f62ca64SPatrick Georgi 			((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
11398f62ca64SPatrick Georgi 			0x80; /* active */
11408f62ca64SPatrick Georgi 		td->qt_buffer[0] = (uint32_t)buffer + i * elementsize;
11418f62ca64SPatrick Georgi 		td->qt_buffer[1] = (td->qt_buffer[0] + 0x1000) & ~0xfff;
11428f62ca64SPatrick Georgi 		td->qt_buffer[2] = (td->qt_buffer[0] + 0x2000) & ~0xfff;
11438f62ca64SPatrick Georgi 		td->qt_buffer[3] = (td->qt_buffer[0] + 0x3000) & ~0xfff;
11448f62ca64SPatrick Georgi 		td->qt_buffer[4] = (td->qt_buffer[0] + 0x4000) & ~0xfff;
11458f62ca64SPatrick Georgi 
11468f62ca64SPatrick Georgi 		*buf = buffer + i * elementsize;
11478f62ca64SPatrick Georgi 	}
11488f62ca64SPatrick Georgi 
11498f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
11508f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
11518f62ca64SPatrick Georgi 		goto fail3;
11528f62ca64SPatrick Georgi 	}
11538f62ca64SPatrick Georgi 
11548f62ca64SPatrick Georgi 	/* hook up to periodic list */
11558f62ca64SPatrick Georgi 	struct QH *list = &ctrl->periodic_queue;
11568f62ca64SPatrick Georgi 	result->last->qh_link = list->qh_link;
11578f62ca64SPatrick Georgi 	list->qh_link = (uint32_t)result->first | QH_LINK_TYPE_QH;
11588f62ca64SPatrick Georgi 
11598f62ca64SPatrick Georgi 	if (enable_periodic(ctrl) < 0) {
11608f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
11618f62ca64SPatrick Georgi 		goto fail3;
11628f62ca64SPatrick Georgi 	}
11638f62ca64SPatrick Georgi 	periodic_schedules++;
11648f62ca64SPatrick Georgi 
11658f62ca64SPatrick Georgi 	debug("Exit create_int_queue\n");
11668f62ca64SPatrick Georgi 	return result;
11678f62ca64SPatrick Georgi fail3:
11688f62ca64SPatrick Georgi 	if (result->tds)
11698f62ca64SPatrick Georgi 		free(result->tds);
11708f62ca64SPatrick Georgi fail2:
11718f62ca64SPatrick Georgi 	if (result->first)
11728f62ca64SPatrick Georgi 		free(result->first);
11738f62ca64SPatrick Georgi 	if (result)
11748f62ca64SPatrick Georgi 		free(result);
11758f62ca64SPatrick Georgi fail1:
11768f62ca64SPatrick Georgi 	return NULL;
11778f62ca64SPatrick Georgi }
11788f62ca64SPatrick Georgi 
11798f62ca64SPatrick Georgi void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
11808f62ca64SPatrick Georgi {
11818f62ca64SPatrick Georgi 	struct QH *cur = queue->current;
11828f62ca64SPatrick Georgi 
11838f62ca64SPatrick Georgi 	/* depleted queue */
11848f62ca64SPatrick Georgi 	if (cur == NULL) {
11858f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with completed queue\n");
11868f62ca64SPatrick Georgi 		return NULL;
11878f62ca64SPatrick Georgi 	}
11888f62ca64SPatrick Georgi 	/* still active */
11898f62ca64SPatrick Georgi 	if (cur->qh_overlay.qt_token & 0x80) {
11908f62ca64SPatrick Georgi 		debug("Exit poll_int_queue with no completed intr transfer. "
11918f62ca64SPatrick Georgi 		      "token is %x\n", cur->qh_overlay.qt_token);
11928f62ca64SPatrick Georgi 		return NULL;
11938f62ca64SPatrick Georgi 	}
11948f62ca64SPatrick Georgi 	if (!(cur->qh_link & QH_LINK_TERMINATE))
11958f62ca64SPatrick Georgi 		queue->current++;
11968f62ca64SPatrick Georgi 	else
11978f62ca64SPatrick Georgi 		queue->current = NULL;
11988f62ca64SPatrick Georgi 	debug("Exit poll_int_queue with completed intr transfer. "
11998f62ca64SPatrick Georgi 	      "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
12008f62ca64SPatrick Georgi 	      &cur->qh_overlay.qt_token, queue->first);
12018f62ca64SPatrick Georgi 	return cur->buffer;
12028f62ca64SPatrick Georgi }
12038f62ca64SPatrick Georgi 
12048f62ca64SPatrick Georgi /* Do not free buffers associated with QHs, they're owned by someone else */
12058f62ca64SPatrick Georgi int
12068f62ca64SPatrick Georgi destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
12078f62ca64SPatrick Georgi {
12088f62ca64SPatrick Georgi 	struct ehci_ctrl *ctrl = dev->controller;
12098f62ca64SPatrick Georgi 	int result = -1;
12108f62ca64SPatrick Georgi 	unsigned long timeout;
12118f62ca64SPatrick Georgi 
12128f62ca64SPatrick Georgi 	if (disable_periodic(ctrl) < 0) {
12138f62ca64SPatrick Georgi 		debug("FATAL: periodic should never fail, but did");
12148f62ca64SPatrick Georgi 		goto out;
12158f62ca64SPatrick Georgi 	}
12168f62ca64SPatrick Georgi 	periodic_schedules--;
12178f62ca64SPatrick Georgi 
12188f62ca64SPatrick Georgi 	struct QH *cur = &ctrl->periodic_queue;
12198f62ca64SPatrick Georgi 	timeout = get_timer(0) + 500; /* abort after 500ms */
12208f62ca64SPatrick Georgi 	while (!(cur->qh_link & QH_LINK_TERMINATE)) {
12218f62ca64SPatrick Georgi 		debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
12228f62ca64SPatrick Georgi 		if (NEXT_QH(cur) == queue->first) {
12238f62ca64SPatrick Georgi 			debug("found candidate. removing from chain\n");
12248f62ca64SPatrick Georgi 			cur->qh_link = queue->last->qh_link;
12258f62ca64SPatrick Georgi 			result = 0;
12268f62ca64SPatrick Georgi 			break;
12278f62ca64SPatrick Georgi 		}
12288f62ca64SPatrick Georgi 		cur = NEXT_QH(cur);
12298f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
12308f62ca64SPatrick Georgi 			printf("Timeout destroying interrupt endpoint queue\n");
12318f62ca64SPatrick Georgi 			result = -1;
12328f62ca64SPatrick Georgi 			goto out;
12338f62ca64SPatrick Georgi 		}
12348f62ca64SPatrick Georgi 	}
12358f62ca64SPatrick Georgi 
12368f62ca64SPatrick Georgi 	if (periodic_schedules > 0) {
12378f62ca64SPatrick Georgi 		result = enable_periodic(ctrl);
12388f62ca64SPatrick Georgi 		if (result < 0)
12398f62ca64SPatrick Georgi 			debug("FATAL: periodic should never fail, but did");
12408f62ca64SPatrick Georgi 	}
12418f62ca64SPatrick Georgi 
12428f62ca64SPatrick Georgi out:
12438f62ca64SPatrick Georgi 	free(queue->tds);
12448f62ca64SPatrick Georgi 	free(queue->first);
12458f62ca64SPatrick Georgi 	free(queue);
12468f62ca64SPatrick Georgi 
12478f62ca64SPatrick Georgi 	return result;
12488f62ca64SPatrick Georgi }
12498f62ca64SPatrick Georgi 
12502731b9a8SJean-Christophe PLAGNIOL-VILLARD int
12512731b9a8SJean-Christophe PLAGNIOL-VILLARD submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
12522731b9a8SJean-Christophe PLAGNIOL-VILLARD 	       int length, int interval)
12532731b9a8SJean-Christophe PLAGNIOL-VILLARD {
12548f62ca64SPatrick Georgi 	void *backbuffer;
12558f62ca64SPatrick Georgi 	struct int_queue *queue;
12568f62ca64SPatrick Georgi 	unsigned long timeout;
12578f62ca64SPatrick Georgi 	int result = 0, ret;
12588f62ca64SPatrick Georgi 
12592731b9a8SJean-Christophe PLAGNIOL-VILLARD 	debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
12602731b9a8SJean-Christophe PLAGNIOL-VILLARD 	      dev, pipe, buffer, length, interval);
126144ae0be7SBenoît Thébaudeau 
126244ae0be7SBenoît Thébaudeau 	/*
126344ae0be7SBenoît Thébaudeau 	 * Interrupt transfers requiring several transactions are not supported
126444ae0be7SBenoît Thébaudeau 	 * because bInterval is ignored.
12655cec214eSBenoît Thébaudeau 	 *
12665cec214eSBenoît Thébaudeau 	 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1267db191346SBenoît Thébaudeau 	 * <= PKT_ALIGN if several qTDs are required, while the USB
1268db191346SBenoît Thébaudeau 	 * specification does not constrain this for interrupt transfers. That
1269db191346SBenoît Thébaudeau 	 * means that ehci_submit_async() would support interrupt transfers
1270db191346SBenoît Thébaudeau 	 * requiring several transactions only as long as the transfer size does
1271db191346SBenoît Thébaudeau 	 * not require more than a single qTD.
127244ae0be7SBenoît Thébaudeau 	 */
127344ae0be7SBenoît Thébaudeau 	if (length > usb_maxpacket(dev, pipe)) {
12748f62ca64SPatrick Georgi 		printf("%s: Interrupt transfers requiring several "
12758f62ca64SPatrick Georgi 			"transactions are not supported.\n", __func__);
127644ae0be7SBenoît Thébaudeau 		return -1;
127744ae0be7SBenoît Thébaudeau 	}
12788f62ca64SPatrick Georgi 
12798f62ca64SPatrick Georgi 	queue = create_int_queue(dev, pipe, 1, length, buffer);
12808f62ca64SPatrick Georgi 
12818f62ca64SPatrick Georgi 	timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
12828f62ca64SPatrick Georgi 	while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
12838f62ca64SPatrick Georgi 		if (get_timer(0) > timeout) {
12848f62ca64SPatrick Georgi 			printf("Timeout poll on interrupt endpoint\n");
12858f62ca64SPatrick Georgi 			result = -ETIMEDOUT;
12868f62ca64SPatrick Georgi 			break;
12878f62ca64SPatrick Georgi 		}
12888f62ca64SPatrick Georgi 
12898f62ca64SPatrick Georgi 	if (backbuffer != buffer) {
12908f62ca64SPatrick Georgi 		debug("got wrong buffer back (%x instead of %x)\n",
12918f62ca64SPatrick Georgi 		      (uint32_t)backbuffer, (uint32_t)buffer);
12928f62ca64SPatrick Georgi 		return -EINVAL;
12938f62ca64SPatrick Georgi 	}
12948f62ca64SPatrick Georgi 
12958f62ca64SPatrick Georgi 	ret = destroy_int_queue(dev, queue);
12968f62ca64SPatrick Georgi 	if (ret < 0)
12978f62ca64SPatrick Georgi 		return ret;
12988f62ca64SPatrick Georgi 
12998f62ca64SPatrick Georgi 	/* everything worked out fine */
13008f62ca64SPatrick Georgi 	return result;
13012731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1302