1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2011, Marvell Semiconductor Inc.
3*4882a593Smuzhiyun * Lei Wen <leiwen@marvell.com>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Back ported to the 8xx platform (from the 8260 platform) by
8*4882a593Smuzhiyun * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <command.h>
13*4882a593Smuzhiyun #include <config.h>
14*4882a593Smuzhiyun #include <net.h>
15*4882a593Smuzhiyun #include <malloc.h>
16*4882a593Smuzhiyun #include <asm/byteorder.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <asm/io.h>
19*4882a593Smuzhiyun #include <asm/unaligned.h>
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/usb/ch9.h>
22*4882a593Smuzhiyun #include <linux/usb/gadget.h>
23*4882a593Smuzhiyun #include <usb/ci_udc.h>
24*4882a593Smuzhiyun #include "../host/ehci.h"
25*4882a593Smuzhiyun #include "ci_udc.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Check if the system has too long cachelines. If the cachelines are
29*4882a593Smuzhiyun * longer then 128b, the driver will not be able flush/invalidate data
30*4882a593Smuzhiyun * cache over separate QH entries. We use 128b because one QH entry is
31*4882a593Smuzhiyun * 64b long and there are always two QH list entries for each endpoint.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #if ARCH_DMA_MINALIGN > 128
34*4882a593Smuzhiyun #error This driver can not work on systems with caches longer than 128b
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Every QTD must be individually aligned, since we can program any
39*4882a593Smuzhiyun * QTD's address into HW. Cache flushing requires ARCH_DMA_MINALIGN,
40*4882a593Smuzhiyun * and the USB HW requires 32-byte alignment. Align to both:
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun #define ILIST_ALIGN roundup(ARCH_DMA_MINALIGN, 32)
43*4882a593Smuzhiyun /* Each QTD is this size */
44*4882a593Smuzhiyun #define ILIST_ENT_RAW_SZ sizeof(struct ept_queue_item)
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Align the size of the QTD too, so we can add this value to each
47*4882a593Smuzhiyun * QTD's address to get another aligned address.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun #define ILIST_ENT_SZ roundup(ILIST_ENT_RAW_SZ, ILIST_ALIGN)
50*4882a593Smuzhiyun /* For each endpoint, we need 2 QTDs, one for each of IN and OUT */
51*4882a593Smuzhiyun #define ILIST_SZ (NUM_ENDPOINTS * 2 * ILIST_ENT_SZ)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define EP_MAX_LENGTH_TRANSFER 0x4000
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #ifndef DEBUG
56*4882a593Smuzhiyun #define DBG(x...) do {} while (0)
57*4882a593Smuzhiyun #else
58*4882a593Smuzhiyun #define DBG(x...) printf(x)
reqname(unsigned r)59*4882a593Smuzhiyun static const char *reqname(unsigned r)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun switch (r) {
62*4882a593Smuzhiyun case USB_REQ_GET_STATUS: return "GET_STATUS";
63*4882a593Smuzhiyun case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
64*4882a593Smuzhiyun case USB_REQ_SET_FEATURE: return "SET_FEATURE";
65*4882a593Smuzhiyun case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
66*4882a593Smuzhiyun case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
67*4882a593Smuzhiyun case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
68*4882a593Smuzhiyun case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
69*4882a593Smuzhiyun case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
70*4882a593Smuzhiyun case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
71*4882a593Smuzhiyun case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
72*4882a593Smuzhiyun default: return "*UNKNOWN*";
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun static struct usb_endpoint_descriptor ep0_desc = {
78*4882a593Smuzhiyun .bLength = sizeof(struct usb_endpoint_descriptor),
79*4882a593Smuzhiyun .bDescriptorType = USB_DT_ENDPOINT,
80*4882a593Smuzhiyun .bEndpointAddress = USB_DIR_IN,
81*4882a593Smuzhiyun .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun static int ci_pullup(struct usb_gadget *gadget, int is_on);
85*4882a593Smuzhiyun static int ci_ep_enable(struct usb_ep *ep,
86*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc);
87*4882a593Smuzhiyun static int ci_ep_disable(struct usb_ep *ep);
88*4882a593Smuzhiyun static int ci_ep_queue(struct usb_ep *ep,
89*4882a593Smuzhiyun struct usb_request *req, gfp_t gfp_flags);
90*4882a593Smuzhiyun static int ci_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
91*4882a593Smuzhiyun static struct usb_request *
92*4882a593Smuzhiyun ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
93*4882a593Smuzhiyun static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static struct usb_gadget_ops ci_udc_ops = {
96*4882a593Smuzhiyun .pullup = ci_pullup,
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun static struct usb_ep_ops ci_ep_ops = {
100*4882a593Smuzhiyun .enable = ci_ep_enable,
101*4882a593Smuzhiyun .disable = ci_ep_disable,
102*4882a593Smuzhiyun .queue = ci_ep_queue,
103*4882a593Smuzhiyun .dequeue = ci_ep_dequeue,
104*4882a593Smuzhiyun .alloc_request = ci_ep_alloc_request,
105*4882a593Smuzhiyun .free_request = ci_ep_free_request,
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* Init values for USB endpoints. */
109*4882a593Smuzhiyun static const struct usb_ep ci_ep_init[5] = {
110*4882a593Smuzhiyun [0] = { /* EP 0 */
111*4882a593Smuzhiyun .maxpacket = 64,
112*4882a593Smuzhiyun .name = "ep0",
113*4882a593Smuzhiyun .ops = &ci_ep_ops,
114*4882a593Smuzhiyun },
115*4882a593Smuzhiyun [1] = {
116*4882a593Smuzhiyun .maxpacket = 512,
117*4882a593Smuzhiyun .name = "ep1in-bulk",
118*4882a593Smuzhiyun .ops = &ci_ep_ops,
119*4882a593Smuzhiyun },
120*4882a593Smuzhiyun [2] = {
121*4882a593Smuzhiyun .maxpacket = 512,
122*4882a593Smuzhiyun .name = "ep2out-bulk",
123*4882a593Smuzhiyun .ops = &ci_ep_ops,
124*4882a593Smuzhiyun },
125*4882a593Smuzhiyun [3] = {
126*4882a593Smuzhiyun .maxpacket = 512,
127*4882a593Smuzhiyun .name = "ep3in-int",
128*4882a593Smuzhiyun .ops = &ci_ep_ops,
129*4882a593Smuzhiyun },
130*4882a593Smuzhiyun [4] = {
131*4882a593Smuzhiyun .maxpacket = 512,
132*4882a593Smuzhiyun .name = "ep-",
133*4882a593Smuzhiyun .ops = &ci_ep_ops,
134*4882a593Smuzhiyun },
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun static struct ci_drv controller = {
138*4882a593Smuzhiyun .gadget = {
139*4882a593Smuzhiyun .name = "ci_udc",
140*4882a593Smuzhiyun .ops = &ci_udc_ops,
141*4882a593Smuzhiyun .is_dualspeed = 1,
142*4882a593Smuzhiyun },
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * ci_get_qh() - return queue head for endpoint
147*4882a593Smuzhiyun * @ep_num: Endpoint number
148*4882a593Smuzhiyun * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * This function returns the QH associated with particular endpoint
151*4882a593Smuzhiyun * and it's direction.
152*4882a593Smuzhiyun */
ci_get_qh(int ep_num,int dir_in)153*4882a593Smuzhiyun static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun return &controller.epts[(ep_num * 2) + dir_in];
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun * ci_get_qtd() - return queue item for endpoint
160*4882a593Smuzhiyun * @ep_num: Endpoint number
161*4882a593Smuzhiyun * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * This function returns the QH associated with particular endpoint
164*4882a593Smuzhiyun * and it's direction.
165*4882a593Smuzhiyun */
ci_get_qtd(int ep_num,int dir_in)166*4882a593Smuzhiyun static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun int index = (ep_num * 2) + dir_in;
169*4882a593Smuzhiyun uint8_t *imem = controller.items_mem + (index * ILIST_ENT_SZ);
170*4882a593Smuzhiyun return (struct ept_queue_item *)imem;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun * ci_flush_qh - flush cache over queue head
175*4882a593Smuzhiyun * @ep_num: Endpoint number
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * This function flushes cache over QH for particular endpoint.
178*4882a593Smuzhiyun */
ci_flush_qh(int ep_num)179*4882a593Smuzhiyun static void ci_flush_qh(int ep_num)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct ept_queue_head *head = ci_get_qh(ep_num, 0);
182*4882a593Smuzhiyun const unsigned long start = (unsigned long)head;
183*4882a593Smuzhiyun const unsigned long end = start + 2 * sizeof(*head);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun flush_dcache_range(start, end);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun * ci_invalidate_qh - invalidate cache over queue head
190*4882a593Smuzhiyun * @ep_num: Endpoint number
191*4882a593Smuzhiyun *
192*4882a593Smuzhiyun * This function invalidates cache over QH for particular endpoint.
193*4882a593Smuzhiyun */
ci_invalidate_qh(int ep_num)194*4882a593Smuzhiyun static void ci_invalidate_qh(int ep_num)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun struct ept_queue_head *head = ci_get_qh(ep_num, 0);
197*4882a593Smuzhiyun unsigned long start = (unsigned long)head;
198*4882a593Smuzhiyun unsigned long end = start + 2 * sizeof(*head);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun invalidate_dcache_range(start, end);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun * ci_flush_qtd - flush cache over queue item
205*4882a593Smuzhiyun * @ep_num: Endpoint number
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * This function flushes cache over qTD pair for particular endpoint.
208*4882a593Smuzhiyun */
ci_flush_qtd(int ep_num)209*4882a593Smuzhiyun static void ci_flush_qtd(int ep_num)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
212*4882a593Smuzhiyun const unsigned long start = (unsigned long)item;
213*4882a593Smuzhiyun const unsigned long end = start + 2 * ILIST_ENT_SZ;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun flush_dcache_range(start, end);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun * ci_flush_td - flush cache over queue item
220*4882a593Smuzhiyun * @td: td pointer
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * This function flushes cache for particular transfer descriptor.
223*4882a593Smuzhiyun */
ci_flush_td(struct ept_queue_item * td)224*4882a593Smuzhiyun static void ci_flush_td(struct ept_queue_item *td)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun const unsigned long start = (unsigned long)td;
227*4882a593Smuzhiyun const unsigned long end = (unsigned long)td + ILIST_ENT_SZ;
228*4882a593Smuzhiyun flush_dcache_range(start, end);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * ci_invalidate_qtd - invalidate cache over queue item
233*4882a593Smuzhiyun * @ep_num: Endpoint number
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * This function invalidates cache over qTD pair for particular endpoint.
236*4882a593Smuzhiyun */
ci_invalidate_qtd(int ep_num)237*4882a593Smuzhiyun static void ci_invalidate_qtd(int ep_num)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
240*4882a593Smuzhiyun const unsigned long start = (unsigned long)item;
241*4882a593Smuzhiyun const unsigned long end = start + 2 * ILIST_ENT_SZ;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun invalidate_dcache_range(start, end);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /**
247*4882a593Smuzhiyun * ci_invalidate_td - invalidate cache over queue item
248*4882a593Smuzhiyun * @td: td pointer
249*4882a593Smuzhiyun *
250*4882a593Smuzhiyun * This function invalidates cache for particular transfer descriptor.
251*4882a593Smuzhiyun */
ci_invalidate_td(struct ept_queue_item * td)252*4882a593Smuzhiyun static void ci_invalidate_td(struct ept_queue_item *td)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun const unsigned long start = (unsigned long)td;
255*4882a593Smuzhiyun const unsigned long end = start + ILIST_ENT_SZ;
256*4882a593Smuzhiyun invalidate_dcache_range(start, end);
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun static struct usb_request *
ci_ep_alloc_request(struct usb_ep * ep,unsigned int gfp_flags)260*4882a593Smuzhiyun ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
263*4882a593Smuzhiyun int num = -1;
264*4882a593Smuzhiyun struct ci_req *ci_req;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (ci_ep->desc)
267*4882a593Smuzhiyun num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (num == 0 && controller.ep0_req)
270*4882a593Smuzhiyun return &controller.ep0_req->req;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun ci_req = calloc(1, sizeof(*ci_req));
273*4882a593Smuzhiyun if (!ci_req)
274*4882a593Smuzhiyun return NULL;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun INIT_LIST_HEAD(&ci_req->queue);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (num == 0)
279*4882a593Smuzhiyun controller.ep0_req = ci_req;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return &ci_req->req;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
ci_ep_free_request(struct usb_ep * ep,struct usb_request * req)284*4882a593Smuzhiyun static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
287*4882a593Smuzhiyun struct ci_req *ci_req = container_of(req, struct ci_req, req);
288*4882a593Smuzhiyun int num = -1;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun if (ci_ep->desc)
291*4882a593Smuzhiyun num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun if (num == 0) {
294*4882a593Smuzhiyun if (!controller.ep0_req)
295*4882a593Smuzhiyun return;
296*4882a593Smuzhiyun controller.ep0_req = 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (ci_req->b_buf)
300*4882a593Smuzhiyun free(ci_req->b_buf);
301*4882a593Smuzhiyun free(ci_req);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
ep_enable(int num,int in,int maxpacket)304*4882a593Smuzhiyun static void ep_enable(int num, int in, int maxpacket)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
307*4882a593Smuzhiyun unsigned n;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun n = readl(&udc->epctrl[num]);
310*4882a593Smuzhiyun if (in)
311*4882a593Smuzhiyun n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
312*4882a593Smuzhiyun else
313*4882a593Smuzhiyun n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (num != 0) {
316*4882a593Smuzhiyun struct ept_queue_head *head = ci_get_qh(num, in);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
319*4882a593Smuzhiyun ci_flush_qh(num);
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun writel(n, &udc->epctrl[num]);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
ci_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)324*4882a593Smuzhiyun static int ci_ep_enable(struct usb_ep *ep,
325*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
328*4882a593Smuzhiyun int num, in;
329*4882a593Smuzhiyun num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
330*4882a593Smuzhiyun in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
331*4882a593Smuzhiyun ci_ep->desc = desc;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun if (num) {
334*4882a593Smuzhiyun int max = get_unaligned_le16(&desc->wMaxPacketSize);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
337*4882a593Smuzhiyun max = 64;
338*4882a593Smuzhiyun if (ep->maxpacket != max) {
339*4882a593Smuzhiyun DBG("%s: from %d to %d\n", __func__,
340*4882a593Smuzhiyun ep->maxpacket, max);
341*4882a593Smuzhiyun ep->maxpacket = max;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun ep_enable(num, in, ep->maxpacket);
345*4882a593Smuzhiyun DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
346*4882a593Smuzhiyun return 0;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
ci_ep_disable(struct usb_ep * ep)349*4882a593Smuzhiyun static int ci_ep_disable(struct usb_ep *ep)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun ci_ep->desc = NULL;
354*4882a593Smuzhiyun return 0;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
ci_bounce(struct ci_req * ci_req,int in)357*4882a593Smuzhiyun static int ci_bounce(struct ci_req *ci_req, int in)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun struct usb_request *req = &ci_req->req;
360*4882a593Smuzhiyun unsigned long addr = (unsigned long)req->buf;
361*4882a593Smuzhiyun unsigned long hwaddr;
362*4882a593Smuzhiyun uint32_t aligned_used_len;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* Input buffer address is not aligned. */
365*4882a593Smuzhiyun if (addr & (ARCH_DMA_MINALIGN - 1))
366*4882a593Smuzhiyun goto align;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /* Input buffer length is not aligned. */
369*4882a593Smuzhiyun if (req->length & (ARCH_DMA_MINALIGN - 1))
370*4882a593Smuzhiyun goto align;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* The buffer is well aligned, only flush cache. */
373*4882a593Smuzhiyun ci_req->hw_len = req->length;
374*4882a593Smuzhiyun ci_req->hw_buf = req->buf;
375*4882a593Smuzhiyun goto flush;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun align:
378*4882a593Smuzhiyun if (ci_req->b_buf && req->length > ci_req->b_len) {
379*4882a593Smuzhiyun free(ci_req->b_buf);
380*4882a593Smuzhiyun ci_req->b_buf = 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun if (!ci_req->b_buf) {
383*4882a593Smuzhiyun ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
384*4882a593Smuzhiyun ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
385*4882a593Smuzhiyun if (!ci_req->b_buf)
386*4882a593Smuzhiyun return -ENOMEM;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun ci_req->hw_len = ci_req->b_len;
389*4882a593Smuzhiyun ci_req->hw_buf = ci_req->b_buf;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (in)
392*4882a593Smuzhiyun memcpy(ci_req->hw_buf, req->buf, req->length);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun flush:
395*4882a593Smuzhiyun hwaddr = (unsigned long)ci_req->hw_buf;
396*4882a593Smuzhiyun aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
397*4882a593Smuzhiyun flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
ci_debounce(struct ci_req * ci_req,int in)402*4882a593Smuzhiyun static void ci_debounce(struct ci_req *ci_req, int in)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun struct usb_request *req = &ci_req->req;
405*4882a593Smuzhiyun unsigned long addr = (unsigned long)req->buf;
406*4882a593Smuzhiyun unsigned long hwaddr = (unsigned long)ci_req->hw_buf;
407*4882a593Smuzhiyun uint32_t aligned_used_len;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun if (in)
410*4882a593Smuzhiyun return;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
413*4882a593Smuzhiyun invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (addr == hwaddr)
416*4882a593Smuzhiyun return; /* not a bounce */
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun memcpy(req->buf, ci_req->hw_buf, req->actual);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
ci_ep_submit_next_request(struct ci_ep * ci_ep)421*4882a593Smuzhiyun static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
424*4882a593Smuzhiyun struct ept_queue_item *item;
425*4882a593Smuzhiyun struct ept_queue_head *head;
426*4882a593Smuzhiyun int bit, num, len, in;
427*4882a593Smuzhiyun struct ci_req *ci_req;
428*4882a593Smuzhiyun u8 *buf;
429*4882a593Smuzhiyun uint32_t len_left, len_this_dtd;
430*4882a593Smuzhiyun struct ept_queue_item *dtd, *qtd;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun ci_ep->req_primed = true;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
435*4882a593Smuzhiyun in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
436*4882a593Smuzhiyun item = ci_get_qtd(num, in);
437*4882a593Smuzhiyun head = ci_get_qh(num, in);
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
440*4882a593Smuzhiyun len = ci_req->req.length;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun head->next = (unsigned long)item;
443*4882a593Smuzhiyun head->info = 0;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun ci_req->dtd_count = 0;
446*4882a593Smuzhiyun buf = ci_req->hw_buf;
447*4882a593Smuzhiyun len_left = len;
448*4882a593Smuzhiyun dtd = item;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun do {
451*4882a593Smuzhiyun len_this_dtd = min(len_left, (unsigned)EP_MAX_LENGTH_TRANSFER);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun dtd->info = INFO_BYTES(len_this_dtd) | INFO_ACTIVE;
454*4882a593Smuzhiyun dtd->page0 = (unsigned long)buf;
455*4882a593Smuzhiyun dtd->page1 = ((unsigned long)buf & 0xfffff000) + 0x1000;
456*4882a593Smuzhiyun dtd->page2 = ((unsigned long)buf & 0xfffff000) + 0x2000;
457*4882a593Smuzhiyun dtd->page3 = ((unsigned long)buf & 0xfffff000) + 0x3000;
458*4882a593Smuzhiyun dtd->page4 = ((unsigned long)buf & 0xfffff000) + 0x4000;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun len_left -= len_this_dtd;
461*4882a593Smuzhiyun buf += len_this_dtd;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (len_left) {
464*4882a593Smuzhiyun qtd = (struct ept_queue_item *)
465*4882a593Smuzhiyun memalign(ILIST_ALIGN, ILIST_ENT_SZ);
466*4882a593Smuzhiyun dtd->next = (unsigned long)qtd;
467*4882a593Smuzhiyun dtd = qtd;
468*4882a593Smuzhiyun memset(dtd, 0, ILIST_ENT_SZ);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun ci_req->dtd_count++;
472*4882a593Smuzhiyun } while (len_left);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun item = dtd;
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun * When sending the data for an IN transaction, the attached host
477*4882a593Smuzhiyun * knows that all data for the IN is sent when one of the following
478*4882a593Smuzhiyun * occurs:
479*4882a593Smuzhiyun * a) A zero-length packet is transmitted.
480*4882a593Smuzhiyun * b) A packet with length that isn't an exact multiple of the ep's
481*4882a593Smuzhiyun * maxpacket is transmitted.
482*4882a593Smuzhiyun * c) Enough data is sent to exactly fill the host's maximum expected
483*4882a593Smuzhiyun * IN transaction size.
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * One of these conditions MUST apply at the end of an IN transaction,
486*4882a593Smuzhiyun * or the transaction will not be considered complete by the host. If
487*4882a593Smuzhiyun * none of (a)..(c) already applies, then we must force (a) to apply
488*4882a593Smuzhiyun * by explicitly sending an extra zero-length packet.
489*4882a593Smuzhiyun */
490*4882a593Smuzhiyun /* IN !a !b !c */
491*4882a593Smuzhiyun if (in && len && !(len % ci_ep->ep.maxpacket) && ci_req->req.zero) {
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun * Each endpoint has 2 items allocated, even though typically
494*4882a593Smuzhiyun * only 1 is used at a time since either an IN or an OUT but
495*4882a593Smuzhiyun * not both is queued. For an IN transaction, item currently
496*4882a593Smuzhiyun * points at the second of these items, so we know that we
497*4882a593Smuzhiyun * can use the other to transmit the extra zero-length packet.
498*4882a593Smuzhiyun */
499*4882a593Smuzhiyun struct ept_queue_item *other_item = ci_get_qtd(num, 0);
500*4882a593Smuzhiyun item->next = (unsigned long)other_item;
501*4882a593Smuzhiyun item = other_item;
502*4882a593Smuzhiyun item->info = INFO_ACTIVE;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun item->next = TERMINATE;
506*4882a593Smuzhiyun item->info |= INFO_IOC;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun ci_flush_qtd(num);
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun item = (struct ept_queue_item *)(unsigned long)head->next;
511*4882a593Smuzhiyun while (item->next != TERMINATE) {
512*4882a593Smuzhiyun ci_flush_td((struct ept_queue_item *)(unsigned long)item->next);
513*4882a593Smuzhiyun item = (struct ept_queue_item *)(unsigned long)item->next;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun DBG("ept%d %s queue len %x, req %p, buffer %p\n",
517*4882a593Smuzhiyun num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
518*4882a593Smuzhiyun ci_flush_qh(num);
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun if (in)
521*4882a593Smuzhiyun bit = EPT_TX(num);
522*4882a593Smuzhiyun else
523*4882a593Smuzhiyun bit = EPT_RX(num);
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun writel(bit, &udc->epprime);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
ci_ep_dequeue(struct usb_ep * _ep,struct usb_request * _req)528*4882a593Smuzhiyun static int ci_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(_ep, struct ci_ep, ep);
531*4882a593Smuzhiyun struct ci_req *ci_req;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun list_for_each_entry(ci_req, &ci_ep->queue, queue) {
534*4882a593Smuzhiyun if (&ci_req->req == _req)
535*4882a593Smuzhiyun break;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (&ci_req->req != _req)
539*4882a593Smuzhiyun return -EINVAL;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun list_del_init(&ci_req->queue);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if (ci_req->req.status == -EINPROGRESS) {
544*4882a593Smuzhiyun ci_req->req.status = -ECONNRESET;
545*4882a593Smuzhiyun if (ci_req->req.complete)
546*4882a593Smuzhiyun ci_req->req.complete(_ep, _req);
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun return 0;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
ci_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)552*4882a593Smuzhiyun static int ci_ep_queue(struct usb_ep *ep,
553*4882a593Smuzhiyun struct usb_request *req, gfp_t gfp_flags)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
556*4882a593Smuzhiyun struct ci_req *ci_req = container_of(req, struct ci_req, req);
557*4882a593Smuzhiyun int in, ret;
558*4882a593Smuzhiyun int __maybe_unused num;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
561*4882a593Smuzhiyun in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun if (!num && ci_ep->req_primed) {
564*4882a593Smuzhiyun /*
565*4882a593Smuzhiyun * The flipping of ep0 between IN and OUT relies on
566*4882a593Smuzhiyun * ci_ep_queue consuming the current IN/OUT setting
567*4882a593Smuzhiyun * immediately. If this is deferred to a later point when the
568*4882a593Smuzhiyun * req is pulled out of ci_req->queue, then the IN/OUT setting
569*4882a593Smuzhiyun * may have been changed since the req was queued, and state
570*4882a593Smuzhiyun * will get out of sync. This condition doesn't occur today,
571*4882a593Smuzhiyun * but could if bugs were introduced later, and this error
572*4882a593Smuzhiyun * check will save a lot of debugging time.
573*4882a593Smuzhiyun */
574*4882a593Smuzhiyun printf("%s: ep0 transaction already in progress\n", __func__);
575*4882a593Smuzhiyun return -EPROTO;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun ret = ci_bounce(ci_req, in);
579*4882a593Smuzhiyun if (ret)
580*4882a593Smuzhiyun return ret;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun DBG("ept%d %s pre-queue req %p, buffer %p\n",
583*4882a593Smuzhiyun num, in ? "in" : "out", ci_req, ci_req->hw_buf);
584*4882a593Smuzhiyun list_add_tail(&ci_req->queue, &ci_ep->queue);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun if (!ci_ep->req_primed)
587*4882a593Smuzhiyun ci_ep_submit_next_request(ci_ep);
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun return 0;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
flip_ep0_direction(void)592*4882a593Smuzhiyun static void flip_ep0_direction(void)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun if (ep0_desc.bEndpointAddress == USB_DIR_IN) {
595*4882a593Smuzhiyun DBG("%s: Flipping ep0 to OUT\n", __func__);
596*4882a593Smuzhiyun ep0_desc.bEndpointAddress = 0;
597*4882a593Smuzhiyun } else {
598*4882a593Smuzhiyun DBG("%s: Flipping ep0 to IN\n", __func__);
599*4882a593Smuzhiyun ep0_desc.bEndpointAddress = USB_DIR_IN;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
handle_ep_complete(struct ci_ep * ci_ep)603*4882a593Smuzhiyun static void handle_ep_complete(struct ci_ep *ci_ep)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun struct ept_queue_item *item, *next_td;
606*4882a593Smuzhiyun int num, in, len, j;
607*4882a593Smuzhiyun struct ci_req *ci_req;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
610*4882a593Smuzhiyun in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
611*4882a593Smuzhiyun item = ci_get_qtd(num, in);
612*4882a593Smuzhiyun ci_invalidate_qtd(num);
613*4882a593Smuzhiyun ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun next_td = item;
616*4882a593Smuzhiyun len = 0;
617*4882a593Smuzhiyun for (j = 0; j < ci_req->dtd_count; j++) {
618*4882a593Smuzhiyun ci_invalidate_td(next_td);
619*4882a593Smuzhiyun item = next_td;
620*4882a593Smuzhiyun len += (item->info >> 16) & 0x7fff;
621*4882a593Smuzhiyun if (item->info & 0xff)
622*4882a593Smuzhiyun printf("EP%d/%s FAIL info=%x pg0=%x\n",
623*4882a593Smuzhiyun num, in ? "in" : "out", item->info, item->page0);
624*4882a593Smuzhiyun if (j != ci_req->dtd_count - 1)
625*4882a593Smuzhiyun next_td = (struct ept_queue_item *)(unsigned long)
626*4882a593Smuzhiyun item->next;
627*4882a593Smuzhiyun if (j != 0)
628*4882a593Smuzhiyun free(item);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun list_del_init(&ci_req->queue);
632*4882a593Smuzhiyun ci_ep->req_primed = false;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun if (!list_empty(&ci_ep->queue))
635*4882a593Smuzhiyun ci_ep_submit_next_request(ci_ep);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun ci_req->req.actual = ci_req->req.length - len;
638*4882a593Smuzhiyun ci_debounce(ci_req, in);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun DBG("ept%d %s req %p, complete %x\n",
641*4882a593Smuzhiyun num, in ? "in" : "out", ci_req, len);
642*4882a593Smuzhiyun if (num != 0 || controller.ep0_data_phase)
643*4882a593Smuzhiyun ci_req->req.complete(&ci_ep->ep, &ci_req->req);
644*4882a593Smuzhiyun if (num == 0 && controller.ep0_data_phase) {
645*4882a593Smuzhiyun /*
646*4882a593Smuzhiyun * Data Stage is complete, so flip ep0 dir for Status Stage,
647*4882a593Smuzhiyun * which always transfers a packet in the opposite direction.
648*4882a593Smuzhiyun */
649*4882a593Smuzhiyun DBG("%s: flip ep0 dir for Status Stage\n", __func__);
650*4882a593Smuzhiyun flip_ep0_direction();
651*4882a593Smuzhiyun controller.ep0_data_phase = false;
652*4882a593Smuzhiyun ci_req->req.length = 0;
653*4882a593Smuzhiyun usb_ep_queue(&ci_ep->ep, &ci_req->req, 0);
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun #define SETUP(type, request) (((type) << 8) | (request))
658*4882a593Smuzhiyun
handle_setup(void)659*4882a593Smuzhiyun static void handle_setup(void)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun struct ci_ep *ci_ep = &controller.ep[0];
662*4882a593Smuzhiyun struct ci_req *ci_req;
663*4882a593Smuzhiyun struct usb_request *req;
664*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
665*4882a593Smuzhiyun struct ept_queue_head *head;
666*4882a593Smuzhiyun struct usb_ctrlrequest r;
667*4882a593Smuzhiyun int status = 0;
668*4882a593Smuzhiyun int num, in, _num, _in, i;
669*4882a593Smuzhiyun char *buf;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun ci_req = controller.ep0_req;
672*4882a593Smuzhiyun req = &ci_req->req;
673*4882a593Smuzhiyun head = ci_get_qh(0, 0); /* EP0 OUT */
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun ci_invalidate_qh(0);
676*4882a593Smuzhiyun memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
677*4882a593Smuzhiyun #ifdef CONFIG_CI_UDC_HAS_HOSTPC
678*4882a593Smuzhiyun writel(EPT_RX(0), &udc->epsetupstat);
679*4882a593Smuzhiyun #else
680*4882a593Smuzhiyun writel(EPT_RX(0), &udc->epstat);
681*4882a593Smuzhiyun #endif
682*4882a593Smuzhiyun DBG("handle setup %s, %x, %x index %x value %x length %x\n",
683*4882a593Smuzhiyun reqname(r.bRequest), r.bRequestType, r.bRequest, r.wIndex,
684*4882a593Smuzhiyun r.wValue, r.wLength);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /* Set EP0 dir for Data Stage based on Setup Stage data */
687*4882a593Smuzhiyun if (r.bRequestType & USB_DIR_IN) {
688*4882a593Smuzhiyun DBG("%s: Set ep0 to IN for Data Stage\n", __func__);
689*4882a593Smuzhiyun ep0_desc.bEndpointAddress = USB_DIR_IN;
690*4882a593Smuzhiyun } else {
691*4882a593Smuzhiyun DBG("%s: Set ep0 to OUT for Data Stage\n", __func__);
692*4882a593Smuzhiyun ep0_desc.bEndpointAddress = 0;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun if (r.wLength) {
695*4882a593Smuzhiyun controller.ep0_data_phase = true;
696*4882a593Smuzhiyun } else {
697*4882a593Smuzhiyun /* 0 length -> no Data Stage. Flip dir for Status Stage */
698*4882a593Smuzhiyun DBG("%s: 0 length: flip ep0 dir for Status Stage\n", __func__);
699*4882a593Smuzhiyun flip_ep0_direction();
700*4882a593Smuzhiyun controller.ep0_data_phase = false;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun list_del_init(&ci_req->queue);
704*4882a593Smuzhiyun ci_ep->req_primed = false;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun switch (SETUP(r.bRequestType, r.bRequest)) {
707*4882a593Smuzhiyun case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
708*4882a593Smuzhiyun _num = r.wIndex & 15;
709*4882a593Smuzhiyun _in = !!(r.wIndex & 0x80);
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun if ((r.wValue == 0) && (r.wLength == 0)) {
712*4882a593Smuzhiyun req->length = 0;
713*4882a593Smuzhiyun for (i = 0; i < NUM_ENDPOINTS; i++) {
714*4882a593Smuzhiyun struct ci_ep *ep = &controller.ep[i];
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun if (!ep->desc)
717*4882a593Smuzhiyun continue;
718*4882a593Smuzhiyun num = ep->desc->bEndpointAddress
719*4882a593Smuzhiyun & USB_ENDPOINT_NUMBER_MASK;
720*4882a593Smuzhiyun in = (ep->desc->bEndpointAddress
721*4882a593Smuzhiyun & USB_DIR_IN) != 0;
722*4882a593Smuzhiyun if ((num == _num) && (in == _in)) {
723*4882a593Smuzhiyun ep_enable(num, in, ep->ep.maxpacket);
724*4882a593Smuzhiyun usb_ep_queue(controller.gadget.ep0,
725*4882a593Smuzhiyun req, 0);
726*4882a593Smuzhiyun break;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun return;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
733*4882a593Smuzhiyun /*
734*4882a593Smuzhiyun * write address delayed (will take effect
735*4882a593Smuzhiyun * after the next IN txn)
736*4882a593Smuzhiyun */
737*4882a593Smuzhiyun writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
738*4882a593Smuzhiyun req->length = 0;
739*4882a593Smuzhiyun usb_ep_queue(controller.gadget.ep0, req, 0);
740*4882a593Smuzhiyun return;
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
743*4882a593Smuzhiyun req->length = 2;
744*4882a593Smuzhiyun buf = (char *)req->buf;
745*4882a593Smuzhiyun buf[0] = 1 << USB_DEVICE_SELF_POWERED;
746*4882a593Smuzhiyun buf[1] = 0;
747*4882a593Smuzhiyun usb_ep_queue(controller.gadget.ep0, req, 0);
748*4882a593Smuzhiyun return;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun /* pass request up to the gadget driver */
751*4882a593Smuzhiyun if (controller.driver)
752*4882a593Smuzhiyun status = controller.driver->setup(&controller.gadget, &r);
753*4882a593Smuzhiyun else
754*4882a593Smuzhiyun status = -ENODEV;
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (!status)
757*4882a593Smuzhiyun return;
758*4882a593Smuzhiyun DBG("STALL reqname %s type %x value %x, index %x\n",
759*4882a593Smuzhiyun reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
760*4882a593Smuzhiyun writel((1<<16) | (1 << 0), &udc->epctrl[0]);
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun
stop_activity(void)763*4882a593Smuzhiyun static void stop_activity(void)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun int i, num, in;
766*4882a593Smuzhiyun struct ept_queue_head *head;
767*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
768*4882a593Smuzhiyun writel(readl(&udc->epcomp), &udc->epcomp);
769*4882a593Smuzhiyun #ifdef CONFIG_CI_UDC_HAS_HOSTPC
770*4882a593Smuzhiyun writel(readl(&udc->epsetupstat), &udc->epsetupstat);
771*4882a593Smuzhiyun #endif
772*4882a593Smuzhiyun writel(readl(&udc->epstat), &udc->epstat);
773*4882a593Smuzhiyun writel(0xffffffff, &udc->epflush);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* error out any pending reqs */
776*4882a593Smuzhiyun for (i = 0; i < NUM_ENDPOINTS; i++) {
777*4882a593Smuzhiyun if (i != 0)
778*4882a593Smuzhiyun writel(0, &udc->epctrl[i]);
779*4882a593Smuzhiyun if (controller.ep[i].desc) {
780*4882a593Smuzhiyun num = controller.ep[i].desc->bEndpointAddress
781*4882a593Smuzhiyun & USB_ENDPOINT_NUMBER_MASK;
782*4882a593Smuzhiyun in = (controller.ep[i].desc->bEndpointAddress
783*4882a593Smuzhiyun & USB_DIR_IN) != 0;
784*4882a593Smuzhiyun head = ci_get_qh(num, in);
785*4882a593Smuzhiyun head->info = INFO_ACTIVE;
786*4882a593Smuzhiyun ci_flush_qh(num);
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
udc_irq(void)791*4882a593Smuzhiyun void udc_irq(void)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
794*4882a593Smuzhiyun unsigned n = readl(&udc->usbsts);
795*4882a593Smuzhiyun writel(n, &udc->usbsts);
796*4882a593Smuzhiyun int bit, i, num, in;
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
799*4882a593Smuzhiyun if (n == 0)
800*4882a593Smuzhiyun return;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun if (n & STS_URI) {
803*4882a593Smuzhiyun DBG("-- reset --\n");
804*4882a593Smuzhiyun stop_activity();
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun if (n & STS_SLI)
807*4882a593Smuzhiyun DBG("-- suspend --\n");
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun if (n & STS_PCI) {
810*4882a593Smuzhiyun int max = 64;
811*4882a593Smuzhiyun int speed = USB_SPEED_FULL;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun #ifdef CONFIG_CI_UDC_HAS_HOSTPC
814*4882a593Smuzhiyun bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
815*4882a593Smuzhiyun #else
816*4882a593Smuzhiyun bit = (readl(&udc->portsc) >> 26) & 3;
817*4882a593Smuzhiyun #endif
818*4882a593Smuzhiyun DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
819*4882a593Smuzhiyun if (bit == 2) {
820*4882a593Smuzhiyun speed = USB_SPEED_HIGH;
821*4882a593Smuzhiyun max = 512;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun controller.gadget.speed = speed;
824*4882a593Smuzhiyun for (i = 1; i < NUM_ENDPOINTS; i++) {
825*4882a593Smuzhiyun if (controller.ep[i].ep.maxpacket > max)
826*4882a593Smuzhiyun controller.ep[i].ep.maxpacket = max;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun if (n & STS_UEI)
831*4882a593Smuzhiyun printf("<UEI %x>\n", readl(&udc->epcomp));
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun if ((n & STS_UI) || (n & STS_UEI)) {
834*4882a593Smuzhiyun #ifdef CONFIG_CI_UDC_HAS_HOSTPC
835*4882a593Smuzhiyun n = readl(&udc->epsetupstat);
836*4882a593Smuzhiyun #else
837*4882a593Smuzhiyun n = readl(&udc->epstat);
838*4882a593Smuzhiyun #endif
839*4882a593Smuzhiyun if (n & EPT_RX(0))
840*4882a593Smuzhiyun handle_setup();
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun n = readl(&udc->epcomp);
843*4882a593Smuzhiyun if (n != 0)
844*4882a593Smuzhiyun writel(n, &udc->epcomp);
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun for (i = 0; i < NUM_ENDPOINTS && n; i++) {
847*4882a593Smuzhiyun if (controller.ep[i].desc) {
848*4882a593Smuzhiyun num = controller.ep[i].desc->bEndpointAddress
849*4882a593Smuzhiyun & USB_ENDPOINT_NUMBER_MASK;
850*4882a593Smuzhiyun in = (controller.ep[i].desc->bEndpointAddress
851*4882a593Smuzhiyun & USB_DIR_IN) != 0;
852*4882a593Smuzhiyun bit = (in) ? EPT_TX(num) : EPT_RX(num);
853*4882a593Smuzhiyun if (n & bit)
854*4882a593Smuzhiyun handle_ep_complete(&controller.ep[i]);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun
usb_gadget_handle_interrupts(int index)860*4882a593Smuzhiyun int usb_gadget_handle_interrupts(int index)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun u32 value;
863*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun value = readl(&udc->usbsts);
866*4882a593Smuzhiyun if (value)
867*4882a593Smuzhiyun udc_irq();
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun return value;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
udc_disconnect(void)872*4882a593Smuzhiyun void udc_disconnect(void)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
875*4882a593Smuzhiyun /* disable pullup */
876*4882a593Smuzhiyun stop_activity();
877*4882a593Smuzhiyun writel(USBCMD_FS2, &udc->usbcmd);
878*4882a593Smuzhiyun udelay(800);
879*4882a593Smuzhiyun if (controller.driver)
880*4882a593Smuzhiyun controller.driver->disconnect(&controller.gadget);
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun
ci_pullup(struct usb_gadget * gadget,int is_on)883*4882a593Smuzhiyun static int ci_pullup(struct usb_gadget *gadget, int is_on)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
886*4882a593Smuzhiyun if (is_on) {
887*4882a593Smuzhiyun /* RESET */
888*4882a593Smuzhiyun writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
889*4882a593Smuzhiyun udelay(200);
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun writel((unsigned long)controller.epts, &udc->epinitaddr);
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun /* select DEVICE mode */
894*4882a593Smuzhiyun writel(USBMODE_DEVICE, &udc->usbmode);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun #if !defined(CONFIG_USB_GADGET_DUALSPEED)
897*4882a593Smuzhiyun /* Port force Full-Speed Connect */
898*4882a593Smuzhiyun setbits_le32(&udc->portsc, PFSC);
899*4882a593Smuzhiyun #endif
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun writel(0xffffffff, &udc->epflush);
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun /* Turn on the USB connection by enabling the pullup resistor */
904*4882a593Smuzhiyun writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
905*4882a593Smuzhiyun } else {
906*4882a593Smuzhiyun udc_disconnect();
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun return 0;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun
ci_udc_probe(void)912*4882a593Smuzhiyun static int ci_udc_probe(void)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun struct ept_queue_head *head;
915*4882a593Smuzhiyun int i;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun const int num = 2 * NUM_ENDPOINTS;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun const int eplist_min_align = 4096;
920*4882a593Smuzhiyun const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
921*4882a593Smuzhiyun const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
922*4882a593Smuzhiyun const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun /* The QH list must be aligned to 4096 bytes. */
925*4882a593Smuzhiyun controller.epts = memalign(eplist_align, eplist_sz);
926*4882a593Smuzhiyun if (!controller.epts)
927*4882a593Smuzhiyun return -ENOMEM;
928*4882a593Smuzhiyun memset(controller.epts, 0, eplist_sz);
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun controller.items_mem = memalign(ILIST_ALIGN, ILIST_SZ);
931*4882a593Smuzhiyun if (!controller.items_mem) {
932*4882a593Smuzhiyun free(controller.epts);
933*4882a593Smuzhiyun return -ENOMEM;
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun memset(controller.items_mem, 0, ILIST_SZ);
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
938*4882a593Smuzhiyun /*
939*4882a593Smuzhiyun * Configure QH for each endpoint. The structure of the QH list
940*4882a593Smuzhiyun * is such that each two subsequent fields, N and N+1 where N is
941*4882a593Smuzhiyun * even, in the QH list represent QH for one endpoint. The Nth
942*4882a593Smuzhiyun * entry represents OUT configuration and the N+1th entry does
943*4882a593Smuzhiyun * represent IN configuration of the endpoint.
944*4882a593Smuzhiyun */
945*4882a593Smuzhiyun head = controller.epts + i;
946*4882a593Smuzhiyun if (i < 2)
947*4882a593Smuzhiyun head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
948*4882a593Smuzhiyun | CONFIG_ZLT | CONFIG_IOS;
949*4882a593Smuzhiyun else
950*4882a593Smuzhiyun head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
951*4882a593Smuzhiyun | CONFIG_ZLT;
952*4882a593Smuzhiyun head->next = TERMINATE;
953*4882a593Smuzhiyun head->info = 0;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun if (i & 1) {
956*4882a593Smuzhiyun ci_flush_qh(i / 2);
957*4882a593Smuzhiyun ci_flush_qtd(i / 2);
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun INIT_LIST_HEAD(&controller.gadget.ep_list);
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun /* Init EP 0 */
964*4882a593Smuzhiyun memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
965*4882a593Smuzhiyun controller.ep[0].desc = &ep0_desc;
966*4882a593Smuzhiyun INIT_LIST_HEAD(&controller.ep[0].queue);
967*4882a593Smuzhiyun controller.ep[0].req_primed = false;
968*4882a593Smuzhiyun controller.gadget.ep0 = &controller.ep[0].ep;
969*4882a593Smuzhiyun INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /* Init EP 1..3 */
972*4882a593Smuzhiyun for (i = 1; i < 4; i++) {
973*4882a593Smuzhiyun memcpy(&controller.ep[i].ep, &ci_ep_init[i],
974*4882a593Smuzhiyun sizeof(*ci_ep_init));
975*4882a593Smuzhiyun INIT_LIST_HEAD(&controller.ep[i].queue);
976*4882a593Smuzhiyun controller.ep[i].req_primed = false;
977*4882a593Smuzhiyun list_add_tail(&controller.ep[i].ep.ep_list,
978*4882a593Smuzhiyun &controller.gadget.ep_list);
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun /* Init EP 4..n */
982*4882a593Smuzhiyun for (i = 4; i < NUM_ENDPOINTS; i++) {
983*4882a593Smuzhiyun memcpy(&controller.ep[i].ep, &ci_ep_init[4],
984*4882a593Smuzhiyun sizeof(*ci_ep_init));
985*4882a593Smuzhiyun INIT_LIST_HEAD(&controller.ep[i].queue);
986*4882a593Smuzhiyun controller.ep[i].req_primed = false;
987*4882a593Smuzhiyun list_add_tail(&controller.ep[i].ep.ep_list,
988*4882a593Smuzhiyun &controller.gadget.ep_list);
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun ci_ep_alloc_request(&controller.ep[0].ep, 0);
992*4882a593Smuzhiyun if (!controller.ep0_req) {
993*4882a593Smuzhiyun free(controller.items_mem);
994*4882a593Smuzhiyun free(controller.epts);
995*4882a593Smuzhiyun return -ENOMEM;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun return 0;
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun
usb_gadget_register_driver(struct usb_gadget_driver * driver)1001*4882a593Smuzhiyun int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun int ret;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun if (!driver)
1006*4882a593Smuzhiyun return -EINVAL;
1007*4882a593Smuzhiyun if (!driver->bind || !driver->setup || !driver->disconnect)
1008*4882a593Smuzhiyun return -EINVAL;
1009*4882a593Smuzhiyun if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
1010*4882a593Smuzhiyun return -EINVAL;
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
1013*4882a593Smuzhiyun ret = usb_setup_ehci_gadget(&controller.ctrl);
1014*4882a593Smuzhiyun #else
1015*4882a593Smuzhiyun ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
1016*4882a593Smuzhiyun #endif
1017*4882a593Smuzhiyun if (ret)
1018*4882a593Smuzhiyun return ret;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun ret = ci_udc_probe();
1021*4882a593Smuzhiyun if (ret) {
1022*4882a593Smuzhiyun DBG("udc probe failed, returned %d\n", ret);
1023*4882a593Smuzhiyun return ret;
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun ret = driver->bind(&controller.gadget);
1027*4882a593Smuzhiyun if (ret) {
1028*4882a593Smuzhiyun DBG("driver->bind() returned %d\n", ret);
1029*4882a593Smuzhiyun return ret;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun controller.driver = driver;
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun return 0;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)1036*4882a593Smuzhiyun int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun udc_disconnect();
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun driver->unbind(&controller.gadget);
1041*4882a593Smuzhiyun controller.driver = NULL;
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun ci_ep_free_request(&controller.ep[0].ep, &controller.ep0_req->req);
1044*4882a593Smuzhiyun free(controller.items_mem);
1045*4882a593Smuzhiyun free(controller.epts);
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun return 0;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
dfu_usb_get_reset(void)1050*4882a593Smuzhiyun bool dfu_usb_get_reset(void)
1051*4882a593Smuzhiyun {
1052*4882a593Smuzhiyun struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun return !!(readl(&udc->usbsts) & STS_URI);
1055*4882a593Smuzhiyun }
1056