blob: c9cd24a1fb82810955a777cd6ee477b75598d33d [file] [log] [blame]
Lei Wen26cc5122011-10-05 08:11:40 -07001/*
2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Lei Wen26cc5122011-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 Vasuta7eafcf2013-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
Lei Wen26cc5122011-10-05 08:11:40 -070024#ifndef DEBUG
25#define DBG(x...) do {} while (0)
26#else
27#define DBG(x...) printf(x)
28static const char *reqname(unsigned r)
29{
30 switch (r) {
31 case USB_REQ_GET_STATUS: return "GET_STATUS";
32 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
33 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
34 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
35 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
36 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
37 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
38 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
39 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
40 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
41 default: return "*UNKNOWN*";
42 }
43}
44#endif
45
46#define PAGE_SIZE 4096
Marek Vasutf6463172013-07-10 03:16:34 +020047
Lei Wen26cc5122011-10-05 08:11:40 -070048static struct usb_endpoint_descriptor ep0_out_desc = {
49 .bLength = sizeof(struct usb_endpoint_descriptor),
50 .bDescriptorType = USB_DT_ENDPOINT,
51 .bEndpointAddress = 0,
52 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
53};
54
55static struct usb_endpoint_descriptor ep0_in_desc = {
56 .bLength = sizeof(struct usb_endpoint_descriptor),
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bEndpointAddress = USB_DIR_IN,
59 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
60};
61
62struct ept_queue_head *epts;
63struct ept_queue_item *items[2 * NUM_ENDPOINTS];
64static int mv_pullup(struct usb_gadget *gadget, int is_on);
65static int mv_ep_enable(struct usb_ep *ep,
66 const struct usb_endpoint_descriptor *desc);
67static int mv_ep_disable(struct usb_ep *ep);
68static int mv_ep_queue(struct usb_ep *ep,
69 struct usb_request *req, gfp_t gfp_flags);
70static struct usb_request *
71mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
72static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
73
74static struct usb_gadget_ops mv_udc_ops = {
75 .pullup = mv_pullup,
76};
77
78static struct usb_ep_ops mv_ep_ops = {
79 .enable = mv_ep_enable,
80 .disable = mv_ep_disable,
81 .queue = mv_ep_queue,
82 .alloc_request = mv_ep_alloc_request,
83 .free_request = mv_ep_free_request,
84};
85
Marek Vasut2ea4b442013-07-10 03:16:30 +020086/* Init values for USB endpoints. */
87static const struct usb_ep mv_ep_init[2] = {
88 [0] = { /* EP 0 */
89 .maxpacket = 64,
90 .name = "ep0",
91 .ops = &mv_ep_ops,
92 },
93 [1] = { /* EP 1..n */
94 .maxpacket = 512,
95 .name = "ep-",
96 .ops = &mv_ep_ops,
97 },
98};
99
Lei Wen26cc5122011-10-05 08:11:40 -0700100static struct mv_drv controller = {
Marek Vasutfe48f052013-07-10 03:16:35 +0200101 .gadget = {
102 .name = "mv_udc",
103 .ops = &mv_udc_ops,
Lei Wen26cc5122011-10-05 08:11:40 -0700104 },
105};
106
107static struct usb_request *
108mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
109{
110 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
111 return &mv_ep->req;
112}
113
114static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
115{
116 return;
117}
118
119static void ep_enable(int num, int in)
120{
121 struct ept_queue_head *head;
Marek Vasutbe7ed252013-07-10 03:16:32 +0200122 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700123 unsigned n;
124 head = epts + 2*num + in;
125
126 n = readl(&udc->epctrl[num]);
127 if (in)
128 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
129 else
130 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
131
132 if (num != 0)
133 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
134 writel(n, &udc->epctrl[num]);
135}
136
137static int mv_ep_enable(struct usb_ep *ep,
138 const struct usb_endpoint_descriptor *desc)
139{
140 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
141 int num, in;
142 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
143 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
144 ep_enable(num, in);
145 mv_ep->desc = desc;
146 return 0;
147}
148
149static int mv_ep_disable(struct usb_ep *ep)
150{
151 return 0;
152}
153
154static int mv_ep_queue(struct usb_ep *ep,
155 struct usb_request *req, gfp_t gfp_flags)
156{
157 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasutbe7ed252013-07-10 03:16:32 +0200158 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700159 struct ept_queue_item *item;
160 struct ept_queue_head *head;
161 unsigned phys;
162 int bit, num, len, in;
163 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
164 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
165 item = items[2 * num + in];
166 head = epts + 2 * num + in;
167 phys = (unsigned)req->buf;
168 len = req->length;
169
170 item->next = TERMINATE;
171 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
172 item->page0 = phys;
173 item->page1 = (phys & 0xfffff000) + 0x1000;
174
175 head->next = (unsigned) item;
176 head->info = 0;
177
178 DBG("ept%d %s queue len %x, buffer %x\n",
179 num, in ? "in" : "out", len, phys);
180
181 if (in)
182 bit = EPT_TX(num);
183 else
184 bit = EPT_RX(num);
185
186 flush_cache(phys, len);
187 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
188 writel(bit, &udc->epprime);
189
190 return 0;
191}
192
193static void handle_ep_complete(struct mv_ep *ep)
194{
195 struct ept_queue_item *item;
196 int num, in, len;
197 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
198 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
199 if (num == 0)
200 ep->desc = &ep0_out_desc;
201 item = items[2 * num + in];
202
203 if (item->info & 0xff)
204 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
205 num, in ? "in" : "out", item->info, item->page0);
206
207 len = (item->info >> 16) & 0x7fff;
208 ep->req.length -= len;
209 DBG("ept%d %s complete %x\n",
210 num, in ? "in" : "out", len);
211 ep->req.complete(&ep->ep, &ep->req);
212 if (num == 0) {
213 ep->req.length = 0;
214 usb_ep_queue(&ep->ep, &ep->req, 0);
215 ep->desc = &ep0_in_desc;
216 }
217}
218
219#define SETUP(type, request) (((type) << 8) | (request))
220
221static void handle_setup(void)
222{
Marek Vasut532d8462013-07-10 03:16:29 +0200223 struct usb_request *req = &controller.ep[0].req;
Marek Vasutbe7ed252013-07-10 03:16:32 +0200224 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700225 struct ept_queue_head *head;
226 struct usb_ctrlrequest r;
227 int status = 0;
228 int num, in, _num, _in, i;
229 char *buf;
230 head = epts;
231
232 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
233 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
234 writel(EPT_RX(0), &udc->epstat);
235 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
236 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
237
238 switch (SETUP(r.bRequestType, r.bRequest)) {
239 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
240 _num = r.wIndex & 15;
241 _in = !!(r.wIndex & 0x80);
242
243 if ((r.wValue == 0) && (r.wLength == 0)) {
244 req->length = 0;
245 for (i = 0; i < NUM_ENDPOINTS; i++) {
Marek Vasut532d8462013-07-10 03:16:29 +0200246 if (!controller.ep[i].desc)
Lei Wen26cc5122011-10-05 08:11:40 -0700247 continue;
Marek Vasut532d8462013-07-10 03:16:29 +0200248 num = controller.ep[i].desc->bEndpointAddress
249 & USB_ENDPOINT_NUMBER_MASK;
250 in = (controller.ep[i].desc->bEndpointAddress
Lei Wen26cc5122011-10-05 08:11:40 -0700251 & USB_DIR_IN) != 0;
252 if ((num == _num) && (in == _in)) {
253 ep_enable(num, in);
254 usb_ep_queue(controller.gadget.ep0,
255 req, 0);
256 break;
257 }
258 }
259 }
260 return;
261
262 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
263 /*
264 * write address delayed (will take effect
265 * after the next IN txn)
266 */
267 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
268 req->length = 0;
269 usb_ep_queue(controller.gadget.ep0, req, 0);
270 return;
271
272 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
273 req->length = 2;
274 buf = (char *)req->buf;
275 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
276 buf[1] = 0;
277 usb_ep_queue(controller.gadget.ep0, req, 0);
278 return;
279 }
280 /* pass request up to the gadget driver */
281 if (controller.driver)
282 status = controller.driver->setup(&controller.gadget, &r);
283 else
284 status = -ENODEV;
285
286 if (!status)
287 return;
288 DBG("STALL reqname %s type %x value %x, index %x\n",
289 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
290 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
291}
292
293static void stop_activity(void)
294{
295 int i, num, in;
296 struct ept_queue_head *head;
Marek Vasutbe7ed252013-07-10 03:16:32 +0200297 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700298 writel(readl(&udc->epcomp), &udc->epcomp);
299 writel(readl(&udc->epstat), &udc->epstat);
300 writel(0xffffffff, &udc->epflush);
301
302 /* error out any pending reqs */
303 for (i = 0; i < NUM_ENDPOINTS; i++) {
304 if (i != 0)
305 writel(0, &udc->epctrl[i]);
Marek Vasut532d8462013-07-10 03:16:29 +0200306 if (controller.ep[i].desc) {
307 num = controller.ep[i].desc->bEndpointAddress
Lei Wen26cc5122011-10-05 08:11:40 -0700308 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut532d8462013-07-10 03:16:29 +0200309 in = (controller.ep[i].desc->bEndpointAddress
310 & USB_DIR_IN) != 0;
Lei Wen26cc5122011-10-05 08:11:40 -0700311 head = epts + (num * 2) + (in);
312 head->info = INFO_ACTIVE;
313 }
314 }
315}
316
317void udc_irq(void)
318{
Marek Vasutbe7ed252013-07-10 03:16:32 +0200319 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700320 unsigned n = readl(&udc->usbsts);
321 writel(n, &udc->usbsts);
322 int bit, i, num, in;
323
324 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
325 if (n == 0)
326 return;
327
328 if (n & STS_URI) {
329 DBG("-- reset --\n");
330 stop_activity();
331 }
332 if (n & STS_SLI)
333 DBG("-- suspend --\n");
334
335 if (n & STS_PCI) {
336 DBG("-- portchange --\n");
337 bit = (readl(&udc->portsc) >> 26) & 3;
338 if (bit == 2) {
339 controller.gadget.speed = USB_SPEED_HIGH;
340 for (i = 1; i < NUM_ENDPOINTS && n; i++)
Marek Vasut532d8462013-07-10 03:16:29 +0200341 if (controller.ep[i].desc)
342 controller.ep[i].ep.maxpacket = 512;
Lei Wen26cc5122011-10-05 08:11:40 -0700343 } else {
344 controller.gadget.speed = USB_SPEED_FULL;
345 }
346 }
347
348 if (n & STS_UEI)
349 printf("<UEI %x>\n", readl(&udc->epcomp));
350
351 if ((n & STS_UI) || (n & STS_UEI)) {
352 n = readl(&udc->epstat);
353 if (n & EPT_RX(0))
354 handle_setup();
355
356 n = readl(&udc->epcomp);
357 if (n != 0)
358 writel(n, &udc->epcomp);
359
360 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut532d8462013-07-10 03:16:29 +0200361 if (controller.ep[i].desc) {
362 num = controller.ep[i].desc->bEndpointAddress
Lei Wen26cc5122011-10-05 08:11:40 -0700363 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut532d8462013-07-10 03:16:29 +0200364 in = (controller.ep[i].desc->bEndpointAddress
Lei Wen26cc5122011-10-05 08:11:40 -0700365 & USB_DIR_IN) != 0;
366 bit = (in) ? EPT_TX(num) : EPT_RX(num);
367 if (n & bit)
Marek Vasut532d8462013-07-10 03:16:29 +0200368 handle_ep_complete(&controller.ep[i]);
Lei Wen26cc5122011-10-05 08:11:40 -0700369 }
370 }
371 }
372}
373
374int usb_gadget_handle_interrupts(void)
375{
376 u32 value;
Marek Vasutbe7ed252013-07-10 03:16:32 +0200377 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700378
379 value = readl(&udc->usbsts);
380 if (value)
381 udc_irq();
382
383 return value;
384}
385
386static int mv_pullup(struct usb_gadget *gadget, int is_on)
387{
Marek Vasutbe7ed252013-07-10 03:16:32 +0200388 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700389 if (is_on) {
390 /* RESET */
391 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
392 udelay(200);
393
394 writel((unsigned) epts, &udc->epinitaddr);
395
396 /* select DEVICE mode */
397 writel(USBMODE_DEVICE, &udc->usbmode);
398
399 writel(0xffffffff, &udc->epflush);
400
401 /* Turn on the USB connection by enabling the pullup resistor */
402 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
403 } else {
404 stop_activity();
405 writel(USBCMD_FS2, &udc->usbcmd);
406 udelay(800);
407 if (controller.driver)
408 controller.driver->disconnect(gadget);
409 }
410
411 return 0;
412}
413
414void udc_disconnect(void)
415{
Marek Vasutbe7ed252013-07-10 03:16:32 +0200416 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wen26cc5122011-10-05 08:11:40 -0700417 /* disable pullup */
418 stop_activity();
419 writel(USBCMD_FS2, &udc->usbcmd);
420 udelay(800);
421 if (controller.driver)
422 controller.driver->disconnect(&controller.gadget);
423}
424
425static int mvudc_probe(void)
426{
427 struct ept_queue_head *head;
428 int i;
Marek Vasutf6463172013-07-10 03:16:34 +0200429 const int num = 2 * NUM_ENDPOINTS;
Lei Wen26cc5122011-10-05 08:11:40 -0700430
Marek Vasutf6463172013-07-10 03:16:34 +0200431 epts = memalign(PAGE_SIZE, num * sizeof(struct ept_queue_head));
432 memset(epts, 0, num * sizeof(struct ept_queue_head));
Lei Wen26cc5122011-10-05 08:11:40 -0700433 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
434 /*
435 * For item0 and item1, they are served as ep0
436 * out&in seperately
437 */
438 head = epts + i;
439 if (i < 2)
440 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
441 | CONFIG_ZLT | CONFIG_IOS;
442 else
443 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
444 | CONFIG_ZLT;
445 head->next = TERMINATE;
446 head->info = 0;
447
448 items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
449 }
450
451 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasut2ea4b442013-07-10 03:16:30 +0200452
453 /* Init EP 0 */
454 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut532d8462013-07-10 03:16:29 +0200455 controller.ep[0].desc = &ep0_in_desc;
Marek Vasut2ea4b442013-07-10 03:16:30 +0200456 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wen26cc5122011-10-05 08:11:40 -0700457 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasut2ea4b442013-07-10 03:16:30 +0200458
459 /* Init EP 1..n */
460 for (i = 1; i < NUM_ENDPOINTS; i++) {
461 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
462 sizeof(*mv_ep_init));
463 list_add_tail(&controller.ep[i].ep.ep_list,
464 &controller.gadget.ep_list);
Lei Wen26cc5122011-10-05 08:11:40 -0700465 }
Marek Vasut2ea4b442013-07-10 03:16:30 +0200466
Lei Wen26cc5122011-10-05 08:11:40 -0700467 return 0;
468}
469
470int usb_gadget_register_driver(struct usb_gadget_driver *driver)
471{
Marek Vasutbe7ed252013-07-10 03:16:32 +0200472 struct mv_udc *udc;
473 int ret;
Lei Wen26cc5122011-10-05 08:11:40 -0700474
Marek Vasutd7663032013-07-10 03:16:33 +0200475 if (!driver)
Lei Wen26cc5122011-10-05 08:11:40 -0700476 return -EINVAL;
Marek Vasutd7663032013-07-10 03:16:33 +0200477 if (!driver->bind || !driver->setup || !driver->disconnect)
478 return -EINVAL;
479 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
480 return -EINVAL;
Lei Wen26cc5122011-10-05 08:11:40 -0700481
Marek Vasutbe7ed252013-07-10 03:16:32 +0200482 ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
483 if (ret)
484 return ret;
485
486 ret = mvudc_probe();
487 if (!ret) {
488 udc = (struct mv_udc *)controller.ctrl->hcor;
489
Lei Wen26cc5122011-10-05 08:11:40 -0700490 /* select ULPI phy */
491 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
492 }
Marek Vasutbe7ed252013-07-10 03:16:32 +0200493
494 ret = driver->bind(&controller.gadget);
495 if (ret) {
496 DBG("driver->bind() returned %d\n", ret);
497 return ret;
Lei Wen26cc5122011-10-05 08:11:40 -0700498 }
499 controller.driver = driver;
500
501 return 0;
502}
503
504int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
505{
506 return 0;
507}