xref: /rk3399_rockchip-uboot/drivers/usb/gadget/ci_udc.c (revision 7484d84cbb58e01ff1d2d458d899ea1fba012724)
1f016f8caSMarek Vasut /*
2f016f8caSMarek Vasut  * Copyright 2011, Marvell Semiconductor Inc.
3f016f8caSMarek Vasut  * Lei Wen <leiwen@marvell.com>
4f016f8caSMarek Vasut  *
5f016f8caSMarek Vasut  * SPDX-License-Identifier:	GPL-2.0+
6f016f8caSMarek Vasut  *
7f016f8caSMarek Vasut  * Back ported to the 8xx platform (from the 8260 platform) by
8f016f8caSMarek Vasut  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9f016f8caSMarek Vasut  */
10f016f8caSMarek Vasut 
11f016f8caSMarek Vasut #include <common.h>
12f016f8caSMarek Vasut #include <command.h>
13f016f8caSMarek Vasut #include <config.h>
14f016f8caSMarek Vasut #include <net.h>
15f016f8caSMarek Vasut #include <malloc.h>
16f016f8caSMarek Vasut #include <asm/byteorder.h>
17f016f8caSMarek Vasut #include <asm/errno.h>
18f016f8caSMarek Vasut #include <asm/io.h>
19f016f8caSMarek Vasut #include <asm/unaligned.h>
20f016f8caSMarek Vasut #include <linux/types.h>
21f016f8caSMarek Vasut #include <linux/usb/ch9.h>
22f016f8caSMarek Vasut #include <linux/usb/gadget.h>
23f016f8caSMarek Vasut #include <usb/ci_udc.h>
24f016f8caSMarek Vasut #include "../host/ehci.h"
25f016f8caSMarek Vasut #include "ci_udc.h"
26f016f8caSMarek Vasut 
27f016f8caSMarek Vasut /*
28f016f8caSMarek Vasut  * Check if the system has too long cachelines. If the cachelines are
29f016f8caSMarek Vasut  * longer then 128b, the driver will not be able flush/invalidate data
30f016f8caSMarek Vasut  * cache over separate QH entries. We use 128b because one QH entry is
31f016f8caSMarek Vasut  * 64b long and there are always two QH list entries for each endpoint.
32f016f8caSMarek Vasut  */
33f016f8caSMarek Vasut #if ARCH_DMA_MINALIGN > 128
34f016f8caSMarek Vasut #error This driver can not work on systems with caches longer than 128b
35f016f8caSMarek Vasut #endif
36f016f8caSMarek Vasut 
37f016f8caSMarek Vasut #ifndef DEBUG
38f016f8caSMarek Vasut #define DBG(x...) do {} while (0)
39f016f8caSMarek Vasut #else
40f016f8caSMarek Vasut #define DBG(x...) printf(x)
41f016f8caSMarek Vasut static const char *reqname(unsigned r)
42f016f8caSMarek Vasut {
43f016f8caSMarek Vasut 	switch (r) {
44f016f8caSMarek Vasut 	case USB_REQ_GET_STATUS: return "GET_STATUS";
45f016f8caSMarek Vasut 	case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
46f016f8caSMarek Vasut 	case USB_REQ_SET_FEATURE: return "SET_FEATURE";
47f016f8caSMarek Vasut 	case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
48f016f8caSMarek Vasut 	case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
49f016f8caSMarek Vasut 	case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
50f016f8caSMarek Vasut 	case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
51f016f8caSMarek Vasut 	case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
52f016f8caSMarek Vasut 	case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
53f016f8caSMarek Vasut 	case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
54f016f8caSMarek Vasut 	default: return "*UNKNOWN*";
55f016f8caSMarek Vasut 	}
56f016f8caSMarek Vasut }
57f016f8caSMarek Vasut #endif
58f016f8caSMarek Vasut 
59f016f8caSMarek Vasut static struct usb_endpoint_descriptor ep0_out_desc = {
60f016f8caSMarek Vasut 	.bLength = sizeof(struct usb_endpoint_descriptor),
61f016f8caSMarek Vasut 	.bDescriptorType = USB_DT_ENDPOINT,
62f016f8caSMarek Vasut 	.bEndpointAddress = 0,
63f016f8caSMarek Vasut 	.bmAttributes =	USB_ENDPOINT_XFER_CONTROL,
64f016f8caSMarek Vasut };
65f016f8caSMarek Vasut 
66f016f8caSMarek Vasut static struct usb_endpoint_descriptor ep0_in_desc = {
67f016f8caSMarek Vasut 	.bLength = sizeof(struct usb_endpoint_descriptor),
68f016f8caSMarek Vasut 	.bDescriptorType = USB_DT_ENDPOINT,
69f016f8caSMarek Vasut 	.bEndpointAddress = USB_DIR_IN,
70f016f8caSMarek Vasut 	.bmAttributes =	USB_ENDPOINT_XFER_CONTROL,
71f016f8caSMarek Vasut };
72f016f8caSMarek Vasut 
73f016f8caSMarek Vasut static int ci_pullup(struct usb_gadget *gadget, int is_on);
74f016f8caSMarek Vasut static int ci_ep_enable(struct usb_ep *ep,
75f016f8caSMarek Vasut 		const struct usb_endpoint_descriptor *desc);
76f016f8caSMarek Vasut static int ci_ep_disable(struct usb_ep *ep);
77f016f8caSMarek Vasut static int ci_ep_queue(struct usb_ep *ep,
78f016f8caSMarek Vasut 		struct usb_request *req, gfp_t gfp_flags);
79f016f8caSMarek Vasut static struct usb_request *
80f016f8caSMarek Vasut ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
81f016f8caSMarek Vasut static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
82f016f8caSMarek Vasut 
83f016f8caSMarek Vasut static struct usb_gadget_ops ci_udc_ops = {
84f016f8caSMarek Vasut 	.pullup = ci_pullup,
85f016f8caSMarek Vasut };
86f016f8caSMarek Vasut 
87f016f8caSMarek Vasut static struct usb_ep_ops ci_ep_ops = {
88f016f8caSMarek Vasut 	.enable         = ci_ep_enable,
89f016f8caSMarek Vasut 	.disable        = ci_ep_disable,
90f016f8caSMarek Vasut 	.queue          = ci_ep_queue,
91f016f8caSMarek Vasut 	.alloc_request  = ci_ep_alloc_request,
92f016f8caSMarek Vasut 	.free_request   = ci_ep_free_request,
93f016f8caSMarek Vasut };
94f016f8caSMarek Vasut 
95f016f8caSMarek Vasut /* Init values for USB endpoints. */
96f016f8caSMarek Vasut static const struct usb_ep ci_ep_init[2] = {
97f016f8caSMarek Vasut 	[0] = {	/* EP 0 */
98f016f8caSMarek Vasut 		.maxpacket	= 64,
99f016f8caSMarek Vasut 		.name		= "ep0",
100f016f8caSMarek Vasut 		.ops		= &ci_ep_ops,
101f016f8caSMarek Vasut 	},
102f016f8caSMarek Vasut 	[1] = {	/* EP 1..n */
103f016f8caSMarek Vasut 		.maxpacket	= 512,
104f016f8caSMarek Vasut 		.name		= "ep-",
105f016f8caSMarek Vasut 		.ops		= &ci_ep_ops,
106f016f8caSMarek Vasut 	},
107f016f8caSMarek Vasut };
108f016f8caSMarek Vasut 
109f016f8caSMarek Vasut static struct ci_drv controller = {
110f016f8caSMarek Vasut 	.gadget	= {
111f016f8caSMarek Vasut 		.name	= "ci_udc",
112f016f8caSMarek Vasut 		.ops	= &ci_udc_ops,
113f016f8caSMarek Vasut 		.is_dualspeed = 1,
114f016f8caSMarek Vasut 	},
115f016f8caSMarek Vasut };
116f016f8caSMarek Vasut 
117f016f8caSMarek Vasut /**
118f016f8caSMarek Vasut  * ci_get_qh() - return queue head for endpoint
119f016f8caSMarek Vasut  * @ep_num:	Endpoint number
120f016f8caSMarek Vasut  * @dir_in:	Direction of the endpoint (IN = 1, OUT = 0)
121f016f8caSMarek Vasut  *
122f016f8caSMarek Vasut  * This function returns the QH associated with particular endpoint
123f016f8caSMarek Vasut  * and it's direction.
124f016f8caSMarek Vasut  */
125f016f8caSMarek Vasut static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
126f016f8caSMarek Vasut {
127f016f8caSMarek Vasut 	return &controller.epts[(ep_num * 2) + dir_in];
128f016f8caSMarek Vasut }
129f016f8caSMarek Vasut 
130f016f8caSMarek Vasut /**
131f016f8caSMarek Vasut  * ci_get_qtd() - return queue item for endpoint
132f016f8caSMarek Vasut  * @ep_num:	Endpoint number
133f016f8caSMarek Vasut  * @dir_in:	Direction of the endpoint (IN = 1, OUT = 0)
134f016f8caSMarek Vasut  *
135f016f8caSMarek Vasut  * This function returns the QH associated with particular endpoint
136f016f8caSMarek Vasut  * and it's direction.
137f016f8caSMarek Vasut  */
138f016f8caSMarek Vasut static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
139f016f8caSMarek Vasut {
140f016f8caSMarek Vasut 	return controller.items[(ep_num * 2) + dir_in];
141f016f8caSMarek Vasut }
142f016f8caSMarek Vasut 
143f016f8caSMarek Vasut /**
144f016f8caSMarek Vasut  * ci_flush_qh - flush cache over queue head
145f016f8caSMarek Vasut  * @ep_num:	Endpoint number
146f016f8caSMarek Vasut  *
147f016f8caSMarek Vasut  * This function flushes cache over QH for particular endpoint.
148f016f8caSMarek Vasut  */
149f016f8caSMarek Vasut static void ci_flush_qh(int ep_num)
150f016f8caSMarek Vasut {
151f016f8caSMarek Vasut 	struct ept_queue_head *head = ci_get_qh(ep_num, 0);
152f016f8caSMarek Vasut 	const uint32_t start = (uint32_t)head;
153f016f8caSMarek Vasut 	const uint32_t end = start + 2 * sizeof(*head);
154f016f8caSMarek Vasut 
155f016f8caSMarek Vasut 	flush_dcache_range(start, end);
156f016f8caSMarek Vasut }
157f016f8caSMarek Vasut 
158f016f8caSMarek Vasut /**
159f016f8caSMarek Vasut  * ci_invalidate_qh - invalidate cache over queue head
160f016f8caSMarek Vasut  * @ep_num:	Endpoint number
161f016f8caSMarek Vasut  *
162f016f8caSMarek Vasut  * This function invalidates cache over QH for particular endpoint.
163f016f8caSMarek Vasut  */
164f016f8caSMarek Vasut static void ci_invalidate_qh(int ep_num)
165f016f8caSMarek Vasut {
166f016f8caSMarek Vasut 	struct ept_queue_head *head = ci_get_qh(ep_num, 0);
167f016f8caSMarek Vasut 	uint32_t start = (uint32_t)head;
168f016f8caSMarek Vasut 	uint32_t end = start + 2 * sizeof(*head);
169f016f8caSMarek Vasut 
170f016f8caSMarek Vasut 	invalidate_dcache_range(start, end);
171f016f8caSMarek Vasut }
172f016f8caSMarek Vasut 
173f016f8caSMarek Vasut /**
174f016f8caSMarek Vasut  * ci_flush_qtd - flush cache over queue item
175f016f8caSMarek Vasut  * @ep_num:	Endpoint number
176f016f8caSMarek Vasut  *
177f016f8caSMarek Vasut  * This function flushes cache over qTD pair for particular endpoint.
178f016f8caSMarek Vasut  */
179f016f8caSMarek Vasut static void ci_flush_qtd(int ep_num)
180f016f8caSMarek Vasut {
181f016f8caSMarek Vasut 	struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
182f016f8caSMarek Vasut 	const uint32_t start = (uint32_t)item;
183f016f8caSMarek Vasut 	const uint32_t end_raw = start + 2 * sizeof(*item);
184f016f8caSMarek Vasut 	const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
185f016f8caSMarek Vasut 
186f016f8caSMarek Vasut 	flush_dcache_range(start, end);
187f016f8caSMarek Vasut }
188f016f8caSMarek Vasut 
189f016f8caSMarek Vasut /**
190f016f8caSMarek Vasut  * ci_invalidate_qtd - invalidate cache over queue item
191f016f8caSMarek Vasut  * @ep_num:	Endpoint number
192f016f8caSMarek Vasut  *
193f016f8caSMarek Vasut  * This function invalidates cache over qTD pair for particular endpoint.
194f016f8caSMarek Vasut  */
195f016f8caSMarek Vasut static void ci_invalidate_qtd(int ep_num)
196f016f8caSMarek Vasut {
197f016f8caSMarek Vasut 	struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
198f016f8caSMarek Vasut 	const uint32_t start = (uint32_t)item;
199f016f8caSMarek Vasut 	const uint32_t end_raw = start + 2 * sizeof(*item);
200f016f8caSMarek Vasut 	const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
201f016f8caSMarek Vasut 
202f016f8caSMarek Vasut 	invalidate_dcache_range(start, end);
203f016f8caSMarek Vasut }
204f016f8caSMarek Vasut 
205f016f8caSMarek Vasut static struct usb_request *
206f016f8caSMarek Vasut ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
207f016f8caSMarek Vasut {
2082813006fSStephen Warren 	struct ci_req *ci_req;
2092813006fSStephen Warren 
2102813006fSStephen Warren 	ci_req = memalign(ARCH_DMA_MINALIGN, sizeof(*ci_req));
2112813006fSStephen Warren 	if (!ci_req)
2122813006fSStephen Warren 		return NULL;
2132813006fSStephen Warren 
2142813006fSStephen Warren 	INIT_LIST_HEAD(&ci_req->queue);
2152813006fSStephen Warren 	ci_req->b_buf = 0;
2162813006fSStephen Warren 
2172813006fSStephen Warren 	return &ci_req->req;
218f016f8caSMarek Vasut }
219f016f8caSMarek Vasut 
2202813006fSStephen Warren static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
221f016f8caSMarek Vasut {
2222813006fSStephen Warren 	struct ci_req *ci_req;
2232813006fSStephen Warren 
2242813006fSStephen Warren 	ci_req = container_of(req, struct ci_req, req);
2252813006fSStephen Warren 	if (ci_req->b_buf)
2262813006fSStephen Warren 		free(ci_req->b_buf);
2272813006fSStephen Warren 	free(ci_req);
228f016f8caSMarek Vasut }
229f016f8caSMarek Vasut 
230f016f8caSMarek Vasut static void ep_enable(int num, int in, int maxpacket)
231f016f8caSMarek Vasut {
232f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
233f016f8caSMarek Vasut 	unsigned n;
234f016f8caSMarek Vasut 
235f016f8caSMarek Vasut 	n = readl(&udc->epctrl[num]);
236f016f8caSMarek Vasut 	if (in)
237f016f8caSMarek Vasut 		n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
238f016f8caSMarek Vasut 	else
239f016f8caSMarek Vasut 		n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
240f016f8caSMarek Vasut 
241f016f8caSMarek Vasut 	if (num != 0) {
242f016f8caSMarek Vasut 		struct ept_queue_head *head = ci_get_qh(num, in);
243f016f8caSMarek Vasut 
244f016f8caSMarek Vasut 		head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
245f016f8caSMarek Vasut 		ci_flush_qh(num);
246f016f8caSMarek Vasut 	}
247f016f8caSMarek Vasut 	writel(n, &udc->epctrl[num]);
248f016f8caSMarek Vasut }
249f016f8caSMarek Vasut 
250f016f8caSMarek Vasut static int ci_ep_enable(struct usb_ep *ep,
251f016f8caSMarek Vasut 		const struct usb_endpoint_descriptor *desc)
252f016f8caSMarek Vasut {
253f016f8caSMarek Vasut 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
254f016f8caSMarek Vasut 	int num, in;
255f016f8caSMarek Vasut 	num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
256f016f8caSMarek Vasut 	in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
257f016f8caSMarek Vasut 	ci_ep->desc = desc;
258f016f8caSMarek Vasut 
259f016f8caSMarek Vasut 	if (num) {
260f016f8caSMarek Vasut 		int max = get_unaligned_le16(&desc->wMaxPacketSize);
261f016f8caSMarek Vasut 
262f016f8caSMarek Vasut 		if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
263f016f8caSMarek Vasut 			max = 64;
264f016f8caSMarek Vasut 		if (ep->maxpacket != max) {
265f016f8caSMarek Vasut 			DBG("%s: from %d to %d\n", __func__,
266f016f8caSMarek Vasut 			    ep->maxpacket, max);
267f016f8caSMarek Vasut 			ep->maxpacket = max;
268f016f8caSMarek Vasut 		}
269f016f8caSMarek Vasut 	}
270f016f8caSMarek Vasut 	ep_enable(num, in, ep->maxpacket);
271f016f8caSMarek Vasut 	DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
272f016f8caSMarek Vasut 	return 0;
273f016f8caSMarek Vasut }
274f016f8caSMarek Vasut 
275f016f8caSMarek Vasut static int ci_ep_disable(struct usb_ep *ep)
276f016f8caSMarek Vasut {
277f016f8caSMarek Vasut 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
278f016f8caSMarek Vasut 
279f016f8caSMarek Vasut 	ci_ep->desc = NULL;
280f016f8caSMarek Vasut 	return 0;
281f016f8caSMarek Vasut }
282f016f8caSMarek Vasut 
2832813006fSStephen Warren static int ci_bounce(struct ci_req *ci_req, int in)
284f016f8caSMarek Vasut {
2852813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
2862813006fSStephen Warren 	uint32_t addr = (uint32_t)req->buf;
2872813006fSStephen Warren 	uint32_t hwaddr;
2882813006fSStephen Warren 	uint32_t aligned_used_len;
289f016f8caSMarek Vasut 
290f016f8caSMarek Vasut 	/* Input buffer address is not aligned. */
291f016f8caSMarek Vasut 	if (addr & (ARCH_DMA_MINALIGN - 1))
292f016f8caSMarek Vasut 		goto align;
293f016f8caSMarek Vasut 
294f016f8caSMarek Vasut 	/* Input buffer length is not aligned. */
2952813006fSStephen Warren 	if (req->length & (ARCH_DMA_MINALIGN - 1))
296f016f8caSMarek Vasut 		goto align;
297f016f8caSMarek Vasut 
298f016f8caSMarek Vasut 	/* The buffer is well aligned, only flush cache. */
2992813006fSStephen Warren 	ci_req->hw_len = req->length;
3002813006fSStephen Warren 	ci_req->hw_buf = req->buf;
301f016f8caSMarek Vasut 	goto flush;
302f016f8caSMarek Vasut 
303f016f8caSMarek Vasut align:
3042813006fSStephen Warren 	if (ci_req->b_buf && req->length > ci_req->b_len) {
3052813006fSStephen Warren 		free(ci_req->b_buf);
3062813006fSStephen Warren 		ci_req->b_buf = 0;
3072813006fSStephen Warren 	}
3082813006fSStephen Warren 	if (!ci_req->b_buf) {
3092813006fSStephen Warren 		ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
3102813006fSStephen Warren 		ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
3112813006fSStephen Warren 		if (!ci_req->b_buf)
312f016f8caSMarek Vasut 			return -ENOMEM;
313f016f8caSMarek Vasut 	}
3142813006fSStephen Warren 	ci_req->hw_len = ci_req->b_len;
3152813006fSStephen Warren 	ci_req->hw_buf = ci_req->b_buf;
3162813006fSStephen Warren 
317f016f8caSMarek Vasut 	if (in)
3182813006fSStephen Warren 		memcpy(ci_req->hw_buf, req->buf, req->length);
319f016f8caSMarek Vasut 
320f016f8caSMarek Vasut flush:
3212813006fSStephen Warren 	hwaddr = (uint32_t)ci_req->hw_buf;
3222813006fSStephen Warren 	aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
3232813006fSStephen Warren 	flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
324f016f8caSMarek Vasut 
325f016f8caSMarek Vasut 	return 0;
326f016f8caSMarek Vasut }
327f016f8caSMarek Vasut 
3282813006fSStephen Warren static void ci_debounce(struct ci_req *ci_req, int in)
329f016f8caSMarek Vasut {
3302813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
3312813006fSStephen Warren 	uint32_t addr = (uint32_t)req->buf;
3322813006fSStephen Warren 	uint32_t hwaddr = (uint32_t)ci_req->hw_buf;
3332813006fSStephen Warren 	uint32_t aligned_used_len;
334f016f8caSMarek Vasut 
3352813006fSStephen Warren 	if (in)
3362813006fSStephen Warren 		return;
337f016f8caSMarek Vasut 
3382813006fSStephen Warren 	aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
3392813006fSStephen Warren 	invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
3402813006fSStephen Warren 
3412813006fSStephen Warren 	if (addr == hwaddr)
342f016f8caSMarek Vasut 		return; /* not a bounce */
343f016f8caSMarek Vasut 
3442813006fSStephen Warren 	memcpy(req->buf, ci_req->hw_buf, req->actual);
345f016f8caSMarek Vasut }
346f016f8caSMarek Vasut 
3472813006fSStephen Warren static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
348f016f8caSMarek Vasut {
349f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
350f016f8caSMarek Vasut 	struct ept_queue_item *item;
351f016f8caSMarek Vasut 	struct ept_queue_head *head;
3522813006fSStephen Warren 	int bit, num, len, in;
3532813006fSStephen Warren 	struct ci_req *ci_req;
3542813006fSStephen Warren 
3552813006fSStephen Warren 	ci_ep->req_primed = true;
3562813006fSStephen Warren 
357f016f8caSMarek Vasut 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
358f016f8caSMarek Vasut 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
359f016f8caSMarek Vasut 	item = ci_get_qtd(num, in);
360f016f8caSMarek Vasut 	head = ci_get_qh(num, in);
361f016f8caSMarek Vasut 
3622813006fSStephen Warren 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
3632813006fSStephen Warren 	len = ci_req->req.length;
364f016f8caSMarek Vasut 
365f016f8caSMarek Vasut 	item->next = TERMINATE;
366f016f8caSMarek Vasut 	item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
3672813006fSStephen Warren 	item->page0 = (uint32_t)ci_req->hw_buf;
3682813006fSStephen Warren 	item->page1 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x1000;
3692813006fSStephen Warren 	item->page2 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x2000;
3702813006fSStephen Warren 	item->page3 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x3000;
3712813006fSStephen Warren 	item->page4 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x4000;
372f016f8caSMarek Vasut 	ci_flush_qtd(num);
373f016f8caSMarek Vasut 
374f016f8caSMarek Vasut 	head->next = (unsigned) item;
375f016f8caSMarek Vasut 	head->info = 0;
376f016f8caSMarek Vasut 
3772813006fSStephen Warren 	DBG("ept%d %s queue len %x, req %p, buffer %p\n",
3782813006fSStephen Warren 	    num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
379f016f8caSMarek Vasut 	ci_flush_qh(num);
380f016f8caSMarek Vasut 
381f016f8caSMarek Vasut 	if (in)
382f016f8caSMarek Vasut 		bit = EPT_TX(num);
383f016f8caSMarek Vasut 	else
384f016f8caSMarek Vasut 		bit = EPT_RX(num);
385f016f8caSMarek Vasut 
386f016f8caSMarek Vasut 	writel(bit, &udc->epprime);
3872813006fSStephen Warren }
3882813006fSStephen Warren 
3892813006fSStephen Warren static int ci_ep_queue(struct usb_ep *ep,
3902813006fSStephen Warren 		struct usb_request *req, gfp_t gfp_flags)
3912813006fSStephen Warren {
3922813006fSStephen Warren 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
3932813006fSStephen Warren 	struct ci_req *ci_req = container_of(req, struct ci_req, req);
3942813006fSStephen Warren 	int in, ret;
3952813006fSStephen Warren 	int __maybe_unused num;
3962813006fSStephen Warren 
3972813006fSStephen Warren 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
3982813006fSStephen Warren 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
3992813006fSStephen Warren 
400*7484d84cSStephen Warren 	if (!num && ci_ep->req_primed) {
401*7484d84cSStephen Warren 		/*
402*7484d84cSStephen Warren 		 * The flipping of ep0 between IN and OUT relies on
403*7484d84cSStephen Warren 		 * ci_ep_queue consuming the current IN/OUT setting
404*7484d84cSStephen Warren 		 * immediately. If this is deferred to a later point when the
405*7484d84cSStephen Warren 		 * req is pulled out of ci_req->queue, then the IN/OUT setting
406*7484d84cSStephen Warren 		 * may have been changed since the req was queued, and state
407*7484d84cSStephen Warren 		 * will get out of sync. This condition doesn't occur today,
408*7484d84cSStephen Warren 		 * but could if bugs were introduced later, and this error
409*7484d84cSStephen Warren 		 * check will save a lot of debugging time.
410*7484d84cSStephen Warren 		 */
411*7484d84cSStephen Warren 		printf("%s: ep0 transaction already in progress\n", __func__);
412*7484d84cSStephen Warren 		return -EPROTO;
413*7484d84cSStephen Warren 	}
414*7484d84cSStephen Warren 
4152813006fSStephen Warren 	ret = ci_bounce(ci_req, in);
4162813006fSStephen Warren 	if (ret)
4172813006fSStephen Warren 		return ret;
4182813006fSStephen Warren 
4192813006fSStephen Warren 	DBG("ept%d %s pre-queue req %p, buffer %p\n",
4202813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, ci_req->hw_buf);
4212813006fSStephen Warren 	list_add_tail(&ci_req->queue, &ci_ep->queue);
4222813006fSStephen Warren 
4232813006fSStephen Warren 	if (!ci_ep->req_primed)
4242813006fSStephen Warren 		ci_ep_submit_next_request(ci_ep);
425f016f8caSMarek Vasut 
426f016f8caSMarek Vasut 	return 0;
427f016f8caSMarek Vasut }
428f016f8caSMarek Vasut 
429f016f8caSMarek Vasut static void handle_ep_complete(struct ci_ep *ep)
430f016f8caSMarek Vasut {
431f016f8caSMarek Vasut 	struct ept_queue_item *item;
432f016f8caSMarek Vasut 	int num, in, len;
4332813006fSStephen Warren 	struct ci_req *ci_req;
4342813006fSStephen Warren 
435f016f8caSMarek Vasut 	num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
436f016f8caSMarek Vasut 	in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
437f016f8caSMarek Vasut 	if (num == 0)
438f016f8caSMarek Vasut 		ep->desc = &ep0_out_desc;
439f016f8caSMarek Vasut 	item = ci_get_qtd(num, in);
440f016f8caSMarek Vasut 	ci_invalidate_qtd(num);
441f016f8caSMarek Vasut 
4428630c1c7SStephen Warren 	len = (item->info >> 16) & 0x7fff;
443f016f8caSMarek Vasut 	if (item->info & 0xff)
444f016f8caSMarek Vasut 		printf("EP%d/%s FAIL info=%x pg0=%x\n",
445f016f8caSMarek Vasut 		       num, in ? "in" : "out", item->info, item->page0);
446f016f8caSMarek Vasut 
4472813006fSStephen Warren 	ci_req = list_first_entry(&ep->queue, struct ci_req, queue);
4482813006fSStephen Warren 	list_del_init(&ci_req->queue);
4492813006fSStephen Warren 	ep->req_primed = false;
450f016f8caSMarek Vasut 
4512813006fSStephen Warren 	if (!list_empty(&ep->queue))
4522813006fSStephen Warren 		ci_ep_submit_next_request(ep);
4532813006fSStephen Warren 
4542813006fSStephen Warren 	ci_req->req.actual = ci_req->req.length - len;
4552813006fSStephen Warren 	ci_debounce(ci_req, in);
4562813006fSStephen Warren 
4572813006fSStephen Warren 	DBG("ept%d %s req %p, complete %x\n",
4582813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, len);
4592813006fSStephen Warren 	ci_req->req.complete(&ep->ep, &ci_req->req);
460f016f8caSMarek Vasut 	if (num == 0) {
4612813006fSStephen Warren 		ci_req->req.length = 0;
4622813006fSStephen Warren 		usb_ep_queue(&ep->ep, &ci_req->req, 0);
463f016f8caSMarek Vasut 		ep->desc = &ep0_in_desc;
464f016f8caSMarek Vasut 	}
465f016f8caSMarek Vasut }
466f016f8caSMarek Vasut 
467f016f8caSMarek Vasut #define SETUP(type, request) (((type) << 8) | (request))
468f016f8caSMarek Vasut 
469f016f8caSMarek Vasut static void handle_setup(void)
470f016f8caSMarek Vasut {
4712813006fSStephen Warren 	struct ci_ep *ci_ep = &controller.ep[0];
4722813006fSStephen Warren 	struct ci_req *ci_req;
4732813006fSStephen Warren 	struct usb_request *req;
474f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
475f016f8caSMarek Vasut 	struct ept_queue_head *head;
476f016f8caSMarek Vasut 	struct usb_ctrlrequest r;
477f016f8caSMarek Vasut 	int status = 0;
478f016f8caSMarek Vasut 	int num, in, _num, _in, i;
479f016f8caSMarek Vasut 	char *buf;
4802813006fSStephen Warren 
4812813006fSStephen Warren 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
4822813006fSStephen Warren 	req = &ci_req->req;
483f016f8caSMarek Vasut 	head = ci_get_qh(0, 0);	/* EP0 OUT */
484f016f8caSMarek Vasut 
485f016f8caSMarek Vasut 	ci_invalidate_qh(0);
486f016f8caSMarek Vasut 	memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
487fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
488fcf2ede1SStephen Warren 	writel(EPT_RX(0), &udc->epsetupstat);
489fcf2ede1SStephen Warren #else
490f016f8caSMarek Vasut 	writel(EPT_RX(0), &udc->epstat);
491fcf2ede1SStephen Warren #endif
492f016f8caSMarek Vasut 	DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
493f016f8caSMarek Vasut 	    r.bRequestType, r.bRequest, r.wIndex, r.wValue);
494f016f8caSMarek Vasut 
4952813006fSStephen Warren 	list_del_init(&ci_req->queue);
4962813006fSStephen Warren 	ci_ep->req_primed = false;
4972813006fSStephen Warren 
498f016f8caSMarek Vasut 	switch (SETUP(r.bRequestType, r.bRequest)) {
499f016f8caSMarek Vasut 	case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
500f016f8caSMarek Vasut 		_num = r.wIndex & 15;
501f016f8caSMarek Vasut 		_in = !!(r.wIndex & 0x80);
502f016f8caSMarek Vasut 
503f016f8caSMarek Vasut 		if ((r.wValue == 0) && (r.wLength == 0)) {
504f016f8caSMarek Vasut 			req->length = 0;
505f016f8caSMarek Vasut 			for (i = 0; i < NUM_ENDPOINTS; i++) {
506f016f8caSMarek Vasut 				struct ci_ep *ep = &controller.ep[i];
507f016f8caSMarek Vasut 
508f016f8caSMarek Vasut 				if (!ep->desc)
509f016f8caSMarek Vasut 					continue;
510f016f8caSMarek Vasut 				num = ep->desc->bEndpointAddress
511f016f8caSMarek Vasut 						& USB_ENDPOINT_NUMBER_MASK;
512f016f8caSMarek Vasut 				in = (ep->desc->bEndpointAddress
513f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
514f016f8caSMarek Vasut 				if ((num == _num) && (in == _in)) {
515f016f8caSMarek Vasut 					ep_enable(num, in, ep->ep.maxpacket);
516f016f8caSMarek Vasut 					usb_ep_queue(controller.gadget.ep0,
517f016f8caSMarek Vasut 							req, 0);
518f016f8caSMarek Vasut 					break;
519f016f8caSMarek Vasut 				}
520f016f8caSMarek Vasut 			}
521f016f8caSMarek Vasut 		}
522f016f8caSMarek Vasut 		return;
523f016f8caSMarek Vasut 
524f016f8caSMarek Vasut 	case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
525f016f8caSMarek Vasut 		/*
526f016f8caSMarek Vasut 		 * write address delayed (will take effect
527f016f8caSMarek Vasut 		 * after the next IN txn)
528f016f8caSMarek Vasut 		 */
529f016f8caSMarek Vasut 		writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
530f016f8caSMarek Vasut 		req->length = 0;
531f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
532f016f8caSMarek Vasut 		return;
533f016f8caSMarek Vasut 
534f016f8caSMarek Vasut 	case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
535f016f8caSMarek Vasut 		req->length = 2;
536f016f8caSMarek Vasut 		buf = (char *)req->buf;
537f016f8caSMarek Vasut 		buf[0] = 1 << USB_DEVICE_SELF_POWERED;
538f016f8caSMarek Vasut 		buf[1] = 0;
539f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
540f016f8caSMarek Vasut 		return;
541f016f8caSMarek Vasut 	}
542f016f8caSMarek Vasut 	/* pass request up to the gadget driver */
543f016f8caSMarek Vasut 	if (controller.driver)
544f016f8caSMarek Vasut 		status = controller.driver->setup(&controller.gadget, &r);
545f016f8caSMarek Vasut 	else
546f016f8caSMarek Vasut 		status = -ENODEV;
547f016f8caSMarek Vasut 
548f016f8caSMarek Vasut 	if (!status)
549f016f8caSMarek Vasut 		return;
550f016f8caSMarek Vasut 	DBG("STALL reqname %s type %x value %x, index %x\n",
551f016f8caSMarek Vasut 	    reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
552f016f8caSMarek Vasut 	writel((1<<16) | (1 << 0), &udc->epctrl[0]);
553f016f8caSMarek Vasut }
554f016f8caSMarek Vasut 
555f016f8caSMarek Vasut static void stop_activity(void)
556f016f8caSMarek Vasut {
557f016f8caSMarek Vasut 	int i, num, in;
558f016f8caSMarek Vasut 	struct ept_queue_head *head;
559f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
560f016f8caSMarek Vasut 	writel(readl(&udc->epcomp), &udc->epcomp);
561fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
562fcf2ede1SStephen Warren 	writel(readl(&udc->epsetupstat), &udc->epsetupstat);
563fcf2ede1SStephen Warren #endif
564f016f8caSMarek Vasut 	writel(readl(&udc->epstat), &udc->epstat);
565f016f8caSMarek Vasut 	writel(0xffffffff, &udc->epflush);
566f016f8caSMarek Vasut 
567f016f8caSMarek Vasut 	/* error out any pending reqs */
568f016f8caSMarek Vasut 	for (i = 0; i < NUM_ENDPOINTS; i++) {
569f016f8caSMarek Vasut 		if (i != 0)
570f016f8caSMarek Vasut 			writel(0, &udc->epctrl[i]);
571f016f8caSMarek Vasut 		if (controller.ep[i].desc) {
572f016f8caSMarek Vasut 			num = controller.ep[i].desc->bEndpointAddress
573f016f8caSMarek Vasut 				& USB_ENDPOINT_NUMBER_MASK;
574f016f8caSMarek Vasut 			in = (controller.ep[i].desc->bEndpointAddress
575f016f8caSMarek Vasut 				& USB_DIR_IN) != 0;
576f016f8caSMarek Vasut 			head = ci_get_qh(num, in);
577f016f8caSMarek Vasut 			head->info = INFO_ACTIVE;
578f016f8caSMarek Vasut 			ci_flush_qh(num);
579f016f8caSMarek Vasut 		}
580f016f8caSMarek Vasut 	}
581f016f8caSMarek Vasut }
582f016f8caSMarek Vasut 
583f016f8caSMarek Vasut void udc_irq(void)
584f016f8caSMarek Vasut {
585f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
586f016f8caSMarek Vasut 	unsigned n = readl(&udc->usbsts);
587f016f8caSMarek Vasut 	writel(n, &udc->usbsts);
588f016f8caSMarek Vasut 	int bit, i, num, in;
589f016f8caSMarek Vasut 
590f016f8caSMarek Vasut 	n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
591f016f8caSMarek Vasut 	if (n == 0)
592f016f8caSMarek Vasut 		return;
593f016f8caSMarek Vasut 
594f016f8caSMarek Vasut 	if (n & STS_URI) {
595f016f8caSMarek Vasut 		DBG("-- reset --\n");
596f016f8caSMarek Vasut 		stop_activity();
597f016f8caSMarek Vasut 	}
598f016f8caSMarek Vasut 	if (n & STS_SLI)
599f016f8caSMarek Vasut 		DBG("-- suspend --\n");
600f016f8caSMarek Vasut 
601f016f8caSMarek Vasut 	if (n & STS_PCI) {
602f016f8caSMarek Vasut 		int max = 64;
603f016f8caSMarek Vasut 		int speed = USB_SPEED_FULL;
604f016f8caSMarek Vasut 
605fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
606fcf2ede1SStephen Warren 		bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
607fcf2ede1SStephen Warren #else
608f016f8caSMarek Vasut 		bit = (readl(&udc->portsc) >> 26) & 3;
609fcf2ede1SStephen Warren #endif
610f016f8caSMarek Vasut 		DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
611f016f8caSMarek Vasut 		if (bit == 2) {
612f016f8caSMarek Vasut 			speed = USB_SPEED_HIGH;
613f016f8caSMarek Vasut 			max = 512;
614f016f8caSMarek Vasut 		}
615f016f8caSMarek Vasut 		controller.gadget.speed = speed;
616f016f8caSMarek Vasut 		for (i = 1; i < NUM_ENDPOINTS; i++) {
617f016f8caSMarek Vasut 			if (controller.ep[i].ep.maxpacket > max)
618f016f8caSMarek Vasut 				controller.ep[i].ep.maxpacket = max;
619f016f8caSMarek Vasut 		}
620f016f8caSMarek Vasut 	}
621f016f8caSMarek Vasut 
622f016f8caSMarek Vasut 	if (n & STS_UEI)
623f016f8caSMarek Vasut 		printf("<UEI %x>\n", readl(&udc->epcomp));
624f016f8caSMarek Vasut 
625f016f8caSMarek Vasut 	if ((n & STS_UI) || (n & STS_UEI)) {
626fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
627fcf2ede1SStephen Warren 		n = readl(&udc->epsetupstat);
628fcf2ede1SStephen Warren #else
629f016f8caSMarek Vasut 		n = readl(&udc->epstat);
630fcf2ede1SStephen Warren #endif
631f016f8caSMarek Vasut 		if (n & EPT_RX(0))
632f016f8caSMarek Vasut 			handle_setup();
633f016f8caSMarek Vasut 
634f016f8caSMarek Vasut 		n = readl(&udc->epcomp);
635f016f8caSMarek Vasut 		if (n != 0)
636f016f8caSMarek Vasut 			writel(n, &udc->epcomp);
637f016f8caSMarek Vasut 
638f016f8caSMarek Vasut 		for (i = 0; i < NUM_ENDPOINTS && n; i++) {
639f016f8caSMarek Vasut 			if (controller.ep[i].desc) {
640f016f8caSMarek Vasut 				num = controller.ep[i].desc->bEndpointAddress
641f016f8caSMarek Vasut 					& USB_ENDPOINT_NUMBER_MASK;
642f016f8caSMarek Vasut 				in = (controller.ep[i].desc->bEndpointAddress
643f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
644f016f8caSMarek Vasut 				bit = (in) ? EPT_TX(num) : EPT_RX(num);
645f016f8caSMarek Vasut 				if (n & bit)
646f016f8caSMarek Vasut 					handle_ep_complete(&controller.ep[i]);
647f016f8caSMarek Vasut 			}
648f016f8caSMarek Vasut 		}
649f016f8caSMarek Vasut 	}
650f016f8caSMarek Vasut }
651f016f8caSMarek Vasut 
652f016f8caSMarek Vasut int usb_gadget_handle_interrupts(void)
653f016f8caSMarek Vasut {
654f016f8caSMarek Vasut 	u32 value;
655f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
656f016f8caSMarek Vasut 
657f016f8caSMarek Vasut 	value = readl(&udc->usbsts);
658f016f8caSMarek Vasut 	if (value)
659f016f8caSMarek Vasut 		udc_irq();
660f016f8caSMarek Vasut 
661f016f8caSMarek Vasut 	return value;
662f016f8caSMarek Vasut }
663f016f8caSMarek Vasut 
664f016f8caSMarek Vasut static int ci_pullup(struct usb_gadget *gadget, int is_on)
665f016f8caSMarek Vasut {
666f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
667f016f8caSMarek Vasut 	if (is_on) {
668f016f8caSMarek Vasut 		/* RESET */
669f016f8caSMarek Vasut 		writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
670f016f8caSMarek Vasut 		udelay(200);
671f016f8caSMarek Vasut 
672f016f8caSMarek Vasut 		writel((unsigned)controller.epts, &udc->epinitaddr);
673f016f8caSMarek Vasut 
674f016f8caSMarek Vasut 		/* select DEVICE mode */
675f016f8caSMarek Vasut 		writel(USBMODE_DEVICE, &udc->usbmode);
676f016f8caSMarek Vasut 
677f016f8caSMarek Vasut 		writel(0xffffffff, &udc->epflush);
678f016f8caSMarek Vasut 
679f016f8caSMarek Vasut 		/* Turn on the USB connection by enabling the pullup resistor */
680f016f8caSMarek Vasut 		writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
681f016f8caSMarek Vasut 	} else {
682f016f8caSMarek Vasut 		stop_activity();
683f016f8caSMarek Vasut 		writel(USBCMD_FS2, &udc->usbcmd);
684f016f8caSMarek Vasut 		udelay(800);
685f016f8caSMarek Vasut 		if (controller.driver)
686f016f8caSMarek Vasut 			controller.driver->disconnect(gadget);
687f016f8caSMarek Vasut 	}
688f016f8caSMarek Vasut 
689f016f8caSMarek Vasut 	return 0;
690f016f8caSMarek Vasut }
691f016f8caSMarek Vasut 
692f016f8caSMarek Vasut void udc_disconnect(void)
693f016f8caSMarek Vasut {
694f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
695f016f8caSMarek Vasut 	/* disable pullup */
696f016f8caSMarek Vasut 	stop_activity();
697f016f8caSMarek Vasut 	writel(USBCMD_FS2, &udc->usbcmd);
698f016f8caSMarek Vasut 	udelay(800);
699f016f8caSMarek Vasut 	if (controller.driver)
700f016f8caSMarek Vasut 		controller.driver->disconnect(&controller.gadget);
701f016f8caSMarek Vasut }
702f016f8caSMarek Vasut 
703f016f8caSMarek Vasut static int ci_udc_probe(void)
704f016f8caSMarek Vasut {
705f016f8caSMarek Vasut 	struct ept_queue_head *head;
706f016f8caSMarek Vasut 	uint8_t *imem;
707f016f8caSMarek Vasut 	int i;
708f016f8caSMarek Vasut 
709f016f8caSMarek Vasut 	const int num = 2 * NUM_ENDPOINTS;
710f016f8caSMarek Vasut 
711f016f8caSMarek Vasut 	const int eplist_min_align = 4096;
712f016f8caSMarek Vasut 	const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
713f016f8caSMarek Vasut 	const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
714f016f8caSMarek Vasut 	const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
715f016f8caSMarek Vasut 
716f016f8caSMarek Vasut 	const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
717f016f8caSMarek Vasut 	const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
718f016f8caSMarek Vasut 	const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
719f016f8caSMarek Vasut 	const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
720f016f8caSMarek Vasut 
721f016f8caSMarek Vasut 	/* The QH list must be aligned to 4096 bytes. */
722f016f8caSMarek Vasut 	controller.epts = memalign(eplist_align, eplist_sz);
723f016f8caSMarek Vasut 	if (!controller.epts)
724f016f8caSMarek Vasut 		return -ENOMEM;
725f016f8caSMarek Vasut 	memset(controller.epts, 0, eplist_sz);
726f016f8caSMarek Vasut 
727f016f8caSMarek Vasut 	/*
728f016f8caSMarek Vasut 	 * Each qTD item must be 32-byte aligned, each qTD touple must be
729f016f8caSMarek Vasut 	 * cacheline aligned. There are two qTD items for each endpoint and
730f016f8caSMarek Vasut 	 * only one of them is used for the endpoint at time, so we can group
731f016f8caSMarek Vasut 	 * them together.
732f016f8caSMarek Vasut 	 */
733f016f8caSMarek Vasut 	controller.items_mem = memalign(ilist_align, ilist_sz);
734f016f8caSMarek Vasut 	if (!controller.items_mem) {
735f016f8caSMarek Vasut 		free(controller.epts);
736f016f8caSMarek Vasut 		return -ENOMEM;
737f016f8caSMarek Vasut 	}
738f016f8caSMarek Vasut 	memset(controller.items_mem, 0, ilist_sz);
739f016f8caSMarek Vasut 
740f016f8caSMarek Vasut 	for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
741f016f8caSMarek Vasut 		/*
742f016f8caSMarek Vasut 		 * Configure QH for each endpoint. The structure of the QH list
743f016f8caSMarek Vasut 		 * is such that each two subsequent fields, N and N+1 where N is
744f016f8caSMarek Vasut 		 * even, in the QH list represent QH for one endpoint. The Nth
745f016f8caSMarek Vasut 		 * entry represents OUT configuration and the N+1th entry does
746f016f8caSMarek Vasut 		 * represent IN configuration of the endpoint.
747f016f8caSMarek Vasut 		 */
748f016f8caSMarek Vasut 		head = controller.epts + i;
749f016f8caSMarek Vasut 		if (i < 2)
750f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
751f016f8caSMarek Vasut 				| CONFIG_ZLT | CONFIG_IOS;
752f016f8caSMarek Vasut 		else
753f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
754f016f8caSMarek Vasut 				| CONFIG_ZLT;
755f016f8caSMarek Vasut 		head->next = TERMINATE;
756f016f8caSMarek Vasut 		head->info = 0;
757f016f8caSMarek Vasut 
758f016f8caSMarek Vasut 		imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
759f016f8caSMarek Vasut 		if (i & 1)
760f016f8caSMarek Vasut 			imem += sizeof(struct ept_queue_item);
761f016f8caSMarek Vasut 
762f016f8caSMarek Vasut 		controller.items[i] = (struct ept_queue_item *)imem;
763f016f8caSMarek Vasut 
764f016f8caSMarek Vasut 		if (i & 1) {
765f016f8caSMarek Vasut 			ci_flush_qh(i - 1);
766f016f8caSMarek Vasut 			ci_flush_qtd(i - 1);
767f016f8caSMarek Vasut 		}
768f016f8caSMarek Vasut 	}
769f016f8caSMarek Vasut 
770f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep_list);
771f016f8caSMarek Vasut 
772f016f8caSMarek Vasut 	/* Init EP 0 */
773f016f8caSMarek Vasut 	memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
774f016f8caSMarek Vasut 	controller.ep[0].desc = &ep0_in_desc;
7752813006fSStephen Warren 	INIT_LIST_HEAD(&controller.ep[0].queue);
7762813006fSStephen Warren 	controller.ep[0].req_primed = false;
777f016f8caSMarek Vasut 	controller.gadget.ep0 = &controller.ep[0].ep;
778f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
779f016f8caSMarek Vasut 
780f016f8caSMarek Vasut 	/* Init EP 1..n */
781f016f8caSMarek Vasut 	for (i = 1; i < NUM_ENDPOINTS; i++) {
782f016f8caSMarek Vasut 		memcpy(&controller.ep[i].ep, &ci_ep_init[1],
783f016f8caSMarek Vasut 		       sizeof(*ci_ep_init));
7842813006fSStephen Warren 		INIT_LIST_HEAD(&controller.ep[i].queue);
7852813006fSStephen Warren 		controller.ep[i].req_primed = false;
786f016f8caSMarek Vasut 		list_add_tail(&controller.ep[i].ep.ep_list,
787f016f8caSMarek Vasut 			      &controller.gadget.ep_list);
788f016f8caSMarek Vasut 	}
789f016f8caSMarek Vasut 
790f016f8caSMarek Vasut 	return 0;
791f016f8caSMarek Vasut }
792f016f8caSMarek Vasut 
793f016f8caSMarek Vasut int usb_gadget_register_driver(struct usb_gadget_driver *driver)
794f016f8caSMarek Vasut {
795f016f8caSMarek Vasut 	int ret;
796f016f8caSMarek Vasut 
797f016f8caSMarek Vasut 	if (!driver)
798f016f8caSMarek Vasut 		return -EINVAL;
799f016f8caSMarek Vasut 	if (!driver->bind || !driver->setup || !driver->disconnect)
800f016f8caSMarek Vasut 		return -EINVAL;
801f016f8caSMarek Vasut 	if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
802f016f8caSMarek Vasut 		return -EINVAL;
803f016f8caSMarek Vasut 
804f016f8caSMarek Vasut 	ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
805f016f8caSMarek Vasut 	if (ret)
806f016f8caSMarek Vasut 		return ret;
807f016f8caSMarek Vasut 
808f016f8caSMarek Vasut 	ret = ci_udc_probe();
8090c51dc6dSStephen Warren #if defined(CONFIG_USB_EHCI_MX6) || defined(CONFIG_USB_EHCI_MXS)
8100c51dc6dSStephen Warren 	/*
8110c51dc6dSStephen Warren 	 * FIXME: usb_lowlevel_init()->ehci_hcd_init() should be doing all
8120c51dc6dSStephen Warren 	 * HW-specific initialization, e.g. ULPI-vs-UTMI PHY selection
8130c51dc6dSStephen Warren 	 */
814f016f8caSMarek Vasut 	if (!ret) {
8150c51dc6dSStephen Warren 		struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
816f016f8caSMarek Vasut 
817f016f8caSMarek Vasut 		/* select ULPI phy */
818f016f8caSMarek Vasut 		writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
819f016f8caSMarek Vasut 	}
8200c51dc6dSStephen Warren #endif
821f016f8caSMarek Vasut 
822f016f8caSMarek Vasut 	ret = driver->bind(&controller.gadget);
823f016f8caSMarek Vasut 	if (ret) {
824f016f8caSMarek Vasut 		DBG("driver->bind() returned %d\n", ret);
825f016f8caSMarek Vasut 		return ret;
826f016f8caSMarek Vasut 	}
827f016f8caSMarek Vasut 	controller.driver = driver;
828f016f8caSMarek Vasut 
829f016f8caSMarek Vasut 	return 0;
830f016f8caSMarek Vasut }
831f016f8caSMarek Vasut 
832f016f8caSMarek Vasut int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
833f016f8caSMarek Vasut {
834f016f8caSMarek Vasut 	return 0;
835f016f8caSMarek Vasut }
836