xref: /rk3399_rockchip-uboot/drivers/usb/gadget/ci_udc.c (revision 2813006fecdab740c3745b2dcbb06777cdd51dff)
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 {
208*2813006fSStephen Warren 	struct ci_req *ci_req;
209*2813006fSStephen Warren 
210*2813006fSStephen Warren 	ci_req = memalign(ARCH_DMA_MINALIGN, sizeof(*ci_req));
211*2813006fSStephen Warren 	if (!ci_req)
212*2813006fSStephen Warren 		return NULL;
213*2813006fSStephen Warren 
214*2813006fSStephen Warren 	INIT_LIST_HEAD(&ci_req->queue);
215*2813006fSStephen Warren 	ci_req->b_buf = 0;
216*2813006fSStephen Warren 
217*2813006fSStephen Warren 	return &ci_req->req;
218f016f8caSMarek Vasut }
219f016f8caSMarek Vasut 
220*2813006fSStephen Warren static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
221f016f8caSMarek Vasut {
222*2813006fSStephen Warren 	struct ci_req *ci_req;
223*2813006fSStephen Warren 
224*2813006fSStephen Warren 	ci_req = container_of(req, struct ci_req, req);
225*2813006fSStephen Warren 	if (ci_req->b_buf)
226*2813006fSStephen Warren 		free(ci_req->b_buf);
227*2813006fSStephen 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 
283*2813006fSStephen Warren static int ci_bounce(struct ci_req *ci_req, int in)
284f016f8caSMarek Vasut {
285*2813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
286*2813006fSStephen Warren 	uint32_t addr = (uint32_t)req->buf;
287*2813006fSStephen Warren 	uint32_t hwaddr;
288*2813006fSStephen 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. */
295*2813006fSStephen Warren 	if (req->length & (ARCH_DMA_MINALIGN - 1))
296f016f8caSMarek Vasut 		goto align;
297f016f8caSMarek Vasut 
298f016f8caSMarek Vasut 	/* The buffer is well aligned, only flush cache. */
299*2813006fSStephen Warren 	ci_req->hw_len = req->length;
300*2813006fSStephen Warren 	ci_req->hw_buf = req->buf;
301f016f8caSMarek Vasut 	goto flush;
302f016f8caSMarek Vasut 
303f016f8caSMarek Vasut align:
304*2813006fSStephen Warren 	if (ci_req->b_buf && req->length > ci_req->b_len) {
305*2813006fSStephen Warren 		free(ci_req->b_buf);
306*2813006fSStephen Warren 		ci_req->b_buf = 0;
307*2813006fSStephen Warren 	}
308*2813006fSStephen Warren 	if (!ci_req->b_buf) {
309*2813006fSStephen Warren 		ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
310*2813006fSStephen Warren 		ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
311*2813006fSStephen Warren 		if (!ci_req->b_buf)
312f016f8caSMarek Vasut 			return -ENOMEM;
313f016f8caSMarek Vasut 	}
314*2813006fSStephen Warren 	ci_req->hw_len = ci_req->b_len;
315*2813006fSStephen Warren 	ci_req->hw_buf = ci_req->b_buf;
316*2813006fSStephen Warren 
317f016f8caSMarek Vasut 	if (in)
318*2813006fSStephen Warren 		memcpy(ci_req->hw_buf, req->buf, req->length);
319f016f8caSMarek Vasut 
320f016f8caSMarek Vasut flush:
321*2813006fSStephen Warren 	hwaddr = (uint32_t)ci_req->hw_buf;
322*2813006fSStephen Warren 	aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
323*2813006fSStephen Warren 	flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
324f016f8caSMarek Vasut 
325f016f8caSMarek Vasut 	return 0;
326f016f8caSMarek Vasut }
327f016f8caSMarek Vasut 
328*2813006fSStephen Warren static void ci_debounce(struct ci_req *ci_req, int in)
329f016f8caSMarek Vasut {
330*2813006fSStephen Warren 	struct usb_request *req = &ci_req->req;
331*2813006fSStephen Warren 	uint32_t addr = (uint32_t)req->buf;
332*2813006fSStephen Warren 	uint32_t hwaddr = (uint32_t)ci_req->hw_buf;
333*2813006fSStephen Warren 	uint32_t aligned_used_len;
334f016f8caSMarek Vasut 
335*2813006fSStephen Warren 	if (in)
336*2813006fSStephen Warren 		return;
337f016f8caSMarek Vasut 
338*2813006fSStephen Warren 	aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
339*2813006fSStephen Warren 	invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
340*2813006fSStephen Warren 
341*2813006fSStephen Warren 	if (addr == hwaddr)
342f016f8caSMarek Vasut 		return; /* not a bounce */
343f016f8caSMarek Vasut 
344*2813006fSStephen Warren 	memcpy(req->buf, ci_req->hw_buf, req->actual);
345f016f8caSMarek Vasut }
346f016f8caSMarek Vasut 
347*2813006fSStephen 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;
352*2813006fSStephen Warren 	int bit, num, len, in;
353*2813006fSStephen Warren 	struct ci_req *ci_req;
354*2813006fSStephen Warren 
355*2813006fSStephen Warren 	ci_ep->req_primed = true;
356*2813006fSStephen 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 
362*2813006fSStephen Warren 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
363*2813006fSStephen Warren 	len = ci_req->req.length;
364f016f8caSMarek Vasut 
365f016f8caSMarek Vasut 	item->next = TERMINATE;
366f016f8caSMarek Vasut 	item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
367*2813006fSStephen Warren 	item->page0 = (uint32_t)ci_req->hw_buf;
368*2813006fSStephen Warren 	item->page1 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x1000;
369*2813006fSStephen Warren 	item->page2 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x2000;
370*2813006fSStephen Warren 	item->page3 = ((uint32_t)ci_req->hw_buf & 0xfffff000) + 0x3000;
371*2813006fSStephen 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 
377*2813006fSStephen Warren 	DBG("ept%d %s queue len %x, req %p, buffer %p\n",
378*2813006fSStephen 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);
387*2813006fSStephen Warren }
388*2813006fSStephen Warren 
389*2813006fSStephen Warren static int ci_ep_queue(struct usb_ep *ep,
390*2813006fSStephen Warren 		struct usb_request *req, gfp_t gfp_flags)
391*2813006fSStephen Warren {
392*2813006fSStephen Warren 	struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
393*2813006fSStephen Warren 	struct ci_req *ci_req = container_of(req, struct ci_req, req);
394*2813006fSStephen Warren 	int in, ret;
395*2813006fSStephen Warren 	int __maybe_unused num;
396*2813006fSStephen Warren 
397*2813006fSStephen Warren 	num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
398*2813006fSStephen Warren 	in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
399*2813006fSStephen Warren 
400*2813006fSStephen Warren 	ret = ci_bounce(ci_req, in);
401*2813006fSStephen Warren 	if (ret)
402*2813006fSStephen Warren 		return ret;
403*2813006fSStephen Warren 
404*2813006fSStephen Warren 	DBG("ept%d %s pre-queue req %p, buffer %p\n",
405*2813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, ci_req->hw_buf);
406*2813006fSStephen Warren 	list_add_tail(&ci_req->queue, &ci_ep->queue);
407*2813006fSStephen Warren 
408*2813006fSStephen Warren 	if (!ci_ep->req_primed)
409*2813006fSStephen Warren 		ci_ep_submit_next_request(ci_ep);
410f016f8caSMarek Vasut 
411f016f8caSMarek Vasut 	return 0;
412f016f8caSMarek Vasut }
413f016f8caSMarek Vasut 
414f016f8caSMarek Vasut static void handle_ep_complete(struct ci_ep *ep)
415f016f8caSMarek Vasut {
416f016f8caSMarek Vasut 	struct ept_queue_item *item;
417f016f8caSMarek Vasut 	int num, in, len;
418*2813006fSStephen Warren 	struct ci_req *ci_req;
419*2813006fSStephen Warren 
420f016f8caSMarek Vasut 	num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
421f016f8caSMarek Vasut 	in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
422f016f8caSMarek Vasut 	if (num == 0)
423f016f8caSMarek Vasut 		ep->desc = &ep0_out_desc;
424f016f8caSMarek Vasut 	item = ci_get_qtd(num, in);
425f016f8caSMarek Vasut 	ci_invalidate_qtd(num);
426f016f8caSMarek Vasut 
427f016f8caSMarek Vasut 	if (item->info & 0xff)
428f016f8caSMarek Vasut 		printf("EP%d/%s FAIL info=%x pg0=%x\n",
429f016f8caSMarek Vasut 		       num, in ? "in" : "out", item->info, item->page0);
430f016f8caSMarek Vasut 
431*2813006fSStephen Warren 	ci_req = list_first_entry(&ep->queue, struct ci_req, queue);
432*2813006fSStephen Warren 	list_del_init(&ci_req->queue);
433*2813006fSStephen Warren 	ep->req_primed = false;
434f016f8caSMarek Vasut 
435*2813006fSStephen Warren 	if (!list_empty(&ep->queue))
436*2813006fSStephen Warren 		ci_ep_submit_next_request(ep);
437*2813006fSStephen Warren 
438*2813006fSStephen Warren 	len = (item->info >> 16) & 0x7fff;
439*2813006fSStephen Warren 	ci_req->req.actual = ci_req->req.length - len;
440*2813006fSStephen Warren 	ci_debounce(ci_req, in);
441*2813006fSStephen Warren 
442*2813006fSStephen Warren 	DBG("ept%d %s req %p, complete %x\n",
443*2813006fSStephen Warren 	    num, in ? "in" : "out", ci_req, len);
444*2813006fSStephen Warren 	ci_req->req.complete(&ep->ep, &ci_req->req);
445f016f8caSMarek Vasut 	if (num == 0) {
446*2813006fSStephen Warren 		ci_req->req.length = 0;
447*2813006fSStephen Warren 		usb_ep_queue(&ep->ep, &ci_req->req, 0);
448f016f8caSMarek Vasut 		ep->desc = &ep0_in_desc;
449f016f8caSMarek Vasut 	}
450f016f8caSMarek Vasut }
451f016f8caSMarek Vasut 
452f016f8caSMarek Vasut #define SETUP(type, request) (((type) << 8) | (request))
453f016f8caSMarek Vasut 
454f016f8caSMarek Vasut static void handle_setup(void)
455f016f8caSMarek Vasut {
456*2813006fSStephen Warren 	struct ci_ep *ci_ep = &controller.ep[0];
457*2813006fSStephen Warren 	struct ci_req *ci_req;
458*2813006fSStephen Warren 	struct usb_request *req;
459f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
460f016f8caSMarek Vasut 	struct ept_queue_head *head;
461f016f8caSMarek Vasut 	struct usb_ctrlrequest r;
462f016f8caSMarek Vasut 	int status = 0;
463f016f8caSMarek Vasut 	int num, in, _num, _in, i;
464f016f8caSMarek Vasut 	char *buf;
465*2813006fSStephen Warren 
466*2813006fSStephen Warren 	ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
467*2813006fSStephen Warren 	req = &ci_req->req;
468f016f8caSMarek Vasut 	head = ci_get_qh(0, 0);	/* EP0 OUT */
469f016f8caSMarek Vasut 
470f016f8caSMarek Vasut 	ci_invalidate_qh(0);
471f016f8caSMarek Vasut 	memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
472fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
473fcf2ede1SStephen Warren 	writel(EPT_RX(0), &udc->epsetupstat);
474fcf2ede1SStephen Warren #else
475f016f8caSMarek Vasut 	writel(EPT_RX(0), &udc->epstat);
476fcf2ede1SStephen Warren #endif
477f016f8caSMarek Vasut 	DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
478f016f8caSMarek Vasut 	    r.bRequestType, r.bRequest, r.wIndex, r.wValue);
479f016f8caSMarek Vasut 
480*2813006fSStephen Warren 	list_del_init(&ci_req->queue);
481*2813006fSStephen Warren 	ci_ep->req_primed = false;
482*2813006fSStephen Warren 
483f016f8caSMarek Vasut 	switch (SETUP(r.bRequestType, r.bRequest)) {
484f016f8caSMarek Vasut 	case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
485f016f8caSMarek Vasut 		_num = r.wIndex & 15;
486f016f8caSMarek Vasut 		_in = !!(r.wIndex & 0x80);
487f016f8caSMarek Vasut 
488f016f8caSMarek Vasut 		if ((r.wValue == 0) && (r.wLength == 0)) {
489f016f8caSMarek Vasut 			req->length = 0;
490f016f8caSMarek Vasut 			for (i = 0; i < NUM_ENDPOINTS; i++) {
491f016f8caSMarek Vasut 				struct ci_ep *ep = &controller.ep[i];
492f016f8caSMarek Vasut 
493f016f8caSMarek Vasut 				if (!ep->desc)
494f016f8caSMarek Vasut 					continue;
495f016f8caSMarek Vasut 				num = ep->desc->bEndpointAddress
496f016f8caSMarek Vasut 						& USB_ENDPOINT_NUMBER_MASK;
497f016f8caSMarek Vasut 				in = (ep->desc->bEndpointAddress
498f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
499f016f8caSMarek Vasut 				if ((num == _num) && (in == _in)) {
500f016f8caSMarek Vasut 					ep_enable(num, in, ep->ep.maxpacket);
501f016f8caSMarek Vasut 					usb_ep_queue(controller.gadget.ep0,
502f016f8caSMarek Vasut 							req, 0);
503f016f8caSMarek Vasut 					break;
504f016f8caSMarek Vasut 				}
505f016f8caSMarek Vasut 			}
506f016f8caSMarek Vasut 		}
507f016f8caSMarek Vasut 		return;
508f016f8caSMarek Vasut 
509f016f8caSMarek Vasut 	case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
510f016f8caSMarek Vasut 		/*
511f016f8caSMarek Vasut 		 * write address delayed (will take effect
512f016f8caSMarek Vasut 		 * after the next IN txn)
513f016f8caSMarek Vasut 		 */
514f016f8caSMarek Vasut 		writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
515f016f8caSMarek Vasut 		req->length = 0;
516f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
517f016f8caSMarek Vasut 		return;
518f016f8caSMarek Vasut 
519f016f8caSMarek Vasut 	case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
520f016f8caSMarek Vasut 		req->length = 2;
521f016f8caSMarek Vasut 		buf = (char *)req->buf;
522f016f8caSMarek Vasut 		buf[0] = 1 << USB_DEVICE_SELF_POWERED;
523f016f8caSMarek Vasut 		buf[1] = 0;
524f016f8caSMarek Vasut 		usb_ep_queue(controller.gadget.ep0, req, 0);
525f016f8caSMarek Vasut 		return;
526f016f8caSMarek Vasut 	}
527f016f8caSMarek Vasut 	/* pass request up to the gadget driver */
528f016f8caSMarek Vasut 	if (controller.driver)
529f016f8caSMarek Vasut 		status = controller.driver->setup(&controller.gadget, &r);
530f016f8caSMarek Vasut 	else
531f016f8caSMarek Vasut 		status = -ENODEV;
532f016f8caSMarek Vasut 
533f016f8caSMarek Vasut 	if (!status)
534f016f8caSMarek Vasut 		return;
535f016f8caSMarek Vasut 	DBG("STALL reqname %s type %x value %x, index %x\n",
536f016f8caSMarek Vasut 	    reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
537f016f8caSMarek Vasut 	writel((1<<16) | (1 << 0), &udc->epctrl[0]);
538f016f8caSMarek Vasut }
539f016f8caSMarek Vasut 
540f016f8caSMarek Vasut static void stop_activity(void)
541f016f8caSMarek Vasut {
542f016f8caSMarek Vasut 	int i, num, in;
543f016f8caSMarek Vasut 	struct ept_queue_head *head;
544f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
545f016f8caSMarek Vasut 	writel(readl(&udc->epcomp), &udc->epcomp);
546fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
547fcf2ede1SStephen Warren 	writel(readl(&udc->epsetupstat), &udc->epsetupstat);
548fcf2ede1SStephen Warren #endif
549f016f8caSMarek Vasut 	writel(readl(&udc->epstat), &udc->epstat);
550f016f8caSMarek Vasut 	writel(0xffffffff, &udc->epflush);
551f016f8caSMarek Vasut 
552f016f8caSMarek Vasut 	/* error out any pending reqs */
553f016f8caSMarek Vasut 	for (i = 0; i < NUM_ENDPOINTS; i++) {
554f016f8caSMarek Vasut 		if (i != 0)
555f016f8caSMarek Vasut 			writel(0, &udc->epctrl[i]);
556f016f8caSMarek Vasut 		if (controller.ep[i].desc) {
557f016f8caSMarek Vasut 			num = controller.ep[i].desc->bEndpointAddress
558f016f8caSMarek Vasut 				& USB_ENDPOINT_NUMBER_MASK;
559f016f8caSMarek Vasut 			in = (controller.ep[i].desc->bEndpointAddress
560f016f8caSMarek Vasut 				& USB_DIR_IN) != 0;
561f016f8caSMarek Vasut 			head = ci_get_qh(num, in);
562f016f8caSMarek Vasut 			head->info = INFO_ACTIVE;
563f016f8caSMarek Vasut 			ci_flush_qh(num);
564f016f8caSMarek Vasut 		}
565f016f8caSMarek Vasut 	}
566f016f8caSMarek Vasut }
567f016f8caSMarek Vasut 
568f016f8caSMarek Vasut void udc_irq(void)
569f016f8caSMarek Vasut {
570f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
571f016f8caSMarek Vasut 	unsigned n = readl(&udc->usbsts);
572f016f8caSMarek Vasut 	writel(n, &udc->usbsts);
573f016f8caSMarek Vasut 	int bit, i, num, in;
574f016f8caSMarek Vasut 
575f016f8caSMarek Vasut 	n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
576f016f8caSMarek Vasut 	if (n == 0)
577f016f8caSMarek Vasut 		return;
578f016f8caSMarek Vasut 
579f016f8caSMarek Vasut 	if (n & STS_URI) {
580f016f8caSMarek Vasut 		DBG("-- reset --\n");
581f016f8caSMarek Vasut 		stop_activity();
582f016f8caSMarek Vasut 	}
583f016f8caSMarek Vasut 	if (n & STS_SLI)
584f016f8caSMarek Vasut 		DBG("-- suspend --\n");
585f016f8caSMarek Vasut 
586f016f8caSMarek Vasut 	if (n & STS_PCI) {
587f016f8caSMarek Vasut 		int max = 64;
588f016f8caSMarek Vasut 		int speed = USB_SPEED_FULL;
589f016f8caSMarek Vasut 
590fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
591fcf2ede1SStephen Warren 		bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
592fcf2ede1SStephen Warren #else
593f016f8caSMarek Vasut 		bit = (readl(&udc->portsc) >> 26) & 3;
594fcf2ede1SStephen Warren #endif
595f016f8caSMarek Vasut 		DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
596f016f8caSMarek Vasut 		if (bit == 2) {
597f016f8caSMarek Vasut 			speed = USB_SPEED_HIGH;
598f016f8caSMarek Vasut 			max = 512;
599f016f8caSMarek Vasut 		}
600f016f8caSMarek Vasut 		controller.gadget.speed = speed;
601f016f8caSMarek Vasut 		for (i = 1; i < NUM_ENDPOINTS; i++) {
602f016f8caSMarek Vasut 			if (controller.ep[i].ep.maxpacket > max)
603f016f8caSMarek Vasut 				controller.ep[i].ep.maxpacket = max;
604f016f8caSMarek Vasut 		}
605f016f8caSMarek Vasut 	}
606f016f8caSMarek Vasut 
607f016f8caSMarek Vasut 	if (n & STS_UEI)
608f016f8caSMarek Vasut 		printf("<UEI %x>\n", readl(&udc->epcomp));
609f016f8caSMarek Vasut 
610f016f8caSMarek Vasut 	if ((n & STS_UI) || (n & STS_UEI)) {
611fcf2ede1SStephen Warren #ifdef CONFIG_CI_UDC_HAS_HOSTPC
612fcf2ede1SStephen Warren 		n = readl(&udc->epsetupstat);
613fcf2ede1SStephen Warren #else
614f016f8caSMarek Vasut 		n = readl(&udc->epstat);
615fcf2ede1SStephen Warren #endif
616f016f8caSMarek Vasut 		if (n & EPT_RX(0))
617f016f8caSMarek Vasut 			handle_setup();
618f016f8caSMarek Vasut 
619f016f8caSMarek Vasut 		n = readl(&udc->epcomp);
620f016f8caSMarek Vasut 		if (n != 0)
621f016f8caSMarek Vasut 			writel(n, &udc->epcomp);
622f016f8caSMarek Vasut 
623f016f8caSMarek Vasut 		for (i = 0; i < NUM_ENDPOINTS && n; i++) {
624f016f8caSMarek Vasut 			if (controller.ep[i].desc) {
625f016f8caSMarek Vasut 				num = controller.ep[i].desc->bEndpointAddress
626f016f8caSMarek Vasut 					& USB_ENDPOINT_NUMBER_MASK;
627f016f8caSMarek Vasut 				in = (controller.ep[i].desc->bEndpointAddress
628f016f8caSMarek Vasut 						& USB_DIR_IN) != 0;
629f016f8caSMarek Vasut 				bit = (in) ? EPT_TX(num) : EPT_RX(num);
630f016f8caSMarek Vasut 				if (n & bit)
631f016f8caSMarek Vasut 					handle_ep_complete(&controller.ep[i]);
632f016f8caSMarek Vasut 			}
633f016f8caSMarek Vasut 		}
634f016f8caSMarek Vasut 	}
635f016f8caSMarek Vasut }
636f016f8caSMarek Vasut 
637f016f8caSMarek Vasut int usb_gadget_handle_interrupts(void)
638f016f8caSMarek Vasut {
639f016f8caSMarek Vasut 	u32 value;
640f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
641f016f8caSMarek Vasut 
642f016f8caSMarek Vasut 	value = readl(&udc->usbsts);
643f016f8caSMarek Vasut 	if (value)
644f016f8caSMarek Vasut 		udc_irq();
645f016f8caSMarek Vasut 
646f016f8caSMarek Vasut 	return value;
647f016f8caSMarek Vasut }
648f016f8caSMarek Vasut 
649f016f8caSMarek Vasut static int ci_pullup(struct usb_gadget *gadget, int is_on)
650f016f8caSMarek Vasut {
651f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
652f016f8caSMarek Vasut 	if (is_on) {
653f016f8caSMarek Vasut 		/* RESET */
654f016f8caSMarek Vasut 		writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
655f016f8caSMarek Vasut 		udelay(200);
656f016f8caSMarek Vasut 
657f016f8caSMarek Vasut 		writel((unsigned)controller.epts, &udc->epinitaddr);
658f016f8caSMarek Vasut 
659f016f8caSMarek Vasut 		/* select DEVICE mode */
660f016f8caSMarek Vasut 		writel(USBMODE_DEVICE, &udc->usbmode);
661f016f8caSMarek Vasut 
662f016f8caSMarek Vasut 		writel(0xffffffff, &udc->epflush);
663f016f8caSMarek Vasut 
664f016f8caSMarek Vasut 		/* Turn on the USB connection by enabling the pullup resistor */
665f016f8caSMarek Vasut 		writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
666f016f8caSMarek Vasut 	} else {
667f016f8caSMarek Vasut 		stop_activity();
668f016f8caSMarek Vasut 		writel(USBCMD_FS2, &udc->usbcmd);
669f016f8caSMarek Vasut 		udelay(800);
670f016f8caSMarek Vasut 		if (controller.driver)
671f016f8caSMarek Vasut 			controller.driver->disconnect(gadget);
672f016f8caSMarek Vasut 	}
673f016f8caSMarek Vasut 
674f016f8caSMarek Vasut 	return 0;
675f016f8caSMarek Vasut }
676f016f8caSMarek Vasut 
677f016f8caSMarek Vasut void udc_disconnect(void)
678f016f8caSMarek Vasut {
679f016f8caSMarek Vasut 	struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
680f016f8caSMarek Vasut 	/* disable pullup */
681f016f8caSMarek Vasut 	stop_activity();
682f016f8caSMarek Vasut 	writel(USBCMD_FS2, &udc->usbcmd);
683f016f8caSMarek Vasut 	udelay(800);
684f016f8caSMarek Vasut 	if (controller.driver)
685f016f8caSMarek Vasut 		controller.driver->disconnect(&controller.gadget);
686f016f8caSMarek Vasut }
687f016f8caSMarek Vasut 
688f016f8caSMarek Vasut static int ci_udc_probe(void)
689f016f8caSMarek Vasut {
690f016f8caSMarek Vasut 	struct ept_queue_head *head;
691f016f8caSMarek Vasut 	uint8_t *imem;
692f016f8caSMarek Vasut 	int i;
693f016f8caSMarek Vasut 
694f016f8caSMarek Vasut 	const int num = 2 * NUM_ENDPOINTS;
695f016f8caSMarek Vasut 
696f016f8caSMarek Vasut 	const int eplist_min_align = 4096;
697f016f8caSMarek Vasut 	const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
698f016f8caSMarek Vasut 	const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
699f016f8caSMarek Vasut 	const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
700f016f8caSMarek Vasut 
701f016f8caSMarek Vasut 	const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
702f016f8caSMarek Vasut 	const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
703f016f8caSMarek Vasut 	const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
704f016f8caSMarek Vasut 	const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
705f016f8caSMarek Vasut 
706f016f8caSMarek Vasut 	/* The QH list must be aligned to 4096 bytes. */
707f016f8caSMarek Vasut 	controller.epts = memalign(eplist_align, eplist_sz);
708f016f8caSMarek Vasut 	if (!controller.epts)
709f016f8caSMarek Vasut 		return -ENOMEM;
710f016f8caSMarek Vasut 	memset(controller.epts, 0, eplist_sz);
711f016f8caSMarek Vasut 
712f016f8caSMarek Vasut 	/*
713f016f8caSMarek Vasut 	 * Each qTD item must be 32-byte aligned, each qTD touple must be
714f016f8caSMarek Vasut 	 * cacheline aligned. There are two qTD items for each endpoint and
715f016f8caSMarek Vasut 	 * only one of them is used for the endpoint at time, so we can group
716f016f8caSMarek Vasut 	 * them together.
717f016f8caSMarek Vasut 	 */
718f016f8caSMarek Vasut 	controller.items_mem = memalign(ilist_align, ilist_sz);
719f016f8caSMarek Vasut 	if (!controller.items_mem) {
720f016f8caSMarek Vasut 		free(controller.epts);
721f016f8caSMarek Vasut 		return -ENOMEM;
722f016f8caSMarek Vasut 	}
723f016f8caSMarek Vasut 	memset(controller.items_mem, 0, ilist_sz);
724f016f8caSMarek Vasut 
725f016f8caSMarek Vasut 	for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
726f016f8caSMarek Vasut 		/*
727f016f8caSMarek Vasut 		 * Configure QH for each endpoint. The structure of the QH list
728f016f8caSMarek Vasut 		 * is such that each two subsequent fields, N and N+1 where N is
729f016f8caSMarek Vasut 		 * even, in the QH list represent QH for one endpoint. The Nth
730f016f8caSMarek Vasut 		 * entry represents OUT configuration and the N+1th entry does
731f016f8caSMarek Vasut 		 * represent IN configuration of the endpoint.
732f016f8caSMarek Vasut 		 */
733f016f8caSMarek Vasut 		head = controller.epts + i;
734f016f8caSMarek Vasut 		if (i < 2)
735f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
736f016f8caSMarek Vasut 				| CONFIG_ZLT | CONFIG_IOS;
737f016f8caSMarek Vasut 		else
738f016f8caSMarek Vasut 			head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
739f016f8caSMarek Vasut 				| CONFIG_ZLT;
740f016f8caSMarek Vasut 		head->next = TERMINATE;
741f016f8caSMarek Vasut 		head->info = 0;
742f016f8caSMarek Vasut 
743f016f8caSMarek Vasut 		imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
744f016f8caSMarek Vasut 		if (i & 1)
745f016f8caSMarek Vasut 			imem += sizeof(struct ept_queue_item);
746f016f8caSMarek Vasut 
747f016f8caSMarek Vasut 		controller.items[i] = (struct ept_queue_item *)imem;
748f016f8caSMarek Vasut 
749f016f8caSMarek Vasut 		if (i & 1) {
750f016f8caSMarek Vasut 			ci_flush_qh(i - 1);
751f016f8caSMarek Vasut 			ci_flush_qtd(i - 1);
752f016f8caSMarek Vasut 		}
753f016f8caSMarek Vasut 	}
754f016f8caSMarek Vasut 
755f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep_list);
756f016f8caSMarek Vasut 
757f016f8caSMarek Vasut 	/* Init EP 0 */
758f016f8caSMarek Vasut 	memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
759f016f8caSMarek Vasut 	controller.ep[0].desc = &ep0_in_desc;
760*2813006fSStephen Warren 	INIT_LIST_HEAD(&controller.ep[0].queue);
761*2813006fSStephen Warren 	controller.ep[0].req_primed = false;
762f016f8caSMarek Vasut 	controller.gadget.ep0 = &controller.ep[0].ep;
763f016f8caSMarek Vasut 	INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
764f016f8caSMarek Vasut 
765f016f8caSMarek Vasut 	/* Init EP 1..n */
766f016f8caSMarek Vasut 	for (i = 1; i < NUM_ENDPOINTS; i++) {
767f016f8caSMarek Vasut 		memcpy(&controller.ep[i].ep, &ci_ep_init[1],
768f016f8caSMarek Vasut 		       sizeof(*ci_ep_init));
769*2813006fSStephen Warren 		INIT_LIST_HEAD(&controller.ep[i].queue);
770*2813006fSStephen Warren 		controller.ep[i].req_primed = false;
771f016f8caSMarek Vasut 		list_add_tail(&controller.ep[i].ep.ep_list,
772f016f8caSMarek Vasut 			      &controller.gadget.ep_list);
773f016f8caSMarek Vasut 	}
774f016f8caSMarek Vasut 
775f016f8caSMarek Vasut 	return 0;
776f016f8caSMarek Vasut }
777f016f8caSMarek Vasut 
778f016f8caSMarek Vasut int usb_gadget_register_driver(struct usb_gadget_driver *driver)
779f016f8caSMarek Vasut {
780f016f8caSMarek Vasut 	int ret;
781f016f8caSMarek Vasut 
782f016f8caSMarek Vasut 	if (!driver)
783f016f8caSMarek Vasut 		return -EINVAL;
784f016f8caSMarek Vasut 	if (!driver->bind || !driver->setup || !driver->disconnect)
785f016f8caSMarek Vasut 		return -EINVAL;
786f016f8caSMarek Vasut 	if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
787f016f8caSMarek Vasut 		return -EINVAL;
788f016f8caSMarek Vasut 
789f016f8caSMarek Vasut 	ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
790f016f8caSMarek Vasut 	if (ret)
791f016f8caSMarek Vasut 		return ret;
792f016f8caSMarek Vasut 
793f016f8caSMarek Vasut 	ret = ci_udc_probe();
7940c51dc6dSStephen Warren #if defined(CONFIG_USB_EHCI_MX6) || defined(CONFIG_USB_EHCI_MXS)
7950c51dc6dSStephen Warren 	/*
7960c51dc6dSStephen Warren 	 * FIXME: usb_lowlevel_init()->ehci_hcd_init() should be doing all
7970c51dc6dSStephen Warren 	 * HW-specific initialization, e.g. ULPI-vs-UTMI PHY selection
7980c51dc6dSStephen Warren 	 */
799f016f8caSMarek Vasut 	if (!ret) {
8000c51dc6dSStephen Warren 		struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
801f016f8caSMarek Vasut 
802f016f8caSMarek Vasut 		/* select ULPI phy */
803f016f8caSMarek Vasut 		writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
804f016f8caSMarek Vasut 	}
8050c51dc6dSStephen Warren #endif
806f016f8caSMarek Vasut 
807f016f8caSMarek Vasut 	ret = driver->bind(&controller.gadget);
808f016f8caSMarek Vasut 	if (ret) {
809f016f8caSMarek Vasut 		DBG("driver->bind() returned %d\n", ret);
810f016f8caSMarek Vasut 		return ret;
811f016f8caSMarek Vasut 	}
812f016f8caSMarek Vasut 	controller.driver = driver;
813f016f8caSMarek Vasut 
814f016f8caSMarek Vasut 	return 0;
815f016f8caSMarek Vasut }
816f016f8caSMarek Vasut 
817f016f8caSMarek Vasut int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
818f016f8caSMarek Vasut {
819f016f8caSMarek Vasut 	return 0;
820f016f8caSMarek Vasut }
821