blob: bbf4418a2e540dc51fd41fe1f8933ca23e083d29 [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
Lei Wena91ee2a2011-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
Lei Wena91ee2a2011-10-05 08:11:40 -070046static struct usb_endpoint_descriptor ep0_out_desc = {
47 .bLength = sizeof(struct usb_endpoint_descriptor),
48 .bDescriptorType = USB_DT_ENDPOINT,
49 .bEndpointAddress = 0,
50 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
51};
52
53static struct usb_endpoint_descriptor ep0_in_desc = {
54 .bLength = sizeof(struct usb_endpoint_descriptor),
55 .bDescriptorType = USB_DT_ENDPOINT,
56 .bEndpointAddress = USB_DIR_IN,
57 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
58};
59
Lei Wena91ee2a2011-10-05 08:11:40 -070060static int mv_pullup(struct usb_gadget *gadget, int is_on);
61static int mv_ep_enable(struct usb_ep *ep,
62 const struct usb_endpoint_descriptor *desc);
63static int mv_ep_disable(struct usb_ep *ep);
64static int mv_ep_queue(struct usb_ep *ep,
65 struct usb_request *req, gfp_t gfp_flags);
66static struct usb_request *
67mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
68static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
69
70static struct usb_gadget_ops mv_udc_ops = {
71 .pullup = mv_pullup,
72};
73
74static struct usb_ep_ops mv_ep_ops = {
75 .enable = mv_ep_enable,
76 .disable = mv_ep_disable,
77 .queue = mv_ep_queue,
78 .alloc_request = mv_ep_alloc_request,
79 .free_request = mv_ep_free_request,
80};
81
Marek Vasute1263412013-07-10 03:16:30 +020082/* Init values for USB endpoints. */
83static const struct usb_ep mv_ep_init[2] = {
84 [0] = { /* EP 0 */
85 .maxpacket = 64,
86 .name = "ep0",
87 .ops = &mv_ep_ops,
88 },
89 [1] = { /* EP 1..n */
90 .maxpacket = 512,
91 .name = "ep-",
92 .ops = &mv_ep_ops,
93 },
94};
95
Lei Wena91ee2a2011-10-05 08:11:40 -070096static struct mv_drv controller = {
Marek Vasutbe0b0e72013-07-10 03:16:35 +020097 .gadget = {
98 .name = "mv_udc",
99 .ops = &mv_udc_ops,
Lei Wena91ee2a2011-10-05 08:11:40 -0700100 },
101};
102
103static struct usb_request *
104mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
105{
106 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
107 return &mv_ep->req;
108}
109
110static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
111{
112 return;
113}
114
115static void ep_enable(int num, int in)
116{
117 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200118 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700119 unsigned n;
Marek Vasut393004e2013-07-10 03:16:36 +0200120 head = controller.epts + 2*num + in;
Lei Wena91ee2a2011-10-05 08:11:40 -0700121
122 n = readl(&udc->epctrl[num]);
123 if (in)
124 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
125 else
126 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
127
128 if (num != 0)
129 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
130 writel(n, &udc->epctrl[num]);
131}
132
133static int mv_ep_enable(struct usb_ep *ep,
134 const struct usb_endpoint_descriptor *desc)
135{
136 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
137 int num, in;
138 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
139 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
140 ep_enable(num, in);
141 mv_ep->desc = desc;
142 return 0;
143}
144
145static int mv_ep_disable(struct usb_ep *ep)
146{
147 return 0;
148}
149
150static int mv_ep_queue(struct usb_ep *ep,
151 struct usb_request *req, gfp_t gfp_flags)
152{
153 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
Marek Vasut1f4b4912013-07-10 03:16:32 +0200154 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700155 struct ept_queue_item *item;
156 struct ept_queue_head *head;
157 unsigned phys;
158 int bit, num, len, in;
159 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
160 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
Marek Vasut393004e2013-07-10 03:16:36 +0200161 item = controller.items[2 * num + in];
162 head = controller.epts + 2 * num + in;
Lei Wena91ee2a2011-10-05 08:11:40 -0700163 phys = (unsigned)req->buf;
164 len = req->length;
165
166 item->next = TERMINATE;
167 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
168 item->page0 = phys;
169 item->page1 = (phys & 0xfffff000) + 0x1000;
170
171 head->next = (unsigned) item;
172 head->info = 0;
173
174 DBG("ept%d %s queue len %x, buffer %x\n",
175 num, in ? "in" : "out", len, phys);
176
177 if (in)
178 bit = EPT_TX(num);
179 else
180 bit = EPT_RX(num);
181
182 flush_cache(phys, len);
183 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
184 writel(bit, &udc->epprime);
185
186 return 0;
187}
188
189static void handle_ep_complete(struct mv_ep *ep)
190{
191 struct ept_queue_item *item;
192 int num, in, len;
193 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
194 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
195 if (num == 0)
196 ep->desc = &ep0_out_desc;
Marek Vasut393004e2013-07-10 03:16:36 +0200197 item = controller.items[2 * num + in];
Lei Wena91ee2a2011-10-05 08:11:40 -0700198
199 if (item->info & 0xff)
200 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
201 num, in ? "in" : "out", item->info, item->page0);
202
203 len = (item->info >> 16) & 0x7fff;
204 ep->req.length -= len;
205 DBG("ept%d %s complete %x\n",
206 num, in ? "in" : "out", len);
207 ep->req.complete(&ep->ep, &ep->req);
208 if (num == 0) {
209 ep->req.length = 0;
210 usb_ep_queue(&ep->ep, &ep->req, 0);
211 ep->desc = &ep0_in_desc;
212 }
213}
214
215#define SETUP(type, request) (((type) << 8) | (request))
216
217static void handle_setup(void)
218{
Marek Vasut98377322013-07-10 03:16:29 +0200219 struct usb_request *req = &controller.ep[0].req;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200220 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700221 struct ept_queue_head *head;
222 struct usb_ctrlrequest r;
223 int status = 0;
224 int num, in, _num, _in, i;
225 char *buf;
Marek Vasut393004e2013-07-10 03:16:36 +0200226 head = controller.epts + 2 * 0 + 0;
Lei Wena91ee2a2011-10-05 08:11:40 -0700227
228 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
229 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
230 writel(EPT_RX(0), &udc->epstat);
231 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
232 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
233
234 switch (SETUP(r.bRequestType, r.bRequest)) {
235 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
236 _num = r.wIndex & 15;
237 _in = !!(r.wIndex & 0x80);
238
239 if ((r.wValue == 0) && (r.wLength == 0)) {
240 req->length = 0;
241 for (i = 0; i < NUM_ENDPOINTS; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200242 if (!controller.ep[i].desc)
Lei Wena91ee2a2011-10-05 08:11:40 -0700243 continue;
Marek Vasut98377322013-07-10 03:16:29 +0200244 num = controller.ep[i].desc->bEndpointAddress
245 & USB_ENDPOINT_NUMBER_MASK;
246 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700247 & USB_DIR_IN) != 0;
248 if ((num == _num) && (in == _in)) {
249 ep_enable(num, in);
250 usb_ep_queue(controller.gadget.ep0,
251 req, 0);
252 break;
253 }
254 }
255 }
256 return;
257
258 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
259 /*
260 * write address delayed (will take effect
261 * after the next IN txn)
262 */
263 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
264 req->length = 0;
265 usb_ep_queue(controller.gadget.ep0, req, 0);
266 return;
267
268 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
269 req->length = 2;
270 buf = (char *)req->buf;
271 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
272 buf[1] = 0;
273 usb_ep_queue(controller.gadget.ep0, req, 0);
274 return;
275 }
276 /* pass request up to the gadget driver */
277 if (controller.driver)
278 status = controller.driver->setup(&controller.gadget, &r);
279 else
280 status = -ENODEV;
281
282 if (!status)
283 return;
284 DBG("STALL reqname %s type %x value %x, index %x\n",
285 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
286 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
287}
288
289static void stop_activity(void)
290{
291 int i, num, in;
292 struct ept_queue_head *head;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200293 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700294 writel(readl(&udc->epcomp), &udc->epcomp);
295 writel(readl(&udc->epstat), &udc->epstat);
296 writel(0xffffffff, &udc->epflush);
297
298 /* error out any pending reqs */
299 for (i = 0; i < NUM_ENDPOINTS; i++) {
300 if (i != 0)
301 writel(0, &udc->epctrl[i]);
Marek Vasut98377322013-07-10 03:16:29 +0200302 if (controller.ep[i].desc) {
303 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700304 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200305 in = (controller.ep[i].desc->bEndpointAddress
306 & USB_DIR_IN) != 0;
Marek Vasut393004e2013-07-10 03:16:36 +0200307 head = controller.epts + (num * 2) + (in);
Lei Wena91ee2a2011-10-05 08:11:40 -0700308 head->info = INFO_ACTIVE;
309 }
310 }
311}
312
313void udc_irq(void)
314{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200315 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700316 unsigned n = readl(&udc->usbsts);
317 writel(n, &udc->usbsts);
318 int bit, i, num, in;
319
320 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
321 if (n == 0)
322 return;
323
324 if (n & STS_URI) {
325 DBG("-- reset --\n");
326 stop_activity();
327 }
328 if (n & STS_SLI)
329 DBG("-- suspend --\n");
330
331 if (n & STS_PCI) {
332 DBG("-- portchange --\n");
333 bit = (readl(&udc->portsc) >> 26) & 3;
334 if (bit == 2) {
335 controller.gadget.speed = USB_SPEED_HIGH;
336 for (i = 1; i < NUM_ENDPOINTS && n; i++)
Marek Vasut98377322013-07-10 03:16:29 +0200337 if (controller.ep[i].desc)
338 controller.ep[i].ep.maxpacket = 512;
Lei Wena91ee2a2011-10-05 08:11:40 -0700339 } else {
340 controller.gadget.speed = USB_SPEED_FULL;
341 }
342 }
343
344 if (n & STS_UEI)
345 printf("<UEI %x>\n", readl(&udc->epcomp));
346
347 if ((n & STS_UI) || (n & STS_UEI)) {
348 n = readl(&udc->epstat);
349 if (n & EPT_RX(0))
350 handle_setup();
351
352 n = readl(&udc->epcomp);
353 if (n != 0)
354 writel(n, &udc->epcomp);
355
356 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
Marek Vasut98377322013-07-10 03:16:29 +0200357 if (controller.ep[i].desc) {
358 num = controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700359 & USB_ENDPOINT_NUMBER_MASK;
Marek Vasut98377322013-07-10 03:16:29 +0200360 in = (controller.ep[i].desc->bEndpointAddress
Lei Wena91ee2a2011-10-05 08:11:40 -0700361 & USB_DIR_IN) != 0;
362 bit = (in) ? EPT_TX(num) : EPT_RX(num);
363 if (n & bit)
Marek Vasut98377322013-07-10 03:16:29 +0200364 handle_ep_complete(&controller.ep[i]);
Lei Wena91ee2a2011-10-05 08:11:40 -0700365 }
366 }
367 }
368}
369
370int usb_gadget_handle_interrupts(void)
371{
372 u32 value;
Marek Vasut1f4b4912013-07-10 03:16:32 +0200373 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700374
375 value = readl(&udc->usbsts);
376 if (value)
377 udc_irq();
378
379 return value;
380}
381
382static int mv_pullup(struct usb_gadget *gadget, int is_on)
383{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200384 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700385 if (is_on) {
386 /* RESET */
387 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
388 udelay(200);
389
Marek Vasut393004e2013-07-10 03:16:36 +0200390 writel((unsigned) controller.epts, &udc->epinitaddr);
Lei Wena91ee2a2011-10-05 08:11:40 -0700391
392 /* select DEVICE mode */
393 writel(USBMODE_DEVICE, &udc->usbmode);
394
395 writel(0xffffffff, &udc->epflush);
396
397 /* Turn on the USB connection by enabling the pullup resistor */
398 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
399 } else {
400 stop_activity();
401 writel(USBCMD_FS2, &udc->usbcmd);
402 udelay(800);
403 if (controller.driver)
404 controller.driver->disconnect(gadget);
405 }
406
407 return 0;
408}
409
410void udc_disconnect(void)
411{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200412 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
Lei Wena91ee2a2011-10-05 08:11:40 -0700413 /* disable pullup */
414 stop_activity();
415 writel(USBCMD_FS2, &udc->usbcmd);
416 udelay(800);
417 if (controller.driver)
418 controller.driver->disconnect(&controller.gadget);
419}
420
421static int mvudc_probe(void)
422{
423 struct ept_queue_head *head;
424 int i;
Marek Vasut456707d2013-07-10 03:16:37 +0200425
Marek Vasut806d76c2013-07-10 03:16:34 +0200426 const int num = 2 * NUM_ENDPOINTS;
Lei Wena91ee2a2011-10-05 08:11:40 -0700427
Marek Vasut456707d2013-07-10 03:16:37 +0200428 const int eplist_min_align = 4096;
429 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
430 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
431 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
432
433 /* The QH list must be aligned to 4096 bytes. */
434 controller.epts = memalign(eplist_align, eplist_sz);
435 if (!controller.epts)
436 return -ENOMEM;
437 memset(controller.epts, 0, eplist_sz);
438
Lei Wena91ee2a2011-10-05 08:11:40 -0700439 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
440 /*
Marek Vasut456707d2013-07-10 03:16:37 +0200441 * Configure QH for each endpoint. The structure of the QH list
442 * is such that each two subsequent fields, N and N+1 where N is
443 * even, in the QH list represent QH for one endpoint. The Nth
444 * entry represents OUT configuration and the N+1th entry does
445 * represent IN configuration of the endpoint.
Lei Wena91ee2a2011-10-05 08:11:40 -0700446 */
Marek Vasut393004e2013-07-10 03:16:36 +0200447 head = controller.epts + i;
Lei Wena91ee2a2011-10-05 08:11:40 -0700448 if (i < 2)
449 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
450 | CONFIG_ZLT | CONFIG_IOS;
451 else
452 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
453 | CONFIG_ZLT;
454 head->next = TERMINATE;
455 head->info = 0;
456
Marek Vasut456707d2013-07-10 03:16:37 +0200457 controller.items[i] = memalign(roundup(32, ARCH_DMA_MINALIGN),
Marek Vasut393004e2013-07-10 03:16:36 +0200458 sizeof(struct ept_queue_item));
Lei Wena91ee2a2011-10-05 08:11:40 -0700459 }
460
461 INIT_LIST_HEAD(&controller.gadget.ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200462
463 /* Init EP 0 */
464 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
Marek Vasut98377322013-07-10 03:16:29 +0200465 controller.ep[0].desc = &ep0_in_desc;
Marek Vasute1263412013-07-10 03:16:30 +0200466 controller.gadget.ep0 = &controller.ep[0].ep;
Lei Wena91ee2a2011-10-05 08:11:40 -0700467 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
Marek Vasute1263412013-07-10 03:16:30 +0200468
469 /* Init EP 1..n */
470 for (i = 1; i < NUM_ENDPOINTS; i++) {
471 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
472 sizeof(*mv_ep_init));
473 list_add_tail(&controller.ep[i].ep.ep_list,
474 &controller.gadget.ep_list);
Lei Wena91ee2a2011-10-05 08:11:40 -0700475 }
Marek Vasute1263412013-07-10 03:16:30 +0200476
Lei Wena91ee2a2011-10-05 08:11:40 -0700477 return 0;
478}
479
480int usb_gadget_register_driver(struct usb_gadget_driver *driver)
481{
Marek Vasut1f4b4912013-07-10 03:16:32 +0200482 struct mv_udc *udc;
483 int ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700484
Marek Vasutfe67fe72013-07-10 03:16:33 +0200485 if (!driver)
486 return -EINVAL;
487 if (!driver->bind || !driver->setup || !driver->disconnect)
488 return -EINVAL;
489 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
Lei Wena91ee2a2011-10-05 08:11:40 -0700490 return -EINVAL;
Lei Wena91ee2a2011-10-05 08:11:40 -0700491
Marek Vasut1f4b4912013-07-10 03:16:32 +0200492 ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
493 if (ret)
494 return ret;
495
496 ret = mvudc_probe();
497 if (!ret) {
498 udc = (struct mv_udc *)controller.ctrl->hcor;
499
Lei Wena91ee2a2011-10-05 08:11:40 -0700500 /* select ULPI phy */
501 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
502 }
Marek Vasut1f4b4912013-07-10 03:16:32 +0200503
504 ret = driver->bind(&controller.gadget);
505 if (ret) {
506 DBG("driver->bind() returned %d\n", ret);
507 return ret;
Lei Wena91ee2a2011-10-05 08:11:40 -0700508 }
509 controller.driver = driver;
510
511 return 0;
512}
513
514int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
515{
516 return 0;
517}