blob: 1c5760535e37a42ceeaed80e4ccf3fd26daf3482 [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>
Troy Kisky256a3172013-10-10 15:28:00 -070017#include <asm/unaligned.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070018#include <linux/types.h>
19#include <usb/mv_udc.h>
20
Marek Vasut9d64ac92013-07-10 03:16:38 +020021/*
22 * Check if the system has too long cachelines. If the cachelines are
23 * longer then 128b, the driver will not be able flush/invalidate data
24 * cache over separate QH entries. We use 128b because one QH entry is
25 * 64b long and there are always two QH list entries for each endpoint.
26 */
27#if ARCH_DMA_MINALIGN > 128
28#error This driver can not work on systems with caches longer than 128b
29#endif
30
Lei Wena91ee2a2011-10-05 08:11:40 -070031#ifndef DEBUG
32#define DBG(x...) do {} while (0)
33#else
34#define DBG(x...) printf(x)
35static const char *reqname(unsigned r)
36{
37 switch (r) {
38 case USB_REQ_GET_STATUS: return "GET_STATUS";
39 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
40 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
41 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
42 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
43 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
44 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
45 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
46 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
47 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
48 default: return "*UNKNOWN*";
49 }
50}
51#endif
52
Lei Wena91ee2a2011-10-05 08:11:40 -070053static struct usb_endpoint_descriptor ep0_out_desc = {
54 .bLength = sizeof(struct usb_endpoint_descriptor),
55 .bDescriptorType = USB_DT_ENDPOINT,
56 .bEndpointAddress = 0,
57 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
58};
59
60static struct usb_endpoint_descriptor ep0_in_desc = {
61 .bLength = sizeof(struct usb_endpoint_descriptor),
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = USB_DIR_IN,
64 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
65};
66
Lei Wena91ee2a2011-10-05 08:11:40 -070067static int mv_pullup(struct usb_gadget *gadget, int is_on);
68static int mv_ep_enable(struct usb_ep *ep,
69 const struct usb_endpoint_descriptor *desc);
70static int mv_ep_disable(struct usb_ep *ep);
71static int mv_ep_queue(struct usb_ep *ep,
72 struct usb_request *req, gfp_t gfp_flags);
73static struct usb_request *
74mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
75static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
76
77static struct usb_gadget_ops mv_udc_ops = {
78 .pullup = mv_pullup,
79};
80
81static struct usb_ep_ops mv_ep_ops = {
82 .enable = mv_ep_enable,
83 .disable = mv_ep_disable,
84 .queue = mv_ep_queue,
85 .alloc_request = mv_ep_alloc_request,
86 .free_request = mv_ep_free_request,
87};
88
Marek Vasute1263412013-07-10 03:16:30 +020089/* Init values for USB endpoints. */
90static const struct usb_ep mv_ep_init[2] = {
91 [0] = { /* EP 0 */
92 .maxpacket = 64,
93 .name = "ep0",
94 .ops = &mv_ep_ops,
95 },
96 [1] = { /* EP 1..n */
97 .maxpacket = 512,
98 .name = "ep-",
99 .ops = &mv_ep_ops,
100 },
101};
102
Lei Wena91ee2a2011-10-05 08:11:40 -0700103static struct mv_drv controller = {
Marek Vasutbe0b0e72013-07-10 03:16:35 +0200104 .gadget = {
105 .name = "mv_udc",
106 .ops = &mv_udc_ops,
Troy Kisky4b4df472013-09-25 18:41:09 -0700107 .is_dualspeed = 1,
Lei Wena91ee2a2011-10-05 08:11:40 -0700108 },
109};
110
Marek Vasut9212f892013-07-10 03:16:39 +0200111/**
112 * mv_get_qh() - return queue head for endpoint
113 * @ep_num: Endpoint number
114 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
115 *
116 * This function returns the QH associated with particular endpoint
117 * and it's direction.
118 */
119static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
120{
121 return &controller.epts[(ep_num * 2) + dir_in];
122}
123
Marek Vasuta6f6b082013-07-10 03:16:41 +0200124/**
125 * mv_get_qtd() - return queue item for endpoint
126 * @ep_num: Endpoint number
127 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
128 *
129 * This function returns the QH associated with particular endpoint
130 * and it's direction.
131 */
132static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
133{
134 return controller.items[(ep_num * 2) + dir_in];
135}
136
Marek Vasut58465342013-07-10 03:16:42 +0200137/**
138 * mv_flush_qh - flush cache over queue head
139 * @ep_num: Endpoint number
140 *
141 * This function flushes cache over QH for particular endpoint.
142 */
143static void mv_flush_qh(int ep_num)
144{
145 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
146 const uint32_t start = (uint32_t)head;
147 const uint32_t end = start + 2 * sizeof(*head);
148
149 flush_dcache_range(start, end);
150}
151
152/**
153 * mv_invalidate_qh - invalidate cache over queue head
154 * @ep_num: Endpoint number
155 *
156 * This function invalidates cache over QH for particular endpoint.
157 */
158static void mv_invalidate_qh(int ep_num)
159{
160 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
161 uint32_t start = (uint32_t)head;
162 uint32_t end = start + 2 * sizeof(*head);
163
164 invalidate_dcache_range(start, end);
165}
166
167/**
168 * mv_flush_qtd - flush cache over queue item
169 * @ep_num: Endpoint number
170 *
171 * This function flushes cache over qTD pair for particular endpoint.
172 */
173static void mv_flush_qtd(int ep_num)
174{
175 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
176 const uint32_t start = (uint32_t)item;
177 const uint32_t end_raw = start + 2 * sizeof(*item);
178 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
179
180 flush_dcache_range(start, end);
181}
182
183/**
184 * mv_invalidate_qtd - invalidate cache over queue item
185 * @ep_num: Endpoint number
186 *
187 * This function invalidates cache over qTD pair for particular endpoint.
188 */
189static void mv_invalidate_qtd(int ep_num)
190{
191 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
192 const uint32_t start = (uint32_t)item;
193 const uint32_t end_raw = start + 2 * sizeof(*item);
194 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
195
196 invalidate_dcache_range(start, end);
197}
198
Lei Wena91ee2a2011-10-05 08:11:40 -0700199static struct usb_request *
200mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
201{
202 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
203 return &mv_ep->req;
204}
205
206static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
207{
208 return;
209}
210
Troy Kisky256a3172013-10-10 15:28:00 -0700211static void ep_enable(int num, int in, int maxpacket)
Lei Wena91ee2a2011-10-05 08:11:40 -0700212{
213 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200214 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700215 unsigned n;
Marek Vasut9212f892013-07-10 03:16:39 +0200216 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700217
218 n = readl(&udc->epctrl[num]);
219 if (in)
220 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
221 else
222 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
223
Marek Vasut58465342013-07-10 03:16:42 +0200224 if (num != 0) {
Troy Kisky256a3172013-10-10 15:28:00 -0700225 head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
Marek Vasut58465342013-07-10 03:16:42 +0200226 mv_flush_qh(num);
227 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700228 writel(n, &udc->epctrl[num]);
229}
230
231static int mv_ep_enable(struct usb_ep *ep,
232 const struct usb_endpoint_descriptor *desc)
233{
234 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
235 int num, in;
236 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
237 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
Lei Wena91ee2a2011-10-05 08:11:40 -0700238 mv_ep->desc = desc;
Troy Kisky256a3172013-10-10 15:28:00 -0700239
240 if (num) {
241 int max = get_unaligned_le16(&desc->wMaxPacketSize);
242
243 if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
244 max = 64;
245 if (ep->maxpacket != max) {
246 DBG("%s: from %d to %d\n", __func__,
247 ep->maxpacket, max);
248 ep->maxpacket = max;
249 }
250 }
251 ep_enable(num, in, ep->maxpacket);
252 DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700253 return 0;
254}
255
256static int mv_ep_disable(struct usb_ep *ep)
257{
Troy Kisky35494072013-09-25 18:41:15 -0700258 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
259
260 mv_ep->desc = NULL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700261 return 0;
262}
263
Troy Kisky1567b882013-10-10 15:28:01 -0700264static int mv_bounce(struct mv_ep *ep, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200265{
266 uint32_t addr = (uint32_t)ep->req.buf;
267 uint32_t ba;
268
269 /* Input buffer address is not aligned. */
270 if (addr & (ARCH_DMA_MINALIGN - 1))
271 goto align;
272
273 /* Input buffer length is not aligned. */
274 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
275 goto align;
276
277 /* The buffer is well aligned, only flush cache. */
278 ep->b_len = ep->req.length;
279 ep->b_buf = ep->req.buf;
280 goto flush;
281
282align:
283 /* Use internal buffer for small payloads. */
284 if (ep->req.length <= 64) {
285 ep->b_len = 64;
286 ep->b_buf = ep->b_fast;
287 } else {
288 ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN);
289 ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len);
290 if (!ep->b_buf)
291 return -ENOMEM;
292 }
Troy Kisky1567b882013-10-10 15:28:01 -0700293 if (in)
294 memcpy(ep->b_buf, ep->req.buf, ep->req.length);
Marek Vasute0fc5822013-07-10 03:16:43 +0200295
296flush:
297 ba = (uint32_t)ep->b_buf;
298 flush_dcache_range(ba, ba + ep->b_len);
299
300 return 0;
301}
302
Troy Kisky1567b882013-10-10 15:28:01 -0700303static void mv_debounce(struct mv_ep *ep, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200304{
305 uint32_t addr = (uint32_t)ep->req.buf;
306 uint32_t ba = (uint32_t)ep->b_buf;
307
Troy Kisky1567b882013-10-10 15:28:01 -0700308 if (in) {
309 if (addr == ba)
310 return; /* not a bounce */
311 goto free;
312 }
Marek Vasute0fc5822013-07-10 03:16:43 +0200313 invalidate_dcache_range(ba, ba + ep->b_len);
314
Troy Kisky1567b882013-10-10 15:28:01 -0700315 if (addr == ba)
316 return; /* not a bounce */
Marek Vasute0fc5822013-07-10 03:16:43 +0200317
Marek Vasute0fc5822013-07-10 03:16:43 +0200318 memcpy(ep->req.buf, ep->b_buf, ep->req.length);
Troy Kisky1567b882013-10-10 15:28:01 -0700319free:
Marek Vasute0fc5822013-07-10 03:16:43 +0200320 /* Large payloads use allocated buffer, free it. */
Troy Kisky1567b882013-10-10 15:28:01 -0700321 if (ep->b_buf != ep->b_fast)
Marek Vasute0fc5822013-07-10 03:16:43 +0200322 free(ep->b_buf);
323}
324
Lei Wena91ee2a2011-10-05 08:11:40 -0700325static int mv_ep_queue(struct usb_ep *ep,
326 struct usb_request *req, gfp_t gfp_flags)
327{
328 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200329 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700330 struct ept_queue_item *item;
331 struct ept_queue_head *head;
Marek Vasute0fc5822013-07-10 03:16:43 +0200332 int bit, num, len, in, ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700333 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
334 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200335 item = mv_get_qtd(num, in);
Marek Vasut9212f892013-07-10 03:16:39 +0200336 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700337 len = req->length;
338
Troy Kisky1567b882013-10-10 15:28:01 -0700339 ret = mv_bounce(mv_ep, in);
Marek Vasute0fc5822013-07-10 03:16:43 +0200340 if (ret)
341 return ret;
342
Lei Wena91ee2a2011-10-05 08:11:40 -0700343 item->next = TERMINATE;
344 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
Marek Vasute0fc5822013-07-10 03:16:43 +0200345 item->page0 = (uint32_t)mv_ep->b_buf;
346 item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000;
Troy Kisky573d1312013-09-25 18:41:12 -0700347 mv_flush_qtd(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700348
349 head->next = (unsigned) item;
350 head->info = 0;
351
Marek Vasute0fc5822013-07-10 03:16:43 +0200352 DBG("ept%d %s queue len %x, buffer %p\n",
353 num, in ? "in" : "out", len, mv_ep->b_buf);
Troy Kisky573d1312013-09-25 18:41:12 -0700354 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700355
356 if (in)
357 bit = EPT_TX(num);
358 else
359 bit = EPT_RX(num);
360
Lei Wena91ee2a2011-10-05 08:11:40 -0700361 writel(bit, &udc->epprime);
362
363 return 0;
364}
365
366static void handle_ep_complete(struct mv_ep *ep)
367{
368 struct ept_queue_item *item;
369 int num, in, len;
370 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
371 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
372 if (num == 0)
373 ep->desc = &ep0_out_desc;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200374 item = mv_get_qtd(num, in);
Marek Vasut58465342013-07-10 03:16:42 +0200375 mv_invalidate_qtd(num);
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +0200376
Lei Wena91ee2a2011-10-05 08:11:40 -0700377 if (item->info & 0xff)
Troy Kisky3703c452013-09-25 18:41:08 -0700378 printf("EP%d/%s FAIL info=%x pg0=%x\n",
379 num, in ? "in" : "out", item->info, item->page0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700380
381 len = (item->info >> 16) & 0x7fff;
382 ep->req.length -= len;
Troy Kisky1567b882013-10-10 15:28:01 -0700383 mv_debounce(ep, in);
384
Lei Wena91ee2a2011-10-05 08:11:40 -0700385 DBG("ept%d %s complete %x\n",
386 num, in ? "in" : "out", len);
387 ep->req.complete(&ep->ep, &ep->req);
388 if (num == 0) {
389 ep->req.length = 0;
390 usb_ep_queue(&ep->ep, &ep->req, 0);
391 ep->desc = &ep0_in_desc;
392 }
393}
394
395#define SETUP(type, request) (((type) << 8) | (request))
396
397static void handle_setup(void)
398{
Marek Vasut98377322013-07-10 03:16:29 +0200399 struct usb_request *req = &controller.ep[0].req;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200400 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700401 struct ept_queue_head *head;
402 struct usb_ctrlrequest r;
403 int status = 0;
404 int num, in, _num, _in, i;
405 char *buf;
Marek Vasut9212f892013-07-10 03:16:39 +0200406 head = mv_get_qh(0, 0); /* EP0 OUT */
Lei Wena91ee2a2011-10-05 08:11:40 -0700407
Marek Vasut58465342013-07-10 03:16:42 +0200408 mv_invalidate_qh(0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700409 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
410 writel(EPT_RX(0), &udc->epstat);
411 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
412 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
413
414 switch (SETUP(r.bRequestType, r.bRequest)) {
415 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
416 _num = r.wIndex & 15;
417 _in = !!(r.wIndex & 0x80);
418
419 if ((r.wValue == 0) && (r.wLength == 0)) {
420 req->length = 0;
421 for (i = 0; i < NUM_ENDPOINTS; i++) {
Troy Kisky256a3172013-10-10 15:28:00 -0700422 struct mv_ep *ep = &controller.ep[i];
423
424 if (!ep->desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700425 continue;
Troy Kisky256a3172013-10-10 15:28:00 -0700426 num = ep->desc->bEndpointAddress
Marek Vasut98377322013-07-10 03:16:29 +0200427 & USB_ENDPOINT_NUMBER_MASK;
Troy Kisky256a3172013-10-10 15:28:00 -0700428 in = (ep->desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700429 & USB_DIR_IN) != 0;
430 if ((num == _num) && (in == _in)) {
Troy Kisky256a3172013-10-10 15:28:00 -0700431 ep_enable(num, in, ep->ep.maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700432 usb_ep_queue(controller.gadget.ep0,
433 req, 0);
434 break;
435 }
436 }
437 }
438 return;
439
440 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
441 /*
442 * write address delayed (will take effect
443 * after the next IN txn)
444 */
445 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
446 req->length = 0;
447 usb_ep_queue(controller.gadget.ep0, req, 0);
448 return;
449
450 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
451 req->length = 2;
452 buf = (char *)req->buf;
453 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
454 buf[1] = 0;
455 usb_ep_queue(controller.gadget.ep0, req, 0);
456 return;
457 }
458 /* pass request up to the gadget driver */
459 if (controller.driver)
460 status = controller.driver->setup(&controller.gadget, &r);
461 else
462 status = -ENODEV;
463
464 if (!status)
465 return;
466 DBG("STALL reqname %s type %x value %x, index %x\n",
467 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
468 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
469}
470
471static void stop_activity(void)
472{
473 int i, num, in;
474 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200475 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700476 writel(readl(&udc->epcomp), &udc->epcomp);
477 writel(readl(&udc->epstat), &udc->epstat);
478 writel(0xffffffff, &udc->epflush);
479
480 /* error out any pending reqs */
481 for (i = 0; i < NUM_ENDPOINTS; i++) {
482 if (i != 0)
483 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200484 if (controller.ep[i].desc) {
485 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700486 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200487 in = (controller.ep[i].desc->bEndpointAddress
488 & USB_DIR_IN) != 0;
Marek Vasut9212f892013-07-10 03:16:39 +0200489 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700490 head->info = INFO_ACTIVE;
Marek Vasut58465342013-07-10 03:16:42 +0200491 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700492 }
493 }
494}
495
496void udc_irq(void)
497{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200498 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700499 unsigned n = readl(&udc->usbsts);
500 writel(n, &udc->usbsts);
501 int bit, i, num, in;
502
503 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
504 if (n == 0)
505 return;
506
507 if (n & STS_URI) {
508 DBG("-- reset --\n");
509 stop_activity();
510 }
511 if (n & STS_SLI)
512 DBG("-- suspend --\n");
513
514 if (n & STS_PCI) {
Troy Kisky256a3172013-10-10 15:28:00 -0700515 int max = 64;
516 int speed = USB_SPEED_FULL;
517
Lei Wena91ee2a2011-10-05 08:11:40 -0700518 bit = (readl(&udc->portsc) >> 26) & 3;
Troy Kisky256a3172013-10-10 15:28:00 -0700519 DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
Lei Wena91ee2a2011-10-05 08:11:40 -0700520 if (bit == 2) {
Troy Kisky256a3172013-10-10 15:28:00 -0700521 speed = USB_SPEED_HIGH;
522 max = 512;
523 }
524 controller.gadget.speed = speed;
525 for (i = 1; i < NUM_ENDPOINTS; i++) {
526 if (controller.ep[i].ep.maxpacket > max)
527 controller.ep[i].ep.maxpacket = max;
Lei Wena91ee2a2011-10-05 08:11:40 -0700528 }
529 }
530
531 if (n & STS_UEI)
532 printf("<UEI %x>\n", readl(&udc->epcomp));
533
534 if ((n & STS_UI) || (n & STS_UEI)) {
535 n = readl(&udc->epstat);
536 if (n & EPT_RX(0))
537 handle_setup();
538
539 n = readl(&udc->epcomp);
540 if (n != 0)
541 writel(n, &udc->epcomp);
542
543 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200544 if (controller.ep[i].desc) {
545 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700546 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200547 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700548 & USB_DIR_IN) != 0;
549 bit = (in) ? EPT_TX(num) : EPT_RX(num);
550 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200551 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700552 }
553 }
554 }
555}
556
557int usb_gadget_handle_interrupts(void)
558{
559 u32 value;
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
562 value = readl(&udc->usbsts);
563 if (value)
564 udc_irq();
565
566 return value;
567}
568
569static int mv_pullup(struct usb_gadget *gadget, int is_on)
570{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200571 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700572 if (is_on) {
573 /* RESET */
574 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
575 udelay(200);
576
Marek Vasut9212f892013-07-10 03:16:39 +0200577 writel((unsigned)controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700578
579 /* select DEVICE mode */
580 writel(USBMODE_DEVICE, &udc->usbmode);
581
582 writel(0xffffffff, &udc->epflush);
583
584 /* Turn on the USB connection by enabling the pullup resistor */
585 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
586 } else {
587 stop_activity();
588 writel(USBCMD_FS2, &udc->usbcmd);
589 udelay(800);
590 if (controller.driver)
591 controller.driver->disconnect(gadget);
592 }
593
594 return 0;
595}
596
597void udc_disconnect(void)
598{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200599 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700600 /* disable pullup */
601 stop_activity();
602 writel(USBCMD_FS2, &udc->usbcmd);
603 udelay(800);
604 if (controller.driver)
605 controller.driver->disconnect(&controller.gadget);
606}
607
608static int mvudc_probe(void)
609{
610 struct ept_queue_head *head;
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200611 uint8_t *imem;
Lei Wena91ee2a2011-10-05 08:11:40 -0700612 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200613
Marek Vasut806d76c2013-07-10 03:16:34 +0200614 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700615
Marek Vasut456707d2013-07-10 03:16:37 +0200616 const int eplist_min_align = 4096;
617 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
618 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
619 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
620
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200621 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
622 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
623 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
624 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
625
Marek Vasut456707d2013-07-10 03:16:37 +0200626 /* The QH list must be aligned to 4096 bytes. */
627 controller.epts = memalign(eplist_align, eplist_sz);
628 if (!controller.epts)
629 return -ENOMEM;
630 memset(controller.epts, 0, eplist_sz);
631
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200632 /*
633 * Each qTD item must be 32-byte aligned, each qTD touple must be
634 * cacheline aligned. There are two qTD items for each endpoint and
635 * only one of them is used for the endpoint at time, so we can group
636 * them together.
637 */
638 controller.items_mem = memalign(ilist_align, ilist_sz);
639 if (!controller.items_mem) {
640 free(controller.epts);
641 return -ENOMEM;
642 }
Troy Kisky0dee8ce2013-09-25 18:41:14 -0700643 memset(controller.items_mem, 0, ilist_sz);
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200644
Lei Wena91ee2a2011-10-05 08:11:40 -0700645 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
646 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200647 * Configure QH for each endpoint. The structure of the QH list
648 * is such that each two subsequent fields, N and N+1 where N is
649 * even, in the QH list represent QH for one endpoint. The Nth
650 * entry represents OUT configuration and the N+1th entry does
651 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700652 */
Marek Vasut393004e2013-07-10 03:16:36 +0200653 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700654 if (i < 2)
655 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
656 | CONFIG_ZLT | CONFIG_IOS;
657 else
658 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
659 | CONFIG_ZLT;
660 head->next = TERMINATE;
661 head->info = 0;
662
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200663 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
664 if (i & 1)
665 imem += sizeof(struct ept_queue_item);
666
667 controller.items[i] = (struct ept_queue_item *)imem;
Marek Vasut58465342013-07-10 03:16:42 +0200668
669 if (i & 1) {
670 mv_flush_qh(i - 1);
671 mv_flush_qtd(i - 1);
672 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700673 }
674
675 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200676
677 /* Init EP 0 */
678 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut98377322013-07-10 03:16:29 +0200679 controller.ep[0].desc = &ep0_in_desc;
Marek Vasute1263412013-07-10 03:16:30 +0200680 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700681 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200682
683 /* Init EP 1..n */
684 for (i = 1; i < NUM_ENDPOINTS; i++) {
685 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
686 sizeof(*mv_ep_init));
687 list_add_tail(&controller.ep[i].ep.ep_list,
688 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700689 }
Marek Vasute1263412013-07-10 03:16:30 +0200690
Lei Wena91ee2a2011-10-05 08:11:40 -0700691 return 0;
692}
693
694int usb_gadget_register_driver(struct usb_gadget_driver *driver)
695{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200696 struct mv_udc *udc;
697 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700698
Marek Vasutfe67fe72013-07-10 03:16:33 +0200699 if (!driver)
700 return -EINVAL;
701 if (!driver->bind || !driver->setup || !driver->disconnect)
702 return -EINVAL;
703 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700704 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700705
Troy Kisky8f9c49d2013-10-10 15:27:56 -0700706 ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200707 if (ret)
708 return ret;
709
710 ret = mvudc_probe();
711 if (!ret) {
712 udc = (struct mv_udc *)controller.ctrl->hcor;
713
Lei Wena91ee2a2011-10-05 08:11:40 -0700714 /* select ULPI phy */
715 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
716 }
Marek Vasut1f4b4912013-07-10 03:16:32 +0200717
718 ret = driver->bind(&controller.gadget);
719 if (ret) {
720 DBG("driver->bind() returned %d\n", ret);
721 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700722 }
723 controller.driver = driver;
724
725 return 0;
726}
727
728int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
729{
730 return 0;
731}