blob: da41738653eb13efac685c7ca74d7672a081eb56 [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>
Troy Kiskyf1573942013-10-10 15:28:03 -070016#include <asm/byteorder.h>
17#include <asm/errno.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070018#include <asm/io.h>
Troy Kisky256a3172013-10-10 15:28:00 -070019#include <asm/unaligned.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070020#include <linux/types.h>
Troy Kiskyf1573942013-10-10 15:28:03 -070021#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
Lei Wena91ee2a2011-10-05 08:11:40 -070023#include <usb/mv_udc.h>
Troy Kiskyf1573942013-10-10 15:28:03 -070024#include "../host/ehci.h"
25#include "mv_udc.h"
Lei Wena91ee2a2011-10-05 08:11:40 -070026
Marek Vasut9d64ac92013-07-10 03:16:38 +020027/*
28 * Check if the system has too long cachelines. If the cachelines are
29 * longer then 128b, the driver will not be able flush/invalidate data
30 * cache over separate QH entries. We use 128b because one QH entry is
31 * 64b long and there are always two QH list entries for each endpoint.
32 */
33#if ARCH_DMA_MINALIGN > 128
34#error This driver can not work on systems with caches longer than 128b
35#endif
36
Lei Wena91ee2a2011-10-05 08:11:40 -070037#ifndef DEBUG
38#define DBG(x...) do {} while (0)
39#else
40#define DBG(x...) printf(x)
41static const char *reqname(unsigned r)
42{
43 switch (r) {
44 case USB_REQ_GET_STATUS: return "GET_STATUS";
45 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
46 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
47 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
48 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
49 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
50 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
51 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
52 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
53 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
54 default: return "*UNKNOWN*";
55 }
56}
57#endif
58
Lei Wena91ee2a2011-10-05 08:11:40 -070059static struct usb_endpoint_descriptor ep0_out_desc = {
60 .bLength = sizeof(struct usb_endpoint_descriptor),
61 .bDescriptorType = USB_DT_ENDPOINT,
62 .bEndpointAddress = 0,
63 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
64};
65
66static struct usb_endpoint_descriptor ep0_in_desc = {
67 .bLength = sizeof(struct usb_endpoint_descriptor),
68 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = USB_DIR_IN,
70 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
71};
72
Lei Wena91ee2a2011-10-05 08:11:40 -070073static int mv_pullup(struct usb_gadget *gadget, int is_on);
74static int mv_ep_enable(struct usb_ep *ep,
75 const struct usb_endpoint_descriptor *desc);
76static int mv_ep_disable(struct usb_ep *ep);
77static int mv_ep_queue(struct usb_ep *ep,
78 struct usb_request *req, gfp_t gfp_flags);
79static struct usb_request *
80mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
81static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
82
83static struct usb_gadget_ops mv_udc_ops = {
84 .pullup = mv_pullup,
85};
86
87static struct usb_ep_ops mv_ep_ops = {
88 .enable = mv_ep_enable,
89 .disable = mv_ep_disable,
90 .queue = mv_ep_queue,
91 .alloc_request = mv_ep_alloc_request,
92 .free_request = mv_ep_free_request,
93};
94
Marek Vasute1263412013-07-10 03:16:30 +020095/* Init values for USB endpoints. */
96static const struct usb_ep mv_ep_init[2] = {
97 [0] = { /* EP 0 */
98 .maxpacket = 64,
99 .name = "ep0",
100 .ops = &mv_ep_ops,
101 },
102 [1] = { /* EP 1..n */
103 .maxpacket = 512,
104 .name = "ep-",
105 .ops = &mv_ep_ops,
106 },
107};
108
Lei Wena91ee2a2011-10-05 08:11:40 -0700109static struct mv_drv controller = {
Marek Vasutbe0b0e72013-07-10 03:16:35 +0200110 .gadget = {
111 .name = "mv_udc",
112 .ops = &mv_udc_ops,
Troy Kisky4b4df472013-09-25 18:41:09 -0700113 .is_dualspeed = 1,
Lei Wena91ee2a2011-10-05 08:11:40 -0700114 },
115};
116
Marek Vasut9212f892013-07-10 03:16:39 +0200117/**
118 * mv_get_qh() - return queue head for endpoint
119 * @ep_num: Endpoint number
120 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
121 *
122 * This function returns the QH associated with particular endpoint
123 * and it's direction.
124 */
125static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
126{
127 return &controller.epts[(ep_num * 2) + dir_in];
128}
129
Marek Vasuta6f6b082013-07-10 03:16:41 +0200130/**
131 * mv_get_qtd() - return queue item for endpoint
132 * @ep_num: Endpoint number
133 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
134 *
135 * This function returns the QH associated with particular endpoint
136 * and it's direction.
137 */
138static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
139{
140 return controller.items[(ep_num * 2) + dir_in];
141}
142
Marek Vasut58465342013-07-10 03:16:42 +0200143/**
144 * mv_flush_qh - flush cache over queue head
145 * @ep_num: Endpoint number
146 *
147 * This function flushes cache over QH for particular endpoint.
148 */
149static void mv_flush_qh(int ep_num)
150{
151 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
152 const uint32_t start = (uint32_t)head;
153 const uint32_t end = start + 2 * sizeof(*head);
154
155 flush_dcache_range(start, end);
156}
157
158/**
159 * mv_invalidate_qh - invalidate cache over queue head
160 * @ep_num: Endpoint number
161 *
162 * This function invalidates cache over QH for particular endpoint.
163 */
164static void mv_invalidate_qh(int ep_num)
165{
166 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
167 uint32_t start = (uint32_t)head;
168 uint32_t end = start + 2 * sizeof(*head);
169
170 invalidate_dcache_range(start, end);
171}
172
173/**
174 * mv_flush_qtd - flush cache over queue item
175 * @ep_num: Endpoint number
176 *
177 * This function flushes cache over qTD pair for particular endpoint.
178 */
179static void mv_flush_qtd(int ep_num)
180{
181 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
182 const uint32_t start = (uint32_t)item;
183 const uint32_t end_raw = start + 2 * sizeof(*item);
184 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
185
186 flush_dcache_range(start, end);
187}
188
189/**
190 * mv_invalidate_qtd - invalidate cache over queue item
191 * @ep_num: Endpoint number
192 *
193 * This function invalidates cache over qTD pair for particular endpoint.
194 */
195static void mv_invalidate_qtd(int ep_num)
196{
197 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
198 const uint32_t start = (uint32_t)item;
199 const uint32_t end_raw = start + 2 * sizeof(*item);
200 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
201
202 invalidate_dcache_range(start, end);
203}
204
Lei Wena91ee2a2011-10-05 08:11:40 -0700205static struct usb_request *
206mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
207{
208 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
209 return &mv_ep->req;
210}
211
212static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
213{
214 return;
215}
216
Troy Kisky256a3172013-10-10 15:28:00 -0700217static void ep_enable(int num, int in, int maxpacket)
Lei Wena91ee2a2011-10-05 08:11:40 -0700218{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200219 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700220 unsigned n;
Lei Wena91ee2a2011-10-05 08:11:40 -0700221
222 n = readl(&udc->epctrl[num]);
223 if (in)
224 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
225 else
226 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
227
Marek Vasut58465342013-07-10 03:16:42 +0200228 if (num != 0) {
Troy Kisky06bb97a2013-10-10 15:28:02 -0700229 struct ept_queue_head *head = mv_get_qh(num, in);
230
Troy Kisky256a3172013-10-10 15:28:00 -0700231 head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
Marek Vasut58465342013-07-10 03:16:42 +0200232 mv_flush_qh(num);
233 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700234 writel(n, &udc->epctrl[num]);
235}
236
237static int mv_ep_enable(struct usb_ep *ep,
238 const struct usb_endpoint_descriptor *desc)
239{
240 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
241 int num, in;
242 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
243 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
Lei Wena91ee2a2011-10-05 08:11:40 -0700244 mv_ep->desc = desc;
Troy Kisky256a3172013-10-10 15:28:00 -0700245
246 if (num) {
247 int max = get_unaligned_le16(&desc->wMaxPacketSize);
248
249 if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
250 max = 64;
251 if (ep->maxpacket != max) {
252 DBG("%s: from %d to %d\n", __func__,
253 ep->maxpacket, max);
254 ep->maxpacket = max;
255 }
256 }
257 ep_enable(num, in, ep->maxpacket);
258 DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700259 return 0;
260}
261
262static int mv_ep_disable(struct usb_ep *ep)
263{
Troy Kisky35494072013-09-25 18:41:15 -0700264 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
265
266 mv_ep->desc = NULL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700267 return 0;
268}
269
Troy Kisky1567b882013-10-10 15:28:01 -0700270static int mv_bounce(struct mv_ep *ep, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200271{
272 uint32_t addr = (uint32_t)ep->req.buf;
273 uint32_t ba;
274
275 /* Input buffer address is not aligned. */
276 if (addr & (ARCH_DMA_MINALIGN - 1))
277 goto align;
278
279 /* Input buffer length is not aligned. */
280 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
281 goto align;
282
283 /* The buffer is well aligned, only flush cache. */
284 ep->b_len = ep->req.length;
285 ep->b_buf = ep->req.buf;
286 goto flush;
287
288align:
289 /* Use internal buffer for small payloads. */
290 if (ep->req.length <= 64) {
291 ep->b_len = 64;
292 ep->b_buf = ep->b_fast;
293 } else {
294 ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN);
295 ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len);
296 if (!ep->b_buf)
297 return -ENOMEM;
298 }
Troy Kisky1567b882013-10-10 15:28:01 -0700299 if (in)
300 memcpy(ep->b_buf, ep->req.buf, ep->req.length);
Marek Vasute0fc5822013-07-10 03:16:43 +0200301
302flush:
303 ba = (uint32_t)ep->b_buf;
304 flush_dcache_range(ba, ba + ep->b_len);
305
306 return 0;
307}
308
Troy Kisky1567b882013-10-10 15:28:01 -0700309static void mv_debounce(struct mv_ep *ep, int in)
Marek Vasute0fc5822013-07-10 03:16:43 +0200310{
311 uint32_t addr = (uint32_t)ep->req.buf;
312 uint32_t ba = (uint32_t)ep->b_buf;
313
Troy Kisky1567b882013-10-10 15:28:01 -0700314 if (in) {
315 if (addr == ba)
316 return; /* not a bounce */
317 goto free;
318 }
Marek Vasute0fc5822013-07-10 03:16:43 +0200319 invalidate_dcache_range(ba, ba + ep->b_len);
320
Troy Kisky1567b882013-10-10 15:28:01 -0700321 if (addr == ba)
322 return; /* not a bounce */
Marek Vasute0fc5822013-07-10 03:16:43 +0200323
Marek Vasute0fc5822013-07-10 03:16:43 +0200324 memcpy(ep->req.buf, ep->b_buf, ep->req.length);
Troy Kisky1567b882013-10-10 15:28:01 -0700325free:
Marek Vasute0fc5822013-07-10 03:16:43 +0200326 /* Large payloads use allocated buffer, free it. */
Troy Kisky1567b882013-10-10 15:28:01 -0700327 if (ep->b_buf != ep->b_fast)
Marek Vasute0fc5822013-07-10 03:16:43 +0200328 free(ep->b_buf);
329}
330
Lei Wena91ee2a2011-10-05 08:11:40 -0700331static int mv_ep_queue(struct usb_ep *ep,
332 struct usb_request *req, gfp_t gfp_flags)
333{
334 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200335 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700336 struct ept_queue_item *item;
337 struct ept_queue_head *head;
Marek Vasute0fc5822013-07-10 03:16:43 +0200338 int bit, num, len, in, ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700339 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
340 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200341 item = mv_get_qtd(num, in);
Marek Vasut9212f892013-07-10 03:16:39 +0200342 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700343 len = req->length;
344
Troy Kisky1567b882013-10-10 15:28:01 -0700345 ret = mv_bounce(mv_ep, in);
Marek Vasute0fc5822013-07-10 03:16:43 +0200346 if (ret)
347 return ret;
348
Lei Wena91ee2a2011-10-05 08:11:40 -0700349 item->next = TERMINATE;
350 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
Marek Vasute0fc5822013-07-10 03:16:43 +0200351 item->page0 = (uint32_t)mv_ep->b_buf;
352 item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000;
Troy Kisky573d1312013-09-25 18:41:12 -0700353 mv_flush_qtd(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700354
355 head->next = (unsigned) item;
356 head->info = 0;
357
Marek Vasute0fc5822013-07-10 03:16:43 +0200358 DBG("ept%d %s queue len %x, buffer %p\n",
359 num, in ? "in" : "out", len, mv_ep->b_buf);
Troy Kisky573d1312013-09-25 18:41:12 -0700360 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700361
362 if (in)
363 bit = EPT_TX(num);
364 else
365 bit = EPT_RX(num);
366
Lei Wena91ee2a2011-10-05 08:11:40 -0700367 writel(bit, &udc->epprime);
368
369 return 0;
370}
371
372static void handle_ep_complete(struct mv_ep *ep)
373{
374 struct ept_queue_item *item;
375 int num, in, len;
376 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
377 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
378 if (num == 0)
379 ep->desc = &ep0_out_desc;
Marek Vasuta6f6b082013-07-10 03:16:41 +0200380 item = mv_get_qtd(num, in);
Marek Vasut58465342013-07-10 03:16:42 +0200381 mv_invalidate_qtd(num);
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +0200382
Lei Wena91ee2a2011-10-05 08:11:40 -0700383 if (item->info & 0xff)
Troy Kisky3703c452013-09-25 18:41:08 -0700384 printf("EP%d/%s FAIL info=%x pg0=%x\n",
385 num, in ? "in" : "out", item->info, item->page0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700386
387 len = (item->info >> 16) & 0x7fff;
388 ep->req.length -= len;
Troy Kisky1567b882013-10-10 15:28:01 -0700389 mv_debounce(ep, in);
390
Lei Wena91ee2a2011-10-05 08:11:40 -0700391 DBG("ept%d %s complete %x\n",
392 num, in ? "in" : "out", len);
393 ep->req.complete(&ep->ep, &ep->req);
394 if (num == 0) {
395 ep->req.length = 0;
396 usb_ep_queue(&ep->ep, &ep->req, 0);
397 ep->desc = &ep0_in_desc;
398 }
399}
400
401#define SETUP(type, request) (((type) << 8) | (request))
402
403static void handle_setup(void)
404{
Marek Vasut98377322013-07-10 03:16:29 +0200405 struct usb_request *req = &controller.ep[0].req;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200406 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700407 struct ept_queue_head *head;
408 struct usb_ctrlrequest r;
409 int status = 0;
410 int num, in, _num, _in, i;
411 char *buf;
Marek Vasut9212f892013-07-10 03:16:39 +0200412 head = mv_get_qh(0, 0); /* EP0 OUT */
Lei Wena91ee2a2011-10-05 08:11:40 -0700413
Marek Vasut58465342013-07-10 03:16:42 +0200414 mv_invalidate_qh(0);
Lei Wena91ee2a2011-10-05 08:11:40 -0700415 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
416 writel(EPT_RX(0), &udc->epstat);
417 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
418 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
419
420 switch (SETUP(r.bRequestType, r.bRequest)) {
421 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
422 _num = r.wIndex & 15;
423 _in = !!(r.wIndex & 0x80);
424
425 if ((r.wValue == 0) && (r.wLength == 0)) {
426 req->length = 0;
427 for (i = 0; i < NUM_ENDPOINTS; i++) {
Troy Kisky256a3172013-10-10 15:28:00 -0700428 struct mv_ep *ep = &controller.ep[i];
429
430 if (!ep->desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700431 continue;
Troy Kisky256a3172013-10-10 15:28:00 -0700432 num = ep->desc->bEndpointAddress
Marek Vasut98377322013-07-10 03:16:29 +0200433 & USB_ENDPOINT_NUMBER_MASK;
Troy Kisky256a3172013-10-10 15:28:00 -0700434 in = (ep->desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700435 & USB_DIR_IN) != 0;
436 if ((num == _num) && (in == _in)) {
Troy Kisky256a3172013-10-10 15:28:00 -0700437 ep_enable(num, in, ep->ep.maxpacket);
Lei Wena91ee2a2011-10-05 08:11:40 -0700438 usb_ep_queue(controller.gadget.ep0,
439 req, 0);
440 break;
441 }
442 }
443 }
444 return;
445
446 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
447 /*
448 * write address delayed (will take effect
449 * after the next IN txn)
450 */
451 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
452 req->length = 0;
453 usb_ep_queue(controller.gadget.ep0, req, 0);
454 return;
455
456 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
457 req->length = 2;
458 buf = (char *)req->buf;
459 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
460 buf[1] = 0;
461 usb_ep_queue(controller.gadget.ep0, req, 0);
462 return;
463 }
464 /* pass request up to the gadget driver */
465 if (controller.driver)
466 status = controller.driver->setup(&controller.gadget, &r);
467 else
468 status = -ENODEV;
469
470 if (!status)
471 return;
472 DBG("STALL reqname %s type %x value %x, index %x\n",
473 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
474 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
475}
476
477static void stop_activity(void)
478{
479 int i, num, in;
480 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200481 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700482 writel(readl(&udc->epcomp), &udc->epcomp);
483 writel(readl(&udc->epstat), &udc->epstat);
484 writel(0xffffffff, &udc->epflush);
485
486 /* error out any pending reqs */
487 for (i = 0; i < NUM_ENDPOINTS; i++) {
488 if (i != 0)
489 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200490 if (controller.ep[i].desc) {
491 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700492 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200493 in = (controller.ep[i].desc->bEndpointAddress
494 & USB_DIR_IN) != 0;
Marek Vasut9212f892013-07-10 03:16:39 +0200495 head = mv_get_qh(num, in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700496 head->info = INFO_ACTIVE;
Marek Vasut58465342013-07-10 03:16:42 +0200497 mv_flush_qh(num);
Lei Wena91ee2a2011-10-05 08:11:40 -0700498 }
499 }
500}
501
502void udc_irq(void)
503{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200504 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700505 unsigned n = readl(&udc->usbsts);
506 writel(n, &udc->usbsts);
507 int bit, i, num, in;
508
509 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
510 if (n == 0)
511 return;
512
513 if (n & STS_URI) {
514 DBG("-- reset --\n");
515 stop_activity();
516 }
517 if (n & STS_SLI)
518 DBG("-- suspend --\n");
519
520 if (n & STS_PCI) {
Troy Kisky256a3172013-10-10 15:28:00 -0700521 int max = 64;
522 int speed = USB_SPEED_FULL;
523
Lei Wena91ee2a2011-10-05 08:11:40 -0700524 bit = (readl(&udc->portsc) >> 26) & 3;
Troy Kisky256a3172013-10-10 15:28:00 -0700525 DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
Lei Wena91ee2a2011-10-05 08:11:40 -0700526 if (bit == 2) {
Troy Kisky256a3172013-10-10 15:28:00 -0700527 speed = USB_SPEED_HIGH;
528 max = 512;
529 }
530 controller.gadget.speed = speed;
531 for (i = 1; i < NUM_ENDPOINTS; i++) {
532 if (controller.ep[i].ep.maxpacket > max)
533 controller.ep[i].ep.maxpacket = max;
Lei Wena91ee2a2011-10-05 08:11:40 -0700534 }
535 }
536
537 if (n & STS_UEI)
538 printf("<UEI %x>\n", readl(&udc->epcomp));
539
540 if ((n & STS_UI) || (n & STS_UEI)) {
541 n = readl(&udc->epstat);
542 if (n & EPT_RX(0))
543 handle_setup();
544
545 n = readl(&udc->epcomp);
546 if (n != 0)
547 writel(n, &udc->epcomp);
548
549 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200550 if (controller.ep[i].desc) {
551 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700552 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200553 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700554 & USB_DIR_IN) != 0;
555 bit = (in) ? EPT_TX(num) : EPT_RX(num);
556 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200557 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700558 }
559 }
560 }
561}
562
563int usb_gadget_handle_interrupts(void)
564{
565 u32 value;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200566 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700567
568 value = readl(&udc->usbsts);
569 if (value)
570 udc_irq();
571
572 return value;
573}
574
575static int mv_pullup(struct usb_gadget *gadget, int is_on)
576{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200577 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700578 if (is_on) {
579 /* RESET */
580 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
581 udelay(200);
582
Marek Vasut9212f892013-07-10 03:16:39 +0200583 writel((unsigned)controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700584
585 /* select DEVICE mode */
586 writel(USBMODE_DEVICE, &udc->usbmode);
587
588 writel(0xffffffff, &udc->epflush);
589
590 /* Turn on the USB connection by enabling the pullup resistor */
591 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
592 } else {
593 stop_activity();
594 writel(USBCMD_FS2, &udc->usbcmd);
595 udelay(800);
596 if (controller.driver)
597 controller.driver->disconnect(gadget);
598 }
599
600 return 0;
601}
602
603void udc_disconnect(void)
604{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200605 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700606 /* disable pullup */
607 stop_activity();
608 writel(USBCMD_FS2, &udc->usbcmd);
609 udelay(800);
610 if (controller.driver)
611 controller.driver->disconnect(&controller.gadget);
612}
613
614static int mvudc_probe(void)
615{
616 struct ept_queue_head *head;
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200617 uint8_t *imem;
Lei Wena91ee2a2011-10-05 08:11:40 -0700618 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200619
Marek Vasut806d76c2013-07-10 03:16:34 +0200620 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700621
Marek Vasut456707d2013-07-10 03:16:37 +0200622 const int eplist_min_align = 4096;
623 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
624 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
625 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
626
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200627 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
628 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
629 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
630 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
631
Marek Vasut456707d2013-07-10 03:16:37 +0200632 /* The QH list must be aligned to 4096 bytes. */
633 controller.epts = memalign(eplist_align, eplist_sz);
634 if (!controller.epts)
635 return -ENOMEM;
636 memset(controller.epts, 0, eplist_sz);
637
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200638 /*
639 * Each qTD item must be 32-byte aligned, each qTD touple must be
640 * cacheline aligned. There are two qTD items for each endpoint and
641 * only one of them is used for the endpoint at time, so we can group
642 * them together.
643 */
644 controller.items_mem = memalign(ilist_align, ilist_sz);
645 if (!controller.items_mem) {
646 free(controller.epts);
647 return -ENOMEM;
648 }
Troy Kisky0dee8ce2013-09-25 18:41:14 -0700649 memset(controller.items_mem, 0, ilist_sz);
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200650
Lei Wena91ee2a2011-10-05 08:11:40 -0700651 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
652 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200653 * Configure QH for each endpoint. The structure of the QH list
654 * is such that each two subsequent fields, N and N+1 where N is
655 * even, in the QH list represent QH for one endpoint. The Nth
656 * entry represents OUT configuration and the N+1th entry does
657 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700658 */
Marek Vasut393004e2013-07-10 03:16:36 +0200659 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700660 if (i < 2)
661 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
662 | CONFIG_ZLT | CONFIG_IOS;
663 else
664 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
665 | CONFIG_ZLT;
666 head->next = TERMINATE;
667 head->info = 0;
668
Marek Vasutb62fa7d2013-07-10 03:16:40 +0200669 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
670 if (i & 1)
671 imem += sizeof(struct ept_queue_item);
672
673 controller.items[i] = (struct ept_queue_item *)imem;
Marek Vasut58465342013-07-10 03:16:42 +0200674
675 if (i & 1) {
676 mv_flush_qh(i - 1);
677 mv_flush_qtd(i - 1);
678 }
Lei Wena91ee2a2011-10-05 08:11:40 -0700679 }
680
681 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200682
683 /* Init EP 0 */
684 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut98377322013-07-10 03:16:29 +0200685 controller.ep[0].desc = &ep0_in_desc;
Marek Vasute1263412013-07-10 03:16:30 +0200686 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700687 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200688
689 /* Init EP 1..n */
690 for (i = 1; i < NUM_ENDPOINTS; i++) {
691 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
692 sizeof(*mv_ep_init));
693 list_add_tail(&controller.ep[i].ep.ep_list,
694 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700695 }
Marek Vasute1263412013-07-10 03:16:30 +0200696
Lei Wena91ee2a2011-10-05 08:11:40 -0700697 return 0;
698}
699
700int usb_gadget_register_driver(struct usb_gadget_driver *driver)
701{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200702 struct mv_udc *udc;
703 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700704
Marek Vasutfe67fe72013-07-10 03:16:33 +0200705 if (!driver)
706 return -EINVAL;
707 if (!driver->bind || !driver->setup || !driver->disconnect)
708 return -EINVAL;
709 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700710 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700711
Troy Kisky8f9c49d2013-10-10 15:27:56 -0700712 ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200713 if (ret)
714 return ret;
715
716 ret = mvudc_probe();
717 if (!ret) {
718 udc = (struct mv_udc *)controller.ctrl->hcor;
719
Lei Wena91ee2a2011-10-05 08:11:40 -0700720 /* select ULPI phy */
721 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
722 }
Marek Vasut1f4b4912013-07-10 03:16:32 +0200723
724 ret = driver->bind(&controller.gadget);
725 if (ret) {
726 DBG("driver->bind() returned %d\n", ret);
727 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700728 }
729 controller.driver = driver;
730
731 return 0;
732}
733
734int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
735{
736 return 0;
737}