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