2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
5 * SPDX-License-Identifier: GPL-2.0+
7 * Back ported to the 8xx platform (from the 8260 platform) by
8 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
17 #include <linux/types.h>
18 #include <usb/mv_udc.h>
20 #if CONFIG_USB_MAX_CONTROLLER_COUNT > 1
21 #error This driver only supports one single controller.
25 #define DBG(x...) do {} while (0)
27 #define DBG(x...) printf(x)
28 static const char *reqname(unsigned 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*";
46 #define PAGE_SIZE 4096
48 static 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,
55 static 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,
62 static int mv_pullup(struct usb_gadget *gadget, int is_on);
63 static int mv_ep_enable(struct usb_ep *ep,
64 const struct usb_endpoint_descriptor *desc);
65 static int mv_ep_disable(struct usb_ep *ep);
66 static int mv_ep_queue(struct usb_ep *ep,
67 struct usb_request *req, gfp_t gfp_flags);
68 static struct usb_request *
69 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
70 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
72 static struct usb_gadget_ops mv_udc_ops = {
76 static struct usb_ep_ops mv_ep_ops = {
77 .enable = mv_ep_enable,
78 .disable = mv_ep_disable,
80 .alloc_request = mv_ep_alloc_request,
81 .free_request = mv_ep_free_request,
84 /* Init values for USB endpoints. */
85 static const struct usb_ep mv_ep_init[2] = {
98 static struct mv_drv controller = {
105 static struct usb_request *
106 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
108 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
112 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
117 static void ep_enable(int num, int in)
119 struct ept_queue_head *head;
120 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
122 head = controller.epts + 2*num + in;
124 n = readl(&udc->epctrl[num]);
126 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
128 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
131 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
132 writel(n, &udc->epctrl[num]);
135 static int mv_ep_enable(struct usb_ep *ep,
136 const struct usb_endpoint_descriptor *desc)
138 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
140 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
141 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
147 static int mv_ep_disable(struct usb_ep *ep)
152 static int mv_ep_queue(struct usb_ep *ep,
153 struct usb_request *req, gfp_t gfp_flags)
155 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
156 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
157 struct ept_queue_item *item;
158 struct ept_queue_head *head;
160 int bit, num, len, in;
161 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
162 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
163 item = controller.items[2 * num + in];
164 head = controller.epts + 2 * num + in;
165 phys = (unsigned)req->buf;
168 item->next = TERMINATE;
169 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
171 item->page1 = (phys & 0xfffff000) + 0x1000;
173 head->next = (unsigned) item;
176 DBG("ept%d %s queue len %x, buffer %x\n",
177 num, in ? "in" : "out", len, phys);
184 flush_cache(phys, len);
185 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
186 writel(bit, &udc->epprime);
191 static void handle_ep_complete(struct mv_ep *ep)
193 struct ept_queue_item *item;
195 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
196 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
198 ep->desc = &ep0_out_desc;
199 item = controller.items[2 * num + in];
201 if (item->info & 0xff)
202 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
203 num, in ? "in" : "out", item->info, item->page0);
205 len = (item->info >> 16) & 0x7fff;
206 ep->req.length -= len;
207 DBG("ept%d %s complete %x\n",
208 num, in ? "in" : "out", len);
209 ep->req.complete(&ep->ep, &ep->req);
212 usb_ep_queue(&ep->ep, &ep->req, 0);
213 ep->desc = &ep0_in_desc;
217 #define SETUP(type, request) (((type) << 8) | (request))
219 static void handle_setup(void)
221 struct usb_request *req = &controller.ep[0].req;
222 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
223 struct ept_queue_head *head;
224 struct usb_ctrlrequest r;
226 int num, in, _num, _in, i;
228 head = controller.epts + 2 * 0 + 0;
230 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
231 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
232 writel(EPT_RX(0), &udc->epstat);
233 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
234 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
236 switch (SETUP(r.bRequestType, r.bRequest)) {
237 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
238 _num = r.wIndex & 15;
239 _in = !!(r.wIndex & 0x80);
241 if ((r.wValue == 0) && (r.wLength == 0)) {
243 for (i = 0; i < NUM_ENDPOINTS; i++) {
244 if (!controller.ep[i].desc)
246 num = controller.ep[i].desc->bEndpointAddress
247 & USB_ENDPOINT_NUMBER_MASK;
248 in = (controller.ep[i].desc->bEndpointAddress
250 if ((num == _num) && (in == _in)) {
252 usb_ep_queue(controller.gadget.ep0,
260 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
262 * write address delayed (will take effect
263 * after the next IN txn)
265 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
267 usb_ep_queue(controller.gadget.ep0, req, 0);
270 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
272 buf = (char *)req->buf;
273 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
275 usb_ep_queue(controller.gadget.ep0, req, 0);
278 /* pass request up to the gadget driver */
279 if (controller.driver)
280 status = controller.driver->setup(&controller.gadget, &r);
286 DBG("STALL reqname %s type %x value %x, index %x\n",
287 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
288 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
291 static void stop_activity(void)
294 struct ept_queue_head *head;
295 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
296 writel(readl(&udc->epcomp), &udc->epcomp);
297 writel(readl(&udc->epstat), &udc->epstat);
298 writel(0xffffffff, &udc->epflush);
300 /* error out any pending reqs */
301 for (i = 0; i < NUM_ENDPOINTS; i++) {
303 writel(0, &udc->epctrl[i]);
304 if (controller.ep[i].desc) {
305 num = controller.ep[i].desc->bEndpointAddress
306 & USB_ENDPOINT_NUMBER_MASK;
307 in = (controller.ep[i].desc->bEndpointAddress
309 head = controller.epts + (num * 2) + (in);
310 head->info = INFO_ACTIVE;
317 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
318 unsigned n = readl(&udc->usbsts);
319 writel(n, &udc->usbsts);
322 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
327 DBG("-- reset --\n");
331 DBG("-- suspend --\n");
334 DBG("-- portchange --\n");
335 bit = (readl(&udc->portsc) >> 26) & 3;
337 controller.gadget.speed = USB_SPEED_HIGH;
338 for (i = 1; i < NUM_ENDPOINTS && n; i++)
339 if (controller.ep[i].desc)
340 controller.ep[i].ep.maxpacket = 512;
342 controller.gadget.speed = USB_SPEED_FULL;
347 printf("<UEI %x>\n", readl(&udc->epcomp));
349 if ((n & STS_UI) || (n & STS_UEI)) {
350 n = readl(&udc->epstat);
354 n = readl(&udc->epcomp);
356 writel(n, &udc->epcomp);
358 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
359 if (controller.ep[i].desc) {
360 num = controller.ep[i].desc->bEndpointAddress
361 & USB_ENDPOINT_NUMBER_MASK;
362 in = (controller.ep[i].desc->bEndpointAddress
364 bit = (in) ? EPT_TX(num) : EPT_RX(num);
366 handle_ep_complete(&controller.ep[i]);
372 int usb_gadget_handle_interrupts(void)
375 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
377 value = readl(&udc->usbsts);
384 static int mv_pullup(struct usb_gadget *gadget, int is_on)
386 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
389 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
392 writel((unsigned) controller.epts, &udc->epinitaddr);
394 /* select DEVICE mode */
395 writel(USBMODE_DEVICE, &udc->usbmode);
397 writel(0xffffffff, &udc->epflush);
399 /* Turn on the USB connection by enabling the pullup resistor */
400 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
403 writel(USBCMD_FS2, &udc->usbcmd);
405 if (controller.driver)
406 controller.driver->disconnect(gadget);
412 void udc_disconnect(void)
414 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
417 writel(USBCMD_FS2, &udc->usbcmd);
419 if (controller.driver)
420 controller.driver->disconnect(&controller.gadget);
423 static int mvudc_probe(void)
425 struct ept_queue_head *head;
427 const int num = 2 * NUM_ENDPOINTS;
429 controller.epts = memalign(PAGE_SIZE,
430 num * sizeof(struct ept_queue_head));
431 memset(controller.epts, 0, num * sizeof(struct ept_queue_head));
432 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
434 * For item0 and item1, they are served as ep0
437 head = controller.epts + i;
439 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
440 | CONFIG_ZLT | CONFIG_IOS;
442 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
444 head->next = TERMINATE;
447 controller.items[i] = memalign(PAGE_SIZE,
448 sizeof(struct ept_queue_item));
451 INIT_LIST_HEAD(&controller.gadget.ep_list);
454 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
455 controller.ep[0].desc = &ep0_in_desc;
456 controller.gadget.ep0 = &controller.ep[0].ep;
457 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
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);
470 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
477 if (!driver->bind || !driver->setup || !driver->disconnect)
479 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
482 ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
488 udc = (struct mv_udc *)controller.ctrl->hcor;
490 /* select ULPI phy */
491 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
494 ret = driver->bind(&controller.gadget);
496 DBG("driver->bind() returned %d\n", ret);
499 controller.driver = driver;
504 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)