usb: mv_udc: Move QH and qTD into mv_drv
[platform/kernel/u-boot.git] / drivers / usb / gadget / mv_udc.c
1 /*
2  * Copyright 2011, Marvell Semiconductor Inc.
3  * Lei Wen <leiwen@marvell.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  *
7  * Back ported to the 8xx platform (from the 8260 platform) by
8  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9  */
10
11 #include <common.h>
12 #include <command.h>
13 #include <config.h>
14 #include <net.h>
15 #include <malloc.h>
16 #include <asm/io.h>
17 #include <linux/types.h>
18 #include <usb/mv_udc.h>
19
20 #if CONFIG_USB_MAX_CONTROLLER_COUNT > 1
21 #error This driver only supports one single controller.
22 #endif
23
24 #ifndef DEBUG
25 #define DBG(x...) do {} while (0)
26 #else
27 #define DBG(x...) printf(x)
28 static const char *reqname(unsigned r)
29 {
30         switch (r) {
31         case USB_REQ_GET_STATUS: return "GET_STATUS";
32         case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
33         case USB_REQ_SET_FEATURE: return "SET_FEATURE";
34         case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
35         case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
36         case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
37         case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
38         case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
39         case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
40         case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
41         default: return "*UNKNOWN*";
42         }
43 }
44 #endif
45
46 #define PAGE_SIZE       4096
47
48 static struct usb_endpoint_descriptor ep0_out_desc = {
49         .bLength = sizeof(struct usb_endpoint_descriptor),
50         .bDescriptorType = USB_DT_ENDPOINT,
51         .bEndpointAddress = 0,
52         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
53 };
54
55 static struct usb_endpoint_descriptor ep0_in_desc = {
56         .bLength = sizeof(struct usb_endpoint_descriptor),
57         .bDescriptorType = USB_DT_ENDPOINT,
58         .bEndpointAddress = USB_DIR_IN,
59         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
60 };
61
62 static int mv_pullup(struct usb_gadget *gadget, int is_on);
63 static int mv_ep_enable(struct usb_ep *ep,
64                 const struct usb_endpoint_descriptor *desc);
65 static int mv_ep_disable(struct usb_ep *ep);
66 static int mv_ep_queue(struct usb_ep *ep,
67                 struct usb_request *req, gfp_t gfp_flags);
68 static struct usb_request *
69 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
70 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
71
72 static struct usb_gadget_ops mv_udc_ops = {
73         .pullup = mv_pullup,
74 };
75
76 static struct usb_ep_ops mv_ep_ops = {
77         .enable         = mv_ep_enable,
78         .disable        = mv_ep_disable,
79         .queue          = mv_ep_queue,
80         .alloc_request  = mv_ep_alloc_request,
81         .free_request   = mv_ep_free_request,
82 };
83
84 /* Init values for USB endpoints. */
85 static const struct usb_ep mv_ep_init[2] = {
86         [0] = { /* EP 0 */
87                 .maxpacket      = 64,
88                 .name           = "ep0",
89                 .ops            = &mv_ep_ops,
90         },
91         [1] = { /* EP 1..n */
92                 .maxpacket      = 512,
93                 .name           = "ep-",
94                 .ops            = &mv_ep_ops,
95         },
96 };
97
98 static struct mv_drv controller = {
99         .gadget = {
100                 .name   = "mv_udc",
101                 .ops    = &mv_udc_ops,
102         },
103 };
104
105 static struct usb_request *
106 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
107 {
108         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
109         return &mv_ep->req;
110 }
111
112 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
113 {
114         return;
115 }
116
117 static void ep_enable(int num, int in)
118 {
119         struct ept_queue_head *head;
120         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
121         unsigned n;
122         head = controller.epts + 2*num + in;
123
124         n = readl(&udc->epctrl[num]);
125         if (in)
126                 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
127         else
128                 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
129
130         if (num != 0)
131                 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
132         writel(n, &udc->epctrl[num]);
133 }
134
135 static int mv_ep_enable(struct usb_ep *ep,
136                 const struct usb_endpoint_descriptor *desc)
137 {
138         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
139         int num, in;
140         num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
141         in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
142         ep_enable(num, in);
143         mv_ep->desc = desc;
144         return 0;
145 }
146
147 static int mv_ep_disable(struct usb_ep *ep)
148 {
149         return 0;
150 }
151
152 static int mv_ep_queue(struct usb_ep *ep,
153                 struct usb_request *req, gfp_t gfp_flags)
154 {
155         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
156         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
157         struct ept_queue_item *item;
158         struct ept_queue_head *head;
159         unsigned phys;
160         int bit, num, len, in;
161         num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
162         in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
163         item = controller.items[2 * num + in];
164         head = controller.epts + 2 * num + in;
165         phys = (unsigned)req->buf;
166         len = req->length;
167
168         item->next = TERMINATE;
169         item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
170         item->page0 = phys;
171         item->page1 = (phys & 0xfffff000) + 0x1000;
172
173         head->next = (unsigned) item;
174         head->info = 0;
175
176         DBG("ept%d %s queue len %x, buffer %x\n",
177                         num, in ? "in" : "out", len, phys);
178
179         if (in)
180                 bit = EPT_TX(num);
181         else
182                 bit = EPT_RX(num);
183
184         flush_cache(phys, len);
185         flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
186         writel(bit, &udc->epprime);
187
188         return 0;
189 }
190
191 static void handle_ep_complete(struct mv_ep *ep)
192 {
193         struct ept_queue_item *item;
194         int num, in, len;
195         num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
196         in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
197         if (num == 0)
198                 ep->desc = &ep0_out_desc;
199         item = controller.items[2 * num + in];
200
201         if (item->info & 0xff)
202                 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
203                         num, in ? "in" : "out", item->info, item->page0);
204
205         len = (item->info >> 16) & 0x7fff;
206         ep->req.length -= len;
207         DBG("ept%d %s complete %x\n",
208                         num, in ? "in" : "out", len);
209         ep->req.complete(&ep->ep, &ep->req);
210         if (num == 0) {
211                 ep->req.length = 0;
212                 usb_ep_queue(&ep->ep, &ep->req, 0);
213                 ep->desc = &ep0_in_desc;
214         }
215 }
216
217 #define SETUP(type, request) (((type) << 8) | (request))
218
219 static void handle_setup(void)
220 {
221         struct usb_request *req = &controller.ep[0].req;
222         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
223         struct ept_queue_head *head;
224         struct usb_ctrlrequest r;
225         int status = 0;
226         int num, in, _num, _in, i;
227         char *buf;
228         head = controller.epts + 2 * 0 + 0;
229
230         flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
231         memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
232         writel(EPT_RX(0), &udc->epstat);
233         DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
234             r.bRequestType, r.bRequest, r.wIndex, r.wValue);
235
236         switch (SETUP(r.bRequestType, r.bRequest)) {
237         case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
238                 _num = r.wIndex & 15;
239                 _in = !!(r.wIndex & 0x80);
240
241                 if ((r.wValue == 0) && (r.wLength == 0)) {
242                         req->length = 0;
243                         for (i = 0; i < NUM_ENDPOINTS; i++) {
244                                 if (!controller.ep[i].desc)
245                                         continue;
246                                 num = controller.ep[i].desc->bEndpointAddress
247                                                 & USB_ENDPOINT_NUMBER_MASK;
248                                 in = (controller.ep[i].desc->bEndpointAddress
249                                                 & USB_DIR_IN) != 0;
250                                 if ((num == _num) && (in == _in)) {
251                                         ep_enable(num, in);
252                                         usb_ep_queue(controller.gadget.ep0,
253                                                         req, 0);
254                                         break;
255                                 }
256                         }
257                 }
258                 return;
259
260         case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
261                 /*
262                  * write address delayed (will take effect
263                  * after the next IN txn)
264                  */
265                 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
266                 req->length = 0;
267                 usb_ep_queue(controller.gadget.ep0, req, 0);
268                 return;
269
270         case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
271                 req->length = 2;
272                 buf = (char *)req->buf;
273                 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
274                 buf[1] = 0;
275                 usb_ep_queue(controller.gadget.ep0, req, 0);
276                 return;
277         }
278         /* pass request up to the gadget driver */
279         if (controller.driver)
280                 status = controller.driver->setup(&controller.gadget, &r);
281         else
282                 status = -ENODEV;
283
284         if (!status)
285                 return;
286         DBG("STALL reqname %s type %x value %x, index %x\n",
287             reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
288         writel((1<<16) | (1 << 0), &udc->epctrl[0]);
289 }
290
291 static void stop_activity(void)
292 {
293         int i, num, in;
294         struct ept_queue_head *head;
295         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
296         writel(readl(&udc->epcomp), &udc->epcomp);
297         writel(readl(&udc->epstat), &udc->epstat);
298         writel(0xffffffff, &udc->epflush);
299
300         /* error out any pending reqs */
301         for (i = 0; i < NUM_ENDPOINTS; i++) {
302                 if (i != 0)
303                         writel(0, &udc->epctrl[i]);
304                 if (controller.ep[i].desc) {
305                         num = controller.ep[i].desc->bEndpointAddress
306                                 & USB_ENDPOINT_NUMBER_MASK;
307                         in = (controller.ep[i].desc->bEndpointAddress
308                                 & USB_DIR_IN) != 0;
309                         head = controller.epts + (num * 2) + (in);
310                         head->info = INFO_ACTIVE;
311                 }
312         }
313 }
314
315 void udc_irq(void)
316 {
317         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
318         unsigned n = readl(&udc->usbsts);
319         writel(n, &udc->usbsts);
320         int bit, i, num, in;
321
322         n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
323         if (n == 0)
324                 return;
325
326         if (n & STS_URI) {
327                 DBG("-- reset --\n");
328                 stop_activity();
329         }
330         if (n & STS_SLI)
331                 DBG("-- suspend --\n");
332
333         if (n & STS_PCI) {
334                 DBG("-- portchange --\n");
335                 bit = (readl(&udc->portsc) >> 26) & 3;
336                 if (bit == 2) {
337                         controller.gadget.speed = USB_SPEED_HIGH;
338                         for (i = 1; i < NUM_ENDPOINTS && n; i++)
339                                 if (controller.ep[i].desc)
340                                         controller.ep[i].ep.maxpacket = 512;
341                 } else {
342                         controller.gadget.speed = USB_SPEED_FULL;
343                 }
344         }
345
346         if (n & STS_UEI)
347                 printf("<UEI %x>\n", readl(&udc->epcomp));
348
349         if ((n & STS_UI) || (n & STS_UEI)) {
350                 n = readl(&udc->epstat);
351                 if (n & EPT_RX(0))
352                         handle_setup();
353
354                 n = readl(&udc->epcomp);
355                 if (n != 0)
356                         writel(n, &udc->epcomp);
357
358                 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
359                         if (controller.ep[i].desc) {
360                                 num = controller.ep[i].desc->bEndpointAddress
361                                         & USB_ENDPOINT_NUMBER_MASK;
362                                 in = (controller.ep[i].desc->bEndpointAddress
363                                                 & USB_DIR_IN) != 0;
364                                 bit = (in) ? EPT_TX(num) : EPT_RX(num);
365                                 if (n & bit)
366                                         handle_ep_complete(&controller.ep[i]);
367                         }
368                 }
369         }
370 }
371
372 int usb_gadget_handle_interrupts(void)
373 {
374         u32 value;
375         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
376
377         value = readl(&udc->usbsts);
378         if (value)
379                 udc_irq();
380
381         return value;
382 }
383
384 static int mv_pullup(struct usb_gadget *gadget, int is_on)
385 {
386         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
387         if (is_on) {
388                 /* RESET */
389                 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
390                 udelay(200);
391
392                 writel((unsigned) controller.epts, &udc->epinitaddr);
393
394                 /* select DEVICE mode */
395                 writel(USBMODE_DEVICE, &udc->usbmode);
396
397                 writel(0xffffffff, &udc->epflush);
398
399                 /* Turn on the USB connection by enabling the pullup resistor */
400                 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
401         } else {
402                 stop_activity();
403                 writel(USBCMD_FS2, &udc->usbcmd);
404                 udelay(800);
405                 if (controller.driver)
406                         controller.driver->disconnect(gadget);
407         }
408
409         return 0;
410 }
411
412 void udc_disconnect(void)
413 {
414         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
415         /* disable pullup */
416         stop_activity();
417         writel(USBCMD_FS2, &udc->usbcmd);
418         udelay(800);
419         if (controller.driver)
420                 controller.driver->disconnect(&controller.gadget);
421 }
422
423 static int mvudc_probe(void)
424 {
425         struct ept_queue_head *head;
426         int i;
427         const int num = 2 * NUM_ENDPOINTS;
428
429         controller.epts = memalign(PAGE_SIZE,
430                                    num * sizeof(struct ept_queue_head));
431         memset(controller.epts, 0, num * sizeof(struct ept_queue_head));
432         for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
433                 /*
434                  * For item0 and item1, they are served as ep0
435                  * out&in seperately
436                  */
437                 head = controller.epts + i;
438                 if (i < 2)
439                         head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
440                                 | CONFIG_ZLT | CONFIG_IOS;
441                 else
442                         head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
443                                 | CONFIG_ZLT;
444                 head->next = TERMINATE;
445                 head->info = 0;
446
447                 controller.items[i] = memalign(PAGE_SIZE,
448                                                sizeof(struct ept_queue_item));
449         }
450
451         INIT_LIST_HEAD(&controller.gadget.ep_list);
452
453         /* Init EP 0 */
454         memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
455         controller.ep[0].desc = &ep0_in_desc;
456         controller.gadget.ep0 = &controller.ep[0].ep;
457         INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
458
459         /* Init EP 1..n */
460         for (i = 1; i < NUM_ENDPOINTS; i++) {
461                 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
462                        sizeof(*mv_ep_init));
463                 list_add_tail(&controller.ep[i].ep.ep_list,
464                               &controller.gadget.ep_list);
465         }
466
467         return 0;
468 }
469
470 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
471 {
472         struct mv_udc *udc;
473         int ret;
474
475         if (!driver)
476                 return -EINVAL;
477         if (!driver->bind || !driver->setup || !driver->disconnect)
478                 return -EINVAL;
479         if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
480                 return -EINVAL;
481
482         ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
483         if (ret)
484                 return ret;
485
486         ret = mvudc_probe();
487         if (!ret) {
488                 udc = (struct mv_udc *)controller.ctrl->hcor;
489
490                 /* select ULPI phy */
491                 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
492         }
493
494         ret = driver->bind(&controller.gadget);
495         if (ret) {
496                 DBG("driver->bind() returned %d\n", ret);
497                 return ret;
498         }
499         controller.driver = driver;
500
501         return 0;
502 }
503
504 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
505 {
506         return 0;
507 }