xref: /openbmc/u-boot/drivers/usb/gadget/ci_udc.c (revision 67cf22cbdef8c62ffa28b4caf935825fe410c68d)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2f016f8caSMarek Vasut /*
3f016f8caSMarek Vasut  * Copyright 2011, Marvell Semiconductor Inc.
4f016f8caSMarek Vasut  * Lei Wen <leiwen@marvell.com>
5f016f8caSMarek Vasut  *
6f016f8caSMarek Vasut  * Back ported to the 8xx platform (from the 8260 platform) by
7f016f8caSMarek Vasut  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
8f016f8caSMarek Vasut  */
9f016f8caSMarek Vasut 
10f016f8caSMarek Vasut #include <common.h>
11f016f8caSMarek Vasut #include <command.h>
12f016f8caSMarek Vasut #include <config.h>
13f016f8caSMarek Vasut #include <net.h>
14f016f8caSMarek Vasut #include <malloc.h>
15f016f8caSMarek Vasut #include <asm/byteorder.h>
161221ce45SMasahiro Yamada #include <linux/errno.h>
17f016f8caSMarek Vasut #include <asm/io.h>
18f016f8caSMarek Vasut #include <asm/unaligned.h>
19f016f8caSMarek Vasut #include <linux/types.h>
20f016f8caSMarek Vasut #include <linux/usb/ch9.h>
21f016f8caSMarek Vasut #include <linux/usb/gadget.h>
22f016f8caSMarek Vasut #include <usb/ci_udc.h>
23f016f8caSMarek Vasut #include "../host/ehci.h"
24f016f8caSMarek Vasut #include "ci_udc.h"
25f016f8caSMarek Vasut 
26f016f8caSMarek Vasut /*
27f016f8caSMarek Vasut  * Check if the system has too long cachelines. If the cachelines are
28f016f8caSMarek Vasut  * longer then 128b, the driver will not be able flush/invalidate data
29f016f8caSMarek Vasut  * cache over separate QH entries. We use 128b because one QH entry is
30f016f8caSMarek Vasut  * 64b long and there are always two QH list entries for each endpoint.
31f016f8caSMarek Vasut  */
32f016f8caSMarek Vasut #if ARCH_DMA_MINALIGN > 128
33f016f8caSMarek Vasut #error This driver can not work on systems with caches longer than 128b
34f016f8caSMarek Vasut #endif
35f016f8caSMarek Vasut 
3606b38fcbSStephen Warren /*
377e541887SStephen Warren  * Every QTD must be individually aligned, since we can program any
387e541887SStephen Warren  * QTD's address into HW. Cache flushing requires ARCH_DMA_MINALIGN,
397e541887SStephen Warren  * and the USB HW requires 32-byte alignment. Align to both:
4006b38fcbSStephen Warren  */
4106b38fcbSStephen Warren #define ILIST_ALIGN		roundup(ARCH_DMA_MINALIGN, 32)
427e541887SStephen Warren /* Each QTD is this size */
437e541887SStephen Warren #define ILIST_ENT_RAW_SZ	sizeof(struct ept_queue_item)
447e541887SStephen Warren /*
457e541887SStephen Warren  * Align the size of the QTD too, so we can add this value to each
467e541887SStephen Warren  * QTD's address to get another aligned address.
477e541887SStephen Warren  */
487e541887SStephen Warren #define ILIST_ENT_SZ		roundup(ILIST_ENT_RAW_SZ, ILIST_ALIGN)
497e541887SStephen Warren /* For each endpoint, we need 2 QTDs, one for each of IN and OUT */
507e541887SStephen Warren #define ILIST_SZ		(NUM_ENDPOINTS * 2 * ILIST_ENT_SZ)
5106b38fcbSStephen Warren 
526a132416SSiva Durga Prasad Paladugu #define EP_MAX_LENGTH_TRANSFER	0x4000
536a132416SSiva Durga Prasad Paladugu 
54f016f8caSMarek Vasut #ifndef DEBUG
55f016f8caSMarek Vasut #define DBG(x...) do {} while (0)
56f016f8caSMarek Vasut #else
57f016f8caSMarek Vasut #define DBG(x...) printf(x)
reqname(unsigned r)58f016f8caSMarek Vasut static const char *reqname(unsigned r)
59f016f8caSMarek Vasut {
60f016f8caSMarek Vasut 	switch (r) {
61f016f8caSMarek Vasut 	case USB_REQ_GET_STATUS: return "GET_STATUS";
62f016f8caSMarek Vasut 	case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
63f016f8caSMarek Vasut 	case USB_REQ_SET_FEATURE: return "SET_FEATURE";
64f016f8caSMarek Vasut 	case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
65f016f8caSMarek Vasut 	case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
66f016f8caSMarek Vasut 	case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
67f016f8caSMarek Vasut 	case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
68f016f8caSMarek Vasut 	case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
69f016f8caSMarek Vasut 	case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
70f016f8caSMarek Vasut 	case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
71f016f8caSMarek Vasut 	default: return "*UNKNOWN*";
72f016f8caSMarek Vasut 	}
73f016f8caSMarek Vasut }
74f016f8caSMarek Vasut #endif
75f016f8caSMarek Vasut 
76054731b0SStephen Warren static struct usb_endpoint_descriptor ep0_desc = {
77f016f8caSMarek Vasut 	.bLength = sizeof(struct usb_endpoint_descriptor),
78f016f8caSMarek Vasut 	.bDescriptorType = USB_DT_ENDPOINT,
79f016f8caSMarek Vasut 	.bEndpointAddress = USB_DIR_IN,
80f016f8caSMarek Vasut 	.bmAttributes =	USB_ENDPOINT_XFER_CONTROL,
81f016f8caSMarek Vasut };
82f016f8caSMarek Vasut 
83f016f8caSMarek Vasut static int ci_pullup(struct usb_gadget *gadget, int is_on);
84f016f8caSMarek Vasut static int ci_ep_enable(struct usb_ep *ep,
85f016f8caSMarek Vasut 		const struct usb_endpoint_descriptor *desc);
86f016f8caSMarek Vasut static int ci_ep_disable(struct usb_ep *ep);
87f016f8caSMarek Vasut static int ci_ep_queue(struct usb_ep *ep,
88f016f8caSMarek Vasut 		struct usb_request *req, gfp_t gfp_flags);
8970eaeb03SPeng Fan static int ci_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
90f016f8caSMarek Vasut static struct usb_request *
91f016f8caSMarek Vasut ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
92f016f8caSMarek Vasut static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
93f016f8caSMarek Vasut 
94f016f8caSMarek Vasut static struct usb_gadget_ops ci_udc_ops = {
95f016f8caSMarek Vasut 	.pullup = ci_pullup,
96f016f8caSMarek Vasut };
97f016f8caSMarek Vasut 
98f016f8caSMarek Vasut static struct usb_ep_ops ci_ep_ops = {
99f016f8caSMarek Vasut 	.enable         = ci_ep_enable,
100f016f8caSMarek Vasut 	.disable        = ci_ep_disable,
101f016f8caSMarek Vasut 	.queue          = ci_ep_queue,
10270eaeb03SPeng Fan 	.dequeue	= ci_ep_dequeue,
103f016f8caSMarek Vasut 	.alloc_request  = ci_ep_alloc_request,
104f016f8caSMarek Vasut 	.free_request   = ci_ep_free_request,
105f016f8caSMarek Vasut };
106f016f8caSMarek Vasut 
ci_init_after_reset(struct ehci_ctrl * ctrl)10781649529SRamon Fried __weak void ci_init_after_reset(struct ehci_ctrl *ctrl)
10881649529SRamon Fried {
10981649529SRamon Fried }
11081649529SRamon Fried 
111f016f8caSMarek Vasut /* Init values for USB endpoints. */
1126a132416SSiva Durga Prasad Paladugu static const struct usb_ep ci_ep_init[5] = {
113f016f8caSMarek Vasut 	[0] = {	/* EP 0 */
114f016f8caSMarek Vasut 		.maxpacket	= 64,
115f016f8caSMarek Vasut 		.name		= "ep0",
116f016f8caSMarek Vasut 		.ops		= &ci_ep_ops,
117f016f8caSMarek Vasut 	},
1186a132416SSiva Durga Prasad Paladugu 	[1] = {
1196a132416SSiva Durga Prasad Paladugu 		.maxpacket	= 512,
1206a132416SSiva Durga Prasad Paladugu 		.name		= "ep1in-bulk",
1216a132416SSiva Durga Prasad Paladugu 		.ops		= &ci_ep_ops,
1226a132416SSiva Durga Prasad Paladugu 	},
1236a132416SSiva Durga Prasad Paladugu 	[2] = {
1246a132416SSiva Durga Prasad Paladugu 		.maxpacket	= 512,
1256a132416SSiva Durga Prasad Paladugu 		.name		= "ep2out-bulk",
1266a132416SSiva Durga Prasad Paladugu 		.ops		= &ci_ep_ops,
1276a132416SSiva Durga Prasad Paladugu 	},
1286a132416SSiva Durga Prasad Paladugu 	[3] = {
1296a132416SSiva Durga Prasad Paladugu 		.maxpacket	= 512,
1306a132416SSiva Durga Prasad Paladugu 		.name		= "ep3in-int",
1316a132416SSiva Durga Prasad Paladugu 		.ops		= &ci_ep_ops,
1326a132416SSiva Durga Prasad Paladugu 	},
1336a132416SSiva Durga Prasad Paladugu 	[4] = {
134f016f8caSMarek Vasut 		.maxpacket	= 512,
135f016f8caSMarek Vasut 		.name		= "ep-",
136f016f8caSMarek Vasut 		.ops		= &ci_ep_ops,
137f016f8caSMarek Vasut 	},
138f016f8caSMarek Vasut };
139f016f8caSMarek Vasut 
140f016f8caSMarek Vasut static struct ci_drv controller = {
141f016f8caSMarek Vasut 	.gadget	= {
142f016f8caSMarek Vasut 		.name	= "ci_udc",
143f016f8caSMarek Vasut 		.ops	= &ci_udc_ops,
144f016f8caSMarek Vasut 		.is_dualspeed = 1,
145f016f8caSMarek Vasut 	},
146f016f8caSMarek Vasut };
147f016f8caSMarek Vasut 
148f016f8caSMarek Vasut /**
149f016f8caSMarek Vasut  * ci_get_qh() - return queue head for endpoint
150f016f8caSMarek Vasut  * @ep_num:	Endpoint number
151f016f8caSMarek Vasut  * @dir_in:	Direction of the endpoint (IN = 1, OUT = 0)
152f016f8caSMarek Vasut  *
153f016f8caSMarek Vasut  * This function returns the QH associated with particular endpoint
154f016f8caSMarek Vasut  * and it's direction.
155f016f8caSMarek Vasut  */
ci_get_qh(int ep_num,int dir_in)156f016f8caSMarek Vasut static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
157f016f8caSMarek Vasut {
158f016f8caSMarek Vasut 	return &controller.epts[(ep_num * 2) + dir_in];
159f016f8caSMarek Vasut }
160f016f8caSMarek Vasut 
161f016f8caSMarek Vasut /**
162f016f8caSMarek Vasut  * ci_get_qtd() - return queue item for endpoint
163f016f8caSMarek Vasut  * @ep_num:	Endpoint number
164f016f8caSMarek Vasut  * @dir_in:	Direction of the endpoint (IN = 1, OUT = 0)
165f016f8caSMarek Vasut  *
166f016f8caSMarek Vasut  * This function returns the QH associated with particular endpoint
167f016f8caSMarek Vasut  * and it's direction.
168f016f8caSMarek Vasut  */
ci_get_qtd(int ep_num,int dir_in)169f016f8caSMarek Vasut static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
170f016f8caSMarek Vasut {
1716ac15fdaSStephen Warren 	int index = (ep_num * 2) + dir_in;
1726ac15fdaSStephen Warren 	uint8_t *imem = controller.items_mem + (index * ILIST_ENT_SZ);
1736ac15fdaSStephen Warren 	return (struct ept_queue_item *)imem;
174f016f8caSMarek Vasut }
175f016f8caSMarek Vasut 
176f016f8caSMarek Vasut /**
177f016f8caSMarek Vasut  * ci_flush_qh - flush cache over queue head
178f016f8caSMarek Vasut  * @ep_num:	Endpoint number
179f016f8caSMarek Vasut  *
180f016f8caSMarek Vasut  * This function flushes cache over QH for particular endpoint.
181f016f8caSMarek Vasut  */
ci_flush_qh(int ep_num)182f016f8caSMarek Vasut static void ci_flush_qh(int ep_num)
183f016f8caSMarek Vasut {
184f016f8caSMarek Vasut 	struct ept_queue_head *head = ci_get_qh(ep_num, 0);
185f72d8320SRob Herring 	const unsigned long start = (unsigned long)head;
186f72d8320SRob Herring 	const unsigned long end = start + 2 * sizeof(*head);
187f016f8caSMarek Vasut 
188f016f8caSMarek Vasut 	flush_dcache_range(start, end);
189f016f8caSMarek Vasut }
190f016f8caSMarek Vasut 
191f016f8caSMarek Vasut /**
192f016f8caSMarek Vasut  * ci_invalidate_qh - invalidate cache over queue head
193f016f8caSMarek Vasut  * @ep_num:	Endpoint number
194f016f8caSMarek Vasut  *
195f016f8caSMarek Vasut  * This function invalidates cache over QH for particular endpoint.
196f016f8caSMarek Vasut  */
ci_invalidate_qh(int ep_num)197f016f8caSMarek Vasut static void ci_invalidate_qh(int ep_num)
198f016f8caSMarek Vasut {
199f016f8caSMarek Vasut 	struct ept_queue_head *head = ci_get_qh(ep_num, 0);
200f72d8320SRob Herring 	unsigned long start = (unsigned long)head;
201f72d8320SRob Herring 	unsigned long end = start + 2 * sizeof(*head);
202f016f8caSMarek Vasut 
203f016f8caSMarek Vasut 	invalidate_dcache_range(start, end);
204f016f8caSMarek Vasut }
205f016f8caSMarek Vasut 
206f016f8caSMarek Vasut /**
207f016f8caSMarek Vasut  * ci_flush_qtd - flush cache over queue item
208f016f8caSMarek Vasut  * @ep_num:	Endpoint number
209f016f8caSMarek Vasut  *
210f016f8caSMarek Vasut  * This function flushes cache over qTD pair for particular endpoint.
211f016f8caSMarek Vasut  */
ci_flush_qtd(int ep_num)212f016f8caSMarek Vasut static void ci_flush_qtd(int ep_num)
213f016f8caSMarek Vasut {
214f016f8caSMarek Vasut 	struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
215f72d8320SRob Herring 	const unsigned long start = (unsigned long)item;
216f72d8320SRob Herring 	const unsigned long end = start + 2 * ILIST_ENT_SZ;
217f016f8caSMarek Vasut 
218f016f8caSMarek Vasut 	flush_dcache_range(start, end);
219f016f8caSMarek Vasut }
220f016f8caSMarek Vasut 
221f016f8caSMarek Vasut /**
2226a132416SSiva Durga Prasad Paladugu  * ci_flush_td - flush cache over queue item
2236a132416SSiva Durga Prasad Paladugu  * @td:	td pointer
2246a132416SSiva Durga Prasad Paladugu  *
2256a132416SSiva Durga Prasad Paladugu  * This function flushes cache for particular transfer descriptor.
2266a132416SSiva Durga Prasad Paladugu  */
ci_flush_td(struct ept_queue_item * td)2276a132416SSiva Durga Prasad Paladugu static void ci_flush_td(struct ept_queue_item *td)
2286a132416SSiva Durga Prasad Paladugu {
22985a9ea31SStephen Warren 	const unsigned long start = (unsigned long)td;
23085a9ea31SStephen Warren 	const unsigned long end = (unsigned long)td + ILIST_ENT_SZ;
2316a132416SSiva Durga Prasad Paladugu 	flush_dcache_range(start, end);
2326a132416SSiva Durga Prasad Paladugu }
2336a132416SSiva Durga Prasad Paladugu 
2346a132416SSiva Durga Prasad Paladugu /**
235f016f8caSMarek Vasut  * ci_invalidate_qtd - invalidate cache over queue item
236f016f8caSMarek Vasut  * @ep_num:	Endpoint number
237f016f8caSMarek Vasut  *
238f016f8caSMarek Vasut  * This function invalidates cache over qTD pair for particular endpoint.
239f016f8caSMarek Vasut  */
ci_invalidate_qtd(int ep_num)240f016f8caSMarek Vasut static void ci_invalidate_qtd(int ep_num)
241f016f8caSMarek Vasut {
242f016f8caSMarek Vasut 	struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
243f72d8320SRob Herring 	const unsigned long start = (unsigned long)item;
244f72d8320SRob Herring 	const unsigned long end = start + 2 * ILIST_ENT_SZ;
245f016f8caSMarek Vasut 
246f016f8caSMarek Vasut 	invalidate_dcache_range(start, end);
247f016f8caSMarek Vasut }
248f016f8caSMarek Vasut 
2496a132416SSiva Durga Prasad Paladugu /**
2506a132416SSiva Durga Prasad Paladugu  * ci_invalidate_td - invalidate cache over queue item
2516a132416SSiva Durga Prasad Paladugu  * @td:	td pointer
2526a132416SSiva Durga Prasad Paladugu  *
2536a132416SSiva Durga Prasad Paladugu  * This function invalidates cache for particular transfer descriptor.
2546a132416SSiva Durga Prasad Paladugu  */
ci_invalidate_td(struct ept_queue_item * td)2556a132416SSiva Durga Prasad Paladugu static void ci_invalidate_td(struct ept_queue_item *td)
2566a132416SSiva Durga Prasad Paladugu {
25785a9ea31SStephen Warren 	const unsigned long start = (unsigned long)td;
25885a9ea31SStephen Warren 	const unsigned long end = start + ILIST_ENT_SZ;
2596a132416SSiva Durga Prasad Paladugu 	invalidate_dcache_range(start, end);
2606a132416SSiva Durga Prasad Paladugu }
2616a132416SSiva Durga Prasad Paladugu 
262f016f8caSMarek Vasut static struct usb_request *
ci_ep_alloc_request(struct usb_ep * ep,unsigned int gfp_flags)263f016f8caSMarek Vasut ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
264f016f8caSMarek Vasut {
265a2d8f929SStephen Warren 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
26658d6d139SRob Herring 	int num = -1;
2672813006fSStephen Warren 	struct ci_req *ci_req;
2682813006fSStephen Warren 
26958d6d139SRob Herring 	if (ci_ep->desc)
270a2d8f929SStephen Warren 		num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
27158d6d139SRob Herring 
272a2d8f929SStephen Warren 	if (num == 0 && controller.ep0_req)
273a2d8f929SStephen Warren 		return &controller.ep0_req->req;
274a2d8f929SStephen Warren 
275639e9903SStephen Warren 	ci_req = calloc(1, sizeof(*ci_req));
2762813006fSStephen Warren 	if (!ci_req)
2772813006fSStephen Warren 		return NULL;
2782813006fSStephen Warren 
2792813006fSStephen Warren 	INIT_LIST_HEAD(&ci_req->queue);
2802813006fSStephen Warren 
281a2d8f929SStephen Warren 	if (num == 0)
282a2d8f929SStephen Warren 		controller.ep0_req = ci_req;
283a2d8f929SStephen Warren 
2842813006fSStephen Warren 	return &ci_req->req;
285f016f8caSMarek Vasut }
286f016f8caSMarek Vasut 
ci_ep_free_request(struct usb_ep * ep,struct usb_request * req)2872813006fSStephen Warren static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
288f016f8caSMarek Vasut {
289bdf81611SStephen Warren 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
290bdf81611SStephen Warren 	struct ci_req *ci_req = container_of(req, struct ci_req, req);
29158d6d139SRob Herring 	int num = -1;
2922813006fSStephen Warren 
29358d6d139SRob Herring 	if (ci_ep->desc)
294bdf81611SStephen Warren 		num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
29558d6d139SRob Herring 
296fb22ae6cSStephen Warren 	if (num == 0) {
297fb22ae6cSStephen Warren 		if (!controller.ep0_req)
298fb22ae6cSStephen Warren 			return;
299bdf81611SStephen Warren 		controller.ep0_req = 0;
300fb22ae6cSStephen Warren 	}
301bdf81611SStephen Warren 
3022813006fSStephen Warren 	if (ci_req->b_buf)
3032813006fSStephen Warren 		free(ci_req->b_buf);
3042813006fSStephen Warren 	free(ci_req);
305f016f8caSMarek Vasut }
306f016f8caSMarek Vasut 
ep_enable(int num,int in,int maxpacket)307f016f8caSMarek Vasut static void ep_enable(int num, int in, int maxpacket)
308f016f8caSMarek Vasut {
309f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
310f016f8caSMarek Vasut 	unsigned n;
311f016f8caSMarek Vasut 
312f016f8caSMarek Vasut 	n = readl(&udc->epctrl[num]);
313f016f8caSMarek Vasut 	if (in)
314f016f8caSMarek Vasut 		n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
315f016f8caSMarek Vasut 	else
316f016f8caSMarek Vasut 		n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
317f016f8caSMarek Vasut 
318f016f8caSMarek Vasut 	if (num != 0) {
319f016f8caSMarek Vasut 		struct ept_queue_head *head = ci_get_qh(num, in);
320f016f8caSMarek Vasut 
321f016f8caSMarek Vasut 		head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
322f016f8caSMarek Vasut 		ci_flush_qh(num);
323f016f8caSMarek Vasut 	}
324f016f8caSMarek Vasut 	writel(n, &udc->epctrl[num]);
325f016f8caSMarek Vasut }
326f016f8caSMarek Vasut 
ci_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)327f016f8caSMarek Vasut static int ci_ep_enable(struct usb_ep *ep,
328f016f8caSMarek Vasut 		const struct usb_endpoint_descriptor *desc)
329f016f8caSMarek Vasut {
330f016f8caSMarek Vasut 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
331f016f8caSMarek Vasut 	int num, in;
332f016f8caSMarek Vasut 	num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
333f016f8caSMarek Vasut 	in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
334f016f8caSMarek Vasut 	ci_ep->desc = desc;
335f016f8caSMarek Vasut 
336f016f8caSMarek Vasut 	if (num) {
337f016f8caSMarek Vasut 		int max = get_unaligned_le16(&desc->wMaxPacketSize);
338f016f8caSMarek Vasut 
339f016f8caSMarek Vasut 		if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
340f016f8caSMarek Vasut 			max = 64;
341f016f8caSMarek Vasut 		if (ep->maxpacket != max) {
342f016f8caSMarek Vasut 			DBG("%s: from %d to %d\n", __func__,
343f016f8caSMarek Vasut 			    ep->maxpacket, max);
344f016f8caSMarek Vasut 			ep->maxpacket = max;
345f016f8caSMarek Vasut 		}
346f016f8caSMarek Vasut 	}
347f016f8caSMarek Vasut 	ep_enable(num, in, ep->maxpacket);
348f016f8caSMarek Vasut 	DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
349f016f8caSMarek Vasut 	return 0;
350f016f8caSMarek Vasut }
351f016f8caSMarek Vasut 
ci_ep_disable(struct usb_ep * ep)352f016f8caSMarek Vasut static int ci_ep_disable(struct usb_ep *ep)
353f016f8caSMarek Vasut {
354f016f8caSMarek Vasut 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
355f016f8caSMarek Vasut 
356f016f8caSMarek Vasut 	ci_ep->desc = NULL;
357f016f8caSMarek Vasut 	return 0;
358f016f8caSMarek Vasut }
359f016f8caSMarek Vasut 
ci_bounce(struct ci_req * ci_req,int in)3602813006fSStephen Warren static int ci_bounce(struct ci_req *ci_req, int in)
361f016f8caSMarek Vasut {
3622813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
363f72d8320SRob Herring 	unsigned long addr = (unsigned long)req->buf;
364f72d8320SRob Herring 	unsigned long hwaddr;
3652813006fSStephen Warren 	uint32_t aligned_used_len;
366f016f8caSMarek Vasut 
367f016f8caSMarek Vasut 	/* Input buffer address is not aligned. */
368f016f8caSMarek Vasut 	if (addr & (ARCH_DMA_MINALIGN - 1))
369f016f8caSMarek Vasut 		goto align;
370f016f8caSMarek Vasut 
371f016f8caSMarek Vasut 	/* Input buffer length is not aligned. */
3722813006fSStephen Warren 	if (req->length & (ARCH_DMA_MINALIGN - 1))
373f016f8caSMarek Vasut 		goto align;
374f016f8caSMarek Vasut 
375f016f8caSMarek Vasut 	/* The buffer is well aligned, only flush cache. */
3762813006fSStephen Warren 	ci_req->hw_len = req->length;
3772813006fSStephen Warren 	ci_req->hw_buf = req->buf;
378f016f8caSMarek Vasut 	goto flush;
379f016f8caSMarek Vasut 
380f016f8caSMarek Vasut align:
3812813006fSStephen Warren 	if (ci_req->b_buf && req->length > ci_req->b_len) {
3822813006fSStephen Warren 		free(ci_req->b_buf);
3832813006fSStephen Warren 		ci_req->b_buf = 0;
3842813006fSStephen Warren 	}
3852813006fSStephen Warren 	if (!ci_req->b_buf) {
3862813006fSStephen Warren 		ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
3872813006fSStephen Warren 		ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
3882813006fSStephen Warren 		if (!ci_req->b_buf)
389f016f8caSMarek Vasut 			return -ENOMEM;
390f016f8caSMarek Vasut 	}
3912813006fSStephen Warren 	ci_req->hw_len = ci_req->b_len;
3922813006fSStephen Warren 	ci_req->hw_buf = ci_req->b_buf;
3932813006fSStephen Warren 
394f016f8caSMarek Vasut 	if (in)
3952813006fSStephen Warren 		memcpy(ci_req->hw_buf, req->buf, req->length);
396f016f8caSMarek Vasut 
397f016f8caSMarek Vasut flush:
398f72d8320SRob Herring 	hwaddr = (unsigned long)ci_req->hw_buf;
3992813006fSStephen Warren 	aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
4002813006fSStephen Warren 	flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
401f016f8caSMarek Vasut 
402f016f8caSMarek Vasut 	return 0;
403f016f8caSMarek Vasut }
404f016f8caSMarek Vasut 
ci_debounce(struct ci_req * ci_req,int in)4052813006fSStephen Warren static void ci_debounce(struct ci_req *ci_req, int in)
406f016f8caSMarek Vasut {
4072813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
408f72d8320SRob Herring 	unsigned long addr = (unsigned long)req->buf;
409f72d8320SRob Herring 	unsigned long hwaddr = (unsigned long)ci_req->hw_buf;
4102813006fSStephen Warren 	uint32_t aligned_used_len;
411f016f8caSMarek Vasut 
4122813006fSStephen Warren 	if (in)
4132813006fSStephen Warren 		return;
414f016f8caSMarek Vasut 
4152813006fSStephen Warren 	aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
4162813006fSStephen Warren 	invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
4172813006fSStephen Warren 
4182813006fSStephen Warren 	if (addr == hwaddr)
419f016f8caSMarek Vasut 		return; /* not a bounce */
420f016f8caSMarek Vasut 
4212813006fSStephen Warren 	memcpy(req->buf, ci_req->hw_buf, req->actual);
422f016f8caSMarek Vasut }
423f016f8caSMarek Vasut 
ci_ep_submit_next_request(struct ci_ep * ci_ep)4242813006fSStephen Warren static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
425f016f8caSMarek Vasut {
426f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
427f016f8caSMarek Vasut 	struct ept_queue_item *item;
428f016f8caSMarek Vasut 	struct ept_queue_head *head;
4292813006fSStephen Warren 	int bit, num, len, in;
4302813006fSStephen Warren 	struct ci_req *ci_req;
4316a132416SSiva Durga Prasad Paladugu 	u8 *buf;
432b337b3b2SStephen Warren 	uint32_t len_left, len_this_dtd;
4336a132416SSiva Durga Prasad Paladugu 	struct ept_queue_item *dtd, *qtd;
4342813006fSStephen Warren 
4352813006fSStephen Warren 	ci_ep->req_primed = true;
4362813006fSStephen Warren 
437f016f8caSMarek Vasut 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
438f016f8caSMarek Vasut 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
439f016f8caSMarek Vasut 	item = ci_get_qtd(num, in);
440f016f8caSMarek Vasut 	head = ci_get_qh(num, in);
441f016f8caSMarek Vasut 
4422813006fSStephen Warren 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
4432813006fSStephen Warren 	len = ci_req->req.length;
444f016f8caSMarek Vasut 
445f72d8320SRob Herring 	head->next = (unsigned long)item;
446f016f8caSMarek Vasut 	head->info = 0;
447f016f8caSMarek Vasut 
4486a132416SSiva Durga Prasad Paladugu 	ci_req->dtd_count = 0;
4496a132416SSiva Durga Prasad Paladugu 	buf = ci_req->hw_buf;
450b337b3b2SStephen Warren 	len_left = len;
4516a132416SSiva Durga Prasad Paladugu 	dtd = item;
4526a132416SSiva Durga Prasad Paladugu 
4536a132416SSiva Durga Prasad Paladugu 	do {
454b337b3b2SStephen Warren 		len_this_dtd = min(len_left, (unsigned)EP_MAX_LENGTH_TRANSFER);
4556a132416SSiva Durga Prasad Paladugu 
456b337b3b2SStephen Warren 		dtd->info = INFO_BYTES(len_this_dtd) | INFO_ACTIVE;
4576a132416SSiva Durga Prasad Paladugu 		dtd->page0 = (unsigned long)buf;
4586a132416SSiva Durga Prasad Paladugu 		dtd->page1 = ((unsigned long)buf & 0xfffff000) + 0x1000;
4596a132416SSiva Durga Prasad Paladugu 		dtd->page2 = ((unsigned long)buf & 0xfffff000) + 0x2000;
4606a132416SSiva Durga Prasad Paladugu 		dtd->page3 = ((unsigned long)buf & 0xfffff000) + 0x3000;
4616a132416SSiva Durga Prasad Paladugu 		dtd->page4 = ((unsigned long)buf & 0xfffff000) + 0x4000;
4626a132416SSiva Durga Prasad Paladugu 
463b337b3b2SStephen Warren 		len_left -= len_this_dtd;
464b337b3b2SStephen Warren 		buf += len_this_dtd;
4656a132416SSiva Durga Prasad Paladugu 
466b337b3b2SStephen Warren 		if (len_left) {
4676a132416SSiva Durga Prasad Paladugu 			qtd = (struct ept_queue_item *)
4686a132416SSiva Durga Prasad Paladugu 			       memalign(ILIST_ALIGN, ILIST_ENT_SZ);
46985a9ea31SStephen Warren 			dtd->next = (unsigned long)qtd;
4706a132416SSiva Durga Prasad Paladugu 			dtd = qtd;
4716a132416SSiva Durga Prasad Paladugu 			memset(dtd, 0, ILIST_ENT_SZ);
4726a132416SSiva Durga Prasad Paladugu 		}
4736a132416SSiva Durga Prasad Paladugu 
4746a132416SSiva Durga Prasad Paladugu 		ci_req->dtd_count++;
475b337b3b2SStephen Warren 	} while (len_left);
4766a132416SSiva Durga Prasad Paladugu 
4776a132416SSiva Durga Prasad Paladugu 	item = dtd;
478e0672b3cSStephen Warren 	/*
479e0672b3cSStephen Warren 	 * When sending the data for an IN transaction, the attached host
480e0672b3cSStephen Warren 	 * knows that all data for the IN is sent when one of the following
481e0672b3cSStephen Warren 	 * occurs:
482e0672b3cSStephen Warren 	 * a) A zero-length packet is transmitted.
483e0672b3cSStephen Warren 	 * b) A packet with length that isn't an exact multiple of the ep's
484e0672b3cSStephen Warren 	 *    maxpacket is transmitted.
485e0672b3cSStephen Warren 	 * c) Enough data is sent to exactly fill the host's maximum expected
486e0672b3cSStephen Warren 	 *    IN transaction size.
487e0672b3cSStephen Warren 	 *
488e0672b3cSStephen Warren 	 * One of these conditions MUST apply at the end of an IN transaction,
489e0672b3cSStephen Warren 	 * or the transaction will not be considered complete by the host. If
490e0672b3cSStephen Warren 	 * none of (a)..(c) already applies, then we must force (a) to apply
491e0672b3cSStephen Warren 	 * by explicitly sending an extra zero-length packet.
492e0672b3cSStephen Warren 	 */
493e0672b3cSStephen Warren 	/*  IN    !a     !b                              !c */
494e0672b3cSStephen Warren 	if (in && len && !(len % ci_ep->ep.maxpacket) && ci_req->req.zero) {
495e0672b3cSStephen Warren 		/*
496e0672b3cSStephen Warren 		 * Each endpoint has 2 items allocated, even though typically
497e0672b3cSStephen Warren 		 * only 1 is used at a time since either an IN or an OUT but
498e0672b3cSStephen Warren 		 * not both is queued. For an IN transaction, item currently
499e0672b3cSStephen Warren 		 * points at the second of these items, so we know that we
5008d7c39d3SStephen Warren 		 * can use the other to transmit the extra zero-length packet.
501e0672b3cSStephen Warren 		 */
5028d7c39d3SStephen Warren 		struct ept_queue_item *other_item = ci_get_qtd(num, 0);
503f72d8320SRob Herring 		item->next = (unsigned long)other_item;
5048d7c39d3SStephen Warren 		item = other_item;
505e0672b3cSStephen Warren 		item->info = INFO_ACTIVE;
506e0672b3cSStephen Warren 	}
507e0672b3cSStephen Warren 
508e0672b3cSStephen Warren 	item->next = TERMINATE;
509e0672b3cSStephen Warren 	item->info |= INFO_IOC;
510e0672b3cSStephen Warren 
511e0672b3cSStephen Warren 	ci_flush_qtd(num);
512e0672b3cSStephen Warren 
51385a9ea31SStephen Warren 	item = (struct ept_queue_item *)(unsigned long)head->next;
5146a132416SSiva Durga Prasad Paladugu 	while (item->next != TERMINATE) {
51585a9ea31SStephen Warren 		ci_flush_td((struct ept_queue_item *)(unsigned long)item->next);
51685a9ea31SStephen Warren 		item = (struct ept_queue_item *)(unsigned long)item->next;
5176a132416SSiva Durga Prasad Paladugu 	}
5186a132416SSiva Durga Prasad Paladugu 
5192813006fSStephen Warren 	DBG("ept%d %s queue len %x, req %p, buffer %p\n",
5202813006fSStephen Warren 	    num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
521f016f8caSMarek Vasut 	ci_flush_qh(num);
522f016f8caSMarek Vasut 
523f016f8caSMarek Vasut 	if (in)
524f016f8caSMarek Vasut 		bit = EPT_TX(num);
525f016f8caSMarek Vasut 	else
526f016f8caSMarek Vasut 		bit = EPT_RX(num);
527f016f8caSMarek Vasut 
528f016f8caSMarek Vasut 	writel(bit, &udc->epprime);
5292813006fSStephen Warren }
5302813006fSStephen Warren 
ci_ep_dequeue(struct usb_ep * _ep,struct usb_request * _req)53170eaeb03SPeng Fan static int ci_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
53270eaeb03SPeng Fan {
53370eaeb03SPeng Fan 	struct ci_ep *ci_ep = container_of(_ep, struct ci_ep, ep);
53470eaeb03SPeng Fan 	struct ci_req *ci_req;
53570eaeb03SPeng Fan 
53670eaeb03SPeng Fan 	list_for_each_entry(ci_req, &ci_ep->queue, queue) {
53770eaeb03SPeng Fan 		if (&ci_req->req == _req)
53870eaeb03SPeng Fan 			break;
53970eaeb03SPeng Fan 	}
54070eaeb03SPeng Fan 
54170eaeb03SPeng Fan 	if (&ci_req->req != _req)
54270eaeb03SPeng Fan 		return -EINVAL;
54370eaeb03SPeng Fan 
54470eaeb03SPeng Fan 	list_del_init(&ci_req->queue);
54570eaeb03SPeng Fan 
54670eaeb03SPeng Fan 	if (ci_req->req.status == -EINPROGRESS) {
54770eaeb03SPeng Fan 		ci_req->req.status = -ECONNRESET;
54870eaeb03SPeng Fan 		if (ci_req->req.complete)
54970eaeb03SPeng Fan 			ci_req->req.complete(_ep, _req);
55070eaeb03SPeng Fan 	}
55170eaeb03SPeng Fan 
55270eaeb03SPeng Fan 	return 0;
55370eaeb03SPeng Fan }
55470eaeb03SPeng Fan 
ci_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)5552813006fSStephen Warren static int ci_ep_queue(struct usb_ep *ep,
5562813006fSStephen Warren 		struct usb_request *req, gfp_t gfp_flags)
5572813006fSStephen Warren {
5582813006fSStephen Warren 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
5592813006fSStephen Warren 	struct ci_req *ci_req = container_of(req, struct ci_req, req);
5602813006fSStephen Warren 	int in, ret;
5612813006fSStephen Warren 	int __maybe_unused num;
5622813006fSStephen Warren 
5632813006fSStephen Warren 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
5642813006fSStephen Warren 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
5652813006fSStephen Warren 
5667484d84cSStephen Warren 	if (!num && ci_ep->req_primed) {
5677484d84cSStephen Warren 		/*
5687484d84cSStephen Warren 		 * The flipping of ep0 between IN and OUT relies on
5697484d84cSStephen Warren 		 * ci_ep_queue consuming the current IN/OUT setting
5707484d84cSStephen Warren 		 * immediately. If this is deferred to a later point when the
5717484d84cSStephen Warren 		 * req is pulled out of ci_req->queue, then the IN/OUT setting
5727484d84cSStephen Warren 		 * may have been changed since the req was queued, and state
5737484d84cSStephen Warren 		 * will get out of sync. This condition doesn't occur today,
5747484d84cSStephen Warren 		 * but could if bugs were introduced later, and this error
5757484d84cSStephen Warren 		 * check will save a lot of debugging time.
5767484d84cSStephen Warren 		 */
5777484d84cSStephen Warren 		printf("%s: ep0 transaction already in progress\n", __func__);
5787484d84cSStephen Warren 		return -EPROTO;
5797484d84cSStephen Warren 	}
5807484d84cSStephen Warren 
5812813006fSStephen Warren 	ret = ci_bounce(ci_req, in);
5822813006fSStephen Warren 	if (ret)
5832813006fSStephen Warren 		return ret;
5842813006fSStephen Warren 
5852813006fSStephen Warren 	DBG("ept%d %s pre-queue req %p, buffer %p\n",
5862813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, ci_req->hw_buf);
5872813006fSStephen Warren 	list_add_tail(&ci_req->queue, &ci_ep->queue);
5882813006fSStephen Warren 
5892813006fSStephen Warren 	if (!ci_ep->req_primed)
5902813006fSStephen Warren 		ci_ep_submit_next_request(ci_ep);
591f016f8caSMarek Vasut 
592f016f8caSMarek Vasut 	return 0;
593f016f8caSMarek Vasut }
594f016f8caSMarek Vasut 
flip_ep0_direction(void)595006c7026SStephen Warren static void flip_ep0_direction(void)
596006c7026SStephen Warren {
597006c7026SStephen Warren 	if (ep0_desc.bEndpointAddress == USB_DIR_IN) {
59883c37500SStephen Warren 		DBG("%s: Flipping ep0 to OUT\n", __func__);
599006c7026SStephen Warren 		ep0_desc.bEndpointAddress = 0;
600006c7026SStephen Warren 	} else {
60183c37500SStephen Warren 		DBG("%s: Flipping ep0 to IN\n", __func__);
602006c7026SStephen Warren 		ep0_desc.bEndpointAddress = USB_DIR_IN;
603006c7026SStephen Warren 	}
604006c7026SStephen Warren }
605006c7026SStephen Warren 
handle_ep_complete(struct ci_ep * ci_ep)606dcb89b5aSStephen Warren static void handle_ep_complete(struct ci_ep *ci_ep)
607f016f8caSMarek Vasut {
6086a132416SSiva Durga Prasad Paladugu 	struct ept_queue_item *item, *next_td;
6096a132416SSiva Durga Prasad Paladugu 	int num, in, len, j;
6102813006fSStephen Warren 	struct ci_req *ci_req;
6112813006fSStephen Warren 
612dcb89b5aSStephen Warren 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
613dcb89b5aSStephen Warren 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
614f016f8caSMarek Vasut 	item = ci_get_qtd(num, in);
615f016f8caSMarek Vasut 	ci_invalidate_qtd(num);
6166a132416SSiva Durga Prasad Paladugu 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
617f016f8caSMarek Vasut 
6186a132416SSiva Durga Prasad Paladugu 	next_td = item;
6196a132416SSiva Durga Prasad Paladugu 	len = 0;
6206a132416SSiva Durga Prasad Paladugu 	for (j = 0; j < ci_req->dtd_count; j++) {
6216a132416SSiva Durga Prasad Paladugu 		ci_invalidate_td(next_td);
6226a132416SSiva Durga Prasad Paladugu 		item = next_td;
6236a132416SSiva Durga Prasad Paladugu 		len += (item->info >> 16) & 0x7fff;
624f016f8caSMarek Vasut 		if (item->info & 0xff)
625f016f8caSMarek Vasut 			printf("EP%d/%s FAIL info=%x pg0=%x\n",
626f016f8caSMarek Vasut 			       num, in ? "in" : "out", item->info, item->page0);
6276a132416SSiva Durga Prasad Paladugu 		if (j != ci_req->dtd_count - 1)
62885a9ea31SStephen Warren 			next_td = (struct ept_queue_item *)(unsigned long)
62985a9ea31SStephen Warren 				item->next;
6306a132416SSiva Durga Prasad Paladugu 		if (j != 0)
6316a132416SSiva Durga Prasad Paladugu 			free(item);
6326a132416SSiva Durga Prasad Paladugu 	}
633f016f8caSMarek Vasut 
6342813006fSStephen Warren 	list_del_init(&ci_req->queue);
635dcb89b5aSStephen Warren 	ci_ep->req_primed = false;
636f016f8caSMarek Vasut 
637dcb89b5aSStephen Warren 	if (!list_empty(&ci_ep->queue))
638dcb89b5aSStephen Warren 		ci_ep_submit_next_request(ci_ep);
6392813006fSStephen Warren 
6402813006fSStephen Warren 	ci_req->req.actual = ci_req->req.length - len;
6412813006fSStephen Warren 	ci_debounce(ci_req, in);
6422813006fSStephen Warren 
6432813006fSStephen Warren 	DBG("ept%d %s req %p, complete %x\n",
6442813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, len);
645006c7026SStephen Warren 	if (num != 0 || controller.ep0_data_phase)
646dcb89b5aSStephen Warren 		ci_req->req.complete(&ci_ep->ep, &ci_req->req);
647006c7026SStephen Warren 	if (num == 0 && controller.ep0_data_phase) {
648006c7026SStephen Warren 		/*
649006c7026SStephen Warren 		 * Data Stage is complete, so flip ep0 dir for Status Stage,
650006c7026SStephen Warren 		 * which always transfers a packet in the opposite direction.
651006c7026SStephen Warren 		 */
652006c7026SStephen Warren 		DBG("%s: flip ep0 dir for Status Stage\n", __func__);
653006c7026SStephen Warren 		flip_ep0_direction();
654006c7026SStephen Warren 		controller.ep0_data_phase = false;
6552813006fSStephen Warren 		ci_req->req.length = 0;
656dcb89b5aSStephen Warren 		usb_ep_queue(&ci_ep->ep, &ci_req->req, 0);
657f016f8caSMarek Vasut 	}
658f016f8caSMarek Vasut }
659f016f8caSMarek Vasut 
660f016f8caSMarek Vasut #define SETUP(type, request) (((type) << 8) | (request))
661f016f8caSMarek Vasut 
handle_setup(void)662f016f8caSMarek Vasut static void handle_setup(void)
663f016f8caSMarek Vasut {
6642813006fSStephen Warren 	struct ci_ep *ci_ep = &controller.ep[0];
6652813006fSStephen Warren 	struct ci_req *ci_req;
6662813006fSStephen Warren 	struct usb_request *req;
667f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
668f016f8caSMarek Vasut 	struct ept_queue_head *head;
669f016f8caSMarek Vasut 	struct usb_ctrlrequest r;
670f016f8caSMarek Vasut 	int status = 0;
671f016f8caSMarek Vasut 	int num, in, _num, _in, i;
672f016f8caSMarek Vasut 	char *buf;
6732813006fSStephen Warren 
674a2d8f929SStephen Warren 	ci_req = controller.ep0_req;
6752813006fSStephen Warren 	req = &ci_req->req;
676f016f8caSMarek Vasut 	head = ci_get_qh(0, 0);	/* EP0 OUT */
677f016f8caSMarek Vasut 
678f016f8caSMarek Vasut 	ci_invalidate_qh(0);
679f016f8caSMarek Vasut 	memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
680fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
681fcf2ede1SStephen Warren 	writel(EPT_RX(0), &udc->epsetupstat);
682fcf2ede1SStephen Warren #else
683f016f8caSMarek Vasut 	writel(EPT_RX(0), &udc->epstat);
684fcf2ede1SStephen Warren #endif
685006c7026SStephen Warren 	DBG("handle setup %s, %x, %x index %x value %x length %x\n",
686006c7026SStephen Warren 	    reqname(r.bRequest), r.bRequestType, r.bRequest, r.wIndex,
687006c7026SStephen Warren 	    r.wValue, r.wLength);
688006c7026SStephen Warren 
689006c7026SStephen Warren 	/* Set EP0 dir for Data Stage based on Setup Stage data */
690006c7026SStephen Warren 	if (r.bRequestType & USB_DIR_IN) {
691006c7026SStephen Warren 		DBG("%s: Set ep0 to IN for Data Stage\n", __func__);
692006c7026SStephen Warren 		ep0_desc.bEndpointAddress = USB_DIR_IN;
693006c7026SStephen Warren 	} else {
694006c7026SStephen Warren 		DBG("%s: Set ep0 to OUT for Data Stage\n", __func__);
695006c7026SStephen Warren 		ep0_desc.bEndpointAddress = 0;
696006c7026SStephen Warren 	}
697006c7026SStephen Warren 	if (r.wLength) {
698006c7026SStephen Warren 		controller.ep0_data_phase = true;
699006c7026SStephen Warren 	} else {
700006c7026SStephen Warren 		/* 0 length -> no Data Stage. Flip dir for Status Stage */
701006c7026SStephen Warren 		DBG("%s: 0 length: flip ep0 dir for Status Stage\n", __func__);
702006c7026SStephen Warren 		flip_ep0_direction();
703006c7026SStephen Warren 		controller.ep0_data_phase = false;
704006c7026SStephen Warren 	}
705f016f8caSMarek Vasut 
7062813006fSStephen Warren 	list_del_init(&ci_req->queue);
7072813006fSStephen Warren 	ci_ep->req_primed = false;
7082813006fSStephen Warren 
709f016f8caSMarek Vasut 	switch (SETUP(r.bRequestType, r.bRequest)) {
710f016f8caSMarek Vasut 	case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
711f016f8caSMarek Vasut 		_num = r.wIndex & 15;
712f016f8caSMarek Vasut 		_in = !!(r.wIndex & 0x80);
713f016f8caSMarek Vasut 
714f016f8caSMarek Vasut 		if ((r.wValue == 0) && (r.wLength == 0)) {
715f016f8caSMarek Vasut 			req->length = 0;
716f016f8caSMarek Vasut 			for (i = 0; i < NUM_ENDPOINTS; i++) {
717f016f8caSMarek Vasut 				struct ci_ep *ep = &controller.ep[i];
718f016f8caSMarek Vasut 
719f016f8caSMarek Vasut 				if (!ep->desc)
720f016f8caSMarek Vasut 					continue;
721f016f8caSMarek Vasut 				num = ep->desc->bEndpointAddress
722f016f8caSMarek Vasut 						& USB_ENDPOINT_NUMBER_MASK;
723f016f8caSMarek Vasut 				in = (ep->desc->bEndpointAddress
724f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
725f016f8caSMarek Vasut 				if ((num == _num) && (in == _in)) {
726f016f8caSMarek Vasut 					ep_enable(num, in, ep->ep.maxpacket);
727f016f8caSMarek Vasut 					usb_ep_queue(controller.gadget.ep0,
728f016f8caSMarek Vasut 							req, 0);
729f016f8caSMarek Vasut 					break;
730f016f8caSMarek Vasut 				}
731f016f8caSMarek Vasut 			}
732f016f8caSMarek Vasut 		}
733f016f8caSMarek Vasut 		return;
734f016f8caSMarek Vasut 
735f016f8caSMarek Vasut 	case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
736f016f8caSMarek Vasut 		/*
737f016f8caSMarek Vasut 		 * write address delayed (will take effect
738f016f8caSMarek Vasut 		 * after the next IN txn)
739f016f8caSMarek Vasut 		 */
740f016f8caSMarek Vasut 		writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
741f016f8caSMarek Vasut 		req->length = 0;
742f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
743f016f8caSMarek Vasut 		return;
744f016f8caSMarek Vasut 
745f016f8caSMarek Vasut 	case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
746f016f8caSMarek Vasut 		req->length = 2;
747f016f8caSMarek Vasut 		buf = (char *)req->buf;
748f016f8caSMarek Vasut 		buf[0] = 1 << USB_DEVICE_SELF_POWERED;
749f016f8caSMarek Vasut 		buf[1] = 0;
750f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
751f016f8caSMarek Vasut 		return;
752f016f8caSMarek Vasut 	}
753f016f8caSMarek Vasut 	/* pass request up to the gadget driver */
754f016f8caSMarek Vasut 	if (controller.driver)
755f016f8caSMarek Vasut 		status = controller.driver->setup(&controller.gadget, &r);
756f016f8caSMarek Vasut 	else
757f016f8caSMarek Vasut 		status = -ENODEV;
758f016f8caSMarek Vasut 
759f016f8caSMarek Vasut 	if (!status)
760f016f8caSMarek Vasut 		return;
761f016f8caSMarek Vasut 	DBG("STALL reqname %s type %x value %x, index %x\n",
762f016f8caSMarek Vasut 	    reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
763f016f8caSMarek Vasut 	writel((1<<16) | (1 << 0), &udc->epctrl[0]);
764f016f8caSMarek Vasut }
765f016f8caSMarek Vasut 
stop_activity(void)766f016f8caSMarek Vasut static void stop_activity(void)
767f016f8caSMarek Vasut {
768f016f8caSMarek Vasut 	int i, num, in;
769f016f8caSMarek Vasut 	struct ept_queue_head *head;
770f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
771f016f8caSMarek Vasut 	writel(readl(&udc->epcomp), &udc->epcomp);
772fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
773fcf2ede1SStephen Warren 	writel(readl(&udc->epsetupstat), &udc->epsetupstat);
774fcf2ede1SStephen Warren #endif
775f016f8caSMarek Vasut 	writel(readl(&udc->epstat), &udc->epstat);
776f016f8caSMarek Vasut 	writel(0xffffffff, &udc->epflush);
777f016f8caSMarek Vasut 
778f016f8caSMarek Vasut 	/* error out any pending reqs */
779f016f8caSMarek Vasut 	for (i = 0; i < NUM_ENDPOINTS; i++) {
780f016f8caSMarek Vasut 		if (i != 0)
781f016f8caSMarek Vasut 			writel(0, &udc->epctrl[i]);
782f016f8caSMarek Vasut 		if (controller.ep[i].desc) {
783f016f8caSMarek Vasut 			num = controller.ep[i].desc->bEndpointAddress
784f016f8caSMarek Vasut 				& USB_ENDPOINT_NUMBER_MASK;
785f016f8caSMarek Vasut 			in = (controller.ep[i].desc->bEndpointAddress
786f016f8caSMarek Vasut 				& USB_DIR_IN) != 0;
787f016f8caSMarek Vasut 			head = ci_get_qh(num, in);
788f016f8caSMarek Vasut 			head->info = INFO_ACTIVE;
789f016f8caSMarek Vasut 			ci_flush_qh(num);
790f016f8caSMarek Vasut 		}
791f016f8caSMarek Vasut 	}
792f016f8caSMarek Vasut }
793f016f8caSMarek Vasut 
udc_irq(void)794f016f8caSMarek Vasut void udc_irq(void)
795f016f8caSMarek Vasut {
796f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
797f016f8caSMarek Vasut 	unsigned n = readl(&udc->usbsts);
798f016f8caSMarek Vasut 	writel(n, &udc->usbsts);
799f016f8caSMarek Vasut 	int bit, i, num, in;
800f016f8caSMarek Vasut 
801f016f8caSMarek Vasut 	n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
802f016f8caSMarek Vasut 	if (n == 0)
803f016f8caSMarek Vasut 		return;
804f016f8caSMarek Vasut 
805f016f8caSMarek Vasut 	if (n & STS_URI) {
806f016f8caSMarek Vasut 		DBG("-- reset --\n");
807f016f8caSMarek Vasut 		stop_activity();
808f016f8caSMarek Vasut 	}
809f016f8caSMarek Vasut 	if (n & STS_SLI)
810f016f8caSMarek Vasut 		DBG("-- suspend --\n");
811f016f8caSMarek Vasut 
812f016f8caSMarek Vasut 	if (n & STS_PCI) {
813f016f8caSMarek Vasut 		int max = 64;
814f016f8caSMarek Vasut 		int speed = USB_SPEED_FULL;
815f016f8caSMarek Vasut 
816fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
817fcf2ede1SStephen Warren 		bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
818fcf2ede1SStephen Warren #else
819f016f8caSMarek Vasut 		bit = (readl(&udc->portsc) >> 26) & 3;
820fcf2ede1SStephen Warren #endif
821f016f8caSMarek Vasut 		DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
822f016f8caSMarek Vasut 		if (bit == 2) {
823f016f8caSMarek Vasut 			speed = USB_SPEED_HIGH;
824f016f8caSMarek Vasut 			max = 512;
825f016f8caSMarek Vasut 		}
826f016f8caSMarek Vasut 		controller.gadget.speed = speed;
827f016f8caSMarek Vasut 		for (i = 1; i < NUM_ENDPOINTS; i++) {
828f016f8caSMarek Vasut 			if (controller.ep[i].ep.maxpacket > max)
829f016f8caSMarek Vasut 				controller.ep[i].ep.maxpacket = max;
830f016f8caSMarek Vasut 		}
831f016f8caSMarek Vasut 	}
832f016f8caSMarek Vasut 
833f016f8caSMarek Vasut 	if (n & STS_UEI)
834f016f8caSMarek Vasut 		printf("<UEI %x>\n", readl(&udc->epcomp));
835f016f8caSMarek Vasut 
836f016f8caSMarek Vasut 	if ((n & STS_UI) || (n & STS_UEI)) {
837fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
838fcf2ede1SStephen Warren 		n = readl(&udc->epsetupstat);
839fcf2ede1SStephen Warren #else
840f016f8caSMarek Vasut 		n = readl(&udc->epstat);
841fcf2ede1SStephen Warren #endif
842f016f8caSMarek Vasut 		if (n & EPT_RX(0))
843f016f8caSMarek Vasut 			handle_setup();
844f016f8caSMarek Vasut 
845f016f8caSMarek Vasut 		n = readl(&udc->epcomp);
846f016f8caSMarek Vasut 		if (n != 0)
847f016f8caSMarek Vasut 			writel(n, &udc->epcomp);
848f016f8caSMarek Vasut 
849f016f8caSMarek Vasut 		for (i = 0; i < NUM_ENDPOINTS && n; i++) {
850f016f8caSMarek Vasut 			if (controller.ep[i].desc) {
851f016f8caSMarek Vasut 				num = controller.ep[i].desc->bEndpointAddress
852f016f8caSMarek Vasut 					& USB_ENDPOINT_NUMBER_MASK;
853f016f8caSMarek Vasut 				in = (controller.ep[i].desc->bEndpointAddress
854f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
855f016f8caSMarek Vasut 				bit = (in) ? EPT_TX(num) : EPT_RX(num);
856f016f8caSMarek Vasut 				if (n & bit)
857f016f8caSMarek Vasut 					handle_ep_complete(&controller.ep[i]);
858f016f8caSMarek Vasut 			}
859f016f8caSMarek Vasut 		}
860f016f8caSMarek Vasut 	}
861f016f8caSMarek Vasut }
862f016f8caSMarek Vasut 
usb_gadget_handle_interrupts(int index)8632d48aa69SKishon Vijay Abraham I int usb_gadget_handle_interrupts(int index)
864f016f8caSMarek Vasut {
865f016f8caSMarek Vasut 	u32 value;
866f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
867f016f8caSMarek Vasut 
868f016f8caSMarek Vasut 	value = readl(&udc->usbsts);
869f016f8caSMarek Vasut 	if (value)
870f016f8caSMarek Vasut 		udc_irq();
871f016f8caSMarek Vasut 
872f016f8caSMarek Vasut 	return value;
873f016f8caSMarek Vasut }
874f016f8caSMarek Vasut 
udc_disconnect(void)87543a8f25bSStephen Warren void udc_disconnect(void)
87643a8f25bSStephen Warren {
87743a8f25bSStephen Warren 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
87843a8f25bSStephen Warren 	/* disable pullup */
87943a8f25bSStephen Warren 	stop_activity();
88043a8f25bSStephen Warren 	writel(USBCMD_FS2, &udc->usbcmd);
88143a8f25bSStephen Warren 	udelay(800);
88243a8f25bSStephen Warren 	if (controller.driver)
88343a8f25bSStephen Warren 		controller.driver->disconnect(&controller.gadget);
88443a8f25bSStephen Warren }
88543a8f25bSStephen Warren 
ci_pullup(struct usb_gadget * gadget,int is_on)886f016f8caSMarek Vasut static int ci_pullup(struct usb_gadget *gadget, int is_on)
887f016f8caSMarek Vasut {
888f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
889f016f8caSMarek Vasut 	if (is_on) {
890f016f8caSMarek Vasut 		/* RESET */
891f016f8caSMarek Vasut 		writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
892f016f8caSMarek Vasut 		udelay(200);
893f016f8caSMarek Vasut 
89481649529SRamon Fried 		ci_init_after_reset(controller.ctrl);
89581649529SRamon Fried 
896f72d8320SRob Herring 		writel((unsigned long)controller.epts, &udc->epinitaddr);
897f016f8caSMarek Vasut 
898f016f8caSMarek Vasut 		/* select DEVICE mode */
899f016f8caSMarek Vasut 		writel(USBMODE_DEVICE, &udc->usbmode);
900f016f8caSMarek Vasut 
901e2067993SEric Nelson #if !defined(CONFIG_USB_GADGET_DUALSPEED)
902e2067993SEric Nelson 		/* Port force Full-Speed Connect */
903e2067993SEric Nelson 		setbits_le32(&udc->portsc, PFSC);
904e2067993SEric Nelson #endif
905e2067993SEric Nelson 
906f016f8caSMarek Vasut 		writel(0xffffffff, &udc->epflush);
907f016f8caSMarek Vasut 
908f016f8caSMarek Vasut 		/* Turn on the USB connection by enabling the pullup resistor */
90930b32413SRamon Fried 		setbits_le32(&udc->usbcmd, USBCMD_ITC(MICRO_8FRAME) |
91030b32413SRamon Fried 			     USBCMD_RUN);
911f016f8caSMarek Vasut 	} else {
91243a8f25bSStephen Warren 		udc_disconnect();
913f016f8caSMarek Vasut 	}
914f016f8caSMarek Vasut 
915f016f8caSMarek Vasut 	return 0;
916f016f8caSMarek Vasut }
917f016f8caSMarek Vasut 
ci_udc_probe(void)918f016f8caSMarek Vasut static int ci_udc_probe(void)
919f016f8caSMarek Vasut {
920f016f8caSMarek Vasut 	struct ept_queue_head *head;
921f016f8caSMarek Vasut 	int i;
922f016f8caSMarek Vasut 
923f016f8caSMarek Vasut 	const int num = 2 * NUM_ENDPOINTS;
924f016f8caSMarek Vasut 
925f016f8caSMarek Vasut 	const int eplist_min_align = 4096;
926f016f8caSMarek Vasut 	const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
927f016f8caSMarek Vasut 	const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
928f016f8caSMarek Vasut 	const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
929f016f8caSMarek Vasut 
930f016f8caSMarek Vasut 	/* The QH list must be aligned to 4096 bytes. */
931f016f8caSMarek Vasut 	controller.epts = memalign(eplist_align, eplist_sz);
932f016f8caSMarek Vasut 	if (!controller.epts)
933f016f8caSMarek Vasut 		return -ENOMEM;
934f016f8caSMarek Vasut 	memset(controller.epts, 0, eplist_sz);
935f016f8caSMarek Vasut 
93606b38fcbSStephen Warren 	controller.items_mem = memalign(ILIST_ALIGN, ILIST_SZ);
937f016f8caSMarek Vasut 	if (!controller.items_mem) {
938f016f8caSMarek Vasut 		free(controller.epts);
939f016f8caSMarek Vasut 		return -ENOMEM;
940f016f8caSMarek Vasut 	}
94106b38fcbSStephen Warren 	memset(controller.items_mem, 0, ILIST_SZ);
942f016f8caSMarek Vasut 
943f016f8caSMarek Vasut 	for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
944f016f8caSMarek Vasut 		/*
945f016f8caSMarek Vasut 		 * Configure QH for each endpoint. The structure of the QH list
946f016f8caSMarek Vasut 		 * is such that each two subsequent fields, N and N+1 where N is
947f016f8caSMarek Vasut 		 * even, in the QH list represent QH for one endpoint. The Nth
948f016f8caSMarek Vasut 		 * entry represents OUT configuration and the N+1th entry does
949f016f8caSMarek Vasut 		 * represent IN configuration of the endpoint.
950f016f8caSMarek Vasut 		 */
951f016f8caSMarek Vasut 		head = controller.epts + i;
952f016f8caSMarek Vasut 		if (i < 2)
953f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
954f016f8caSMarek Vasut 				| CONFIG_ZLT | CONFIG_IOS;
955f016f8caSMarek Vasut 		else
956f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
957f016f8caSMarek Vasut 				| CONFIG_ZLT;
958f016f8caSMarek Vasut 		head->next = TERMINATE;
959f016f8caSMarek Vasut 		head->info = 0;
960f016f8caSMarek Vasut 
961f016f8caSMarek Vasut 		if (i & 1) {
962d7beeb93SStephen Warren 			ci_flush_qh(i / 2);
963d7beeb93SStephen Warren 			ci_flush_qtd(i / 2);
964f016f8caSMarek Vasut 		}
965f016f8caSMarek Vasut 	}
966f016f8caSMarek Vasut 
967f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep_list);
968f016f8caSMarek Vasut 
969f016f8caSMarek Vasut 	/* Init EP 0 */
970f016f8caSMarek Vasut 	memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
971054731b0SStephen Warren 	controller.ep[0].desc = &ep0_desc;
9722813006fSStephen Warren 	INIT_LIST_HEAD(&controller.ep[0].queue);
9732813006fSStephen Warren 	controller.ep[0].req_primed = false;
974f016f8caSMarek Vasut 	controller.gadget.ep0 = &controller.ep[0].ep;
975f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
976f016f8caSMarek Vasut 
9776a132416SSiva Durga Prasad Paladugu 	/* Init EP 1..3 */
9786a132416SSiva Durga Prasad Paladugu 	for (i = 1; i < 4; i++) {
9796a132416SSiva Durga Prasad Paladugu 		memcpy(&controller.ep[i].ep, &ci_ep_init[i],
9806a132416SSiva Durga Prasad Paladugu 		       sizeof(*ci_ep_init));
9816a132416SSiva Durga Prasad Paladugu 		INIT_LIST_HEAD(&controller.ep[i].queue);
9826a132416SSiva Durga Prasad Paladugu 		controller.ep[i].req_primed = false;
9836a132416SSiva Durga Prasad Paladugu 		list_add_tail(&controller.ep[i].ep.ep_list,
9846a132416SSiva Durga Prasad Paladugu 			      &controller.gadget.ep_list);
9856a132416SSiva Durga Prasad Paladugu 	}
9866a132416SSiva Durga Prasad Paladugu 
9876a132416SSiva Durga Prasad Paladugu 	/* Init EP 4..n */
9886a132416SSiva Durga Prasad Paladugu 	for (i = 4; i < NUM_ENDPOINTS; i++) {
9896a132416SSiva Durga Prasad Paladugu 		memcpy(&controller.ep[i].ep, &ci_ep_init[4],
990f016f8caSMarek Vasut 		       sizeof(*ci_ep_init));
9912813006fSStephen Warren 		INIT_LIST_HEAD(&controller.ep[i].queue);
9922813006fSStephen Warren 		controller.ep[i].req_primed = false;
993f016f8caSMarek Vasut 		list_add_tail(&controller.ep[i].ep.ep_list,
994f016f8caSMarek Vasut 			      &controller.gadget.ep_list);
995f016f8caSMarek Vasut 	}
996f016f8caSMarek Vasut 
997a2d8f929SStephen Warren 	ci_ep_alloc_request(&controller.ep[0].ep, 0);
998a2d8f929SStephen Warren 	if (!controller.ep0_req) {
9999a7d34beSStephen Warren 		free(controller.items_mem);
1000a2d8f929SStephen Warren 		free(controller.epts);
1001a2d8f929SStephen Warren 		return -ENOMEM;
1002a2d8f929SStephen Warren 	}
1003a2d8f929SStephen Warren 
1004f016f8caSMarek Vasut 	return 0;
1005f016f8caSMarek Vasut }
1006f016f8caSMarek Vasut 
usb_gadget_register_driver(struct usb_gadget_driver * driver)1007f016f8caSMarek Vasut int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1008f016f8caSMarek Vasut {
1009f016f8caSMarek Vasut 	int ret;
1010f016f8caSMarek Vasut 
1011f016f8caSMarek Vasut 	if (!driver)
1012f016f8caSMarek Vasut 		return -EINVAL;
1013f016f8caSMarek Vasut 	if (!driver->bind || !driver->setup || !driver->disconnect)
1014f016f8caSMarek Vasut 		return -EINVAL;
1015f016f8caSMarek Vasut 	if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
1016f016f8caSMarek Vasut 		return -EINVAL;
1017f016f8caSMarek Vasut 
1018*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
1019fbeceb26SSimon Glass 	ret = usb_setup_ehci_gadget(&controller.ctrl);
1020fbeceb26SSimon Glass #else
1021f016f8caSMarek Vasut 	ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
1022fbeceb26SSimon Glass #endif
1023f016f8caSMarek Vasut 	if (ret)
1024f016f8caSMarek Vasut 		return ret;
1025f016f8caSMarek Vasut 
1026f016f8caSMarek Vasut 	ret = ci_udc_probe();
10278991fdefSYe.Li 	if (ret) {
10288991fdefSYe.Li 		DBG("udc probe failed, returned %d\n", ret);
10298991fdefSYe.Li 		return ret;
1030f016f8caSMarek Vasut 	}
1031f016f8caSMarek Vasut 
1032f016f8caSMarek Vasut 	ret = driver->bind(&controller.gadget);
1033f016f8caSMarek Vasut 	if (ret) {
1034f016f8caSMarek Vasut 		DBG("driver->bind() returned %d\n", ret);
1035f016f8caSMarek Vasut 		return ret;
1036f016f8caSMarek Vasut 	}
1037f016f8caSMarek Vasut 	controller.driver = driver;
1038f016f8caSMarek Vasut 
1039f016f8caSMarek Vasut 	return 0;
1040f016f8caSMarek Vasut }
1041f016f8caSMarek Vasut 
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)1042f016f8caSMarek Vasut int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1043f016f8caSMarek Vasut {
1044b7c00516SStephen Warren 	udc_disconnect();
1045b7c00516SStephen Warren 
1046fb22ae6cSStephen Warren 	driver->unbind(&controller.gadget);
1047fb22ae6cSStephen Warren 	controller.driver = NULL;
1048fb22ae6cSStephen Warren 
1049b7c00516SStephen Warren 	ci_ep_free_request(&controller.ep[0].ep, &controller.ep0_req->req);
1050b7c00516SStephen Warren 	free(controller.items_mem);
1051b7c00516SStephen Warren 	free(controller.epts);
1052b7c00516SStephen Warren 
1053f016f8caSMarek Vasut 	return 0;
1054f016f8caSMarek Vasut }
1055842ef9a9SStephen Warren 
dfu_usb_get_reset(void)1056842ef9a9SStephen Warren bool dfu_usb_get_reset(void)
1057842ef9a9SStephen Warren {
1058842ef9a9SStephen Warren 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
1059842ef9a9SStephen Warren 
1060842ef9a9SStephen Warren 	return !!(readl(&udc->usbsts) & STS_URI);
1061842ef9a9SStephen Warren }
1062