blob: 8a5593ae0a0c38828504a70a6a5973d8adc4e3d4 [file] [log] [blame]
Lei Wena91ee2a2011-10-05 08:11:40 -07001/*
2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lei Wena91ee2a2011-10-05 08:11:40 -07006 *
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
Marek Vasut1edb9992013-07-10 03:16:27 +020020#if CONFIG_USB_MAX_CONTROLLER_COUNT > 1
21#error This driver only supports one single controller.
22#endif
23
Marek Vasut9d64ac92013-07-10 03:16:38 +020024/*
25 * Check if the system has too long cachelines. If the cachelines are
26 * longer then 128b, the driver will not be able flush/invalidate data
27 * cache over separate QH entries. We use 128b because one QH entry is
28 * 64b long and there are always two QH list entries for each endpoint.
29 */
30#if ARCH_DMA_MINALIGN > 128
31#error This driver can not work on systems with caches longer than 128b
32#endif
33
Lei Wena91ee2a2011-10-05 08:11:40 -070034#ifndef DEBUG
35#define DBG(x...) do {} while (0)
36#else
37#define DBG(x...) printf(x)
38static const char *reqname(unsigned r)
39{
40 switch (r) {
41 case USB_REQ_GET_STATUS: return "GET_STATUS";
42 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
43 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
44 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
45 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
46 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
47 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
48 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
49 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
50 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
51 default: return "*UNKNOWN*";
52 }
53}
54#endif
55
Lei Wena91ee2a2011-10-05 08:11:40 -070056static struct usb_endpoint_descriptor ep0_out_desc = {
57 .bLength = sizeof(struct usb_endpoint_descriptor),
58 .bDescriptorType = USB_DT_ENDPOINT,
59 .bEndpointAddress = 0,
60 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
61};
62
63static struct usb_endpoint_descriptor ep0_in_desc = {
64 .bLength = sizeof(struct usb_endpoint_descriptor),
65 .bDescriptorType = USB_DT_ENDPOINT,
66 .bEndpointAddress = USB_DIR_IN,
67 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
68};
69
Lei Wena91ee2a2011-10-05 08:11:40 -070070static int mv_pullup(struct usb_gadget *gadget, int is_on);
71static int mv_ep_enable(struct usb_ep *ep,
72 const struct usb_endpoint_descriptor *desc);
73static int mv_ep_disable(struct usb_ep *ep);
74static int mv_ep_queue(struct usb_ep *ep,
75 struct usb_request *req, gfp_t gfp_flags);
76static struct usb_request *
77mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
78static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
79
80static struct usb_gadget_ops mv_udc_ops = {
81 .pullup = mv_pullup,
82};
83
84static struct usb_ep_ops mv_ep_ops = {
85 .enable = mv_ep_enable,
86 .disable = mv_ep_disable,
87 .queue = mv_ep_queue,
88 .alloc_request = mv_ep_alloc_request,
89 .free_request = mv_ep_free_request,
90};
91
Marek Vasute1263412013-07-10 03:16:30 +020092/* Init values for USB endpoints. */
93static const struct usb_ep mv_ep_init[2] = {
94 [0] = { /* EP 0 */
95 .maxpacket = 64,
96 .name = "ep0",
97 .ops = &mv_ep_ops,
98 },
99 [1] = { /* EP 1..n */
100 .maxpacket = 512,
101 .name = "ep-",
102 .ops = &mv_ep_ops,
103 },
104};
105
Lei Wena91ee2a2011-10-05 08:11:40 -0700106static struct mv_drv controller = {
Marek Vasutbe0b0e72013-07-10 03:16:35 +0200107 .gadget = {
108 .name = "mv_udc",
109 .ops = &mv_udc_ops,
Lei Wena91ee2a2011-10-05 08:11:40 -0700110 },
111};
112
Marek Vasut9212f892013-07-10 03:16:39 +0200113/**
114 * mv_get_qh() - return queue head for endpoint
115 * @ep_num: Endpoint number
116 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
117 *
118 * This function returns the QH associated with particular endpoint
119 * and it's direction.
120 */
121static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
122{
123 return &controller.epts[(ep_num * 2) + dir_in];
124}
125
Marek Vasuta6f6b082013-07-10 03:16:41 +0200126/**
127 * mv_get_qtd() - return queue item for endpoint
128 * @ep_num: Endpoint number
129 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
130 *
131 * This function returns the QH associated with particular endpoint
132 * and it's direction.
133 */
134static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
135{
136 return controller.items[(ep_num * 2) + dir_in];
137}
138
Marek Vasut58465342013-07-10 03:16:42 +0200139/**
140 * mv_flush_qh - flush cache over queue head
141 * @ep_num: Endpoint number
142 *
143 * This function flushes cache over QH for particular endpoint.
144 */
145static void mv_flush_qh(int ep_num)
146{
147 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
148 const uint32_t start = (uint32_t)head;
149 const uint32_t end = start + 2 * sizeof(*head);
150
151 flush_dcache_range(start, end);
152}
153
154/**
155 * mv_invalidate_qh - invalidate cache over queue head
156 * @ep_num: Endpoint number
157 *
158 * This function invalidates cache over QH for particular endpoint.
159 */
160static void mv_invalidate_qh(int ep_num)
161{
162 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
163 uint32_t start = (uint32_t)head;
164 uint32_t end = start + 2 * sizeof(*head);
165
166 invalidate_dcache_range(start, end);
167}
168
169/**
170 * mv_flush_qtd - flush cache over queue item
171 * @ep_num: Endpoint number
172 *
173 * This function flushes cache over qTD pair for particular endpoint.
174 */
175static void mv_flush_qtd(int ep_num)
176{
177 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
178 const uint32_t start = (uint32_t)item;
179 const uint32_t end_raw = start + 2 * sizeof(*item);
180 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
181
182 flush_dcache_range(start, end);
183}
184
185/**
186 * mv_invalidate_qtd - invalidate cache over queue item
187 * @ep_num: Endpoint number
188 *
189 * This function invalidates cache over qTD pair for particular endpoint.
190 */
191static void mv_invalidate_qtd(int ep_num)
192{
193 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
194 const uint32_t start = (uint32_t)item;
195 const uint32_t end_raw = start + 2 * sizeof(*item);
196 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
197
198 invalidate_dcache_range(start, end);
199}
200
Lei Wena91ee2a2011-10-05 08:11:40 -0700201static struct usb_request *
202mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
203{
204 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
205 return &mv_ep->req;
206}
207
208static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
209{
210 return;
211}
212
213static void ep_enable(int num, int in)
214{
215 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200216 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700217 unsigned n;
Marek Vasut9212f892013-07-10 03:16:39 +0200218 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700219
220 n = readl(&udc->epctrl[num]);
221 if (in)
222 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
223 else
224 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
225
Marek Vasut58465342013-07-10 03:16:42 +0200226 if (num != 0) {
Lei Wena91ee2a2011-10-05 08:11:40 -0700227 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
Marek Vasut58465342013-07-10 03:16:42 +0200228 mv_flush_qh(num);
229 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700230 writel(n, &udc->epctrl[num]);
231}
232
233static int mv_ep_enable(struct usb_ep *ep,
234 const struct usb_endpoint_descriptor *desc)
235{
236 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
237 int num, in;
238 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
239 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
240 ep_enable(num, in);
241 mv_ep->desc = desc;
242 return 0;
243}
244
245static int mv_ep_disable(struct usb_ep *ep)
246{
247 return 0;
248}
249
250static int mv_ep_queue(struct usb_ep *ep,
251 struct usb_request *req, gfp_t gfp_flags)
252{
253 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200254 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700255 struct ept_queue_item *item;
256 struct ept_queue_head *head;
257 unsigned phys;
258 int bit, num, len, in;
259 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
260 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200261 item = mv_get_qtd(num, in);
Marek Vasut9212f892013-07-10 03:16:39 +0200262 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700263 phys = (unsigned)req->buf;
264 len = req->length;
265
266 item->next = TERMINATE;
267 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
268 item->page0 = phys;
269 item->page1 = (phys & 0xfffff000) + 0x1000;
270
271 head->next = (unsigned) item;
272 head->info = 0;
273
274 DBG("ept%d %s queue len %x, buffer %x\n",
275 num, in ? "in" : "out", len, phys);
276
277 if (in)
278 bit = EPT_TX(num);
279 else
280 bit = EPT_RX(num);
281
Marek Vasut58465342013-07-10 03:16:42 +0200282 mv_flush_qh(num);
283 mv_flush_qtd(num);
284
Lei Wena91ee2a2011-10-05 08:11:40 -0700285 writel(bit, &udc->epprime);
286
287 return 0;
288}
289
290static void handle_ep_complete(struct mv_ep *ep)
291{
292 struct ept_queue_item *item;
293 int num, in, len;
294 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
295 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
296 if (num == 0)
297 ep->desc = &ep0_out_desc;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200298 item = mv_get_qtd(num, in);
Marek Vasut58465342013-07-10 03:16:42 +0200299 mv_invalidate_qtd(num);
300
Lei Wena91ee2a2011-10-05 08:11:40 -0700301 if (item->info & 0xff)
302 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
303 num, in ? "in" : "out", item->info, item->page0);
304
305 len = (item->info >> 16) & 0x7fff;
306 ep->req.length -= len;
307 DBG("ept%d %s complete %x\n",
308 num, in ? "in" : "out", len);
309 ep->req.complete(&ep->ep, &ep->req);
310 if (num == 0) {
311 ep->req.length = 0;
312 usb_ep_queue(&ep->ep, &ep->req, 0);
313 ep->desc = &ep0_in_desc;
314 }
315}
316
317#define SETUP(type, request) (((type) << 8) | (request))
318
319static void handle_setup(void)
320{
Marek Vasut98377322013-07-10 03:16:29 +0200321 struct usb_request *req = &controller.ep[0].req;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200322 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700323 struct ept_queue_head *head;
324 struct usb_ctrlrequest r;
325 int status = 0;
326 int num, in, _num, _in, i;
327 char *buf;
Marek Vasut9212f892013-07-10 03:16:39 +0200328 head = mv_get_qh(0, 0); /* EP0 OUT */
Lei Wena91ee2a2011-10-05 08:11:40 -0700329
Marek Vasut58465342013-07-10 03:16:42 +0200330 mv_invalidate_qh(0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700331 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
332 writel(EPT_RX(0), &udc->epstat);
333 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
334 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
335
336 switch (SETUP(r.bRequestType, r.bRequest)) {
337 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
338 _num = r.wIndex & 15;
339 _in = !!(r.wIndex & 0x80);
340
341 if ((r.wValue == 0) && (r.wLength == 0)) {
342 req->length = 0;
343 for (i = 0; i < NUM_ENDPOINTS; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200344 if (!controller.ep[i].desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700345 continue;
Marek Vasut98377322013-07-10 03:16:29 +0200346 num = controller.ep[i].desc->bEndpointAddress
347 & USB_ENDPOINT_NUMBER_MASK;
348 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700349 & USB_DIR_IN) != 0;
350 if ((num == _num) && (in == _in)) {
351 ep_enable(num, in);
352 usb_ep_queue(controller.gadget.ep0,
353 req, 0);
354 break;
355 }
356 }
357 }
358 return;
359
360 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
361 /*
362 * write address delayed (will take effect
363 * after the next IN txn)
364 */
365 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
366 req->length = 0;
367 usb_ep_queue(controller.gadget.ep0, req, 0);
368 return;
369
370 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
371 req->length = 2;
372 buf = (char *)req->buf;
373 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
374 buf[1] = 0;
375 usb_ep_queue(controller.gadget.ep0, req, 0);
376 return;
377 }
378 /* pass request up to the gadget driver */
379 if (controller.driver)
380 status = controller.driver->setup(&controller.gadget, &r);
381 else
382 status = -ENODEV;
383
384 if (!status)
385 return;
386 DBG("STALL reqname %s type %x value %x, index %x\n",
387 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
388 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
389}
390
391static void stop_activity(void)
392{
393 int i, num, in;
394 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200395 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700396 writel(readl(&udc->epcomp), &udc->epcomp);
397 writel(readl(&udc->epstat), &udc->epstat);
398 writel(0xffffffff, &udc->epflush);
399
400 /* error out any pending reqs */
401 for (i = 0; i < NUM_ENDPOINTS; i++) {
402 if (i != 0)
403 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200404 if (controller.ep[i].desc) {
405 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700406 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200407 in = (controller.ep[i].desc->bEndpointAddress
408 & USB_DIR_IN) != 0;
Marek Vasut9212f892013-07-10 03:16:39 +0200409 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700410 head->info = INFO_ACTIVE;
Marek Vasut58465342013-07-10 03:16:42 +0200411 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700412 }
413 }
414}
415
416void udc_irq(void)
417{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200418 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700419 unsigned n = readl(&udc->usbsts);
420 writel(n, &udc->usbsts);
421 int bit, i, num, in;
422
423 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
424 if (n == 0)
425 return;
426
427 if (n & STS_URI) {
428 DBG("-- reset --\n");
429 stop_activity();
430 }
431 if (n & STS_SLI)
432 DBG("-- suspend --\n");
433
434 if (n & STS_PCI) {
435 DBG("-- portchange --\n");
436 bit = (readl(&udc->portsc) >> 26) & 3;
437 if (bit == 2) {
438 controller.gadget.speed = USB_SPEED_HIGH;
439 for (i = 1; i < NUM_ENDPOINTS && n; i++)
Marek Vasut98377322013-07-10 03:16:29 +0200440 if (controller.ep[i].desc)
441 controller.ep[i].ep.maxpacket = 512;
Lei Wena91ee2a2011-10-05 08:11:40 -0700442 } else {
443 controller.gadget.speed = USB_SPEED_FULL;
444 }
445 }
446
447 if (n & STS_UEI)
448 printf("<UEI %x>\n", readl(&udc->epcomp));
449
450 if ((n & STS_UI) || (n & STS_UEI)) {
451 n = readl(&udc->epstat);
452 if (n & EPT_RX(0))
453 handle_setup();
454
455 n = readl(&udc->epcomp);
456 if (n != 0)
457 writel(n, &udc->epcomp);
458
459 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200460 if (controller.ep[i].desc) {
461 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700462 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200463 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700464 & USB_DIR_IN) != 0;
465 bit = (in) ? EPT_TX(num) : EPT_RX(num);
466 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200467 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700468 }
469 }
470 }
471}
472
473int usb_gadget_handle_interrupts(void)
474{
475 u32 value;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200476 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700477
478 value = readl(&udc->usbsts);
479 if (value)
480 udc_irq();
481
482 return value;
483}
484
485static int mv_pullup(struct usb_gadget *gadget, int is_on)
486{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200487 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700488 if (is_on) {
489 /* RESET */
490 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
491 udelay(200);
492
Marek Vasut9212f892013-07-10 03:16:39 +0200493 writel((unsigned)controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700494
495 /* select DEVICE mode */
496 writel(USBMODE_DEVICE, &udc->usbmode);
497
498 writel(0xffffffff, &udc->epflush);
499
500 /* Turn on the USB connection by enabling the pullup resistor */
501 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
502 } else {
503 stop_activity();
504 writel(USBCMD_FS2, &udc->usbcmd);
505 udelay(800);
506 if (controller.driver)
507 controller.driver->disconnect(gadget);
508 }
509
510 return 0;
511}
512
513void udc_disconnect(void)
514{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200515 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700516 /* disable pullup */
517 stop_activity();
518 writel(USBCMD_FS2, &udc->usbcmd);
519 udelay(800);
520 if (controller.driver)
521 controller.driver->disconnect(&controller.gadget);
522}
523
524static int mvudc_probe(void)
525{
526 struct ept_queue_head *head;
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200527 uint8_t *imem;
Lei Wena91ee2a2011-10-05 08:11:40 -0700528 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200529
Marek Vasut806d76c2013-07-10 03:16:34 +0200530 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700531
Marek Vasut456707d2013-07-10 03:16:37 +0200532 const int eplist_min_align = 4096;
533 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
534 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
535 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
536
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200537 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
538 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
539 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
540 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
541
Marek Vasut456707d2013-07-10 03:16:37 +0200542 /* The QH list must be aligned to 4096 bytes. */
543 controller.epts = memalign(eplist_align, eplist_sz);
544 if (!controller.epts)
545 return -ENOMEM;
546 memset(controller.epts, 0, eplist_sz);
547
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200548 /*
549 * Each qTD item must be 32-byte aligned, each qTD touple must be
550 * cacheline aligned. There are two qTD items for each endpoint and
551 * only one of them is used for the endpoint at time, so we can group
552 * them together.
553 */
554 controller.items_mem = memalign(ilist_align, ilist_sz);
555 if (!controller.items_mem) {
556 free(controller.epts);
557 return -ENOMEM;
558 }
559
Lei Wena91ee2a2011-10-05 08:11:40 -0700560 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
561 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200562 * Configure QH for each endpoint. The structure of the QH list
563 * is such that each two subsequent fields, N and N+1 where N is
564 * even, in the QH list represent QH for one endpoint. The Nth
565 * entry represents OUT configuration and the N+1th entry does
566 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700567 */
Marek Vasut393004e2013-07-10 03:16:36 +0200568 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700569 if (i < 2)
570 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
571 | CONFIG_ZLT | CONFIG_IOS;
572 else
573 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
574 | CONFIG_ZLT;
575 head->next = TERMINATE;
576 head->info = 0;
577
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200578 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
579 if (i & 1)
580 imem += sizeof(struct ept_queue_item);
581
582 controller.items[i] = (struct ept_queue_item *)imem;
Marek Vasut58465342013-07-10 03:16:42 +0200583
584 if (i & 1) {
585 mv_flush_qh(i - 1);
586 mv_flush_qtd(i - 1);
587 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700588 }
589
590 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200591
592 /* Init EP 0 */
593 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut98377322013-07-10 03:16:29 +0200594 controller.ep[0].desc = &ep0_in_desc;
Marek Vasute1263412013-07-10 03:16:30 +0200595 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700596 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200597
598 /* Init EP 1..n */
599 for (i = 1; i < NUM_ENDPOINTS; i++) {
600 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
601 sizeof(*mv_ep_init));
602 list_add_tail(&controller.ep[i].ep.ep_list,
603 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700604 }
Marek Vasute1263412013-07-10 03:16:30 +0200605
Lei Wena91ee2a2011-10-05 08:11:40 -0700606 return 0;
607}
608
609int usb_gadget_register_driver(struct usb_gadget_driver *driver)
610{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200611 struct mv_udc *udc;
612 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700613
Marek Vasutfe67fe72013-07-10 03:16:33 +0200614 if (!driver)
615 return -EINVAL;
616 if (!driver->bind || !driver->setup || !driver->disconnect)
617 return -EINVAL;
618 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700619 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700620
Marek Vasut1f4b4912013-07-10 03:16:32 +0200621 ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
622 if (ret)
623 return ret;
624
625 ret = mvudc_probe();
626 if (!ret) {
627 udc = (struct mv_udc *)controller.ctrl->hcor;
628
Lei Wena91ee2a2011-10-05 08:11:40 -0700629 /* select ULPI phy */
630 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
631 }
Marek Vasut1f4b4912013-07-10 03:16:32 +0200632
633 ret = driver->bind(&controller.gadget);
634 if (ret) {
635 DBG("driver->bind() returned %d\n", ret);
636 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700637 }
638 controller.driver = driver;
639
640 return 0;
641}
642
643int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
644{
645 return 0;
646}