blob: 83bd07596a6f8959cfc8f7f5eaf5aa7a4a34cb5e [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,
Troy Kisky4b4df472013-09-25 18:41:09 -0700110 .is_dualspeed = 1,
Lei Wena91ee2a2011-10-05 08:11:40 -0700111 },
112};
113
Marek Vasut9212f892013-07-10 03:16:39 +0200114/**
115 * mv_get_qh() - return queue head for endpoint
116 * @ep_num: Endpoint number
117 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
118 *
119 * This function returns the QH associated with particular endpoint
120 * and it's direction.
121 */
122static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
123{
124 return &controller.epts[(ep_num * 2) + dir_in];
125}
126
Marek Vasuta6f6b082013-07-10 03:16:41 +0200127/**
128 * mv_get_qtd() - return queue item for endpoint
129 * @ep_num: Endpoint number
130 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
131 *
132 * This function returns the QH associated with particular endpoint
133 * and it's direction.
134 */
135static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
136{
137 return controller.items[(ep_num * 2) + dir_in];
138}
139
Marek Vasut58465342013-07-10 03:16:42 +0200140/**
141 * mv_flush_qh - flush cache over queue head
142 * @ep_num: Endpoint number
143 *
144 * This function flushes cache over QH for particular endpoint.
145 */
146static void mv_flush_qh(int ep_num)
147{
148 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
149 const uint32_t start = (uint32_t)head;
150 const uint32_t end = start + 2 * sizeof(*head);
151
152 flush_dcache_range(start, end);
153}
154
155/**
156 * mv_invalidate_qh - invalidate cache over queue head
157 * @ep_num: Endpoint number
158 *
159 * This function invalidates cache over QH for particular endpoint.
160 */
161static void mv_invalidate_qh(int ep_num)
162{
163 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
164 uint32_t start = (uint32_t)head;
165 uint32_t end = start + 2 * sizeof(*head);
166
167 invalidate_dcache_range(start, end);
168}
169
170/**
171 * mv_flush_qtd - flush cache over queue item
172 * @ep_num: Endpoint number
173 *
174 * This function flushes cache over qTD pair for particular endpoint.
175 */
176static void mv_flush_qtd(int ep_num)
177{
178 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
179 const uint32_t start = (uint32_t)item;
180 const uint32_t end_raw = start + 2 * sizeof(*item);
181 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
182
183 flush_dcache_range(start, end);
184}
185
186/**
187 * mv_invalidate_qtd - invalidate cache over queue item
188 * @ep_num: Endpoint number
189 *
190 * This function invalidates cache over qTD pair for particular endpoint.
191 */
192static void mv_invalidate_qtd(int ep_num)
193{
194 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
195 const uint32_t start = (uint32_t)item;
196 const uint32_t end_raw = start + 2 * sizeof(*item);
197 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
198
199 invalidate_dcache_range(start, end);
200}
201
Lei Wena91ee2a2011-10-05 08:11:40 -0700202static struct usb_request *
203mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
204{
205 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
206 return &mv_ep->req;
207}
208
209static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
210{
211 return;
212}
213
214static void ep_enable(int num, int in)
215{
216 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200217 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700218 unsigned n;
Marek Vasut9212f892013-07-10 03:16:39 +0200219 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700220
221 n = readl(&udc->epctrl[num]);
222 if (in)
223 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
224 else
225 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
226
Marek Vasut58465342013-07-10 03:16:42 +0200227 if (num != 0) {
Lei Wena91ee2a2011-10-05 08:11:40 -0700228 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
Marek Vasut58465342013-07-10 03:16:42 +0200229 mv_flush_qh(num);
230 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700231 writel(n, &udc->epctrl[num]);
232}
233
234static int mv_ep_enable(struct usb_ep *ep,
235 const struct usb_endpoint_descriptor *desc)
236{
237 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
238 int num, in;
239 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
240 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
241 ep_enable(num, in);
242 mv_ep->desc = desc;
243 return 0;
244}
245
246static int mv_ep_disable(struct usb_ep *ep)
247{
Troy Kisky35494072013-09-25 18:41:15 -0700248 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
249
250 mv_ep->desc = NULL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700251 return 0;
252}
253
Marek Vasute0fc5822013-07-10 03:16:43 +0200254static int mv_bounce(struct mv_ep *ep)
255{
256 uint32_t addr = (uint32_t)ep->req.buf;
257 uint32_t ba;
258
259 /* Input buffer address is not aligned. */
260 if (addr & (ARCH_DMA_MINALIGN - 1))
261 goto align;
262
263 /* Input buffer length is not aligned. */
264 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
265 goto align;
266
267 /* The buffer is well aligned, only flush cache. */
268 ep->b_len = ep->req.length;
269 ep->b_buf = ep->req.buf;
270 goto flush;
271
272align:
273 /* Use internal buffer for small payloads. */
274 if (ep->req.length <= 64) {
275 ep->b_len = 64;
276 ep->b_buf = ep->b_fast;
277 } else {
278 ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN);
279 ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len);
280 if (!ep->b_buf)
281 return -ENOMEM;
282 }
283
284 memcpy(ep->b_buf, ep->req.buf, ep->req.length);
285
286flush:
287 ba = (uint32_t)ep->b_buf;
288 flush_dcache_range(ba, ba + ep->b_len);
289
290 return 0;
291}
292
293static void mv_debounce(struct mv_ep *ep)
294{
295 uint32_t addr = (uint32_t)ep->req.buf;
296 uint32_t ba = (uint32_t)ep->b_buf;
297
298 invalidate_dcache_range(ba, ba + ep->b_len);
299
300 /* Input buffer address is not aligned. */
301 if (addr & (ARCH_DMA_MINALIGN - 1))
302 goto copy;
303
304 /* Input buffer length is not aligned. */
305 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
306 goto copy;
307
308 /* The buffer is well aligned, only invalidate cache. */
309 return;
310
311copy:
312 memcpy(ep->req.buf, ep->b_buf, ep->req.length);
313
314 /* Large payloads use allocated buffer, free it. */
315 if (ep->req.length > 64)
316 free(ep->b_buf);
317}
318
Lei Wena91ee2a2011-10-05 08:11:40 -0700319static int mv_ep_queue(struct usb_ep *ep,
320 struct usb_request *req, gfp_t gfp_flags)
321{
322 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200323 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700324 struct ept_queue_item *item;
325 struct ept_queue_head *head;
Marek Vasute0fc5822013-07-10 03:16:43 +0200326 int bit, num, len, in, ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700327 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
328 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200329 item = mv_get_qtd(num, in);
Marek Vasut9212f892013-07-10 03:16:39 +0200330 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700331 len = req->length;
332
Marek Vasute0fc5822013-07-10 03:16:43 +0200333 ret = mv_bounce(mv_ep);
334 if (ret)
335 return ret;
336
Lei Wena91ee2a2011-10-05 08:11:40 -0700337 item->next = TERMINATE;
338 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
Marek Vasute0fc5822013-07-10 03:16:43 +0200339 item->page0 = (uint32_t)mv_ep->b_buf;
340 item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000;
Troy Kisky573d1312013-09-25 18:41:12 -0700341 mv_flush_qtd(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700342
343 head->next = (unsigned) item;
344 head->info = 0;
345
Marek Vasute0fc5822013-07-10 03:16:43 +0200346 DBG("ept%d %s queue len %x, buffer %p\n",
347 num, in ? "in" : "out", len, mv_ep->b_buf);
Troy Kisky573d1312013-09-25 18:41:12 -0700348 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700349
350 if (in)
351 bit = EPT_TX(num);
352 else
353 bit = EPT_RX(num);
354
Lei Wena91ee2a2011-10-05 08:11:40 -0700355 writel(bit, &udc->epprime);
356
357 return 0;
358}
359
360static void handle_ep_complete(struct mv_ep *ep)
361{
362 struct ept_queue_item *item;
363 int num, in, len;
364 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
365 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
366 if (num == 0)
367 ep->desc = &ep0_out_desc;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200368 item = mv_get_qtd(num, in);
Marek Vasut58465342013-07-10 03:16:42 +0200369 mv_invalidate_qtd(num);
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +0200370
Lei Wena91ee2a2011-10-05 08:11:40 -0700371 if (item->info & 0xff)
Troy Kisky3703c452013-09-25 18:41:08 -0700372 printf("EP%d/%s FAIL info=%x pg0=%x\n",
373 num, in ? "in" : "out", item->info, item->page0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700374
375 len = (item->info >> 16) & 0x7fff;
Marek Vasute0fc5822013-07-10 03:16:43 +0200376
377 mv_debounce(ep);
378
Lei Wena91ee2a2011-10-05 08:11:40 -0700379 ep->req.length -= len;
380 DBG("ept%d %s complete %x\n",
381 num, in ? "in" : "out", len);
382 ep->req.complete(&ep->ep, &ep->req);
383 if (num == 0) {
384 ep->req.length = 0;
385 usb_ep_queue(&ep->ep, &ep->req, 0);
386 ep->desc = &ep0_in_desc;
387 }
388}
389
390#define SETUP(type, request) (((type) << 8) | (request))
391
392static void handle_setup(void)
393{
Marek Vasut98377322013-07-10 03:16:29 +0200394 struct usb_request *req = &controller.ep[0].req;
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 struct ept_queue_head *head;
397 struct usb_ctrlrequest r;
398 int status = 0;
399 int num, in, _num, _in, i;
400 char *buf;
Marek Vasut9212f892013-07-10 03:16:39 +0200401 head = mv_get_qh(0, 0); /* EP0 OUT */
Lei Wena91ee2a2011-10-05 08:11:40 -0700402
Marek Vasut58465342013-07-10 03:16:42 +0200403 mv_invalidate_qh(0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700404 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
405 writel(EPT_RX(0), &udc->epstat);
406 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
407 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
408
409 switch (SETUP(r.bRequestType, r.bRequest)) {
410 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
411 _num = r.wIndex & 15;
412 _in = !!(r.wIndex & 0x80);
413
414 if ((r.wValue == 0) && (r.wLength == 0)) {
415 req->length = 0;
416 for (i = 0; i < NUM_ENDPOINTS; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200417 if (!controller.ep[i].desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700418 continue;
Marek Vasut98377322013-07-10 03:16:29 +0200419 num = controller.ep[i].desc->bEndpointAddress
420 & USB_ENDPOINT_NUMBER_MASK;
421 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700422 & USB_DIR_IN) != 0;
423 if ((num == _num) && (in == _in)) {
424 ep_enable(num, in);
425 usb_ep_queue(controller.gadget.ep0,
426 req, 0);
427 break;
428 }
429 }
430 }
431 return;
432
433 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
434 /*
435 * write address delayed (will take effect
436 * after the next IN txn)
437 */
438 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
439 req->length = 0;
440 usb_ep_queue(controller.gadget.ep0, req, 0);
441 return;
442
443 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
444 req->length = 2;
445 buf = (char *)req->buf;
446 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
447 buf[1] = 0;
448 usb_ep_queue(controller.gadget.ep0, req, 0);
449 return;
450 }
451 /* pass request up to the gadget driver */
452 if (controller.driver)
453 status = controller.driver->setup(&controller.gadget, &r);
454 else
455 status = -ENODEV;
456
457 if (!status)
458 return;
459 DBG("STALL reqname %s type %x value %x, index %x\n",
460 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
461 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
462}
463
464static void stop_activity(void)
465{
466 int i, num, in;
467 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200468 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700469 writel(readl(&udc->epcomp), &udc->epcomp);
470 writel(readl(&udc->epstat), &udc->epstat);
471 writel(0xffffffff, &udc->epflush);
472
473 /* error out any pending reqs */
474 for (i = 0; i < NUM_ENDPOINTS; i++) {
475 if (i != 0)
476 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200477 if (controller.ep[i].desc) {
478 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700479 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200480 in = (controller.ep[i].desc->bEndpointAddress
481 & USB_DIR_IN) != 0;
Marek Vasut9212f892013-07-10 03:16:39 +0200482 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700483 head->info = INFO_ACTIVE;
Marek Vasut58465342013-07-10 03:16:42 +0200484 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700485 }
486 }
487}
488
489void udc_irq(void)
490{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200491 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700492 unsigned n = readl(&udc->usbsts);
493 writel(n, &udc->usbsts);
494 int bit, i, num, in;
495
496 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
497 if (n == 0)
498 return;
499
500 if (n & STS_URI) {
501 DBG("-- reset --\n");
502 stop_activity();
503 }
504 if (n & STS_SLI)
505 DBG("-- suspend --\n");
506
507 if (n & STS_PCI) {
508 DBG("-- portchange --\n");
509 bit = (readl(&udc->portsc) >> 26) & 3;
510 if (bit == 2) {
511 controller.gadget.speed = USB_SPEED_HIGH;
512 for (i = 1; i < NUM_ENDPOINTS && n; i++)
Marek Vasut98377322013-07-10 03:16:29 +0200513 if (controller.ep[i].desc)
514 controller.ep[i].ep.maxpacket = 512;
Lei Wena91ee2a2011-10-05 08:11:40 -0700515 } else {
516 controller.gadget.speed = USB_SPEED_FULL;
517 }
518 }
519
520 if (n & STS_UEI)
521 printf("<UEI %x>\n", readl(&udc->epcomp));
522
523 if ((n & STS_UI) || (n & STS_UEI)) {
524 n = readl(&udc->epstat);
525 if (n & EPT_RX(0))
526 handle_setup();
527
528 n = readl(&udc->epcomp);
529 if (n != 0)
530 writel(n, &udc->epcomp);
531
532 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200533 if (controller.ep[i].desc) {
534 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700535 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200536 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700537 & USB_DIR_IN) != 0;
538 bit = (in) ? EPT_TX(num) : EPT_RX(num);
539 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200540 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700541 }
542 }
543 }
544}
545
546int usb_gadget_handle_interrupts(void)
547{
548 u32 value;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200549 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700550
551 value = readl(&udc->usbsts);
552 if (value)
553 udc_irq();
554
555 return value;
556}
557
558static int mv_pullup(struct usb_gadget *gadget, int is_on)
559{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200560 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700561 if (is_on) {
562 /* RESET */
563 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
564 udelay(200);
565
Marek Vasut9212f892013-07-10 03:16:39 +0200566 writel((unsigned)controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700567
568 /* select DEVICE mode */
569 writel(USBMODE_DEVICE, &udc->usbmode);
570
571 writel(0xffffffff, &udc->epflush);
572
573 /* Turn on the USB connection by enabling the pullup resistor */
574 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
575 } else {
576 stop_activity();
577 writel(USBCMD_FS2, &udc->usbcmd);
578 udelay(800);
579 if (controller.driver)
580 controller.driver->disconnect(gadget);
581 }
582
583 return 0;
584}
585
586void udc_disconnect(void)
587{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200588 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700589 /* disable pullup */
590 stop_activity();
591 writel(USBCMD_FS2, &udc->usbcmd);
592 udelay(800);
593 if (controller.driver)
594 controller.driver->disconnect(&controller.gadget);
595}
596
597static int mvudc_probe(void)
598{
599 struct ept_queue_head *head;
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200600 uint8_t *imem;
Lei Wena91ee2a2011-10-05 08:11:40 -0700601 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200602
Marek Vasut806d76c2013-07-10 03:16:34 +0200603 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700604
Marek Vasut456707d2013-07-10 03:16:37 +0200605 const int eplist_min_align = 4096;
606 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
607 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
608 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
609
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200610 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
611 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
612 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
613 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
614
Marek Vasut456707d2013-07-10 03:16:37 +0200615 /* The QH list must be aligned to 4096 bytes. */
616 controller.epts = memalign(eplist_align, eplist_sz);
617 if (!controller.epts)
618 return -ENOMEM;
619 memset(controller.epts, 0, eplist_sz);
620
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200621 /*
622 * Each qTD item must be 32-byte aligned, each qTD touple must be
623 * cacheline aligned. There are two qTD items for each endpoint and
624 * only one of them is used for the endpoint at time, so we can group
625 * them together.
626 */
627 controller.items_mem = memalign(ilist_align, ilist_sz);
628 if (!controller.items_mem) {
629 free(controller.epts);
630 return -ENOMEM;
631 }
Troy Kisky0dee8ce2013-09-25 18:41:14 -0700632 memset(controller.items_mem, 0, ilist_sz);
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200633
Lei Wena91ee2a2011-10-05 08:11:40 -0700634 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
635 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200636 * Configure QH for each endpoint. The structure of the QH list
637 * is such that each two subsequent fields, N and N+1 where N is
638 * even, in the QH list represent QH for one endpoint. The Nth
639 * entry represents OUT configuration and the N+1th entry does
640 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700641 */
Marek Vasut393004e2013-07-10 03:16:36 +0200642 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700643 if (i < 2)
644 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
645 | CONFIG_ZLT | CONFIG_IOS;
646 else
647 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
648 | CONFIG_ZLT;
649 head->next = TERMINATE;
650 head->info = 0;
651
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200652 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
653 if (i & 1)
654 imem += sizeof(struct ept_queue_item);
655
656 controller.items[i] = (struct ept_queue_item *)imem;
Marek Vasut58465342013-07-10 03:16:42 +0200657
658 if (i & 1) {
659 mv_flush_qh(i - 1);
660 mv_flush_qtd(i - 1);
661 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700662 }
663
664 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200665
666 /* Init EP 0 */
667 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut98377322013-07-10 03:16:29 +0200668 controller.ep[0].desc = &ep0_in_desc;
Marek Vasute1263412013-07-10 03:16:30 +0200669 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700670 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200671
672 /* Init EP 1..n */
673 for (i = 1; i < NUM_ENDPOINTS; i++) {
674 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
675 sizeof(*mv_ep_init));
676 list_add_tail(&controller.ep[i].ep.ep_list,
677 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700678 }
Marek Vasute1263412013-07-10 03:16:30 +0200679
Lei Wena91ee2a2011-10-05 08:11:40 -0700680 return 0;
681}
682
683int usb_gadget_register_driver(struct usb_gadget_driver *driver)
684{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200685 struct mv_udc *udc;
686 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700687
Marek Vasutfe67fe72013-07-10 03:16:33 +0200688 if (!driver)
689 return -EINVAL;
690 if (!driver->bind || !driver->setup || !driver->disconnect)
691 return -EINVAL;
692 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700693 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700694
Troy Kisky8f9c49d2013-10-10 15:27:56 -0700695 ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200696 if (ret)
697 return ret;
698
699 ret = mvudc_probe();
700 if (!ret) {
701 udc = (struct mv_udc *)controller.ctrl->hcor;
702
Lei Wena91ee2a2011-10-05 08:11:40 -0700703 /* select ULPI phy */
704 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
705 }
Marek Vasut1f4b4912013-07-10 03:16:32 +0200706
707 ret = driver->bind(&controller.gadget);
708 if (ret) {
709 DBG("driver->bind() returned %d\n", ret);
710 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700711 }
712 controller.driver = driver;
713
714 return 0;
715}
716
717int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
718{
719 return 0;
720}